hid-sensor-trigger.c 3.2 KB

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