Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 1 | /* |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 2 | * Copyright 2010-2011 Analog Devices Inc. |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 3 | * Copyright (C) 2008 Jonathan Cameron |
| 4 | * |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 5 | * Licensed under the GPL-2. |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 6 | * |
| 7 | * ad7887_ring.c |
| 8 | */ |
| 9 | |
| 10 | #include <linux/interrupt.h> |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 13 | #include <linux/spi/spi.h> |
| 14 | |
| 15 | #include "../iio.h" |
Jonathan Cameron | af5046a | 2011-10-26 17:41:32 +0100 | [diff] [blame^] | 16 | #include "../buffer.h" |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 17 | #include "../ring_sw.h" |
Jonathan Cameron | 3f72395 | 2011-08-24 17:28:39 +0100 | [diff] [blame] | 18 | #include "../trigger_consumer.h" |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 19 | |
| 20 | #include "ad7887.h" |
| 21 | |
Jonathan Cameron | 32b5eec | 2011-09-02 17:14:38 +0100 | [diff] [blame] | 22 | int ad7887_scan_from_ring(struct ad7887_state *st, int channum) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 23 | { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 24 | struct iio_buffer *ring = iio_priv_to_dev(st)->buffer; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 25 | int count = 0, ret; |
| 26 | u16 *ring_data; |
| 27 | |
Jonathan Cameron | 32b5eec | 2011-09-02 17:14:38 +0100 | [diff] [blame] | 28 | if (!(test_bit(channum, ring->scan_mask))) { |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 29 | ret = -EBUSY; |
| 30 | goto error_ret; |
| 31 | } |
| 32 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 33 | ring_data = kmalloc(ring->access->get_bytes_per_datum(ring), |
| 34 | GFP_KERNEL); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 35 | if (ring_data == NULL) { |
| 36 | ret = -ENOMEM; |
| 37 | goto error_ret; |
| 38 | } |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 39 | ret = ring->access->read_last(ring, (u8 *) ring_data); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 40 | if (ret) |
| 41 | goto error_free_ring_data; |
| 42 | |
| 43 | /* for single channel scan the result is stored with zero offset */ |
Jonathan Cameron | 32b5eec | 2011-09-02 17:14:38 +0100 | [diff] [blame] | 44 | if ((test_bit(1, ring->scan_mask) || test_bit(0, ring->scan_mask)) && |
| 45 | (channum == 1)) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 46 | count = 1; |
| 47 | |
| 48 | ret = be16_to_cpu(ring_data[count]); |
| 49 | |
| 50 | error_free_ring_data: |
| 51 | kfree(ring_data); |
| 52 | error_ret: |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * ad7887_ring_preenable() setup the parameters of the ring before enabling |
| 58 | * |
| 59 | * The complex nature of the setting of the nuber of bytes per datum is due |
| 60 | * to this driver currently ensuring that the timestamp is stored at an 8 |
| 61 | * byte boundary. |
| 62 | **/ |
| 63 | static int ad7887_ring_preenable(struct iio_dev *indio_dev) |
| 64 | { |
Jonathan Cameron | f490f42 | 2011-06-27 13:07:19 +0100 | [diff] [blame] | 65 | struct ad7887_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 66 | struct iio_buffer *ring = indio_dev->buffer; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 67 | |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 68 | st->d_size = ring->scan_count * |
| 69 | st->chip_info->channel[0].scan_type.storagebits / 8; |
Michael Hennerich | e08d026 | 2011-02-24 22:19:48 +0100 | [diff] [blame] | 70 | |
| 71 | if (ring->scan_timestamp) { |
| 72 | st->d_size += sizeof(s64); |
| 73 | |
| 74 | if (st->d_size % sizeof(s64)) |
| 75 | st->d_size += sizeof(s64) - (st->d_size % sizeof(s64)); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 78 | if (indio_dev->buffer->access->set_bytes_per_datum) |
| 79 | indio_dev->buffer->access-> |
| 80 | set_bytes_per_datum(indio_dev->buffer, st->d_size); |
Michael Hennerich | e08d026 | 2011-02-24 22:19:48 +0100 | [diff] [blame] | 81 | |
Jonathan Cameron | 32b5eec | 2011-09-02 17:14:38 +0100 | [diff] [blame] | 82 | /* We know this is a single long so can 'cheat' */ |
| 83 | switch (*ring->scan_mask) { |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 84 | case (1 << 0): |
| 85 | st->ring_msg = &st->msg[AD7887_CH0]; |
| 86 | break; |
| 87 | case (1 << 1): |
| 88 | st->ring_msg = &st->msg[AD7887_CH1]; |
| 89 | /* Dummy read: push CH1 setting down to hardware */ |
| 90 | spi_sync(st->spi, st->ring_msg); |
| 91 | break; |
| 92 | case ((1 << 1) | (1 << 0)): |
| 93 | st->ring_msg = &st->msg[AD7887_CH0_CH1]; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int ad7887_ring_postdisable(struct iio_dev *indio_dev) |
| 101 | { |
Jonathan Cameron | f490f42 | 2011-06-27 13:07:19 +0100 | [diff] [blame] | 102 | struct ad7887_state *st = iio_priv(indio_dev); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 103 | |
| 104 | /* dummy read: restore default CH0 settin */ |
| 105 | return spi_sync(st->spi, &st->msg[AD7887_CH0]); |
| 106 | } |
| 107 | |
| 108 | /** |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 109 | * ad7887_trigger_handler() bh of trigger launched polling to ring buffer |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 110 | * |
| 111 | * Currently there is no option in this driver to disable the saving of |
| 112 | * timestamps within the ring. |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 113 | **/ |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 114 | static irqreturn_t ad7887_trigger_handler(int irq, void *p) |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 115 | { |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 116 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 117 | struct iio_dev *indio_dev = pf->indio_dev; |
Jonathan Cameron | f490f42 | 2011-06-27 13:07:19 +0100 | [diff] [blame] | 118 | struct ad7887_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 119 | struct iio_buffer *ring = indio_dev->buffer; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 120 | s64 time_ns; |
| 121 | __u8 *buf; |
| 122 | int b_sent; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 123 | |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 124 | unsigned int bytes = ring->scan_count * |
| 125 | st->chip_info->channel[0].scan_type.storagebits / 8; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 126 | |
Michael Hennerich | e08d026 | 2011-02-24 22:19:48 +0100 | [diff] [blame] | 127 | buf = kzalloc(st->d_size, GFP_KERNEL); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 128 | if (buf == NULL) |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 129 | return -ENOMEM; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 130 | |
| 131 | b_sent = spi_sync(st->spi, st->ring_msg); |
| 132 | if (b_sent) |
| 133 | goto done; |
| 134 | |
| 135 | time_ns = iio_get_time_ns(); |
| 136 | |
| 137 | memcpy(buf, st->data, bytes); |
Michael Hennerich | e08d026 | 2011-02-24 22:19:48 +0100 | [diff] [blame] | 138 | if (ring->scan_timestamp) |
| 139 | memcpy(buf + st->d_size - sizeof(s64), |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 140 | &time_ns, sizeof(time_ns)); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 141 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 142 | indio_dev->buffer->access->store_to(indio_dev->buffer, buf, time_ns); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 143 | done: |
| 144 | kfree(buf); |
Michael Hennerich | 596d060 | 2011-05-18 14:41:50 +0100 | [diff] [blame] | 145 | iio_trigger_notify_done(indio_dev->trig); |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 146 | |
| 147 | return IRQ_HANDLED; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 150 | static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = { |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 151 | .preenable = &ad7887_ring_preenable, |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 152 | .postenable = &iio_triggered_buffer_postenable, |
| 153 | .predisable = &iio_triggered_buffer_predisable, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 154 | .postdisable = &ad7887_ring_postdisable, |
| 155 | }; |
| 156 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 157 | int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev) |
| 158 | { |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 159 | int ret; |
| 160 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 161 | indio_dev->buffer = iio_sw_rb_allocate(indio_dev); |
| 162 | if (!indio_dev->buffer) { |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 163 | ret = -ENOMEM; |
| 164 | goto error_ret; |
| 165 | } |
| 166 | /* Effectively select the ring buffer implementation */ |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 167 | indio_dev->buffer->access = &ring_sw_access_funcs; |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 168 | indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time, |
| 169 | &ad7887_trigger_handler, |
| 170 | IRQF_ONESHOT, |
| 171 | indio_dev, |
| 172 | "ad7887_consumer%d", |
| 173 | indio_dev->id); |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 174 | if (indio_dev->pollfunc == NULL) { |
| 175 | ret = -ENOMEM; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 176 | goto error_deallocate_sw_rb; |
Jonathan Cameron | e362dfb | 2011-05-18 14:41:39 +0100 | [diff] [blame] | 177 | } |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 178 | /* Ring buffer functions - here trigger setup related */ |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 179 | indio_dev->buffer->setup_ops = &ad7887_ring_setup_ops; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 180 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 181 | /* Flag that polled ring buffering is possible */ |
Jonathan Cameron | ec3afa4 | 2011-09-21 11:15:54 +0100 | [diff] [blame] | 182 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 183 | return 0; |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 184 | |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 185 | error_deallocate_sw_rb: |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 186 | iio_sw_rb_free(indio_dev->buffer); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 187 | error_ret: |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | void ad7887_ring_cleanup(struct iio_dev *indio_dev) |
| 192 | { |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 193 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 194 | iio_sw_rb_free(indio_dev->buffer); |
Michael Hennerich | 2b4756a | 2010-11-22 14:35:32 +0100 | [diff] [blame] | 195 | } |