/drivers/staging/iio/adc/max1363_ring.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 209 lines · 152 code · 27 blank · 30 comment · 29 complexity · d121b13ce3867a0ab013d5fbe7b0622e MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Jonathan Cameron
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * max1363_ring.c
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/device.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/list.h>
  16. #include <linux/i2c.h>
  17. #include <linux/bitops.h>
  18. #include "../iio.h"
  19. #include "../ring_generic.h"
  20. #include "../ring_sw.h"
  21. #include "../trigger.h"
  22. #include "../sysfs.h"
  23. #include "max1363.h"
  24. int max1363_single_channel_from_ring(long mask, struct max1363_state *st)
  25. {
  26. struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
  27. int count = 0, ret;
  28. u8 *ring_data;
  29. if (!(st->current_mode->modemask & mask)) {
  30. ret = -EBUSY;
  31. goto error_ret;
  32. }
  33. ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
  34. GFP_KERNEL);
  35. if (ring_data == NULL) {
  36. ret = -ENOMEM;
  37. goto error_ret;
  38. }
  39. ret = ring->access->read_last(ring, ring_data);
  40. if (ret)
  41. goto error_free_ring_data;
  42. /* Need a count of channels prior to this one */
  43. mask >>= 1;
  44. while (mask) {
  45. if (mask & st->current_mode->modemask)
  46. count++;
  47. mask >>= 1;
  48. }
  49. if (st->chip_info->bits != 8)
  50. ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
  51. + (int)(ring_data[count*2 + 1]);
  52. else
  53. ret = ring_data[count];
  54. error_free_ring_data:
  55. kfree(ring_data);
  56. error_ret:
  57. return ret;
  58. }
  59. /**
  60. * max1363_ring_preenable() - setup the parameters of the ring before enabling
  61. *
  62. * The complex nature of the setting of the nuber of bytes per datum is due
  63. * to this driver currently ensuring that the timestamp is stored at an 8
  64. * byte boundary.
  65. **/
  66. static int max1363_ring_preenable(struct iio_dev *indio_dev)
  67. {
  68. struct max1363_state *st = iio_priv(indio_dev);
  69. struct iio_ring_buffer *ring = indio_dev->ring;
  70. size_t d_size = 0;
  71. unsigned long numvals;
  72. /*
  73. * Need to figure out the current mode based upon the requested
  74. * scan mask in iio_dev
  75. */
  76. st->current_mode = max1363_match_mode(ring->scan_mask,
  77. st->chip_info);
  78. if (!st->current_mode)
  79. return -EINVAL;
  80. max1363_set_scan_mode(st);
  81. numvals = hweight_long(st->current_mode->modemask);
  82. if (ring->access->set_bytes_per_datum) {
  83. if (ring->scan_timestamp)
  84. d_size += sizeof(s64);
  85. if (st->chip_info->bits != 8)
  86. d_size += numvals*2;
  87. else
  88. d_size += numvals;
  89. if (ring->scan_timestamp && (d_size % 8))
  90. d_size += 8 - (d_size % 8);
  91. ring->access->set_bytes_per_datum(ring, d_size);
  92. }
  93. return 0;
  94. }
  95. static irqreturn_t max1363_trigger_handler(int irq, void *p)
  96. {
  97. struct iio_poll_func *pf = p;
  98. struct iio_dev *indio_dev = pf->private_data;
  99. struct max1363_state *st = iio_priv(indio_dev);
  100. s64 time_ns;
  101. __u8 *rxbuf;
  102. int b_sent;
  103. size_t d_size;
  104. unsigned long numvals = hweight_long(st->current_mode->modemask);
  105. /* Ensure the timestamp is 8 byte aligned */
  106. if (st->chip_info->bits != 8)
  107. d_size = numvals*2 + sizeof(s64);
  108. else
  109. d_size = numvals + sizeof(s64);
  110. if (d_size % sizeof(s64))
  111. d_size += sizeof(s64) - (d_size % sizeof(s64));
  112. /* Monitor mode prevents reading. Whilst not currently implemented
  113. * might as well have this test in here in the meantime as it does
  114. * no harm.
  115. */
  116. if (numvals == 0)
  117. return IRQ_HANDLED;
  118. rxbuf = kmalloc(d_size, GFP_KERNEL);
  119. if (rxbuf == NULL)
  120. return -ENOMEM;
  121. if (st->chip_info->bits != 8)
  122. b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
  123. else
  124. b_sent = i2c_master_recv(st->client, rxbuf, numvals);
  125. if (b_sent < 0)
  126. goto done;
  127. time_ns = iio_get_time_ns();
  128. memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
  129. indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
  130. done:
  131. iio_trigger_notify_done(indio_dev->trig);
  132. kfree(rxbuf);
  133. return IRQ_HANDLED;
  134. }
  135. static const struct iio_ring_setup_ops max1363_ring_setup_ops = {
  136. .postenable = &iio_triggered_ring_postenable,
  137. .preenable = &max1363_ring_preenable,
  138. .predisable = &iio_triggered_ring_predisable,
  139. };
  140. int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
  141. {
  142. struct max1363_state *st = iio_priv(indio_dev);
  143. int ret = 0;
  144. indio_dev->ring = iio_sw_rb_allocate(indio_dev);
  145. if (!indio_dev->ring) {
  146. ret = -ENOMEM;
  147. goto error_ret;
  148. }
  149. indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
  150. &max1363_trigger_handler,
  151. IRQF_ONESHOT,
  152. indio_dev,
  153. "%s_consumer%d",
  154. st->client->name,
  155. indio_dev->id);
  156. if (indio_dev->pollfunc == NULL) {
  157. ret = -ENOMEM;
  158. goto error_deallocate_sw_rb;
  159. }
  160. /* Effectively select the ring buffer implementation */
  161. indio_dev->ring->access = &ring_sw_access_funcs;
  162. /* Ring buffer functions - here trigger setup related */
  163. indio_dev->ring->setup_ops = &max1363_ring_setup_ops;
  164. /* Flag that polled ring buffering is possible */
  165. indio_dev->modes |= INDIO_RING_TRIGGERED;
  166. return 0;
  167. error_deallocate_sw_rb:
  168. iio_sw_rb_free(indio_dev->ring);
  169. error_ret:
  170. return ret;
  171. }
  172. void max1363_ring_cleanup(struct iio_dev *indio_dev)
  173. {
  174. /* ensure that the trigger has been detached */
  175. if (indio_dev->trig) {
  176. iio_put_trigger(indio_dev->trig);
  177. iio_trigger_dettach_poll_func(indio_dev->trig,
  178. indio_dev->pollfunc);
  179. }
  180. iio_dealloc_pollfunc(indio_dev->pollfunc);
  181. iio_sw_rb_free(indio_dev->ring);
  182. }