xref: /linux/drivers/input/touchscreen/mxs-lradc-ts.c (revision be709d48329a500621d2a05835283150ae137b45)
1 /*
2  * Freescale MXS LRADC touchscreen driver
3  *
4  * Copyright (c) 2012 DENX Software Engineering, GmbH.
5  * Copyright (c) 2017 Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
6  *
7  * Authors:
8  *  Marek Vasut <marex@denx.de>
9  *  Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  */
21 
22 #include <linux/device.h>
23 #include <linux/err.h>
24 #include <linux/input.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/mxs-lradc.h>
29 #include <linux/of.h>
30 #include <linux/of_irq.h>
31 #include <linux/platform_device.h>
32 
33 static const char * const mxs_lradc_ts_irq_names[] = {
34 	"mxs-lradc-touchscreen",
35 	"mxs-lradc-channel6",
36 	"mxs-lradc-channel7",
37 };
38 
39 /*
40  * Touchscreen handling
41  */
42 enum mxs_lradc_ts_plate {
43 	LRADC_TOUCH = 0,
44 	LRADC_SAMPLE_X,
45 	LRADC_SAMPLE_Y,
46 	LRADC_SAMPLE_PRESSURE,
47 	LRADC_SAMPLE_VALID,
48 };
49 
50 struct mxs_lradc_ts {
51 	struct mxs_lradc	*lradc;
52 	struct device		*dev;
53 
54 	void __iomem		*base;
55 	/*
56 	 * When the touchscreen is enabled, we give it two private virtual
57 	 * channels: #6 and #7. This means that only 6 virtual channels (instead
58 	 * of 8) will be available for buffered capture.
59 	 */
60 #define TOUCHSCREEN_VCHANNEL1		7
61 #define TOUCHSCREEN_VCHANNEL2		6
62 
63 	struct input_dev	*ts_input;
64 
65 	enum mxs_lradc_ts_plate	cur_plate; /* state machine */
66 	bool			ts_valid;
67 	unsigned int		ts_x_pos;
68 	unsigned int		ts_y_pos;
69 	unsigned int		ts_pressure;
70 
71 	/* handle touchscreen's physical behaviour */
72 	/* samples per coordinate */
73 	unsigned int		over_sample_cnt;
74 	/* time clocks between samples */
75 	unsigned int		over_sample_delay;
76 	/* time in clocks to wait after the plates where switched */
77 	unsigned int		settling_delay;
78 	spinlock_t		lock;
79 };
80 
81 struct state_info {
82 	u32		mask;
83 	u32		bit;
84 	u32		x_plate;
85 	u32		y_plate;
86 	u32		pressure;
87 };
88 
89 static struct state_info info[] = {
90 	{LRADC_CTRL0_MX23_PLATE_MASK, LRADC_CTRL0_MX23_TOUCH_DETECT_ENABLE,
91 	 LRADC_CTRL0_MX23_XP | LRADC_CTRL0_MX23_XM,
92 	 LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_YM,
93 	 LRADC_CTRL0_MX23_YP | LRADC_CTRL0_MX23_XM},
94 	{LRADC_CTRL0_MX28_PLATE_MASK, LRADC_CTRL0_MX28_TOUCH_DETECT_ENABLE,
95 	 LRADC_CTRL0_MX28_XPPSW | LRADC_CTRL0_MX28_XNNSW,
96 	 LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_YNNSW,
97 	 LRADC_CTRL0_MX28_YPPSW | LRADC_CTRL0_MX28_XNNSW}
98 };
99 
100 static bool mxs_lradc_check_touch_event(struct mxs_lradc_ts *ts)
101 {
102 	return !!(readl(ts->base + LRADC_STATUS) &
103 					LRADC_STATUS_TOUCH_DETECT_RAW);
104 }
105 
106 static void mxs_lradc_map_ts_channel(struct mxs_lradc_ts *ts, unsigned int vch,
107 				     unsigned int ch)
108 {
109 	writel(LRADC_CTRL4_LRADCSELECT_MASK(vch),
110 	       ts->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR);
111 	writel(LRADC_CTRL4_LRADCSELECT(vch, ch),
112 	       ts->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET);
113 }
114 
115 static void mxs_lradc_setup_ts_channel(struct mxs_lradc_ts *ts, unsigned int ch)
116 {
117 	/*
118 	 * prepare for oversampling conversion
119 	 *
120 	 * from the datasheet:
121 	 * "The ACCUMULATE bit in the appropriate channel register
122 	 * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
123 	 * otherwise, the IRQs will not fire."
124 	 */
125 	writel(LRADC_CH_ACCUMULATE |
126 	       LRADC_CH_NUM_SAMPLES(ts->over_sample_cnt - 1),
127 	       ts->base + LRADC_CH(ch));
128 
129 	/* from the datasheet:
130 	 * "Software must clear this register in preparation for a
131 	 * multi-cycle accumulation.
132 	 */
133 	writel(LRADC_CH_VALUE_MASK,
134 	       ts->base + LRADC_CH(ch) + STMP_OFFSET_REG_CLR);
135 
136 	/*
137 	 * prepare the delay/loop unit according to the oversampling count
138 	 *
139 	 * from the datasheet:
140 	 * "The DELAY fields in HW_LRADC_DELAY0, HW_LRADC_DELAY1,
141 	 * HW_LRADC_DELAY2, and HW_LRADC_DELAY3 must be non-zero; otherwise,
142 	 * the LRADC will not trigger the delay group."
143 	 */
144 	writel(LRADC_DELAY_TRIGGER(1 << ch) | LRADC_DELAY_TRIGGER_DELAYS(0) |
145 	       LRADC_DELAY_LOOP(ts->over_sample_cnt - 1) |
146 	       LRADC_DELAY_DELAY(ts->over_sample_delay - 1),
147 	       ts->base + LRADC_DELAY(3));
148 
149 	writel(LRADC_CTRL1_LRADC_IRQ(ch),
150 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
151 
152 	/*
153 	 * after changing the touchscreen plates setting
154 	 * the signals need some initial time to settle. Start the
155 	 * SoC's delay unit and start the conversion later
156 	 * and automatically.
157 	 */
158 	writel(LRADC_DELAY_TRIGGER(0) | LRADC_DELAY_TRIGGER_DELAYS(BIT(3)) |
159 	       LRADC_DELAY_KICK | LRADC_DELAY_DELAY(ts->settling_delay),
160 	       ts->base + LRADC_DELAY(2));
161 }
162 
163 /*
164  * Pressure detection is special:
165  * We want to do both required measurements for the pressure detection in
166  * one turn. Use the hardware features to chain both conversions and let the
167  * hardware report one interrupt if both conversions are done
168  */
169 static void mxs_lradc_setup_ts_pressure(struct mxs_lradc_ts *ts,
170 					unsigned int ch1, unsigned int ch2)
171 {
172 	u32 reg;
173 
174 	/*
175 	 * prepare for oversampling conversion
176 	 *
177 	 * from the datasheet:
178 	 * "The ACCUMULATE bit in the appropriate channel register
179 	 * HW_LRADC_CHn must be set to 1 if NUM_SAMPLES is greater then 0;
180 	 * otherwise, the IRQs will not fire."
181 	 */
182 	reg = LRADC_CH_ACCUMULATE |
183 		LRADC_CH_NUM_SAMPLES(ts->over_sample_cnt - 1);
184 	writel(reg, ts->base + LRADC_CH(ch1));
185 	writel(reg, ts->base + LRADC_CH(ch2));
186 
187 	/* from the datasheet:
188 	 * "Software must clear this register in preparation for a
189 	 * multi-cycle accumulation.
190 	 */
191 	writel(LRADC_CH_VALUE_MASK,
192 	       ts->base + LRADC_CH(ch1) + STMP_OFFSET_REG_CLR);
193 	writel(LRADC_CH_VALUE_MASK,
194 	       ts->base + LRADC_CH(ch2) + STMP_OFFSET_REG_CLR);
195 
196 	/* prepare the delay/loop unit according to the oversampling count */
197 	writel(LRADC_DELAY_TRIGGER(1 << ch1) | LRADC_DELAY_TRIGGER(1 << ch2) |
198 	       LRADC_DELAY_TRIGGER_DELAYS(0) |
199 	       LRADC_DELAY_LOOP(ts->over_sample_cnt - 1) |
200 	       LRADC_DELAY_DELAY(ts->over_sample_delay - 1),
201 	       ts->base + LRADC_DELAY(3));
202 
203 	writel(LRADC_CTRL1_LRADC_IRQ(ch2),
204 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
205 
206 	/*
207 	 * after changing the touchscreen plates setting
208 	 * the signals need some initial time to settle. Start the
209 	 * SoC's delay unit and start the conversion later
210 	 * and automatically.
211 	 */
212 	writel(LRADC_DELAY_TRIGGER(0) | LRADC_DELAY_TRIGGER_DELAYS(BIT(3)) |
213 	       LRADC_DELAY_KICK | LRADC_DELAY_DELAY(ts->settling_delay),
214 	       ts->base + LRADC_DELAY(2));
215 }
216 
217 static unsigned int mxs_lradc_ts_read_raw_channel(struct mxs_lradc_ts *ts,
218 						  unsigned int channel)
219 {
220 	u32 reg;
221 	unsigned int num_samples, val;
222 
223 	reg = readl(ts->base + LRADC_CH(channel));
224 	if (reg & LRADC_CH_ACCUMULATE)
225 		num_samples = ts->over_sample_cnt;
226 	else
227 		num_samples = 1;
228 
229 	val = (reg & LRADC_CH_VALUE_MASK) >> LRADC_CH_VALUE_OFFSET;
230 	return val / num_samples;
231 }
232 
233 static unsigned int mxs_lradc_read_ts_pressure(struct mxs_lradc_ts *ts,
234 					unsigned int ch1, unsigned int ch2)
235 {
236 	u32 reg, mask;
237 	unsigned int pressure, m1, m2;
238 
239 	mask = LRADC_CTRL1_LRADC_IRQ(ch1) | LRADC_CTRL1_LRADC_IRQ(ch2);
240 	reg = readl(ts->base + LRADC_CTRL1) & mask;
241 
242 	while (reg != mask) {
243 		reg = readl(ts->base + LRADC_CTRL1) & mask;
244 		dev_dbg(ts->dev, "One channel is still busy: %X\n", reg);
245 	}
246 
247 	m1 = mxs_lradc_ts_read_raw_channel(ts, ch1);
248 	m2 = mxs_lradc_ts_read_raw_channel(ts, ch2);
249 
250 	if (m2 == 0) {
251 		dev_warn(ts->dev, "Cannot calculate pressure\n");
252 		return 1 << (LRADC_RESOLUTION - 1);
253 	}
254 
255 	/* simply scale the value from 0 ... max ADC resolution */
256 	pressure = m1;
257 	pressure *= (1 << LRADC_RESOLUTION);
258 	pressure /= m2;
259 
260 	dev_dbg(ts->dev, "Pressure = %u\n", pressure);
261 	return pressure;
262 }
263 
264 #define TS_CH_XP 2
265 #define TS_CH_YP 3
266 #define TS_CH_XM 4
267 #define TS_CH_YM 5
268 
269 /*
270  * YP(open)--+-------------+
271  *	     |		   |--+
272  *	     |		   |  |
273  *    YM(-)--+-------------+  |
274  *	       +--------------+
275  *	       |	      |
276  *	   XP(weak+)	    XM(open)
277  *
278  * "weak+" means 200k Ohm VDDIO
279  * (-) means GND
280  */
281 static void mxs_lradc_setup_touch_detection(struct mxs_lradc_ts *ts)
282 {
283 	struct mxs_lradc *lradc = ts->lradc;
284 
285 	/*
286 	 * In order to detect a touch event the 'touch detect enable' bit
287 	 * enables:
288 	 *  - a weak pullup to the X+ connector
289 	 *  - a strong ground at the Y- connector
290 	 */
291 	writel(info[lradc->soc].mask,
292 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
293 	writel(info[lradc->soc].bit,
294 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
295 }
296 
297 /*
298  * YP(meas)--+-------------+
299  *	     |		   |--+
300  *	     |		   |  |
301  * YM(open)--+-------------+  |
302  *	       +--------------+
303  *	       |	      |
304  *	     XP(+)	    XM(-)
305  *
306  * (+) means here 1.85 V
307  * (-) means here GND
308  */
309 static void mxs_lradc_prepare_x_pos(struct mxs_lradc_ts *ts)
310 {
311 	struct mxs_lradc *lradc = ts->lradc;
312 
313 	writel(info[lradc->soc].mask,
314 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
315 	writel(info[lradc->soc].x_plate,
316 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
317 
318 	ts->cur_plate = LRADC_SAMPLE_X;
319 	mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL1, TS_CH_YP);
320 	mxs_lradc_setup_ts_channel(ts, TOUCHSCREEN_VCHANNEL1);
321 }
322 
323 /*
324  *   YP(+)--+-------------+
325  *	    |		  |--+
326  *	    |		  |  |
327  *   YM(-)--+-------------+  |
328  *	      +--------------+
329  *	      |		     |
330  *	   XP(open)	   XM(meas)
331  *
332  * (+) means here 1.85 V
333  * (-) means here GND
334  */
335 static void mxs_lradc_prepare_y_pos(struct mxs_lradc_ts *ts)
336 {
337 	struct mxs_lradc *lradc = ts->lradc;
338 
339 	writel(info[lradc->soc].mask,
340 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
341 	writel(info[lradc->soc].y_plate,
342 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
343 
344 	ts->cur_plate = LRADC_SAMPLE_Y;
345 	mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL1, TS_CH_XM);
346 	mxs_lradc_setup_ts_channel(ts, TOUCHSCREEN_VCHANNEL1);
347 }
348 
349 /*
350  *    YP(+)--+-------------+
351  *	     |		   |--+
352  *	     |		   |  |
353  * YM(meas)--+-------------+  |
354  *	       +--------------+
355  *	       |	      |
356  *	    XP(meas)	    XM(-)
357  *
358  * (+) means here 1.85 V
359  * (-) means here GND
360  */
361 static void mxs_lradc_prepare_pressure(struct mxs_lradc_ts *ts)
362 {
363 	struct mxs_lradc *lradc = ts->lradc;
364 
365 	writel(info[lradc->soc].mask,
366 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
367 	writel(info[lradc->soc].pressure,
368 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
369 
370 	ts->cur_plate = LRADC_SAMPLE_PRESSURE;
371 	mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL1, TS_CH_YM);
372 	mxs_lradc_map_ts_channel(ts, TOUCHSCREEN_VCHANNEL2, TS_CH_XP);
373 	mxs_lradc_setup_ts_pressure(ts, TOUCHSCREEN_VCHANNEL2,
374 				    TOUCHSCREEN_VCHANNEL1);
375 }
376 
377 static void mxs_lradc_enable_touch_detection(struct mxs_lradc_ts *ts)
378 {
379 	mxs_lradc_setup_touch_detection(ts);
380 
381 	ts->cur_plate = LRADC_TOUCH;
382 	writel(LRADC_CTRL1_TOUCH_DETECT_IRQ | LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
383 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
384 	writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
385 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET);
386 }
387 
388 static void mxs_lradc_start_touch_event(struct mxs_lradc_ts *ts)
389 {
390 	writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
391 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
392 	writel(LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1),
393 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET);
394 	/*
395 	 * start with the Y-pos, because it uses nearly the same plate
396 	 * settings like the touch detection
397 	 */
398 	mxs_lradc_prepare_y_pos(ts);
399 }
400 
401 static void mxs_lradc_report_ts_event(struct mxs_lradc_ts *ts)
402 {
403 	input_report_abs(ts->ts_input, ABS_X, ts->ts_x_pos);
404 	input_report_abs(ts->ts_input, ABS_Y, ts->ts_y_pos);
405 	input_report_abs(ts->ts_input, ABS_PRESSURE, ts->ts_pressure);
406 	input_report_key(ts->ts_input, BTN_TOUCH, 1);
407 	input_sync(ts->ts_input);
408 }
409 
410 static void mxs_lradc_complete_touch_event(struct mxs_lradc_ts *ts)
411 {
412 	mxs_lradc_setup_touch_detection(ts);
413 	ts->cur_plate = LRADC_SAMPLE_VALID;
414 	/*
415 	 * start a dummy conversion to burn time to settle the signals
416 	 * note: we are not interested in the conversion's value
417 	 */
418 	writel(0, ts->base + LRADC_CH(TOUCHSCREEN_VCHANNEL1));
419 	writel(LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
420 	       LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2),
421 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
422 	writel(LRADC_DELAY_TRIGGER(1 << TOUCHSCREEN_VCHANNEL1) |
423 	       LRADC_DELAY_KICK | LRADC_DELAY_DELAY(10),
424 	       ts->base + LRADC_DELAY(2));
425 }
426 
427 /*
428  * in order to avoid false measurements, report only samples where
429  * the surface is still touched after the position measurement
430  */
431 static void mxs_lradc_finish_touch_event(struct mxs_lradc_ts *ts, bool valid)
432 {
433 	/* if it is still touched, report the sample */
434 	if (valid && mxs_lradc_check_touch_event(ts)) {
435 		ts->ts_valid = true;
436 		mxs_lradc_report_ts_event(ts);
437 	}
438 
439 	/* if it is even still touched, continue with the next measurement */
440 	if (mxs_lradc_check_touch_event(ts)) {
441 		mxs_lradc_prepare_y_pos(ts);
442 		return;
443 	}
444 
445 	if (ts->ts_valid) {
446 		/* signal the release */
447 		ts->ts_valid = false;
448 		input_report_key(ts->ts_input, BTN_TOUCH, 0);
449 		input_sync(ts->ts_input);
450 	}
451 
452 	/* if it is released, wait for the next touch via IRQ */
453 	ts->cur_plate = LRADC_TOUCH;
454 	writel(0, ts->base + LRADC_DELAY(2));
455 	writel(0, ts->base + LRADC_DELAY(3));
456 	writel(LRADC_CTRL1_TOUCH_DETECT_IRQ |
457 	       LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1) |
458 	       LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1),
459 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
460 	writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN,
461 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET);
462 }
463 
464 /* touchscreen's state machine */
465 static void mxs_lradc_handle_touch(struct mxs_lradc_ts *ts)
466 {
467 	switch (ts->cur_plate) {
468 	case LRADC_TOUCH:
469 		if (mxs_lradc_check_touch_event(ts))
470 			mxs_lradc_start_touch_event(ts);
471 		writel(LRADC_CTRL1_TOUCH_DETECT_IRQ,
472 		       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
473 		return;
474 
475 	case LRADC_SAMPLE_Y:
476 		ts->ts_y_pos =
477 		    mxs_lradc_ts_read_raw_channel(ts, TOUCHSCREEN_VCHANNEL1);
478 		mxs_lradc_prepare_x_pos(ts);
479 		return;
480 
481 	case LRADC_SAMPLE_X:
482 		ts->ts_x_pos =
483 		    mxs_lradc_ts_read_raw_channel(ts, TOUCHSCREEN_VCHANNEL1);
484 		mxs_lradc_prepare_pressure(ts);
485 		return;
486 
487 	case LRADC_SAMPLE_PRESSURE:
488 		ts->ts_pressure =
489 		    mxs_lradc_read_ts_pressure(ts,
490 					       TOUCHSCREEN_VCHANNEL2,
491 					       TOUCHSCREEN_VCHANNEL1);
492 		mxs_lradc_complete_touch_event(ts);
493 		return;
494 
495 	case LRADC_SAMPLE_VALID:
496 		mxs_lradc_finish_touch_event(ts, 1);
497 		break;
498 	}
499 }
500 
501 /* IRQ Handling */
502 static irqreturn_t mxs_lradc_ts_handle_irq(int irq, void *data)
503 {
504 	struct mxs_lradc_ts *ts = data;
505 	struct mxs_lradc *lradc = ts->lradc;
506 	unsigned long reg = readl(ts->base + LRADC_CTRL1);
507 	u32 clr_irq = mxs_lradc_irq_mask(lradc);
508 	const u32 ts_irq_mask =
509 		LRADC_CTRL1_TOUCH_DETECT_IRQ |
510 		LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
511 		LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2);
512 	unsigned long flags;
513 
514 	if (!(reg & mxs_lradc_irq_mask(lradc)))
515 		return IRQ_NONE;
516 
517 	if (reg & ts_irq_mask) {
518 		spin_lock_irqsave(&ts->lock, flags);
519 		mxs_lradc_handle_touch(ts);
520 		spin_unlock_irqrestore(&ts->lock, flags);
521 		/* Make sure we don't clear the next conversion's interrupt. */
522 		clr_irq &= ~(LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL1) |
523 				LRADC_CTRL1_LRADC_IRQ(TOUCHSCREEN_VCHANNEL2));
524 		writel(reg & clr_irq,
525 		       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
526 	}
527 
528 	return IRQ_HANDLED;
529 }
530 
531 static int mxs_lradc_ts_open(struct input_dev *dev)
532 {
533 	struct mxs_lradc_ts *ts = input_get_drvdata(dev);
534 
535 	/* Enable the touch-detect circuitry. */
536 	mxs_lradc_enable_touch_detection(ts);
537 
538 	return 0;
539 }
540 
541 static void mxs_lradc_ts_stop(struct mxs_lradc_ts *ts)
542 {
543 	int i;
544 	struct mxs_lradc *lradc = ts->lradc;
545 
546 	/* stop all interrupts from firing */
547 	writel(LRADC_CTRL1_TOUCH_DETECT_IRQ_EN |
548 	       LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL1) |
549 	       LRADC_CTRL1_LRADC_IRQ_EN(TOUCHSCREEN_VCHANNEL2),
550 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
551 
552 	/* Power-down touchscreen touch-detect circuitry. */
553 	writel(info[lradc->soc].mask,
554 	       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
555 
556 	writel(lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
557 	       ts->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
558 
559 	for (i = 1; i < LRADC_MAX_DELAY_CHANS; i++)
560 		writel(0, ts->base + LRADC_DELAY(i));
561 }
562 
563 static void mxs_lradc_ts_close(struct input_dev *dev)
564 {
565 	struct mxs_lradc_ts *ts = input_get_drvdata(dev);
566 
567 	mxs_lradc_ts_stop(ts);
568 }
569 
570 static void mxs_lradc_ts_hw_init(struct mxs_lradc_ts *ts)
571 {
572 	struct mxs_lradc *lradc = ts->lradc;
573 
574 	/* Configure the touchscreen type */
575 	if (lradc->soc == IMX28_LRADC) {
576 		writel(LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
577 		       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
578 
579 		if (lradc->touchscreen_wire == MXS_LRADC_TOUCHSCREEN_5WIRE)
580 			writel(LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
581 			       ts->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
582 	}
583 }
584 
585 static int mxs_lradc_ts_register(struct mxs_lradc_ts *ts)
586 {
587 	struct input_dev *input;
588 	struct device *dev = ts->dev;
589 
590 	input = devm_input_allocate_device(dev);
591 	if (!input)
592 		return -ENOMEM;
593 
594 	input->name = "mxs-lradc-ts";
595 	input->id.bustype = BUS_HOST;
596 	input->open = mxs_lradc_ts_open;
597 	input->close = mxs_lradc_ts_close;
598 
599 	__set_bit(INPUT_PROP_DIRECT, input->propbit);
600 	input_set_capability(input, EV_KEY, BTN_TOUCH);
601 	input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
602 	input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
603 	input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK,
604 			     0, 0);
605 
606 	ts->ts_input = input;
607 	input_set_drvdata(input, ts);
608 
609 	return input_register_device(input);
610 }
611 
612 static int mxs_lradc_ts_probe(struct platform_device *pdev)
613 {
614 	struct device *dev = &pdev->dev;
615 	struct device_node *node = dev->parent->of_node;
616 	struct mxs_lradc *lradc = dev_get_drvdata(dev->parent);
617 	struct mxs_lradc_ts *ts;
618 	struct resource *iores;
619 	int ret, irq, virq, i;
620 	u32 ts_wires = 0, adapt;
621 
622 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
623 	if (!ts)
624 		return -ENOMEM;
625 
626 	platform_set_drvdata(pdev, ts);
627 
628 	ts->lradc = lradc;
629 	ts->dev = dev;
630 	spin_lock_init(&ts->lock);
631 
632 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
633 	if (!iores)
634 		return -EINVAL;
635 	ts->base = devm_ioremap(dev, iores->start, resource_size(iores));
636 	if (!ts->base)
637 		return -ENOMEM;
638 
639 	ret = of_property_read_u32(node, "fsl,lradc-touchscreen-wires",
640 				   &ts_wires);
641 	if (ret)
642 		return ret;
643 
644 	if (of_property_read_u32(node, "fsl,ave-ctrl", &adapt)) {
645 		ts->over_sample_cnt = 4;
646 	} else {
647 		if (adapt >= 1 && adapt <= 32) {
648 			ts->over_sample_cnt = adapt;
649 		} else {
650 			dev_err(ts->dev, "Invalid sample count (%u)\n",
651 				adapt);
652 			return -EINVAL;
653 		}
654 	}
655 
656 	if (of_property_read_u32(node, "fsl,ave-delay", &adapt)) {
657 		ts->over_sample_delay = 2;
658 	} else {
659 		if (adapt >= 2 && adapt <= LRADC_DELAY_DELAY_MASK + 1) {
660 			ts->over_sample_delay = adapt;
661 		} else {
662 			dev_err(ts->dev, "Invalid sample delay (%u)\n",
663 				adapt);
664 			return -EINVAL;
665 		}
666 	}
667 
668 	if (of_property_read_u32(node, "fsl,settling", &adapt)) {
669 		ts->settling_delay = 10;
670 	} else {
671 		if (adapt >= 1 && adapt <= LRADC_DELAY_DELAY_MASK) {
672 			ts->settling_delay = adapt;
673 		} else {
674 			dev_err(ts->dev, "Invalid settling delay (%u)\n",
675 				adapt);
676 			return -EINVAL;
677 		}
678 	}
679 
680 	ret = stmp_reset_block(ts->base);
681 	if (ret)
682 		return ret;
683 
684 	mxs_lradc_ts_hw_init(ts);
685 
686 	for (i = 0; i < 3; i++) {
687 		irq = platform_get_irq_byname(pdev, mxs_lradc_ts_irq_names[i]);
688 		if (irq < 0)
689 			return irq;
690 
691 		virq = irq_of_parse_and_map(node, irq);
692 
693 		mxs_lradc_ts_stop(ts);
694 
695 		ret = devm_request_irq(dev, virq,
696 				       mxs_lradc_ts_handle_irq,
697 				       0, mxs_lradc_ts_irq_names[i], ts);
698 		if (ret)
699 			return ret;
700 	}
701 
702 	return mxs_lradc_ts_register(ts);
703 }
704 
705 static struct platform_driver mxs_lradc_ts_driver = {
706 	.driver	= {
707 		.name = "mxs-lradc-ts",
708 	},
709 	.probe	= mxs_lradc_ts_probe,
710 };
711 module_platform_driver(mxs_lradc_ts_driver);
712 
713 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
714 MODULE_DESCRIPTION("Freescale MXS LRADC touchscreen driver");
715 MODULE_LICENSE("GPL");
716 MODULE_ALIAS("platform:mxs-lradc-ts");
717