1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
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] __aligned(IIO_DMA_MINALIGN);
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 void adc0832_reg_disable(void *reg)
241 regulator_disable(reg);
244 static int adc0832_probe(struct spi_device *spi)
246 struct iio_dev *indio_dev;
250 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
254 adc = iio_priv(indio_dev);
256 mutex_init(&adc->lock);
258 indio_dev->name = spi_get_device_id(spi)->name;
259 indio_dev->info = &adc0832_info;
260 indio_dev->modes = INDIO_DIRECT_MODE;
262 switch (spi_get_device_id(spi)->driver_data) {
265 indio_dev->channels = adc0831_channels;
266 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
270 indio_dev->channels = adc0832_channels;
271 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
275 indio_dev->channels = adc0834_channels;
276 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
280 indio_dev->channels = adc0838_channels;
281 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
287 adc->reg = devm_regulator_get(&spi->dev, "vref");
288 if (IS_ERR(adc->reg))
289 return PTR_ERR(adc->reg);
291 ret = regulator_enable(adc->reg);
295 ret = devm_add_action_or_reset(&spi->dev, adc0832_reg_disable,
300 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
301 adc0832_trigger_handler, NULL);
305 return devm_iio_device_register(&spi->dev, indio_dev);
308 static const struct of_device_id adc0832_dt_ids[] = {
309 { .compatible = "ti,adc0831", },
310 { .compatible = "ti,adc0832", },
311 { .compatible = "ti,adc0834", },
312 { .compatible = "ti,adc0838", },
315 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
317 static const struct spi_device_id adc0832_id[] = {
318 { "adc0831", adc0831 },
319 { "adc0832", adc0832 },
320 { "adc0834", adc0834 },
321 { "adc0838", adc0838 },
324 MODULE_DEVICE_TABLE(spi, adc0832_id);
326 static struct spi_driver adc0832_driver = {
329 .of_match_table = adc0832_dt_ids,
331 .probe = adc0832_probe,
332 .id_table = adc0832_id,
334 module_spi_driver(adc0832_driver);
337 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
338 MODULE_LICENSE("GPL v2");