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 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
tiadc_readl(struct tiadc_device * adc,unsigned int reg)44 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
45 {
46 return readl(adc->mfd_tscadc->tscadc_base + reg);
47 }
48
tiadc_writel(struct tiadc_device * adc,unsigned int reg,unsigned int val)49 static 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
get_adc_step_mask(struct tiadc_device * adc_dev)55 static 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
get_adc_chan_step_mask(struct tiadc_device * adc_dev,struct iio_chan_spec const * chan)64 static 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
get_adc_step_bit(struct tiadc_device * adc_dev,int chan)82 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
83 {
84 return 1 << adc_dev->channel_step[chan];
85 }
86
tiadc_step_config(struct iio_dev * indio_dev)87 static 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
tiadc_irq_h(int irq,void * private)150 static 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
tiadc_worker_h(int irq,void * private)190 static 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
tiadc_buffer_preenable(struct iio_dev * indio_dev)212 static 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
tiadc_buffer_postenable(struct iio_dev * indio_dev)229 static 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
tiadc_buffer_predisable(struct iio_dev * indio_dev)250 static 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
tiadc_buffer_postdisable(struct iio_dev * indio_dev)268 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
269 {
270 tiadc_step_config(indio_dev);
271
272 return 0;
273 }
274
275 static 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
tiadc_iio_buffered_hardware_setup(struct iio_dev * indio_dev,irqreturn_t (* pollfunc_bh)(int irq,void * p),irqreturn_t (* pollfunc_th)(int irq,void * p),int irq,unsigned long flags,const struct iio_buffer_setup_ops * setup_ops)282 static 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
308 error_kfifo_free:
309 iio_kfifo_free(indio_dev->buffer);
310 return ret;
311 }
312
tiadc_iio_buffered_hardware_remove(struct iio_dev * indio_dev)313 static 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
322 static const char * const chan_name_ain[] = {
323 "AIN0",
324 "AIN1",
325 "AIN2",
326 "AIN3",
327 "AIN4",
328 "AIN5",
329 "AIN6",
330 "AIN7",
331 };
332
tiadc_channel_init(struct iio_dev * indio_dev,int channels)333 static 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, sizeof(*chan_array), GFP_KERNEL);
342 if (chan_array == NULL)
343 return -ENOMEM;
344
345 chan = chan_array;
346 for (i = 0; i < channels; i++, chan++) {
347
348 chan->type = IIO_VOLTAGE;
349 chan->indexed = 1;
350 chan->channel = adc_dev->channel_line[i];
351 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
352 chan->datasheet_name = chan_name_ain[chan->channel];
353 chan->scan_index = i;
354 chan->scan_type.sign = 'u';
355 chan->scan_type.realbits = 12;
356 chan->scan_type.storagebits = 16;
357 }
358
359 indio_dev->channels = chan_array;
360
361 return 0;
362 }
363
tiadc_channels_remove(struct iio_dev * indio_dev)364 static void tiadc_channels_remove(struct iio_dev *indio_dev)
365 {
366 kfree(indio_dev->channels);
367 }
368
tiadc_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)369 static int tiadc_read_raw(struct iio_dev *indio_dev,
370 struct iio_chan_spec const *chan,
371 int *val, int *val2, long mask)
372 {
373 struct tiadc_device *adc_dev = iio_priv(indio_dev);
374 int ret = IIO_VAL_INT;
375 int i, map_val;
376 unsigned int fifo1count, read, stepid;
377 bool found = false;
378 u32 step_en;
379 unsigned long timeout;
380
381 if (iio_buffer_enabled(indio_dev))
382 return -EBUSY;
383
384 step_en = get_adc_chan_step_mask(adc_dev, chan);
385 if (!step_en)
386 return -EINVAL;
387
388 mutex_lock(&adc_dev->fifo1_lock);
389 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
390 while (fifo1count--)
391 tiadc_readl(adc_dev, REG_FIFO1);
392
393 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
394
395 timeout = jiffies + msecs_to_jiffies
396 (IDLE_TIMEOUT * adc_dev->channels);
397 /* Wait for Fifo threshold interrupt */
398 while (1) {
399 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
400 if (fifo1count)
401 break;
402
403 if (time_after(jiffies, timeout)) {
404 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
405 ret = -EAGAIN;
406 goto err_unlock;
407 }
408 }
409 map_val = adc_dev->channel_step[chan->scan_index];
410
411 /*
412 * We check the complete FIFO. We programmed just one entry but in case
413 * something went wrong we left empty handed (-EAGAIN previously) and
414 * then the value apeared somehow in the FIFO we would have two entries.
415 * Therefore we read every item and keep only the latest version of the
416 * requested channel.
417 */
418 for (i = 0; i < fifo1count; i++) {
419 read = tiadc_readl(adc_dev, REG_FIFO1);
420 stepid = read & FIFOREAD_CHNLID_MASK;
421 stepid = stepid >> 0x10;
422
423 if (stepid == map_val) {
424 read = read & FIFOREAD_DATA_MASK;
425 found = true;
426 *val = (u16) read;
427 }
428 }
429 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
430
431 if (found == false)
432 ret = -EBUSY;
433
434 err_unlock:
435 mutex_unlock(&adc_dev->fifo1_lock);
436 return ret;
437 }
438
439 static const struct iio_info tiadc_info = {
440 .read_raw = &tiadc_read_raw,
441 .driver_module = THIS_MODULE,
442 };
443
tiadc_parse_dt(struct platform_device * pdev,struct tiadc_device * adc_dev)444 static int tiadc_parse_dt(struct platform_device *pdev,
445 struct tiadc_device *adc_dev)
446 {
447 struct device_node *node = pdev->dev.of_node;
448 struct property *prop;
449 const __be32 *cur;
450 int channels = 0;
451 u32 val;
452
453 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
454 adc_dev->channel_line[channels] = val;
455
456 /* Set Default values for optional DT parameters */
457 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
458 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
459 adc_dev->step_avg[channels] = 16;
460
461 channels++;
462 }
463
464 of_property_read_u32_array(node, "ti,chan-step-avg",
465 adc_dev->step_avg, channels);
466 of_property_read_u32_array(node, "ti,chan-step-opendelay",
467 adc_dev->open_delay, channels);
468 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
469 adc_dev->sample_delay, channels);
470
471 adc_dev->channels = channels;
472 return 0;
473 }
474
tiadc_probe(struct platform_device * pdev)475 static int tiadc_probe(struct platform_device *pdev)
476 {
477 struct iio_dev *indio_dev;
478 struct tiadc_device *adc_dev;
479 struct device_node *node = pdev->dev.of_node;
480 int err;
481
482 if (!node) {
483 dev_err(&pdev->dev, "Could not find valid DT data.\n");
484 return -EINVAL;
485 }
486
487 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
488 if (indio_dev == NULL) {
489 dev_err(&pdev->dev, "failed to allocate iio device\n");
490 return -ENOMEM;
491 }
492 adc_dev = iio_priv(indio_dev);
493
494 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
495 tiadc_parse_dt(pdev, adc_dev);
496
497 indio_dev->dev.parent = &pdev->dev;
498 indio_dev->name = dev_name(&pdev->dev);
499 indio_dev->modes = INDIO_DIRECT_MODE;
500 indio_dev->info = &tiadc_info;
501
502 tiadc_step_config(indio_dev);
503 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
504 mutex_init(&adc_dev->fifo1_lock);
505
506 err = tiadc_channel_init(indio_dev, adc_dev->channels);
507 if (err < 0)
508 return err;
509
510 err = tiadc_iio_buffered_hardware_setup(indio_dev,
511 &tiadc_worker_h,
512 &tiadc_irq_h,
513 adc_dev->mfd_tscadc->irq,
514 IRQF_SHARED,
515 &tiadc_buffer_setup_ops);
516
517 if (err)
518 goto err_free_channels;
519
520 err = iio_device_register(indio_dev);
521 if (err)
522 goto err_buffer_unregister;
523
524 platform_set_drvdata(pdev, indio_dev);
525
526 return 0;
527
528 err_buffer_unregister:
529 tiadc_iio_buffered_hardware_remove(indio_dev);
530 err_free_channels:
531 tiadc_channels_remove(indio_dev);
532 return err;
533 }
534
tiadc_remove(struct platform_device * pdev)535 static int tiadc_remove(struct platform_device *pdev)
536 {
537 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
538 struct tiadc_device *adc_dev = iio_priv(indio_dev);
539 u32 step_en;
540
541 iio_device_unregister(indio_dev);
542 tiadc_iio_buffered_hardware_remove(indio_dev);
543 tiadc_channels_remove(indio_dev);
544
545 step_en = get_adc_step_mask(adc_dev);
546 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
547
548 return 0;
549 }
550
tiadc_suspend(struct device * dev)551 static int __maybe_unused tiadc_suspend(struct device *dev)
552 {
553 struct iio_dev *indio_dev = dev_get_drvdata(dev);
554 struct tiadc_device *adc_dev = iio_priv(indio_dev);
555 struct ti_tscadc_dev *tscadc_dev;
556 unsigned int idle;
557
558 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
559 if (!device_may_wakeup(tscadc_dev->dev)) {
560 idle = tiadc_readl(adc_dev, REG_CTRL);
561 idle &= ~(CNTRLREG_TSCSSENB);
562 tiadc_writel(adc_dev, REG_CTRL, (idle |
563 CNTRLREG_POWERDOWN));
564 }
565
566 return 0;
567 }
568
tiadc_resume(struct device * dev)569 static int __maybe_unused tiadc_resume(struct device *dev)
570 {
571 struct iio_dev *indio_dev = dev_get_drvdata(dev);
572 struct tiadc_device *adc_dev = iio_priv(indio_dev);
573 unsigned int restore;
574
575 /* Make sure ADC is powered up */
576 restore = tiadc_readl(adc_dev, REG_CTRL);
577 restore &= ~(CNTRLREG_POWERDOWN);
578 tiadc_writel(adc_dev, REG_CTRL, restore);
579
580 tiadc_step_config(indio_dev);
581 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
582 adc_dev->buffer_en_ch_steps);
583 return 0;
584 }
585
586 static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
587
588 static const struct of_device_id ti_adc_dt_ids[] = {
589 { .compatible = "ti,am3359-adc", },
590 { }
591 };
592 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
593
594 static struct platform_driver tiadc_driver = {
595 .driver = {
596 .name = "TI-am335x-adc",
597 .pm = &tiadc_pm_ops,
598 .of_match_table = ti_adc_dt_ids,
599 },
600 .probe = tiadc_probe,
601 .remove = tiadc_remove,
602 };
603 module_platform_driver(tiadc_driver);
604
605 MODULE_DESCRIPTION("TI ADC controller driver");
606 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
607 MODULE_LICENSE("GPL");
608