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