blob: 00b336f37fb30312bcd4a5efd8c1ff2c4078d593 [file] [log] [blame]
Haibo Chen9a436d52015-09-05 11:31:21 -07001/*
2 * Freescale i.MX6UL touchscreen controller driver
3 *
4 * Copyright (C) 2015 Freescale Semiconductor, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/gpio/consumer.h>
15#include <linux/input.h>
16#include <linux/slab.h>
17#include <linux/completion.h>
18#include <linux/delay.h>
19#include <linux/of.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/clk.h>
23#include <linux/io.h>
24
25/* ADC configuration registers field define */
26#define ADC_AIEN (0x1 << 7)
27#define ADC_CONV_DISABLE 0x1F
Guy Shapiro031bfed2016-11-27 20:40:39 -080028#define ADC_AVGE (0x1 << 5)
Haibo Chen9a436d52015-09-05 11:31:21 -070029#define ADC_CAL (0x1 << 7)
30#define ADC_CALF 0x2
31#define ADC_12BIT_MODE (0x2 << 2)
32#define ADC_IPG_CLK 0x00
33#define ADC_CLK_DIV_8 (0x03 << 5)
34#define ADC_SHORT_SAMPLE_MODE (0x0 << 4)
35#define ADC_HARDWARE_TRIGGER (0x1 << 13)
Guy Shapiro031bfed2016-11-27 20:40:39 -080036#define ADC_AVGS_SHIFT 14
Haibo Chen9a436d52015-09-05 11:31:21 -070037#define SELECT_CHANNEL_4 0x04
38#define SELECT_CHANNEL_1 0x01
39#define DISABLE_CONVERSION_INT (0x0 << 7)
40
41/* ADC registers */
42#define REG_ADC_HC0 0x00
43#define REG_ADC_HC1 0x04
44#define REG_ADC_HC2 0x08
45#define REG_ADC_HC3 0x0C
46#define REG_ADC_HC4 0x10
47#define REG_ADC_HS 0x14
48#define REG_ADC_R0 0x18
49#define REG_ADC_CFG 0x2C
50#define REG_ADC_GC 0x30
51#define REG_ADC_GS 0x34
52
53#define ADC_TIMEOUT msecs_to_jiffies(100)
54
55/* TSC registers */
56#define REG_TSC_BASIC_SETING 0x00
57#define REG_TSC_PRE_CHARGE_TIME 0x10
58#define REG_TSC_FLOW_CONTROL 0x20
59#define REG_TSC_MEASURE_VALUE 0x30
60#define REG_TSC_INT_EN 0x40
61#define REG_TSC_INT_SIG_EN 0x50
62#define REG_TSC_INT_STATUS 0x60
63#define REG_TSC_DEBUG_MODE 0x70
64#define REG_TSC_DEBUG_MODE2 0x80
65
66/* TSC configuration registers field define */
67#define DETECT_4_WIRE_MODE (0x0 << 4)
68#define AUTO_MEASURE 0x1
69#define MEASURE_SIGNAL 0x1
70#define DETECT_SIGNAL (0x1 << 4)
71#define VALID_SIGNAL (0x1 << 8)
72#define MEASURE_INT_EN 0x1
73#define MEASURE_SIG_EN 0x1
74#define VALID_SIG_EN (0x1 << 8)
75#define DE_GLITCH_2 (0x2 << 29)
76#define START_SENSE (0x1 << 12)
77#define TSC_DISABLE (0x1 << 16)
78#define DETECT_MODE 0x2
79
80struct imx6ul_tsc {
81 struct device *dev;
82 struct input_dev *input;
83 void __iomem *tsc_regs;
84 void __iomem *adc_regs;
85 struct clk *tsc_clk;
86 struct clk *adc_clk;
87 struct gpio_desc *xnur_gpio;
88
89 int measure_delay_time;
90 int pre_charge_time;
Guy Shapiro031bfed2016-11-27 20:40:39 -080091 int average_samples;
Haibo Chen9a436d52015-09-05 11:31:21 -070092
93 struct completion completion;
94};
95
96/*
97 * TSC module need ADC to get the measure value. So
98 * before config TSC, we should initialize ADC module.
99 */
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700100static int imx6ul_adc_init(struct imx6ul_tsc *tsc)
Haibo Chen9a436d52015-09-05 11:31:21 -0700101{
102 int adc_hc = 0;
103 int adc_gc;
104 int adc_gs;
105 int adc_cfg;
106 int timeout;
107
108 reinit_completion(&tsc->completion);
109
110 adc_cfg = readl(tsc->adc_regs + REG_ADC_CFG);
111 adc_cfg |= ADC_12BIT_MODE | ADC_IPG_CLK;
112 adc_cfg |= ADC_CLK_DIV_8 | ADC_SHORT_SAMPLE_MODE;
Guy Shapiro031bfed2016-11-27 20:40:39 -0800113 if (tsc->average_samples)
114 adc_cfg |= (tsc->average_samples - 1) << ADC_AVGS_SHIFT;
Haibo Chen9a436d52015-09-05 11:31:21 -0700115 adc_cfg &= ~ADC_HARDWARE_TRIGGER;
116 writel(adc_cfg, tsc->adc_regs + REG_ADC_CFG);
117
118 /* enable calibration interrupt */
119 adc_hc |= ADC_AIEN;
120 adc_hc |= ADC_CONV_DISABLE;
121 writel(adc_hc, tsc->adc_regs + REG_ADC_HC0);
122
123 /* start ADC calibration */
124 adc_gc = readl(tsc->adc_regs + REG_ADC_GC);
125 adc_gc |= ADC_CAL;
Guy Shapiro031bfed2016-11-27 20:40:39 -0800126 if (tsc->average_samples)
127 adc_gc |= ADC_AVGE;
Haibo Chen9a436d52015-09-05 11:31:21 -0700128 writel(adc_gc, tsc->adc_regs + REG_ADC_GC);
129
130 timeout = wait_for_completion_timeout
131 (&tsc->completion, ADC_TIMEOUT);
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700132 if (timeout == 0) {
Haibo Chen9a436d52015-09-05 11:31:21 -0700133 dev_err(tsc->dev, "Timeout for adc calibration\n");
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700134 return -ETIMEDOUT;
135 }
Haibo Chen9a436d52015-09-05 11:31:21 -0700136
137 adc_gs = readl(tsc->adc_regs + REG_ADC_GS);
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700138 if (adc_gs & ADC_CALF) {
Haibo Chen9a436d52015-09-05 11:31:21 -0700139 dev_err(tsc->dev, "ADC calibration failed\n");
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700140 return -EINVAL;
141 }
Haibo Chen9a436d52015-09-05 11:31:21 -0700142
143 /* TSC need the ADC work in hardware trigger */
144 adc_cfg = readl(tsc->adc_regs + REG_ADC_CFG);
145 adc_cfg |= ADC_HARDWARE_TRIGGER;
146 writel(adc_cfg, tsc->adc_regs + REG_ADC_CFG);
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700147
148 return 0;
Haibo Chen9a436d52015-09-05 11:31:21 -0700149}
150
151/*
152 * This is a TSC workaround. Currently TSC misconnect two
153 * ADC channels, this function remap channel configure for
154 * hardware trigger.
155 */
156static void imx6ul_tsc_channel_config(struct imx6ul_tsc *tsc)
157{
158 int adc_hc0, adc_hc1, adc_hc2, adc_hc3, adc_hc4;
159
160 adc_hc0 = DISABLE_CONVERSION_INT;
161 writel(adc_hc0, tsc->adc_regs + REG_ADC_HC0);
162
163 adc_hc1 = DISABLE_CONVERSION_INT | SELECT_CHANNEL_4;
164 writel(adc_hc1, tsc->adc_regs + REG_ADC_HC1);
165
166 adc_hc2 = DISABLE_CONVERSION_INT;
167 writel(adc_hc2, tsc->adc_regs + REG_ADC_HC2);
168
169 adc_hc3 = DISABLE_CONVERSION_INT | SELECT_CHANNEL_1;
170 writel(adc_hc3, tsc->adc_regs + REG_ADC_HC3);
171
172 adc_hc4 = DISABLE_CONVERSION_INT;
173 writel(adc_hc4, tsc->adc_regs + REG_ADC_HC4);
174}
175
176/*
177 * TSC setting, confige the pre-charge time and measure delay time.
178 * different touch screen may need different pre-charge time and
179 * measure delay time.
180 */
181static void imx6ul_tsc_set(struct imx6ul_tsc *tsc)
182{
183 int basic_setting = 0;
184 int start;
185
186 basic_setting |= tsc->measure_delay_time << 8;
187 basic_setting |= DETECT_4_WIRE_MODE | AUTO_MEASURE;
188 writel(basic_setting, tsc->tsc_regs + REG_TSC_BASIC_SETING);
189
190 writel(DE_GLITCH_2, tsc->tsc_regs + REG_TSC_DEBUG_MODE2);
191
192 writel(tsc->pre_charge_time, tsc->tsc_regs + REG_TSC_PRE_CHARGE_TIME);
193 writel(MEASURE_INT_EN, tsc->tsc_regs + REG_TSC_INT_EN);
194 writel(MEASURE_SIG_EN | VALID_SIG_EN,
195 tsc->tsc_regs + REG_TSC_INT_SIG_EN);
196
197 /* start sense detection */
198 start = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
199 start |= START_SENSE;
200 start &= ~TSC_DISABLE;
201 writel(start, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
202}
203
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700204static int imx6ul_tsc_init(struct imx6ul_tsc *tsc)
Haibo Chen9a436d52015-09-05 11:31:21 -0700205{
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700206 int err;
207
208 err = imx6ul_adc_init(tsc);
209 if (err)
210 return err;
Haibo Chen9a436d52015-09-05 11:31:21 -0700211 imx6ul_tsc_channel_config(tsc);
212 imx6ul_tsc_set(tsc);
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700213
214 return 0;
Haibo Chen9a436d52015-09-05 11:31:21 -0700215}
216
217static void imx6ul_tsc_disable(struct imx6ul_tsc *tsc)
218{
219 int tsc_flow;
220 int adc_cfg;
221
222 /* TSC controller enters to idle status */
223 tsc_flow = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
224 tsc_flow |= TSC_DISABLE;
225 writel(tsc_flow, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
226
227 /* ADC controller enters to stop mode */
228 adc_cfg = readl(tsc->adc_regs + REG_ADC_HC0);
229 adc_cfg |= ADC_CONV_DISABLE;
230 writel(adc_cfg, tsc->adc_regs + REG_ADC_HC0);
231}
232
233/* Delay some time (max 2ms), wait the pre-charge done. */
234static bool tsc_wait_detect_mode(struct imx6ul_tsc *tsc)
235{
236 unsigned long timeout = jiffies + msecs_to_jiffies(2);
237 int state_machine;
238 int debug_mode2;
239
240 do {
241 if (time_after(jiffies, timeout))
242 return false;
243
244 usleep_range(200, 400);
245 debug_mode2 = readl(tsc->tsc_regs + REG_TSC_DEBUG_MODE2);
246 state_machine = (debug_mode2 >> 20) & 0x7;
247 } while (state_machine != DETECT_MODE);
248
249 usleep_range(200, 400);
250 return true;
251}
252
253static irqreturn_t tsc_irq_fn(int irq, void *dev_id)
254{
255 struct imx6ul_tsc *tsc = dev_id;
256 int status;
257 int value;
258 int x, y;
259 int start;
260
261 status = readl(tsc->tsc_regs + REG_TSC_INT_STATUS);
262
263 /* write 1 to clear the bit measure-signal */
264 writel(MEASURE_SIGNAL | DETECT_SIGNAL,
265 tsc->tsc_regs + REG_TSC_INT_STATUS);
266
267 /* It's a HW self-clean bit. Set this bit and start sense detection */
268 start = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
269 start |= START_SENSE;
270 writel(start, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
271
272 if (status & MEASURE_SIGNAL) {
273 value = readl(tsc->tsc_regs + REG_TSC_MEASURE_VALUE);
274 x = (value >> 16) & 0x0fff;
275 y = value & 0x0fff;
276
277 /*
278 * In detect mode, we can get the xnur gpio value,
279 * otherwise assume contact is stiull active.
280 */
281 if (!tsc_wait_detect_mode(tsc) ||
282 gpiod_get_value_cansleep(tsc->xnur_gpio)) {
283 input_report_key(tsc->input, BTN_TOUCH, 1);
284 input_report_abs(tsc->input, ABS_X, x);
285 input_report_abs(tsc->input, ABS_Y, y);
286 } else {
287 input_report_key(tsc->input, BTN_TOUCH, 0);
288 }
289
290 input_sync(tsc->input);
291 }
292
293 return IRQ_HANDLED;
294}
295
296static irqreturn_t adc_irq_fn(int irq, void *dev_id)
297{
298 struct imx6ul_tsc *tsc = dev_id;
299 int coco;
300 int value;
301
302 coco = readl(tsc->adc_regs + REG_ADC_HS);
303 if (coco & 0x01) {
304 value = readl(tsc->adc_regs + REG_ADC_R0);
305 complete(&tsc->completion);
306 }
307
308 return IRQ_HANDLED;
309}
310
311static int imx6ul_tsc_open(struct input_dev *input_dev)
312{
313 struct imx6ul_tsc *tsc = input_get_drvdata(input_dev);
314 int err;
315
316 err = clk_prepare_enable(tsc->adc_clk);
317 if (err) {
318 dev_err(tsc->dev,
319 "Could not prepare or enable the adc clock: %d\n",
320 err);
321 return err;
322 }
323
324 err = clk_prepare_enable(tsc->tsc_clk);
325 if (err) {
326 dev_err(tsc->dev,
327 "Could not prepare or enable the tsc clock: %d\n",
328 err);
329 clk_disable_unprepare(tsc->adc_clk);
330 return err;
331 }
332
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700333 return imx6ul_tsc_init(tsc);
Haibo Chen9a436d52015-09-05 11:31:21 -0700334}
335
336static void imx6ul_tsc_close(struct input_dev *input_dev)
337{
338 struct imx6ul_tsc *tsc = input_get_drvdata(input_dev);
339
340 imx6ul_tsc_disable(tsc);
341
342 clk_disable_unprepare(tsc->tsc_clk);
343 clk_disable_unprepare(tsc->adc_clk);
344}
345
346static int imx6ul_tsc_probe(struct platform_device *pdev)
347{
348 struct device_node *np = pdev->dev.of_node;
349 struct imx6ul_tsc *tsc;
350 struct input_dev *input_dev;
351 struct resource *tsc_mem;
352 struct resource *adc_mem;
353 int err;
354 int tsc_irq;
355 int adc_irq;
356
Fabio Estevam5eab3cf2015-09-14 10:37:31 -0700357 tsc = devm_kzalloc(&pdev->dev, sizeof(*tsc), GFP_KERNEL);
Haibo Chen9a436d52015-09-05 11:31:21 -0700358 if (!tsc)
359 return -ENOMEM;
360
361 input_dev = devm_input_allocate_device(&pdev->dev);
362 if (!input_dev)
363 return -ENOMEM;
364
Fabio Estevam002801f2015-09-14 10:37:55 -0700365 input_dev->name = "iMX6UL Touchscreen Controller";
Haibo Chen9a436d52015-09-05 11:31:21 -0700366 input_dev->id.bustype = BUS_HOST;
367
368 input_dev->open = imx6ul_tsc_open;
369 input_dev->close = imx6ul_tsc_close;
370
371 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
372 input_set_abs_params(input_dev, ABS_X, 0, 0xFFF, 0, 0);
373 input_set_abs_params(input_dev, ABS_Y, 0, 0xFFF, 0, 0);
374
375 input_set_drvdata(input_dev, tsc);
376
377 tsc->dev = &pdev->dev;
378 tsc->input = input_dev;
379 init_completion(&tsc->completion);
380
381 tsc->xnur_gpio = devm_gpiod_get(&pdev->dev, "xnur", GPIOD_IN);
382 if (IS_ERR(tsc->xnur_gpio)) {
383 err = PTR_ERR(tsc->xnur_gpio);
384 dev_err(&pdev->dev,
385 "failed to request GPIO tsc_X- (xnur): %d\n", err);
386 return err;
387 }
388
389 tsc_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
390 tsc->tsc_regs = devm_ioremap_resource(&pdev->dev, tsc_mem);
391 if (IS_ERR(tsc->tsc_regs)) {
392 err = PTR_ERR(tsc->tsc_regs);
393 dev_err(&pdev->dev, "failed to remap tsc memory: %d\n", err);
394 return err;
395 }
396
397 adc_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
398 tsc->adc_regs = devm_ioremap_resource(&pdev->dev, adc_mem);
399 if (IS_ERR(tsc->adc_regs)) {
400 err = PTR_ERR(tsc->adc_regs);
401 dev_err(&pdev->dev, "failed to remap adc memory: %d\n", err);
402 return err;
403 }
404
405 tsc->tsc_clk = devm_clk_get(&pdev->dev, "tsc");
406 if (IS_ERR(tsc->tsc_clk)) {
407 err = PTR_ERR(tsc->tsc_clk);
408 dev_err(&pdev->dev, "failed getting tsc clock: %d\n", err);
409 return err;
410 }
411
412 tsc->adc_clk = devm_clk_get(&pdev->dev, "adc");
413 if (IS_ERR(tsc->adc_clk)) {
414 err = PTR_ERR(tsc->adc_clk);
415 dev_err(&pdev->dev, "failed getting adc clock: %d\n", err);
416 return err;
417 }
418
419 tsc_irq = platform_get_irq(pdev, 0);
420 if (tsc_irq < 0) {
421 dev_err(&pdev->dev, "no tsc irq resource?\n");
422 return tsc_irq;
423 }
424
425 adc_irq = platform_get_irq(pdev, 1);
Fabio Estevam3905de62015-09-14 10:37:08 -0700426 if (adc_irq < 0) {
Haibo Chen9a436d52015-09-05 11:31:21 -0700427 dev_err(&pdev->dev, "no adc irq resource?\n");
428 return adc_irq;
429 }
430
431 err = devm_request_threaded_irq(tsc->dev, tsc_irq,
432 NULL, tsc_irq_fn, IRQF_ONESHOT,
433 dev_name(&pdev->dev), tsc);
434 if (err) {
435 dev_err(&pdev->dev,
436 "failed requesting tsc irq %d: %d\n",
437 tsc_irq, err);
438 return err;
439 }
440
441 err = devm_request_irq(tsc->dev, adc_irq, adc_irq_fn, 0,
442 dev_name(&pdev->dev), tsc);
443 if (err) {
444 dev_err(&pdev->dev,
445 "failed requesting adc irq %d: %d\n",
446 adc_irq, err);
447 return err;
448 }
449
450 err = of_property_read_u32(np, "measure-delay-time",
451 &tsc->measure_delay_time);
452 if (err)
453 tsc->measure_delay_time = 0xffff;
454
455 err = of_property_read_u32(np, "pre-charge-time",
456 &tsc->pre_charge_time);
457 if (err)
458 tsc->pre_charge_time = 0xfff;
459
Guy Shapiro031bfed2016-11-27 20:40:39 -0800460 err = of_property_read_u32(np, "average-samples",
461 &tsc->average_samples);
462 if (err)
463 tsc->average_samples = 0;
464
465 if (tsc->average_samples > 4) {
466 dev_err(&pdev->dev, "average-samples (%u) must be [0-4]\n",
467 tsc->average_samples);
468 return -EINVAL;
469 }
470
Haibo Chen9a436d52015-09-05 11:31:21 -0700471 err = input_register_device(tsc->input);
472 if (err) {
473 dev_err(&pdev->dev,
474 "failed to register input device: %d\n", err);
475 return err;
476 }
477
478 platform_set_drvdata(pdev, tsc);
479 return 0;
480}
481
482static int __maybe_unused imx6ul_tsc_suspend(struct device *dev)
483{
484 struct platform_device *pdev = to_platform_device(dev);
485 struct imx6ul_tsc *tsc = platform_get_drvdata(pdev);
486 struct input_dev *input_dev = tsc->input;
487
488 mutex_lock(&input_dev->mutex);
489
490 if (input_dev->users) {
491 imx6ul_tsc_disable(tsc);
492
493 clk_disable_unprepare(tsc->tsc_clk);
494 clk_disable_unprepare(tsc->adc_clk);
495 }
496
497 mutex_unlock(&input_dev->mutex);
498
499 return 0;
500}
501
502static int __maybe_unused imx6ul_tsc_resume(struct device *dev)
503{
504 struct platform_device *pdev = to_platform_device(dev);
505 struct imx6ul_tsc *tsc = platform_get_drvdata(pdev);
506 struct input_dev *input_dev = tsc->input;
507 int retval = 0;
508
509 mutex_lock(&input_dev->mutex);
510
511 if (input_dev->users) {
512 retval = clk_prepare_enable(tsc->adc_clk);
513 if (retval)
514 goto out;
515
516 retval = clk_prepare_enable(tsc->tsc_clk);
517 if (retval) {
518 clk_disable_unprepare(tsc->adc_clk);
519 goto out;
520 }
521
Fabio Estevam6cc527b2015-09-14 10:36:35 -0700522 retval = imx6ul_tsc_init(tsc);
Haibo Chen9a436d52015-09-05 11:31:21 -0700523 }
524
525out:
526 mutex_unlock(&input_dev->mutex);
527 return retval;
528}
529
530static SIMPLE_DEV_PM_OPS(imx6ul_tsc_pm_ops,
531 imx6ul_tsc_suspend, imx6ul_tsc_resume);
532
533static const struct of_device_id imx6ul_tsc_match[] = {
534 { .compatible = "fsl,imx6ul-tsc", },
535 { /* sentinel */ }
536};
537MODULE_DEVICE_TABLE(of, imx6ul_tsc_match);
538
539static struct platform_driver imx6ul_tsc_driver = {
540 .driver = {
541 .name = "imx6ul-tsc",
542 .of_match_table = imx6ul_tsc_match,
543 .pm = &imx6ul_tsc_pm_ops,
544 },
545 .probe = imx6ul_tsc_probe,
546};
547module_platform_driver(imx6ul_tsc_driver);
548
549MODULE_AUTHOR("Haibo Chen <haibo.chen@freescale.com>");
550MODULE_DESCRIPTION("Freescale i.MX6UL Touchscreen controller driver");
551MODULE_LICENSE("GPL v2");