1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Common library for ADIS16XXX devices
5 * Copyright 2012 Analog Devices Inc.
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>
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>
22 static int adis_update_scan_mode_burst(struct iio_dev *indio_dev,
23 const unsigned long *scan_mask)
25 struct adis *adis = iio_device_get_drvdata(indio_dev);
26 unsigned int burst_length;
29 /* All but the timestamp channel */
30 burst_length = (indio_dev->num_channels - 1) * sizeof(u16);
31 burst_length += adis->burst->extra_len;
33 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
37 adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL);
44 tx = adis->buffer + burst_length;
45 tx[0] = ADIS_READ_REG(adis->burst->reg_cmd);
48 adis->xfer[0].tx_buf = tx;
49 adis->xfer[0].bits_per_word = 8;
50 adis->xfer[0].len = 2;
51 adis->xfer[1].rx_buf = adis->buffer;
52 adis->xfer[1].bits_per_word = 8;
53 adis->xfer[1].len = burst_length;
55 spi_message_init(&adis->msg);
56 spi_message_add_tail(&adis->xfer[0], &adis->msg);
57 spi_message_add_tail(&adis->xfer[1], &adis->msg);
62 int adis_update_scan_mode(struct iio_dev *indio_dev,
63 const unsigned long *scan_mask)
65 struct adis *adis = iio_device_get_drvdata(indio_dev);
66 const struct iio_chan_spec *chan;
67 unsigned int scan_count;
74 if (adis->burst && adis->burst->en)
75 return adis_update_scan_mode_burst(indio_dev, scan_mask);
77 scan_count = indio_dev->scan_bytes / 2;
79 adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
83 adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
93 spi_message_init(&adis->msg);
95 for (j = 0; j <= scan_count; j++) {
96 adis->xfer[j].bits_per_word = 8;
98 adis->xfer[j].cs_change = 1;
99 adis->xfer[j].len = 2;
100 adis->xfer[j].delay_usecs = adis->data->read_delay;
102 adis->xfer[j].tx_buf = &tx[j];
104 adis->xfer[j].rx_buf = &rx[j - 1];
105 spi_message_add_tail(&adis->xfer[j], &adis->msg);
108 chan = indio_dev->channels;
109 for (i = 0; i < indio_dev->num_channels; i++, chan++) {
110 if (!test_bit(chan->scan_index, scan_mask))
112 if (chan->scan_type.storagebits == 32)
113 *tx++ = cpu_to_be16((chan->address + 2) << 8);
114 *tx++ = cpu_to_be16(chan->address << 8);
119 EXPORT_SYMBOL_GPL(adis_update_scan_mode);
121 static irqreturn_t adis_trigger_handler(int irq, void *p)
123 struct iio_poll_func *pf = p;
124 struct iio_dev *indio_dev = pf->indio_dev;
125 struct adis *adis = iio_device_get_drvdata(indio_dev);
131 if (adis->data->has_paging) {
132 mutex_lock(&adis->state_lock);
133 if (adis->current_page != 0) {
134 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
136 spi_write(adis->spi, adis->tx, 2);
140 ret = spi_sync(adis->spi, &adis->msg);
142 dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
145 if (adis->data->has_paging) {
146 adis->current_page = 0;
147 mutex_unlock(&adis->state_lock);
150 iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
153 iio_trigger_notify_done(indio_dev->trig);
159 * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
160 * @adis: The adis device.
161 * @indio_dev: The IIO device.
162 * @trigger_handler: Optional trigger handler, may be NULL.
164 * Returns 0 on success, a negative error code otherwise.
166 * This function sets up the buffer and trigger for a adis devices. If
167 * 'trigger_handler' is NULL the default trigger handler will be used. The
168 * default trigger handler will simply read the registers assigned to the
169 * currently active channels.
171 * adis_cleanup_buffer_and_trigger() should be called to free the resources
172 * allocated by this function.
174 int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
175 irqreturn_t (*trigger_handler)(int, void *))
179 if (!trigger_handler)
180 trigger_handler = adis_trigger_handler;
182 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
183 trigger_handler, NULL);
187 if (adis->spi->irq) {
188 ret = adis_probe_trigger(adis, indio_dev);
190 goto error_buffer_cleanup;
194 error_buffer_cleanup:
195 iio_triggered_buffer_cleanup(indio_dev);
198 EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
201 * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
202 * @adis: The adis device.
203 * @indio_dev: The IIO device.
205 * Frees resources allocated by adis_setup_buffer_and_trigger()
207 void adis_cleanup_buffer_and_trigger(struct adis *adis,
208 struct iio_dev *indio_dev)
211 adis_remove_trigger(adis);
214 iio_triggered_buffer_cleanup(indio_dev);
216 EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);