Michael Hennerich | 7c31b98 | 2011-02-24 12:32:45 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * AD7298 SPI ADC driver |
| 3 | * |
| 4 | * Copyright 2011 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the GPL-2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/workqueue.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/sysfs.h> |
| 15 | #include <linux/spi/spi.h> |
| 16 | |
| 17 | #include "../iio.h" |
| 18 | #include "../ring_generic.h" |
| 19 | #include "../ring_sw.h" |
| 20 | #include "../trigger.h" |
| 21 | #include "../sysfs.h" |
| 22 | |
| 23 | #include "ad7298.h" |
| 24 | |
| 25 | static IIO_SCAN_EL_C(in0, 0, 0, NULL); |
| 26 | static IIO_SCAN_EL_C(in1, 1, 0, NULL); |
| 27 | static IIO_SCAN_EL_C(in2, 2, 0, NULL); |
| 28 | static IIO_SCAN_EL_C(in3, 3, 0, NULL); |
| 29 | static IIO_SCAN_EL_C(in4, 4, 0, NULL); |
| 30 | static IIO_SCAN_EL_C(in5, 5, 0, NULL); |
| 31 | static IIO_SCAN_EL_C(in6, 6, 0, NULL); |
| 32 | static IIO_SCAN_EL_C(in7, 7, 0, NULL); |
| 33 | |
| 34 | static IIO_SCAN_EL_TIMESTAMP(8); |
| 35 | static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64); |
| 36 | |
| 37 | static IIO_CONST_ATTR(in_type, "u12/16") ; |
| 38 | |
| 39 | static struct attribute *ad7298_scan_el_attrs[] = { |
| 40 | &iio_scan_el_in0.dev_attr.attr, |
| 41 | &iio_const_attr_in0_index.dev_attr.attr, |
| 42 | &iio_scan_el_in1.dev_attr.attr, |
| 43 | &iio_const_attr_in1_index.dev_attr.attr, |
| 44 | &iio_scan_el_in2.dev_attr.attr, |
| 45 | &iio_const_attr_in2_index.dev_attr.attr, |
| 46 | &iio_scan_el_in3.dev_attr.attr, |
| 47 | &iio_const_attr_in3_index.dev_attr.attr, |
| 48 | &iio_scan_el_in4.dev_attr.attr, |
| 49 | &iio_const_attr_in4_index.dev_attr.attr, |
| 50 | &iio_scan_el_in5.dev_attr.attr, |
| 51 | &iio_const_attr_in5_index.dev_attr.attr, |
| 52 | &iio_scan_el_in6.dev_attr.attr, |
| 53 | &iio_const_attr_in6_index.dev_attr.attr, |
| 54 | &iio_scan_el_in7.dev_attr.attr, |
| 55 | &iio_const_attr_in7_index.dev_attr.attr, |
| 56 | &iio_const_attr_timestamp_index.dev_attr.attr, |
| 57 | &iio_scan_el_timestamp.dev_attr.attr, |
| 58 | &iio_const_attr_timestamp_type.dev_attr.attr, |
| 59 | &iio_const_attr_in_type.dev_attr.attr, |
| 60 | NULL, |
| 61 | }; |
| 62 | |
| 63 | static struct attribute_group ad7298_scan_el_group = { |
| 64 | .name = "scan_elements", |
| 65 | .attrs = ad7298_scan_el_attrs, |
| 66 | }; |
| 67 | |
| 68 | int ad7298_scan_from_ring(struct ad7298_state *st, long ch) |
| 69 | { |
| 70 | struct iio_ring_buffer *ring = st->indio_dev->ring; |
| 71 | int ret; |
| 72 | u16 *ring_data; |
| 73 | |
| 74 | if (!(ring->scan_mask & (1 << ch))) { |
| 75 | ret = -EBUSY; |
| 76 | goto error_ret; |
| 77 | } |
| 78 | |
| 79 | ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL); |
| 80 | if (ring_data == NULL) { |
| 81 | ret = -ENOMEM; |
| 82 | goto error_ret; |
| 83 | } |
| 84 | ret = ring->access.read_last(ring, (u8 *) ring_data); |
| 85 | if (ret) |
| 86 | goto error_free_ring_data; |
| 87 | |
| 88 | ret = be16_to_cpu(ring_data[ch]); |
| 89 | |
| 90 | error_free_ring_data: |
| 91 | kfree(ring_data); |
| 92 | error_ret: |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * ad7298_ring_preenable() setup the parameters of the ring before enabling |
| 98 | * |
| 99 | * The complex nature of the setting of the number of bytes per datum is due |
| 100 | * to this driver currently ensuring that the timestamp is stored at an 8 |
| 101 | * byte boundary. |
| 102 | **/ |
| 103 | static int ad7298_ring_preenable(struct iio_dev *indio_dev) |
| 104 | { |
| 105 | struct ad7298_state *st = indio_dev->dev_data; |
| 106 | struct iio_ring_buffer *ring = indio_dev->ring; |
| 107 | size_t d_size; |
| 108 | int i, m; |
| 109 | unsigned short command; |
| 110 | |
| 111 | d_size = ring->scan_count * (AD7298_STORAGE_BITS / 8); |
| 112 | |
| 113 | if (ring->scan_timestamp) { |
| 114 | d_size += sizeof(s64); |
| 115 | |
| 116 | if (d_size % sizeof(s64)) |
| 117 | d_size += sizeof(s64) - (d_size % sizeof(s64)); |
| 118 | } |
| 119 | |
| 120 | if (ring->access.set_bytes_per_datum) |
| 121 | ring->access.set_bytes_per_datum(ring, d_size); |
| 122 | |
| 123 | st->d_size = d_size; |
| 124 | |
| 125 | command = AD7298_WRITE | st->ext_ref; |
| 126 | |
| 127 | for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1) |
| 128 | if (ring->scan_mask & (1 << i)) |
| 129 | command |= m; |
| 130 | |
| 131 | st->tx_buf[0] = cpu_to_be16(command); |
| 132 | |
| 133 | /* build spi ring message */ |
| 134 | st->ring_xfer[0].tx_buf = &st->tx_buf[0]; |
| 135 | st->ring_xfer[0].len = 2; |
| 136 | st->ring_xfer[0].cs_change = 1; |
| 137 | st->ring_xfer[1].tx_buf = &st->tx_buf[1]; |
| 138 | st->ring_xfer[1].len = 2; |
| 139 | st->ring_xfer[1].cs_change = 1; |
| 140 | |
| 141 | spi_message_init(&st->ring_msg); |
| 142 | spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg); |
| 143 | spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg); |
| 144 | |
| 145 | for (i = 0; i < ring->scan_count; i++) { |
| 146 | st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i]; |
| 147 | st->ring_xfer[i + 2].len = 2; |
| 148 | st->ring_xfer[i + 2].cs_change = 1; |
| 149 | spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg); |
| 150 | } |
| 151 | /* make sure last transfer cs_change is not set */ |
| 152 | st->ring_xfer[i + 1].cs_change = 0; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * ad7298_poll_func_th() th of trigger launched polling to ring buffer |
| 159 | * |
| 160 | * As sampling only occurs on spi comms occuring, leave timestamping until |
| 161 | * then. Some triggers will generate their own time stamp. Currently |
| 162 | * there is no way of notifying them when no one cares. |
| 163 | **/ |
| 164 | static void ad7298_poll_func_th(struct iio_dev *indio_dev, s64 time) |
| 165 | { |
| 166 | struct ad7298_state *st = indio_dev->dev_data; |
| 167 | |
| 168 | schedule_work(&st->poll_work); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * ad7298_poll_bh_to_ring() bh of trigger launched polling to ring buffer |
| 174 | * @work_s: the work struct through which this was scheduled |
| 175 | * |
| 176 | * Currently there is no option in this driver to disable the saving of |
| 177 | * timestamps within the ring. |
| 178 | * I think the one copy of this at a time was to avoid problems if the |
| 179 | * trigger was set far too high and the reads then locked up the computer. |
| 180 | **/ |
| 181 | static void ad7298_poll_bh_to_ring(struct work_struct *work_s) |
| 182 | { |
| 183 | struct ad7298_state *st = container_of(work_s, struct ad7298_state, |
| 184 | poll_work); |
| 185 | struct iio_dev *indio_dev = st->indio_dev; |
| 186 | struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring); |
| 187 | struct iio_ring_buffer *ring = indio_dev->ring; |
| 188 | s64 time_ns; |
| 189 | __u16 buf[16]; |
| 190 | int b_sent, i; |
| 191 | |
| 192 | /* Ensure only one copy of this function running at a time */ |
| 193 | if (atomic_inc_return(&st->protect_ring) > 1) |
| 194 | return; |
| 195 | |
| 196 | b_sent = spi_sync(st->spi, &st->ring_msg); |
| 197 | if (b_sent) |
| 198 | goto done; |
| 199 | |
| 200 | if (ring->scan_timestamp) { |
| 201 | time_ns = iio_get_time_ns(); |
| 202 | memcpy((u8 *)buf + st->d_size - sizeof(s64), |
| 203 | &time_ns, sizeof(time_ns)); |
| 204 | } |
| 205 | |
| 206 | for (i = 0; i < ring->scan_count; i++) |
| 207 | buf[i] = be16_to_cpu(st->rx_buf[i]); |
| 208 | |
| 209 | indio_dev->ring->access.store_to(&sw_ring->buf, (u8 *)buf, time_ns); |
| 210 | done: |
| 211 | atomic_dec(&st->protect_ring); |
| 212 | } |
| 213 | |
| 214 | int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev) |
| 215 | { |
| 216 | struct ad7298_state *st = indio_dev->dev_data; |
| 217 | int ret; |
| 218 | |
| 219 | indio_dev->ring = iio_sw_rb_allocate(indio_dev); |
| 220 | if (!indio_dev->ring) { |
| 221 | ret = -ENOMEM; |
| 222 | goto error_ret; |
| 223 | } |
| 224 | /* Effectively select the ring buffer implementation */ |
| 225 | iio_ring_sw_register_funcs(&indio_dev->ring->access); |
| 226 | ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7298_poll_func_th); |
| 227 | if (ret) |
| 228 | goto error_deallocate_sw_rb; |
| 229 | |
| 230 | /* Ring buffer functions - here trigger setup related */ |
| 231 | |
| 232 | indio_dev->ring->preenable = &ad7298_ring_preenable; |
| 233 | indio_dev->ring->postenable = &iio_triggered_ring_postenable; |
| 234 | indio_dev->ring->predisable = &iio_triggered_ring_predisable; |
| 235 | indio_dev->ring->scan_el_attrs = &ad7298_scan_el_group; |
| 236 | indio_dev->ring->scan_timestamp = true; |
| 237 | |
| 238 | INIT_WORK(&st->poll_work, &ad7298_poll_bh_to_ring); |
| 239 | |
| 240 | /* Flag that polled ring buffering is possible */ |
| 241 | indio_dev->modes |= INDIO_RING_TRIGGERED; |
| 242 | return 0; |
| 243 | error_deallocate_sw_rb: |
| 244 | iio_sw_rb_free(indio_dev->ring); |
| 245 | error_ret: |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | void ad7298_ring_cleanup(struct iio_dev *indio_dev) |
| 250 | { |
| 251 | if (indio_dev->trig) { |
| 252 | iio_put_trigger(indio_dev->trig); |
| 253 | iio_trigger_dettach_poll_func(indio_dev->trig, |
| 254 | indio_dev->pollfunc); |
| 255 | } |
| 256 | kfree(indio_dev->pollfunc); |
| 257 | iio_sw_rb_free(indio_dev->ring); |
| 258 | } |