Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 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 | |
| 10 | #include <linux/interrupt.h> |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/slab.h> |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 14 | #include <linux/spi/spi.h> |
| 15 | |
| 16 | #include "../iio.h" |
| 17 | #include "../ring_generic.h" |
| 18 | #include "../ring_sw.h" |
Jonathan Cameron | 3f72395 | 2011-08-24 17:28:39 +0100 | [diff] [blame] | 19 | #include "../trigger_consumer.h" |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 20 | |
| 21 | #include "ad7476.h" |
| 22 | |
Jonathan Cameron | 6768810 | 2011-06-27 13:07:18 +0100 | [diff] [blame] | 23 | int ad7476_scan_from_ring(struct iio_dev *indio_dev) |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 24 | { |
Jonathan Cameron | 6768810 | 2011-06-27 13:07:18 +0100 | [diff] [blame] | 25 | struct iio_ring_buffer *ring = indio_dev->ring; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 26 | int ret; |
| 27 | u8 *ring_data; |
| 28 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 29 | ring_data = kmalloc(ring->access->get_bytes_per_datum(ring), |
| 30 | GFP_KERNEL); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 31 | if (ring_data == NULL) { |
| 32 | ret = -ENOMEM; |
| 33 | goto error_ret; |
| 34 | } |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 35 | ret = ring->access->read_last(ring, ring_data); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 36 | if (ret) |
| 37 | goto error_free_ring_data; |
| 38 | |
| 39 | ret = (ring_data[0] << 8) | ring_data[1]; |
| 40 | |
| 41 | error_free_ring_data: |
| 42 | kfree(ring_data); |
| 43 | error_ret: |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * ad7476_ring_preenable() setup the parameters of the ring before enabling |
| 49 | * |
| 50 | * The complex nature of the setting of the nuber of bytes per datum is due |
| 51 | * to this driver currently ensuring that the timestamp is stored at an 8 |
| 52 | * byte boundary. |
| 53 | **/ |
| 54 | static int ad7476_ring_preenable(struct iio_dev *indio_dev) |
| 55 | { |
Jonathan Cameron | 6768810 | 2011-06-27 13:07:18 +0100 | [diff] [blame] | 56 | struct ad7476_state *st = iio_priv(indio_dev); |
Michael Hennerich | 86f702a | 2011-02-24 22:19:47 +0100 | [diff] [blame] | 57 | struct iio_ring_buffer *ring = indio_dev->ring; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 58 | |
Jonathan Cameron | c5e0819 | 2011-05-18 14:41:24 +0100 | [diff] [blame] | 59 | st->d_size = ring->scan_count * |
| 60 | st->chip_info->channel[0].scan_type.storagebits / 8; |
Michael Hennerich | 86f702a | 2011-02-24 22:19:47 +0100 | [diff] [blame] | 61 | |
| 62 | if (ring->scan_timestamp) { |
| 63 | st->d_size += sizeof(s64); |
| 64 | |
| 65 | if (st->d_size % sizeof(s64)) |
| 66 | st->d_size += sizeof(s64) - (st->d_size % sizeof(s64)); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 69 | if (indio_dev->ring->access->set_bytes_per_datum) |
| 70 | indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring, |
Michael Hennerich | 86f702a | 2011-02-24 22:19:47 +0100 | [diff] [blame] | 71 | st->d_size); |
| 72 | |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 76 | static irqreturn_t ad7476_trigger_handler(int irq, void *p) |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 77 | { |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 78 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 79 | struct iio_dev *indio_dev = pf->indio_dev; |
Jonathan Cameron | 6768810 | 2011-06-27 13:07:18 +0100 | [diff] [blame] | 80 | struct ad7476_state *st = iio_priv(indio_dev); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 81 | s64 time_ns; |
| 82 | __u8 *rxbuf; |
| 83 | int b_sent; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 84 | |
Michael Hennerich | 86f702a | 2011-02-24 22:19:47 +0100 | [diff] [blame] | 85 | rxbuf = kzalloc(st->d_size, GFP_KERNEL); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 86 | if (rxbuf == NULL) |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 87 | return -ENOMEM; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 88 | |
Jonathan Cameron | c5e0819 | 2011-05-18 14:41:24 +0100 | [diff] [blame] | 89 | b_sent = spi_read(st->spi, rxbuf, |
| 90 | st->chip_info->channel[0].scan_type.storagebits / 8); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 91 | if (b_sent < 0) |
| 92 | goto done; |
| 93 | |
| 94 | time_ns = iio_get_time_ns(); |
| 95 | |
Michael Hennerich | 86f702a | 2011-02-24 22:19:47 +0100 | [diff] [blame] | 96 | if (indio_dev->ring->scan_timestamp) |
| 97 | memcpy(rxbuf + st->d_size - sizeof(s64), |
| 98 | &time_ns, sizeof(time_ns)); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 99 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 100 | indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 101 | done: |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 102 | iio_trigger_notify_done(indio_dev->trig); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 103 | kfree(rxbuf); |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 104 | |
| 105 | return IRQ_HANDLED; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 108 | static const struct iio_ring_setup_ops ad7476_ring_setup_ops = { |
| 109 | .preenable = &ad7476_ring_preenable, |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 110 | .postenable = &iio_triggered_buffer_postenable, |
| 111 | .predisable = &iio_triggered_buffer_predisable, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 114 | int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev) |
| 115 | { |
Jonathan Cameron | 6768810 | 2011-06-27 13:07:18 +0100 | [diff] [blame] | 116 | struct ad7476_state *st = iio_priv(indio_dev); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 117 | int ret = 0; |
| 118 | |
| 119 | indio_dev->ring = iio_sw_rb_allocate(indio_dev); |
| 120 | if (!indio_dev->ring) { |
| 121 | ret = -ENOMEM; |
| 122 | goto error_ret; |
| 123 | } |
| 124 | /* Effectively select the ring buffer implementation */ |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 125 | indio_dev->ring->access = &ring_sw_access_funcs; |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 126 | indio_dev->pollfunc |
| 127 | = iio_alloc_pollfunc(NULL, |
| 128 | &ad7476_trigger_handler, |
| 129 | IRQF_ONESHOT, |
| 130 | indio_dev, |
| 131 | "%s_consumer%d", |
| 132 | spi_get_device_id(st->spi)->name, |
| 133 | indio_dev->id); |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 134 | if (indio_dev->pollfunc == NULL) { |
| 135 | ret = -ENOMEM; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 136 | goto error_deallocate_sw_rb; |
Jonathan Cameron | f20546e | 2011-05-18 14:41:25 +0100 | [diff] [blame] | 137 | } |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 138 | |
| 139 | /* Ring buffer functions - here trigger setup related */ |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 140 | indio_dev->ring->setup_ops = &ad7476_ring_setup_ops; |
Michael Hennerich | 86f702a | 2011-02-24 22:19:47 +0100 | [diff] [blame] | 141 | indio_dev->ring->scan_timestamp = true; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 142 | |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 143 | /* Flag that polled ring buffering is possible */ |
Jonathan Cameron | ec3afa4 | 2011-09-21 11:15:54 +0100 | [diff] [blame^] | 144 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 145 | return 0; |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 146 | |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 147 | error_deallocate_sw_rb: |
| 148 | iio_sw_rb_free(indio_dev->ring); |
| 149 | error_ret: |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | void ad7476_ring_cleanup(struct iio_dev *indio_dev) |
| 154 | { |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 155 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
Michael Hennerich | 349282d | 2010-10-11 18:05:37 +0200 | [diff] [blame] | 156 | iio_sw_rb_free(indio_dev->ring); |
| 157 | } |