iio: sw-device: Fix config group initialization
[linux-2.6-block.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
5e53a69b
PR
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>
6f39ac4e
PR
24#include <linux/of.h>
25#include <linux/of_device.h>
c80df483
PA
26#include <linux/iio/machine.h>
27#include <linux/iio/driver.h>
5e53a69b
PR
28
29#include <linux/mfd/ti_am335x_tscadc.h>
ca9a5638
ZL
30#include <linux/iio/buffer.h>
31#include <linux/iio/kfifo_buf.h>
5e53a69b 32
f438b9da
M
33#include <linux/dmaengine.h>
34#include <linux/dma-mapping.h>
35
36#define DMA_BUFFER_SIZE SZ_2K
37
38struct tiadc_dma {
39 struct dma_slave_config conf;
40 struct dma_chan *chan;
41 dma_addr_t addr;
42 dma_cookie_t cookie;
43 u8 *buf;
44 int current_period;
45 int period_size;
46 u8 fifo_thresh;
47};
48
5e53a69b
PR
49struct tiadc_device {
50 struct ti_tscadc_dev *mfd_tscadc;
f438b9da 51 struct tiadc_dma dma;
90c43ec6 52 struct mutex fifo1_lock; /* to protect fifo access */
5e53a69b 53 int channels;
f438b9da 54 int total_ch_enabled;
18926ede
SAS
55 u8 channel_line[8];
56 u8 channel_step[8];
ca9a5638 57 int buffer_en_ch_steps;
ca9a5638 58 u16 data[8];
5dc11e81 59 u32 open_delay[8], sample_delay[8], step_avg[8];
5e53a69b
PR
60};
61
62static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
63{
64 return readl(adc->mfd_tscadc->tscadc_base + reg);
65}
66
67static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
68 unsigned int val)
69{
70 writel(val, adc->mfd_tscadc->tscadc_base + reg);
71}
72
abeccee4
PR
73static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
74{
75 u32 step_en;
76
77 step_en = ((1 << adc_dev->channels) - 1);
78 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
79 return step_en;
80}
81
7ca6740c
SAS
82static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
83 struct iio_chan_spec const *chan)
84{
85 int i;
86
87 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
88 if (chan->channel == adc_dev->channel_line[i]) {
89 u32 step;
90
91 step = adc_dev->channel_step[i];
92 /* +1 for the charger */
93 return 1 << (step + 1);
94 }
95 }
96 WARN_ON(1);
97 return 0;
98}
99
ca9a5638 100static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
5e53a69b 101{
ca9a5638
ZL
102 return 1 << adc_dev->channel_step[chan];
103}
104
105static void tiadc_step_config(struct iio_dev *indio_dev)
106{
107 struct tiadc_device *adc_dev = iio_priv(indio_dev);
5dc11e81 108 struct device *dev = adc_dev->mfd_tscadc->dev;
5e53a69b 109 unsigned int stepconfig;
3a59684c 110 int i, steps = 0;
5e53a69b
PR
111
112 /*
113 * There are 16 configurable steps and 8 analog input
114 * lines available which are shared between Touchscreen and ADC.
115 *
3a59684c 116 * Steps forwards i.e. from 0 towards 16 are used by ADC
5e53a69b
PR
117 * depending on number of input lines needed.
118 * Channel would represent which analog input
119 * needs to be given to ADC to digitalize data.
120 */
121
5e53a69b 122
18926ede
SAS
123 for (i = 0; i < adc_dev->channels; i++) {
124 int chan;
125
126 chan = adc_dev->channel_line[i];
5dc11e81
V
127
128 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) {
129 dev_warn(dev, "chan %d step_avg truncating to %d\n",
130 chan, STEPCONFIG_AVG_16);
131 adc_dev->step_avg[i] = STEPCONFIG_AVG_16;
132 }
133
134 if (adc_dev->step_avg[i])
135 stepconfig =
136 STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) |
137 STEPCONFIG_FIFO1;
138 else
139 stepconfig = STEPCONFIG_FIFO1;
140
141 if (iio_buffer_enabled(indio_dev))
142 stepconfig |= STEPCONFIG_MODE_SWCNT;
143
18926ede
SAS
144 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
145 stepconfig | STEPCONFIG_INP(chan));
5dc11e81
V
146
147 if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) {
148 dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n",
149 chan);
150 adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK;
151 }
152
153 if (adc_dev->sample_delay[i] > 0xFF) {
154 dev_warn(dev, "chan %d sample delay truncating to 0xFF\n",
155 chan);
156 adc_dev->sample_delay[i] = 0xFF;
157 }
158
18926ede 159 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
5dc11e81
V
160 STEPDELAY_OPEN(adc_dev->open_delay[i]) |
161 STEPDELAY_SAMPLE(adc_dev->sample_delay[i]));
162
18926ede
SAS
163 adc_dev->channel_step[i] = steps;
164 steps++;
5e53a69b 165 }
ca9a5638
ZL
166}
167
168static irqreturn_t tiadc_irq_h(int irq, void *private)
169{
170 struct iio_dev *indio_dev = private;
171 struct tiadc_device *adc_dev = iio_priv(indio_dev);
172 unsigned int status, config;
173 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
174
175 /*
176 * ADC and touchscreen share the IRQ line.
177 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
178 */
179 if (status & IRQENB_FIFO1OVRRUN) {
180 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
181 config = tiadc_readl(adc_dev, REG_CTRL);
182 config &= ~(CNTRLREG_TSCSSENB);
183 tiadc_writel(adc_dev, REG_CTRL, config);
184 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
185 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
186 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
187 return IRQ_HANDLED;
188 } else if (status & IRQENB_FIFO1THRES) {
189 /* Disable irq and wake worker thread */
190 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
191 return IRQ_WAKE_THREAD;
192 }
193
194 return IRQ_NONE;
195}
196
197static irqreturn_t tiadc_worker_h(int irq, void *private)
198{
199 struct iio_dev *indio_dev = private;
200 struct tiadc_device *adc_dev = iio_priv(indio_dev);
201 int i, k, fifo1count, read;
202 u16 *data = adc_dev->data;
203
204 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
205 for (k = 0; k < fifo1count; k = k + i) {
206 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
207 read = tiadc_readl(adc_dev, REG_FIFO1);
208 data[i] = read & FIFOREAD_DATA_MASK;
209 }
210 iio_push_to_buffers(indio_dev, (u8 *) data);
211 }
212
213 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
214 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
215
216 return IRQ_HANDLED;
217}
218
f438b9da
M
219static void tiadc_dma_rx_complete(void *param)
220{
221 struct iio_dev *indio_dev = param;
222 struct tiadc_device *adc_dev = iio_priv(indio_dev);
223 struct tiadc_dma *dma = &adc_dev->dma;
224 u8 *data;
225 int i;
226
227 data = dma->buf + dma->current_period * dma->period_size;
228 dma->current_period = 1 - dma->current_period; /* swap the buffer ID */
229
230 for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) {
231 iio_push_to_buffers(indio_dev, data);
232 data += indio_dev->scan_bytes;
233 }
234}
235
236static int tiadc_start_dma(struct iio_dev *indio_dev)
237{
238 struct tiadc_device *adc_dev = iio_priv(indio_dev);
239 struct tiadc_dma *dma = &adc_dev->dma;
240 struct dma_async_tx_descriptor *desc;
241
242 dma->current_period = 0; /* We start to fill period 0 */
243 /*
244 * Make the fifo thresh as the multiple of total number of
245 * channels enabled, so make sure that cyclic DMA period
246 * length is also a multiple of total number of channels
247 * enabled. This ensures that no invalid data is reported
248 * to the stack via iio_push_to_buffers().
249 */
250 dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1,
251 adc_dev->total_ch_enabled) - 1;
252 /* Make sure that period length is multiple of fifo thresh level */
253 dma->period_size = rounddown(DMA_BUFFER_SIZE / 2,
254 (dma->fifo_thresh + 1) * sizeof(u16));
255
256 dma->conf.src_maxburst = dma->fifo_thresh + 1;
257 dmaengine_slave_config(dma->chan, &dma->conf);
258
259 desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr,
260 dma->period_size * 2,
261 dma->period_size, DMA_DEV_TO_MEM,
262 DMA_PREP_INTERRUPT);
263 if (!desc)
264 return -EBUSY;
265
266 desc->callback = tiadc_dma_rx_complete;
267 desc->callback_param = indio_dev;
268
269 dma->cookie = dmaengine_submit(desc);
270
271 dma_async_issue_pending(dma->chan);
272
273 tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh);
274 tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh);
275 tiadc_writel(adc_dev, REG_DMAENABLE_SET, DMA_FIFO1);
276
277 return 0;
278}
279
ca9a5638
ZL
280static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
281{
282 struct tiadc_device *adc_dev = iio_priv(indio_dev);
283 int i, fifo1count, read;
284
285 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
286 IRQENB_FIFO1OVRRUN |
287 IRQENB_FIFO1UNDRFLW));
288
289 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
290 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
291 for (i = 0; i < fifo1count; i++)
292 read = tiadc_readl(adc_dev, REG_FIFO1);
293
24adaf79 294 return 0;
ca9a5638
ZL
295}
296
297static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
298{
299 struct tiadc_device *adc_dev = iio_priv(indio_dev);
f438b9da
M
300 struct tiadc_dma *dma = &adc_dev->dma;
301 unsigned int irq_enable;
ca9a5638
ZL
302 unsigned int enb = 0;
303 u8 bit;
304
305 tiadc_step_config(indio_dev);
f438b9da 306 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) {
ca9a5638 307 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
f438b9da
M
308 adc_dev->total_ch_enabled++;
309 }
ca9a5638
ZL
310 adc_dev->buffer_en_ch_steps = enb;
311
f438b9da
M
312 if (dma->chan)
313 tiadc_start_dma(indio_dev);
314
7e170c6e 315 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
ca9a5638
ZL
316
317 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
318 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
f438b9da
M
319
320 irq_enable = IRQENB_FIFO1OVRRUN;
321 if (!dma->chan)
322 irq_enable |= IRQENB_FIFO1THRES;
323 tiadc_writel(adc_dev, REG_IRQENABLE, irq_enable);
ca9a5638
ZL
324
325 return 0;
326}
327
328static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
329{
330 struct tiadc_device *adc_dev = iio_priv(indio_dev);
f438b9da 331 struct tiadc_dma *dma = &adc_dev->dma;
ca9a5638
ZL
332 int fifo1count, i, read;
333
334 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
335 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
336 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
3954b7bf 337 adc_dev->buffer_en_ch_steps = 0;
f438b9da
M
338 adc_dev->total_ch_enabled = 0;
339 if (dma->chan) {
340 tiadc_writel(adc_dev, REG_DMAENABLE_CLEAR, 0x2);
341 dmaengine_terminate_async(dma->chan);
342 }
b1451e54 343
ca9a5638
ZL
344 /* Flush FIFO of leftover data in the time it takes to disable adc */
345 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
346 for (i = 0; i < fifo1count; i++)
347 read = tiadc_readl(adc_dev, REG_FIFO1);
348
349 return 0;
5e53a69b
PR
350}
351
ca9a5638
ZL
352static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
353{
354 tiadc_step_config(indio_dev);
355
356 return 0;
357}
358
359static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
360 .preenable = &tiadc_buffer_preenable,
361 .postenable = &tiadc_buffer_postenable,
362 .predisable = &tiadc_buffer_predisable,
363 .postdisable = &tiadc_buffer_postdisable,
364};
365
98c08cf4 366static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
ca9a5638
ZL
367 irqreturn_t (*pollfunc_bh)(int irq, void *p),
368 irqreturn_t (*pollfunc_th)(int irq, void *p),
369 int irq,
370 unsigned long flags,
371 const struct iio_buffer_setup_ops *setup_ops)
372{
fe26980e 373 struct iio_buffer *buffer;
ca9a5638
ZL
374 int ret;
375
7ab374a0 376 buffer = iio_kfifo_allocate();
fe26980e 377 if (!buffer)
ca9a5638
ZL
378 return -ENOMEM;
379
fe26980e
LPC
380 iio_device_attach_buffer(indio_dev, buffer);
381
ca9a5638
ZL
382 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
383 flags, indio_dev->name, indio_dev);
384 if (ret)
385 goto error_kfifo_free;
386
387 indio_dev->setup_ops = setup_ops;
9d0be85d 388 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
ca9a5638 389
ca9a5638
ZL
390 return 0;
391
ca9a5638
ZL
392error_kfifo_free:
393 iio_kfifo_free(indio_dev->buffer);
394 return ret;
395}
396
397static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
398{
399 struct tiadc_device *adc_dev = iio_priv(indio_dev);
400
401 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
402 iio_kfifo_free(indio_dev->buffer);
ca9a5638
ZL
403}
404
405
c80df483
PA
406static const char * const chan_name_ain[] = {
407 "AIN0",
408 "AIN1",
409 "AIN2",
410 "AIN3",
411 "AIN4",
412 "AIN5",
413 "AIN6",
414 "AIN7",
415};
416
5e53a69b
PR
417static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
418{
c80df483 419 struct tiadc_device *adc_dev = iio_priv(indio_dev);
5e53a69b 420 struct iio_chan_spec *chan_array;
c80df483 421 struct iio_chan_spec *chan;
5e53a69b
PR
422 int i;
423
424 indio_dev->num_channels = channels;
fea89e2d 425 chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
5e53a69b
PR
426 if (chan_array == NULL)
427 return -ENOMEM;
428
c80df483
PA
429 chan = chan_array;
430 for (i = 0; i < channels; i++, chan++) {
431
5e53a69b
PR
432 chan->type = IIO_VOLTAGE;
433 chan->indexed = 1;
18926ede 434 chan->channel = adc_dev->channel_line[i];
6c572522 435 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
18926ede 436 chan->datasheet_name = chan_name_ain[chan->channel];
ca9a5638 437 chan->scan_index = i;
c80df483
PA
438 chan->scan_type.sign = 'u';
439 chan->scan_type.realbits = 12;
0f6fc7d5 440 chan->scan_type.storagebits = 16;
5e53a69b
PR
441 }
442
443 indio_dev->channels = chan_array;
444
c80df483 445 return 0;
5e53a69b
PR
446}
447
448static void tiadc_channels_remove(struct iio_dev *indio_dev)
449{
450 kfree(indio_dev->channels);
451}
452
453static int tiadc_read_raw(struct iio_dev *indio_dev,
454 struct iio_chan_spec const *chan,
455 int *val, int *val2, long mask)
456{
457 struct tiadc_device *adc_dev = iio_priv(indio_dev);
90c43ec6 458 int ret = IIO_VAL_INT;
b1451e54
PR
459 int i, map_val;
460 unsigned int fifo1count, read, stepid;
1460c152 461 bool found = false;
b1451e54 462 u32 step_en;
7ca6740c 463 unsigned long timeout;
ca9a5638
ZL
464
465 if (iio_buffer_enabled(indio_dev))
466 return -EBUSY;
467
7ca6740c
SAS
468 step_en = get_adc_chan_step_mask(adc_dev, chan);
469 if (!step_en)
470 return -EINVAL;
471
90c43ec6 472 mutex_lock(&adc_dev->fifo1_lock);
7ca6740c
SAS
473 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
474 while (fifo1count--)
475 tiadc_readl(adc_dev, REG_FIFO1);
476
7e170c6e 477 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
b1451e54 478
7175cce1 479 timeout = jiffies + msecs_to_jiffies
7ca6740c
SAS
480 (IDLE_TIMEOUT * adc_dev->channels);
481 /* Wait for Fifo threshold interrupt */
482 while (1) {
483 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
484 if (fifo1count)
485 break;
486
487 if (time_after(jiffies, timeout)) {
488 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
90c43ec6
V
489 ret = -EAGAIN;
490 goto err_unlock;
b1451e54 491 }
fb7f8ce3 492 }
baa3c652 493 map_val = adc_dev->channel_step[chan->scan_index];
5e53a69b
PR
494
495 /*
7ca6740c
SAS
496 * We check the complete FIFO. We programmed just one entry but in case
497 * something went wrong we left empty handed (-EAGAIN previously) and
498 * then the value apeared somehow in the FIFO we would have two entries.
499 * Therefore we read every item and keep only the latest version of the
500 * requested channel.
5e53a69b 501 */
5e53a69b 502 for (i = 0; i < fifo1count; i++) {
18926ede 503 read = tiadc_readl(adc_dev, REG_FIFO1);
b1451e54
PR
504 stepid = read & FIFOREAD_CHNLID_MASK;
505 stepid = stepid >> 0x10;
506
507 if (stepid == map_val) {
508 read = read & FIFOREAD_DATA_MASK;
1460c152 509 found = true;
0f6fc7d5 510 *val = (u16) read;
1460c152 511 }
5e53a69b 512 }
7ca6740c 513 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
b1451e54 514
1460c152 515 if (found == false)
90c43ec6
V
516 ret = -EBUSY;
517
518err_unlock:
519 mutex_unlock(&adc_dev->fifo1_lock);
520 return ret;
5e53a69b
PR
521}
522
523static const struct iio_info tiadc_info = {
524 .read_raw = &tiadc_read_raw,
bc93aa76 525 .driver_module = THIS_MODULE,
5e53a69b
PR
526};
527
f438b9da
M
528static int tiadc_request_dma(struct platform_device *pdev,
529 struct tiadc_device *adc_dev)
530{
531 struct tiadc_dma *dma = &adc_dev->dma;
532 dma_cap_mask_t mask;
533
534 /* Default slave configuration parameters */
535 dma->conf.direction = DMA_DEV_TO_MEM;
536 dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
537 dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1;
538
539 dma_cap_zero(mask);
540 dma_cap_set(DMA_CYCLIC, mask);
541
542 /* Get a channel for RX */
543 dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1");
544 if (IS_ERR(dma->chan)) {
545 int ret = PTR_ERR(dma->chan);
546
547 dma->chan = NULL;
548 return ret;
549 }
550
551 /* RX buffer */
552 dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
553 &dma->addr, GFP_KERNEL);
554 if (!dma->buf)
555 goto err;
556
557 return 0;
558err:
559 dma_release_channel(dma->chan);
560 return -ENOMEM;
561}
562
dee1f550
V
563static int tiadc_parse_dt(struct platform_device *pdev,
564 struct tiadc_device *adc_dev)
565{
566 struct device_node *node = pdev->dev.of_node;
567 struct property *prop;
568 const __be32 *cur;
569 int channels = 0;
570 u32 val;
571
572 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
573 adc_dev->channel_line[channels] = val;
5dc11e81
V
574
575 /* Set Default values for optional DT parameters */
576 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
577 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
578 adc_dev->step_avg[channels] = 16;
579
dee1f550
V
580 channels++;
581 }
582
5dc11e81
V
583 of_property_read_u32_array(node, "ti,chan-step-avg",
584 adc_dev->step_avg, channels);
585 of_property_read_u32_array(node, "ti,chan-step-opendelay",
586 adc_dev->open_delay, channels);
587 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
588 adc_dev->sample_delay, channels);
589
dee1f550
V
590 adc_dev->channels = channels;
591 return 0;
592}
593
fc52692c 594static int tiadc_probe(struct platform_device *pdev)
5e53a69b
PR
595{
596 struct iio_dev *indio_dev;
597 struct tiadc_device *adc_dev;
6f39ac4e 598 struct device_node *node = pdev->dev.of_node;
5e53a69b
PR
599 int err;
600
0ead4fb2
SAS
601 if (!node) {
602 dev_err(&pdev->dev, "Could not find valid DT data.\n");
5e53a69b
PR
603 return -EINVAL;
604 }
605
fea89e2d 606 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev));
5e53a69b
PR
607 if (indio_dev == NULL) {
608 dev_err(&pdev->dev, "failed to allocate iio device\n");
a0648130 609 return -ENOMEM;
5e53a69b
PR
610 }
611 adc_dev = iio_priv(indio_dev);
612
6f39ac4e 613 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
dee1f550 614 tiadc_parse_dt(pdev, adc_dev);
5e53a69b
PR
615
616 indio_dev->dev.parent = &pdev->dev;
617 indio_dev->name = dev_name(&pdev->dev);
618 indio_dev->modes = INDIO_DIRECT_MODE;
619 indio_dev->info = &tiadc_info;
620
ca9a5638
ZL
621 tiadc_step_config(indio_dev);
622 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
90c43ec6 623 mutex_init(&adc_dev->fifo1_lock);
5e53a69b
PR
624
625 err = tiadc_channel_init(indio_dev, adc_dev->channels);
626 if (err < 0)
a0648130 627 return err;
5e53a69b 628
ca9a5638
ZL
629 err = tiadc_iio_buffered_hardware_setup(indio_dev,
630 &tiadc_worker_h,
631 &tiadc_irq_h,
632 adc_dev->mfd_tscadc->irq,
633 IRQF_SHARED,
634 &tiadc_buffer_setup_ops);
635
5e53a69b
PR
636 if (err)
637 goto err_free_channels;
638
ca9a5638
ZL
639 err = iio_device_register(indio_dev);
640 if (err)
641 goto err_buffer_unregister;
642
5e53a69b
PR
643 platform_set_drvdata(pdev, indio_dev);
644
f438b9da
M
645 err = tiadc_request_dma(pdev, adc_dev);
646 if (err && err == -EPROBE_DEFER)
647 goto err_dma;
648
5e53a69b
PR
649 return 0;
650
f438b9da
M
651err_dma:
652 iio_device_unregister(indio_dev);
ca9a5638
ZL
653err_buffer_unregister:
654 tiadc_iio_buffered_hardware_remove(indio_dev);
5e53a69b
PR
655err_free_channels:
656 tiadc_channels_remove(indio_dev);
5e53a69b
PR
657 return err;
658}
659
fc52692c 660static int tiadc_remove(struct platform_device *pdev)
5e53a69b
PR
661{
662 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
abeccee4 663 struct tiadc_device *adc_dev = iio_priv(indio_dev);
f438b9da 664 struct tiadc_dma *dma = &adc_dev->dma;
abeccee4 665 u32 step_en;
5e53a69b 666
f438b9da
M
667 if (dma->chan) {
668 dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
669 dma->buf, dma->addr);
670 dma_release_channel(dma->chan);
671 }
5e53a69b 672 iio_device_unregister(indio_dev);
ca9a5638 673 tiadc_iio_buffered_hardware_remove(indio_dev);
5e53a69b
PR
674 tiadc_channels_remove(indio_dev);
675
abeccee4
PR
676 step_en = get_adc_step_mask(adc_dev);
677 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
678
5e53a69b
PR
679 return 0;
680}
681
27aa832d 682static int __maybe_unused tiadc_suspend(struct device *dev)
5e53a69b
PR
683{
684 struct iio_dev *indio_dev = dev_get_drvdata(dev);
685 struct tiadc_device *adc_dev = iio_priv(indio_dev);
a9bce1b0 686 struct ti_tscadc_dev *tscadc_dev;
5e53a69b
PR
687 unsigned int idle;
688
a9bce1b0 689 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
5e53a69b
PR
690 if (!device_may_wakeup(tscadc_dev->dev)) {
691 idle = tiadc_readl(adc_dev, REG_CTRL);
692 idle &= ~(CNTRLREG_TSCSSENB);
693 tiadc_writel(adc_dev, REG_CTRL, (idle |
694 CNTRLREG_POWERDOWN));
695 }
696
697 return 0;
698}
699
27aa832d 700static int __maybe_unused tiadc_resume(struct device *dev)
5e53a69b
PR
701{
702 struct iio_dev *indio_dev = dev_get_drvdata(dev);
703 struct tiadc_device *adc_dev = iio_priv(indio_dev);
704 unsigned int restore;
705
706 /* Make sure ADC is powered up */
707 restore = tiadc_readl(adc_dev, REG_CTRL);
708 restore &= ~(CNTRLREG_POWERDOWN);
709 tiadc_writel(adc_dev, REG_CTRL, restore);
710
ca9a5638 711 tiadc_step_config(indio_dev);
7ca6740c
SAS
712 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
713 adc_dev->buffer_en_ch_steps);
5e53a69b
PR
714 return 0;
715}
716
27aa832d 717static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
5e53a69b 718
6f39ac4e
PR
719static const struct of_device_id ti_adc_dt_ids[] = {
720 { .compatible = "ti,am3359-adc", },
721 { }
722};
723MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
724
5e53a69b
PR
725static struct platform_driver tiadc_driver = {
726 .driver = {
9f99928f 727 .name = "TI-am335x-adc",
27aa832d 728 .pm = &tiadc_pm_ops,
de06b344 729 .of_match_table = ti_adc_dt_ids,
5e53a69b
PR
730 },
731 .probe = tiadc_probe,
fc52692c 732 .remove = tiadc_remove,
5e53a69b 733};
5e53a69b
PR
734module_platform_driver(tiadc_driver);
735
736MODULE_DESCRIPTION("TI ADC controller driver");
737MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
738MODULE_LICENSE("GPL");