hid-sensor-trigger.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/hid-sensor-hub.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/trigger.h>
  29. #include <linux/iio/sysfs.h>
  30. #include "hid-sensor-trigger.h"
  31. static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
  32. {
  33. int state_val;
  34. int report_val;
  35. s32 poll_value = 0;
  36. if (state) {
  37. if (sensor_hub_device_open(st->hsdev))
  38. return -EIO;
  39. atomic_inc(&st->data_ready);
  40. state_val = hid_sensor_get_usage_index(st->hsdev,
  41. st->power_state.report_id,
  42. st->power_state.index,
  43. HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
  44. report_val = hid_sensor_get_usage_index(st->hsdev,
  45. st->report_state.report_id,
  46. st->report_state.index,
  47. HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
  48. poll_value = hid_sensor_read_poll_value(st);
  49. } else {
  50. int val;
  51. val = atomic_dec_if_positive(&st->data_ready);
  52. if (val < 0)
  53. return 0;
  54. sensor_hub_device_close(st->hsdev);
  55. state_val = hid_sensor_get_usage_index(st->hsdev,
  56. st->power_state.report_id,
  57. st->power_state.index,
  58. HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
  59. report_val = hid_sensor_get_usage_index(st->hsdev,
  60. st->report_state.report_id,
  61. st->report_state.index,
  62. HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
  63. }
  64. if (state_val >= 0) {
  65. state_val += st->power_state.logical_minimum;
  66. sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
  67. st->power_state.index, sizeof(state_val),
  68. &state_val);
  69. }
  70. if (report_val >= 0) {
  71. report_val += st->report_state.logical_minimum;
  72. sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
  73. st->report_state.index,
  74. sizeof(report_val),
  75. &report_val);
  76. }
  77. pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
  78. st->pdev->name, state_val, report_val);
  79. sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
  80. st->power_state.index,
  81. sizeof(state_val), &state_val);
  82. if (state && poll_value)
  83. msleep_interruptible(poll_value * 2);
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(hid_sensor_power_state);
  87. int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
  88. {
  89. #ifdef CONFIG_PM
  90. int ret;
  91. atomic_set(&st->user_requested_state, state);
  92. if (state)
  93. ret = pm_runtime_get_sync(&st->pdev->dev);
  94. else {
  95. pm_runtime_mark_last_busy(&st->pdev->dev);
  96. pm_runtime_use_autosuspend(&st->pdev->dev);
  97. ret = pm_runtime_put_autosuspend(&st->pdev->dev);
  98. }
  99. if (ret < 0) {
  100. if (state)
  101. pm_runtime_put_noidle(&st->pdev->dev);
  102. return ret;
  103. }
  104. return 0;
  105. #else
  106. atomic_set(&st->user_requested_state, state);
  107. return _hid_sensor_power_state(st, state);
  108. #endif
  109. }
  110. static void hid_sensor_set_power_work(struct work_struct *work)
  111. {
  112. struct hid_sensor_common *attrb = container_of(work,
  113. struct hid_sensor_common,
  114. work);
  115. if (attrb->poll_interval >= 0)
  116. sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
  117. attrb->poll.index,
  118. sizeof(attrb->poll_interval),
  119. &attrb->poll_interval);
  120. if (attrb->raw_hystersis >= 0)
  121. sensor_hub_set_feature(attrb->hsdev,
  122. attrb->sensitivity.report_id,
  123. attrb->sensitivity.index,
  124. sizeof(attrb->raw_hystersis),
  125. &attrb->raw_hystersis);
  126. _hid_sensor_power_state(attrb, true);
  127. }
  128. static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  129. bool state)
  130. {
  131. return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
  132. }
  133. void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
  134. {
  135. cancel_work_sync(&attrb->work);
  136. iio_trigger_unregister(attrb->trigger);
  137. iio_trigger_free(attrb->trigger);
  138. }
  139. EXPORT_SYMBOL(hid_sensor_remove_trigger);
  140. static const struct iio_trigger_ops hid_sensor_trigger_ops = {
  141. .owner = THIS_MODULE,
  142. .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
  143. };
  144. int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
  145. struct hid_sensor_common *attrb)
  146. {
  147. int ret;
  148. struct iio_trigger *trig;
  149. trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
  150. if (trig == NULL) {
  151. dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
  152. ret = -ENOMEM;
  153. goto error_ret;
  154. }
  155. trig->dev.parent = indio_dev->dev.parent;
  156. iio_trigger_set_drvdata(trig, attrb);
  157. trig->ops = &hid_sensor_trigger_ops;
  158. ret = iio_trigger_register(trig);
  159. if (ret) {
  160. dev_err(&indio_dev->dev, "Trigger Register Failed\n");
  161. goto error_free_trig;
  162. }
  163. attrb->trigger = trig;
  164. indio_dev->trig = iio_trigger_get(trig);
  165. ret = pm_runtime_set_active(&indio_dev->dev);
  166. if (ret)
  167. goto error_unreg_trigger;
  168. iio_device_set_drvdata(indio_dev, attrb);
  169. INIT_WORK(&attrb->work, hid_sensor_set_power_work);
  170. pm_suspend_ignore_children(&attrb->pdev->dev, true);
  171. pm_runtime_enable(&attrb->pdev->dev);
  172. /* Default to 3 seconds, but can be changed from sysfs */
  173. pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
  174. 3000);
  175. return ret;
  176. error_unreg_trigger:
  177. iio_trigger_unregister(trig);
  178. error_free_trig:
  179. iio_trigger_free(trig);
  180. error_ret:
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(hid_sensor_setup_trigger);
  184. static int __maybe_unused hid_sensor_suspend(struct device *dev)
  185. {
  186. struct platform_device *pdev = to_platform_device(dev);
  187. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  188. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  189. return _hid_sensor_power_state(attrb, false);
  190. }
  191. static int __maybe_unused hid_sensor_resume(struct device *dev)
  192. {
  193. struct platform_device *pdev = to_platform_device(dev);
  194. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  195. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  196. schedule_work(&attrb->work);
  197. return 0;
  198. }
  199. static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
  200. {
  201. struct platform_device *pdev = to_platform_device(dev);
  202. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  203. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  204. return _hid_sensor_power_state(attrb, true);
  205. }
  206. const struct dev_pm_ops hid_sensor_pm_ops = {
  207. SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
  208. SET_RUNTIME_PM_OPS(hid_sensor_suspend,
  209. hid_sensor_runtime_resume, NULL)
  210. };
  211. EXPORT_SYMBOL(hid_sensor_pm_ops);
  212. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  213. MODULE_DESCRIPTION("HID Sensor trigger processing");
  214. MODULE_LICENSE("GPL");