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