PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/iio/adc/ad7476_ring.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 165 lines | 119 code | 27 blank | 19 comment | 14 complexity | 8c7379a4de81c2d507e804a3b372107d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright 2010 Analog Devices Inc.
  3. * Copyright (C) 2008 Jonathan Cameron
  4. *
  5. * Licensed under the GPL-2 or later.
  6. *
  7. * ad7476_ring.c
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/spi/spi.h>
  15. #include "../iio.h"
  16. #include "../ring_generic.h"
  17. #include "../ring_sw.h"
  18. #include "../trigger.h"
  19. #include "../sysfs.h"
  20. #include "ad7476.h"
  21. int ad7476_scan_from_ring(struct ad7476_state *st)
  22. {
  23. struct iio_ring_buffer *ring = st->indio_dev->ring;
  24. int ret;
  25. u8 *ring_data;
  26. ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
  27. GFP_KERNEL);
  28. if (ring_data == NULL) {
  29. ret = -ENOMEM;
  30. goto error_ret;
  31. }
  32. ret = ring->access->read_last(ring, ring_data);
  33. if (ret)
  34. goto error_free_ring_data;
  35. ret = (ring_data[0] << 8) | ring_data[1];
  36. error_free_ring_data:
  37. kfree(ring_data);
  38. error_ret:
  39. return ret;
  40. }
  41. /**
  42. * ad7476_ring_preenable() setup the parameters of the ring before enabling
  43. *
  44. * The complex nature of the setting of the nuber of bytes per datum is due
  45. * to this driver currently ensuring that the timestamp is stored at an 8
  46. * byte boundary.
  47. **/
  48. static int ad7476_ring_preenable(struct iio_dev *indio_dev)
  49. {
  50. struct ad7476_state *st = indio_dev->dev_data;
  51. struct iio_ring_buffer *ring = indio_dev->ring;
  52. st->d_size = ring->scan_count *
  53. st->chip_info->channel[0].scan_type.storagebits / 8;
  54. if (ring->scan_timestamp) {
  55. st->d_size += sizeof(s64);
  56. if (st->d_size % sizeof(s64))
  57. st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
  58. }
  59. if (indio_dev->ring->access->set_bytes_per_datum)
  60. indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
  61. st->d_size);
  62. return 0;
  63. }
  64. static irqreturn_t ad7476_trigger_handler(int irq, void *p)
  65. {
  66. struct iio_poll_func *pf = p;
  67. struct iio_dev *indio_dev = pf->private_data;
  68. struct ad7476_state *st = iio_dev_get_devdata(indio_dev);
  69. s64 time_ns;
  70. __u8 *rxbuf;
  71. int b_sent;
  72. rxbuf = kzalloc(st->d_size, GFP_KERNEL);
  73. if (rxbuf == NULL)
  74. return -ENOMEM;
  75. b_sent = spi_read(st->spi, rxbuf,
  76. st->chip_info->channel[0].scan_type.storagebits / 8);
  77. if (b_sent < 0)
  78. goto done;
  79. time_ns = iio_get_time_ns();
  80. if (indio_dev->ring->scan_timestamp)
  81. memcpy(rxbuf + st->d_size - sizeof(s64),
  82. &time_ns, sizeof(time_ns));
  83. indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
  84. done:
  85. iio_trigger_notify_done(indio_dev->trig);
  86. kfree(rxbuf);
  87. return IRQ_HANDLED;
  88. }
  89. static const struct iio_ring_setup_ops ad7476_ring_setup_ops = {
  90. .preenable = &ad7476_ring_preenable,
  91. .postenable = &iio_triggered_ring_postenable,
  92. .predisable = &iio_triggered_ring_predisable,
  93. };
  94. int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
  95. {
  96. struct ad7476_state *st = indio_dev->dev_data;
  97. int ret = 0;
  98. indio_dev->ring = iio_sw_rb_allocate(indio_dev);
  99. if (!indio_dev->ring) {
  100. ret = -ENOMEM;
  101. goto error_ret;
  102. }
  103. /* Effectively select the ring buffer implementation */
  104. indio_dev->ring->access = &ring_sw_access_funcs;
  105. indio_dev->pollfunc
  106. = iio_alloc_pollfunc(NULL,
  107. &ad7476_trigger_handler,
  108. IRQF_ONESHOT,
  109. indio_dev,
  110. "%s_consumer%d",
  111. spi_get_device_id(st->spi)->name,
  112. indio_dev->id);
  113. if (indio_dev->pollfunc == NULL) {
  114. ret = -ENOMEM;
  115. goto error_deallocate_sw_rb;
  116. }
  117. /* Ring buffer functions - here trigger setup related */
  118. indio_dev->ring->setup_ops = &ad7476_ring_setup_ops;
  119. indio_dev->ring->scan_timestamp = true;
  120. /* Flag that polled ring buffering is possible */
  121. indio_dev->modes |= INDIO_RING_TRIGGERED;
  122. return 0;
  123. error_deallocate_sw_rb:
  124. iio_sw_rb_free(indio_dev->ring);
  125. error_ret:
  126. return ret;
  127. }
  128. void ad7476_ring_cleanup(struct iio_dev *indio_dev)
  129. {
  130. /* ensure that the trigger has been detached */
  131. if (indio_dev->trig) {
  132. iio_put_trigger(indio_dev->trig);
  133. iio_trigger_dettach_poll_func(indio_dev->trig,
  134. indio_dev->pollfunc);
  135. }
  136. iio_dealloc_pollfunc(indio_dev->pollfunc);
  137. iio_sw_rb_free(indio_dev->ring);
  138. }