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