]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/iio/adc/ti_am335x_adc.c
iio: adc: xilinx: Fix VREFP scale
[mirror_ubuntu-jammy-kernel.git] / drivers / iio / adc / ti_am335x_adc.c
1 /*
2 * TI ADC MFD driver
3 *
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/io.h>
23 #include <linux/iio/iio.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/iio/machine.h>
27 #include <linux/iio/driver.h>
28
29 #include <linux/mfd/ti_am335x_tscadc.h>
30 #include <linux/iio/buffer.h>
31 #include <linux/iio/kfifo_buf.h>
32
33 struct tiadc_device {
34 struct ti_tscadc_dev *mfd_tscadc;
35 int channels;
36 u8 channel_line[8];
37 u8 channel_step[8];
38 int buffer_en_ch_steps;
39 u16 data[8];
40 };
41
42 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
43 {
44 return readl(adc->mfd_tscadc->tscadc_base + reg);
45 }
46
47 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
48 unsigned int val)
49 {
50 writel(val, adc->mfd_tscadc->tscadc_base + reg);
51 }
52
53 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
54 {
55 u32 step_en;
56
57 step_en = ((1 << adc_dev->channels) - 1);
58 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
59 return step_en;
60 }
61
62 static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
63 struct iio_chan_spec const *chan)
64 {
65 int i;
66
67 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
68 if (chan->channel == adc_dev->channel_line[i]) {
69 u32 step;
70
71 step = adc_dev->channel_step[i];
72 /* +1 for the charger */
73 return 1 << (step + 1);
74 }
75 }
76 WARN_ON(1);
77 return 0;
78 }
79
80 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
81 {
82 return 1 << adc_dev->channel_step[chan];
83 }
84
85 static void tiadc_step_config(struct iio_dev *indio_dev)
86 {
87 struct tiadc_device *adc_dev = iio_priv(indio_dev);
88 unsigned int stepconfig;
89 int i, steps = 0;
90
91 /*
92 * There are 16 configurable steps and 8 analog input
93 * lines available which are shared between Touchscreen and ADC.
94 *
95 * Steps forwards i.e. from 0 towards 16 are used by ADC
96 * depending on number of input lines needed.
97 * Channel would represent which analog input
98 * needs to be given to ADC to digitalize data.
99 */
100
101 if (iio_buffer_enabled(indio_dev))
102 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
103 | STEPCONFIG_MODE_SWCNT;
104 else
105 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
106
107 for (i = 0; i < adc_dev->channels; i++) {
108 int chan;
109
110 chan = adc_dev->channel_line[i];
111 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
112 stepconfig | STEPCONFIG_INP(chan));
113 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
114 STEPCONFIG_OPENDLY);
115 adc_dev->channel_step[i] = steps;
116 steps++;
117 }
118 }
119
120 static irqreturn_t tiadc_irq_h(int irq, void *private)
121 {
122 struct iio_dev *indio_dev = private;
123 struct tiadc_device *adc_dev = iio_priv(indio_dev);
124 unsigned int status, config;
125 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
126
127 /*
128 * ADC and touchscreen share the IRQ line.
129 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
130 */
131 if (status & IRQENB_FIFO1OVRRUN) {
132 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
133 config = tiadc_readl(adc_dev, REG_CTRL);
134 config &= ~(CNTRLREG_TSCSSENB);
135 tiadc_writel(adc_dev, REG_CTRL, config);
136 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
137 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
138 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
139 return IRQ_HANDLED;
140 } else if (status & IRQENB_FIFO1THRES) {
141 /* Disable irq and wake worker thread */
142 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
143 return IRQ_WAKE_THREAD;
144 }
145
146 return IRQ_NONE;
147 }
148
149 static irqreturn_t tiadc_worker_h(int irq, void *private)
150 {
151 struct iio_dev *indio_dev = private;
152 struct tiadc_device *adc_dev = iio_priv(indio_dev);
153 int i, k, fifo1count, read;
154 u16 *data = adc_dev->data;
155
156 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
157 for (k = 0; k < fifo1count; k = k + i) {
158 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
159 read = tiadc_readl(adc_dev, REG_FIFO1);
160 data[i] = read & FIFOREAD_DATA_MASK;
161 }
162 iio_push_to_buffers(indio_dev, (u8 *) data);
163 }
164
165 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
166 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
167
168 return IRQ_HANDLED;
169 }
170
171 static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
172 {
173 struct tiadc_device *adc_dev = iio_priv(indio_dev);
174 int i, fifo1count, read;
175
176 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
177 IRQENB_FIFO1OVRRUN |
178 IRQENB_FIFO1UNDRFLW));
179
180 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
181 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
182 for (i = 0; i < fifo1count; i++)
183 read = tiadc_readl(adc_dev, REG_FIFO1);
184
185 return 0;
186 }
187
188 static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
189 {
190 struct tiadc_device *adc_dev = iio_priv(indio_dev);
191 unsigned int enb = 0;
192 u8 bit;
193
194 tiadc_step_config(indio_dev);
195 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels)
196 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
197 adc_dev->buffer_en_ch_steps = enb;
198
199 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
200
201 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
202 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
203 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
204 | IRQENB_FIFO1OVRRUN);
205
206 return 0;
207 }
208
209 static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
210 {
211 struct tiadc_device *adc_dev = iio_priv(indio_dev);
212 int fifo1count, i, read;
213
214 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
215 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
216 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
217 adc_dev->buffer_en_ch_steps = 0;
218
219 /* Flush FIFO of leftover data in the time it takes to disable adc */
220 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
221 for (i = 0; i < fifo1count; i++)
222 read = tiadc_readl(adc_dev, REG_FIFO1);
223
224 return 0;
225 }
226
227 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
228 {
229 tiadc_step_config(indio_dev);
230
231 return 0;
232 }
233
234 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
235 .preenable = &tiadc_buffer_preenable,
236 .postenable = &tiadc_buffer_postenable,
237 .predisable = &tiadc_buffer_predisable,
238 .postdisable = &tiadc_buffer_postdisable,
239 };
240
241 static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
242 irqreturn_t (*pollfunc_bh)(int irq, void *p),
243 irqreturn_t (*pollfunc_th)(int irq, void *p),
244 int irq,
245 unsigned long flags,
246 const struct iio_buffer_setup_ops *setup_ops)
247 {
248 struct iio_buffer *buffer;
249 int ret;
250
251 buffer = iio_kfifo_allocate();
252 if (!buffer)
253 return -ENOMEM;
254
255 iio_device_attach_buffer(indio_dev, buffer);
256
257 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
258 flags, indio_dev->name, indio_dev);
259 if (ret)
260 goto error_kfifo_free;
261
262 indio_dev->setup_ops = setup_ops;
263 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
264
265 return 0;
266
267 error_kfifo_free:
268 iio_kfifo_free(indio_dev->buffer);
269 return ret;
270 }
271
272 static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
273 {
274 struct tiadc_device *adc_dev = iio_priv(indio_dev);
275
276 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
277 iio_kfifo_free(indio_dev->buffer);
278 }
279
280
281 static const char * const chan_name_ain[] = {
282 "AIN0",
283 "AIN1",
284 "AIN2",
285 "AIN3",
286 "AIN4",
287 "AIN5",
288 "AIN6",
289 "AIN7",
290 };
291
292 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
293 {
294 struct tiadc_device *adc_dev = iio_priv(indio_dev);
295 struct iio_chan_spec *chan_array;
296 struct iio_chan_spec *chan;
297 int i;
298
299 indio_dev->num_channels = channels;
300 chan_array = kcalloc(channels,
301 sizeof(struct iio_chan_spec), GFP_KERNEL);
302 if (chan_array == NULL)
303 return -ENOMEM;
304
305 chan = chan_array;
306 for (i = 0; i < channels; i++, chan++) {
307
308 chan->type = IIO_VOLTAGE;
309 chan->indexed = 1;
310 chan->channel = adc_dev->channel_line[i];
311 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
312 chan->datasheet_name = chan_name_ain[chan->channel];
313 chan->scan_index = i;
314 chan->scan_type.sign = 'u';
315 chan->scan_type.realbits = 12;
316 chan->scan_type.storagebits = 16;
317 }
318
319 indio_dev->channels = chan_array;
320
321 return 0;
322 }
323
324 static void tiadc_channels_remove(struct iio_dev *indio_dev)
325 {
326 kfree(indio_dev->channels);
327 }
328
329 static int tiadc_read_raw(struct iio_dev *indio_dev,
330 struct iio_chan_spec const *chan,
331 int *val, int *val2, long mask)
332 {
333 struct tiadc_device *adc_dev = iio_priv(indio_dev);
334 int i, map_val;
335 unsigned int fifo1count, read, stepid;
336 bool found = false;
337 u32 step_en;
338 unsigned long timeout;
339
340 if (iio_buffer_enabled(indio_dev))
341 return -EBUSY;
342
343 step_en = get_adc_chan_step_mask(adc_dev, chan);
344 if (!step_en)
345 return -EINVAL;
346
347 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
348 while (fifo1count--)
349 tiadc_readl(adc_dev, REG_FIFO1);
350
351 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
352
353 timeout = jiffies + usecs_to_jiffies
354 (IDLE_TIMEOUT * adc_dev->channels);
355 /* Wait for Fifo threshold interrupt */
356 while (1) {
357 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
358 if (fifo1count)
359 break;
360
361 if (time_after(jiffies, timeout)) {
362 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
363 return -EAGAIN;
364 }
365 }
366 map_val = adc_dev->channel_step[chan->scan_index];
367
368 /*
369 * We check the complete FIFO. We programmed just one entry but in case
370 * something went wrong we left empty handed (-EAGAIN previously) and
371 * then the value apeared somehow in the FIFO we would have two entries.
372 * Therefore we read every item and keep only the latest version of the
373 * requested channel.
374 */
375 for (i = 0; i < fifo1count; i++) {
376 read = tiadc_readl(adc_dev, REG_FIFO1);
377 stepid = read & FIFOREAD_CHNLID_MASK;
378 stepid = stepid >> 0x10;
379
380 if (stepid == map_val) {
381 read = read & FIFOREAD_DATA_MASK;
382 found = true;
383 *val = (u16) read;
384 }
385 }
386 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
387
388 if (found == false)
389 return -EBUSY;
390 return IIO_VAL_INT;
391 }
392
393 static const struct iio_info tiadc_info = {
394 .read_raw = &tiadc_read_raw,
395 .driver_module = THIS_MODULE,
396 };
397
398 static int tiadc_probe(struct platform_device *pdev)
399 {
400 struct iio_dev *indio_dev;
401 struct tiadc_device *adc_dev;
402 struct device_node *node = pdev->dev.of_node;
403 struct property *prop;
404 const __be32 *cur;
405 int err;
406 u32 val;
407 int channels = 0;
408
409 if (!node) {
410 dev_err(&pdev->dev, "Could not find valid DT data.\n");
411 return -EINVAL;
412 }
413
414 indio_dev = devm_iio_device_alloc(&pdev->dev,
415 sizeof(struct tiadc_device));
416 if (indio_dev == NULL) {
417 dev_err(&pdev->dev, "failed to allocate iio device\n");
418 return -ENOMEM;
419 }
420 adc_dev = iio_priv(indio_dev);
421
422 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
423
424 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
425 adc_dev->channel_line[channels] = val;
426 channels++;
427 }
428 adc_dev->channels = channels;
429
430 indio_dev->dev.parent = &pdev->dev;
431 indio_dev->name = dev_name(&pdev->dev);
432 indio_dev->modes = INDIO_DIRECT_MODE;
433 indio_dev->info = &tiadc_info;
434
435 tiadc_step_config(indio_dev);
436 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
437
438 err = tiadc_channel_init(indio_dev, adc_dev->channels);
439 if (err < 0)
440 return err;
441
442 err = tiadc_iio_buffered_hardware_setup(indio_dev,
443 &tiadc_worker_h,
444 &tiadc_irq_h,
445 adc_dev->mfd_tscadc->irq,
446 IRQF_SHARED,
447 &tiadc_buffer_setup_ops);
448
449 if (err)
450 goto err_free_channels;
451
452 err = iio_device_register(indio_dev);
453 if (err)
454 goto err_buffer_unregister;
455
456 platform_set_drvdata(pdev, indio_dev);
457
458 return 0;
459
460 err_buffer_unregister:
461 tiadc_iio_buffered_hardware_remove(indio_dev);
462 err_free_channels:
463 tiadc_channels_remove(indio_dev);
464 return err;
465 }
466
467 static int tiadc_remove(struct platform_device *pdev)
468 {
469 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
470 struct tiadc_device *adc_dev = iio_priv(indio_dev);
471 u32 step_en;
472
473 iio_device_unregister(indio_dev);
474 tiadc_iio_buffered_hardware_remove(indio_dev);
475 tiadc_channels_remove(indio_dev);
476
477 step_en = get_adc_step_mask(adc_dev);
478 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
479
480 return 0;
481 }
482
483 #ifdef CONFIG_PM
484 static int tiadc_suspend(struct device *dev)
485 {
486 struct iio_dev *indio_dev = dev_get_drvdata(dev);
487 struct tiadc_device *adc_dev = iio_priv(indio_dev);
488 struct ti_tscadc_dev *tscadc_dev;
489 unsigned int idle;
490
491 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
492 if (!device_may_wakeup(tscadc_dev->dev)) {
493 idle = tiadc_readl(adc_dev, REG_CTRL);
494 idle &= ~(CNTRLREG_TSCSSENB);
495 tiadc_writel(adc_dev, REG_CTRL, (idle |
496 CNTRLREG_POWERDOWN));
497 }
498
499 return 0;
500 }
501
502 static int tiadc_resume(struct device *dev)
503 {
504 struct iio_dev *indio_dev = dev_get_drvdata(dev);
505 struct tiadc_device *adc_dev = iio_priv(indio_dev);
506 unsigned int restore;
507
508 /* Make sure ADC is powered up */
509 restore = tiadc_readl(adc_dev, REG_CTRL);
510 restore &= ~(CNTRLREG_POWERDOWN);
511 tiadc_writel(adc_dev, REG_CTRL, restore);
512
513 tiadc_step_config(indio_dev);
514 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
515 adc_dev->buffer_en_ch_steps);
516 return 0;
517 }
518
519 static const struct dev_pm_ops tiadc_pm_ops = {
520 .suspend = tiadc_suspend,
521 .resume = tiadc_resume,
522 };
523 #define TIADC_PM_OPS (&tiadc_pm_ops)
524 #else
525 #define TIADC_PM_OPS NULL
526 #endif
527
528 static const struct of_device_id ti_adc_dt_ids[] = {
529 { .compatible = "ti,am3359-adc", },
530 { }
531 };
532 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
533
534 static struct platform_driver tiadc_driver = {
535 .driver = {
536 .name = "TI-am335x-adc",
537 .pm = TIADC_PM_OPS,
538 .of_match_table = ti_adc_dt_ids,
539 },
540 .probe = tiadc_probe,
541 .remove = tiadc_remove,
542 };
543 module_platform_driver(tiadc_driver);
544
545 MODULE_DESCRIPTION("TI ADC controller driver");
546 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
547 MODULE_LICENSE("GPL");