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
adis_update_scan_mode(struct iio_dev * indio_dev,const unsigned long * scan_mask)23 int 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 = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
42 if (!adis->buffer) {
43 kfree(adis->xfer);
44 adis->xfer = NULL;
45 return -ENOMEM;
46 }
47
48 rx = adis->buffer;
49 tx = rx + scan_count;
50
51 spi_message_init(&adis->msg);
52
53 for (j = 0; j <= scan_count; j++) {
54 adis->xfer[j].bits_per_word = 8;
55 if (j != scan_count)
56 adis->xfer[j].cs_change = 1;
57 adis->xfer[j].len = 2;
58 adis->xfer[j].delay_usecs = adis->data->read_delay;
59 if (j < scan_count)
60 adis->xfer[j].tx_buf = &tx[j];
61 if (j >= 1)
62 adis->xfer[j].rx_buf = &rx[j - 1];
63 spi_message_add_tail(&adis->xfer[j], &adis->msg);
64 }
65
66 chan = indio_dev->channels;
67 for (i = 0; i < indio_dev->num_channels; i++, chan++) {
68 if (!test_bit(chan->scan_index, scan_mask))
69 continue;
70 if (chan->scan_type.storagebits == 32)
71 *tx++ = cpu_to_be16((chan->address + 2) << 8);
72 *tx++ = cpu_to_be16(chan->address << 8);
73 }
74
75 return 0;
76 }
77 EXPORT_SYMBOL_GPL(adis_update_scan_mode);
78
adis_trigger_handler(int irq,void * p)79 static irqreturn_t adis_trigger_handler(int irq, void *p)
80 {
81 struct iio_poll_func *pf = p;
82 struct iio_dev *indio_dev = pf->indio_dev;
83 struct adis *adis = iio_device_get_drvdata(indio_dev);
84 int ret;
85
86 if (!adis->buffer)
87 return -ENOMEM;
88
89 if (adis->data->has_paging) {
90 mutex_lock(&adis->txrx_lock);
91 if (adis->current_page != 0) {
92 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
93 adis->tx[1] = 0;
94 spi_write(adis->spi, adis->tx, 2);
95 }
96 }
97
98 ret = spi_sync(adis->spi, &adis->msg);
99 if (ret)
100 dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
101
102
103 if (adis->data->has_paging) {
104 adis->current_page = 0;
105 mutex_unlock(&adis->txrx_lock);
106 }
107
108 iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
109 pf->timestamp);
110
111 iio_trigger_notify_done(indio_dev->trig);
112
113 return IRQ_HANDLED;
114 }
115
116 /**
117 * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
118 * @adis: The adis device.
119 * @indio_dev: The IIO device.
120 * @trigger_handler: Optional trigger handler, may be NULL.
121 *
122 * Returns 0 on success, a negative error code otherwise.
123 *
124 * This function sets up the buffer and trigger for a adis devices. If
125 * 'trigger_handler' is NULL the default trigger handler will be used. The
126 * default trigger handler will simply read the registers assigned to the
127 * currently active channels.
128 *
129 * adis_cleanup_buffer_and_trigger() should be called to free the resources
130 * allocated by this function.
131 */
adis_setup_buffer_and_trigger(struct adis * adis,struct iio_dev * indio_dev,irqreturn_t (* trigger_handler)(int,void *))132 int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
133 irqreturn_t (*trigger_handler)(int, void *))
134 {
135 int ret;
136
137 if (!trigger_handler)
138 trigger_handler = adis_trigger_handler;
139
140 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
141 trigger_handler, NULL);
142 if (ret)
143 return ret;
144
145 if (adis->spi->irq) {
146 ret = adis_probe_trigger(adis, indio_dev);
147 if (ret)
148 goto error_buffer_cleanup;
149 }
150 return 0;
151
152 error_buffer_cleanup:
153 iio_triggered_buffer_cleanup(indio_dev);
154 return ret;
155 }
156 EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
157
158 /**
159 * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
160 * @adis: The adis device.
161 * @indio_dev: The IIO device.
162 *
163 * Frees resources allocated by adis_setup_buffer_and_trigger()
164 */
adis_cleanup_buffer_and_trigger(struct adis * adis,struct iio_dev * indio_dev)165 void adis_cleanup_buffer_and_trigger(struct adis *adis,
166 struct iio_dev *indio_dev)
167 {
168 if (adis->spi->irq)
169 adis_remove_trigger(adis);
170 kfree(adis->buffer);
171 kfree(adis->xfer);
172 iio_triggered_buffer_cleanup(indio_dev);
173 }
174 EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);
175