xref: /openbmc/linux/drivers/iio/imu/adis_buffer.c (revision 3dc4b6fb)
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 
22 static 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;
27 	u8 *tx;
28 
29 	/* All but the timestamp channel */
30 	burst_length = (indio_dev->num_channels - 1) * sizeof(u16);
31 	burst_length += adis->burst->extra_len;
32 
33 	adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
34 	if (!adis->xfer)
35 		return -ENOMEM;
36 
37 	adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL);
38 	if (!adis->buffer)
39 		return -ENOMEM;
40 
41 	tx = adis->buffer + burst_length;
42 	tx[0] = ADIS_READ_REG(adis->burst->reg_cmd);
43 	tx[1] = 0;
44 
45 	adis->xfer[0].tx_buf = tx;
46 	adis->xfer[0].bits_per_word = 8;
47 	adis->xfer[0].len = 2;
48 	adis->xfer[1].rx_buf = adis->buffer;
49 	adis->xfer[1].bits_per_word = 8;
50 	adis->xfer[1].len = burst_length;
51 
52 	spi_message_init(&adis->msg);
53 	spi_message_add_tail(&adis->xfer[0], &adis->msg);
54 	spi_message_add_tail(&adis->xfer[1], &adis->msg);
55 
56 	return 0;
57 }
58 
59 int adis_update_scan_mode(struct iio_dev *indio_dev,
60 	const unsigned long *scan_mask)
61 {
62 	struct adis *adis = iio_device_get_drvdata(indio_dev);
63 	const struct iio_chan_spec *chan;
64 	unsigned int scan_count;
65 	unsigned int i, j;
66 	__be16 *tx, *rx;
67 
68 	kfree(adis->xfer);
69 	kfree(adis->buffer);
70 
71 	if (adis->burst && adis->burst->en)
72 		return adis_update_scan_mode_burst(indio_dev, scan_mask);
73 
74 	scan_count = indio_dev->scan_bytes / 2;
75 
76 	adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
77 	if (!adis->xfer)
78 		return -ENOMEM;
79 
80 	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
81 	if (!adis->buffer)
82 		return -ENOMEM;
83 
84 	rx = adis->buffer;
85 	tx = rx + scan_count;
86 
87 	spi_message_init(&adis->msg);
88 
89 	for (j = 0; j <= scan_count; j++) {
90 		adis->xfer[j].bits_per_word = 8;
91 		if (j != scan_count)
92 			adis->xfer[j].cs_change = 1;
93 		adis->xfer[j].len = 2;
94 		adis->xfer[j].delay_usecs = adis->data->read_delay;
95 		if (j < scan_count)
96 			adis->xfer[j].tx_buf = &tx[j];
97 		if (j >= 1)
98 			adis->xfer[j].rx_buf = &rx[j - 1];
99 		spi_message_add_tail(&adis->xfer[j], &adis->msg);
100 	}
101 
102 	chan = indio_dev->channels;
103 	for (i = 0; i < indio_dev->num_channels; i++, chan++) {
104 		if (!test_bit(chan->scan_index, scan_mask))
105 			continue;
106 		if (chan->scan_type.storagebits == 32)
107 			*tx++ = cpu_to_be16((chan->address + 2) << 8);
108 		*tx++ = cpu_to_be16(chan->address << 8);
109 	}
110 
111 	return 0;
112 }
113 EXPORT_SYMBOL_GPL(adis_update_scan_mode);
114 
115 static irqreturn_t adis_trigger_handler(int irq, void *p)
116 {
117 	struct iio_poll_func *pf = p;
118 	struct iio_dev *indio_dev = pf->indio_dev;
119 	struct adis *adis = iio_device_get_drvdata(indio_dev);
120 	int ret;
121 
122 	if (!adis->buffer)
123 		return -ENOMEM;
124 
125 	if (adis->data->has_paging) {
126 		mutex_lock(&adis->txrx_lock);
127 		if (adis->current_page != 0) {
128 			adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
129 			adis->tx[1] = 0;
130 			spi_write(adis->spi, adis->tx, 2);
131 		}
132 	}
133 
134 	ret = spi_sync(adis->spi, &adis->msg);
135 	if (ret)
136 		dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
137 
138 
139 	if (adis->data->has_paging) {
140 		adis->current_page = 0;
141 		mutex_unlock(&adis->txrx_lock);
142 	}
143 
144 	iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
145 		pf->timestamp);
146 
147 	iio_trigger_notify_done(indio_dev->trig);
148 
149 	return IRQ_HANDLED;
150 }
151 
152 /**
153  * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
154  * @adis: The adis device.
155  * @indio_dev: The IIO device.
156  * @trigger_handler: Optional trigger handler, may be NULL.
157  *
158  * Returns 0 on success, a negative error code otherwise.
159  *
160  * This function sets up the buffer and trigger for a adis devices.  If
161  * 'trigger_handler' is NULL the default trigger handler will be used. The
162  * default trigger handler will simply read the registers assigned to the
163  * currently active channels.
164  *
165  * adis_cleanup_buffer_and_trigger() should be called to free the resources
166  * allocated by this function.
167  */
168 int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
169 	irqreturn_t (*trigger_handler)(int, void *))
170 {
171 	int ret;
172 
173 	if (!trigger_handler)
174 		trigger_handler = adis_trigger_handler;
175 
176 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
177 		trigger_handler, NULL);
178 	if (ret)
179 		return ret;
180 
181 	if (adis->spi->irq) {
182 		ret = adis_probe_trigger(adis, indio_dev);
183 		if (ret)
184 			goto error_buffer_cleanup;
185 	}
186 	return 0;
187 
188 error_buffer_cleanup:
189 	iio_triggered_buffer_cleanup(indio_dev);
190 	return ret;
191 }
192 EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
193 
194 /**
195  * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
196  * @adis: The adis device.
197  * @indio_dev: The IIO device.
198  *
199  * Frees resources allocated by adis_setup_buffer_and_trigger()
200  */
201 void adis_cleanup_buffer_and_trigger(struct adis *adis,
202 	struct iio_dev *indio_dev)
203 {
204 	if (adis->spi->irq)
205 		adis_remove_trigger(adis);
206 	kfree(adis->buffer);
207 	kfree(adis->xfer);
208 	iio_triggered_buffer_cleanup(indio_dev);
209 }
210 EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);
211