blob: ad9dec30bb304ffbbb0f67f01b753f7bc88cd891 [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
Mugunthan V Nf438b9d2016-10-05 14:34:41 +053033#include <linux/dmaengine.h>
34#include <linux/dma-mapping.h>
35
36#define DMA_BUFFER_SIZE SZ_2K
37
38struct tiadc_dma {
39 struct dma_slave_config conf;
40 struct dma_chan *chan;
41 dma_addr_t addr;
42 dma_cookie_t cookie;
43 u8 *buf;
44 int current_period;
45 int period_size;
46 u8 fifo_thresh;
47};
48
Patil, Rachna5e53a692012-10-16 12:55:45 +053049struct tiadc_device {
50 struct ti_tscadc_dev *mfd_tscadc;
Mugunthan V Nf438b9d2016-10-05 14:34:41 +053051 struct tiadc_dma dma;
Vignesh R90c43ec2016-08-17 17:43:00 +053052 struct mutex fifo1_lock; /* to protect fifo access */
Patil, Rachna5e53a692012-10-16 12:55:45 +053053 int channels;
Mugunthan V Nf438b9d2016-10-05 14:34:41 +053054 int total_ch_enabled;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +020055 u8 channel_line[8];
56 u8 channel_step[8];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010057 int buffer_en_ch_steps;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +010058 u16 data[8];
Vignesh R5dc11e82015-03-31 16:42:37 +053059 u32 open_delay[8], sample_delay[8], step_avg[8];
Patil, Rachna5e53a692012-10-16 12:55:45 +053060};
61
62static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
63{
64 return readl(adc->mfd_tscadc->tscadc_base + reg);
65}
66
67static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
68 unsigned int val)
69{
70 writel(val, adc->mfd_tscadc->tscadc_base + reg);
71}
72
Patil, Rachnaabeccee2013-01-24 03:45:05 +000073static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
74{
75 u32 step_en;
76
77 step_en = ((1 << adc_dev->channels) - 1);
78 step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
79 return step_en;
80}
81
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +010082static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
83 struct iio_chan_spec const *chan)
84{
85 int i;
86
87 for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
88 if (chan->channel == adc_dev->channel_line[i]) {
89 u32 step;
90
91 step = adc_dev->channel_step[i];
92 /* +1 for the charger */
93 return 1 << (step + 1);
94 }
95 }
96 WARN_ON(1);
97 return 0;
98}
99
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100100static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530101{
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100102 return 1 << adc_dev->channel_step[chan];
103}
104
105static void tiadc_step_config(struct iio_dev *indio_dev)
106{
107 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Vignesh R5dc11e82015-03-31 16:42:37 +0530108 struct device *dev = adc_dev->mfd_tscadc->dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530109 unsigned int stepconfig;
Brad Griffis3a596842015-02-03 11:41:58 -0800110 int i, steps = 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530111
112 /*
113 * There are 16 configurable steps and 8 analog input
114 * lines available which are shared between Touchscreen and ADC.
115 *
Brad Griffis3a596842015-02-03 11:41:58 -0800116 * Steps forwards i.e. from 0 towards 16 are used by ADC
Patil, Rachna5e53a692012-10-16 12:55:45 +0530117 * depending on number of input lines needed.
118 * Channel would represent which analog input
119 * needs to be given to ADC to digitalize data.
120 */
121
Patil, Rachna5e53a692012-10-16 12:55:45 +0530122
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200123 for (i = 0; i < adc_dev->channels; i++) {
124 int chan;
125
126 chan = adc_dev->channel_line[i];
Vignesh R5dc11e82015-03-31 16:42:37 +0530127
128 if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) {
129 dev_warn(dev, "chan %d step_avg truncating to %d\n",
130 chan, STEPCONFIG_AVG_16);
131 adc_dev->step_avg[i] = STEPCONFIG_AVG_16;
132 }
133
134 if (adc_dev->step_avg[i])
135 stepconfig =
136 STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) |
137 STEPCONFIG_FIFO1;
138 else
139 stepconfig = STEPCONFIG_FIFO1;
140
141 if (iio_buffer_enabled(indio_dev))
142 stepconfig |= STEPCONFIG_MODE_SWCNT;
143
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200144 tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
145 stepconfig | STEPCONFIG_INP(chan));
Vignesh R5dc11e82015-03-31 16:42:37 +0530146
147 if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) {
148 dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n",
149 chan);
150 adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK;
151 }
152
153 if (adc_dev->sample_delay[i] > 0xFF) {
154 dev_warn(dev, "chan %d sample delay truncating to 0xFF\n",
155 chan);
156 adc_dev->sample_delay[i] = 0xFF;
157 }
158
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200159 tiadc_writel(adc_dev, REG_STEPDELAY(steps),
Vignesh R5dc11e82015-03-31 16:42:37 +0530160 STEPDELAY_OPEN(adc_dev->open_delay[i]) |
161 STEPDELAY_SAMPLE(adc_dev->sample_delay[i]));
162
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200163 adc_dev->channel_step[i] = steps;
164 steps++;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530165 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530166}
167
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100168static irqreturn_t tiadc_irq_h(int irq, void *private)
169{
170 struct iio_dev *indio_dev = private;
171 struct tiadc_device *adc_dev = iio_priv(indio_dev);
172 unsigned int status, config;
173 status = tiadc_readl(adc_dev, REG_IRQSTATUS);
174
175 /*
176 * ADC and touchscreen share the IRQ line.
177 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
178 */
179 if (status & IRQENB_FIFO1OVRRUN) {
180 /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
181 config = tiadc_readl(adc_dev, REG_CTRL);
182 config &= ~(CNTRLREG_TSCSSENB);
183 tiadc_writel(adc_dev, REG_CTRL, config);
184 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
185 | IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
186 tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
187 return IRQ_HANDLED;
188 } else if (status & IRQENB_FIFO1THRES) {
189 /* Disable irq and wake worker thread */
190 tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
191 return IRQ_WAKE_THREAD;
192 }
193
194 return IRQ_NONE;
195}
196
197static irqreturn_t tiadc_worker_h(int irq, void *private)
198{
199 struct iio_dev *indio_dev = private;
200 struct tiadc_device *adc_dev = iio_priv(indio_dev);
201 int i, k, fifo1count, read;
202 u16 *data = adc_dev->data;
203
204 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
205 for (k = 0; k < fifo1count; k = k + i) {
206 for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
207 read = tiadc_readl(adc_dev, REG_FIFO1);
208 data[i] = read & FIFOREAD_DATA_MASK;
209 }
210 iio_push_to_buffers(indio_dev, (u8 *) data);
211 }
212
213 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
214 tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
215
216 return IRQ_HANDLED;
217}
218
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530219static void tiadc_dma_rx_complete(void *param)
220{
221 struct iio_dev *indio_dev = param;
222 struct tiadc_device *adc_dev = iio_priv(indio_dev);
223 struct tiadc_dma *dma = &adc_dev->dma;
224 u8 *data;
225 int i;
226
227 data = dma->buf + dma->current_period * dma->period_size;
228 dma->current_period = 1 - dma->current_period; /* swap the buffer ID */
229
230 for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) {
231 iio_push_to_buffers(indio_dev, data);
232 data += indio_dev->scan_bytes;
233 }
234}
235
236static int tiadc_start_dma(struct iio_dev *indio_dev)
237{
238 struct tiadc_device *adc_dev = iio_priv(indio_dev);
239 struct tiadc_dma *dma = &adc_dev->dma;
240 struct dma_async_tx_descriptor *desc;
241
242 dma->current_period = 0; /* We start to fill period 0 */
243 /*
244 * Make the fifo thresh as the multiple of total number of
245 * channels enabled, so make sure that cyclic DMA period
246 * length is also a multiple of total number of channels
247 * enabled. This ensures that no invalid data is reported
248 * to the stack via iio_push_to_buffers().
249 */
250 dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1,
251 adc_dev->total_ch_enabled) - 1;
252 /* Make sure that period length is multiple of fifo thresh level */
253 dma->period_size = rounddown(DMA_BUFFER_SIZE / 2,
254 (dma->fifo_thresh + 1) * sizeof(u16));
255
256 dma->conf.src_maxburst = dma->fifo_thresh + 1;
257 dmaengine_slave_config(dma->chan, &dma->conf);
258
259 desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr,
260 dma->period_size * 2,
261 dma->period_size, DMA_DEV_TO_MEM,
262 DMA_PREP_INTERRUPT);
263 if (!desc)
264 return -EBUSY;
265
266 desc->callback = tiadc_dma_rx_complete;
267 desc->callback_param = indio_dev;
268
269 dma->cookie = dmaengine_submit(desc);
270
271 dma_async_issue_pending(dma->chan);
272
273 tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh);
274 tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh);
275 tiadc_writel(adc_dev, REG_DMAENABLE_SET, DMA_FIFO1);
276
277 return 0;
278}
279
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100280static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
281{
282 struct tiadc_device *adc_dev = iio_priv(indio_dev);
283 int i, fifo1count, read;
284
285 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
286 IRQENB_FIFO1OVRRUN |
287 IRQENB_FIFO1UNDRFLW));
288
289 /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
290 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
291 for (i = 0; i < fifo1count; i++)
292 read = tiadc_readl(adc_dev, REG_FIFO1);
293
Lars-Peter Clausen24adaf72013-10-14 17:49:00 +0100294 return 0;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100295}
296
297static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
298{
299 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530300 struct tiadc_dma *dma = &adc_dev->dma;
301 unsigned int irq_enable;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100302 unsigned int enb = 0;
303 u8 bit;
304
305 tiadc_step_config(indio_dev);
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530306 for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) {
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100307 enb |= (get_adc_step_bit(adc_dev, bit) << 1);
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530308 adc_dev->total_ch_enabled++;
309 }
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100310 adc_dev->buffer_en_ch_steps = enb;
311
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530312 if (dma->chan)
313 tiadc_start_dma(indio_dev);
314
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100315 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100316
317 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES
318 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530319
320 irq_enable = IRQENB_FIFO1OVRRUN;
321 if (!dma->chan)
322 irq_enable |= IRQENB_FIFO1THRES;
323 tiadc_writel(adc_dev, REG_IRQENABLE, irq_enable);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100324
325 return 0;
326}
327
328static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
329{
330 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530331 struct tiadc_dma *dma = &adc_dev->dma;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100332 int fifo1count, i, read;
333
334 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
335 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
336 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
Sebastian Andrzej Siewior3954b7b2013-12-19 16:28:30 +0100337 adc_dev->buffer_en_ch_steps = 0;
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530338 adc_dev->total_ch_enabled = 0;
339 if (dma->chan) {
340 tiadc_writel(adc_dev, REG_DMAENABLE_CLEAR, 0x2);
341 dmaengine_terminate_async(dma->chan);
342 }
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100343
344 /* Flush FIFO of leftover data in the time it takes to disable adc */
345 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
346 for (i = 0; i < fifo1count; i++)
347 read = tiadc_readl(adc_dev, REG_FIFO1);
348
349 return 0;
350}
351
352static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
353{
354 tiadc_step_config(indio_dev);
355
356 return 0;
357}
358
359static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
360 .preenable = &tiadc_buffer_preenable,
361 .postenable = &tiadc_buffer_postenable,
362 .predisable = &tiadc_buffer_predisable,
363 .postdisable = &tiadc_buffer_postdisable,
364};
365
Zubair Lutfullah98c08cf2013-09-22 09:20:00 +0100366static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100367 irqreturn_t (*pollfunc_bh)(int irq, void *p),
368 irqreturn_t (*pollfunc_th)(int irq, void *p),
369 int irq,
370 unsigned long flags,
371 const struct iio_buffer_setup_ops *setup_ops)
372{
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100373 struct iio_buffer *buffer;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100374 int ret;
375
Karol Wrona7ab374a2014-12-19 18:39:24 +0100376 buffer = iio_kfifo_allocate();
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100377 if (!buffer)
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100378 return -ENOMEM;
379
Lars-Peter Clausenfe269802013-10-24 10:41:00 +0100380 iio_device_attach_buffer(indio_dev, buffer);
381
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100382 ret = request_threaded_irq(irq, pollfunc_th, pollfunc_bh,
383 flags, indio_dev->name, indio_dev);
384 if (ret)
385 goto error_kfifo_free;
386
387 indio_dev->setup_ops = setup_ops;
Jonathan Cameron9d0be852016-01-01 18:05:34 +0000388 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100389
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100390 return 0;
391
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100392error_kfifo_free:
393 iio_kfifo_free(indio_dev->buffer);
394 return ret;
395}
396
397static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
398{
399 struct tiadc_device *adc_dev = iio_priv(indio_dev);
400
401 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
402 iio_kfifo_free(indio_dev->buffer);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100403}
404
405
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300406static const char * const chan_name_ain[] = {
407 "AIN0",
408 "AIN1",
409 "AIN2",
410 "AIN3",
411 "AIN4",
412 "AIN5",
413 "AIN6",
414 "AIN7",
415};
416
Patil, Rachna5e53a692012-10-16 12:55:45 +0530417static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
418{
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300419 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530420 struct iio_chan_spec *chan_array;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300421 struct iio_chan_spec *chan;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530422 int i;
423
424 indio_dev->num_channels = channels;
Andrew F. Davisfea89e22016-05-31 12:00:12 -0500425 chan_array = kcalloc(channels, sizeof(*chan_array), GFP_KERNEL);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530426 if (chan_array == NULL)
427 return -ENOMEM;
428
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300429 chan = chan_array;
430 for (i = 0; i < channels; i++, chan++) {
431
Patil, Rachna5e53a692012-10-16 12:55:45 +0530432 chan->type = IIO_VOLTAGE;
433 chan->indexed = 1;
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200434 chan->channel = adc_dev->channel_line[i];
Jonathan Cameron6c572522013-02-27 19:07:18 +0000435 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200436 chan->datasheet_name = chan_name_ain[chan->channel];
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100437 chan->scan_index = i;
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300438 chan->scan_type.sign = 'u';
439 chan->scan_type.realbits = 12;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100440 chan->scan_type.storagebits = 16;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530441 }
442
443 indio_dev->channels = chan_array;
444
Pantelis Antoniouc80df482012-10-13 16:37:24 +0300445 return 0;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530446}
447
448static void tiadc_channels_remove(struct iio_dev *indio_dev)
449{
450 kfree(indio_dev->channels);
451}
452
453static int tiadc_read_raw(struct iio_dev *indio_dev,
454 struct iio_chan_spec const *chan,
455 int *val, int *val2, long mask)
456{
457 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Vignesh R90c43ec2016-08-17 17:43:00 +0530458 int ret = IIO_VAL_INT;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100459 int i, map_val;
460 unsigned int fifo1count, read, stepid;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200461 bool found = false;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100462 u32 step_en;
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100463 unsigned long timeout;
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100464
465 if (iio_buffer_enabled(indio_dev))
466 return -EBUSY;
467
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100468 step_en = get_adc_chan_step_mask(adc_dev, chan);
469 if (!step_en)
470 return -EINVAL;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100471
Vignesh R90c43ec2016-08-17 17:43:00 +0530472 mutex_lock(&adc_dev->fifo1_lock);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100473 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
474 while (fifo1count--)
475 tiadc_readl(adc_dev, REG_FIFO1);
476
Sebastian Andrzej Siewior7e170c62013-12-19 16:28:29 +0100477 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100478
Vignesh R7175cce2016-08-17 17:43:01 +0530479 timeout = jiffies + msecs_to_jiffies
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100480 (IDLE_TIMEOUT * adc_dev->channels);
481 /* Wait for Fifo threshold interrupt */
482 while (1) {
483 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
484 if (fifo1count)
485 break;
486
487 if (time_after(jiffies, timeout)) {
488 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Vignesh R90c43ec2016-08-17 17:43:00 +0530489 ret = -EAGAIN;
490 goto err_unlock;
Patil, Rachnab1451e52013-07-20 17:27:00 +0100491 }
Sebastian Andrzej Siewiorfb7f8ce2013-12-19 16:28:27 +0100492 }
Jan Kardellbaa3c652014-11-06 22:18:00 +0000493 map_val = adc_dev->channel_step[chan->scan_index];
Patil, Rachna5e53a692012-10-16 12:55:45 +0530494
495 /*
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100496 * We check the complete FIFO. We programmed just one entry but in case
497 * something went wrong we left empty handed (-EAGAIN previously) and
498 * then the value apeared somehow in the FIFO we would have two entries.
499 * Therefore we read every item and keep only the latest version of the
500 * requested channel.
Patil, Rachna5e53a692012-10-16 12:55:45 +0530501 */
Patil, Rachna5e53a692012-10-16 12:55:45 +0530502 for (i = 0; i < fifo1count; i++) {
Sebastian Andrzej Siewior18926ed2013-05-29 17:39:02 +0200503 read = tiadc_readl(adc_dev, REG_FIFO1);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100504 stepid = read & FIFOREAD_CHNLID_MASK;
505 stepid = stepid >> 0x10;
506
507 if (stepid == map_val) {
508 read = read & FIFOREAD_DATA_MASK;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200509 found = true;
Zubair Lutfullah0f6fc7d2013-09-19 07:24:00 +0100510 *val = (u16) read;
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200511 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530512 }
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100513 am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
Patil, Rachnab1451e52013-07-20 17:27:00 +0100514
Sebastian Andrzej Siewior1460c152013-05-29 18:49:55 +0200515 if (found == false)
Vignesh R90c43ec2016-08-17 17:43:00 +0530516 ret = -EBUSY;
517
518err_unlock:
519 mutex_unlock(&adc_dev->fifo1_lock);
520 return ret;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530521}
522
523static const struct iio_info tiadc_info = {
524 .read_raw = &tiadc_read_raw,
Wei Yongjunbc93aa72013-07-06 05:20:00 +0100525 .driver_module = THIS_MODULE,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530526};
527
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530528static int tiadc_request_dma(struct platform_device *pdev,
529 struct tiadc_device *adc_dev)
530{
531 struct tiadc_dma *dma = &adc_dev->dma;
532 dma_cap_mask_t mask;
533
534 /* Default slave configuration parameters */
535 dma->conf.direction = DMA_DEV_TO_MEM;
536 dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
537 dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1;
538
539 dma_cap_zero(mask);
540 dma_cap_set(DMA_CYCLIC, mask);
541
542 /* Get a channel for RX */
543 dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1");
544 if (IS_ERR(dma->chan)) {
545 int ret = PTR_ERR(dma->chan);
546
547 dma->chan = NULL;
548 return ret;
549 }
550
551 /* RX buffer */
552 dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
553 &dma->addr, GFP_KERNEL);
554 if (!dma->buf)
555 goto err;
556
557 return 0;
558err:
559 dma_release_channel(dma->chan);
560 return -ENOMEM;
561}
562
Vignesh Rdee1f552015-03-31 16:42:36 +0530563static int tiadc_parse_dt(struct platform_device *pdev,
564 struct tiadc_device *adc_dev)
565{
566 struct device_node *node = pdev->dev.of_node;
567 struct property *prop;
568 const __be32 *cur;
569 int channels = 0;
570 u32 val;
571
572 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
573 adc_dev->channel_line[channels] = val;
Vignesh R5dc11e82015-03-31 16:42:37 +0530574
575 /* Set Default values for optional DT parameters */
576 adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY;
577 adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY;
578 adc_dev->step_avg[channels] = 16;
579
Vignesh Rdee1f552015-03-31 16:42:36 +0530580 channels++;
581 }
582
Vignesh R5dc11e82015-03-31 16:42:37 +0530583 of_property_read_u32_array(node, "ti,chan-step-avg",
584 adc_dev->step_avg, channels);
585 of_property_read_u32_array(node, "ti,chan-step-opendelay",
586 adc_dev->open_delay, channels);
587 of_property_read_u32_array(node, "ti,chan-step-sampledelay",
588 adc_dev->sample_delay, channels);
589
Vignesh Rdee1f552015-03-31 16:42:36 +0530590 adc_dev->channels = channels;
591 return 0;
592}
593
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800594static int tiadc_probe(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530595{
596 struct iio_dev *indio_dev;
597 struct tiadc_device *adc_dev;
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000598 struct device_node *node = pdev->dev.of_node;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530599 int err;
600
Sebastian Andrzej Siewior0ead4fb2013-05-21 17:49:22 +0200601 if (!node) {
602 dev_err(&pdev->dev, "Could not find valid DT data.\n");
Patil, Rachna5e53a692012-10-16 12:55:45 +0530603 return -EINVAL;
604 }
605
Andrew F. Davisfea89e22016-05-31 12:00:12 -0500606 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*indio_dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530607 if (indio_dev == NULL) {
608 dev_err(&pdev->dev, "failed to allocate iio device\n");
Sachin Kamata0648132013-07-23 09:46:00 +0100609 return -ENOMEM;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530610 }
611 adc_dev = iio_priv(indio_dev);
612
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000613 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
Vignesh Rdee1f552015-03-31 16:42:36 +0530614 tiadc_parse_dt(pdev, adc_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530615
616 indio_dev->dev.parent = &pdev->dev;
617 indio_dev->name = dev_name(&pdev->dev);
618 indio_dev->modes = INDIO_DIRECT_MODE;
619 indio_dev->info = &tiadc_info;
620
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100621 tiadc_step_config(indio_dev);
622 tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
Vignesh R90c43ec2016-08-17 17:43:00 +0530623 mutex_init(&adc_dev->fifo1_lock);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530624
625 err = tiadc_channel_init(indio_dev, adc_dev->channels);
626 if (err < 0)
Sachin Kamata0648132013-07-23 09:46:00 +0100627 return err;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530628
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100629 err = tiadc_iio_buffered_hardware_setup(indio_dev,
630 &tiadc_worker_h,
631 &tiadc_irq_h,
632 adc_dev->mfd_tscadc->irq,
633 IRQF_SHARED,
634 &tiadc_buffer_setup_ops);
635
Patil, Rachna5e53a692012-10-16 12:55:45 +0530636 if (err)
637 goto err_free_channels;
638
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100639 err = iio_device_register(indio_dev);
640 if (err)
641 goto err_buffer_unregister;
642
Patil, Rachna5e53a692012-10-16 12:55:45 +0530643 platform_set_drvdata(pdev, indio_dev);
644
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530645 err = tiadc_request_dma(pdev, adc_dev);
646 if (err && err == -EPROBE_DEFER)
647 goto err_dma;
648
Patil, Rachna5e53a692012-10-16 12:55:45 +0530649 return 0;
650
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530651err_dma:
652 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100653err_buffer_unregister:
654 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530655err_free_channels:
656 tiadc_channels_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530657 return err;
658}
659
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800660static int tiadc_remove(struct platform_device *pdev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530661{
662 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000663 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530664 struct tiadc_dma *dma = &adc_dev->dma;
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000665 u32 step_en;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530666
Mugunthan V Nf438b9d2016-10-05 14:34:41 +0530667 if (dma->chan) {
668 dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE,
669 dma->buf, dma->addr);
670 dma_release_channel(dma->chan);
671 }
Patil, Rachna5e53a692012-10-16 12:55:45 +0530672 iio_device_unregister(indio_dev);
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100673 tiadc_iio_buffered_hardware_remove(indio_dev);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530674 tiadc_channels_remove(indio_dev);
675
Patil, Rachnaabeccee2013-01-24 03:45:05 +0000676 step_en = get_adc_step_mask(adc_dev);
677 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
678
Patil, Rachna5e53a692012-10-16 12:55:45 +0530679 return 0;
680}
681
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500682static int __maybe_unused tiadc_suspend(struct device *dev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530683{
684 struct iio_dev *indio_dev = dev_get_drvdata(dev);
685 struct tiadc_device *adc_dev = iio_priv(indio_dev);
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200686 struct ti_tscadc_dev *tscadc_dev;
Patil, Rachna5e53a692012-10-16 12:55:45 +0530687 unsigned int idle;
688
Sebastian Andrzej Siewiora9bce1b2013-06-05 16:13:47 +0200689 tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
Patil, Rachna5e53a692012-10-16 12:55:45 +0530690 if (!device_may_wakeup(tscadc_dev->dev)) {
691 idle = tiadc_readl(adc_dev, REG_CTRL);
692 idle &= ~(CNTRLREG_TSCSSENB);
693 tiadc_writel(adc_dev, REG_CTRL, (idle |
694 CNTRLREG_POWERDOWN));
695 }
696
697 return 0;
698}
699
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500700static int __maybe_unused tiadc_resume(struct device *dev)
Patil, Rachna5e53a692012-10-16 12:55:45 +0530701{
702 struct iio_dev *indio_dev = dev_get_drvdata(dev);
703 struct tiadc_device *adc_dev = iio_priv(indio_dev);
704 unsigned int restore;
705
706 /* Make sure ADC is powered up */
707 restore = tiadc_readl(adc_dev, REG_CTRL);
708 restore &= ~(CNTRLREG_POWERDOWN);
709 tiadc_writel(adc_dev, REG_CTRL, restore);
710
Zubair Lutfullahca9a5632013-09-19 07:24:00 +0100711 tiadc_step_config(indio_dev);
Sebastian Andrzej Siewior7ca67402013-12-19 16:28:31 +0100712 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
713 adc_dev->buffer_en_ch_steps);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530714 return 0;
715}
716
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500717static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume);
Patil, Rachna5e53a692012-10-16 12:55:45 +0530718
Patil, Rachna6f39ac42013-01-24 03:45:11 +0000719static const struct of_device_id ti_adc_dt_ids[] = {
720 { .compatible = "ti,am3359-adc", },
721 { }
722};
723MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
724
Patil, Rachna5e53a692012-10-16 12:55:45 +0530725static struct platform_driver tiadc_driver = {
726 .driver = {
Sebastian Andrzej Siewior9f999282013-05-27 17:12:52 +0200727 .name = "TI-am335x-adc",
Andrew F. Davis27aa8322016-05-31 12:00:07 -0500728 .pm = &tiadc_pm_ops,
Sachin Kamatde06b342013-10-21 10:27:00 +0100729 .of_match_table = ti_adc_dt_ids,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530730 },
731 .probe = tiadc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800732 .remove = tiadc_remove,
Patil, Rachna5e53a692012-10-16 12:55:45 +0530733};
Patil, Rachna5e53a692012-10-16 12:55:45 +0530734module_platform_driver(tiadc_driver);
735
736MODULE_DESCRIPTION("TI ADC controller driver");
737MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
738MODULE_LICENSE("GPL");