blob: 8a368756881b82431404f86766d70b4b8acdd236 [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;
35 int channels;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020036 u8 channel_line[8];
37 u8 channel_step[8];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010038 int buffer_en_ch_steps;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010039 u16 data[8];
Vignesh R5dc11e82015-03-31 16:42:37 +053040 u32 open_delay[8], sample_delay[8], step_avg[8];
Patil, Rachna5e53a692012-10-16 12:55:45 +053041};
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
Patil, Rachnaabeccee2013-01-24 03:45:05 +000054static 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
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +010063static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
64 struct iio_chan_spec const *chan)
65{
66 int i;
67
68 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
69 if (chan->channel == adc_dev->channel_line[i]) {
70 u32 step;
71
72 step = adc_dev->channel_step[i];
73 /* +1 for the charger */
74 return 1 << (step + 1);
75 }
76 }
77 WARN_ON(1);
78 return 0;
79}
80
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010081static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
Patil, Rachna5e53a692012-10-16 12:55:45 +053082{
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010083 return 1 << adc_dev->channel_step[chan];
84}
85
86static void tiadc_step_config(struct iio_dev *indio_dev)
87{
88 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Vignesh R5dc11e82015-03-31 16:42:37 +053089 struct device *dev = adc_dev->mfd_tscadc->dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +053090 unsigned int stepconfig;
Brad Griffis3a596842015-02-03 11:41:58 -080091 int i, steps = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +053092
93 /*
94 * There are 16 configurable steps and 8 analog input
95 * lines available which are shared between Touchscreen and ADC.
96 *
Brad Griffis3a596842015-02-03 11:41:58 -080097 * Steps forwards i.e. from 0 towards 16 are used by ADC
Patil, Rachna5e53a692012-10-16 12:55:45 +053098 * depending on number of input lines needed.
99 * Channel would represent which analog input
100 * needs to be given to ADC to digitalize data.
101 */
102
Patil, Rachna5e53a692012-10-16 12:55:45 +0530103
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200104 for (i = 0; i < adc_dev->channels; i++) {
105 int chan;
106
107 chan = adc_dev->channel_line[i];
Vignesh R5dc11e82015-03-31 16:42:37 +0530108
109 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) {
110 dev_warn(dev, "chan %d step_avg truncating to %d\n",
111 chan, STEPCONFIG_AVG_16);
112 adc_dev->step_avg[i] = STEPCONFIG_AVG_16;
113 }
114
115 if (adc_dev->step_avg[i])
116 stepconfig =
117 STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) |
118 STEPCONFIG_FIFO1;
119 else
120 stepconfig = STEPCONFIG_FIFO1;
121
122 if (iio_buffer_enabled(indio_dev))
123 stepconfig |= STEPCONFIG_MODE_SWCNT;
124
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200125 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
126 stepconfig | STEPCONFIG_INP(chan));
Vignesh R5dc11e82015-03-31 16:42:37 +0530127
128 if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) {
129 dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n",
130 chan);
131 adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK;
132 }
133
134 if (adc_dev->sample_delay[i] > 0xFF) {
135 dev_warn(dev, "chan %d sample delay truncating to 0xFF\n",
136 chan);
137 adc_dev->sample_delay[i] = 0xFF;
138 }
139
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200140 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Vignesh R5dc11e82015-03-31 16:42:37 +0530141 STEPDELAY_OPEN(adc_dev->open_delay[i]) |
142 STEPDELAY_SAMPLE(adc_dev->sample_delay[i]));
143
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200144 adc_dev->channel_step[i] = steps;
145 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530146 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530147}
148
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100149static irqreturn_t tiadc_irq_h(int irq, void *private)
150{
151 struct iio_dev *indio_dev = private;
152 struct tiadc_device *adc_dev = iio_priv(indio_dev);
153 unsigned int status, config;
154 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
155
156 /*
157 * ADC and touchscreen share the IRQ line.
158 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
159 */
160 if (status & IRQENB_FIFO1OVRRUN) {
161 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
162 config = tiadc_readl(adc_dev, REG_CTRL);
163 config &= ~(CNTRLREG_TSCSSENB);
164 tiadc_writel(adc_dev, REG_CTRL, config);
165 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
166 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
167 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
168 return IRQ_HANDLED;
169 } else if (status & IRQENB_FIFO1THRES) {
170 /* Disable irq and wake worker thread */
171 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
172 return IRQ_WAKE_THREAD;
173 }
174
175 return IRQ_NONE;
176}
177
178static irqreturn_t tiadc_worker_h(int irq, void *private)
179{
180 struct iio_dev *indio_dev = private;
181 struct tiadc_device *adc_dev = iio_priv(indio_dev);
182 int i, k, fifo1count, read;
183 u16 *data = adc_dev->data;
184
185 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
186 for (k = 0; k < fifo1count; k = k + i) {
187 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
188 read = tiadc_readl(adc_dev, REG_FIFO1);
189 data[i] = read & FIFOREAD_DATA_MASK;
190 }
191 iio_push_to_buffers(indio_dev, (u8 *) data);
192 }
193
194 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
195 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
196
197 return IRQ_HANDLED;
198}
199
200static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
201{
202 struct tiadc_device *adc_dev = iio_priv(indio_dev);
203 int i, fifo1count, read;
204
205 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
206 IRQENB_FIFO1OVRRUN |
207 IRQENB_FIFO1UNDRFLW));
208
209 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
210 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
211 for (i = 0; i < fifo1count; i++)
212 read = tiadc_readl(adc_dev, REG_FIFO1);
213
Lars-Peter Clausen24adaf72013-10-14 17:49:00 +0100214 return 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100215}
216
217static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
218{
219 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100220 unsigned int enb = 0;
221 u8 bit;
222
223 tiadc_step_config(indio_dev);
Octavian Purdila70dddee2015-03-02 21:03:05 +0200224 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100225 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
226 adc_dev->buffer_en_ch_steps = enb;
227
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100228 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100229
230 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
231 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
232 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
233 | IRQENB_FIFO1OVRRUN);
234
235 return 0;
236}
237
238static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
239{
240 struct tiadc_device *adc_dev = iio_priv(indio_dev);
241 int fifo1count, i, read;
242
243 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
244 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
245 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
Sebastian Andrzej Siewior3954b7b2013-12-19 16:28:30 +0100246 adc_dev->buffer_en_ch_steps = 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100247
248 /* Flush FIFO of leftover data in the time it takes to disable adc */
249 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
250 for (i = 0; i < fifo1count; i++)
251 read = tiadc_readl(adc_dev, REG_FIFO1);
252
253 return 0;
254}
255
256static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
257{
258 tiadc_step_config(indio_dev);
259
260 return 0;
261}
262
263static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
264 .preenable = &tiadc_buffer_preenable,
265 .postenable = &tiadc_buffer_postenable,
266 .predisable = &tiadc_buffer_predisable,
267 .postdisable = &tiadc_buffer_postdisable,
268};
269
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100270static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100271 irqreturn_t (*pollfunc_bh)(int irq, void *p),
272 irqreturn_t (*pollfunc_th)(int irq, void *p),
273 int irq,
274 unsigned long flags,
275 const struct iio_buffer_setup_ops *setup_ops)
276{
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100277 struct iio_buffer *buffer;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100278 int ret;
279
Karol Wrona7ab374a2014-12-19 18:39:24 +0100280 buffer = iio_kfifo_allocate();
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100281 if (!buffer)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100282 return -ENOMEM;
283
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100284 iio_device_attach_buffer(indio_dev, buffer);
285
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100286 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
287 flags, indio_dev->name, indio_dev);
288 if (ret)
289 goto error_kfifo_free;
290
291 indio_dev->setup_ops = setup_ops;
Jonathan Cameron9d0be852016-01-01 18:05:34 +0000292 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100293
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100294 return 0;
295
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100296error_kfifo_free:
297 iio_kfifo_free(indio_dev->buffer);
298 return ret;
299}
300
301static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
302{
303 struct tiadc_device *adc_dev = iio_priv(indio_dev);
304
305 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
306 iio_kfifo_free(indio_dev->buffer);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100307}
308
309
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300310static const char * const chan_name_ain[] = {
311 "AIN0",
312 "AIN1",
313 "AIN2",
314 "AIN3",
315 "AIN4",
316 "AIN5",
317 "AIN6",
318 "AIN7",
319};
320
Patil, Rachna5e53a692012-10-16 12:55:45 +0530321static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
322{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300323 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530324 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300325 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530326 int i;
327
328 indio_dev->num_channels = channels;
Andrew F. Davisfea89e22016-05-31 12:00:12 -0500329 chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530330 if (chan_array == NULL)
331 return -ENOMEM;
332
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300333 chan = chan_array;
334 for (i = 0; i < channels; i++, chan++) {
335
Patil, Rachna5e53a692012-10-16 12:55:45 +0530336 chan->type = IIO_VOLTAGE;
337 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200338 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000339 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200340 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100341 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300342 chan->scan_type.sign = 'u';
343 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100344 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530345 }
346
347 indio_dev->channels = chan_array;
348
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300349 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530350}
351
352static void tiadc_channels_remove(struct iio_dev *indio_dev)
353{
354 kfree(indio_dev->channels);
355}
356
357static int tiadc_read_raw(struct iio_dev *indio_dev,
358 struct iio_chan_spec const *chan,
359 int *val, int *val2, long mask)
360{
361 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100362 int i, map_val;
363 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200364 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100365 u32 step_en;
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100366 unsigned long timeout;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100367
368 if (iio_buffer_enabled(indio_dev))
369 return -EBUSY;
370
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100371 step_en = get_adc_chan_step_mask(adc_dev, chan);
372 if (!step_en)
373 return -EINVAL;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100374
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100375 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
376 while (fifo1count--)
377 tiadc_readl(adc_dev, REG_FIFO1);
378
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100379 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100380
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100381 timeout = jiffies + usecs_to_jiffies
382 (IDLE_TIMEOUT * adc_dev->channels);
383 /* Wait for Fifo threshold interrupt */
384 while (1) {
385 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
386 if (fifo1count)
387 break;
388
389 if (time_after(jiffies, timeout)) {
390 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100391 return -EAGAIN;
392 }
Sebastian Andrzej Siewiorfb7f8ce2013-12-19 16:28:27 +0100393 }
Jan Kardellbaa3c652014-11-06 22:18:00 +0000394 map_val = adc_dev->channel_step[chan->scan_index];
Patil, Rachna5e53a692012-10-16 12:55:45 +0530395
396 /*
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100397 * We check the complete FIFO. We programmed just one entry but in case
398 * something went wrong we left empty handed (-EAGAIN previously) and
399 * then the value apeared somehow in the FIFO we would have two entries.
400 * Therefore we read every item and keep only the latest version of the
401 * requested channel.
Patil, Rachna5e53a692012-10-16 12:55:45 +0530402 */
Patil, Rachna5e53a692012-10-16 12:55:45 +0530403 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200404 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100405 stepid = read & FIFOREAD_CHNLID_MASK;
406 stepid = stepid >> 0x10;
407
408 if (stepid == map_val) {
409 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200410 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100411 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200412 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530413 }
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100414 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100415
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200416 if (found == false)
417 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530418 return IIO_VAL_INT;
419}
420
421static const struct iio_info tiadc_info = {
422 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100423 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530424};
425
Vignesh Rdee1f552015-03-31 16:42:36 +0530426static int tiadc_parse_dt(struct platform_device *pdev,
427 struct tiadc_device *adc_dev)
428{
429 struct device_node *node = pdev->dev.of_node;
430 struct property *prop;
431 const __be32 *cur;
432 int channels = 0;
433 u32 val;
434
435 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
436 adc_dev->channel_line[channels] = val;
Vignesh R5dc11e82015-03-31 16:42:37 +0530437
438 /* Set Default values for optional DT parameters */
439 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
440 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
441 adc_dev->step_avg[channels] = 16;
442
Vignesh Rdee1f552015-03-31 16:42:36 +0530443 channels++;
444 }
445
Vignesh R5dc11e82015-03-31 16:42:37 +0530446 of_property_read_u32_array(node, "ti,chan-step-avg",
447 adc_dev->step_avg, channels);
448 of_property_read_u32_array(node, "ti,chan-step-opendelay",
449 adc_dev->open_delay, channels);
450 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
451 adc_dev->sample_delay, channels);
452
Vignesh Rdee1f552015-03-31 16:42:36 +0530453 adc_dev->channels = channels;
454 return 0;
455}
456
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800457static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530458{
459 struct iio_dev *indio_dev;
460 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000461 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530462 int err;
463
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200464 if (!node) {
465 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530466 return -EINVAL;
467 }
468
Andrew F. Davisfea89e22016-05-31 12:00:12 -0500469 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530470 if (indio_dev == NULL) {
471 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100472 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530473 }
474 adc_dev = iio_priv(indio_dev);
475
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000476 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
Vignesh Rdee1f552015-03-31 16:42:36 +0530477 tiadc_parse_dt(pdev, adc_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530478
479 indio_dev->dev.parent = &pdev->dev;
480 indio_dev->name = dev_name(&pdev->dev);
481 indio_dev->modes = INDIO_DIRECT_MODE;
482 indio_dev->info = &tiadc_info;
483
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100484 tiadc_step_config(indio_dev);
485 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530486
487 err = tiadc_channel_init(indio_dev, adc_dev->channels);
488 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100489 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530490
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100491 err = tiadc_iio_buffered_hardware_setup(indio_dev,
492 &tiadc_worker_h,
493 &tiadc_irq_h,
494 adc_dev->mfd_tscadc->irq,
495 IRQF_SHARED,
496 &tiadc_buffer_setup_ops);
497
Patil, Rachna5e53a692012-10-16 12:55:45 +0530498 if (err)
499 goto err_free_channels;
500
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100501 err = iio_device_register(indio_dev);
502 if (err)
503 goto err_buffer_unregister;
504
Patil, Rachna5e53a692012-10-16 12:55:45 +0530505 platform_set_drvdata(pdev, indio_dev);
506
507 return 0;
508
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100509err_buffer_unregister:
510 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530511err_free_channels:
512 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530513 return err;
514}
515
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800516static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530517{
518 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000519 struct tiadc_device *adc_dev = iio_priv(indio_dev);
520 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530521
522 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100523 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530524 tiadc_channels_remove(indio_dev);
525
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000526 step_en = get_adc_step_mask(adc_dev);
527 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
528
Patil, Rachna5e53a692012-10-16 12:55:45 +0530529 return 0;
530}
531
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500532static int __maybe_unused tiadc_suspend(struct device *dev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530533{
534 struct iio_dev *indio_dev = dev_get_drvdata(dev);
535 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200536 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530537 unsigned int idle;
538
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200539 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530540 if (!device_may_wakeup(tscadc_dev->dev)) {
541 idle = tiadc_readl(adc_dev, REG_CTRL);
542 idle &= ~(CNTRLREG_TSCSSENB);
543 tiadc_writel(adc_dev, REG_CTRL, (idle |
544 CNTRLREG_POWERDOWN));
545 }
546
547 return 0;
548}
549
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500550static int __maybe_unused tiadc_resume(struct device *dev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530551{
552 struct iio_dev *indio_dev = dev_get_drvdata(dev);
553 struct tiadc_device *adc_dev = iio_priv(indio_dev);
554 unsigned int restore;
555
556 /* Make sure ADC is powered up */
557 restore = tiadc_readl(adc_dev, REG_CTRL);
558 restore &= ~(CNTRLREG_POWERDOWN);
559 tiadc_writel(adc_dev, REG_CTRL, restore);
560
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100561 tiadc_step_config(indio_dev);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100562 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
563 adc_dev->buffer_en_ch_steps);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530564 return 0;
565}
566
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500567static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530568
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000569static const struct of_device_id ti_adc_dt_ids[] = {
570 { .compatible = "ti,am3359-adc", },
571 { }
572};
573MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
574
Patil, Rachna5e53a692012-10-16 12:55:45 +0530575static struct platform_driver tiadc_driver = {
576 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200577 .name = "TI-am335x-adc",
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500578 .pm = &tiadc_pm_ops,
Sachin Kamatde06b342013-10-21 10:27:00 +0100579 .of_match_table = ti_adc_dt_ids,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530580 },
581 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800582 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530583};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530584module_platform_driver(tiadc_driver);
585
586MODULE_DESCRIPTION("TI ADC controller driver");
587MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
588MODULE_LICENSE("GPL");