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 u8 tx_buf
[2] ____cacheline_aligned
;
39 #define ADC0832_VOLTAGE_CHANNEL(chan) \
41 .type = IIO_VOLTAGE, \
44 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
45 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
54 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
56 .type = IIO_VOLTAGE, \
59 .channel2 = (chan2), \
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
71 static const struct iio_chan_spec adc0831_channels
[] = {
72 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
73 IIO_CHAN_SOFT_TIMESTAMP(1),
76 static const struct iio_chan_spec adc0832_channels
[] = {
77 ADC0832_VOLTAGE_CHANNEL(0),
78 ADC0832_VOLTAGE_CHANNEL(1),
79 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
80 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
81 IIO_CHAN_SOFT_TIMESTAMP(4),
84 static const struct iio_chan_spec adc0834_channels
[] = {
85 ADC0832_VOLTAGE_CHANNEL(0),
86 ADC0832_VOLTAGE_CHANNEL(1),
87 ADC0832_VOLTAGE_CHANNEL(2),
88 ADC0832_VOLTAGE_CHANNEL(3),
89 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
90 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
91 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
92 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
93 IIO_CHAN_SOFT_TIMESTAMP(8),
96 static const struct iio_chan_spec adc0838_channels
[] = {
97 ADC0832_VOLTAGE_CHANNEL(0),
98 ADC0832_VOLTAGE_CHANNEL(1),
99 ADC0832_VOLTAGE_CHANNEL(2),
100 ADC0832_VOLTAGE_CHANNEL(3),
101 ADC0832_VOLTAGE_CHANNEL(4),
102 ADC0832_VOLTAGE_CHANNEL(5),
103 ADC0832_VOLTAGE_CHANNEL(6),
104 ADC0832_VOLTAGE_CHANNEL(7),
105 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
106 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
107 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
108 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
109 ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
110 ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
111 ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
112 ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
113 IIO_CHAN_SOFT_TIMESTAMP(16),
116 static int adc0831_adc_conversion(struct adc0832
*adc
)
118 struct spi_device
*spi
= adc
->spi
;
121 ret
= spi_read(spi
, &adc
->rx_buf
, 2);
126 * Skip TRI-STATE and a leading zero
128 return (adc
->rx_buf
[0] << 2 & 0xff) | (adc
->rx_buf
[1] >> 6);
131 static int adc0832_adc_conversion(struct adc0832
*adc
, int channel
,
134 struct spi_device
*spi
= adc
->spi
;
135 struct spi_transfer xfer
= {
136 .tx_buf
= adc
->tx_buf
,
137 .rx_buf
= adc
->rx_buf
,
143 return adc0831_adc_conversion(adc
);
146 adc
->tx_buf
[0] = 1 << (adc
->mux_bits
+ 1);
147 /* single-ended or differential */
148 adc
->tx_buf
[0] |= differential
? 0 : (1 << adc
->mux_bits
);
150 adc
->tx_buf
[0] |= (channel
% 2) << (adc
->mux_bits
- 1);
152 if (adc
->mux_bits
> 1)
153 adc
->tx_buf
[0] |= channel
/ 2;
155 /* align Data output BIT7 (MSB) to 8-bit boundary */
156 adc
->tx_buf
[0] <<= 1;
158 ret
= spi_sync_transfer(spi
, &xfer
, 1);
162 return adc
->rx_buf
[1];
165 static int adc0832_read_raw(struct iio_dev
*iio
,
166 struct iio_chan_spec
const *channel
, int *value
,
167 int *shift
, long mask
)
169 struct adc0832
*adc
= iio_priv(iio
);
172 case IIO_CHAN_INFO_RAW
:
173 mutex_lock(&adc
->lock
);
174 *value
= adc0832_adc_conversion(adc
, channel
->channel
,
175 channel
->differential
);
176 mutex_unlock(&adc
->lock
);
181 case IIO_CHAN_INFO_SCALE
:
182 *value
= regulator_get_voltage(adc
->reg
);
186 /* convert regulator output voltage to mV */
190 return IIO_VAL_FRACTIONAL_LOG2
;
196 static const struct iio_info adc0832_info
= {
197 .read_raw
= adc0832_read_raw
,
200 static irqreturn_t
adc0832_trigger_handler(int irq
, void *p
)
202 struct iio_poll_func
*pf
= p
;
203 struct iio_dev
*indio_dev
= pf
->indio_dev
;
204 struct adc0832
*adc
= iio_priv(indio_dev
);
205 u8 data
[24] = { }; /* 16x 1 byte ADC data + 8 bytes timestamp */
209 mutex_lock(&adc
->lock
);
211 for_each_set_bit(scan_index
, indio_dev
->active_scan_mask
,
212 indio_dev
->masklength
) {
213 const struct iio_chan_spec
*scan_chan
=
214 &indio_dev
->channels
[scan_index
];
215 int ret
= adc0832_adc_conversion(adc
, scan_chan
->channel
,
216 scan_chan
->differential
);
218 dev_warn(&adc
->spi
->dev
,
219 "failed to get conversion data\n");
226 iio_push_to_buffers_with_timestamp(indio_dev
, data
,
227 iio_get_time_ns(indio_dev
));
229 mutex_unlock(&adc
->lock
);
231 iio_trigger_notify_done(indio_dev
->trig
);
236 static int adc0832_probe(struct spi_device
*spi
)
238 struct iio_dev
*indio_dev
;
242 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
246 adc
= iio_priv(indio_dev
);
248 mutex_init(&adc
->lock
);
250 indio_dev
->name
= spi_get_device_id(spi
)->name
;
251 indio_dev
->dev
.parent
= &spi
->dev
;
252 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
253 indio_dev
->info
= &adc0832_info
;
254 indio_dev
->modes
= INDIO_DIRECT_MODE
;
256 switch (spi_get_device_id(spi
)->driver_data
) {
259 indio_dev
->channels
= adc0831_channels
;
260 indio_dev
->num_channels
= ARRAY_SIZE(adc0831_channels
);
264 indio_dev
->channels
= adc0832_channels
;
265 indio_dev
->num_channels
= ARRAY_SIZE(adc0832_channels
);
269 indio_dev
->channels
= adc0834_channels
;
270 indio_dev
->num_channels
= ARRAY_SIZE(adc0834_channels
);
274 indio_dev
->channels
= adc0838_channels
;
275 indio_dev
->num_channels
= ARRAY_SIZE(adc0838_channels
);
281 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
282 if (IS_ERR(adc
->reg
))
283 return PTR_ERR(adc
->reg
);
285 ret
= regulator_enable(adc
->reg
);
289 spi_set_drvdata(spi
, indio_dev
);
291 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
292 adc0832_trigger_handler
, NULL
);
294 goto err_reg_disable
;
296 ret
= iio_device_register(indio_dev
);
298 goto err_buffer_cleanup
;
302 iio_triggered_buffer_cleanup(indio_dev
);
304 regulator_disable(adc
->reg
);
309 static int adc0832_remove(struct spi_device
*spi
)
311 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
312 struct adc0832
*adc
= iio_priv(indio_dev
);
314 iio_device_unregister(indio_dev
);
315 iio_triggered_buffer_cleanup(indio_dev
);
316 regulator_disable(adc
->reg
);
323 static const struct of_device_id adc0832_dt_ids
[] = {
324 { .compatible
= "ti,adc0831", },
325 { .compatible
= "ti,adc0832", },
326 { .compatible
= "ti,adc0834", },
327 { .compatible
= "ti,adc0838", },
330 MODULE_DEVICE_TABLE(of
, adc0832_dt_ids
);
334 static const struct spi_device_id adc0832_id
[] = {
335 { "adc0831", adc0831
},
336 { "adc0832", adc0832
},
337 { "adc0834", adc0834
},
338 { "adc0838", adc0838
},
341 MODULE_DEVICE_TABLE(spi
, adc0832_id
);
343 static struct spi_driver adc0832_driver
= {
346 .of_match_table
= of_match_ptr(adc0832_dt_ids
),
348 .probe
= adc0832_probe
,
349 .remove
= adc0832_remove
,
350 .id_table
= adc0832_id
,
352 module_spi_driver(adc0832_driver
);
354 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
355 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
356 MODULE_LICENSE("GPL v2");