blob: 91dce1f7a770226f57223e26e91a2a77046a2b0f [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
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>
Patil, Rachna6f39ac42013-01-24 03:45:11 +000025#include <linux/of.h>
26#include <linux/of_device.h>
Pantelis Antoniouc80df482012-10-13 16:37:24 +030027#include <linux/iio/machine.h>
28#include <linux/iio/driver.h>
Patil, Rachna5e53a692012-10-16 12:55:45 +053029
30#include <linux/mfd/ti_am335x_tscadc.h>
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010031#include <linux/iio/buffer.h>
32#include <linux/iio/kfifo_buf.h>
33#include <linux/iio/trigger.h>
34#include <linux/iio/trigger_consumer.h>
35#include <linux/iio/triggered_buffer.h>
Patil, Rachna5e53a692012-10-16 12:55:45 +053036
37struct tiadc_device {
38 struct ti_tscadc_dev *mfd_tscadc;
39 int channels;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020040 u8 channel_line[8];
41 u8 channel_step[8];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010042 int buffer_en_ch_steps;
43 struct iio_trigger *trig;
44 u16 data[8];
Patil, Rachna5e53a692012-10-16 12:55:45 +053045};
46
47static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
48{
49 return readl(adc->mfd_tscadc->tscadc_base + reg);
50}
51
52static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
53 unsigned int val)
54{
55 writel(val, adc->mfd_tscadc->tscadc_base + reg);
56}
57
Patil, Rachnaabeccee2013-01-24 03:45:05 +000058static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
59{
60 u32 step_en;
61
62 step_en = ((1 << adc_dev->channels) - 1);
63 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
64 return step_en;
65}
66
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010067static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
Patil, Rachna5e53a692012-10-16 12:55:45 +053068{
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010069 return 1 << adc_dev->channel_step[chan];
70}
71
72static void tiadc_step_config(struct iio_dev *indio_dev)
73{
74 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +053075 unsigned int stepconfig;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020076 int i, steps;
Patil, Rachna5e53a692012-10-16 12:55:45 +053077
78 /*
79 * There are 16 configurable steps and 8 analog input
80 * lines available which are shared between Touchscreen and ADC.
81 *
82 * Steps backwards i.e. from 16 towards 0 are used by ADC
83 * depending on number of input lines needed.
84 * Channel would represent which analog input
85 * needs to be given to ADC to digitalize data.
86 */
87
88 steps = TOTAL_STEPS - adc_dev->channels;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010089 if (iio_buffer_enabled(indio_dev))
90 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
91 | STEPCONFIG_MODE_SWCNT;
92 else
93 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
Patil, Rachna5e53a692012-10-16 12:55:45 +053094
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020095 for (i = 0; i < adc_dev->channels; i++) {
96 int chan;
97
98 chan = adc_dev->channel_line[i];
99 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
100 stepconfig | STEPCONFIG_INP(chan));
101 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530102 STEPCONFIG_OPENDLY);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200103 adc_dev->channel_step[i] = steps;
104 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530105 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530106}
107
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100108static irqreturn_t tiadc_irq_h(int irq, void *private)
109{
110 struct iio_dev *indio_dev = private;
111 struct tiadc_device *adc_dev = iio_priv(indio_dev);
112 unsigned int status, config;
113 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
114
115 /*
116 * ADC and touchscreen share the IRQ line.
117 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
118 */
119 if (status & IRQENB_FIFO1OVRRUN) {
120 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
121 config = tiadc_readl(adc_dev, REG_CTRL);
122 config &= ~(CNTRLREG_TSCSSENB);
123 tiadc_writel(adc_dev, REG_CTRL, config);
124 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
125 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
126 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
127 return IRQ_HANDLED;
128 } else if (status & IRQENB_FIFO1THRES) {
129 /* Disable irq and wake worker thread */
130 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
131 return IRQ_WAKE_THREAD;
132 }
133
134 return IRQ_NONE;
135}
136
137static irqreturn_t tiadc_worker_h(int irq, void *private)
138{
139 struct iio_dev *indio_dev = private;
140 struct tiadc_device *adc_dev = iio_priv(indio_dev);
141 int i, k, fifo1count, read;
142 u16 *data = adc_dev->data;
143
144 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
145 for (k = 0; k < fifo1count; k = k + i) {
146 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
147 read = tiadc_readl(adc_dev, REG_FIFO1);
148 data[i] = read & FIFOREAD_DATA_MASK;
149 }
150 iio_push_to_buffers(indio_dev, (u8 *) data);
151 }
152
153 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
154 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
155
156 return IRQ_HANDLED;
157}
158
159static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
160{
161 struct tiadc_device *adc_dev = iio_priv(indio_dev);
162 int i, fifo1count, read;
163
164 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
165 IRQENB_FIFO1OVRRUN |
166 IRQENB_FIFO1UNDRFLW));
167
168 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
169 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
170 for (i = 0; i < fifo1count; i++)
171 read = tiadc_readl(adc_dev, REG_FIFO1);
172
173 return iio_sw_buffer_preenable(indio_dev);
174}
175
176static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
177{
178 struct tiadc_device *adc_dev = iio_priv(indio_dev);
179 struct iio_buffer *buffer = indio_dev->buffer;
180 unsigned int enb = 0;
181 u8 bit;
182
183 tiadc_step_config(indio_dev);
184 for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels)
185 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
186 adc_dev->buffer_en_ch_steps = enb;
187
188 am335x_tsc_se_set(adc_dev->mfd_tscadc, enb);
189
190 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
191 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
192 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
193 | IRQENB_FIFO1OVRRUN);
194
195 return 0;
196}
197
198static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
199{
200 struct tiadc_device *adc_dev = iio_priv(indio_dev);
201 int fifo1count, i, read;
202
203 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
204 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
205 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
206
207 /* Flush FIFO of leftover data in the time it takes to disable adc */
208 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
209 for (i = 0; i < fifo1count; i++)
210 read = tiadc_readl(adc_dev, REG_FIFO1);
211
212 return 0;
213}
214
215static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
216{
217 tiadc_step_config(indio_dev);
218
219 return 0;
220}
221
222static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
223 .preenable = &tiadc_buffer_preenable,
224 .postenable = &tiadc_buffer_postenable,
225 .predisable = &tiadc_buffer_predisable,
226 .postdisable = &tiadc_buffer_postdisable,
227};
228
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100229static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100230 irqreturn_t (*pollfunc_bh)(int irq, void *p),
231 irqreturn_t (*pollfunc_th)(int irq, void *p),
232 int irq,
233 unsigned long flags,
234 const struct iio_buffer_setup_ops *setup_ops)
235{
236 int ret;
237
238 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
239 if (!indio_dev->buffer)
240 return -ENOMEM;
241
242 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
243 flags, indio_dev->name, indio_dev);
244 if (ret)
245 goto error_kfifo_free;
246
247 indio_dev->setup_ops = setup_ops;
248 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
249
250 ret = iio_buffer_register(indio_dev,
251 indio_dev->channels,
252 indio_dev->num_channels);
253 if (ret)
254 goto error_free_irq;
255
256 return 0;
257
258error_free_irq:
259 free_irq(irq, indio_dev);
260error_kfifo_free:
261 iio_kfifo_free(indio_dev->buffer);
262 return ret;
263}
264
265static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
266{
267 struct tiadc_device *adc_dev = iio_priv(indio_dev);
268
269 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
270 iio_kfifo_free(indio_dev->buffer);
271 iio_buffer_unregister(indio_dev);
272}
273
274
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300275static const char * const chan_name_ain[] = {
276 "AIN0",
277 "AIN1",
278 "AIN2",
279 "AIN3",
280 "AIN4",
281 "AIN5",
282 "AIN6",
283 "AIN7",
284};
285
Patil, Rachna5e53a692012-10-16 12:55:45 +0530286static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
287{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300288 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530289 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300290 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530291 int i;
292
293 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300294 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530295 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530296 if (chan_array == NULL)
297 return -ENOMEM;
298
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300299 chan = chan_array;
300 for (i = 0; i < channels; i++, chan++) {
301
Patil, Rachna5e53a692012-10-16 12:55:45 +0530302 chan->type = IIO_VOLTAGE;
303 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200304 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000305 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200306 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100307 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300308 chan->scan_type.sign = 'u';
309 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100310 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530311 }
312
313 indio_dev->channels = chan_array;
314
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300315 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530316}
317
318static void tiadc_channels_remove(struct iio_dev *indio_dev)
319{
320 kfree(indio_dev->channels);
321}
322
323static int tiadc_read_raw(struct iio_dev *indio_dev,
324 struct iio_chan_spec const *chan,
325 int *val, int *val2, long mask)
326{
327 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100328 int i, map_val;
329 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200330 u32 step = UINT_MAX;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200331 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100332 u32 step_en;
333 unsigned long timeout = jiffies + usecs_to_jiffies
334 (IDLE_TIMEOUT * adc_dev->channels);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100335
336 if (iio_buffer_enabled(indio_dev))
337 return -EBUSY;
338
Patil, Rachnab1451e52013-07-20 17:27:00 +0100339 step_en = get_adc_step_mask(adc_dev);
340 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
341
342 /* Wait for ADC sequencer to complete sampling */
343 while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
344 if (time_after(jiffies, timeout))
345 return -EAGAIN;
346 }
347 map_val = chan->channel + TOTAL_CHANNELS;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530348
349 /*
350 * When the sub-system is first enabled,
351 * the sequencer will always start with the
352 * lowest step (1) and continue until step (16).
353 * For ex: If we have enabled 4 ADC channels and
354 * currently use only 1 out of them, the
355 * sequencer still configures all the 4 steps,
356 * leading to 3 unwanted data.
357 * Hence we need to flush out this data.
358 */
359
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200360 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
361 if (chan->channel == adc_dev->channel_line[i]) {
362 step = adc_dev->channel_step[i];
363 break;
364 }
365 }
366 if (WARN_ON_ONCE(step == UINT_MAX))
367 return -EINVAL;
368
Patil, Rachna5e53a692012-10-16 12:55:45 +0530369 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
370 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200371 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100372 stepid = read & FIFOREAD_CHNLID_MASK;
373 stepid = stepid >> 0x10;
374
375 if (stepid == map_val) {
376 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200377 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100378 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200379 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530380 }
Patil, Rachnab1451e52013-07-20 17:27:00 +0100381
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200382 if (found == false)
383 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530384 return IIO_VAL_INT;
385}
386
387static const struct iio_info tiadc_info = {
388 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100389 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530390};
391
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800392static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530393{
394 struct iio_dev *indio_dev;
395 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000396 struct device_node *node = pdev->dev.of_node;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200397 struct property *prop;
398 const __be32 *cur;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530399 int err;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200400 u32 val;
401 int channels = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530402
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200403 if (!node) {
404 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530405 return -EINVAL;
406 }
407
Sachin Kamata0648132013-07-23 09:46:00 +0100408 indio_dev = devm_iio_device_alloc(&pdev->dev,
409 sizeof(struct tiadc_device));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530410 if (indio_dev == NULL) {
411 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100412 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530413 }
414 adc_dev = iio_priv(indio_dev);
415
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000416 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
417
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200418 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
419 adc_dev->channel_line[channels] = val;
420 channels++;
421 }
422 adc_dev->channels = channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530423
424 indio_dev->dev.parent = &pdev->dev;
425 indio_dev->name = dev_name(&pdev->dev);
426 indio_dev->modes = INDIO_DIRECT_MODE;
427 indio_dev->info = &tiadc_info;
428
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100429 tiadc_step_config(indio_dev);
430 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530431
432 err = tiadc_channel_init(indio_dev, adc_dev->channels);
433 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100434 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530435
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100436 err = tiadc_iio_buffered_hardware_setup(indio_dev,
437 &tiadc_worker_h,
438 &tiadc_irq_h,
439 adc_dev->mfd_tscadc->irq,
440 IRQF_SHARED,
441 &tiadc_buffer_setup_ops);
442
Patil, Rachna5e53a692012-10-16 12:55:45 +0530443 if (err)
444 goto err_free_channels;
445
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100446 err = iio_device_register(indio_dev);
447 if (err)
448 goto err_buffer_unregister;
449
Patil, Rachna5e53a692012-10-16 12:55:45 +0530450 platform_set_drvdata(pdev, indio_dev);
451
452 return 0;
453
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100454err_buffer_unregister:
455 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530456err_free_channels:
457 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530458 return err;
459}
460
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800461static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530462{
463 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000464 struct tiadc_device *adc_dev = iio_priv(indio_dev);
465 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530466
467 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100468 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530469 tiadc_channels_remove(indio_dev);
470
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000471 step_en = get_adc_step_mask(adc_dev);
472 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
473
Patil, Rachna5e53a692012-10-16 12:55:45 +0530474 return 0;
475}
476
477#ifdef CONFIG_PM
478static int tiadc_suspend(struct device *dev)
479{
480 struct iio_dev *indio_dev = dev_get_drvdata(dev);
481 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200482 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530483 unsigned int idle;
484
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200485 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530486 if (!device_may_wakeup(tscadc_dev->dev)) {
487 idle = tiadc_readl(adc_dev, REG_CTRL);
488 idle &= ~(CNTRLREG_TSCSSENB);
489 tiadc_writel(adc_dev, REG_CTRL, (idle |
490 CNTRLREG_POWERDOWN));
491 }
492
493 return 0;
494}
495
496static int tiadc_resume(struct device *dev)
497{
498 struct iio_dev *indio_dev = dev_get_drvdata(dev);
499 struct tiadc_device *adc_dev = iio_priv(indio_dev);
500 unsigned int restore;
501
502 /* Make sure ADC is powered up */
503 restore = tiadc_readl(adc_dev, REG_CTRL);
504 restore &= ~(CNTRLREG_POWERDOWN);
505 tiadc_writel(adc_dev, REG_CTRL, restore);
506
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100507 tiadc_step_config(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530508
509 return 0;
510}
511
512static const struct dev_pm_ops tiadc_pm_ops = {
513 .suspend = tiadc_suspend,
514 .resume = tiadc_resume,
515};
516#define TIADC_PM_OPS (&tiadc_pm_ops)
517#else
518#define TIADC_PM_OPS NULL
519#endif
520
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000521static const struct of_device_id ti_adc_dt_ids[] = {
522 { .compatible = "ti,am3359-adc", },
523 { }
524};
525MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
526
Patil, Rachna5e53a692012-10-16 12:55:45 +0530527static struct platform_driver tiadc_driver = {
528 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200529 .name = "TI-am335x-adc",
Patil, Rachna5e53a692012-10-16 12:55:45 +0530530 .owner = THIS_MODULE,
531 .pm = TIADC_PM_OPS,
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000532 .of_match_table = of_match_ptr(ti_adc_dt_ids),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530533 },
534 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800535 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530536};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530537module_platform_driver(tiadc_driver);
538
539MODULE_DESCRIPTION("TI ADC controller driver");
540MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
541MODULE_LICENSE("GPL");