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