Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Common library for ADIS16XXX devices
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
8
9#include <linux/export.h>
10#include <linux/interrupt.h>
11#include <linux/mutex.h>
12#include <linux/kernel.h>
13#include <linux/spi/spi.h>
14#include <linux/slab.h>
15
16#include <linux/iio/iio.h>
17#include <linux/iio/buffer.h>
18#include <linux/iio/trigger_consumer.h>
19#include <linux/iio/triggered_buffer.h>
20#include <linux/iio/imu/adis.h>
21
22static int adis_update_scan_mode_burst(struct iio_dev *indio_dev,
23 const unsigned long *scan_mask)
24{
25 struct adis *adis = iio_device_get_drvdata(indio_dev);
26 unsigned int burst_length, burst_max_length;
27 u8 *tx;
28
29 burst_length = adis->data->burst_len + adis->burst_extra_len;
30
31 if (adis->data->burst_max_len)
32 burst_max_length = adis->data->burst_max_len;
33 else
34 burst_max_length = burst_length;
35
36 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
37 if (!adis->xfer)
38 return -ENOMEM;
39
40 adis->buffer = kzalloc(burst_max_length + sizeof(u16), GFP_KERNEL);
41 if (!adis->buffer) {
42 kfree(adis->xfer);
43 adis->xfer = NULL;
44 return -ENOMEM;
45 }
46
47 tx = adis->buffer + burst_max_length;
48 tx[0] = ADIS_READ_REG(adis->data->burst_reg_cmd);
49 tx[1] = 0;
50
51 adis->xfer[0].tx_buf = tx;
52 adis->xfer[0].bits_per_word = 8;
53 adis->xfer[0].len = 2;
54 if (adis->data->burst_max_speed_hz)
55 adis->xfer[0].speed_hz = adis->data->burst_max_speed_hz;
56 adis->xfer[1].rx_buf = adis->buffer;
57 adis->xfer[1].bits_per_word = 8;
58 adis->xfer[1].len = burst_length;
59 if (adis->data->burst_max_speed_hz)
60 adis->xfer[1].speed_hz = adis->data->burst_max_speed_hz;
61
62 spi_message_init(&adis->msg);
63 spi_message_add_tail(&adis->xfer[0], &adis->msg);
64 spi_message_add_tail(&adis->xfer[1], &adis->msg);
65
66 return 0;
67}
68
69int adis_update_scan_mode(struct iio_dev *indio_dev,
70 const unsigned long *scan_mask)
71{
72 struct adis *adis = iio_device_get_drvdata(indio_dev);
73 const struct iio_chan_spec *chan;
74 unsigned int scan_count;
75 unsigned int i, j;
76 __be16 *tx, *rx;
77
78 kfree(adis->xfer);
79 kfree(adis->buffer);
80
81 if (adis->data->burst_len)
82 return adis_update_scan_mode_burst(indio_dev, scan_mask);
83
84 scan_count = indio_dev->scan_bytes / 2;
85
86 adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
87 if (!adis->xfer)
88 return -ENOMEM;
89
90 adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
91 if (!adis->buffer) {
92 kfree(adis->xfer);
93 adis->xfer = NULL;
94 return -ENOMEM;
95 }
96
97 rx = adis->buffer;
98 tx = rx + scan_count;
99
100 spi_message_init(&adis->msg);
101
102 for (j = 0; j <= scan_count; j++) {
103 adis->xfer[j].bits_per_word = 8;
104 if (j != scan_count)
105 adis->xfer[j].cs_change = 1;
106 adis->xfer[j].len = 2;
107 adis->xfer[j].delay.value = adis->data->read_delay;
108 adis->xfer[j].delay.unit = SPI_DELAY_UNIT_USECS;
109 if (j < scan_count)
110 adis->xfer[j].tx_buf = &tx[j];
111 if (j >= 1)
112 adis->xfer[j].rx_buf = &rx[j - 1];
113 spi_message_add_tail(&adis->xfer[j], &adis->msg);
114 }
115
116 chan = indio_dev->channels;
117 for (i = 0; i < indio_dev->num_channels; i++, chan++) {
118 if (!test_bit(chan->scan_index, scan_mask))
119 continue;
120 if (chan->scan_type.storagebits == 32)
121 *tx++ = cpu_to_be16((chan->address + 2) << 8);
122 *tx++ = cpu_to_be16(chan->address << 8);
123 }
124
125 return 0;
126}
127EXPORT_SYMBOL_NS_GPL(adis_update_scan_mode, "IIO_ADISLIB");
128
129static int adis_paging_trigger_handler(struct adis *adis)
130{
131 int ret;
132
133 guard(mutex)(&adis->state_lock);
134 if (adis->current_page != 0) {
135 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
136 adis->tx[1] = 0;
137 ret = spi_write(adis->spi, adis->tx, 2);
138 if (ret) {
139 dev_err(&adis->spi->dev, "Failed to change device page: %d\n", ret);
140 return ret;
141 }
142
143 adis->current_page = 0;
144 }
145
146 return spi_sync(adis->spi, &adis->msg);
147}
148
149static irqreturn_t adis_trigger_handler(int irq, void *p)
150{
151 struct iio_poll_func *pf = p;
152 struct iio_dev *indio_dev = pf->indio_dev;
153 struct adis *adis = iio_device_get_drvdata(indio_dev);
154 int ret;
155
156 if (adis->data->has_paging)
157 ret = adis_paging_trigger_handler(adis);
158 else
159 ret = spi_sync(adis->spi, &adis->msg);
160 if (ret) {
161 dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
162 goto irq_done;
163 }
164
165 iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
166 pf->timestamp);
167
168irq_done:
169 iio_trigger_notify_done(indio_dev->trig);
170
171 return IRQ_HANDLED;
172}
173
174static void adis_buffer_cleanup(void *arg)
175{
176 struct adis *adis = arg;
177
178 kfree(adis->buffer);
179 kfree(adis->xfer);
180}
181
182/**
183 * devm_adis_setup_buffer_and_trigger_with_attrs() - Sets up buffer and trigger
184 * for the managed adis device with buffer attributes.
185 * @adis: The adis device
186 * @indio_dev: The IIO device
187 * @trigger_handler: Trigger handler: should handle the buffer readings.
188 * @ops: Optional buffer setup functions, may be NULL.
189 * @buffer_attrs: Extra buffer attributes.
190 *
191 * Returns 0 on success, a negative error code otherwise.
192 *
193 * This function sets up the buffer (with buffer setup functions and extra
194 * buffer attributes) and trigger for a adis devices with buffer attributes.
195 */
196int
197devm_adis_setup_buffer_and_trigger_with_attrs(struct adis *adis, struct iio_dev *indio_dev,
198 irq_handler_t trigger_handler,
199 const struct iio_buffer_setup_ops *ops,
200 const struct iio_dev_attr **buffer_attrs)
201{
202 int ret;
203
204 if (!trigger_handler)
205 trigger_handler = adis_trigger_handler;
206
207 ret = devm_iio_triggered_buffer_setup_ext(&adis->spi->dev, indio_dev,
208 &iio_pollfunc_store_time,
209 trigger_handler,
210 IIO_BUFFER_DIRECTION_IN,
211 ops,
212 buffer_attrs);
213 if (ret)
214 return ret;
215
216 if (adis->spi->irq) {
217 ret = devm_adis_probe_trigger(adis, indio_dev);
218 if (ret)
219 return ret;
220 }
221
222 return devm_add_action_or_reset(&adis->spi->dev, adis_buffer_cleanup,
223 adis);
224}
225EXPORT_SYMBOL_NS_GPL(devm_adis_setup_buffer_and_trigger_with_attrs, "IIO_ADISLIB");
1/*
2 * Common library for ADIS16XXX devices
3 *
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
6 *
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <linux/export.h>
11#include <linux/interrupt.h>
12#include <linux/mutex.h>
13#include <linux/kernel.h>
14#include <linux/spi/spi.h>
15#include <linux/slab.h>
16
17#include <linux/iio/iio.h>
18#include <linux/iio/buffer.h>
19#include <linux/iio/trigger_consumer.h>
20#include <linux/iio/triggered_buffer.h>
21#include <linux/iio/imu/adis.h>
22
23int adis_update_scan_mode(struct iio_dev *indio_dev,
24 const unsigned long *scan_mask)
25{
26 struct adis *adis = iio_device_get_drvdata(indio_dev);
27 const struct iio_chan_spec *chan;
28 unsigned int scan_count;
29 unsigned int i, j;
30 __be16 *tx, *rx;
31
32 kfree(adis->xfer);
33 kfree(adis->buffer);
34
35 scan_count = indio_dev->scan_bytes / 2;
36
37 adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
38 if (!adis->xfer)
39 return -ENOMEM;
40
41 adis->buffer = kzalloc(indio_dev->scan_bytes * 2, GFP_KERNEL);
42 if (!adis->buffer)
43 return -ENOMEM;
44
45 rx = adis->buffer;
46 tx = rx + indio_dev->scan_bytes;
47
48 spi_message_init(&adis->msg);
49
50 for (j = 0; j <= scan_count; j++) {
51 adis->xfer[j].bits_per_word = 8;
52 if (j != scan_count)
53 adis->xfer[j].cs_change = 1;
54 adis->xfer[j].len = 2;
55 adis->xfer[j].delay_usecs = adis->data->read_delay;
56 if (j < scan_count)
57 adis->xfer[j].tx_buf = &tx[j];
58 if (j >= 1)
59 adis->xfer[j].rx_buf = &rx[j - 1];
60 spi_message_add_tail(&adis->xfer[j], &adis->msg);
61 }
62
63 chan = indio_dev->channels;
64 for (i = 0; i < indio_dev->num_channels; i++, chan++) {
65 if (!test_bit(chan->scan_index, scan_mask))
66 continue;
67 if (chan->scan_type.storagebits == 32)
68 *tx++ = cpu_to_be16((chan->address + 2) << 8);
69 *tx++ = cpu_to_be16(chan->address << 8);
70 }
71
72 return 0;
73}
74EXPORT_SYMBOL_GPL(adis_update_scan_mode);
75
76static irqreturn_t adis_trigger_handler(int irq, void *p)
77{
78 struct iio_poll_func *pf = p;
79 struct iio_dev *indio_dev = pf->indio_dev;
80 struct adis *adis = iio_device_get_drvdata(indio_dev);
81 int ret;
82
83 if (!adis->buffer)
84 return -ENOMEM;
85
86 if (adis->data->has_paging) {
87 mutex_lock(&adis->txrx_lock);
88 if (adis->current_page != 0) {
89 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
90 adis->tx[1] = 0;
91 spi_write(adis->spi, adis->tx, 2);
92 }
93 }
94
95 ret = spi_sync(adis->spi, &adis->msg);
96 if (ret)
97 dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
98
99
100 if (adis->data->has_paging) {
101 adis->current_page = 0;
102 mutex_unlock(&adis->txrx_lock);
103 }
104
105 iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
106 pf->timestamp);
107
108 iio_trigger_notify_done(indio_dev->trig);
109
110 return IRQ_HANDLED;
111}
112
113/**
114 * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
115 * @adis: The adis device.
116 * @indio_dev: The IIO device.
117 * @trigger_handler: Optional trigger handler, may be NULL.
118 *
119 * Returns 0 on success, a negative error code otherwise.
120 *
121 * This function sets up the buffer and trigger for a adis devices. If
122 * 'trigger_handler' is NULL the default trigger handler will be used. The
123 * default trigger handler will simply read the registers assigned to the
124 * currently active channels.
125 *
126 * adis_cleanup_buffer_and_trigger() should be called to free the resources
127 * allocated by this function.
128 */
129int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
130 irqreturn_t (*trigger_handler)(int, void *))
131{
132 int ret;
133
134 if (!trigger_handler)
135 trigger_handler = adis_trigger_handler;
136
137 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
138 trigger_handler, NULL);
139 if (ret)
140 return ret;
141
142 if (adis->spi->irq) {
143 ret = adis_probe_trigger(adis, indio_dev);
144 if (ret)
145 goto error_buffer_cleanup;
146 }
147 return 0;
148
149error_buffer_cleanup:
150 iio_triggered_buffer_cleanup(indio_dev);
151 return ret;
152}
153EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
154
155/**
156 * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
157 * @adis: The adis device.
158 * @indio_dev: The IIO device.
159 *
160 * Frees resources allocated by adis_setup_buffer_and_trigger()
161 */
162void adis_cleanup_buffer_and_trigger(struct adis *adis,
163 struct iio_dev *indio_dev)
164{
165 if (adis->spi->irq)
166 adis_remove_trigger(adis);
167 kfree(adis->buffer);
168 kfree(adis->xfer);
169 iio_triggered_buffer_cleanup(indio_dev);
170}
171EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);