blob: d4d748214e4b364dc716889d2b66363353c2ee56 [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>
Patil, Rachna5e53a692012-10-16 12:55:45 +053033
34struct tiadc_device {
35 struct ti_tscadc_dev *mfd_tscadc;
36 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];
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
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010063static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
Patil, Rachna5e53a692012-10-16 12:55:45 +053064{
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010065 return 1 << adc_dev->channel_step[chan];
66}
67
68static void tiadc_step_config(struct iio_dev *indio_dev)
69{
70 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +053071 unsigned int stepconfig;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020072 int i, steps;
Patil, Rachna5e53a692012-10-16 12:55:45 +053073
74 /*
75 * There are 16 configurable steps and 8 analog input
76 * lines available which are shared between Touchscreen and ADC.
77 *
78 * Steps backwards i.e. from 16 towards 0 are used by ADC
79 * depending on number of input lines needed.
80 * Channel would represent which analog input
81 * needs to be given to ADC to digitalize data.
82 */
83
84 steps = TOTAL_STEPS - adc_dev->channels;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010085 if (iio_buffer_enabled(indio_dev))
86 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
87 | STEPCONFIG_MODE_SWCNT;
88 else
89 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
Patil, Rachna5e53a692012-10-16 12:55:45 +053090
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020091 for (i = 0; i < adc_dev->channels; i++) {
92 int chan;
93
94 chan = adc_dev->channel_line[i];
95 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
96 stepconfig | STEPCONFIG_INP(chan));
97 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Patil, Rachna5e53a692012-10-16 12:55:45 +053098 STEPCONFIG_OPENDLY);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020099 adc_dev->channel_step[i] = steps;
100 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530101 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530102}
103
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100104static irqreturn_t tiadc_irq_h(int irq, void *private)
105{
106 struct iio_dev *indio_dev = private;
107 struct tiadc_device *adc_dev = iio_priv(indio_dev);
108 unsigned int status, config;
109 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
110
111 /*
112 * ADC and touchscreen share the IRQ line.
113 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
114 */
115 if (status & IRQENB_FIFO1OVRRUN) {
116 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
117 config = tiadc_readl(adc_dev, REG_CTRL);
118 config &= ~(CNTRLREG_TSCSSENB);
119 tiadc_writel(adc_dev, REG_CTRL, config);
120 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
121 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
122 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
123 return IRQ_HANDLED;
124 } else if (status & IRQENB_FIFO1THRES) {
125 /* Disable irq and wake worker thread */
126 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
127 return IRQ_WAKE_THREAD;
128 }
129
130 return IRQ_NONE;
131}
132
133static irqreturn_t tiadc_worker_h(int irq, void *private)
134{
135 struct iio_dev *indio_dev = private;
136 struct tiadc_device *adc_dev = iio_priv(indio_dev);
137 int i, k, fifo1count, read;
138 u16 *data = adc_dev->data;
139
140 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
141 for (k = 0; k < fifo1count; k = k + i) {
142 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
143 read = tiadc_readl(adc_dev, REG_FIFO1);
144 data[i] = read & FIFOREAD_DATA_MASK;
145 }
146 iio_push_to_buffers(indio_dev, (u8 *) data);
147 }
148
149 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
150 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
151
152 return IRQ_HANDLED;
153}
154
155static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
156{
157 struct tiadc_device *adc_dev = iio_priv(indio_dev);
158 int i, fifo1count, read;
159
160 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
161 IRQENB_FIFO1OVRRUN |
162 IRQENB_FIFO1UNDRFLW));
163
164 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
165 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
166 for (i = 0; i < fifo1count; i++)
167 read = tiadc_readl(adc_dev, REG_FIFO1);
168
Lars-Peter Clausen24adaf72013-10-14 17:49:00 +0100169 return 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100170}
171
172static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
173{
174 struct tiadc_device *adc_dev = iio_priv(indio_dev);
175 struct iio_buffer *buffer = indio_dev->buffer;
176 unsigned int enb = 0;
177 u8 bit;
178
179 tiadc_step_config(indio_dev);
180 for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels)
181 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
182 adc_dev->buffer_en_ch_steps = enb;
183
184 am335x_tsc_se_set(adc_dev->mfd_tscadc, enb);
185
186 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
187 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
188 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
189 | IRQENB_FIFO1OVRRUN);
190
191 return 0;
192}
193
194static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
195{
196 struct tiadc_device *adc_dev = iio_priv(indio_dev);
197 int fifo1count, i, read;
198
199 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
200 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
201 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
202
203 /* Flush FIFO of leftover data in the time it takes to disable adc */
204 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
205 for (i = 0; i < fifo1count; i++)
206 read = tiadc_readl(adc_dev, REG_FIFO1);
207
208 return 0;
209}
210
211static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
212{
213 tiadc_step_config(indio_dev);
214
215 return 0;
216}
217
218static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
219 .preenable = &tiadc_buffer_preenable,
220 .postenable = &tiadc_buffer_postenable,
221 .predisable = &tiadc_buffer_predisable,
222 .postdisable = &tiadc_buffer_postdisable,
223};
224
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100225static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100226 irqreturn_t (*pollfunc_bh)(int irq, void *p),
227 irqreturn_t (*pollfunc_th)(int irq, void *p),
228 int irq,
229 unsigned long flags,
230 const struct iio_buffer_setup_ops *setup_ops)
231{
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100232 struct iio_buffer *buffer;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100233 int ret;
234
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100235 buffer = iio_kfifo_allocate(indio_dev);
236 if (!buffer)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100237 return -ENOMEM;
238
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100239 iio_device_attach_buffer(indio_dev, buffer);
240
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100241 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
242 flags, indio_dev->name, indio_dev);
243 if (ret)
244 goto error_kfifo_free;
245
246 indio_dev->setup_ops = setup_ops;
247 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
248
249 ret = iio_buffer_register(indio_dev,
250 indio_dev->channels,
251 indio_dev->num_channels);
252 if (ret)
253 goto error_free_irq;
254
255 return 0;
256
257error_free_irq:
258 free_irq(irq, indio_dev);
259error_kfifo_free:
260 iio_kfifo_free(indio_dev->buffer);
261 return ret;
262}
263
264static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
265{
266 struct tiadc_device *adc_dev = iio_priv(indio_dev);
267
268 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
269 iio_kfifo_free(indio_dev->buffer);
270 iio_buffer_unregister(indio_dev);
271}
272
273
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300274static const char * const chan_name_ain[] = {
275 "AIN0",
276 "AIN1",
277 "AIN2",
278 "AIN3",
279 "AIN4",
280 "AIN5",
281 "AIN6",
282 "AIN7",
283};
284
Patil, Rachna5e53a692012-10-16 12:55:45 +0530285static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
286{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300287 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530288 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300289 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530290 int i;
291
292 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300293 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530294 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530295 if (chan_array == NULL)
296 return -ENOMEM;
297
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300298 chan = chan_array;
299 for (i = 0; i < channels; i++, chan++) {
300
Patil, Rachna5e53a692012-10-16 12:55:45 +0530301 chan->type = IIO_VOLTAGE;
302 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200303 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000304 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200305 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100306 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300307 chan->scan_type.sign = 'u';
308 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100309 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530310 }
311
312 indio_dev->channels = chan_array;
313
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300314 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530315}
316
317static void tiadc_channels_remove(struct iio_dev *indio_dev)
318{
319 kfree(indio_dev->channels);
320}
321
322static int tiadc_read_raw(struct iio_dev *indio_dev,
323 struct iio_chan_spec const *chan,
324 int *val, int *val2, long mask)
325{
326 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100327 int i, map_val;
328 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200329 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100330 u32 step_en;
331 unsigned long timeout = jiffies + usecs_to_jiffies
332 (IDLE_TIMEOUT * adc_dev->channels);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100333
334 if (iio_buffer_enabled(indio_dev))
335 return -EBUSY;
336
Patil, Rachnab1451e52013-07-20 17:27:00 +0100337 step_en = get_adc_step_mask(adc_dev);
338 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
339
340 /* Wait for ADC sequencer to complete sampling */
341 while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
342 if (time_after(jiffies, timeout))
343 return -EAGAIN;
344 }
345 map_val = chan->channel + TOTAL_CHANNELS;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530346
347 /*
348 * When the sub-system is first enabled,
349 * the sequencer will always start with the
350 * lowest step (1) and continue until step (16).
351 * For ex: If we have enabled 4 ADC channels and
352 * currently use only 1 out of them, the
353 * sequencer still configures all the 4 steps,
354 * leading to 3 unwanted data.
355 * Hence we need to flush out this data.
356 */
357
358 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
359 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200360 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100361 stepid = read & FIFOREAD_CHNLID_MASK;
362 stepid = stepid >> 0x10;
363
364 if (stepid == map_val) {
365 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200366 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100367 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200368 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530369 }
Patil, Rachnab1451e52013-07-20 17:27:00 +0100370
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200371 if (found == false)
372 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530373 return IIO_VAL_INT;
374}
375
376static const struct iio_info tiadc_info = {
377 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100378 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530379};
380
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800381static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530382{
383 struct iio_dev *indio_dev;
384 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000385 struct device_node *node = pdev->dev.of_node;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200386 struct property *prop;
387 const __be32 *cur;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530388 int err;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200389 u32 val;
390 int channels = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530391
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200392 if (!node) {
393 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530394 return -EINVAL;
395 }
396
Sachin Kamata0648132013-07-23 09:46:00 +0100397 indio_dev = devm_iio_device_alloc(&pdev->dev,
398 sizeof(struct tiadc_device));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530399 if (indio_dev == NULL) {
400 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100401 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530402 }
403 adc_dev = iio_priv(indio_dev);
404
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000405 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
406
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200407 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
408 adc_dev->channel_line[channels] = val;
409 channels++;
410 }
411 adc_dev->channels = channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530412
413 indio_dev->dev.parent = &pdev->dev;
414 indio_dev->name = dev_name(&pdev->dev);
415 indio_dev->modes = INDIO_DIRECT_MODE;
416 indio_dev->info = &tiadc_info;
417
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100418 tiadc_step_config(indio_dev);
419 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530420
421 err = tiadc_channel_init(indio_dev, adc_dev->channels);
422 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100423 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530424
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100425 err = tiadc_iio_buffered_hardware_setup(indio_dev,
426 &tiadc_worker_h,
427 &tiadc_irq_h,
428 adc_dev->mfd_tscadc->irq,
429 IRQF_SHARED,
430 &tiadc_buffer_setup_ops);
431
Patil, Rachna5e53a692012-10-16 12:55:45 +0530432 if (err)
433 goto err_free_channels;
434
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100435 err = iio_device_register(indio_dev);
436 if (err)
437 goto err_buffer_unregister;
438
Patil, Rachna5e53a692012-10-16 12:55:45 +0530439 platform_set_drvdata(pdev, indio_dev);
440
441 return 0;
442
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100443err_buffer_unregister:
444 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530445err_free_channels:
446 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530447 return err;
448}
449
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800450static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530451{
452 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000453 struct tiadc_device *adc_dev = iio_priv(indio_dev);
454 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530455
456 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100457 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530458 tiadc_channels_remove(indio_dev);
459
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000460 step_en = get_adc_step_mask(adc_dev);
461 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
462
Patil, Rachna5e53a692012-10-16 12:55:45 +0530463 return 0;
464}
465
466#ifdef CONFIG_PM
467static int tiadc_suspend(struct device *dev)
468{
469 struct iio_dev *indio_dev = dev_get_drvdata(dev);
470 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200471 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530472 unsigned int idle;
473
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200474 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530475 if (!device_may_wakeup(tscadc_dev->dev)) {
476 idle = tiadc_readl(adc_dev, REG_CTRL);
477 idle &= ~(CNTRLREG_TSCSSENB);
478 tiadc_writel(adc_dev, REG_CTRL, (idle |
479 CNTRLREG_POWERDOWN));
480 }
481
482 return 0;
483}
484
485static int tiadc_resume(struct device *dev)
486{
487 struct iio_dev *indio_dev = dev_get_drvdata(dev);
488 struct tiadc_device *adc_dev = iio_priv(indio_dev);
489 unsigned int restore;
490
491 /* Make sure ADC is powered up */
492 restore = tiadc_readl(adc_dev, REG_CTRL);
493 restore &= ~(CNTRLREG_POWERDOWN);
494 tiadc_writel(adc_dev, REG_CTRL, restore);
495
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100496 tiadc_step_config(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530497
498 return 0;
499}
500
501static const struct dev_pm_ops tiadc_pm_ops = {
502 .suspend = tiadc_suspend,
503 .resume = tiadc_resume,
504};
505#define TIADC_PM_OPS (&tiadc_pm_ops)
506#else
507#define TIADC_PM_OPS NULL
508#endif
509
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000510static const struct of_device_id ti_adc_dt_ids[] = {
511 { .compatible = "ti,am3359-adc", },
512 { }
513};
514MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
515
Patil, Rachna5e53a692012-10-16 12:55:45 +0530516static struct platform_driver tiadc_driver = {
517 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200518 .name = "TI-am335x-adc",
Patil, Rachna5e53a692012-10-16 12:55:45 +0530519 .owner = THIS_MODULE,
520 .pm = TIADC_PM_OPS,
Sachin Kamatde06b342013-10-21 10:27:00 +0100521 .of_match_table = ti_adc_dt_ids,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530522 },
523 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800524 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530525};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530526module_platform_driver(tiadc_driver);
527
528MODULE_DESCRIPTION("TI ADC controller driver");
529MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
530MODULE_LICENSE("GPL");