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