1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/acpi.h>
24#include <linux/iio/iio.h>
25#include <linux/iio/buffer.h>
26#include <linux/iio/types.h>
27#include <linux/iio/triggered_buffer.h>
28#include <linux/iio/trigger_consumer.h>
29#include <linux/interrupt.h>
30#include <linux/module.h>
31#include <linux/property.h>
32#include <linux/regulator/consumer.h>
33#include <linux/spi/spi.h>
34
35
36
37
38
39
40#define ADC108S102_VA_MV_ACPI_DEFAULT 5000
41
42
43
44
45
46
47
48#define ADC108S102_BITS 12
49#define ADC108S102_MAX_CHANNELS 8
50
51
52
53
54
55
56
57#define ADC108S102_CMD(ch) ((u16)(ch) << 11)
58
59
60
61
62
63
64#define ADC108S102_RES_DATA(res) ((u16)res & GENMASK(11, 0))
65
66struct adc108s102_state {
67 struct spi_device *spi;
68 struct regulator *reg;
69 u32 va_millivolt;
70
71 struct spi_transfer ring_xfer;
72
73 struct spi_transfer scan_single_xfer;
74
75 struct spi_message ring_msg;
76
77 struct spi_message scan_single_msg;
78
79
80
81
82
83
84
85
86
87 __be16 rx_buf[13] ____cacheline_aligned;
88 __be16 tx_buf[9] ____cacheline_aligned;
89};
90
91#define ADC108S102_V_CHAN(index) \
92 { \
93 .type = IIO_VOLTAGE, \
94 .indexed = 1, \
95 .channel = index, \
96 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
97 BIT(IIO_CHAN_INFO_SCALE), \
98 .address = index, \
99 .scan_index = index, \
100 .scan_type = { \
101 .sign = 'u', \
102 .realbits = ADC108S102_BITS, \
103 .storagebits = 16, \
104 .endianness = IIO_BE, \
105 }, \
106 }
107
108static const struct iio_chan_spec adc108s102_channels[] = {
109 ADC108S102_V_CHAN(0),
110 ADC108S102_V_CHAN(1),
111 ADC108S102_V_CHAN(2),
112 ADC108S102_V_CHAN(3),
113 ADC108S102_V_CHAN(4),
114 ADC108S102_V_CHAN(5),
115 ADC108S102_V_CHAN(6),
116 ADC108S102_V_CHAN(7),
117 IIO_CHAN_SOFT_TIMESTAMP(8),
118};
119
120static int adc108s102_update_scan_mode(struct iio_dev *indio_dev,
121 unsigned long const *active_scan_mask)
122{
123 struct adc108s102_state *st = iio_priv(indio_dev);
124 unsigned int bit, cmds;
125
126
127
128
129
130 cmds = 0;
131 for_each_set_bit(bit, active_scan_mask, ADC108S102_MAX_CHANNELS)
132 st->tx_buf[cmds++] = cpu_to_be16(ADC108S102_CMD(bit));
133
134
135 st->tx_buf[cmds++] = 0x00;
136
137
138 st->ring_xfer.tx_buf = &st->tx_buf[0];
139 st->ring_xfer.rx_buf = &st->rx_buf[0];
140 st->ring_xfer.len = cmds * sizeof(st->tx_buf[0]);
141
142 spi_message_init_with_transfers(&st->ring_msg, &st->ring_xfer, 1);
143
144 return 0;
145}
146
147static irqreturn_t adc108s102_trigger_handler(int irq, void *p)
148{
149 struct iio_poll_func *pf = p;
150 struct iio_dev *indio_dev = pf->indio_dev;
151 struct adc108s102_state *st = iio_priv(indio_dev);
152 int ret;
153
154 ret = spi_sync(st->spi, &st->ring_msg);
155 if (ret < 0)
156 goto out_notify;
157
158
159 iio_push_to_buffers_with_timestamp(indio_dev,
160 (u8 *)&st->rx_buf[1],
161 iio_get_time_ns(indio_dev));
162
163out_notify:
164 iio_trigger_notify_done(indio_dev->trig);
165
166 return IRQ_HANDLED;
167}
168
169static int adc108s102_scan_direct(struct adc108s102_state *st, unsigned int ch)
170{
171 int ret;
172
173 st->tx_buf[0] = cpu_to_be16(ADC108S102_CMD(ch));
174 ret = spi_sync(st->spi, &st->scan_single_msg);
175 if (ret)
176 return ret;
177
178
179 return be16_to_cpu(st->rx_buf[1]);
180}
181
182static int adc108s102_read_raw(struct iio_dev *indio_dev,
183 struct iio_chan_spec const *chan,
184 int *val, int *val2, long m)
185{
186 struct adc108s102_state *st = iio_priv(indio_dev);
187 int ret;
188
189 switch (m) {
190 case IIO_CHAN_INFO_RAW:
191 ret = iio_device_claim_direct_mode(indio_dev);
192 if (ret)
193 return ret;
194
195 ret = adc108s102_scan_direct(st, chan->address);
196
197 iio_device_release_direct_mode(indio_dev);
198
199 if (ret < 0)
200 return ret;
201
202 *val = ADC108S102_RES_DATA(ret);
203
204 return IIO_VAL_INT;
205 case IIO_CHAN_INFO_SCALE:
206 if (chan->type != IIO_VOLTAGE)
207 break;
208
209 *val = st->va_millivolt;
210 *val2 = chan->scan_type.realbits;
211
212 return IIO_VAL_FRACTIONAL_LOG2;
213 default:
214 break;
215 }
216
217 return -EINVAL;
218}
219
220static const struct iio_info adc108s102_info = {
221 .read_raw = &adc108s102_read_raw,
222 .update_scan_mode = &adc108s102_update_scan_mode,
223};
224
225static int adc108s102_probe(struct spi_device *spi)
226{
227 struct adc108s102_state *st;
228 struct iio_dev *indio_dev;
229 int ret;
230
231 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
232 if (!indio_dev)
233 return -ENOMEM;
234
235 st = iio_priv(indio_dev);
236
237 if (ACPI_COMPANION(&spi->dev)) {
238 st->va_millivolt = ADC108S102_VA_MV_ACPI_DEFAULT;
239 } else {
240 st->reg = devm_regulator_get(&spi->dev, "vref");
241 if (IS_ERR(st->reg))
242 return PTR_ERR(st->reg);
243
244 ret = regulator_enable(st->reg);
245 if (ret < 0) {
246 dev_err(&spi->dev, "Cannot enable vref regulator\n");
247 return ret;
248 }
249
250 ret = regulator_get_voltage(st->reg);
251 if (ret < 0) {
252 dev_err(&spi->dev, "vref get voltage failed\n");
253 return ret;
254 }
255
256 st->va_millivolt = ret / 1000;
257 }
258
259 spi_set_drvdata(spi, indio_dev);
260 st->spi = spi;
261
262 indio_dev->name = spi->modalias;
263 indio_dev->dev.parent = &spi->dev;
264 indio_dev->modes = INDIO_DIRECT_MODE;
265 indio_dev->channels = adc108s102_channels;
266 indio_dev->num_channels = ARRAY_SIZE(adc108s102_channels);
267 indio_dev->info = &adc108s102_info;
268
269
270 st->scan_single_xfer.tx_buf = st->tx_buf;
271 st->scan_single_xfer.rx_buf = st->rx_buf;
272 st->scan_single_xfer.len = 2 * sizeof(st->tx_buf[0]);
273
274 spi_message_init_with_transfers(&st->scan_single_msg,
275 &st->scan_single_xfer, 1);
276
277 ret = iio_triggered_buffer_setup(indio_dev, NULL,
278 &adc108s102_trigger_handler, NULL);
279 if (ret)
280 goto error_disable_reg;
281
282 ret = iio_device_register(indio_dev);
283 if (ret) {
284 dev_err(&spi->dev, "Failed to register IIO device\n");
285 goto error_cleanup_triggered_buffer;
286 }
287 return 0;
288
289error_cleanup_triggered_buffer:
290 iio_triggered_buffer_cleanup(indio_dev);
291
292error_disable_reg:
293 regulator_disable(st->reg);
294
295 return ret;
296}
297
298static int adc108s102_remove(struct spi_device *spi)
299{
300 struct iio_dev *indio_dev = spi_get_drvdata(spi);
301 struct adc108s102_state *st = iio_priv(indio_dev);
302
303 iio_device_unregister(indio_dev);
304 iio_triggered_buffer_cleanup(indio_dev);
305
306 regulator_disable(st->reg);
307
308 return 0;
309}
310
311#ifdef CONFIG_OF
312static const struct of_device_id adc108s102_of_match[] = {
313 { .compatible = "ti,adc108s102" },
314 { }
315};
316MODULE_DEVICE_TABLE(of, adc108s102_of_match);
317#endif
318
319#ifdef CONFIG_ACPI
320static const struct acpi_device_id adc108s102_acpi_ids[] = {
321 { "INT3495", 0 },
322 { }
323};
324MODULE_DEVICE_TABLE(acpi, adc108s102_acpi_ids);
325#endif
326
327static const struct spi_device_id adc108s102_id[] = {
328 { "adc108s102", 0 },
329 { }
330};
331MODULE_DEVICE_TABLE(spi, adc108s102_id);
332
333static struct spi_driver adc108s102_driver = {
334 .driver = {
335 .name = "adc108s102",
336 .of_match_table = of_match_ptr(adc108s102_of_match),
337 .acpi_match_table = ACPI_PTR(adc108s102_acpi_ids),
338 },
339 .probe = adc108s102_probe,
340 .remove = adc108s102_remove,
341 .id_table = adc108s102_id,
342};
343module_spi_driver(adc108s102_driver);
344
345MODULE_AUTHOR("Bogdan Pricop <bogdan.pricop@emutex.com>");
346MODULE_DESCRIPTION("Texas Instruments ADC108S102 and ADC128S102 driver");
347MODULE_LICENSE("GPL v2");
348