Commit | Line | Data |
---|---|---|
aaf71200 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5e53a69b PR |
2 | /* |
3 | * TI ADC MFD driver | |
4 | * | |
3593cd53 | 5 | * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com/ |
5e53a69b PR |
6 | */ |
7 | ||
5e53a69b PR |
8 | #include <linux/kernel.h> |
9 | #include <linux/err.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/platform_device.h> | |
14 | #include <linux/io.h> | |
15 | #include <linux/iio/iio.h> | |
6f39ac4e PR |
16 | #include <linux/of.h> |
17 | #include <linux/of_device.h> | |
c80df483 PA |
18 | #include <linux/iio/machine.h> |
19 | #include <linux/iio/driver.h> | |
8bed0166 | 20 | #include <linux/iopoll.h> |
5e53a69b PR |
21 | |
22 | #include <linux/mfd/ti_am335x_tscadc.h> | |
ca9a5638 ZL |
23 | #include <linux/iio/buffer.h> |
24 | #include <linux/iio/kfifo_buf.h> | |
5e53a69b | 25 | |
f438b9da M |
26 | #include <linux/dmaengine.h> |
27 | #include <linux/dma-mapping.h> | |
28 | ||
29 | #define DMA_BUFFER_SIZE SZ_2K | |
30 | ||
31 | struct tiadc_dma { | |
32 | struct dma_slave_config conf; | |
33 | struct dma_chan *chan; | |
34 | dma_addr_t addr; | |
35 | dma_cookie_t cookie; | |
36 | u8 *buf; | |
37 | int current_period; | |
38 | int period_size; | |
39 | u8 fifo_thresh; | |
40 | }; | |
41 | ||
5e53a69b PR |
42 | struct tiadc_device { |
43 | struct ti_tscadc_dev *mfd_tscadc; | |
f438b9da | 44 | struct tiadc_dma dma; |
90c43ec6 | 45 | struct mutex fifo1_lock; /* to protect fifo access */ |
5e53a69b | 46 | int channels; |
f438b9da | 47 | int total_ch_enabled; |
18926ede SAS |
48 | u8 channel_line[8]; |
49 | u8 channel_step[8]; | |
ca9a5638 | 50 | int buffer_en_ch_steps; |
ca9a5638 | 51 | u16 data[8]; |
5dc11e81 | 52 | u32 open_delay[8], sample_delay[8], step_avg[8]; |
5e53a69b PR |
53 | }; |
54 | ||
55 | static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) | |
56 | { | |
57 | return readl(adc->mfd_tscadc->tscadc_base + reg); | |
58 | } | |
59 | ||
60 | static void tiadc_writel(struct tiadc_device *adc, unsigned int reg, | |
9cac0a02 | 61 | unsigned int val) |
5e53a69b PR |
62 | { |
63 | writel(val, adc->mfd_tscadc->tscadc_base + reg); | |
64 | } | |
65 | ||
abeccee4 PR |
66 | static u32 get_adc_step_mask(struct tiadc_device *adc_dev) |
67 | { | |
68 | u32 step_en; | |
69 | ||
70 | step_en = ((1 << adc_dev->channels) - 1); | |
71 | step_en <<= TOTAL_STEPS - adc_dev->channels + 1; | |
72 | return step_en; | |
73 | } | |
74 | ||
7ca6740c | 75 | static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev, |
9cac0a02 | 76 | struct iio_chan_spec const *chan) |
7ca6740c SAS |
77 | { |
78 | int i; | |
79 | ||
80 | for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) { | |
81 | if (chan->channel == adc_dev->channel_line[i]) { | |
82 | u32 step; | |
83 | ||
84 | step = adc_dev->channel_step[i]; | |
85 | /* +1 for the charger */ | |
86 | return 1 << (step + 1); | |
87 | } | |
88 | } | |
89 | WARN_ON(1); | |
90 | return 0; | |
91 | } | |
92 | ||
ca9a5638 | 93 | static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan) |
5e53a69b | 94 | { |
ca9a5638 ZL |
95 | return 1 << adc_dev->channel_step[chan]; |
96 | } | |
97 | ||
8bed0166 MR |
98 | static int tiadc_wait_idle(struct tiadc_device *adc_dev) |
99 | { | |
100 | u32 val; | |
101 | ||
102 | return readl_poll_timeout(adc_dev->mfd_tscadc->tscadc_base + REG_ADCFSM, | |
103 | val, !(val & SEQ_STATUS), 10, | |
789e5ebc | 104 | IDLE_TIMEOUT_MS * 1000 * adc_dev->channels); |
8bed0166 MR |
105 | } |
106 | ||
ca9a5638 ZL |
107 | static void tiadc_step_config(struct iio_dev *indio_dev) |
108 | { | |
109 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
5e53a69b | 110 | unsigned int stepconfig; |
3a59684c | 111 | int i, steps = 0; |
5e53a69b PR |
112 | |
113 | /* | |
114 | * There are 16 configurable steps and 8 analog input | |
115 | * lines available which are shared between Touchscreen and ADC. | |
116 | * | |
3a59684c | 117 | * Steps forwards i.e. from 0 towards 16 are used by ADC |
5e53a69b PR |
118 | * depending on number of input lines needed. |
119 | * Channel would represent which analog input | |
120 | * needs to be given to ADC to digitalize data. | |
121 | */ | |
18926ede SAS |
122 | for (i = 0; i < adc_dev->channels; i++) { |
123 | int chan; | |
124 | ||
125 | chan = adc_dev->channel_line[i]; | |
5dc11e81 | 126 | |
5dc11e81 | 127 | if (adc_dev->step_avg[i]) |
9cac0a02 MR |
128 | stepconfig = STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) | |
129 | STEPCONFIG_FIFO1; | |
5dc11e81 V |
130 | else |
131 | stepconfig = STEPCONFIG_FIFO1; | |
132 | ||
133 | if (iio_buffer_enabled(indio_dev)) | |
134 | stepconfig |= STEPCONFIG_MODE_SWCNT; | |
135 | ||
18926ede | 136 | tiadc_writel(adc_dev, REG_STEPCONFIG(steps), |
9cac0a02 MR |
137 | stepconfig | STEPCONFIG_INP(chan) | |
138 | STEPCONFIG_INM_ADCREFM | STEPCONFIG_RFP_VREFP | | |
139 | STEPCONFIG_RFM_VREFN); | |
5dc11e81 | 140 | |
18926ede | 141 | tiadc_writel(adc_dev, REG_STEPDELAY(steps), |
9cac0a02 MR |
142 | STEPDELAY_OPEN(adc_dev->open_delay[i]) | |
143 | STEPDELAY_SAMPLE(adc_dev->sample_delay[i])); | |
5dc11e81 | 144 | |
18926ede SAS |
145 | adc_dev->channel_step[i] = steps; |
146 | steps++; | |
5e53a69b | 147 | } |
ca9a5638 ZL |
148 | } |
149 | ||
150 | static irqreturn_t tiadc_irq_h(int irq, void *private) | |
151 | { | |
152 | struct iio_dev *indio_dev = private; | |
153 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
e83bb3e6 ME |
154 | unsigned int status, config, adc_fsm; |
155 | unsigned short count = 0; | |
156 | ||
ca9a5638 ZL |
157 | status = tiadc_readl(adc_dev, REG_IRQSTATUS); |
158 | ||
159 | /* | |
160 | * ADC and touchscreen share the IRQ line. | |
161 | * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only | |
162 | */ | |
163 | if (status & IRQENB_FIFO1OVRRUN) { | |
164 | /* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */ | |
165 | config = tiadc_readl(adc_dev, REG_CTRL); | |
c3e36b5d | 166 | config &= ~(CNTRLREG_SSENB); |
ca9a5638 | 167 | tiadc_writel(adc_dev, REG_CTRL, config); |
9cac0a02 MR |
168 | tiadc_writel(adc_dev, REG_IRQSTATUS, |
169 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW | | |
170 | IRQENB_FIFO1THRES); | |
e83bb3e6 | 171 | |
9cac0a02 MR |
172 | /* |
173 | * Wait for the idle state. | |
e83bb3e6 ME |
174 | * ADC needs to finish the current conversion |
175 | * before disabling the module | |
176 | */ | |
177 | do { | |
178 | adc_fsm = tiadc_readl(adc_dev, REG_ADCFSM); | |
179 | } while (adc_fsm != 0x10 && count++ < 100); | |
180 | ||
c3e36b5d | 181 | tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_SSENB)); |
ca9a5638 ZL |
182 | return IRQ_HANDLED; |
183 | } else if (status & IRQENB_FIFO1THRES) { | |
184 | /* Disable irq and wake worker thread */ | |
185 | tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES); | |
186 | return IRQ_WAKE_THREAD; | |
187 | } | |
188 | ||
189 | return IRQ_NONE; | |
190 | } | |
191 | ||
192 | static irqreturn_t tiadc_worker_h(int irq, void *private) | |
193 | { | |
194 | struct iio_dev *indio_dev = private; | |
195 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
196 | int i, k, fifo1count, read; | |
197 | u16 *data = adc_dev->data; | |
198 | ||
199 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); | |
200 | for (k = 0; k < fifo1count; k = k + i) { | |
9cac0a02 | 201 | for (i = 0; i < indio_dev->scan_bytes / 2; i++) { |
ca9a5638 ZL |
202 | read = tiadc_readl(adc_dev, REG_FIFO1); |
203 | data[i] = read & FIFOREAD_DATA_MASK; | |
204 | } | |
9cac0a02 | 205 | iio_push_to_buffers(indio_dev, (u8 *)data); |
ca9a5638 ZL |
206 | } |
207 | ||
208 | tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES); | |
209 | tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES); | |
210 | ||
211 | return IRQ_HANDLED; | |
212 | } | |
213 | ||
f438b9da M |
214 | static void tiadc_dma_rx_complete(void *param) |
215 | { | |
216 | struct iio_dev *indio_dev = param; | |
217 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
218 | struct tiadc_dma *dma = &adc_dev->dma; | |
219 | u8 *data; | |
220 | int i; | |
221 | ||
222 | data = dma->buf + dma->current_period * dma->period_size; | |
223 | dma->current_period = 1 - dma->current_period; /* swap the buffer ID */ | |
224 | ||
225 | for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) { | |
226 | iio_push_to_buffers(indio_dev, data); | |
227 | data += indio_dev->scan_bytes; | |
228 | } | |
229 | } | |
230 | ||
231 | static int tiadc_start_dma(struct iio_dev *indio_dev) | |
232 | { | |
233 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
234 | struct tiadc_dma *dma = &adc_dev->dma; | |
235 | struct dma_async_tx_descriptor *desc; | |
236 | ||
237 | dma->current_period = 0; /* We start to fill period 0 */ | |
9cac0a02 | 238 | |
f438b9da M |
239 | /* |
240 | * Make the fifo thresh as the multiple of total number of | |
241 | * channels enabled, so make sure that cyclic DMA period | |
242 | * length is also a multiple of total number of channels | |
243 | * enabled. This ensures that no invalid data is reported | |
244 | * to the stack via iio_push_to_buffers(). | |
245 | */ | |
246 | dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1, | |
247 | adc_dev->total_ch_enabled) - 1; | |
9cac0a02 | 248 | |
f438b9da M |
249 | /* Make sure that period length is multiple of fifo thresh level */ |
250 | dma->period_size = rounddown(DMA_BUFFER_SIZE / 2, | |
9cac0a02 | 251 | (dma->fifo_thresh + 1) * sizeof(u16)); |
f438b9da M |
252 | |
253 | dma->conf.src_maxburst = dma->fifo_thresh + 1; | |
254 | dmaengine_slave_config(dma->chan, &dma->conf); | |
255 | ||
256 | desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr, | |
257 | dma->period_size * 2, | |
258 | dma->period_size, DMA_DEV_TO_MEM, | |
259 | DMA_PREP_INTERRUPT); | |
260 | if (!desc) | |
261 | return -EBUSY; | |
262 | ||
263 | desc->callback = tiadc_dma_rx_complete; | |
264 | desc->callback_param = indio_dev; | |
265 | ||
266 | dma->cookie = dmaengine_submit(desc); | |
267 | ||
268 | dma_async_issue_pending(dma->chan); | |
269 | ||
270 | tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh); | |
271 | tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh); | |
272 | tiadc_writel(adc_dev, REG_DMAENABLE_SET, DMA_FIFO1); | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
ca9a5638 ZL |
277 | static int tiadc_buffer_preenable(struct iio_dev *indio_dev) |
278 | { | |
279 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
f83d87a0 | 280 | int i, fifo1count; |
8bed0166 MR |
281 | int ret; |
282 | ||
283 | ret = tiadc_wait_idle(adc_dev); | |
284 | if (ret) | |
285 | return ret; | |
ca9a5638 | 286 | |
9cac0a02 MR |
287 | tiadc_writel(adc_dev, REG_IRQCLR, |
288 | IRQENB_FIFO1THRES | IRQENB_FIFO1OVRRUN | | |
289 | IRQENB_FIFO1UNDRFLW); | |
ca9a5638 ZL |
290 | |
291 | /* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */ | |
292 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); | |
293 | for (i = 0; i < fifo1count; i++) | |
f83d87a0 | 294 | tiadc_readl(adc_dev, REG_FIFO1); |
ca9a5638 | 295 | |
24adaf79 | 296 | return 0; |
ca9a5638 ZL |
297 | } |
298 | ||
299 | static int tiadc_buffer_postenable(struct iio_dev *indio_dev) | |
300 | { | |
301 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
f438b9da M |
302 | struct tiadc_dma *dma = &adc_dev->dma; |
303 | unsigned int irq_enable; | |
ca9a5638 ZL |
304 | unsigned int enb = 0; |
305 | u8 bit; | |
306 | ||
307 | tiadc_step_config(indio_dev); | |
f438b9da | 308 | for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) { |
ca9a5638 | 309 | enb |= (get_adc_step_bit(adc_dev, bit) << 1); |
f438b9da M |
310 | adc_dev->total_ch_enabled++; |
311 | } | |
ca9a5638 ZL |
312 | adc_dev->buffer_en_ch_steps = enb; |
313 | ||
f438b9da M |
314 | if (dma->chan) |
315 | tiadc_start_dma(indio_dev); | |
316 | ||
7e170c6e | 317 | am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb); |
ca9a5638 | 318 | |
9cac0a02 MR |
319 | tiadc_writel(adc_dev, REG_IRQSTATUS, |
320 | IRQENB_FIFO1THRES | IRQENB_FIFO1OVRRUN | | |
321 | IRQENB_FIFO1UNDRFLW); | |
f438b9da M |
322 | |
323 | irq_enable = IRQENB_FIFO1OVRRUN; | |
324 | if (!dma->chan) | |
325 | irq_enable |= IRQENB_FIFO1THRES; | |
326 | tiadc_writel(adc_dev, REG_IRQENABLE, irq_enable); | |
ca9a5638 ZL |
327 | |
328 | return 0; | |
329 | } | |
330 | ||
331 | static int tiadc_buffer_predisable(struct iio_dev *indio_dev) | |
332 | { | |
333 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
f438b9da | 334 | struct tiadc_dma *dma = &adc_dev->dma; |
f83d87a0 | 335 | int fifo1count, i; |
ca9a5638 | 336 | |
9cac0a02 MR |
337 | tiadc_writel(adc_dev, REG_IRQCLR, |
338 | IRQENB_FIFO1THRES | IRQENB_FIFO1OVRRUN | | |
339 | IRQENB_FIFO1UNDRFLW); | |
ca9a5638 | 340 | am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); |
3954b7bf | 341 | adc_dev->buffer_en_ch_steps = 0; |
f438b9da M |
342 | adc_dev->total_ch_enabled = 0; |
343 | if (dma->chan) { | |
344 | tiadc_writel(adc_dev, REG_DMAENABLE_CLEAR, 0x2); | |
345 | dmaengine_terminate_async(dma->chan); | |
346 | } | |
b1451e54 | 347 | |
ca9a5638 ZL |
348 | /* Flush FIFO of leftover data in the time it takes to disable adc */ |
349 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); | |
350 | for (i = 0; i < fifo1count; i++) | |
f83d87a0 | 351 | tiadc_readl(adc_dev, REG_FIFO1); |
ca9a5638 ZL |
352 | |
353 | return 0; | |
5e53a69b PR |
354 | } |
355 | ||
ca9a5638 ZL |
356 | static int tiadc_buffer_postdisable(struct iio_dev *indio_dev) |
357 | { | |
358 | tiadc_step_config(indio_dev); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = { | |
364 | .preenable = &tiadc_buffer_preenable, | |
365 | .postenable = &tiadc_buffer_postenable, | |
366 | .predisable = &tiadc_buffer_predisable, | |
367 | .postdisable = &tiadc_buffer_postdisable, | |
368 | }; | |
369 | ||
3c530805 | 370 | static int tiadc_iio_buffered_hardware_setup(struct device *dev, |
9cac0a02 MR |
371 | struct iio_dev *indio_dev, |
372 | irqreturn_t (*pollfunc_bh)(int irq, void *p), | |
373 | irqreturn_t (*pollfunc_th)(int irq, void *p), | |
374 | int irq, unsigned long flags, | |
375 | const struct iio_buffer_setup_ops *setup_ops) | |
ca9a5638 ZL |
376 | { |
377 | int ret; | |
378 | ||
17395ce2 AA |
379 | ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, |
380 | INDIO_BUFFER_SOFTWARE, | |
381 | setup_ops); | |
ca9a5638 | 382 | if (ret) |
7e6d9788 | 383 | return ret; |
ca9a5638 | 384 | |
17395ce2 | 385 | return devm_request_threaded_irq(dev, irq, pollfunc_th, pollfunc_bh, |
9cac0a02 | 386 | flags, indio_dev->name, indio_dev); |
ca9a5638 ZL |
387 | } |
388 | ||
c80df483 PA |
389 | static const char * const chan_name_ain[] = { |
390 | "AIN0", | |
391 | "AIN1", | |
392 | "AIN2", | |
393 | "AIN3", | |
394 | "AIN4", | |
395 | "AIN5", | |
396 | "AIN6", | |
397 | "AIN7", | |
398 | }; | |
399 | ||
6ff0199a AA |
400 | static int tiadc_channel_init(struct device *dev, struct iio_dev *indio_dev, |
401 | int channels) | |
5e53a69b | 402 | { |
c80df483 | 403 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
5e53a69b | 404 | struct iio_chan_spec *chan_array; |
c80df483 | 405 | struct iio_chan_spec *chan; |
5e53a69b PR |
406 | int i; |
407 | ||
408 | indio_dev->num_channels = channels; | |
6ff0199a AA |
409 | chan_array = devm_kcalloc(dev, channels, sizeof(*chan_array), |
410 | GFP_KERNEL); | |
9cac0a02 | 411 | if (!chan_array) |
5e53a69b PR |
412 | return -ENOMEM; |
413 | ||
c80df483 PA |
414 | chan = chan_array; |
415 | for (i = 0; i < channels; i++, chan++) { | |
5e53a69b PR |
416 | chan->type = IIO_VOLTAGE; |
417 | chan->indexed = 1; | |
18926ede | 418 | chan->channel = adc_dev->channel_line[i]; |
6c572522 | 419 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
3af99354 | 420 | chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); |
18926ede | 421 | chan->datasheet_name = chan_name_ain[chan->channel]; |
ca9a5638 | 422 | chan->scan_index = i; |
c80df483 PA |
423 | chan->scan_type.sign = 'u'; |
424 | chan->scan_type.realbits = 12; | |
0f6fc7d5 | 425 | chan->scan_type.storagebits = 16; |
5e53a69b PR |
426 | } |
427 | ||
428 | indio_dev->channels = chan_array; | |
429 | ||
c80df483 | 430 | return 0; |
5e53a69b PR |
431 | } |
432 | ||
5e53a69b | 433 | static int tiadc_read_raw(struct iio_dev *indio_dev, |
9cac0a02 MR |
434 | struct iio_chan_spec const *chan, int *val, int *val2, |
435 | long mask) | |
5e53a69b PR |
436 | { |
437 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
b1451e54 PR |
438 | int i, map_val; |
439 | unsigned int fifo1count, read, stepid; | |
1460c152 | 440 | bool found = false; |
b1451e54 | 441 | u32 step_en; |
7ca6740c | 442 | unsigned long timeout; |
8bed0166 | 443 | int ret; |
ca9a5638 | 444 | |
3af99354 MR |
445 | switch (mask) { |
446 | case IIO_CHAN_INFO_RAW: | |
447 | break; | |
448 | case IIO_CHAN_INFO_SCALE: | |
449 | switch (chan->type) { | |
450 | case IIO_VOLTAGE: | |
451 | *val = 1800; | |
452 | *val2 = chan->scan_type.realbits; | |
453 | return IIO_VAL_FRACTIONAL_LOG2; | |
454 | default: | |
455 | return -EINVAL; | |
456 | } | |
457 | break; | |
458 | default: | |
459 | return -EINVAL; | |
460 | } | |
461 | ||
ca9a5638 ZL |
462 | if (iio_buffer_enabled(indio_dev)) |
463 | return -EBUSY; | |
464 | ||
7ca6740c SAS |
465 | step_en = get_adc_chan_step_mask(adc_dev, chan); |
466 | if (!step_en) | |
467 | return -EINVAL; | |
468 | ||
90c43ec6 | 469 | mutex_lock(&adc_dev->fifo1_lock); |
8bed0166 MR |
470 | |
471 | ret = tiadc_wait_idle(adc_dev); | |
472 | if (ret) | |
473 | goto err_unlock; | |
474 | ||
7ca6740c SAS |
475 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); |
476 | while (fifo1count--) | |
477 | tiadc_readl(adc_dev, REG_FIFO1); | |
478 | ||
7e170c6e | 479 | am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en); |
b1451e54 | 480 | |
7ca6740c | 481 | /* Wait for Fifo threshold interrupt */ |
789e5ebc | 482 | timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT_MS * adc_dev->channels); |
7ca6740c SAS |
483 | while (1) { |
484 | fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); | |
485 | if (fifo1count) | |
486 | break; | |
487 | ||
488 | if (time_after(jiffies, timeout)) { | |
489 | am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); | |
90c43ec6 V |
490 | ret = -EAGAIN; |
491 | goto err_unlock; | |
b1451e54 | 492 | } |
fb7f8ce3 | 493 | } |
9cac0a02 | 494 | |
baa3c652 | 495 | map_val = adc_dev->channel_step[chan->scan_index]; |
5e53a69b PR |
496 | |
497 | /* | |
7ca6740c SAS |
498 | * We check the complete FIFO. We programmed just one entry but in case |
499 | * something went wrong we left empty handed (-EAGAIN previously) and | |
500 | * then the value apeared somehow in the FIFO we would have two entries. | |
501 | * Therefore we read every item and keep only the latest version of the | |
502 | * requested channel. | |
5e53a69b | 503 | */ |
5e53a69b | 504 | for (i = 0; i < fifo1count; i++) { |
18926ede | 505 | read = tiadc_readl(adc_dev, REG_FIFO1); |
b1451e54 PR |
506 | stepid = read & FIFOREAD_CHNLID_MASK; |
507 | stepid = stepid >> 0x10; | |
508 | ||
509 | if (stepid == map_val) { | |
510 | read = read & FIFOREAD_DATA_MASK; | |
1460c152 | 511 | found = true; |
9cac0a02 | 512 | *val = (u16)read; |
1460c152 | 513 | } |
5e53a69b | 514 | } |
9cac0a02 | 515 | |
7ca6740c | 516 | am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); |
b1451e54 | 517 | |
a540243f | 518 | if (!found) |
9cac0a02 | 519 | ret = -EBUSY; |
90c43ec6 V |
520 | |
521 | err_unlock: | |
522 | mutex_unlock(&adc_dev->fifo1_lock); | |
8bed0166 | 523 | return ret ? ret : IIO_VAL_INT; |
5e53a69b PR |
524 | } |
525 | ||
526 | static const struct iio_info tiadc_info = { | |
527 | .read_raw = &tiadc_read_raw, | |
528 | }; | |
529 | ||
f438b9da M |
530 | static int tiadc_request_dma(struct platform_device *pdev, |
531 | struct tiadc_device *adc_dev) | |
532 | { | |
533 | struct tiadc_dma *dma = &adc_dev->dma; | |
534 | dma_cap_mask_t mask; | |
535 | ||
536 | /* Default slave configuration parameters */ | |
537 | dma->conf.direction = DMA_DEV_TO_MEM; | |
538 | dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; | |
539 | dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1; | |
540 | ||
541 | dma_cap_zero(mask); | |
542 | dma_cap_set(DMA_CYCLIC, mask); | |
543 | ||
544 | /* Get a channel for RX */ | |
545 | dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1"); | |
546 | if (IS_ERR(dma->chan)) { | |
547 | int ret = PTR_ERR(dma->chan); | |
548 | ||
549 | dma->chan = NULL; | |
550 | return ret; | |
551 | } | |
552 | ||
553 | /* RX buffer */ | |
554 | dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, | |
555 | &dma->addr, GFP_KERNEL); | |
556 | if (!dma->buf) | |
557 | goto err; | |
558 | ||
559 | return 0; | |
9cac0a02 | 560 | |
f438b9da M |
561 | err: |
562 | dma_release_channel(dma->chan); | |
563 | return -ENOMEM; | |
564 | } | |
565 | ||
dee1f550 V |
566 | static int tiadc_parse_dt(struct platform_device *pdev, |
567 | struct tiadc_device *adc_dev) | |
568 | { | |
569 | struct device_node *node = pdev->dev.of_node; | |
570 | struct property *prop; | |
571 | const __be32 *cur; | |
572 | int channels = 0; | |
573 | u32 val; | |
b61a9d32 | 574 | int i; |
dee1f550 V |
575 | |
576 | of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { | |
577 | adc_dev->channel_line[channels] = val; | |
5dc11e81 V |
578 | |
579 | /* Set Default values for optional DT parameters */ | |
580 | adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY; | |
581 | adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY; | |
582 | adc_dev->step_avg[channels] = 16; | |
583 | ||
dee1f550 V |
584 | channels++; |
585 | } | |
586 | ||
b61a9d32 MR |
587 | adc_dev->channels = channels; |
588 | ||
5dc11e81 V |
589 | of_property_read_u32_array(node, "ti,chan-step-avg", |
590 | adc_dev->step_avg, channels); | |
591 | of_property_read_u32_array(node, "ti,chan-step-opendelay", | |
592 | adc_dev->open_delay, channels); | |
593 | of_property_read_u32_array(node, "ti,chan-step-sampledelay", | |
594 | adc_dev->sample_delay, channels); | |
595 | ||
b61a9d32 MR |
596 | for (i = 0; i < adc_dev->channels; i++) { |
597 | int chan; | |
598 | ||
599 | chan = adc_dev->channel_line[i]; | |
600 | ||
601 | if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) { | |
602 | dev_warn(&pdev->dev, | |
603 | "chan %d: wrong step avg, truncated to %ld\n", | |
604 | chan, STEPCONFIG_AVG_16); | |
605 | adc_dev->step_avg[i] = STEPCONFIG_AVG_16; | |
606 | } | |
607 | ||
608 | if (adc_dev->open_delay[i] > STEPCONFIG_MAX_OPENDLY) { | |
609 | dev_warn(&pdev->dev, | |
610 | "chan %d: wrong open delay, truncated to 0x%lX\n", | |
611 | chan, STEPCONFIG_MAX_OPENDLY); | |
612 | adc_dev->open_delay[i] = STEPCONFIG_MAX_OPENDLY; | |
613 | } | |
614 | ||
615 | if (adc_dev->sample_delay[i] > STEPCONFIG_MAX_SAMPLE) { | |
616 | dev_warn(&pdev->dev, | |
617 | "chan %d: wrong sample delay, truncated to 0x%lX\n", | |
618 | chan, STEPCONFIG_MAX_SAMPLE); | |
619 | adc_dev->sample_delay[i] = STEPCONFIG_MAX_SAMPLE; | |
620 | } | |
621 | } | |
622 | ||
dee1f550 V |
623 | return 0; |
624 | } | |
625 | ||
fc52692c | 626 | static int tiadc_probe(struct platform_device *pdev) |
5e53a69b PR |
627 | { |
628 | struct iio_dev *indio_dev; | |
629 | struct tiadc_device *adc_dev; | |
6f39ac4e | 630 | struct device_node *node = pdev->dev.of_node; |
5e53a69b PR |
631 | int err; |
632 | ||
0ead4fb2 SAS |
633 | if (!node) { |
634 | dev_err(&pdev->dev, "Could not find valid DT data.\n"); | |
5e53a69b PR |
635 | return -EINVAL; |
636 | } | |
637 | ||
5ba5b437 | 638 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev)); |
9cac0a02 | 639 | if (!indio_dev) { |
5e53a69b | 640 | dev_err(&pdev->dev, "failed to allocate iio device\n"); |
a0648130 | 641 | return -ENOMEM; |
5e53a69b PR |
642 | } |
643 | adc_dev = iio_priv(indio_dev); | |
644 | ||
6f39ac4e | 645 | adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); |
dee1f550 | 646 | tiadc_parse_dt(pdev, adc_dev); |
5e53a69b | 647 | |
5e53a69b PR |
648 | indio_dev->name = dev_name(&pdev->dev); |
649 | indio_dev->modes = INDIO_DIRECT_MODE; | |
650 | indio_dev->info = &tiadc_info; | |
651 | ||
ca9a5638 ZL |
652 | tiadc_step_config(indio_dev); |
653 | tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD); | |
90c43ec6 | 654 | mutex_init(&adc_dev->fifo1_lock); |
5e53a69b | 655 | |
6ff0199a | 656 | err = tiadc_channel_init(&pdev->dev, indio_dev, adc_dev->channels); |
5e53a69b | 657 | if (err < 0) |
a0648130 | 658 | return err; |
5e53a69b | 659 | |
3c530805 | 660 | err = tiadc_iio_buffered_hardware_setup(&pdev->dev, indio_dev, |
9cac0a02 MR |
661 | &tiadc_worker_h, |
662 | &tiadc_irq_h, | |
663 | adc_dev->mfd_tscadc->irq, | |
664 | IRQF_SHARED, | |
665 | &tiadc_buffer_setup_ops); | |
5e53a69b | 666 | if (err) |
16e8f8fe | 667 | return err; |
5e53a69b | 668 | |
ca9a5638 ZL |
669 | err = iio_device_register(indio_dev); |
670 | if (err) | |
16e8f8fe | 671 | return err; |
ca9a5638 | 672 | |
5e53a69b PR |
673 | platform_set_drvdata(pdev, indio_dev); |
674 | ||
f438b9da M |
675 | err = tiadc_request_dma(pdev, adc_dev); |
676 | if (err && err == -EPROBE_DEFER) | |
677 | goto err_dma; | |
678 | ||
5e53a69b PR |
679 | return 0; |
680 | ||
f438b9da M |
681 | err_dma: |
682 | iio_device_unregister(indio_dev); | |
16e8f8fe | 683 | |
5e53a69b PR |
684 | return err; |
685 | } | |
686 | ||
fc52692c | 687 | static int tiadc_remove(struct platform_device *pdev) |
5e53a69b PR |
688 | { |
689 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); | |
abeccee4 | 690 | struct tiadc_device *adc_dev = iio_priv(indio_dev); |
f438b9da | 691 | struct tiadc_dma *dma = &adc_dev->dma; |
abeccee4 | 692 | u32 step_en; |
5e53a69b | 693 | |
f438b9da M |
694 | if (dma->chan) { |
695 | dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, | |
696 | dma->buf, dma->addr); | |
697 | dma_release_channel(dma->chan); | |
698 | } | |
5e53a69b | 699 | iio_device_unregister(indio_dev); |
5e53a69b | 700 | |
abeccee4 PR |
701 | step_en = get_adc_step_mask(adc_dev); |
702 | am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); | |
703 | ||
5e53a69b PR |
704 | return 0; |
705 | } | |
706 | ||
27aa832d | 707 | static int __maybe_unused tiadc_suspend(struct device *dev) |
5e53a69b PR |
708 | { |
709 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
710 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
5e53a69b PR |
711 | unsigned int idle; |
712 | ||
9eea8326 | 713 | idle = tiadc_readl(adc_dev, REG_CTRL); |
c3e36b5d | 714 | idle &= ~(CNTRLREG_SSENB); |
9cac0a02 | 715 | tiadc_writel(adc_dev, REG_CTRL, idle | CNTRLREG_POWERDOWN); |
5e53a69b PR |
716 | |
717 | return 0; | |
718 | } | |
719 | ||
27aa832d | 720 | static int __maybe_unused tiadc_resume(struct device *dev) |
5e53a69b PR |
721 | { |
722 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
723 | struct tiadc_device *adc_dev = iio_priv(indio_dev); | |
724 | unsigned int restore; | |
725 | ||
726 | /* Make sure ADC is powered up */ | |
727 | restore = tiadc_readl(adc_dev, REG_CTRL); | |
9cac0a02 | 728 | restore &= ~CNTRLREG_POWERDOWN; |
5e53a69b PR |
729 | tiadc_writel(adc_dev, REG_CTRL, restore); |
730 | ||
ca9a5638 | 731 | tiadc_step_config(indio_dev); |
7ca6740c | 732 | am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, |
9cac0a02 | 733 | adc_dev->buffer_en_ch_steps); |
5e53a69b PR |
734 | return 0; |
735 | } | |
736 | ||
27aa832d | 737 | static SIMPLE_DEV_PM_OPS(tiadc_pm_ops, tiadc_suspend, tiadc_resume); |
5e53a69b | 738 | |
6f39ac4e PR |
739 | static const struct of_device_id ti_adc_dt_ids[] = { |
740 | { .compatible = "ti,am3359-adc", }, | |
741 | { } | |
742 | }; | |
743 | MODULE_DEVICE_TABLE(of, ti_adc_dt_ids); | |
744 | ||
5e53a69b PR |
745 | static struct platform_driver tiadc_driver = { |
746 | .driver = { | |
9f99928f | 747 | .name = "TI-am335x-adc", |
27aa832d | 748 | .pm = &tiadc_pm_ops, |
de06b344 | 749 | .of_match_table = ti_adc_dt_ids, |
5e53a69b PR |
750 | }, |
751 | .probe = tiadc_probe, | |
fc52692c | 752 | .remove = tiadc_remove, |
5e53a69b | 753 | }; |
5e53a69b PR |
754 | module_platform_driver(tiadc_driver); |
755 | ||
756 | MODULE_DESCRIPTION("TI ADC controller driver"); | |
757 | MODULE_AUTHOR("Rachna Patil <rachna@ti.com>"); | |
758 | MODULE_LICENSE("GPL"); |