blob: bed9977a1863bfd7353778235a20529e297fd71d [file] [log] [blame]
Patil, Rachna5e53a692012-10-16 12:55:45 +05301/*
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
Patil, Rachna5e53a692012-10-16 12:55:45 +053016#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>
Patil, Rachna6f39ac42013-01-24 03:45:11 +000024#include <linux/of.h>
25#include <linux/of_device.h>
Pantelis Antoniouc80df482012-10-13 16:37:24 +030026#include <linux/iio/machine.h>
27#include <linux/iio/driver.h>
Patil, Rachna5e53a692012-10-16 12:55:45 +053028
29#include <linux/mfd/ti_am335x_tscadc.h>
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010030#include <linux/iio/buffer.h>
31#include <linux/iio/kfifo_buf.h>
Patil, Rachna5e53a692012-10-16 12:55:45 +053032
33struct tiadc_device {
34 struct ti_tscadc_dev *mfd_tscadc;
Vignesh R90c43ec2016-08-17 17:43:00 +053035 struct mutex fifo1_lock; /* to protect fifo access */
Patil, Rachna5e53a692012-10-16 12:55:45 +053036 int channels;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020037 u8 channel_line[8];
38 u8 channel_step[8];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010039 int buffer_en_ch_steps;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010040 u16 data[8];
Vignesh R5dc11e82015-03-31 16:42:37 +053041 u32 open_delay[8], sample_delay[8], step_avg[8];
Patil, Rachna5e53a692012-10-16 12:55:45 +053042};
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
Patil, Rachnaabeccee2013-01-24 03:45:05 +000055static 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
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +010064static 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
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010082static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
Patil, Rachna5e53a692012-10-16 12:55:45 +053083{
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010084 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);
Vignesh R5dc11e82015-03-31 16:42:37 +053090 struct device *dev = adc_dev->mfd_tscadc->dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +053091 unsigned int stepconfig;
Brad Griffis3a596842015-02-03 11:41:58 -080092 int i, steps = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +053093
94 /*
95 * There are 16 configurable steps and 8 analog input
96 * lines available which are shared between Touchscreen and ADC.
97 *
Brad Griffis3a596842015-02-03 11:41:58 -080098 * Steps forwards i.e. from 0 towards 16 are used by ADC
Patil, Rachna5e53a692012-10-16 12:55:45 +053099 * 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
Patil, Rachna5e53a692012-10-16 12:55:45 +0530104
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200105 for (i = 0; i < adc_dev->channels; i++) {
106 int chan;
107
108 chan = adc_dev->channel_line[i];
Vignesh R5dc11e82015-03-31 16:42:37 +0530109
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
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200126 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
127 stepconfig | STEPCONFIG_INP(chan));
Vignesh R5dc11e82015-03-31 16:42:37 +0530128
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
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200141 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Vignesh R5dc11e82015-03-31 16:42:37 +0530142 STEPDELAY_OPEN(adc_dev->open_delay[i]) |
143 STEPDELAY_SAMPLE(adc_dev->sample_delay[i]));
144
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200145 adc_dev->channel_step[i] = steps;
146 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530147 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530148}
149
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100150static 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
179static 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
201static 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
Lars-Peter Clausen24adaf72013-10-14 17:49:00 +0100215 return 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100216}
217
218static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
219{
220 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100221 unsigned int enb = 0;
222 u8 bit;
223
224 tiadc_step_config(indio_dev);
Octavian Purdila70dddee2015-03-02 21:03:05 +0200225 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100226 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
227 adc_dev->buffer_en_ch_steps = enb;
228
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100229 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100230
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
239static 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);
Sebastian Andrzej Siewior3954b7b2013-12-19 16:28:30 +0100247 adc_dev->buffer_en_ch_steps = 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100248
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
257static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
258{
259 tiadc_step_config(indio_dev);
260
261 return 0;
262}
263
264static 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
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100271static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100272 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{
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100278 struct iio_buffer *buffer;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100279 int ret;
280
Karol Wrona7ab374a2014-12-19 18:39:24 +0100281 buffer = iio_kfifo_allocate();
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100282 if (!buffer)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100283 return -ENOMEM;
284
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100285 iio_device_attach_buffer(indio_dev, buffer);
286
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100287 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;
Jonathan Cameron9d0be852016-01-01 18:05:34 +0000293 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100294
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100295 return 0;
296
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100297error_kfifo_free:
298 iio_kfifo_free(indio_dev->buffer);
299 return ret;
300}
301
302static 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);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100308}
309
310
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300311static const char * const chan_name_ain[] = {
312 "AIN0",
313 "AIN1",
314 "AIN2",
315 "AIN3",
316 "AIN4",
317 "AIN5",
318 "AIN6",
319 "AIN7",
320};
321
Patil, Rachna5e53a692012-10-16 12:55:45 +0530322static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
323{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300324 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530325 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300326 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530327 int i;
328
329 indio_dev->num_channels = channels;
Andrew F. Davisfea89e22016-05-31 12:00:12 -0500330 chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530331 if (chan_array == NULL)
332 return -ENOMEM;
333
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300334 chan = chan_array;
335 for (i = 0; i < channels; i++, chan++) {
336
Patil, Rachna5e53a692012-10-16 12:55:45 +0530337 chan->type = IIO_VOLTAGE;
338 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200339 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000340 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200341 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100342 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300343 chan->scan_type.sign = 'u';
344 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100345 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530346 }
347
348 indio_dev->channels = chan_array;
349
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300350 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530351}
352
353static void tiadc_channels_remove(struct iio_dev *indio_dev)
354{
355 kfree(indio_dev->channels);
356}
357
358static 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);
Vignesh R90c43ec2016-08-17 17:43:00 +0530363 int ret = IIO_VAL_INT;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100364 int i, map_val;
365 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200366 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100367 u32 step_en;
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100368 unsigned long timeout;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100369
370 if (iio_buffer_enabled(indio_dev))
371 return -EBUSY;
372
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100373 step_en = get_adc_chan_step_mask(adc_dev, chan);
374 if (!step_en)
375 return -EINVAL;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100376
Vignesh R90c43ec2016-08-17 17:43:00 +0530377 mutex_lock(&adc_dev->fifo1_lock);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100378 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
379 while (fifo1count--)
380 tiadc_readl(adc_dev, REG_FIFO1);
381
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100382 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100383
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100384 timeout = jiffies + usecs_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);
Vignesh R90c43ec2016-08-17 17:43:00 +0530394 ret = -EAGAIN;
395 goto err_unlock;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100396 }
Sebastian Andrzej Siewiorfb7f8ce2013-12-19 16:28:27 +0100397 }
Jan Kardellbaa3c652014-11-06 22:18:00 +0000398 map_val = adc_dev->channel_step[chan->scan_index];
Patil, Rachna5e53a692012-10-16 12:55:45 +0530399
400 /*
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100401 * 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.
Patil, Rachna5e53a692012-10-16 12:55:45 +0530406 */
Patil, Rachna5e53a692012-10-16 12:55:45 +0530407 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200408 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100409 stepid = read & FIFOREAD_CHNLID_MASK;
410 stepid = stepid >> 0x10;
411
412 if (stepid == map_val) {
413 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200414 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100415 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200416 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530417 }
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100418 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100419
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200420 if (found == false)
Vignesh R90c43ec2016-08-17 17:43:00 +0530421 ret = -EBUSY;
422
423err_unlock:
424 mutex_unlock(&adc_dev->fifo1_lock);
425 return ret;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530426}
427
428static const struct iio_info tiadc_info = {
429 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100430 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530431};
432
Vignesh Rdee1f552015-03-31 16:42:36 +0530433static 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;
Vignesh R5dc11e82015-03-31 16:42:37 +0530444
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
Vignesh Rdee1f552015-03-31 16:42:36 +0530450 channels++;
451 }
452
Vignesh R5dc11e82015-03-31 16:42:37 +0530453 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
Vignesh Rdee1f552015-03-31 16:42:36 +0530460 adc_dev->channels = channels;
461 return 0;
462}
463
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800464static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530465{
466 struct iio_dev *indio_dev;
467 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000468 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530469 int err;
470
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200471 if (!node) {
472 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530473 return -EINVAL;
474 }
475
Andrew F. Davisfea89e22016-05-31 12:00:12 -0500476 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530477 if (indio_dev == NULL) {
478 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100479 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530480 }
481 adc_dev = iio_priv(indio_dev);
482
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000483 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
Vignesh Rdee1f552015-03-31 16:42:36 +0530484 tiadc_parse_dt(pdev, adc_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530485
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
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100491 tiadc_step_config(indio_dev);
492 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Vignesh R90c43ec2016-08-17 17:43:00 +0530493 mutex_init(&adc_dev->fifo1_lock);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530494
495 err = tiadc_channel_init(indio_dev, adc_dev->channels);
496 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100497 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530498
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100499 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
Patil, Rachna5e53a692012-10-16 12:55:45 +0530506 if (err)
507 goto err_free_channels;
508
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100509 err = iio_device_register(indio_dev);
510 if (err)
511 goto err_buffer_unregister;
512
Patil, Rachna5e53a692012-10-16 12:55:45 +0530513 platform_set_drvdata(pdev, indio_dev);
514
515 return 0;
516
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100517err_buffer_unregister:
518 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530519err_free_channels:
520 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530521 return err;
522}
523
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800524static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530525{
526 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000527 struct tiadc_device *adc_dev = iio_priv(indio_dev);
528 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530529
530 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100531 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530532 tiadc_channels_remove(indio_dev);
533
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000534 step_en = get_adc_step_mask(adc_dev);
535 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
536
Patil, Rachna5e53a692012-10-16 12:55:45 +0530537 return 0;
538}
539
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500540static int __maybe_unused tiadc_suspend(struct device *dev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530541{
542 struct iio_dev *indio_dev = dev_get_drvdata(dev);
543 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200544 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530545 unsigned int idle;
546
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200547 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530548 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
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500558static int __maybe_unused tiadc_resume(struct device *dev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530559{
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
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100569 tiadc_step_config(indio_dev);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100570 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
571 adc_dev->buffer_en_ch_steps);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530572 return 0;
573}
574
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500575static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530576
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000577static const struct of_device_id ti_adc_dt_ids[] = {
578 { .compatible = "ti,am3359-adc", },
579 { }
580};
581MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
582
Patil, Rachna5e53a692012-10-16 12:55:45 +0530583static struct platform_driver tiadc_driver = {
584 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200585 .name = "TI-am335x-adc",
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500586 .pm = &tiadc_pm_ops,
Sachin Kamatde06b342013-10-21 10:27:00 +0100587 .of_match_table = ti_adc_dt_ids,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530588 },
589 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800590 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530591};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530592module_platform_driver(tiadc_driver);
593
594MODULE_DESCRIPTION("TI ADC controller driver");
595MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
596MODULE_LICENSE("GPL");