hid-sensor-trigger.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/hid-sensor-hub.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/trigger.h>
  28. #include <linux/iio/sysfs.h>
  29. #include "hid-sensor-trigger.h"
  30. int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
  31. {
  32. int state_val;
  33. int report_val;
  34. if (state) {
  35. if (sensor_hub_device_open(st->hsdev))
  36. return -EIO;
  37. atomic_inc(&st->data_ready);
  38. state_val = hid_sensor_get_usage_index(st->hsdev,
  39. st->power_state.report_id,
  40. st->power_state.index,
  41. HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
  42. report_val = hid_sensor_get_usage_index(st->hsdev,
  43. st->report_state.report_id,
  44. st->report_state.index,
  45. HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
  46. } else {
  47. if (!atomic_dec_and_test(&st->data_ready))
  48. return 0;
  49. sensor_hub_device_close(st->hsdev);
  50. state_val = hid_sensor_get_usage_index(st->hsdev,
  51. st->power_state.report_id,
  52. st->power_state.index,
  53. HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
  54. report_val = hid_sensor_get_usage_index(st->hsdev,
  55. st->report_state.report_id,
  56. st->report_state.index,
  57. HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
  58. }
  59. if (state_val >= 0) {
  60. state_val += st->power_state.logical_minimum;
  61. sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
  62. st->power_state.index,
  63. (s32)state_val);
  64. }
  65. if (report_val >= 0) {
  66. report_val += st->report_state.logical_minimum;
  67. sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
  68. st->report_state.index,
  69. (s32)report_val);
  70. }
  71. sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
  72. st->power_state.index,
  73. &state_val);
  74. return 0;
  75. }
  76. EXPORT_SYMBOL(hid_sensor_power_state);
  77. static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
  78. bool state)
  79. {
  80. return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
  81. }
  82. void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
  83. {
  84. iio_trigger_unregister(attrb->trigger);
  85. iio_trigger_free(attrb->trigger);
  86. }
  87. EXPORT_SYMBOL(hid_sensor_remove_trigger);
  88. static const struct iio_trigger_ops hid_sensor_trigger_ops = {
  89. .owner = THIS_MODULE,
  90. .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
  91. };
  92. int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
  93. struct hid_sensor_common *attrb)
  94. {
  95. int ret;
  96. struct iio_trigger *trig;
  97. trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
  98. if (trig == NULL) {
  99. dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
  100. ret = -ENOMEM;
  101. goto error_ret;
  102. }
  103. trig->dev.parent = indio_dev->dev.parent;
  104. iio_trigger_set_drvdata(trig, attrb);
  105. trig->ops = &hid_sensor_trigger_ops;
  106. ret = iio_trigger_register(trig);
  107. if (ret) {
  108. dev_err(&indio_dev->dev, "Trigger Register Failed\n");
  109. goto error_free_trig;
  110. }
  111. indio_dev->trig = attrb->trigger = trig;
  112. return ret;
  113. error_free_trig:
  114. iio_trigger_free(trig);
  115. error_ret:
  116. return ret;
  117. }
  118. EXPORT_SYMBOL(hid_sensor_setup_trigger);
  119. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  120. MODULE_DESCRIPTION("HID Sensor trigger processing");
  121. MODULE_LICENSE("GPL");