xref: /openbmc/linux/drivers/iio/adc/ti_am335x_adc.c (revision ca9a5638)
1 /*
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>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/iio/machine.h>
28 #include <linux/iio/driver.h>
29 
30 #include <linux/mfd/ti_am335x_tscadc.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/kfifo_buf.h>
33 #include <linux/iio/trigger.h>
34 #include <linux/iio/trigger_consumer.h>
35 #include <linux/iio/triggered_buffer.h>
36 
37 struct tiadc_device {
38 	struct ti_tscadc_dev *mfd_tscadc;
39 	int channels;
40 	u8 channel_line[8];
41 	u8 channel_step[8];
42 	int buffer_en_ch_steps;
43 	struct iio_trigger *trig;
44 	u16 data[8];
45 };
46 
47 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
48 {
49 	return readl(adc->mfd_tscadc->tscadc_base + reg);
50 }
51 
52 static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
53 					unsigned int val)
54 {
55 	writel(val, adc->mfd_tscadc->tscadc_base + reg);
56 }
57 
58 static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
59 {
60 	u32 step_en;
61 
62 	step_en = ((1 << adc_dev->channels) - 1);
63 	step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
64 	return step_en;
65 }
66 
67 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
68 {
69 	return 1 << adc_dev->channel_step[chan];
70 }
71 
72 static void tiadc_step_config(struct iio_dev *indio_dev)
73 {
74 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
75 	unsigned int stepconfig;
76 	int i, steps;
77 
78 	/*
79 	 * There are 16 configurable steps and 8 analog input
80 	 * lines available which are shared between Touchscreen and ADC.
81 	 *
82 	 * Steps backwards i.e. from 16 towards 0 are used by ADC
83 	 * depending on number of input lines needed.
84 	 * Channel would represent which analog input
85 	 * needs to be given to ADC to digitalize data.
86 	 */
87 
88 	steps = TOTAL_STEPS - adc_dev->channels;
89 	if (iio_buffer_enabled(indio_dev))
90 		stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1
91 					| STEPCONFIG_MODE_SWCNT;
92 	else
93 		stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
94 
95 	for (i = 0; i < adc_dev->channels; i++) {
96 		int chan;
97 
98 		chan = adc_dev->channel_line[i];
99 		tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
100 				stepconfig | STEPCONFIG_INP(chan));
101 		tiadc_writel(adc_dev, REG_STEPDELAY(steps),
102 				STEPCONFIG_OPENDLY);
103 		adc_dev->channel_step[i] = steps;
104 		steps++;
105 	}
106 }
107 
108 static irqreturn_t tiadc_irq_h(int irq, void *private)
109 {
110 	struct iio_dev *indio_dev = private;
111 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
112 	unsigned int status, config;
113 	status = tiadc_readl(adc_dev, REG_IRQSTATUS);
114 
115 	/*
116 	 * ADC and touchscreen share the IRQ line.
117 	 * FIFO0 interrupts are used by TSC. Handle FIFO1 IRQs here only
118 	 */
119 	if (status & IRQENB_FIFO1OVRRUN) {
120 		/* FIFO Overrun. Clear flag. Disable/Enable ADC to recover */
121 		config = tiadc_readl(adc_dev, REG_CTRL);
122 		config &= ~(CNTRLREG_TSCSSENB);
123 		tiadc_writel(adc_dev, REG_CTRL, config);
124 		tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1OVRRUN
125 				| IRQENB_FIFO1UNDRFLW | IRQENB_FIFO1THRES);
126 		tiadc_writel(adc_dev, REG_CTRL, (config | CNTRLREG_TSCSSENB));
127 		return IRQ_HANDLED;
128 	} else if (status & IRQENB_FIFO1THRES) {
129 		/* Disable irq and wake worker thread */
130 		tiadc_writel(adc_dev, REG_IRQCLR, IRQENB_FIFO1THRES);
131 		return IRQ_WAKE_THREAD;
132 	}
133 
134 	return IRQ_NONE;
135 }
136 
137 static irqreturn_t tiadc_worker_h(int irq, void *private)
138 {
139 	struct iio_dev *indio_dev = private;
140 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
141 	int i, k, fifo1count, read;
142 	u16 *data = adc_dev->data;
143 
144 	fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
145 	for (k = 0; k < fifo1count; k = k + i) {
146 		for (i = 0; i < (indio_dev->scan_bytes)/2; i++) {
147 			read = tiadc_readl(adc_dev, REG_FIFO1);
148 			data[i] = read & FIFOREAD_DATA_MASK;
149 		}
150 		iio_push_to_buffers(indio_dev, (u8 *) data);
151 	}
152 
153 	tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES);
154 	tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
155 
156 	return IRQ_HANDLED;
157 }
158 
159 static int tiadc_buffer_preenable(struct iio_dev *indio_dev)
160 {
161 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
162 	int i, fifo1count, read;
163 
164 	tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
165 				IRQENB_FIFO1OVRRUN |
166 				IRQENB_FIFO1UNDRFLW));
167 
168 	/* Flush FIFO. Needed in corner cases in simultaneous tsc/adc use */
169 	fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
170 	for (i = 0; i < fifo1count; i++)
171 		read = tiadc_readl(adc_dev, REG_FIFO1);
172 
173 	return iio_sw_buffer_preenable(indio_dev);
174 }
175 
176 static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
177 {
178 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
179 	struct iio_buffer *buffer = indio_dev->buffer;
180 	unsigned int enb = 0;
181 	u8 bit;
182 
183 	tiadc_step_config(indio_dev);
184 	for_each_set_bit(bit, buffer->scan_mask, adc_dev->channels)
185 		enb |= (get_adc_step_bit(adc_dev, bit) << 1);
186 	adc_dev->buffer_en_ch_steps = enb;
187 
188 	am335x_tsc_se_set(adc_dev->mfd_tscadc, enb);
189 
190 	tiadc_writel(adc_dev,  REG_IRQSTATUS, IRQENB_FIFO1THRES
191 				| IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);
192 	tiadc_writel(adc_dev,  REG_IRQENABLE, IRQENB_FIFO1THRES
193 				| IRQENB_FIFO1OVRRUN);
194 
195 	return 0;
196 }
197 
198 static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
199 {
200 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
201 	int fifo1count, i, read;
202 
203 	tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |
204 				IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));
205 	am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
206 
207 	/* Flush FIFO of leftover data in the time it takes to disable adc */
208 	fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
209 	for (i = 0; i < fifo1count; i++)
210 		read = tiadc_readl(adc_dev, REG_FIFO1);
211 
212 	return 0;
213 }
214 
215 static int tiadc_buffer_postdisable(struct iio_dev *indio_dev)
216 {
217 	tiadc_step_config(indio_dev);
218 
219 	return 0;
220 }
221 
222 static const struct iio_buffer_setup_ops tiadc_buffer_setup_ops = {
223 	.preenable = &tiadc_buffer_preenable,
224 	.postenable = &tiadc_buffer_postenable,
225 	.predisable = &tiadc_buffer_predisable,
226 	.postdisable = &tiadc_buffer_postdisable,
227 };
228 
229 int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
230 	irqreturn_t (*pollfunc_bh)(int irq, void *p),
231 	irqreturn_t (*pollfunc_th)(int irq, void *p),
232 	int irq,
233 	unsigned long flags,
234 	const struct iio_buffer_setup_ops *setup_ops)
235 {
236 	int ret;
237 
238 	indio_dev->buffer = iio_kfifo_allocate(indio_dev);
239 	if (!indio_dev->buffer)
240 		return -ENOMEM;
241 
242 	ret = request_threaded_irq(irq,	pollfunc_th, pollfunc_bh,
243 				flags, indio_dev->name, indio_dev);
244 	if (ret)
245 		goto error_kfifo_free;
246 
247 	indio_dev->setup_ops = setup_ops;
248 	indio_dev->modes |= INDIO_BUFFER_HARDWARE;
249 
250 	ret = iio_buffer_register(indio_dev,
251 				  indio_dev->channels,
252 				  indio_dev->num_channels);
253 	if (ret)
254 		goto error_free_irq;
255 
256 	return 0;
257 
258 error_free_irq:
259 	free_irq(irq, indio_dev);
260 error_kfifo_free:
261 	iio_kfifo_free(indio_dev->buffer);
262 	return ret;
263 }
264 
265 static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
266 {
267 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
268 
269 	free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
270 	iio_kfifo_free(indio_dev->buffer);
271 	iio_buffer_unregister(indio_dev);
272 }
273 
274 
275 static const char * const chan_name_ain[] = {
276 	"AIN0",
277 	"AIN1",
278 	"AIN2",
279 	"AIN3",
280 	"AIN4",
281 	"AIN5",
282 	"AIN6",
283 	"AIN7",
284 };
285 
286 static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
287 {
288 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
289 	struct iio_chan_spec *chan_array;
290 	struct iio_chan_spec *chan;
291 	int i;
292 
293 	indio_dev->num_channels = channels;
294 	chan_array = kcalloc(channels,
295 			sizeof(struct iio_chan_spec), GFP_KERNEL);
296 	if (chan_array == NULL)
297 		return -ENOMEM;
298 
299 	chan = chan_array;
300 	for (i = 0; i < channels; i++, chan++) {
301 
302 		chan->type = IIO_VOLTAGE;
303 		chan->indexed = 1;
304 		chan->channel = adc_dev->channel_line[i];
305 		chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
306 		chan->datasheet_name = chan_name_ain[chan->channel];
307 		chan->scan_index = i;
308 		chan->scan_type.sign = 'u';
309 		chan->scan_type.realbits = 12;
310 		chan->scan_type.storagebits = 16;
311 	}
312 
313 	indio_dev->channels = chan_array;
314 
315 	return 0;
316 }
317 
318 static void tiadc_channels_remove(struct iio_dev *indio_dev)
319 {
320 	kfree(indio_dev->channels);
321 }
322 
323 static int tiadc_read_raw(struct iio_dev *indio_dev,
324 		struct iio_chan_spec const *chan,
325 		int *val, int *val2, long mask)
326 {
327 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
328 	int i, map_val;
329 	unsigned int fifo1count, read, stepid;
330 	u32 step = UINT_MAX;
331 	bool found = false;
332 	u32 step_en;
333 	unsigned long timeout = jiffies + usecs_to_jiffies
334 				(IDLE_TIMEOUT * adc_dev->channels);
335 
336 	if (iio_buffer_enabled(indio_dev))
337 		return -EBUSY;
338 
339 	step_en = get_adc_step_mask(adc_dev);
340 	am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
341 
342 	/* Wait for ADC sequencer to complete sampling */
343 	while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
344 		if (time_after(jiffies, timeout))
345 			return -EAGAIN;
346 		}
347 	map_val = chan->channel + TOTAL_CHANNELS;
348 
349 	/*
350 	 * When the sub-system is first enabled,
351 	 * the sequencer will always start with the
352 	 * lowest step (1) and continue until step (16).
353 	 * For ex: If we have enabled 4 ADC channels and
354 	 * currently use only 1 out of them, the
355 	 * sequencer still configures all the 4 steps,
356 	 * leading to 3 unwanted data.
357 	 * Hence we need to flush out this data.
358 	 */
359 
360 	for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
361 		if (chan->channel == adc_dev->channel_line[i]) {
362 			step = adc_dev->channel_step[i];
363 			break;
364 		}
365 	}
366 	if (WARN_ON_ONCE(step == UINT_MAX))
367 		return -EINVAL;
368 
369 	fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
370 	for (i = 0; i < fifo1count; i++) {
371 		read = tiadc_readl(adc_dev, REG_FIFO1);
372 		stepid = read & FIFOREAD_CHNLID_MASK;
373 		stepid = stepid >> 0x10;
374 
375 		if (stepid == map_val) {
376 			read = read & FIFOREAD_DATA_MASK;
377 			found = true;
378 			*val = (u16) read;
379 		}
380 	}
381 
382 	if (found == false)
383 		return -EBUSY;
384 	return IIO_VAL_INT;
385 }
386 
387 static const struct iio_info tiadc_info = {
388 	.read_raw = &tiadc_read_raw,
389 	.driver_module = THIS_MODULE,
390 };
391 
392 static int tiadc_probe(struct platform_device *pdev)
393 {
394 	struct iio_dev		*indio_dev;
395 	struct tiadc_device	*adc_dev;
396 	struct device_node	*node = pdev->dev.of_node;
397 	struct property		*prop;
398 	const __be32		*cur;
399 	int			err;
400 	u32			val;
401 	int			channels = 0;
402 
403 	if (!node) {
404 		dev_err(&pdev->dev, "Could not find valid DT data.\n");
405 		return -EINVAL;
406 	}
407 
408 	indio_dev = devm_iio_device_alloc(&pdev->dev,
409 					  sizeof(struct tiadc_device));
410 	if (indio_dev == NULL) {
411 		dev_err(&pdev->dev, "failed to allocate iio device\n");
412 		return -ENOMEM;
413 	}
414 	adc_dev = iio_priv(indio_dev);
415 
416 	adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
417 
418 	of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
419 		adc_dev->channel_line[channels] = val;
420 		channels++;
421 	}
422 	adc_dev->channels = channels;
423 
424 	indio_dev->dev.parent = &pdev->dev;
425 	indio_dev->name = dev_name(&pdev->dev);
426 	indio_dev->modes = INDIO_DIRECT_MODE;
427 	indio_dev->info = &tiadc_info;
428 
429 	tiadc_step_config(indio_dev);
430 	tiadc_writel(adc_dev, REG_FIFO1THR, FIFO1_THRESHOLD);
431 
432 	err = tiadc_channel_init(indio_dev, adc_dev->channels);
433 	if (err < 0)
434 		return err;
435 
436 	err = tiadc_iio_buffered_hardware_setup(indio_dev,
437 		&tiadc_worker_h,
438 		&tiadc_irq_h,
439 		adc_dev->mfd_tscadc->irq,
440 		IRQF_SHARED,
441 		&tiadc_buffer_setup_ops);
442 
443 	if (err)
444 		goto err_free_channels;
445 
446 	err = iio_device_register(indio_dev);
447 	if (err)
448 		goto err_buffer_unregister;
449 
450 	platform_set_drvdata(pdev, indio_dev);
451 
452 	return 0;
453 
454 err_buffer_unregister:
455 	tiadc_iio_buffered_hardware_remove(indio_dev);
456 err_free_channels:
457 	tiadc_channels_remove(indio_dev);
458 	return err;
459 }
460 
461 static int tiadc_remove(struct platform_device *pdev)
462 {
463 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
464 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
465 	u32 step_en;
466 
467 	iio_device_unregister(indio_dev);
468 	tiadc_iio_buffered_hardware_remove(indio_dev);
469 	tiadc_channels_remove(indio_dev);
470 
471 	step_en = get_adc_step_mask(adc_dev);
472 	am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
473 
474 	return 0;
475 }
476 
477 #ifdef CONFIG_PM
478 static int tiadc_suspend(struct device *dev)
479 {
480 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
481 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
482 	struct ti_tscadc_dev *tscadc_dev;
483 	unsigned int idle;
484 
485 	tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
486 	if (!device_may_wakeup(tscadc_dev->dev)) {
487 		idle = tiadc_readl(adc_dev, REG_CTRL);
488 		idle &= ~(CNTRLREG_TSCSSENB);
489 		tiadc_writel(adc_dev, REG_CTRL, (idle |
490 				CNTRLREG_POWERDOWN));
491 	}
492 
493 	return 0;
494 }
495 
496 static int tiadc_resume(struct device *dev)
497 {
498 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
499 	struct tiadc_device *adc_dev = iio_priv(indio_dev);
500 	unsigned int restore;
501 
502 	/* Make sure ADC is powered up */
503 	restore = tiadc_readl(adc_dev, REG_CTRL);
504 	restore &= ~(CNTRLREG_POWERDOWN);
505 	tiadc_writel(adc_dev, REG_CTRL, restore);
506 
507 	tiadc_step_config(indio_dev);
508 
509 	return 0;
510 }
511 
512 static const struct dev_pm_ops tiadc_pm_ops = {
513 	.suspend = tiadc_suspend,
514 	.resume = tiadc_resume,
515 };
516 #define TIADC_PM_OPS (&tiadc_pm_ops)
517 #else
518 #define TIADC_PM_OPS NULL
519 #endif
520 
521 static const struct of_device_id ti_adc_dt_ids[] = {
522 	{ .compatible = "ti,am3359-adc", },
523 	{ }
524 };
525 MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
526 
527 static struct platform_driver tiadc_driver = {
528 	.driver = {
529 		.name   = "TI-am335x-adc",
530 		.owner	= THIS_MODULE,
531 		.pm	= TIADC_PM_OPS,
532 		.of_match_table = of_match_ptr(ti_adc_dt_ids),
533 	},
534 	.probe	= tiadc_probe,
535 	.remove	= tiadc_remove,
536 };
537 module_platform_driver(tiadc_driver);
538 
539 MODULE_DESCRIPTION("TI ADC controller driver");
540 MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
541 MODULE_LICENSE("GPL");
542