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