hid-sensor-trigger.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. } else {
  51. int val;
  52. val = atomic_dec_if_positive(&st->data_ready);
  53. if (val < 0)
  54. return 0;
  55. sensor_hub_device_close(st->hsdev);
  56. state_val = hid_sensor_get_usage_index(st->hsdev,
  57. st->power_state.report_id,
  58. st->power_state.index,
  59. HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
  60. report_val = hid_sensor_get_usage_index(st->hsdev,
  61. st->report_state.report_id,
  62. st->report_state.index,
  63. HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
  64. }
  65. if (state_val >= 0) {
  66. state_val += st->power_state.logical_minimum;
  67. sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
  68. st->power_state.index, sizeof(state_val),
  69. &state_val);
  70. }
  71. if (report_val >= 0) {
  72. report_val += st->report_state.logical_minimum;
  73. sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
  74. st->report_state.index,
  75. sizeof(report_val),
  76. &report_val);
  77. }
  78. sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
  79. st->power_state.index,
  80. sizeof(state_val), &state_val);
  81. if (state)
  82. poll_value = hid_sensor_read_poll_value(st);
  83. if (poll_value > 0)
  84. msleep_interruptible(poll_value * 2);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL(hid_sensor_power_state);
  88. int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
  89. {
  90. #ifdef CONFIG_PM
  91. int ret;
  92. atomic_set(&st->user_requested_state, state);
  93. if (state)
  94. ret = pm_runtime_get_sync(&st->pdev->dev);
  95. else {
  96. pm_runtime_mark_last_busy(&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. _hid_sensor_power_state(attrb, true);
  116. }
  117. static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  118. bool state)
  119. {
  120. return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
  121. }
  122. void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
  123. {
  124. cancel_work_sync(&attrb->work);
  125. iio_trigger_unregister(attrb->trigger);
  126. iio_trigger_free(attrb->trigger);
  127. }
  128. EXPORT_SYMBOL(hid_sensor_remove_trigger);
  129. static const struct iio_trigger_ops hid_sensor_trigger_ops = {
  130. .owner = THIS_MODULE,
  131. .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
  132. };
  133. int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
  134. struct hid_sensor_common *attrb)
  135. {
  136. int ret;
  137. struct iio_trigger *trig;
  138. trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
  139. if (trig == NULL) {
  140. dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
  141. ret = -ENOMEM;
  142. goto error_ret;
  143. }
  144. trig->dev.parent = indio_dev->dev.parent;
  145. iio_trigger_set_drvdata(trig, attrb);
  146. trig->ops = &hid_sensor_trigger_ops;
  147. ret = iio_trigger_register(trig);
  148. if (ret) {
  149. dev_err(&indio_dev->dev, "Trigger Register Failed\n");
  150. goto error_free_trig;
  151. }
  152. attrb->trigger = trig;
  153. indio_dev->trig = iio_trigger_get(trig);
  154. ret = pm_runtime_set_active(&indio_dev->dev);
  155. if (ret)
  156. goto error_unreg_trigger;
  157. iio_device_set_drvdata(indio_dev, attrb);
  158. INIT_WORK(&attrb->work, hid_sensor_set_power_work);
  159. pm_suspend_ignore_children(&attrb->pdev->dev, true);
  160. pm_runtime_enable(&attrb->pdev->dev);
  161. /* Default to 3 seconds, but can be changed from sysfs */
  162. pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
  163. 3000);
  164. pm_runtime_use_autosuspend(&attrb->pdev->dev);
  165. return ret;
  166. error_unreg_trigger:
  167. iio_trigger_unregister(trig);
  168. error_free_trig:
  169. iio_trigger_free(trig);
  170. error_ret:
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(hid_sensor_setup_trigger);
  174. static int __maybe_unused hid_sensor_suspend(struct device *dev)
  175. {
  176. struct platform_device *pdev = to_platform_device(dev);
  177. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  178. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  179. return _hid_sensor_power_state(attrb, false);
  180. }
  181. static int __maybe_unused hid_sensor_resume(struct device *dev)
  182. {
  183. struct platform_device *pdev = to_platform_device(dev);
  184. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  185. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  186. schedule_work(&attrb->work);
  187. return 0;
  188. }
  189. static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
  190. {
  191. struct platform_device *pdev = to_platform_device(dev);
  192. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  193. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  194. return _hid_sensor_power_state(attrb, true);
  195. }
  196. const struct dev_pm_ops hid_sensor_pm_ops = {
  197. SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
  198. SET_RUNTIME_PM_OPS(hid_sensor_suspend,
  199. hid_sensor_runtime_resume, NULL)
  200. };
  201. EXPORT_SYMBOL(hid_sensor_pm_ops);
  202. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  203. MODULE_DESCRIPTION("HID Sensor trigger processing");
  204. MODULE_LICENSE("GPL");