2 * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Datasheet: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
13 #include <linux/module.h>
14 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
30 struct spi_device *spi;
31 struct regulator *reg;
35 * Max size needed: 16x 1 byte ADC data + 8 bytes timestamp
36 * May be shorter if not all channels are enabled subject
37 * to the timestamp remaining 8 byte aligned.
39 u8 data[24] __aligned(8);
41 u8 tx_buf[2] ____cacheline_aligned;
45 #define ADC0832_VOLTAGE_CHANNEL(chan) \
47 .type = IIO_VOLTAGE, \
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
60 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
62 .type = IIO_VOLTAGE, \
65 .channel2 = (chan2), \
67 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
68 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
77 static const struct iio_chan_spec adc0831_channels[] = {
78 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
79 IIO_CHAN_SOFT_TIMESTAMP(1),
82 static const struct iio_chan_spec adc0832_channels[] = {
83 ADC0832_VOLTAGE_CHANNEL(0),
84 ADC0832_VOLTAGE_CHANNEL(1),
85 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
86 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
87 IIO_CHAN_SOFT_TIMESTAMP(4),
90 static const struct iio_chan_spec adc0834_channels[] = {
91 ADC0832_VOLTAGE_CHANNEL(0),
92 ADC0832_VOLTAGE_CHANNEL(1),
93 ADC0832_VOLTAGE_CHANNEL(2),
94 ADC0832_VOLTAGE_CHANNEL(3),
95 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
96 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
97 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
98 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
99 IIO_CHAN_SOFT_TIMESTAMP(8),
102 static const struct iio_chan_spec adc0838_channels[] = {
103 ADC0832_VOLTAGE_CHANNEL(0),
104 ADC0832_VOLTAGE_CHANNEL(1),
105 ADC0832_VOLTAGE_CHANNEL(2),
106 ADC0832_VOLTAGE_CHANNEL(3),
107 ADC0832_VOLTAGE_CHANNEL(4),
108 ADC0832_VOLTAGE_CHANNEL(5),
109 ADC0832_VOLTAGE_CHANNEL(6),
110 ADC0832_VOLTAGE_CHANNEL(7),
111 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
112 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
113 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
114 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
115 ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
116 ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
117 ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
118 ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
119 IIO_CHAN_SOFT_TIMESTAMP(16),
122 static int adc0831_adc_conversion(struct adc0832 *adc)
124 struct spi_device *spi = adc->spi;
127 ret = spi_read(spi, &adc->rx_buf, 2);
132 * Skip TRI-STATE and a leading zero
134 return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
137 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
140 struct spi_device *spi = adc->spi;
141 struct spi_transfer xfer = {
142 .tx_buf = adc->tx_buf,
143 .rx_buf = adc->rx_buf,
149 return adc0831_adc_conversion(adc);
152 adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
153 /* single-ended or differential */
154 adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
156 adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
158 if (adc->mux_bits > 1)
159 adc->tx_buf[0] |= channel / 2;
161 /* align Data output BIT7 (MSB) to 8-bit boundary */
162 adc->tx_buf[0] <<= 1;
164 ret = spi_sync_transfer(spi, &xfer, 1);
168 return adc->rx_buf[1];
171 static int adc0832_read_raw(struct iio_dev *iio,
172 struct iio_chan_spec const *channel, int *value,
173 int *shift, long mask)
175 struct adc0832 *adc = iio_priv(iio);
178 case IIO_CHAN_INFO_RAW:
179 mutex_lock(&adc->lock);
180 *value = adc0832_adc_conversion(adc, channel->channel,
181 channel->differential);
182 mutex_unlock(&adc->lock);
187 case IIO_CHAN_INFO_SCALE:
188 *value = regulator_get_voltage(adc->reg);
192 /* convert regulator output voltage to mV */
196 return IIO_VAL_FRACTIONAL_LOG2;
202 static const struct iio_info adc0832_info = {
203 .read_raw = adc0832_read_raw,
204 .driver_module = THIS_MODULE,
207 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
209 struct iio_poll_func *pf = p;
210 struct iio_dev *indio_dev = pf->indio_dev;
211 struct adc0832 *adc = iio_priv(indio_dev);
215 mutex_lock(&adc->lock);
217 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
218 indio_dev->masklength) {
219 const struct iio_chan_spec *scan_chan =
220 &indio_dev->channels[scan_index];
221 int ret = adc0832_adc_conversion(adc, scan_chan->channel,
222 scan_chan->differential);
224 dev_warn(&adc->spi->dev,
225 "failed to get conversion data\n");
232 iio_push_to_buffers_with_timestamp(indio_dev, adc->data,
233 iio_get_time_ns(indio_dev));
235 mutex_unlock(&adc->lock);
237 iio_trigger_notify_done(indio_dev->trig);
242 static int adc0832_probe(struct spi_device *spi)
244 struct iio_dev *indio_dev;
248 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
252 adc = iio_priv(indio_dev);
254 mutex_init(&adc->lock);
256 indio_dev->name = spi_get_device_id(spi)->name;
257 indio_dev->dev.parent = &spi->dev;
258 indio_dev->dev.of_node = spi->dev.of_node;
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 spi_set_drvdata(spi, indio_dev);
297 ret = iio_triggered_buffer_setup(indio_dev, NULL,
298 adc0832_trigger_handler, NULL);
300 goto err_reg_disable;
302 ret = iio_device_register(indio_dev);
304 goto err_buffer_cleanup;
308 iio_triggered_buffer_cleanup(indio_dev);
310 regulator_disable(adc->reg);
315 static int adc0832_remove(struct spi_device *spi)
317 struct iio_dev *indio_dev = spi_get_drvdata(spi);
318 struct adc0832 *adc = iio_priv(indio_dev);
320 iio_device_unregister(indio_dev);
321 iio_triggered_buffer_cleanup(indio_dev);
322 regulator_disable(adc->reg);
329 static const struct of_device_id adc0832_dt_ids[] = {
330 { .compatible = "ti,adc0831", },
331 { .compatible = "ti,adc0832", },
332 { .compatible = "ti,adc0834", },
333 { .compatible = "ti,adc0838", },
336 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
340 static const struct spi_device_id adc0832_id[] = {
341 { "adc0831", adc0831 },
342 { "adc0832", adc0832 },
343 { "adc0834", adc0834 },
344 { "adc0838", adc0838 },
347 MODULE_DEVICE_TABLE(spi, adc0832_id);
349 static struct spi_driver adc0832_driver = {
352 .of_match_table = of_match_ptr(adc0832_dt_ids),
354 .probe = adc0832_probe,
355 .remove = adc0832_remove,
356 .id_table = adc0832_id,
358 module_spi_driver(adc0832_driver);
360 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
361 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
362 MODULE_LICENSE("GPL v2");