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>
26 struct spi_device
*spi
;
27 struct regulator
*reg
;
31 u8 tx_buf
[2] ____cacheline_aligned
;
35 #define ADC0832_VOLTAGE_CHANNEL(chan) \
37 .type = IIO_VOLTAGE, \
40 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
41 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
44 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2) \
46 .type = IIO_VOLTAGE, \
49 .channel2 = (chan2), \
51 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
52 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
55 static const struct iio_chan_spec adc0831_channels
[] = {
56 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
59 static const struct iio_chan_spec adc0832_channels
[] = {
60 ADC0832_VOLTAGE_CHANNEL(0),
61 ADC0832_VOLTAGE_CHANNEL(1),
62 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
63 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
66 static const struct iio_chan_spec adc0834_channels
[] = {
67 ADC0832_VOLTAGE_CHANNEL(0),
68 ADC0832_VOLTAGE_CHANNEL(1),
69 ADC0832_VOLTAGE_CHANNEL(2),
70 ADC0832_VOLTAGE_CHANNEL(3),
71 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
72 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
73 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3),
74 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2),
77 static const struct iio_chan_spec adc0838_channels
[] = {
78 ADC0832_VOLTAGE_CHANNEL(0),
79 ADC0832_VOLTAGE_CHANNEL(1),
80 ADC0832_VOLTAGE_CHANNEL(2),
81 ADC0832_VOLTAGE_CHANNEL(3),
82 ADC0832_VOLTAGE_CHANNEL(4),
83 ADC0832_VOLTAGE_CHANNEL(5),
84 ADC0832_VOLTAGE_CHANNEL(6),
85 ADC0832_VOLTAGE_CHANNEL(7),
86 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
87 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
88 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3),
89 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2),
90 ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5),
91 ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4),
92 ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7),
93 ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6),
96 static int adc0831_adc_conversion(struct adc0832
*adc
)
98 struct spi_device
*spi
= adc
->spi
;
101 ret
= spi_read(spi
, &adc
->rx_buf
, 2);
106 * Skip TRI-STATE and a leading zero
108 return (adc
->rx_buf
[0] << 2 & 0xff) | (adc
->rx_buf
[1] >> 6);
111 static int adc0832_adc_conversion(struct adc0832
*adc
, int channel
,
114 struct spi_device
*spi
= adc
->spi
;
115 struct spi_transfer xfer
= {
116 .tx_buf
= adc
->tx_buf
,
117 .rx_buf
= adc
->rx_buf
,
123 return adc0831_adc_conversion(adc
);
126 adc
->tx_buf
[0] = 1 << (adc
->mux_bits
+ 1);
127 /* single-ended or differential */
128 adc
->tx_buf
[0] |= differential
? 0 : (1 << adc
->mux_bits
);
130 adc
->tx_buf
[0] |= (channel
% 2) << (adc
->mux_bits
- 1);
132 if (adc
->mux_bits
> 1)
133 adc
->tx_buf
[0] |= channel
/ 2;
135 /* align Data output BIT7 (MSB) to 8-bit boundary */
136 adc
->tx_buf
[0] <<= 1;
138 ret
= spi_sync_transfer(spi
, &xfer
, 1);
142 return adc
->rx_buf
[1];
145 static int adc0832_read_raw(struct iio_dev
*iio
,
146 struct iio_chan_spec
const *channel
, int *value
,
147 int *shift
, long mask
)
149 struct adc0832
*adc
= iio_priv(iio
);
152 case IIO_CHAN_INFO_RAW
:
153 mutex_lock(&adc
->lock
);
154 *value
= adc0832_adc_conversion(adc
, channel
->channel
,
155 channel
->differential
);
156 mutex_unlock(&adc
->lock
);
161 case IIO_CHAN_INFO_SCALE
:
162 *value
= regulator_get_voltage(adc
->reg
);
166 /* convert regulator output voltage to mV */
170 return IIO_VAL_FRACTIONAL_LOG2
;
176 static const struct iio_info adc0832_info
= {
177 .read_raw
= adc0832_read_raw
,
178 .driver_module
= THIS_MODULE
,
181 static int adc0832_probe(struct spi_device
*spi
)
183 struct iio_dev
*indio_dev
;
187 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
191 adc
= iio_priv(indio_dev
);
193 mutex_init(&adc
->lock
);
195 indio_dev
->name
= spi_get_device_id(spi
)->name
;
196 indio_dev
->dev
.parent
= &spi
->dev
;
197 indio_dev
->info
= &adc0832_info
;
198 indio_dev
->modes
= INDIO_DIRECT_MODE
;
200 switch (spi_get_device_id(spi
)->driver_data
) {
203 indio_dev
->channels
= adc0831_channels
;
204 indio_dev
->num_channels
= ARRAY_SIZE(adc0831_channels
);
208 indio_dev
->channels
= adc0832_channels
;
209 indio_dev
->num_channels
= ARRAY_SIZE(adc0832_channels
);
213 indio_dev
->channels
= adc0834_channels
;
214 indio_dev
->num_channels
= ARRAY_SIZE(adc0834_channels
);
218 indio_dev
->channels
= adc0838_channels
;
219 indio_dev
->num_channels
= ARRAY_SIZE(adc0838_channels
);
225 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
226 if (IS_ERR(adc
->reg
))
227 return PTR_ERR(adc
->reg
);
229 ret
= regulator_enable(adc
->reg
);
233 spi_set_drvdata(spi
, indio_dev
);
235 ret
= iio_device_register(indio_dev
);
237 regulator_disable(adc
->reg
);
242 static int adc0832_remove(struct spi_device
*spi
)
244 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
245 struct adc0832
*adc
= iio_priv(indio_dev
);
247 iio_device_unregister(indio_dev
);
248 regulator_disable(adc
->reg
);
255 static const struct of_device_id adc0832_dt_ids
[] = {
256 { .compatible
= "ti,adc0831", },
257 { .compatible
= "ti,adc0832", },
258 { .compatible
= "ti,adc0834", },
259 { .compatible
= "ti,adc0838", },
262 MODULE_DEVICE_TABLE(of
, adc0832_dt_ids
);
266 static const struct spi_device_id adc0832_id
[] = {
267 { "adc0831", adc0831
},
268 { "adc0832", adc0832
},
269 { "adc0834", adc0834
},
270 { "adc0838", adc0838
},
273 MODULE_DEVICE_TABLE(spi
, adc0832_id
);
275 static struct spi_driver adc0832_driver
= {
278 .of_match_table
= of_match_ptr(adc0832_dt_ids
),
280 .probe
= adc0832_probe
,
281 .remove
= adc0832_remove
,
282 .id_table
= adc0832_id
,
284 module_spi_driver(adc0832_driver
);
286 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
287 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
288 MODULE_LICENSE("GPL v2");