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