Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / iio / adc / ti-adc084s021.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /**
3  * Copyright (C) 2017 Axis Communications AB
4  *
5  * Driver for Texas Instruments' ADC084S021 ADC chip.
6  * Datasheets can be found here:
7  * http://www.ti.com/lit/ds/symlink/adc084s021.pdf
8  */
9
10 #include <linux/err.h>
11 #include <linux/spi/spi.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/triggered_buffer.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/regulator/consumer.h>
19
20 #define ADC084S021_DRIVER_NAME "adc084s021"
21
22 struct adc084s021 {
23         struct spi_device *spi;
24         struct spi_message message;
25         struct spi_transfer spi_trans;
26         struct regulator *reg;
27         struct mutex lock;
28         /*
29          * DMA (thus cache coherency maintenance) requires the
30          * transfer buffers to live in their own cache line.
31          */
32         u16 tx_buf[4] ____cacheline_aligned;
33         __be16 rx_buf[5]; /* First 16-bits are trash */
34 };
35
36 #define ADC084S021_VOLTAGE_CHANNEL(num)                  \
37         {                                                      \
38                 .type = IIO_VOLTAGE,                                 \
39                 .channel = (num),                                    \
40                 .indexed = 1,                                        \
41                 .scan_index = (num),                                 \
42                 .scan_type = {                                       \
43                         .sign = 'u',                                       \
44                         .realbits = 8,                                     \
45                         .storagebits = 16,                                 \
46                         .shift = 4,                                        \
47                         .endianness = IIO_BE,                              \
48                 },                                                   \
49                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),        \
50                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
51         }
52
53 static const struct iio_chan_spec adc084s021_channels[] = {
54         ADC084S021_VOLTAGE_CHANNEL(0),
55         ADC084S021_VOLTAGE_CHANNEL(1),
56         ADC084S021_VOLTAGE_CHANNEL(2),
57         ADC084S021_VOLTAGE_CHANNEL(3),
58         IIO_CHAN_SOFT_TIMESTAMP(4),
59 };
60
61 /**
62  * Read an ADC channel and return its value.
63  *
64  * @adc: The ADC SPI data.
65  * @data: Buffer for converted data.
66  */
67 static int adc084s021_adc_conversion(struct adc084s021 *adc, void *data)
68 {
69         int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word */
70         int ret, i = 0;
71         u16 *p = data;
72
73         /* Do the transfer */
74         ret = spi_sync(adc->spi, &adc->message);
75         if (ret < 0)
76                 return ret;
77
78         for (; i < n_words; i++)
79                 *(p + i) = adc->rx_buf[i + 1];
80
81         return ret;
82 }
83
84 static int adc084s021_read_raw(struct iio_dev *indio_dev,
85                            struct iio_chan_spec const *channel, int *val,
86                            int *val2, long mask)
87 {
88         struct adc084s021 *adc = iio_priv(indio_dev);
89         int ret;
90
91         switch (mask) {
92         case IIO_CHAN_INFO_RAW:
93                 ret = iio_device_claim_direct_mode(indio_dev);
94                 if (ret < 0)
95                         return ret;
96
97                 ret = regulator_enable(adc->reg);
98                 if (ret) {
99                         iio_device_release_direct_mode(indio_dev);
100                         return ret;
101                 }
102
103                 adc->tx_buf[0] = channel->channel << 3;
104                 ret = adc084s021_adc_conversion(adc, val);
105                 iio_device_release_direct_mode(indio_dev);
106                 regulator_disable(adc->reg);
107                 if (ret < 0)
108                         return ret;
109
110                 *val = be16_to_cpu(*val);
111                 *val = (*val >> channel->scan_type.shift) & 0xff;
112
113                 return IIO_VAL_INT;
114         case IIO_CHAN_INFO_SCALE:
115                 ret = regulator_enable(adc->reg);
116                 if (ret)
117                         return ret;
118
119                 ret = regulator_get_voltage(adc->reg);
120                 regulator_disable(adc->reg);
121                 if (ret < 0)
122                         return ret;
123
124                 *val = ret / 1000;
125
126                 return IIO_VAL_INT;
127         default:
128                 return -EINVAL;
129         }
130 }
131
132 /**
133  * Read enabled ADC channels and push data to the buffer.
134  *
135  * @irq: The interrupt number (not used).
136  * @pollfunc: Pointer to the poll func.
137  */
138 static irqreturn_t adc084s021_buffer_trigger_handler(int irq, void *pollfunc)
139 {
140         struct iio_poll_func *pf = pollfunc;
141         struct iio_dev *indio_dev = pf->indio_dev;
142         struct adc084s021 *adc = iio_priv(indio_dev);
143         __be16 data[8] = {0}; /* 4 * 16-bit words of data + 8 bytes timestamp */
144
145         mutex_lock(&adc->lock);
146
147         if (adc084s021_adc_conversion(adc, &data) < 0)
148                 dev_err(&adc->spi->dev, "Failed to read data\n");
149
150         iio_push_to_buffers_with_timestamp(indio_dev, data,
151                                            iio_get_time_ns(indio_dev));
152         mutex_unlock(&adc->lock);
153         iio_trigger_notify_done(indio_dev->trig);
154
155         return IRQ_HANDLED;
156 }
157
158 static int adc084s021_buffer_preenable(struct iio_dev *indio_dev)
159 {
160         struct adc084s021 *adc = iio_priv(indio_dev);
161         int scan_index;
162         int i = 0;
163
164         for_each_set_bit(scan_index, indio_dev->active_scan_mask,
165                          indio_dev->masklength) {
166                 const struct iio_chan_spec *channel =
167                         &indio_dev->channels[scan_index];
168                 adc->tx_buf[i++] = channel->channel << 3;
169         }
170         adc->spi_trans.len = 2 + (i * sizeof(__be16)); /* Trash + channels */
171
172         return regulator_enable(adc->reg);
173 }
174
175 static int adc084s021_buffer_postdisable(struct iio_dev *indio_dev)
176 {
177         struct adc084s021 *adc = iio_priv(indio_dev);
178
179         adc->spi_trans.len = 4; /* Trash + single channel */
180
181         return regulator_disable(adc->reg);
182 }
183
184 static const struct iio_info adc084s021_info = {
185         .read_raw = adc084s021_read_raw,
186 };
187
188 static const struct iio_buffer_setup_ops adc084s021_buffer_setup_ops = {
189         .preenable = adc084s021_buffer_preenable,
190         .postenable = iio_triggered_buffer_postenable,
191         .predisable = iio_triggered_buffer_predisable,
192         .postdisable = adc084s021_buffer_postdisable,
193 };
194
195 static int adc084s021_probe(struct spi_device *spi)
196 {
197         struct iio_dev *indio_dev;
198         struct adc084s021 *adc;
199         int ret;
200
201         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
202         if (!indio_dev) {
203                 dev_err(&spi->dev, "Failed to allocate IIO device\n");
204                 return -ENOMEM;
205         }
206
207         adc = iio_priv(indio_dev);
208         adc->spi = spi;
209
210         /* Connect the SPI device and the iio dev */
211         spi_set_drvdata(spi, indio_dev);
212
213         /* Initiate the Industrial I/O device */
214         indio_dev->dev.parent = &spi->dev;
215         indio_dev->dev.of_node = spi->dev.of_node;
216         indio_dev->name = spi_get_device_id(spi)->name;
217         indio_dev->modes = INDIO_DIRECT_MODE;
218         indio_dev->info = &adc084s021_info;
219         indio_dev->channels = adc084s021_channels;
220         indio_dev->num_channels = ARRAY_SIZE(adc084s021_channels);
221
222         /* Create SPI transfer for channel reads */
223         adc->spi_trans.tx_buf = adc->tx_buf;
224         adc->spi_trans.rx_buf = adc->rx_buf;
225         adc->spi_trans.len = 4; /* Trash + single channel */
226         spi_message_init_with_transfers(&adc->message, &adc->spi_trans, 1);
227
228         adc->reg = devm_regulator_get(&spi->dev, "vref");
229         if (IS_ERR(adc->reg))
230                 return PTR_ERR(adc->reg);
231
232         mutex_init(&adc->lock);
233
234         /* Setup triggered buffer with pollfunction */
235         ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
236                                             adc084s021_buffer_trigger_handler,
237                                             &adc084s021_buffer_setup_ops);
238         if (ret) {
239                 dev_err(&spi->dev, "Failed to setup triggered buffer\n");
240                 return ret;
241         }
242
243         return devm_iio_device_register(&spi->dev, indio_dev);
244 }
245
246 static const struct of_device_id adc084s021_of_match[] = {
247         { .compatible = "ti,adc084s021", },
248         {},
249 };
250 MODULE_DEVICE_TABLE(of, adc084s021_of_match);
251
252 static const struct spi_device_id adc084s021_id[] = {
253         { ADC084S021_DRIVER_NAME, 0},
254         {}
255 };
256 MODULE_DEVICE_TABLE(spi, adc084s021_id);
257
258 static struct spi_driver adc084s021_driver = {
259         .driver = {
260                 .name = ADC084S021_DRIVER_NAME,
261                 .of_match_table = of_match_ptr(adc084s021_of_match),
262         },
263         .probe = adc084s021_probe,
264         .id_table = adc084s021_id,
265 };
266 module_spi_driver(adc084s021_driver);
267
268 MODULE_AUTHOR("MÃ¥rten Lindahl <martenli@axis.com>");
269 MODULE_DESCRIPTION("Texas Instruments ADC084S021");
270 MODULE_LICENSE("GPL v2");
271 MODULE_VERSION("1.0");