blob: 42e444044ea5b1b6a97d03ae6a351da3b996fe83 [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];
Patil, Rachna5e53a692012-10-16 12:55:45 +053040};
41
42static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
43{
44 return readl(adc->mfd_tscadc->tscadc_base + reg);
45}
46
47static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
48 unsigned int val)
49{
50 writel(val, adc->mfd_tscadc->tscadc_base + reg);
51}
52
Patil, Rachnaabeccee2013-01-24 03:45:05 +000053static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
54{
55 u32 step_en;
56
57 step_en = ((1 << adc_dev->channels) - 1);
58 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
59 return step_en;
60}
61
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +010062static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
63 struct iio_chan_spec const *chan)
64{
65 int i;
66
67 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
68 if (chan->channel == adc_dev->channel_line[i]) {
69 u32 step;
70
71 step = adc_dev->channel_step[i];
72 /* +1 for the charger */
73 return 1 << (step + 1);
74 }
75 }
76 WARN_ON(1);
77 return 0;
78}
79
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010080static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
Patil, Rachna5e53a692012-10-16 12:55:45 +053081{
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010082 return 1 << adc_dev->channel_step[chan];
83}
84
85static void tiadc_step_config(struct iio_dev *indio_dev)
86{
87 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +053088 unsigned int stepconfig;
Brad Griffis3a596842015-02-03 11:41:58 -080089 int i, steps = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +053090
91 /*
92 * There are 16 configurable steps and 8 analog input
93 * lines available which are shared between Touchscreen and ADC.
94 *
Brad Griffis3a596842015-02-03 11:41:58 -080095 * Steps forwards i.e. from 0 towards 16 are used by ADC
Patil, Rachna5e53a692012-10-16 12:55:45 +053096 * depending on number of input lines needed.
97 * Channel would represent which analog input
98 * needs to be given to ADC to digitalize data.
99 */
100
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100101 if (iio_buffer_enabled(indio_dev))
102 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
103 | STEPCONFIG_MODE_SWCNT;
104 else
105 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530106
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200107 for (i = 0; i < adc_dev->channels; i++) {
108 int chan;
109
110 chan = adc_dev->channel_line[i];
111 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
112 stepconfig | STEPCONFIG_INP(chan));
113 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Patil, Rachna5e53a692012-10-16 12:55:45 +0530114 STEPCONFIG_OPENDLY);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200115 adc_dev->channel_step[i] = steps;
116 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530117 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530118}
119
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100120static irqreturn_t tiadc_irq_h(int irq, void *private)
121{
122 struct iio_dev *indio_dev = private;
123 struct tiadc_device *adc_dev = iio_priv(indio_dev);
124 unsigned int status, config;
125 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
126
127 /*
128 * ADC and touchscreen share the IRQ line.
129 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
130 */
131 if (status & IRQENB_FIFO1OVRRUN) {
132 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
133 config = tiadc_readl(adc_dev, REG_CTRL);
134 config &= ~(CNTRLREG_TSCSSENB);
135 tiadc_writel(adc_dev, REG_CTRL, config);
136 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
137 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
138 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
139 return IRQ_HANDLED;
140 } else if (status & IRQENB_FIFO1THRES) {
141 /* Disable irq and wake worker thread */
142 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
143 return IRQ_WAKE_THREAD;
144 }
145
146 return IRQ_NONE;
147}
148
149static irqreturn_t tiadc_worker_h(int irq, void *private)
150{
151 struct iio_dev *indio_dev = private;
152 struct tiadc_device *adc_dev = iio_priv(indio_dev);
153 int i, k, fifo1count, read;
154 u16 *data = adc_dev->data;
155
156 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
157 for (k = 0; k < fifo1count; k = k + i) {
158 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
159 read = tiadc_readl(adc_dev, REG_FIFO1);
160 data[i] = read & FIFOREAD_DATA_MASK;
161 }
162 iio_push_to_buffers(indio_dev, (u8 *) data);
163 }
164
165 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
166 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
167
168 return IRQ_HANDLED;
169}
170
171static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
172{
173 struct tiadc_device *adc_dev = iio_priv(indio_dev);
174 int i, fifo1count, read;
175
176 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
177 IRQENB_FIFO1OVRRUN |
178 IRQENB_FIFO1UNDRFLW));
179
180 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
181 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
182 for (i = 0; i < fifo1count; i++)
183 read = tiadc_readl(adc_dev, REG_FIFO1);
184
Lars-Peter Clausen24adaf72013-10-14 17:49:00 +0100185 return 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100186}
187
188static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
189{
190 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100191 unsigned int enb = 0;
192 u8 bit;
193
194 tiadc_step_config(indio_dev);
Octavian Purdila70dddee2015-03-02 21:03:05 +0200195 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100196 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
197 adc_dev->buffer_en_ch_steps = enb;
198
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100199 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100200
201 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
202 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
203 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES
204 | IRQENB_FIFO1OVRRUN);
205
206 return 0;
207}
208
209static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
210{
211 struct tiadc_device *adc_dev = iio_priv(indio_dev);
212 int fifo1count, i, read;
213
214 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
215 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
216 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
Sebastian Andrzej Siewior3954b7b2013-12-19 16:28:30 +0100217 adc_dev->buffer_en_ch_steps = 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100218
219 /* Flush FIFO of leftover data in the time it takes to disable adc */
220 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
221 for (i = 0; i < fifo1count; i++)
222 read = tiadc_readl(adc_dev, REG_FIFO1);
223
224 return 0;
225}
226
227static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
228{
229 tiadc_step_config(indio_dev);
230
231 return 0;
232}
233
234static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
235 .preenable = &tiadc_buffer_preenable,
236 .postenable = &tiadc_buffer_postenable,
237 .predisable = &tiadc_buffer_predisable,
238 .postdisable = &tiadc_buffer_postdisable,
239};
240
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100241static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100242 irqreturn_t (*pollfunc_bh)(int irq, void *p),
243 irqreturn_t (*pollfunc_th)(int irq, void *p),
244 int irq,
245 unsigned long flags,
246 const struct iio_buffer_setup_ops *setup_ops)
247{
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100248 struct iio_buffer *buffer;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100249 int ret;
250
Karol Wrona7ab374a2014-12-19 18:39:24 +0100251 buffer = iio_kfifo_allocate();
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100252 if (!buffer)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100253 return -ENOMEM;
254
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100255 iio_device_attach_buffer(indio_dev, buffer);
256
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100257 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
258 flags, indio_dev->name, indio_dev);
259 if (ret)
260 goto error_kfifo_free;
261
262 indio_dev->setup_ops = setup_ops;
263 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
264
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100265 return 0;
266
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100267error_kfifo_free:
268 iio_kfifo_free(indio_dev->buffer);
269 return ret;
270}
271
272static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
273{
274 struct tiadc_device *adc_dev = iio_priv(indio_dev);
275
276 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
277 iio_kfifo_free(indio_dev->buffer);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100278}
279
280
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300281static const char * const chan_name_ain[] = {
282 "AIN0",
283 "AIN1",
284 "AIN2",
285 "AIN3",
286 "AIN4",
287 "AIN5",
288 "AIN6",
289 "AIN7",
290};
291
Patil, Rachna5e53a692012-10-16 12:55:45 +0530292static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
293{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300294 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530295 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300296 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530297 int i;
298
299 indio_dev->num_channels = channels;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300300 chan_array = kcalloc(channels,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530301 sizeof(struct iio_chan_spec), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530302 if (chan_array == NULL)
303 return -ENOMEM;
304
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300305 chan = chan_array;
306 for (i = 0; i < channels; i++, chan++) {
307
Patil, Rachna5e53a692012-10-16 12:55:45 +0530308 chan->type = IIO_VOLTAGE;
309 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200310 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000311 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200312 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100313 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300314 chan->scan_type.sign = 'u';
315 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100316 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530317 }
318
319 indio_dev->channels = chan_array;
320
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300321 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530322}
323
324static void tiadc_channels_remove(struct iio_dev *indio_dev)
325{
326 kfree(indio_dev->channels);
327}
328
329static int tiadc_read_raw(struct iio_dev *indio_dev,
330 struct iio_chan_spec const *chan,
331 int *val, int *val2, long mask)
332{
333 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100334 int i, map_val;
335 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200336 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100337 u32 step_en;
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100338 unsigned long timeout;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100339
340 if (iio_buffer_enabled(indio_dev))
341 return -EBUSY;
342
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100343 step_en = get_adc_chan_step_mask(adc_dev, chan);
344 if (!step_en)
345 return -EINVAL;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100346
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100347 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
348 while (fifo1count--)
349 tiadc_readl(adc_dev, REG_FIFO1);
350
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100351 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100352
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100353 timeout = jiffies + usecs_to_jiffies
354 (IDLE_TIMEOUT * adc_dev->channels);
355 /* Wait for Fifo threshold interrupt */
356 while (1) {
357 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
358 if (fifo1count)
359 break;
360
361 if (time_after(jiffies, timeout)) {
362 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100363 return -EAGAIN;
364 }
Sebastian Andrzej Siewiorfb7f8ce2013-12-19 16:28:27 +0100365 }
Jan Kardellbaa3c652014-11-06 22:18:00 +0000366 map_val = adc_dev->channel_step[chan->scan_index];
Patil, Rachna5e53a692012-10-16 12:55:45 +0530367
368 /*
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100369 * We check the complete FIFO. We programmed just one entry but in case
370 * something went wrong we left empty handed (-EAGAIN previously) and
371 * then the value apeared somehow in the FIFO we would have two entries.
372 * Therefore we read every item and keep only the latest version of the
373 * requested channel.
Patil, Rachna5e53a692012-10-16 12:55:45 +0530374 */
Patil, Rachna5e53a692012-10-16 12:55:45 +0530375 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200376 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100377 stepid = read & FIFOREAD_CHNLID_MASK;
378 stepid = stepid >> 0x10;
379
380 if (stepid == map_val) {
381 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200382 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100383 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200384 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530385 }
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100386 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100387
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200388 if (found == false)
389 return -EBUSY;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530390 return IIO_VAL_INT;
391}
392
393static const struct iio_info tiadc_info = {
394 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100395 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530396};
397
Vignesh Rdee1f552015-03-31 16:42:36 +0530398static int tiadc_parse_dt(struct platform_device *pdev,
399 struct tiadc_device *adc_dev)
400{
401 struct device_node *node = pdev->dev.of_node;
402 struct property *prop;
403 const __be32 *cur;
404 int channels = 0;
405 u32 val;
406
407 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
408 adc_dev->channel_line[channels] = val;
409 channels++;
410 }
411
412 adc_dev->channels = channels;
413 return 0;
414}
415
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800416static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530417{
418 struct iio_dev *indio_dev;
419 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000420 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530421 int err;
422
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200423 if (!node) {
424 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530425 return -EINVAL;
426 }
427
Sachin Kamata0648132013-07-23 09:46:00 +0100428 indio_dev = devm_iio_device_alloc(&pdev->dev,
429 sizeof(struct tiadc_device));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530430 if (indio_dev == NULL) {
431 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100432 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530433 }
434 adc_dev = iio_priv(indio_dev);
435
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000436 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
Vignesh Rdee1f552015-03-31 16:42:36 +0530437 tiadc_parse_dt(pdev, adc_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530438
439 indio_dev->dev.parent = &pdev->dev;
440 indio_dev->name = dev_name(&pdev->dev);
441 indio_dev->modes = INDIO_DIRECT_MODE;
442 indio_dev->info = &tiadc_info;
443
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100444 tiadc_step_config(indio_dev);
445 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530446
447 err = tiadc_channel_init(indio_dev, adc_dev->channels);
448 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100449 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530450
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100451 err = tiadc_iio_buffered_hardware_setup(indio_dev,
452 &tiadc_worker_h,
453 &tiadc_irq_h,
454 adc_dev->mfd_tscadc->irq,
455 IRQF_SHARED,
456 &tiadc_buffer_setup_ops);
457
Patil, Rachna5e53a692012-10-16 12:55:45 +0530458 if (err)
459 goto err_free_channels;
460
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100461 err = iio_device_register(indio_dev);
462 if (err)
463 goto err_buffer_unregister;
464
Patil, Rachna5e53a692012-10-16 12:55:45 +0530465 platform_set_drvdata(pdev, indio_dev);
466
467 return 0;
468
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100469err_buffer_unregister:
470 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530471err_free_channels:
472 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530473 return err;
474}
475
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800476static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530477{
478 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000479 struct tiadc_device *adc_dev = iio_priv(indio_dev);
480 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530481
482 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100483 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530484 tiadc_channels_remove(indio_dev);
485
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000486 step_en = get_adc_step_mask(adc_dev);
487 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
488
Patil, Rachna5e53a692012-10-16 12:55:45 +0530489 return 0;
490}
491
492#ifdef CONFIG_PM
493static int tiadc_suspend(struct device *dev)
494{
495 struct iio_dev *indio_dev = dev_get_drvdata(dev);
496 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200497 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530498 unsigned int idle;
499
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200500 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530501 if (!device_may_wakeup(tscadc_dev->dev)) {
502 idle = tiadc_readl(adc_dev, REG_CTRL);
503 idle &= ~(CNTRLREG_TSCSSENB);
504 tiadc_writel(adc_dev, REG_CTRL, (idle |
505 CNTRLREG_POWERDOWN));
506 }
507
508 return 0;
509}
510
511static int tiadc_resume(struct device *dev)
512{
513 struct iio_dev *indio_dev = dev_get_drvdata(dev);
514 struct tiadc_device *adc_dev = iio_priv(indio_dev);
515 unsigned int restore;
516
517 /* Make sure ADC is powered up */
518 restore = tiadc_readl(adc_dev, REG_CTRL);
519 restore &= ~(CNTRLREG_POWERDOWN);
520 tiadc_writel(adc_dev, REG_CTRL, restore);
521
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100522 tiadc_step_config(indio_dev);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100523 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
524 adc_dev->buffer_en_ch_steps);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530525 return 0;
526}
527
528static const struct dev_pm_ops tiadc_pm_ops = {
529 .suspend = tiadc_suspend,
530 .resume = tiadc_resume,
531};
532#define TIADC_PM_OPS (&tiadc_pm_ops)
533#else
534#define TIADC_PM_OPS NULL
535#endif
536
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000537static const struct of_device_id ti_adc_dt_ids[] = {
538 { .compatible = "ti,am3359-adc", },
539 { }
540};
541MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
542
Patil, Rachna5e53a692012-10-16 12:55:45 +0530543static struct platform_driver tiadc_driver = {
544 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200545 .name = "TI-am335x-adc",
Patil, Rachna5e53a692012-10-16 12:55:45 +0530546 .pm = TIADC_PM_OPS,
Sachin Kamatde06b342013-10-21 10:27:00 +0100547 .of_match_table = ti_adc_dt_ids,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530548 },
549 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800550 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530551};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530552module_platform_driver(tiadc_driver);
553
554MODULE_DESCRIPTION("TI ADC controller driver");
555MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
556MODULE_LICENSE("GPL");