1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
5 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
7 * Datasheet: https://www.ti.com/lit/ds/symlink/adc0832-n.pdf
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/spi/spi.h>
13 #include <linux/iio/iio.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/trigger.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/iio/trigger_consumer.h>
28 struct spi_device
*spi
;
29 struct regulator
*reg
;
33 * Max size needed: 16x 1 byte ADC data + 8 bytes timestamp
34 * May be shorter if not all channels are enabled subject
35 * to the timestamp remaining 8 byte aligned.
37 u8 data
[24] __aligned(8);
39 u8 tx_buf
[2] ____cacheline_aligned
;
43 #define ADC0832_VOLTAGE_CHANNEL(chan) \
45 .type = IIO_VOLTAGE, \
48 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
49 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
58 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
60 .type = IIO_VOLTAGE, \
63 .channel2 = (chan2), \
65 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
66 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
75 static const struct iio_chan_spec adc0831_channels
[] = {
76 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
77 IIO_CHAN_SOFT_TIMESTAMP(1),
80 static const struct iio_chan_spec adc0832_channels
[] = {
81 ADC0832_VOLTAGE_CHANNEL(0),
82 ADC0832_VOLTAGE_CHANNEL(1),
83 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
84 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
85 IIO_CHAN_SOFT_TIMESTAMP(4),
88 static const struct iio_chan_spec adc0834_channels
[] = {
89 ADC0832_VOLTAGE_CHANNEL(0),
90 ADC0832_VOLTAGE_CHANNEL(1),
91 ADC0832_VOLTAGE_CHANNEL(2),
92 ADC0832_VOLTAGE_CHANNEL(3),
93 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
94 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
95 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
96 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
97 IIO_CHAN_SOFT_TIMESTAMP(8),
100 static const struct iio_chan_spec adc0838_channels
[] = {
101 ADC0832_VOLTAGE_CHANNEL(0),
102 ADC0832_VOLTAGE_CHANNEL(1),
103 ADC0832_VOLTAGE_CHANNEL(2),
104 ADC0832_VOLTAGE_CHANNEL(3),
105 ADC0832_VOLTAGE_CHANNEL(4),
106 ADC0832_VOLTAGE_CHANNEL(5),
107 ADC0832_VOLTAGE_CHANNEL(6),
108 ADC0832_VOLTAGE_CHANNEL(7),
109 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
110 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
111 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
112 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
113 ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
114 ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
115 ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
116 ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
117 IIO_CHAN_SOFT_TIMESTAMP(16),
120 static int adc0831_adc_conversion(struct adc0832
*adc
)
122 struct spi_device
*spi
= adc
->spi
;
125 ret
= spi_read(spi
, &adc
->rx_buf
, 2);
130 * Skip TRI-STATE and a leading zero
132 return (adc
->rx_buf
[0] << 2 & 0xff) | (adc
->rx_buf
[1] >> 6);
135 static int adc0832_adc_conversion(struct adc0832
*adc
, int channel
,
138 struct spi_device
*spi
= adc
->spi
;
139 struct spi_transfer xfer
= {
140 .tx_buf
= adc
->tx_buf
,
141 .rx_buf
= adc
->rx_buf
,
147 return adc0831_adc_conversion(adc
);
150 adc
->tx_buf
[0] = 1 << (adc
->mux_bits
+ 1);
151 /* single-ended or differential */
152 adc
->tx_buf
[0] |= differential
? 0 : (1 << adc
->mux_bits
);
154 adc
->tx_buf
[0] |= (channel
% 2) << (adc
->mux_bits
- 1);
156 if (adc
->mux_bits
> 1)
157 adc
->tx_buf
[0] |= channel
/ 2;
159 /* align Data output BIT7 (MSB) to 8-bit boundary */
160 adc
->tx_buf
[0] <<= 1;
162 ret
= spi_sync_transfer(spi
, &xfer
, 1);
166 return adc
->rx_buf
[1];
169 static int adc0832_read_raw(struct iio_dev
*iio
,
170 struct iio_chan_spec
const *channel
, int *value
,
171 int *shift
, long mask
)
173 struct adc0832
*adc
= iio_priv(iio
);
176 case IIO_CHAN_INFO_RAW
:
177 mutex_lock(&adc
->lock
);
178 *value
= adc0832_adc_conversion(adc
, channel
->channel
,
179 channel
->differential
);
180 mutex_unlock(&adc
->lock
);
185 case IIO_CHAN_INFO_SCALE
:
186 *value
= regulator_get_voltage(adc
->reg
);
190 /* convert regulator output voltage to mV */
194 return IIO_VAL_FRACTIONAL_LOG2
;
200 static const struct iio_info adc0832_info
= {
201 .read_raw
= adc0832_read_raw
,
204 static irqreturn_t
adc0832_trigger_handler(int irq
, void *p
)
206 struct iio_poll_func
*pf
= p
;
207 struct iio_dev
*indio_dev
= pf
->indio_dev
;
208 struct adc0832
*adc
= iio_priv(indio_dev
);
212 mutex_lock(&adc
->lock
);
214 for_each_set_bit(scan_index
, indio_dev
->active_scan_mask
,
215 indio_dev
->masklength
) {
216 const struct iio_chan_spec
*scan_chan
=
217 &indio_dev
->channels
[scan_index
];
218 int ret
= adc0832_adc_conversion(adc
, scan_chan
->channel
,
219 scan_chan
->differential
);
221 dev_warn(&adc
->spi
->dev
,
222 "failed to get conversion data\n");
229 iio_push_to_buffers_with_timestamp(indio_dev
, adc
->data
,
230 iio_get_time_ns(indio_dev
));
232 mutex_unlock(&adc
->lock
);
234 iio_trigger_notify_done(indio_dev
->trig
);
239 static int adc0832_probe(struct spi_device
*spi
)
241 struct iio_dev
*indio_dev
;
245 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
249 adc
= iio_priv(indio_dev
);
251 mutex_init(&adc
->lock
);
253 indio_dev
->name
= spi_get_device_id(spi
)->name
;
254 indio_dev
->info
= &adc0832_info
;
255 indio_dev
->modes
= INDIO_DIRECT_MODE
;
257 switch (spi_get_device_id(spi
)->driver_data
) {
260 indio_dev
->channels
= adc0831_channels
;
261 indio_dev
->num_channels
= ARRAY_SIZE(adc0831_channels
);
265 indio_dev
->channels
= adc0832_channels
;
266 indio_dev
->num_channels
= ARRAY_SIZE(adc0832_channels
);
270 indio_dev
->channels
= adc0834_channels
;
271 indio_dev
->num_channels
= ARRAY_SIZE(adc0834_channels
);
275 indio_dev
->channels
= adc0838_channels
;
276 indio_dev
->num_channels
= ARRAY_SIZE(adc0838_channels
);
282 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
283 if (IS_ERR(adc
->reg
))
284 return PTR_ERR(adc
->reg
);
286 ret
= regulator_enable(adc
->reg
);
290 spi_set_drvdata(spi
, indio_dev
);
292 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
293 adc0832_trigger_handler
, NULL
);
295 goto err_reg_disable
;
297 ret
= iio_device_register(indio_dev
);
299 goto err_buffer_cleanup
;
303 iio_triggered_buffer_cleanup(indio_dev
);
305 regulator_disable(adc
->reg
);
310 static int adc0832_remove(struct spi_device
*spi
)
312 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
313 struct adc0832
*adc
= iio_priv(indio_dev
);
315 iio_device_unregister(indio_dev
);
316 iio_triggered_buffer_cleanup(indio_dev
);
317 regulator_disable(adc
->reg
);
322 static const struct of_device_id adc0832_dt_ids
[] = {
323 { .compatible
= "ti,adc0831", },
324 { .compatible
= "ti,adc0832", },
325 { .compatible
= "ti,adc0834", },
326 { .compatible
= "ti,adc0838", },
329 MODULE_DEVICE_TABLE(of
, adc0832_dt_ids
);
331 static const struct spi_device_id adc0832_id
[] = {
332 { "adc0831", adc0831
},
333 { "adc0832", adc0832
},
334 { "adc0834", adc0834
},
335 { "adc0838", adc0838
},
338 MODULE_DEVICE_TABLE(spi
, adc0832_id
);
340 static struct spi_driver adc0832_driver
= {
343 .of_match_table
= adc0832_dt_ids
,
345 .probe
= adc0832_probe
,
346 .remove
= adc0832_remove
,
347 .id_table
= adc0832_id
,
349 module_spi_driver(adc0832_driver
);
351 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
352 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
353 MODULE_LICENSE("GPL v2");