st_sensors_trigger.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * STMicroelectronics sensors trigger library driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/trigger.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/iio/common/st_sensors.h>
  17. #include "st_sensors_core.h"
  18. /**
  19. * st_sensors_new_samples_available() - check if more samples came in
  20. * returns:
  21. * 0 - no new samples available
  22. * 1 - new samples available
  23. * negative - error or unknown
  24. */
  25. static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
  26. struct st_sensor_data *sdata)
  27. {
  28. u8 status;
  29. int ret;
  30. /* How would I know if I can't check it? */
  31. if (!sdata->sensor_settings->drdy_irq.addr_stat_drdy)
  32. return -EINVAL;
  33. /* No scan mask, no interrupt */
  34. if (!indio_dev->active_scan_mask)
  35. return 0;
  36. ret = sdata->tf->read_byte(&sdata->tb, sdata->dev,
  37. sdata->sensor_settings->drdy_irq.addr_stat_drdy,
  38. &status);
  39. if (ret < 0) {
  40. dev_err(sdata->dev,
  41. "error checking samples available\n");
  42. return ret;
  43. }
  44. /*
  45. * the lower bits of .active_scan_mask[0] is directly mapped
  46. * to the channels on the sensor: either bit 0 for
  47. * one-dimensional sensors, or e.g. x,y,z for accelerometers,
  48. * gyroscopes or magnetometers. No sensor use more than 3
  49. * channels, so cut the other status bits here.
  50. */
  51. status &= 0x07;
  52. if (status & (u8)indio_dev->active_scan_mask[0])
  53. return 1;
  54. return 0;
  55. }
  56. /**
  57. * st_sensors_irq_handler() - top half of the IRQ-based triggers
  58. * @irq: irq number
  59. * @p: private handler data
  60. */
  61. static irqreturn_t st_sensors_irq_handler(int irq, void *p)
  62. {
  63. struct iio_trigger *trig = p;
  64. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  65. struct st_sensor_data *sdata = iio_priv(indio_dev);
  66. /* Get the time stamp as close in time as possible */
  67. sdata->hw_timestamp = iio_get_time_ns(indio_dev);
  68. return IRQ_WAKE_THREAD;
  69. }
  70. /**
  71. * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
  72. * @irq: irq number
  73. * @p: private handler data
  74. */
  75. static irqreturn_t st_sensors_irq_thread(int irq, void *p)
  76. {
  77. struct iio_trigger *trig = p;
  78. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  79. struct st_sensor_data *sdata = iio_priv(indio_dev);
  80. /*
  81. * If this trigger is backed by a hardware interrupt and we have a
  82. * status register, check if this IRQ came from us. Notice that
  83. * we will process also if st_sensors_new_samples_available()
  84. * returns negative: if we can't check status, then poll
  85. * unconditionally.
  86. */
  87. if (sdata->hw_irq_trigger &&
  88. st_sensors_new_samples_available(indio_dev, sdata)) {
  89. iio_trigger_poll_chained(p);
  90. } else {
  91. dev_dbg(sdata->dev, "spurious IRQ\n");
  92. return IRQ_NONE;
  93. }
  94. /*
  95. * If we have proper level IRQs the handler will be re-entered if
  96. * the line is still active, so return here and come back in through
  97. * the top half if need be.
  98. */
  99. if (!sdata->edge_irq)
  100. return IRQ_HANDLED;
  101. /*
  102. * If we are using egde IRQs, new samples arrived while processing
  103. * the IRQ and those may be missed unless we pick them here, so poll
  104. * again. If the sensor delivery frequency is very high, this thread
  105. * turns into a polled loop handler.
  106. */
  107. while (sdata->hw_irq_trigger &&
  108. st_sensors_new_samples_available(indio_dev, sdata)) {
  109. dev_dbg(sdata->dev, "more samples came in during polling\n");
  110. sdata->hw_timestamp = iio_get_time_ns(indio_dev);
  111. iio_trigger_poll_chained(p);
  112. }
  113. return IRQ_HANDLED;
  114. }
  115. int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  116. const struct iio_trigger_ops *trigger_ops)
  117. {
  118. int err, irq;
  119. struct st_sensor_data *sdata = iio_priv(indio_dev);
  120. unsigned long irq_trig;
  121. sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
  122. if (sdata->trig == NULL) {
  123. dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
  124. return -ENOMEM;
  125. }
  126. iio_trigger_set_drvdata(sdata->trig, indio_dev);
  127. sdata->trig->ops = trigger_ops;
  128. sdata->trig->dev.parent = sdata->dev;
  129. irq = sdata->get_irq_data_ready(indio_dev);
  130. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  131. /*
  132. * If the IRQ is triggered on falling edge, we need to mark the
  133. * interrupt as active low, if the hardware supports this.
  134. */
  135. switch(irq_trig) {
  136. case IRQF_TRIGGER_FALLING:
  137. case IRQF_TRIGGER_LOW:
  138. if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
  139. dev_err(&indio_dev->dev,
  140. "falling/low specified for IRQ "
  141. "but hardware only support rising/high: "
  142. "will request rising/high\n");
  143. if (irq_trig == IRQF_TRIGGER_FALLING)
  144. irq_trig = IRQF_TRIGGER_RISING;
  145. if (irq_trig == IRQF_TRIGGER_LOW)
  146. irq_trig = IRQF_TRIGGER_HIGH;
  147. } else {
  148. /* Set up INT active low i.e. falling edge */
  149. err = st_sensors_write_data_with_mask(indio_dev,
  150. sdata->sensor_settings->drdy_irq.addr_ihl,
  151. sdata->sensor_settings->drdy_irq.mask_ihl, 1);
  152. if (err < 0)
  153. goto iio_trigger_free;
  154. dev_info(&indio_dev->dev,
  155. "interrupts on the falling edge or "
  156. "active low level\n");
  157. }
  158. break;
  159. case IRQF_TRIGGER_RISING:
  160. dev_info(&indio_dev->dev,
  161. "interrupts on the rising edge\n");
  162. break;
  163. case IRQF_TRIGGER_HIGH:
  164. dev_info(&indio_dev->dev,
  165. "interrupts active high level\n");
  166. break;
  167. default:
  168. /* This is the most preferred mode, if possible */
  169. dev_err(&indio_dev->dev,
  170. "unsupported IRQ trigger specified (%lx), enforce "
  171. "rising edge\n", irq_trig);
  172. irq_trig = IRQF_TRIGGER_RISING;
  173. }
  174. /* Tell the interrupt handler that we're dealing with edges */
  175. if (irq_trig == IRQF_TRIGGER_FALLING ||
  176. irq_trig == IRQF_TRIGGER_RISING)
  177. sdata->edge_irq = true;
  178. else
  179. /*
  180. * If we're not using edges (i.e. level interrupts) we
  181. * just mask off the IRQ, handle one interrupt, then
  182. * if the line is still low, we return to the
  183. * interrupt handler top half again and start over.
  184. */
  185. irq_trig |= IRQF_ONESHOT;
  186. /*
  187. * If the interrupt pin is Open Drain, by definition this
  188. * means that the interrupt line may be shared with other
  189. * peripherals. But to do this we also need to have a status
  190. * register and mask to figure out if this sensor was firing
  191. * the IRQ or not, so we can tell the interrupt handle that
  192. * it was "our" interrupt.
  193. */
  194. if (sdata->int_pin_open_drain &&
  195. sdata->sensor_settings->drdy_irq.addr_stat_drdy)
  196. irq_trig |= IRQF_SHARED;
  197. err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev),
  198. st_sensors_irq_handler,
  199. st_sensors_irq_thread,
  200. irq_trig,
  201. sdata->trig->name,
  202. sdata->trig);
  203. if (err) {
  204. dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
  205. goto iio_trigger_free;
  206. }
  207. err = iio_trigger_register(sdata->trig);
  208. if (err < 0) {
  209. dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
  210. goto iio_trigger_register_error;
  211. }
  212. indio_dev->trig = iio_trigger_get(sdata->trig);
  213. return 0;
  214. iio_trigger_register_error:
  215. free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
  216. iio_trigger_free:
  217. iio_trigger_free(sdata->trig);
  218. return err;
  219. }
  220. EXPORT_SYMBOL(st_sensors_allocate_trigger);
  221. void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
  222. {
  223. struct st_sensor_data *sdata = iio_priv(indio_dev);
  224. iio_trigger_unregister(sdata->trig);
  225. free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
  226. iio_trigger_free(sdata->trig);
  227. }
  228. EXPORT_SYMBOL(st_sensors_deallocate_trigger);
  229. int st_sensors_validate_device(struct iio_trigger *trig,
  230. struct iio_dev *indio_dev)
  231. {
  232. struct iio_dev *indio = iio_trigger_get_drvdata(trig);
  233. if (indio != indio_dev)
  234. return -EINVAL;
  235. return 0;
  236. }
  237. EXPORT_SYMBOL(st_sensors_validate_device);
  238. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  239. MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
  240. MODULE_LICENSE("GPL v2");