ad7606_ring.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2011-2012 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2.
  5. *
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/gpio.h>
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/buffer.h>
  14. #include <linux/iio/trigger_consumer.h>
  15. #include <linux/iio/triggered_buffer.h>
  16. #include "ad7606.h"
  17. /**
  18. * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
  19. *
  20. **/
  21. static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
  22. {
  23. struct iio_poll_func *pf = p;
  24. struct ad7606_state *st = iio_priv(pf->indio_dev);
  25. gpio_set_value(st->pdata->gpio_convst, 1);
  26. return IRQ_HANDLED;
  27. }
  28. /**
  29. * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
  30. * @work_s: the work struct through which this was scheduled
  31. *
  32. * Currently there is no option in this driver to disable the saving of
  33. * timestamps within the ring.
  34. * I think the one copy of this at a time was to avoid problems if the
  35. * trigger was set far too high and the reads then locked up the computer.
  36. **/
  37. static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
  38. {
  39. struct ad7606_state *st = container_of(work_s, struct ad7606_state,
  40. poll_work);
  41. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  42. __u8 *buf;
  43. int ret;
  44. buf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
  45. if (!buf)
  46. return;
  47. if (gpio_is_valid(st->pdata->gpio_frstdata)) {
  48. ret = st->bops->read_block(st->dev, 1, buf);
  49. if (ret)
  50. goto done;
  51. if (!gpio_get_value(st->pdata->gpio_frstdata)) {
  52. /* This should never happen. However
  53. * some signal glitch caused by bad PCB desgin or
  54. * electrostatic discharge, could cause an extra read
  55. * or clock. This allows recovery.
  56. */
  57. ad7606_reset(st);
  58. goto done;
  59. }
  60. ret = st->bops->read_block(st->dev,
  61. st->chip_info->num_channels - 1, buf + 2);
  62. if (ret)
  63. goto done;
  64. } else {
  65. ret = st->bops->read_block(st->dev,
  66. st->chip_info->num_channels, buf);
  67. if (ret)
  68. goto done;
  69. }
  70. iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns());
  71. done:
  72. gpio_set_value(st->pdata->gpio_convst, 0);
  73. iio_trigger_notify_done(indio_dev->trig);
  74. kfree(buf);
  75. }
  76. int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
  77. {
  78. struct ad7606_state *st = iio_priv(indio_dev);
  79. INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
  80. return iio_triggered_buffer_setup(indio_dev,
  81. &ad7606_trigger_handler_th_bh, &ad7606_trigger_handler_th_bh,
  82. NULL);
  83. }
  84. void ad7606_ring_cleanup(struct iio_dev *indio_dev)
  85. {
  86. iio_triggered_buffer_cleanup(indio_dev);
  87. }