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 u8 tx_buf[2] ____cacheline_aligned;
37 #define ADC0832_VOLTAGE_CHANNEL(chan) \
39 .type = IIO_VOLTAGE, \
42 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
43 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
52 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
54 .type = IIO_VOLTAGE, \
57 .channel2 = (chan2), \
59 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
60 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
69 static const struct iio_chan_spec adc0831_channels[] = {
70 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
71 IIO_CHAN_SOFT_TIMESTAMP(1),
74 static const struct iio_chan_spec adc0832_channels[] = {
75 ADC0832_VOLTAGE_CHANNEL(0),
76 ADC0832_VOLTAGE_CHANNEL(1),
77 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
78 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
79 IIO_CHAN_SOFT_TIMESTAMP(4),
82 static const struct iio_chan_spec adc0834_channels[] = {
83 ADC0832_VOLTAGE_CHANNEL(0),
84 ADC0832_VOLTAGE_CHANNEL(1),
85 ADC0832_VOLTAGE_CHANNEL(2),
86 ADC0832_VOLTAGE_CHANNEL(3),
87 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
88 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
89 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
90 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
91 IIO_CHAN_SOFT_TIMESTAMP(8),
94 static const struct iio_chan_spec adc0838_channels[] = {
95 ADC0832_VOLTAGE_CHANNEL(0),
96 ADC0832_VOLTAGE_CHANNEL(1),
97 ADC0832_VOLTAGE_CHANNEL(2),
98 ADC0832_VOLTAGE_CHANNEL(3),
99 ADC0832_VOLTAGE_CHANNEL(4),
100 ADC0832_VOLTAGE_CHANNEL(5),
101 ADC0832_VOLTAGE_CHANNEL(6),
102 ADC0832_VOLTAGE_CHANNEL(7),
103 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
104 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
105 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
106 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
107 ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
108 ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
109 ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
110 ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
111 IIO_CHAN_SOFT_TIMESTAMP(16),
114 static int adc0831_adc_conversion(struct adc0832 *adc)
116 struct spi_device *spi = adc->spi;
119 ret = spi_read(spi, &adc->rx_buf, 2);
124 * Skip TRI-STATE and a leading zero
126 return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
129 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
132 struct spi_device *spi = adc->spi;
133 struct spi_transfer xfer = {
134 .tx_buf = adc->tx_buf,
135 .rx_buf = adc->rx_buf,
141 return adc0831_adc_conversion(adc);
144 adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
145 /* single-ended or differential */
146 adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
148 adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
150 if (adc->mux_bits > 1)
151 adc->tx_buf[0] |= channel / 2;
153 /* align Data output BIT7 (MSB) to 8-bit boundary */
154 adc->tx_buf[0] <<= 1;
156 ret = spi_sync_transfer(spi, &xfer, 1);
160 return adc->rx_buf[1];
163 static int adc0832_read_raw(struct iio_dev *iio,
164 struct iio_chan_spec const *channel, int *value,
165 int *shift, long mask)
167 struct adc0832 *adc = iio_priv(iio);
170 case IIO_CHAN_INFO_RAW:
171 mutex_lock(&adc->lock);
172 *value = adc0832_adc_conversion(adc, channel->channel,
173 channel->differential);
174 mutex_unlock(&adc->lock);
179 case IIO_CHAN_INFO_SCALE:
180 *value = regulator_get_voltage(adc->reg);
184 /* convert regulator output voltage to mV */
188 return IIO_VAL_FRACTIONAL_LOG2;
194 static const struct iio_info adc0832_info = {
195 .read_raw = adc0832_read_raw,
198 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
200 struct iio_poll_func *pf = p;
201 struct iio_dev *indio_dev = pf->indio_dev;
202 struct adc0832 *adc = iio_priv(indio_dev);
203 u8 data[24] = { }; /* 16x 1 byte ADC data + 8 bytes timestamp */
207 mutex_lock(&adc->lock);
209 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
210 indio_dev->masklength) {
211 const struct iio_chan_spec *scan_chan =
212 &indio_dev->channels[scan_index];
213 int ret = adc0832_adc_conversion(adc, scan_chan->channel,
214 scan_chan->differential);
216 dev_warn(&adc->spi->dev,
217 "failed to get conversion data\n");
224 iio_push_to_buffers_with_timestamp(indio_dev, data,
225 iio_get_time_ns(indio_dev));
227 mutex_unlock(&adc->lock);
229 iio_trigger_notify_done(indio_dev->trig);
234 static int adc0832_probe(struct spi_device *spi)
236 struct iio_dev *indio_dev;
240 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
244 adc = iio_priv(indio_dev);
246 mutex_init(&adc->lock);
248 indio_dev->name = spi_get_device_id(spi)->name;
249 indio_dev->info = &adc0832_info;
250 indio_dev->modes = INDIO_DIRECT_MODE;
252 switch (spi_get_device_id(spi)->driver_data) {
255 indio_dev->channels = adc0831_channels;
256 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
260 indio_dev->channels = adc0832_channels;
261 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
265 indio_dev->channels = adc0834_channels;
266 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
270 indio_dev->channels = adc0838_channels;
271 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
277 adc->reg = devm_regulator_get(&spi->dev, "vref");
278 if (IS_ERR(adc->reg))
279 return PTR_ERR(adc->reg);
281 ret = regulator_enable(adc->reg);
285 spi_set_drvdata(spi, indio_dev);
287 ret = iio_triggered_buffer_setup(indio_dev, NULL,
288 adc0832_trigger_handler, NULL);
290 goto err_reg_disable;
292 ret = iio_device_register(indio_dev);
294 goto err_buffer_cleanup;
298 iio_triggered_buffer_cleanup(indio_dev);
300 regulator_disable(adc->reg);
305 static int adc0832_remove(struct spi_device *spi)
307 struct iio_dev *indio_dev = spi_get_drvdata(spi);
308 struct adc0832 *adc = iio_priv(indio_dev);
310 iio_device_unregister(indio_dev);
311 iio_triggered_buffer_cleanup(indio_dev);
312 regulator_disable(adc->reg);
317 static const struct of_device_id adc0832_dt_ids[] = {
318 { .compatible = "ti,adc0831", },
319 { .compatible = "ti,adc0832", },
320 { .compatible = "ti,adc0834", },
321 { .compatible = "ti,adc0838", },
324 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
326 static const struct spi_device_id adc0832_id[] = {
327 { "adc0831", adc0831 },
328 { "adc0832", adc0832 },
329 { "adc0834", adc0834 },
330 { "adc0838", adc0838 },
333 MODULE_DEVICE_TABLE(spi, adc0832_id);
335 static struct spi_driver adc0832_driver = {
338 .of_match_table = adc0832_dt_ids,
340 .probe = adc0832_probe,
341 .remove = adc0832_remove,
342 .id_table = adc0832_id,
344 module_spi_driver(adc0832_driver);
347 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
348 MODULE_LICENSE("GPL v2");