Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Analog Devices Inc. |
| 3 | * |
| 4 | * Licensed under the GPL-2. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/gpio.h> |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 13 | |
| 14 | #include "../iio.h" |
Jonathan Cameron | af5046a | 2011-10-26 17:41:32 +0100 | [diff] [blame^] | 15 | #include "../buffer.h" |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 16 | #include "../ring_sw.h" |
Jonathan Cameron | 3f72395 | 2011-08-24 17:28:39 +0100 | [diff] [blame] | 17 | #include "../trigger_consumer.h" |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 18 | |
| 19 | #include "ad7606.h" |
| 20 | |
Michael Hennerich | 44039a6 | 2011-05-18 14:42:28 +0100 | [diff] [blame] | 21 | int ad7606_scan_from_ring(struct iio_dev *indio_dev, unsigned ch) |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 22 | { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 23 | struct iio_buffer *ring = indio_dev->buffer; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 24 | int ret; |
| 25 | u16 *ring_data; |
| 26 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 27 | ring_data = kmalloc(ring->access->get_bytes_per_datum(ring), |
Michael Hennerich | e61181d | 2011-05-18 14:42:01 +0100 | [diff] [blame] | 28 | GFP_KERNEL); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 29 | if (ring_data == NULL) { |
| 30 | ret = -ENOMEM; |
| 31 | goto error_ret; |
| 32 | } |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 33 | ret = ring->access->read_last(ring, (u8 *) ring_data); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 34 | if (ret) |
| 35 | goto error_free_ring_data; |
| 36 | |
| 37 | ret = ring_data[ch]; |
| 38 | |
| 39 | error_free_ring_data: |
| 40 | kfree(ring_data); |
| 41 | error_ret: |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | /** |
Michael Hennerich | 1caf7cb | 2011-05-18 14:42:00 +0100 | [diff] [blame] | 46 | * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 47 | * |
| 48 | **/ |
Michael Hennerich | 1caf7cb | 2011-05-18 14:42:00 +0100 | [diff] [blame] | 49 | static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p) |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 50 | { |
Jonathan Cameron | 13cfac2 | 2011-05-18 14:41:38 +0100 | [diff] [blame] | 51 | struct iio_poll_func *pf = p; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 52 | struct ad7606_state *st = iio_priv(pf->indio_dev); |
Michael Hennerich | 1caf7cb | 2011-05-18 14:42:00 +0100 | [diff] [blame] | 53 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 54 | gpio_set_value(st->pdata->gpio_convst, 1); |
| 55 | |
Jonathan Cameron | 13cfac2 | 2011-05-18 14:41:38 +0100 | [diff] [blame] | 56 | return IRQ_HANDLED; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 57 | } |
Jonathan Cameron | 13cfac2 | 2011-05-18 14:41:38 +0100 | [diff] [blame] | 58 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 59 | /** |
| 60 | * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer |
| 61 | * @work_s: the work struct through which this was scheduled |
| 62 | * |
| 63 | * Currently there is no option in this driver to disable the saving of |
| 64 | * timestamps within the ring. |
| 65 | * I think the one copy of this at a time was to avoid problems if the |
| 66 | * trigger was set far too high and the reads then locked up the computer. |
| 67 | **/ |
| 68 | static void ad7606_poll_bh_to_ring(struct work_struct *work_s) |
| 69 | { |
| 70 | struct ad7606_state *st = container_of(work_s, struct ad7606_state, |
| 71 | poll_work); |
Michael Hennerich | e61181d | 2011-05-18 14:42:01 +0100 | [diff] [blame] | 72 | struct iio_dev *indio_dev = iio_priv_to_dev(st); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 73 | struct iio_buffer *ring = indio_dev->buffer; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 74 | s64 time_ns; |
| 75 | __u8 *buf; |
| 76 | int ret; |
| 77 | |
Jonathan Cameron | 4f5495d | 2011-09-30 10:05:41 +0100 | [diff] [blame] | 78 | buf = kzalloc(ring->access->get_bytes_per_datum(ring), |
| 79 | GFP_KERNEL); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 80 | if (buf == NULL) |
| 81 | return; |
| 82 | |
Jonathan Cameron | 68b41ae | 2011-09-30 10:05:40 +0100 | [diff] [blame] | 83 | if (gpio_is_valid(st->pdata->gpio_frstdata)) { |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 84 | ret = st->bops->read_block(st->dev, 1, buf); |
| 85 | if (ret) |
| 86 | goto done; |
| 87 | if (!gpio_get_value(st->pdata->gpio_frstdata)) { |
| 88 | /* This should never happen. However |
| 89 | * some signal glitch caused by bad PCB desgin or |
| 90 | * electrostatic discharge, could cause an extra read |
| 91 | * or clock. This allows recovery. |
| 92 | */ |
| 93 | ad7606_reset(st); |
| 94 | goto done; |
| 95 | } |
| 96 | ret = st->bops->read_block(st->dev, |
| 97 | st->chip_info->num_channels - 1, buf + 2); |
| 98 | if (ret) |
| 99 | goto done; |
| 100 | } else { |
| 101 | ret = st->bops->read_block(st->dev, |
| 102 | st->chip_info->num_channels, buf); |
| 103 | if (ret) |
| 104 | goto done; |
| 105 | } |
| 106 | |
| 107 | time_ns = iio_get_time_ns(); |
Michael Hennerich | dcfb086 | 2011-02-24 22:19:49 +0100 | [diff] [blame] | 108 | |
| 109 | if (ring->scan_timestamp) |
Jonathan Cameron | 4f5495d | 2011-09-30 10:05:41 +0100 | [diff] [blame] | 110 | *((s64 *)(buf + ring->access->get_bytes_per_datum(ring) - |
| 111 | sizeof(s64))) = time_ns; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 112 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 113 | ring->access->store_to(indio_dev->buffer, buf, time_ns); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 114 | done: |
| 115 | gpio_set_value(st->pdata->gpio_convst, 0); |
Michael Hennerich | 1caf7cb | 2011-05-18 14:42:00 +0100 | [diff] [blame] | 116 | iio_trigger_notify_done(indio_dev->trig); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 117 | kfree(buf); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 120 | static const struct iio_buffer_setup_ops ad7606_ring_setup_ops = { |
Jonathan Cameron | 4f5495d | 2011-09-30 10:05:41 +0100 | [diff] [blame] | 121 | .preenable = &iio_sw_buffer_preenable, |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 122 | .postenable = &iio_triggered_buffer_postenable, |
| 123 | .predisable = &iio_triggered_buffer_predisable, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 126 | int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev) |
| 127 | { |
Michael Hennerich | 44039a6 | 2011-05-18 14:42:28 +0100 | [diff] [blame] | 128 | struct ad7606_state *st = iio_priv(indio_dev); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 129 | int ret; |
| 130 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 131 | indio_dev->buffer = iio_sw_rb_allocate(indio_dev); |
| 132 | if (!indio_dev->buffer) { |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 133 | ret = -ENOMEM; |
| 134 | goto error_ret; |
| 135 | } |
| 136 | |
| 137 | /* Effectively select the ring buffer implementation */ |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 138 | indio_dev->buffer->access = &ring_sw_access_funcs; |
Jonathan Cameron | 4f5495d | 2011-09-30 10:05:41 +0100 | [diff] [blame] | 139 | indio_dev->buffer->bpe = |
| 140 | st->chip_info->channels[0].scan_type.storagebits / 8; |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 141 | indio_dev->pollfunc = iio_alloc_pollfunc(&ad7606_trigger_handler_th_bh, |
| 142 | &ad7606_trigger_handler_th_bh, |
| 143 | 0, |
| 144 | indio_dev, |
| 145 | "%s_consumer%d", |
| 146 | indio_dev->name, |
| 147 | indio_dev->id); |
Jonathan Cameron | 13cfac2 | 2011-05-18 14:41:38 +0100 | [diff] [blame] | 148 | if (indio_dev->pollfunc == NULL) { |
| 149 | ret = -ENOMEM; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 150 | goto error_deallocate_sw_rb; |
Jonathan Cameron | 13cfac2 | 2011-05-18 14:41:38 +0100 | [diff] [blame] | 151 | } |
Michael Hennerich | 1caf7cb | 2011-05-18 14:42:00 +0100 | [diff] [blame] | 152 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 153 | /* Ring buffer functions - here trigger setup related */ |
| 154 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 155 | indio_dev->buffer->setup_ops = &ad7606_ring_setup_ops; |
| 156 | indio_dev->buffer->scan_timestamp = true ; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 157 | |
| 158 | INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring); |
| 159 | |
| 160 | /* Flag that polled ring buffering is possible */ |
Jonathan Cameron | ec3afa4 | 2011-09-21 11:15:54 +0100 | [diff] [blame] | 161 | indio_dev->modes |= INDIO_BUFFER_TRIGGERED; |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 162 | return 0; |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 163 | |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 164 | error_deallocate_sw_rb: |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 165 | iio_sw_rb_free(indio_dev->buffer); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 166 | error_ret: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | void ad7606_ring_cleanup(struct iio_dev *indio_dev) |
| 171 | { |
Jonathan Cameron | 0ed731d | 2011-05-18 14:42:39 +0100 | [diff] [blame] | 172 | iio_dealloc_pollfunc(indio_dev->pollfunc); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 173 | iio_sw_rb_free(indio_dev->buffer); |
Michael Hennerich | b9618c0 | 2011-02-22 21:46:18 +0100 | [diff] [blame] | 174 | } |