Merge remote-tracking branch 'lightnvm/for-next'
[deliverable/linux.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 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
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
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
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
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
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
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
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;
155 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
156
157 /*
158 * ADC and touchscreen share the IRQ line.
159 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
160 */
161 if (status & IRQENB_FIFO1OVRRUN) {
162 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
163 config = tiadc_readl(adc_dev, REG_CTRL);
164 config &= ~(CNTRLREG_TSCSSENB);
165 tiadc_writel(adc_dev, REG_CTRL, config);
166 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
167 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
168 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
169 return IRQ_HANDLED;
170 } else if (status & IRQENB_FIFO1THRES) {
171 /* Disable irq and wake worker thread */
172 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
173 return IRQ_WAKE_THREAD;
174 }
175
176 return IRQ_NONE;
177 }
178
179 static irqreturn_t tiadc_worker_h(int irq, void *private)
180 {
181 struct iio_dev *indio_dev = private;
182 struct tiadc_device *adc_dev = iio_priv(indio_dev);
183 int i, k, fifo1count, read;
184 u16 *data = adc_dev->data;
185
186 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
187 for (k = 0; k < fifo1count; k = k + i) {
188 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
189 read = tiadc_readl(adc_dev, REG_FIFO1);
190 data[i] = read & FIFOREAD_DATA_MASK;
191 }
192 iio_push_to_buffers(indio_dev, (u8 *) data);
193 }
194
195 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
196 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
197
198 return IRQ_HANDLED;
199 }
200
201 static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
202 {
203 struct tiadc_device *adc_dev = iio_priv(indio_dev);
204 int i, fifo1count, read;
205
206 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
207 IRQENB_FIFO1OVRRUN |
208 IRQENB_FIFO1UNDRFLW));
209
210 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
211 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
212 for (i = 0; i < fifo1count; i++)
213 read = tiadc_readl(adc_dev, REG_FIFO1);
214
215 return 0;
216 }
217
218 static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
219 {
220 struct tiadc_device *adc_dev = iio_priv(indio_dev);
221 unsigned int enb = 0;
222 u8 bit;
223
224 tiadc_step_config(indio_dev);
225 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels)
226 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
227 adc_dev->buffer_en_ch_steps = enb;
228
229 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
230
231 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
232 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
233 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
234 | IRQENB_FIFO1OVRRUN);
235
236 return 0;
237 }
238
239 static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
240 {
241 struct tiadc_device *adc_dev = iio_priv(indio_dev);
242 int fifo1count, i, read;
243
244 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
245 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
246 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
247 adc_dev->buffer_en_ch_steps = 0;
248
249 /* Flush FIFO of leftover data in the time it takes to disable adc */
250 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
251 for (i = 0; i < fifo1count; i++)
252 read = tiadc_readl(adc_dev, REG_FIFO1);
253
254 return 0;
255 }
256
257 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
258 {
259 tiadc_step_config(indio_dev);
260
261 return 0;
262 }
263
264 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
265 .preenable = &tiadc_buffer_preenable,
266 .postenable = &tiadc_buffer_postenable,
267 .predisable = &tiadc_buffer_predisable,
268 .postdisable = &tiadc_buffer_postdisable,
269 };
270
271 static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
272 irqreturn_t (*pollfunc_bh)(int irq, void *p),
273 irqreturn_t (*pollfunc_th)(int irq, void *p),
274 int irq,
275 unsigned long flags,
276 const struct iio_buffer_setup_ops *setup_ops)
277 {
278 struct iio_buffer *buffer;
279 int ret;
280
281 buffer = iio_kfifo_allocate();
282 if (!buffer)
283 return -ENOMEM;
284
285 iio_device_attach_buffer(indio_dev, buffer);
286
287 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
288 flags, indio_dev->name, indio_dev);
289 if (ret)
290 goto error_kfifo_free;
291
292 indio_dev->setup_ops = setup_ops;
293 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
294
295 return 0;
296
297 error_kfifo_free:
298 iio_kfifo_free(indio_dev->buffer);
299 return ret;
300 }
301
302 static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
303 {
304 struct tiadc_device *adc_dev = iio_priv(indio_dev);
305
306 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
307 iio_kfifo_free(indio_dev->buffer);
308 }
309
310
311 static const char * const chan_name_ain[] = {
312 "AIN0",
313 "AIN1",
314 "AIN2",
315 "AIN3",
316 "AIN4",
317 "AIN5",
318 "AIN6",
319 "AIN7",
320 };
321
322 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
323 {
324 struct tiadc_device *adc_dev = iio_priv(indio_dev);
325 struct iio_chan_spec *chan_array;
326 struct iio_chan_spec *chan;
327 int i;
328
329 indio_dev->num_channels = channels;
330 chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
331 if (chan_array == NULL)
332 return -ENOMEM;
333
334 chan = chan_array;
335 for (i = 0; i < channels; i++, chan++) {
336
337 chan->type = IIO_VOLTAGE;
338 chan->indexed = 1;
339 chan->channel = adc_dev->channel_line[i];
340 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
341 chan->datasheet_name = chan_name_ain[chan->channel];
342 chan->scan_index = i;
343 chan->scan_type.sign = 'u';
344 chan->scan_type.realbits = 12;
345 chan->scan_type.storagebits = 16;
346 }
347
348 indio_dev->channels = chan_array;
349
350 return 0;
351 }
352
353 static void tiadc_channels_remove(struct iio_dev *indio_dev)
354 {
355 kfree(indio_dev->channels);
356 }
357
358 static int tiadc_read_raw(struct iio_dev *indio_dev,
359 struct iio_chan_spec const *chan,
360 int *val, int *val2, long mask)
361 {
362 struct tiadc_device *adc_dev = iio_priv(indio_dev);
363 int ret = IIO_VAL_INT;
364 int i, map_val;
365 unsigned int fifo1count, read, stepid;
366 bool found = false;
367 u32 step_en;
368 unsigned long timeout;
369
370 if (iio_buffer_enabled(indio_dev))
371 return -EBUSY;
372
373 step_en = get_adc_chan_step_mask(adc_dev, chan);
374 if (!step_en)
375 return -EINVAL;
376
377 mutex_lock(&adc_dev->fifo1_lock);
378 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
379 while (fifo1count--)
380 tiadc_readl(adc_dev, REG_FIFO1);
381
382 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
383
384 timeout = jiffies + msecs_to_jiffies
385 (IDLE_TIMEOUT * adc_dev->channels);
386 /* Wait for Fifo threshold interrupt */
387 while (1) {
388 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
389 if (fifo1count)
390 break;
391
392 if (time_after(jiffies, timeout)) {
393 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
394 ret = -EAGAIN;
395 goto err_unlock;
396 }
397 }
398 map_val = adc_dev->channel_step[chan->scan_index];
399
400 /*
401 * We check the complete FIFO. We programmed just one entry but in case
402 * something went wrong we left empty handed (-EAGAIN previously) and
403 * then the value apeared somehow in the FIFO we would have two entries.
404 * Therefore we read every item and keep only the latest version of the
405 * requested channel.
406 */
407 for (i = 0; i < fifo1count; i++) {
408 read = tiadc_readl(adc_dev, REG_FIFO1);
409 stepid = read & FIFOREAD_CHNLID_MASK;
410 stepid = stepid >> 0x10;
411
412 if (stepid == map_val) {
413 read = read & FIFOREAD_DATA_MASK;
414 found = true;
415 *val = (u16) read;
416 }
417 }
418 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
419
420 if (found == false)
421 ret = -EBUSY;
422
423 err_unlock:
424 mutex_unlock(&adc_dev->fifo1_lock);
425 return ret;
426 }
427
428 static const struct iio_info tiadc_info = {
429 .read_raw = &tiadc_read_raw,
430 .driver_module = THIS_MODULE,
431 };
432
433 static int tiadc_parse_dt(struct platform_device *pdev,
434 struct tiadc_device *adc_dev)
435 {
436 struct device_node *node = pdev->dev.of_node;
437 struct property *prop;
438 const __be32 *cur;
439 int channels = 0;
440 u32 val;
441
442 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
443 adc_dev->channel_line[channels] = val;
444
445 /* Set Default values for optional DT parameters */
446 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
447 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
448 adc_dev->step_avg[channels] = 16;
449
450 channels++;
451 }
452
453 of_property_read_u32_array(node, "ti,chan-step-avg",
454 adc_dev->step_avg, channels);
455 of_property_read_u32_array(node, "ti,chan-step-opendelay",
456 adc_dev->open_delay, channels);
457 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
458 adc_dev->sample_delay, channels);
459
460 adc_dev->channels = channels;
461 return 0;
462 }
463
464 static int tiadc_probe(struct platform_device *pdev)
465 {
466 struct iio_dev *indio_dev;
467 struct tiadc_device *adc_dev;
468 struct device_node *node = pdev->dev.of_node;
469 int err;
470
471 if (!node) {
472 dev_err(&pdev->dev, "Could not find valid DT data.\n");
473 return -EINVAL;
474 }
475
476 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev));
477 if (indio_dev == NULL) {
478 dev_err(&pdev->dev, "failed to allocate iio device\n");
479 return -ENOMEM;
480 }
481 adc_dev = iio_priv(indio_dev);
482
483 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
484 tiadc_parse_dt(pdev, adc_dev);
485
486 indio_dev->dev.parent = &pdev->dev;
487 indio_dev->name = dev_name(&pdev->dev);
488 indio_dev->modes = INDIO_DIRECT_MODE;
489 indio_dev->info = &tiadc_info;
490
491 tiadc_step_config(indio_dev);
492 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
493 mutex_init(&adc_dev->fifo1_lock);
494
495 err = tiadc_channel_init(indio_dev, adc_dev->channels);
496 if (err < 0)
497 return err;
498
499 err = tiadc_iio_buffered_hardware_setup(indio_dev,
500 &tiadc_worker_h,
501 &tiadc_irq_h,
502 adc_dev->mfd_tscadc->irq,
503 IRQF_SHARED,
504 &tiadc_buffer_setup_ops);
505
506 if (err)
507 goto err_free_channels;
508
509 err = iio_device_register(indio_dev);
510 if (err)
511 goto err_buffer_unregister;
512
513 platform_set_drvdata(pdev, indio_dev);
514
515 return 0;
516
517 err_buffer_unregister:
518 tiadc_iio_buffered_hardware_remove(indio_dev);
519 err_free_channels:
520 tiadc_channels_remove(indio_dev);
521 return err;
522 }
523
524 static int tiadc_remove(struct platform_device *pdev)
525 {
526 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
527 struct tiadc_device *adc_dev = iio_priv(indio_dev);
528 u32 step_en;
529
530 iio_device_unregister(indio_dev);
531 tiadc_iio_buffered_hardware_remove(indio_dev);
532 tiadc_channels_remove(indio_dev);
533
534 step_en = get_adc_step_mask(adc_dev);
535 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
536
537 return 0;
538 }
539
540 static int __maybe_unused tiadc_suspend(struct device *dev)
541 {
542 struct iio_dev *indio_dev = dev_get_drvdata(dev);
543 struct tiadc_device *adc_dev = iio_priv(indio_dev);
544 struct ti_tscadc_dev *tscadc_dev;
545 unsigned int idle;
546
547 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
548 if (!device_may_wakeup(tscadc_dev->dev)) {
549 idle = tiadc_readl(adc_dev, REG_CTRL);
550 idle &= ~(CNTRLREG_TSCSSENB);
551 tiadc_writel(adc_dev, REG_CTRL, (idle |
552 CNTRLREG_POWERDOWN));
553 }
554
555 return 0;
556 }
557
558 static int __maybe_unused tiadc_resume(struct device *dev)
559 {
560 struct iio_dev *indio_dev = dev_get_drvdata(dev);
561 struct tiadc_device *adc_dev = iio_priv(indio_dev);
562 unsigned int restore;
563
564 /* Make sure ADC is powered up */
565 restore = tiadc_readl(adc_dev, REG_CTRL);
566 restore &= ~(CNTRLREG_POWERDOWN);
567 tiadc_writel(adc_dev, REG_CTRL, restore);
568
569 tiadc_step_config(indio_dev);
570 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
571 adc_dev->buffer_en_ch_steps);
572 return 0;
573 }
574
575 static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
576
577 static const struct of_device_id ti_adc_dt_ids[] = {
578 { .compatible = "ti,am3359-adc", },
579 { }
580 };
581 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
582
583 static struct platform_driver tiadc_driver = {
584 .driver = {
585 .name = "TI-am335x-adc",
586 .pm = &tiadc_pm_ops,
587 .of_match_table = ti_adc_dt_ids,
588 },
589 .probe = tiadc_probe,
590 .remove = tiadc_remove,
591 };
592 module_platform_driver(tiadc_driver);
593
594 MODULE_DESCRIPTION("TI ADC controller driver");
595 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
596 MODULE_LICENSE("GPL");
This page took 0.074592 seconds and 5 git commands to generate.