blob: 31e786e3999b128ac8e53629db48290086162f5c [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
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);
Patil, Rachna5e53a692012-10-16 12:55:45 +053089 unsigned int stepconfig;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020090 int i, steps;
Patil, Rachna5e53a692012-10-16 12:55:45 +053091
92 /*
93 * There are 16 configurable steps and 8 analog input
94 * lines available which are shared between Touchscreen and ADC.
95 *
96 * Steps backwards i.e. from 16 towards 0 are used by ADC
97 * depending on number of input lines needed.
98 * Channel would represent which analog input
99 * needs to be given to ADC to digitalize data.
100 */
101
102 steps = TOTAL_STEPS - adc_dev->channels;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100103 if (iio_buffer_enabled(indio_dev))
104 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
105 | STEPCONFIG_MODE_SWCNT;
106 else
107 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530108
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200109 for (i = 0; i < adc_dev->channels; i++) {
110 int chan;
111
112 chan = adc_dev->channel_line[i];
113 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
114 stepconfig | STEPCONFIG_INP(chan));
115 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530116 STEPCONFIG_OPENDLY);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200117 adc_dev->channel_step[i] = steps;
118 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530119 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530120}
121
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100122static irqreturn_t tiadc_irq_h(int irq, void *private)
123{
124 struct iio_dev *indio_dev = private;
125 struct tiadc_device *adc_dev = iio_priv(indio_dev);
126 unsigned int status, config;
127 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
128
129 /*
130 * ADC and touchscreen share the IRQ line.
131 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
132 */
133 if (status & IRQENB_FIFO1OVRRUN) {
134 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
135 config = tiadc_readl(adc_dev, REG_CTRL);
136 config &= ~(CNTRLREG_TSCSSENB);
137 tiadc_writel(adc_dev, REG_CTRL, config);
138 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
139 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
140 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
141 return IRQ_HANDLED;
142 } else if (status & IRQENB_FIFO1THRES) {
143 /* Disable irq and wake worker thread */
144 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
145 return IRQ_WAKE_THREAD;
146 }
147
148 return IRQ_NONE;
149}
150
151static irqreturn_t tiadc_worker_h(int irq, void *private)
152{
153 struct iio_dev *indio_dev = private;
154 struct tiadc_device *adc_dev = iio_priv(indio_dev);
155 int i, k, fifo1count, read;
156 u16 *data = adc_dev->data;
157
158 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
159 for (k = 0; k < fifo1count; k = k + i) {
160 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
161 read = tiadc_readl(adc_dev, REG_FIFO1);
162 data[i] = read & FIFOREAD_DATA_MASK;
163 }
164 iio_push_to_buffers(indio_dev, (u8 *) data);
165 }
166
167 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
168 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
169
170 return IRQ_HANDLED;
171}
172
173static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
174{
175 struct tiadc_device *adc_dev = iio_priv(indio_dev);
176 int i, fifo1count, read;
177
178 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
179 IRQENB_FIFO1OVRRUN |
180 IRQENB_FIFO1UNDRFLW));
181
182 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
183 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
184 for (i = 0; i < fifo1count; i++)
185 read = tiadc_readl(adc_dev, REG_FIFO1);
186
Lars-Peter Clausen24adaf72013-10-14 17:49:00 +0100187 return 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100188}
189
190static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
191{
192 struct tiadc_device *adc_dev = iio_priv(indio_dev);
193 struct iio_buffer *buffer = indio_dev->buffer;
194 unsigned int enb = 0;
195 u8 bit;
196
197 tiadc_step_config(indio_dev);
198 for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels)
199 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
200 adc_dev->buffer_en_ch_steps = enb;
201
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100202 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100203
204 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
205 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
206 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
207 | IRQENB_FIFO1OVRRUN);
208
209 return 0;
210}
211
212static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
213{
214 struct tiadc_device *adc_dev = iio_priv(indio_dev);
215 int fifo1count, i, read;
216
217 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
218 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
219 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
Sebastian Andrzej Siewior3954b7b2013-12-19 16:28:30 +0100220 adc_dev->buffer_en_ch_steps = 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100221
222 /* Flush FIFO of leftover data in the time it takes to disable adc */
223 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
224 for (i = 0; i < fifo1count; i++)
225 read = tiadc_readl(adc_dev, REG_FIFO1);
226
227 return 0;
228}
229
230static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
231{
232 tiadc_step_config(indio_dev);
233
234 return 0;
235}
236
237static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
238 .preenable = &tiadc_buffer_preenable,
239 .postenable = &tiadc_buffer_postenable,
240 .predisable = &tiadc_buffer_predisable,
241 .postdisable = &tiadc_buffer_postdisable,
242};
243
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100244static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100245 irqreturn_t (*pollfunc_bh)(int irq, void *p),
246 irqreturn_t (*pollfunc_th)(int irq, void *p),
247 int irq,
248 unsigned long flags,
249 const struct iio_buffer_setup_ops *setup_ops)
250{
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100251 struct iio_buffer *buffer;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100252 int ret;
253
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100254 buffer = iio_kfifo_allocate(indio_dev);
255 if (!buffer)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100256 return -ENOMEM;
257
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100258 iio_device_attach_buffer(indio_dev, buffer);
259
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100260 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
261 flags, indio_dev->name, indio_dev);
262 if (ret)
263 goto error_kfifo_free;
264
265 indio_dev->setup_ops = setup_ops;
266 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
267
268 ret = iio_buffer_register(indio_dev,
269 indio_dev->channels,
270 indio_dev->num_channels);
271 if (ret)
272 goto error_free_irq;
273
274 return 0;
275
276error_free_irq:
277 free_irq(irq, indio_dev);
278error_kfifo_free:
279 iio_kfifo_free(indio_dev->buffer);
280 return ret;
281}
282
283static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
284{
285 struct tiadc_device *adc_dev = iio_priv(indio_dev);
286
287 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
288 iio_kfifo_free(indio_dev->buffer);
289 iio_buffer_unregister(indio_dev);
290}
291
292
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300293static const char * const chan_name_ain[] = {
294 "AIN0",
295 "AIN1",
296 "AIN2",
297 "AIN3",
298 "AIN4",
299 "AIN5",
300 "AIN6",
301 "AIN7",
302};
303
Patil, Rachna5e53a692012-10-16 12:55:45 +0530304static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
305{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300306 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530307 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300308 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530309 int i;
310
311 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300312 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530313 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530314 if (chan_array == NULL)
315 return -ENOMEM;
316
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300317 chan = chan_array;
318 for (i = 0; i < channels; i++, chan++) {
319
Patil, Rachna5e53a692012-10-16 12:55:45 +0530320 chan->type = IIO_VOLTAGE;
321 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200322 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000323 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200324 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100325 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300326 chan->scan_type.sign = 'u';
327 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100328 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530329 }
330
331 indio_dev->channels = chan_array;
332
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300333 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530334}
335
336static void tiadc_channels_remove(struct iio_dev *indio_dev)
337{
338 kfree(indio_dev->channels);
339}
340
341static int tiadc_read_raw(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
343 int *val, int *val2, long mask)
344{
345 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100346 int i, map_val;
347 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200348 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100349 u32 step_en;
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100350 unsigned long timeout;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100351
352 if (iio_buffer_enabled(indio_dev))
353 return -EBUSY;
354
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100355 step_en = get_adc_chan_step_mask(adc_dev, chan);
356 if (!step_en)
357 return -EINVAL;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100358
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100359 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
360 while (fifo1count--)
361 tiadc_readl(adc_dev, REG_FIFO1);
362
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100363 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100364
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100365 timeout = jiffies + usecs_to_jiffies
366 (IDLE_TIMEOUT * adc_dev->channels);
367 /* Wait for Fifo threshold interrupt */
368 while (1) {
369 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
370 if (fifo1count)
371 break;
372
373 if (time_after(jiffies, timeout)) {
374 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100375 return -EAGAIN;
376 }
Sebastian Andrzej Siewiorfb7f8ce2013-12-19 16:28:27 +0100377 }
Patil, Rachnab1451e52013-07-20 17:27:00 +0100378 map_val = chan->channel + TOTAL_CHANNELS;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530379
380 /*
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100381 * We check the complete FIFO. We programmed just one entry but in case
382 * something went wrong we left empty handed (-EAGAIN previously) and
383 * then the value apeared somehow in the FIFO we would have two entries.
384 * Therefore we read every item and keep only the latest version of the
385 * requested channel.
Patil, Rachna5e53a692012-10-16 12:55:45 +0530386 */
Patil, Rachna5e53a692012-10-16 12:55:45 +0530387 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200388 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100389 stepid = read & FIFOREAD_CHNLID_MASK;
390 stepid = stepid >> 0x10;
391
392 if (stepid == map_val) {
393 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200394 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100395 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200396 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530397 }
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100398 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100399
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200400 if (found == false)
401 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530402 return IIO_VAL_INT;
403}
404
405static const struct iio_info tiadc_info = {
406 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100407 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530408};
409
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800410static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530411{
412 struct iio_dev *indio_dev;
413 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000414 struct device_node *node = pdev->dev.of_node;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200415 struct property *prop;
416 const __be32 *cur;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530417 int err;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200418 u32 val;
419 int channels = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530420
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200421 if (!node) {
422 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530423 return -EINVAL;
424 }
425
Sachin Kamata0648132013-07-23 09:46:00 +0100426 indio_dev = devm_iio_device_alloc(&pdev->dev,
427 sizeof(struct tiadc_device));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530428 if (indio_dev == NULL) {
429 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100430 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530431 }
432 adc_dev = iio_priv(indio_dev);
433
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000434 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
435
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200436 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
437 adc_dev->channel_line[channels] = val;
438 channels++;
439 }
440 adc_dev->channels = channels;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530441
442 indio_dev->dev.parent = &pdev->dev;
443 indio_dev->name = dev_name(&pdev->dev);
444 indio_dev->modes = INDIO_DIRECT_MODE;
445 indio_dev->info = &tiadc_info;
446
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100447 tiadc_step_config(indio_dev);
448 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530449
450 err = tiadc_channel_init(indio_dev, adc_dev->channels);
451 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100452 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530453
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100454 err = tiadc_iio_buffered_hardware_setup(indio_dev,
455 &tiadc_worker_h,
456 &tiadc_irq_h,
457 adc_dev->mfd_tscadc->irq,
458 IRQF_SHARED,
459 &tiadc_buffer_setup_ops);
460
Patil, Rachna5e53a692012-10-16 12:55:45 +0530461 if (err)
462 goto err_free_channels;
463
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100464 err = iio_device_register(indio_dev);
465 if (err)
466 goto err_buffer_unregister;
467
Patil, Rachna5e53a692012-10-16 12:55:45 +0530468 platform_set_drvdata(pdev, indio_dev);
469
470 return 0;
471
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100472err_buffer_unregister:
473 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530474err_free_channels:
475 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530476 return err;
477}
478
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800479static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530480{
481 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000482 struct tiadc_device *adc_dev = iio_priv(indio_dev);
483 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530484
485 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100486 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530487 tiadc_channels_remove(indio_dev);
488
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000489 step_en = get_adc_step_mask(adc_dev);
490 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
491
Patil, Rachna5e53a692012-10-16 12:55:45 +0530492 return 0;
493}
494
495#ifdef CONFIG_PM
496static int tiadc_suspend(struct device *dev)
497{
498 struct iio_dev *indio_dev = dev_get_drvdata(dev);
499 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200500 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530501 unsigned int idle;
502
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200503 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530504 if (!device_may_wakeup(tscadc_dev->dev)) {
505 idle = tiadc_readl(adc_dev, REG_CTRL);
506 idle &= ~(CNTRLREG_TSCSSENB);
507 tiadc_writel(adc_dev, REG_CTRL, (idle |
508 CNTRLREG_POWERDOWN));
509 }
510
511 return 0;
512}
513
514static int tiadc_resume(struct device *dev)
515{
516 struct iio_dev *indio_dev = dev_get_drvdata(dev);
517 struct tiadc_device *adc_dev = iio_priv(indio_dev);
518 unsigned int restore;
519
520 /* Make sure ADC is powered up */
521 restore = tiadc_readl(adc_dev, REG_CTRL);
522 restore &= ~(CNTRLREG_POWERDOWN);
523 tiadc_writel(adc_dev, REG_CTRL, restore);
524
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100525 tiadc_step_config(indio_dev);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100526 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
527 adc_dev->buffer_en_ch_steps);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530528 return 0;
529}
530
531static const struct dev_pm_ops tiadc_pm_ops = {
532 .suspend = tiadc_suspend,
533 .resume = tiadc_resume,
534};
535#define TIADC_PM_OPS (&tiadc_pm_ops)
536#else
537#define TIADC_PM_OPS NULL
538#endif
539
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000540static const struct of_device_id ti_adc_dt_ids[] = {
541 { .compatible = "ti,am3359-adc", },
542 { }
543};
544MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
545
Patil, Rachna5e53a692012-10-16 12:55:45 +0530546static struct platform_driver tiadc_driver = {
547 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200548 .name = "TI-am335x-adc",
Patil, Rachna5e53a692012-10-16 12:55:45 +0530549 .owner = THIS_MODULE,
550 .pm = TIADC_PM_OPS,
Sachin Kamatde06b342013-10-21 10:27:00 +0100551 .of_match_table = ti_adc_dt_ids,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530552 },
553 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800554 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530555};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530556module_platform_driver(tiadc_driver);
557
558MODULE_DESCRIPTION("TI ADC controller driver");
559MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
560MODULE_LICENSE("GPL");