blob: f4ba23effe9ad9c71fd9d947c3259cc4bbbd2537 [file] [log] [blame]
Akinobu Mitaefc945f2016-02-07 18:14:16 +09001/*
2 * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
3 *
4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
5 *
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.
9 *
10 * Datasheet: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
11 */
12
13#include <linux/module.h>
14#include <linux/spi/spi.h>
15#include <linux/iio/iio.h>
16#include <linux/regulator/consumer.h>
17
18enum {
19 adc0831,
20 adc0832,
21 adc0834,
22 adc0838,
23};
24
25struct adc0832 {
26 struct spi_device *spi;
27 struct regulator *reg;
28 struct mutex lock;
29 u8 mux_bits;
30
31 u8 tx_buf[2] ____cacheline_aligned;
32 u8 rx_buf[2];
33};
34
35#define ADC0832_VOLTAGE_CHANNEL(chan) \
36 { \
37 .type = IIO_VOLTAGE, \
38 .indexed = 1, \
39 .channel = chan, \
40 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
41 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
42 }
43
44#define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2) \
45 { \
46 .type = IIO_VOLTAGE, \
47 .indexed = 1, \
48 .channel = (chan1), \
49 .channel2 = (chan2), \
50 .differential = 1, \
51 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
52 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
53 }
54
55static const struct iio_chan_spec adc0831_channels[] = {
56 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
57};
58
59static 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),
64};
65
66static 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),
75};
76
77static 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),
94};
95
96static int adc0831_adc_conversion(struct adc0832 *adc)
97{
98 struct spi_device *spi = adc->spi;
99 int ret;
100
101 ret = spi_read(spi, &adc->rx_buf, 2);
102 if (ret)
103 return ret;
104
105 /*
106 * Skip TRI-STATE and a leading zero
107 */
108 return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
109}
110
111static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
112 bool differential)
113{
114 struct spi_device *spi = adc->spi;
115 struct spi_transfer xfer = {
116 .tx_buf = adc->tx_buf,
117 .rx_buf = adc->rx_buf,
118 .len = 2,
119 };
120 int ret;
121
122 if (!adc->mux_bits)
123 return adc0831_adc_conversion(adc);
124
125 /* start bit */
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);
129 /* odd / sign */
130 adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
131 /* select */
132 if (adc->mux_bits > 1)
133 adc->tx_buf[0] |= channel / 2;
134
135 /* align Data output BIT7 (MSB) to 8-bit boundary */
136 adc->tx_buf[0] <<= 1;
137
138 ret = spi_sync_transfer(spi, &xfer, 1);
139 if (ret)
140 return ret;
141
142 return adc->rx_buf[1];
143}
144
145static int adc0832_read_raw(struct iio_dev *iio,
146 struct iio_chan_spec const *channel, int *value,
147 int *shift, long mask)
148{
149 struct adc0832 *adc = iio_priv(iio);
150
151 switch (mask) {
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);
157 if (*value < 0)
158 return *value;
159
160 return IIO_VAL_INT;
161 case IIO_CHAN_INFO_SCALE:
162 *value = regulator_get_voltage(adc->reg);
163 if (*value < 0)
164 return *value;
165
166 /* convert regulator output voltage to mV */
167 *value /= 1000;
168 *shift = 8;
169
170 return IIO_VAL_FRACTIONAL_LOG2;
171 }
172
173 return -EINVAL;
174}
175
176static const struct iio_info adc0832_info = {
177 .read_raw = adc0832_read_raw,
178 .driver_module = THIS_MODULE,
179};
180
181static int adc0832_probe(struct spi_device *spi)
182{
183 struct iio_dev *indio_dev;
184 struct adc0832 *adc;
185 int ret;
186
187 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
188 if (!indio_dev)
189 return -ENOMEM;
190
191 adc = iio_priv(indio_dev);
192 adc->spi = spi;
193 mutex_init(&adc->lock);
194
195 indio_dev->name = spi_get_device_id(spi)->name;
196 indio_dev->dev.parent = &spi->dev;
Matt Ranostayb541eaf2016-07-02 17:26:33 -0700197 indio_dev->dev.of_node = spi->dev.of_node;
Akinobu Mitaefc945f2016-02-07 18:14:16 +0900198 indio_dev->info = &adc0832_info;
199 indio_dev->modes = INDIO_DIRECT_MODE;
200
201 switch (spi_get_device_id(spi)->driver_data) {
202 case adc0831:
203 adc->mux_bits = 0;
204 indio_dev->channels = adc0831_channels;
205 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
206 break;
207 case adc0832:
208 adc->mux_bits = 1;
209 indio_dev->channels = adc0832_channels;
210 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
211 break;
212 case adc0834:
213 adc->mux_bits = 2;
214 indio_dev->channels = adc0834_channels;
215 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
216 break;
217 case adc0838:
218 adc->mux_bits = 3;
219 indio_dev->channels = adc0838_channels;
220 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
221 break;
222 default:
223 return -EINVAL;
224 }
225
226 adc->reg = devm_regulator_get(&spi->dev, "vref");
227 if (IS_ERR(adc->reg))
228 return PTR_ERR(adc->reg);
229
230 ret = regulator_enable(adc->reg);
231 if (ret)
232 return ret;
233
234 spi_set_drvdata(spi, indio_dev);
235
236 ret = iio_device_register(indio_dev);
237 if (ret)
238 regulator_disable(adc->reg);
239
240 return ret;
241}
242
243static int adc0832_remove(struct spi_device *spi)
244{
245 struct iio_dev *indio_dev = spi_get_drvdata(spi);
246 struct adc0832 *adc = iio_priv(indio_dev);
247
248 iio_device_unregister(indio_dev);
249 regulator_disable(adc->reg);
250
251 return 0;
252}
253
254#ifdef CONFIG_OF
255
256static const struct of_device_id adc0832_dt_ids[] = {
257 { .compatible = "ti,adc0831", },
258 { .compatible = "ti,adc0832", },
259 { .compatible = "ti,adc0834", },
260 { .compatible = "ti,adc0838", },
261 {}
262};
263MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
264
265#endif
266
267static const struct spi_device_id adc0832_id[] = {
268 { "adc0831", adc0831 },
269 { "adc0832", adc0832 },
270 { "adc0834", adc0834 },
271 { "adc0838", adc0838 },
272 {}
273};
274MODULE_DEVICE_TABLE(spi, adc0832_id);
275
276static struct spi_driver adc0832_driver = {
277 .driver = {
278 .name = "adc0832",
279 .of_match_table = of_match_ptr(adc0832_dt_ids),
280 },
281 .probe = adc0832_probe,
282 .remove = adc0832_remove,
283 .id_table = adc0832_id,
284};
285module_spi_driver(adc0832_driver);
286
287MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
288MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
289MODULE_LICENSE("GPL v2");