2 * Freescale i.MX6UL touchscreen controller driver
4 * Copyright (C) 2015 Freescale Semiconductor, Inc.
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.
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>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.h>
25 /* ADC configuration registers field define */
26 #define ADC_AIEN (0x1 << 7)
27 #define ADC_CONV_DISABLE 0x1F
28 #define ADC_CAL (0x1 << 7)
30 #define ADC_12BIT_MODE (0x2 << 2)
31 #define ADC_IPG_CLK 0x00
32 #define ADC_CLK_DIV_8 (0x03 << 5)
33 #define ADC_SHORT_SAMPLE_MODE (0x0 << 4)
34 #define ADC_HARDWARE_TRIGGER (0x1 << 13)
35 #define SELECT_CHANNEL_4 0x04
36 #define SELECT_CHANNEL_1 0x01
37 #define DISABLE_CONVERSION_INT (0x0 << 7)
40 #define REG_ADC_HC0 0x00
41 #define REG_ADC_HC1 0x04
42 #define REG_ADC_HC2 0x08
43 #define REG_ADC_HC3 0x0C
44 #define REG_ADC_HC4 0x10
45 #define REG_ADC_HS 0x14
46 #define REG_ADC_R0 0x18
47 #define REG_ADC_CFG 0x2C
48 #define REG_ADC_GC 0x30
49 #define REG_ADC_GS 0x34
51 #define ADC_TIMEOUT msecs_to_jiffies(100)
54 #define REG_TSC_BASIC_SETING 0x00
55 #define REG_TSC_PRE_CHARGE_TIME 0x10
56 #define REG_TSC_FLOW_CONTROL 0x20
57 #define REG_TSC_MEASURE_VALUE 0x30
58 #define REG_TSC_INT_EN 0x40
59 #define REG_TSC_INT_SIG_EN 0x50
60 #define REG_TSC_INT_STATUS 0x60
61 #define REG_TSC_DEBUG_MODE 0x70
62 #define REG_TSC_DEBUG_MODE2 0x80
64 /* TSC configuration registers field define */
65 #define DETECT_4_WIRE_MODE (0x0 << 4)
66 #define AUTO_MEASURE 0x1
67 #define MEASURE_SIGNAL 0x1
68 #define DETECT_SIGNAL (0x1 << 4)
69 #define VALID_SIGNAL (0x1 << 8)
70 #define MEASURE_INT_EN 0x1
71 #define MEASURE_SIG_EN 0x1
72 #define VALID_SIG_EN (0x1 << 8)
73 #define DE_GLITCH_2 (0x2 << 29)
74 #define START_SENSE (0x1 << 12)
75 #define TSC_DISABLE (0x1 << 16)
76 #define DETECT_MODE 0x2
80 struct input_dev *input;
81 void __iomem *tsc_regs;
82 void __iomem *adc_regs;
85 struct gpio_desc *xnur_gpio;
87 int measure_delay_time;
90 struct completion completion;
94 * TSC module need ADC to get the measure value. So
95 * before config TSC, we should initialize ADC module.
97 static void imx6ul_adc_init(struct imx6ul_tsc *tsc)
105 reinit_completion(&tsc->completion);
107 adc_cfg = readl(tsc->adc_regs + REG_ADC_CFG);
108 adc_cfg |= ADC_12BIT_MODE | ADC_IPG_CLK;
109 adc_cfg |= ADC_CLK_DIV_8 | ADC_SHORT_SAMPLE_MODE;
110 adc_cfg &= ~ADC_HARDWARE_TRIGGER;
111 writel(adc_cfg, tsc->adc_regs + REG_ADC_CFG);
113 /* enable calibration interrupt */
115 adc_hc |= ADC_CONV_DISABLE;
116 writel(adc_hc, tsc->adc_regs + REG_ADC_HC0);
118 /* start ADC calibration */
119 adc_gc = readl(tsc->adc_regs + REG_ADC_GC);
121 writel(adc_gc, tsc->adc_regs + REG_ADC_GC);
123 timeout = wait_for_completion_timeout
124 (&tsc->completion, ADC_TIMEOUT);
126 dev_err(tsc->dev, "Timeout for adc calibration\n");
128 adc_gs = readl(tsc->adc_regs + REG_ADC_GS);
129 if (adc_gs & ADC_CALF)
130 dev_err(tsc->dev, "ADC calibration failed\n");
132 /* TSC need the ADC work in hardware trigger */
133 adc_cfg = readl(tsc->adc_regs + REG_ADC_CFG);
134 adc_cfg |= ADC_HARDWARE_TRIGGER;
135 writel(adc_cfg, tsc->adc_regs + REG_ADC_CFG);
139 * This is a TSC workaround. Currently TSC misconnect two
140 * ADC channels, this function remap channel configure for
143 static void imx6ul_tsc_channel_config(struct imx6ul_tsc *tsc)
145 int adc_hc0, adc_hc1, adc_hc2, adc_hc3, adc_hc4;
147 adc_hc0 = DISABLE_CONVERSION_INT;
148 writel(adc_hc0, tsc->adc_regs + REG_ADC_HC0);
150 adc_hc1 = DISABLE_CONVERSION_INT | SELECT_CHANNEL_4;
151 writel(adc_hc1, tsc->adc_regs + REG_ADC_HC1);
153 adc_hc2 = DISABLE_CONVERSION_INT;
154 writel(adc_hc2, tsc->adc_regs + REG_ADC_HC2);
156 adc_hc3 = DISABLE_CONVERSION_INT | SELECT_CHANNEL_1;
157 writel(adc_hc3, tsc->adc_regs + REG_ADC_HC3);
159 adc_hc4 = DISABLE_CONVERSION_INT;
160 writel(adc_hc4, tsc->adc_regs + REG_ADC_HC4);
164 * TSC setting, confige the pre-charge time and measure delay time.
165 * different touch screen may need different pre-charge time and
166 * measure delay time.
168 static void imx6ul_tsc_set(struct imx6ul_tsc *tsc)
170 int basic_setting = 0;
173 basic_setting |= tsc->measure_delay_time << 8;
174 basic_setting |= DETECT_4_WIRE_MODE | AUTO_MEASURE;
175 writel(basic_setting, tsc->tsc_regs + REG_TSC_BASIC_SETING);
177 writel(DE_GLITCH_2, tsc->tsc_regs + REG_TSC_DEBUG_MODE2);
179 writel(tsc->pre_charge_time, tsc->tsc_regs + REG_TSC_PRE_CHARGE_TIME);
180 writel(MEASURE_INT_EN, tsc->tsc_regs + REG_TSC_INT_EN);
181 writel(MEASURE_SIG_EN | VALID_SIG_EN,
182 tsc->tsc_regs + REG_TSC_INT_SIG_EN);
184 /* start sense detection */
185 start = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
186 start |= START_SENSE;
187 start &= ~TSC_DISABLE;
188 writel(start, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
191 static void imx6ul_tsc_init(struct imx6ul_tsc *tsc)
193 imx6ul_adc_init(tsc);
194 imx6ul_tsc_channel_config(tsc);
198 static void imx6ul_tsc_disable(struct imx6ul_tsc *tsc)
203 /* TSC controller enters to idle status */
204 tsc_flow = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
205 tsc_flow |= TSC_DISABLE;
206 writel(tsc_flow, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
208 /* ADC controller enters to stop mode */
209 adc_cfg = readl(tsc->adc_regs + REG_ADC_HC0);
210 adc_cfg |= ADC_CONV_DISABLE;
211 writel(adc_cfg, tsc->adc_regs + REG_ADC_HC0);
214 /* Delay some time (max 2ms), wait the pre-charge done. */
215 static bool tsc_wait_detect_mode(struct imx6ul_tsc *tsc)
217 unsigned long timeout = jiffies + msecs_to_jiffies(2);
222 if (time_after(jiffies, timeout))
225 usleep_range(200, 400);
226 debug_mode2 = readl(tsc->tsc_regs + REG_TSC_DEBUG_MODE2);
227 state_machine = (debug_mode2 >> 20) & 0x7;
228 } while (state_machine != DETECT_MODE);
230 usleep_range(200, 400);
234 static irqreturn_t tsc_irq_fn(int irq, void *dev_id)
236 struct imx6ul_tsc *tsc = dev_id;
242 status = readl(tsc->tsc_regs + REG_TSC_INT_STATUS);
244 /* write 1 to clear the bit measure-signal */
245 writel(MEASURE_SIGNAL | DETECT_SIGNAL,
246 tsc->tsc_regs + REG_TSC_INT_STATUS);
248 /* It's a HW self-clean bit. Set this bit and start sense detection */
249 start = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
250 start |= START_SENSE;
251 writel(start, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
253 if (status & MEASURE_SIGNAL) {
254 value = readl(tsc->tsc_regs + REG_TSC_MEASURE_VALUE);
255 x = (value >> 16) & 0x0fff;
259 * In detect mode, we can get the xnur gpio value,
260 * otherwise assume contact is stiull active.
262 if (!tsc_wait_detect_mode(tsc) ||
263 gpiod_get_value_cansleep(tsc->xnur_gpio)) {
264 input_report_key(tsc->input, BTN_TOUCH, 1);
265 input_report_abs(tsc->input, ABS_X, x);
266 input_report_abs(tsc->input, ABS_Y, y);
268 input_report_key(tsc->input, BTN_TOUCH, 0);
271 input_sync(tsc->input);
277 static irqreturn_t adc_irq_fn(int irq, void *dev_id)
279 struct imx6ul_tsc *tsc = dev_id;
283 coco = readl(tsc->adc_regs + REG_ADC_HS);
285 value = readl(tsc->adc_regs + REG_ADC_R0);
286 complete(&tsc->completion);
292 static int imx6ul_tsc_open(struct input_dev *input_dev)
294 struct imx6ul_tsc *tsc = input_get_drvdata(input_dev);
297 err = clk_prepare_enable(tsc->adc_clk);
300 "Could not prepare or enable the adc clock: %d\n",
305 err = clk_prepare_enable(tsc->tsc_clk);
308 "Could not prepare or enable the tsc clock: %d\n",
310 clk_disable_unprepare(tsc->adc_clk);
314 imx6ul_tsc_init(tsc);
319 static void imx6ul_tsc_close(struct input_dev *input_dev)
321 struct imx6ul_tsc *tsc = input_get_drvdata(input_dev);
323 imx6ul_tsc_disable(tsc);
325 clk_disable_unprepare(tsc->tsc_clk);
326 clk_disable_unprepare(tsc->adc_clk);
329 static int imx6ul_tsc_probe(struct platform_device *pdev)
331 struct device_node *np = pdev->dev.of_node;
332 struct imx6ul_tsc *tsc;
333 struct input_dev *input_dev;
334 struct resource *tsc_mem;
335 struct resource *adc_mem;
340 tsc = devm_kzalloc(&pdev->dev, sizeof(struct imx6ul_tsc), GFP_KERNEL);
344 input_dev = devm_input_allocate_device(&pdev->dev);
348 input_dev->name = "iMX6UL TouchScreen Controller";
349 input_dev->id.bustype = BUS_HOST;
351 input_dev->open = imx6ul_tsc_open;
352 input_dev->close = imx6ul_tsc_close;
354 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
355 input_set_abs_params(input_dev, ABS_X, 0, 0xFFF, 0, 0);
356 input_set_abs_params(input_dev, ABS_Y, 0, 0xFFF, 0, 0);
358 input_set_drvdata(input_dev, tsc);
360 tsc->dev = &pdev->dev;
361 tsc->input = input_dev;
362 init_completion(&tsc->completion);
364 tsc->xnur_gpio = devm_gpiod_get(&pdev->dev, "xnur", GPIOD_IN);
365 if (IS_ERR(tsc->xnur_gpio)) {
366 err = PTR_ERR(tsc->xnur_gpio);
368 "failed to request GPIO tsc_X- (xnur): %d\n", err);
372 tsc_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
373 tsc->tsc_regs = devm_ioremap_resource(&pdev->dev, tsc_mem);
374 if (IS_ERR(tsc->tsc_regs)) {
375 err = PTR_ERR(tsc->tsc_regs);
376 dev_err(&pdev->dev, "failed to remap tsc memory: %d\n", err);
380 adc_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
381 tsc->adc_regs = devm_ioremap_resource(&pdev->dev, adc_mem);
382 if (IS_ERR(tsc->adc_regs)) {
383 err = PTR_ERR(tsc->adc_regs);
384 dev_err(&pdev->dev, "failed to remap adc memory: %d\n", err);
388 tsc->tsc_clk = devm_clk_get(&pdev->dev, "tsc");
389 if (IS_ERR(tsc->tsc_clk)) {
390 err = PTR_ERR(tsc->tsc_clk);
391 dev_err(&pdev->dev, "failed getting tsc clock: %d\n", err);
395 tsc->adc_clk = devm_clk_get(&pdev->dev, "adc");
396 if (IS_ERR(tsc->adc_clk)) {
397 err = PTR_ERR(tsc->adc_clk);
398 dev_err(&pdev->dev, "failed getting adc clock: %d\n", err);
402 tsc_irq = platform_get_irq(pdev, 0);
404 dev_err(&pdev->dev, "no tsc irq resource?\n");
408 adc_irq = platform_get_irq(pdev, 1);
410 dev_err(&pdev->dev, "no adc irq resource?\n");
414 err = devm_request_threaded_irq(tsc->dev, tsc_irq,
415 NULL, tsc_irq_fn, IRQF_ONESHOT,
416 dev_name(&pdev->dev), tsc);
419 "failed requesting tsc irq %d: %d\n",
424 err = devm_request_irq(tsc->dev, adc_irq, adc_irq_fn, 0,
425 dev_name(&pdev->dev), tsc);
428 "failed requesting adc irq %d: %d\n",
433 err = of_property_read_u32(np, "measure-delay-time",
434 &tsc->measure_delay_time);
436 tsc->measure_delay_time = 0xffff;
438 err = of_property_read_u32(np, "pre-charge-time",
439 &tsc->pre_charge_time);
441 tsc->pre_charge_time = 0xfff;
443 err = input_register_device(tsc->input);
446 "failed to register input device: %d\n", err);
450 platform_set_drvdata(pdev, tsc);
454 static int __maybe_unused imx6ul_tsc_suspend(struct device *dev)
456 struct platform_device *pdev = to_platform_device(dev);
457 struct imx6ul_tsc *tsc = platform_get_drvdata(pdev);
458 struct input_dev *input_dev = tsc->input;
460 mutex_lock(&input_dev->mutex);
462 if (input_dev->users) {
463 imx6ul_tsc_disable(tsc);
465 clk_disable_unprepare(tsc->tsc_clk);
466 clk_disable_unprepare(tsc->adc_clk);
469 mutex_unlock(&input_dev->mutex);
474 static int __maybe_unused imx6ul_tsc_resume(struct device *dev)
476 struct platform_device *pdev = to_platform_device(dev);
477 struct imx6ul_tsc *tsc = platform_get_drvdata(pdev);
478 struct input_dev *input_dev = tsc->input;
481 mutex_lock(&input_dev->mutex);
483 if (input_dev->users) {
484 retval = clk_prepare_enable(tsc->adc_clk);
488 retval = clk_prepare_enable(tsc->tsc_clk);
490 clk_disable_unprepare(tsc->adc_clk);
494 imx6ul_tsc_init(tsc);
498 mutex_unlock(&input_dev->mutex);
502 static SIMPLE_DEV_PM_OPS(imx6ul_tsc_pm_ops,
503 imx6ul_tsc_suspend, imx6ul_tsc_resume);
505 static const struct of_device_id imx6ul_tsc_match[] = {
506 { .compatible = "fsl,imx6ul-tsc", },
509 MODULE_DEVICE_TABLE(of, imx6ul_tsc_match);
511 static struct platform_driver imx6ul_tsc_driver = {
513 .name = "imx6ul-tsc",
514 .of_match_table = imx6ul_tsc_match,
515 .pm = &imx6ul_tsc_pm_ops,
517 .probe = imx6ul_tsc_probe,
519 module_platform_driver(imx6ul_tsc_driver);
521 MODULE_AUTHOR("Haibo Chen <haibo.chen@freescale.com>");
522 MODULE_DESCRIPTION("Freescale i.MX6UL Touchscreen controller driver");
523 MODULE_LICENSE("GPL v2");