hid-sensor-trigger.c 6.0 KB

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