hid-sensor-trigger.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 (!atomic_read(&st->user_requested_state))
  38. return 0;
  39. if (sensor_hub_device_open(st->hsdev))
  40. return -EIO;
  41. atomic_inc(&st->data_ready);
  42. state_val = hid_sensor_get_usage_index(st->hsdev,
  43. st->power_state.report_id,
  44. st->power_state.index,
  45. HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
  46. report_val = hid_sensor_get_usage_index(st->hsdev,
  47. st->report_state.report_id,
  48. st->report_state.index,
  49. HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
  50. poll_value = hid_sensor_read_poll_value(st);
  51. } else {
  52. int val;
  53. val = atomic_dec_if_positive(&st->data_ready);
  54. if (val < 0)
  55. return 0;
  56. sensor_hub_device_close(st->hsdev);
  57. state_val = hid_sensor_get_usage_index(st->hsdev,
  58. st->power_state.report_id,
  59. st->power_state.index,
  60. HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
  61. report_val = hid_sensor_get_usage_index(st->hsdev,
  62. st->report_state.report_id,
  63. st->report_state.index,
  64. HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
  65. }
  66. if (state_val >= 0) {
  67. state_val += st->power_state.logical_minimum;
  68. sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
  69. st->power_state.index, sizeof(state_val),
  70. &state_val);
  71. }
  72. if (report_val >= 0) {
  73. report_val += st->report_state.logical_minimum;
  74. sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
  75. st->report_state.index,
  76. sizeof(report_val),
  77. &report_val);
  78. }
  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. ret = pm_runtime_put_autosuspend(&st->pdev->dev);
  97. }
  98. if (ret < 0) {
  99. if (state)
  100. pm_runtime_put_noidle(&st->pdev->dev);
  101. return ret;
  102. }
  103. return 0;
  104. #else
  105. atomic_set(&st->user_requested_state, state);
  106. return _hid_sensor_power_state(st, state);
  107. #endif
  108. }
  109. static void hid_sensor_set_power_work(struct work_struct *work)
  110. {
  111. struct hid_sensor_common *attrb = container_of(work,
  112. struct hid_sensor_common,
  113. work);
  114. if (attrb->poll_interval >= 0)
  115. sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
  116. attrb->poll.index,
  117. sizeof(attrb->poll_interval),
  118. &attrb->poll_interval);
  119. if (attrb->raw_hystersis >= 0)
  120. sensor_hub_set_feature(attrb->hsdev,
  121. attrb->sensitivity.report_id,
  122. attrb->sensitivity.index,
  123. sizeof(attrb->raw_hystersis),
  124. &attrb->raw_hystersis);
  125. _hid_sensor_power_state(attrb, true);
  126. }
  127. static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  128. bool state)
  129. {
  130. return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
  131. }
  132. void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
  133. {
  134. pm_runtime_disable(&attrb->pdev->dev);
  135. pm_runtime_set_suspended(&attrb->pdev->dev);
  136. pm_runtime_put_noidle(&attrb->pdev->dev);
  137. cancel_work_sync(&attrb->work);
  138. iio_trigger_unregister(attrb->trigger);
  139. iio_trigger_free(attrb->trigger);
  140. }
  141. EXPORT_SYMBOL(hid_sensor_remove_trigger);
  142. static const struct iio_trigger_ops hid_sensor_trigger_ops = {
  143. .owner = THIS_MODULE,
  144. .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
  145. };
  146. int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
  147. struct hid_sensor_common *attrb)
  148. {
  149. int ret;
  150. struct iio_trigger *trig;
  151. trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
  152. if (trig == NULL) {
  153. dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
  154. ret = -ENOMEM;
  155. goto error_ret;
  156. }
  157. trig->dev.parent = indio_dev->dev.parent;
  158. iio_trigger_set_drvdata(trig, attrb);
  159. trig->ops = &hid_sensor_trigger_ops;
  160. ret = iio_trigger_register(trig);
  161. if (ret) {
  162. dev_err(&indio_dev->dev, "Trigger Register Failed\n");
  163. goto error_free_trig;
  164. }
  165. attrb->trigger = trig;
  166. indio_dev->trig = iio_trigger_get(trig);
  167. ret = pm_runtime_set_active(&indio_dev->dev);
  168. if (ret)
  169. goto error_unreg_trigger;
  170. iio_device_set_drvdata(indio_dev, attrb);
  171. INIT_WORK(&attrb->work, hid_sensor_set_power_work);
  172. pm_suspend_ignore_children(&attrb->pdev->dev, true);
  173. pm_runtime_enable(&attrb->pdev->dev);
  174. /* Default to 3 seconds, but can be changed from sysfs */
  175. pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
  176. 3000);
  177. pm_runtime_use_autosuspend(&attrb->pdev->dev);
  178. return ret;
  179. error_unreg_trigger:
  180. iio_trigger_unregister(trig);
  181. error_free_trig:
  182. iio_trigger_free(trig);
  183. error_ret:
  184. return ret;
  185. }
  186. EXPORT_SYMBOL(hid_sensor_setup_trigger);
  187. static int __maybe_unused hid_sensor_suspend(struct device *dev)
  188. {
  189. struct platform_device *pdev = to_platform_device(dev);
  190. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  191. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  192. return _hid_sensor_power_state(attrb, false);
  193. }
  194. static int __maybe_unused hid_sensor_resume(struct device *dev)
  195. {
  196. struct platform_device *pdev = to_platform_device(dev);
  197. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  198. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  199. schedule_work(&attrb->work);
  200. return 0;
  201. }
  202. static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
  203. {
  204. struct platform_device *pdev = to_platform_device(dev);
  205. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  206. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  207. return _hid_sensor_power_state(attrb, true);
  208. }
  209. const struct dev_pm_ops hid_sensor_pm_ops = {
  210. SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
  211. SET_RUNTIME_PM_OPS(hid_sensor_suspend,
  212. hid_sensor_runtime_resume, NULL)
  213. };
  214. EXPORT_SYMBOL(hid_sensor_pm_ops);
  215. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  216. MODULE_DESCRIPTION("HID Sensor trigger processing");
  217. MODULE_LICENSE("GPL");