GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / iio / adc / ti-adc0832.c
1 /*
2  * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
3  *
4  * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License.  See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * Datasheet: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
11  */
12
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21
22 enum {
23         adc0831,
24         adc0832,
25         adc0834,
26         adc0838,
27 };
28
29 struct adc0832 {
30         struct spi_device *spi;
31         struct regulator *reg;
32         struct mutex lock;
33         u8 mux_bits;
34         /*
35          * Max size needed: 16x 1 byte ADC data + 8 bytes timestamp
36          * May be shorter if not all channels are enabled subject
37          * to the timestamp remaining 8 byte aligned.
38          */
39         u8 data[24] __aligned(8);
40
41         u8 tx_buf[2] ____cacheline_aligned;
42         u8 rx_buf[2];
43 };
44
45 #define ADC0832_VOLTAGE_CHANNEL(chan)                                   \
46         {                                                               \
47                 .type = IIO_VOLTAGE,                                    \
48                 .indexed = 1,                                           \
49                 .channel = chan,                                        \
50                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
51                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
52                 .scan_index = chan,                                     \
53                 .scan_type = {                                          \
54                         .sign = 'u',                                    \
55                         .realbits = 8,                                  \
56                         .storagebits = 8,                               \
57                 },                                                      \
58         }
59
60 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si)                  \
61         {                                                               \
62                 .type = IIO_VOLTAGE,                                    \
63                 .indexed = 1,                                           \
64                 .channel = (chan1),                                     \
65                 .channel2 = (chan2),                                    \
66                 .differential = 1,                                      \
67                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
68                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
69                 .scan_index = si,                                       \
70                 .scan_type = {                                          \
71                         .sign = 'u',                                    \
72                         .realbits = 8,                                  \
73                         .storagebits = 8,                               \
74                 },                                                      \
75         }
76
77 static const struct iio_chan_spec adc0831_channels[] = {
78         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
79         IIO_CHAN_SOFT_TIMESTAMP(1),
80 };
81
82 static const struct iio_chan_spec adc0832_channels[] = {
83         ADC0832_VOLTAGE_CHANNEL(0),
84         ADC0832_VOLTAGE_CHANNEL(1),
85         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
86         ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
87         IIO_CHAN_SOFT_TIMESTAMP(4),
88 };
89
90 static const struct iio_chan_spec adc0834_channels[] = {
91         ADC0832_VOLTAGE_CHANNEL(0),
92         ADC0832_VOLTAGE_CHANNEL(1),
93         ADC0832_VOLTAGE_CHANNEL(2),
94         ADC0832_VOLTAGE_CHANNEL(3),
95         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
96         ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
97         ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
98         ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
99         IIO_CHAN_SOFT_TIMESTAMP(8),
100 };
101
102 static const struct iio_chan_spec adc0838_channels[] = {
103         ADC0832_VOLTAGE_CHANNEL(0),
104         ADC0832_VOLTAGE_CHANNEL(1),
105         ADC0832_VOLTAGE_CHANNEL(2),
106         ADC0832_VOLTAGE_CHANNEL(3),
107         ADC0832_VOLTAGE_CHANNEL(4),
108         ADC0832_VOLTAGE_CHANNEL(5),
109         ADC0832_VOLTAGE_CHANNEL(6),
110         ADC0832_VOLTAGE_CHANNEL(7),
111         ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
112         ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
113         ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
114         ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
115         ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
116         ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
117         ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
118         ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
119         IIO_CHAN_SOFT_TIMESTAMP(16),
120 };
121
122 static int adc0831_adc_conversion(struct adc0832 *adc)
123 {
124         struct spi_device *spi = adc->spi;
125         int ret;
126
127         ret = spi_read(spi, &adc->rx_buf, 2);
128         if (ret)
129                 return ret;
130
131         /*
132          * Skip TRI-STATE and a leading zero
133          */
134         return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
135 }
136
137 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
138                                 bool differential)
139 {
140         struct spi_device *spi = adc->spi;
141         struct spi_transfer xfer = {
142                 .tx_buf = adc->tx_buf,
143                 .rx_buf = adc->rx_buf,
144                 .len = 2,
145         };
146         int ret;
147
148         if (!adc->mux_bits)
149                 return adc0831_adc_conversion(adc);
150
151         /* start bit */
152         adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
153         /* single-ended or differential */
154         adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
155         /* odd / sign */
156         adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
157         /* select */
158         if (adc->mux_bits > 1)
159                 adc->tx_buf[0] |= channel / 2;
160
161         /* align Data output BIT7 (MSB) to 8-bit boundary */
162         adc->tx_buf[0] <<= 1;
163
164         ret = spi_sync_transfer(spi, &xfer, 1);
165         if (ret)
166                 return ret;
167
168         return adc->rx_buf[1];
169 }
170
171 static int adc0832_read_raw(struct iio_dev *iio,
172                         struct iio_chan_spec const *channel, int *value,
173                         int *shift, long mask)
174 {
175         struct adc0832 *adc = iio_priv(iio);
176
177         switch (mask) {
178         case IIO_CHAN_INFO_RAW:
179                 mutex_lock(&adc->lock);
180                 *value = adc0832_adc_conversion(adc, channel->channel,
181                                                 channel->differential);
182                 mutex_unlock(&adc->lock);
183                 if (*value < 0)
184                         return *value;
185
186                 return IIO_VAL_INT;
187         case IIO_CHAN_INFO_SCALE:
188                 *value = regulator_get_voltage(adc->reg);
189                 if (*value < 0)
190                         return *value;
191
192                 /* convert regulator output voltage to mV */
193                 *value /= 1000;
194                 *shift = 8;
195
196                 return IIO_VAL_FRACTIONAL_LOG2;
197         }
198
199         return -EINVAL;
200 }
201
202 static const struct iio_info adc0832_info = {
203         .read_raw = adc0832_read_raw,
204 };
205
206 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
207 {
208         struct iio_poll_func *pf = p;
209         struct iio_dev *indio_dev = pf->indio_dev;
210         struct adc0832 *adc = iio_priv(indio_dev);
211         int scan_index;
212         int i = 0;
213
214         mutex_lock(&adc->lock);
215
216         for_each_set_bit(scan_index, indio_dev->active_scan_mask,
217                          indio_dev->masklength) {
218                 const struct iio_chan_spec *scan_chan =
219                                 &indio_dev->channels[scan_index];
220                 int ret = adc0832_adc_conversion(adc, scan_chan->channel,
221                                                  scan_chan->differential);
222                 if (ret < 0) {
223                         dev_warn(&adc->spi->dev,
224                                  "failed to get conversion data\n");
225                         goto out;
226                 }
227
228                 adc->data[i] = ret;
229                 i++;
230         }
231         iio_push_to_buffers_with_timestamp(indio_dev, adc->data,
232                                            iio_get_time_ns(indio_dev));
233 out:
234         mutex_unlock(&adc->lock);
235
236         iio_trigger_notify_done(indio_dev->trig);
237
238         return IRQ_HANDLED;
239 }
240
241 static int adc0832_probe(struct spi_device *spi)
242 {
243         struct iio_dev *indio_dev;
244         struct adc0832 *adc;
245         int ret;
246
247         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
248         if (!indio_dev)
249                 return -ENOMEM;
250
251         adc = iio_priv(indio_dev);
252         adc->spi = spi;
253         mutex_init(&adc->lock);
254
255         indio_dev->name = spi_get_device_id(spi)->name;
256         indio_dev->dev.parent = &spi->dev;
257         indio_dev->dev.of_node = spi->dev.of_node;
258         indio_dev->info = &adc0832_info;
259         indio_dev->modes = INDIO_DIRECT_MODE;
260
261         switch (spi_get_device_id(spi)->driver_data) {
262         case adc0831:
263                 adc->mux_bits = 0;
264                 indio_dev->channels = adc0831_channels;
265                 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
266                 break;
267         case adc0832:
268                 adc->mux_bits = 1;
269                 indio_dev->channels = adc0832_channels;
270                 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
271                 break;
272         case adc0834:
273                 adc->mux_bits = 2;
274                 indio_dev->channels = adc0834_channels;
275                 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
276                 break;
277         case adc0838:
278                 adc->mux_bits = 3;
279                 indio_dev->channels = adc0838_channels;
280                 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
281                 break;
282         default:
283                 return -EINVAL;
284         }
285
286         adc->reg = devm_regulator_get(&spi->dev, "vref");
287         if (IS_ERR(adc->reg))
288                 return PTR_ERR(adc->reg);
289
290         ret = regulator_enable(adc->reg);
291         if (ret)
292                 return ret;
293
294         spi_set_drvdata(spi, indio_dev);
295
296         ret = iio_triggered_buffer_setup(indio_dev, NULL,
297                                          adc0832_trigger_handler, NULL);
298         if (ret)
299                 goto err_reg_disable;
300
301         ret = iio_device_register(indio_dev);
302         if (ret)
303                 goto err_buffer_cleanup;
304
305         return 0;
306 err_buffer_cleanup:
307         iio_triggered_buffer_cleanup(indio_dev);
308 err_reg_disable:
309         regulator_disable(adc->reg);
310
311         return ret;
312 }
313
314 static int adc0832_remove(struct spi_device *spi)
315 {
316         struct iio_dev *indio_dev = spi_get_drvdata(spi);
317         struct adc0832 *adc = iio_priv(indio_dev);
318
319         iio_device_unregister(indio_dev);
320         iio_triggered_buffer_cleanup(indio_dev);
321         regulator_disable(adc->reg);
322
323         return 0;
324 }
325
326 #ifdef CONFIG_OF
327
328 static const struct of_device_id adc0832_dt_ids[] = {
329         { .compatible = "ti,adc0831", },
330         { .compatible = "ti,adc0832", },
331         { .compatible = "ti,adc0834", },
332         { .compatible = "ti,adc0838", },
333         {}
334 };
335 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
336
337 #endif
338
339 static const struct spi_device_id adc0832_id[] = {
340         { "adc0831", adc0831 },
341         { "adc0832", adc0832 },
342         { "adc0834", adc0834 },
343         { "adc0838", adc0838 },
344         {}
345 };
346 MODULE_DEVICE_TABLE(spi, adc0832_id);
347
348 static struct spi_driver adc0832_driver = {
349         .driver = {
350                 .name = "adc0832",
351                 .of_match_table = of_match_ptr(adc0832_dt_ids),
352         },
353         .probe = adc0832_probe,
354         .remove = adc0832_remove,
355         .id_table = adc0832_id,
356 };
357 module_spi_driver(adc0832_driver);
358
359 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
360 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
361 MODULE_LICENSE("GPL v2");