hid-sensor-trigger.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. _hid_sensor_power_state(attrb, true);
  115. }
  116. static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  117. bool state)
  118. {
  119. return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
  120. }
  121. void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
  122. {
  123. cancel_work_sync(&attrb->work);
  124. iio_trigger_unregister(attrb->trigger);
  125. iio_trigger_free(attrb->trigger);
  126. }
  127. EXPORT_SYMBOL(hid_sensor_remove_trigger);
  128. static const struct iio_trigger_ops hid_sensor_trigger_ops = {
  129. .owner = THIS_MODULE,
  130. .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
  131. };
  132. int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
  133. struct hid_sensor_common *attrb)
  134. {
  135. int ret;
  136. struct iio_trigger *trig;
  137. trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
  138. if (trig == NULL) {
  139. dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
  140. ret = -ENOMEM;
  141. goto error_ret;
  142. }
  143. trig->dev.parent = indio_dev->dev.parent;
  144. iio_trigger_set_drvdata(trig, attrb);
  145. trig->ops = &hid_sensor_trigger_ops;
  146. ret = iio_trigger_register(trig);
  147. if (ret) {
  148. dev_err(&indio_dev->dev, "Trigger Register Failed\n");
  149. goto error_free_trig;
  150. }
  151. attrb->trigger = trig;
  152. indio_dev->trig = iio_trigger_get(trig);
  153. ret = pm_runtime_set_active(&indio_dev->dev);
  154. if (ret)
  155. goto error_unreg_trigger;
  156. iio_device_set_drvdata(indio_dev, attrb);
  157. INIT_WORK(&attrb->work, hid_sensor_set_power_work);
  158. pm_suspend_ignore_children(&attrb->pdev->dev, true);
  159. pm_runtime_enable(&attrb->pdev->dev);
  160. /* Default to 3 seconds, but can be changed from sysfs */
  161. pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
  162. 3000);
  163. pm_runtime_use_autosuspend(&attrb->pdev->dev);
  164. return ret;
  165. error_unreg_trigger:
  166. iio_trigger_unregister(trig);
  167. error_free_trig:
  168. iio_trigger_free(trig);
  169. error_ret:
  170. return ret;
  171. }
  172. EXPORT_SYMBOL(hid_sensor_setup_trigger);
  173. static int __maybe_unused hid_sensor_suspend(struct device *dev)
  174. {
  175. struct platform_device *pdev = to_platform_device(dev);
  176. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  177. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  178. return _hid_sensor_power_state(attrb, false);
  179. }
  180. static int __maybe_unused hid_sensor_resume(struct device *dev)
  181. {
  182. struct platform_device *pdev = to_platform_device(dev);
  183. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  184. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  185. schedule_work(&attrb->work);
  186. return 0;
  187. }
  188. static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
  189. {
  190. struct platform_device *pdev = to_platform_device(dev);
  191. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  192. struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  193. return _hid_sensor_power_state(attrb, true);
  194. }
  195. const struct dev_pm_ops hid_sensor_pm_ops = {
  196. SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
  197. SET_RUNTIME_PM_OPS(hid_sensor_suspend,
  198. hid_sensor_runtime_resume, NULL)
  199. };
  200. EXPORT_SYMBOL(hid_sensor_pm_ops);
  201. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  202. MODULE_DESCRIPTION("HID Sensor trigger processing");
  203. MODULE_LICENSE("GPL");