st_sensors_buffer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * STMicroelectronics sensors buffer 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/buffer.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/iio/common/st_sensors.h>
  21. static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
  22. {
  23. int i;
  24. struct st_sensor_data *sdata = iio_priv(indio_dev);
  25. unsigned int num_data_channels = sdata->num_data_channels;
  26. for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) {
  27. const struct iio_chan_spec *channel = &indio_dev->channels[i];
  28. unsigned int bytes_to_read =
  29. DIV_ROUND_UP(channel->scan_type.realbits +
  30. channel->scan_type.shift, 8);
  31. unsigned int storage_bytes =
  32. channel->scan_type.storagebits >> 3;
  33. buf = PTR_ALIGN(buf, storage_bytes);
  34. if (sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
  35. channel->address,
  36. bytes_to_read, buf,
  37. sdata->multiread_bit) <
  38. bytes_to_read)
  39. return -EIO;
  40. /* Advance the buffer pointer */
  41. buf += storage_bytes;
  42. }
  43. return 0;
  44. }
  45. irqreturn_t st_sensors_trigger_handler(int irq, void *p)
  46. {
  47. int len;
  48. struct iio_poll_func *pf = p;
  49. struct iio_dev *indio_dev = pf->indio_dev;
  50. struct st_sensor_data *sdata = iio_priv(indio_dev);
  51. s64 timestamp;
  52. /*
  53. * If we do timetamping here, do it before reading the values, because
  54. * once we've read the values, new interrupts can occur (when using
  55. * the hardware trigger) and the hw_timestamp may get updated.
  56. * By storing it in a local variable first, we are safe.
  57. */
  58. if (iio_trigger_using_own(indio_dev))
  59. timestamp = sdata->hw_timestamp;
  60. else
  61. timestamp = iio_get_time_ns(indio_dev);
  62. len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
  63. if (len < 0)
  64. goto st_sensors_get_buffer_element_error;
  65. iio_push_to_buffers_with_timestamp(indio_dev, sdata->buffer_data,
  66. timestamp);
  67. st_sensors_get_buffer_element_error:
  68. iio_trigger_notify_done(indio_dev->trig);
  69. return IRQ_HANDLED;
  70. }
  71. EXPORT_SYMBOL(st_sensors_trigger_handler);
  72. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  73. MODULE_DESCRIPTION("STMicroelectronics ST-sensors buffer");
  74. MODULE_LICENSE("GPL v2");