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: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
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>
27 struct spi_device *spi;
28 struct regulator *reg;
32 u8 tx_buf[2] ____cacheline_aligned;
36 #define ADC0832_VOLTAGE_CHANNEL(chan) \
38 .type = IIO_VOLTAGE, \
41 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
42 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
51 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
53 .type = IIO_VOLTAGE, \
56 .channel2 = (chan2), \
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
59 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
68 static const struct iio_chan_spec adc0831_channels[] = {
69 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
70 IIO_CHAN_SOFT_TIMESTAMP(1),
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),
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),
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),
113 static int adc0831_adc_conversion(struct adc0832 *adc)
115 struct spi_device *spi = adc->spi;
118 ret = spi_read(spi, &adc->rx_buf, 2);
123 * Skip TRI-STATE and a leading zero
125 return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
128 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
131 struct spi_device *spi = adc->spi;
132 struct spi_transfer xfer = {
133 .tx_buf = adc->tx_buf,
134 .rx_buf = adc->rx_buf,
140 return adc0831_adc_conversion(adc);
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);
147 adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
149 if (adc->mux_bits > 1)
150 adc->tx_buf[0] |= channel / 2;
152 /* align Data output BIT7 (MSB) to 8-bit boundary */
153 adc->tx_buf[0] <<= 1;
155 ret = spi_sync_transfer(spi, &xfer, 1);
159 return adc->rx_buf[1];
162 static int adc0832_read_raw(struct iio_dev *iio,
163 struct iio_chan_spec const *channel, int *value,
164 int *shift, long mask)
166 struct adc0832 *adc = iio_priv(iio);
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);
178 case IIO_CHAN_INFO_SCALE:
179 *value = regulator_get_voltage(adc->reg);
183 /* convert regulator output voltage to mV */
187 return IIO_VAL_FRACTIONAL_LOG2;
193 static const struct iio_info adc0832_info = {
194 .read_raw = adc0832_read_raw,
197 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
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 */
206 mutex_lock(&adc->lock);
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);
215 dev_warn(&adc->spi->dev,
216 "failed to get conversion data\n");
223 iio_push_to_buffers_with_timestamp(indio_dev, data,
224 iio_get_time_ns(indio_dev));
226 mutex_unlock(&adc->lock);
228 iio_trigger_notify_done(indio_dev->trig);
233 static int adc0832_probe(struct spi_device *spi)
235 struct iio_dev *indio_dev;
239 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
243 adc = iio_priv(indio_dev);
245 mutex_init(&adc->lock);
247 indio_dev->name = spi_get_device_id(spi)->name;
248 indio_dev->info = &adc0832_info;
249 indio_dev->modes = INDIO_DIRECT_MODE;
251 switch (spi_get_device_id(spi)->driver_data) {
254 indio_dev->channels = adc0831_channels;
255 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
259 indio_dev->channels = adc0832_channels;
260 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
264 indio_dev->channels = adc0834_channels;
265 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
269 indio_dev->channels = adc0838_channels;
270 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
276 adc->reg = devm_regulator_get(&spi->dev, "vref");
277 if (IS_ERR(adc->reg))
278 return PTR_ERR(adc->reg);
280 ret = regulator_enable(adc->reg);
284 spi_set_drvdata(spi, indio_dev);
286 ret = iio_triggered_buffer_setup(indio_dev, NULL,
287 adc0832_trigger_handler, NULL);
289 goto err_reg_disable;
291 ret = iio_device_register(indio_dev);
293 goto err_buffer_cleanup;
297 iio_triggered_buffer_cleanup(indio_dev);
299 regulator_disable(adc->reg);
304 static int adc0832_remove(struct spi_device *spi)
306 struct iio_dev *indio_dev = spi_get_drvdata(spi);
307 struct adc0832 *adc = iio_priv(indio_dev);
309 iio_device_unregister(indio_dev);
310 iio_triggered_buffer_cleanup(indio_dev);
311 regulator_disable(adc->reg);
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", },
325 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
329 static const struct spi_device_id adc0832_id[] = {
330 { "adc0831", adc0831 },
331 { "adc0832", adc0832 },
332 { "adc0834", adc0834 },
333 { "adc0838", adc0838 },
336 MODULE_DEVICE_TABLE(spi, adc0832_id);
338 static struct spi_driver adc0832_driver = {
341 .of_match_table = of_match_ptr(adc0832_dt_ids),
343 .probe = adc0832_probe,
344 .remove = adc0832_remove,
345 .id_table = adc0832_id,
347 module_spi_driver(adc0832_driver);
349 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
350 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
351 MODULE_LICENSE("GPL v2");