1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom Corporation
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/input.h>
9 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 #include <linux/keyboard.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
17 #include <linux/clk.h>
18 #include <linux/serio.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/regmap.h>
22 #define IPROC_TS_NAME "iproc-ts"
24 #define PEN_DOWN_STATUS 1
25 #define PEN_UP_STATUS 0
32 /* Value given by controller for invalid coordinate. */
33 #define INVALID_COORD 0xFFFFFFFF
35 /* Register offsets */
38 #define INTERRUPT_THRES 0x08
39 #define INTERRUPT_MASK 0x0c
41 #define INTERRUPT_STATUS 0x10
42 #define CONTROLLER_STATUS 0x14
43 #define FIFO_DATA 0x18
44 #define FIFO_DATA_X_Y_MASK 0xFFFF
45 #define ANALOG_CONTROL 0x1c
48 #define DEBOUNCE_CNTR_STAT 0x24
49 #define SCAN_CNTR_STAT 0x28
50 #define REM_CNTR_STAT 0x2c
52 #define SETTLING_TIMER_STAT 0x30
53 #define SPARE_REG 0x34
54 #define SOFT_BYPASS_CONTROL 0x38
55 #define SOFT_BYPASS_DATA 0x3c
58 /* Bit values for INTERRUPT_MASK and INTERRUPT_STATUS regs */
59 #define TS_PEN_INTR_MASK BIT(0)
60 #define TS_FIFO_INTR_MASK BIT(2)
62 /* Bit values for CONTROLLER_STATUS reg1 */
63 #define TS_PEN_DOWN BIT(0)
65 /* Shift values for control reg1 */
66 #define SCANNING_PERIOD_SHIFT 24
67 #define DEBOUNCE_TIMEOUT_SHIFT 16
68 #define SETTLING_TIMEOUT_SHIFT 8
69 #define TOUCH_TIMEOUT_SHIFT 0
71 /* Shift values for coordinates from fifo */
72 #define X_COORD_SHIFT 0
73 #define Y_COORD_SHIFT 16
75 /* Bit values for REGCTL2 */
76 #define TS_CONTROLLER_EN_BIT BIT(16)
77 #define TS_CONTROLLER_AVGDATA_SHIFT 8
78 #define TS_CONTROLLER_AVGDATA_MASK (0x7 << TS_CONTROLLER_AVGDATA_SHIFT)
79 #define TS_CONTROLLER_PWR_LDO BIT(5)
80 #define TS_CONTROLLER_PWR_ADC BIT(4)
81 #define TS_CONTROLLER_PWR_BGP BIT(3)
82 #define TS_CONTROLLER_PWR_TS BIT(2)
83 #define TS_WIRE_MODE_BIT BIT(1)
85 #define dbg_reg(dev, priv, reg) \
88 regmap_read(priv->regmap, reg, &val); \
89 dev_dbg(dev, "%20s= 0x%08x\n", #reg, val); \
93 /* Each step is 1024 us. Valid 1-256 */
96 /* Each step is 512 us. Valid 0-255 */
100 * The settling duration (in ms) is the amount of time the tsc
101 * waits to allow the voltage to settle after turning on the
102 * drivers in detection mode. Valid values: 0-11
116 u32 settling_timeout
;
118 /* touch timeout in sample counts */
122 * Number of data samples which are averaged before a final data point
123 * is placed into the FIFO
130 /* Optional standard touchscreen properties. */
139 struct iproc_ts_priv
{
140 struct platform_device
*pdev
;
141 struct input_dev
*idev
;
143 struct regmap
*regmap
;
147 struct tsc_param cfg_params
;
151 * Set default values the same as hardware reset values
152 * except for fifo_threshold with is set to 1.
154 static const struct tsc_param iproc_default_config
= {
155 .scanning_period
= 0x5, /* 1 to 256 */
156 .debounce_timeout
= 0x28, /* 0 to 255 */
157 .settling_timeout
= 0x7, /* 0 to 11 */
158 .touch_timeout
= 0xa, /* 0 to 255 */
159 .average_data
= 5, /* entry 5 = 32 pts */
160 .fifo_threshold
= 1, /* 0 to 31 */
165 static void ts_reg_dump(struct iproc_ts_priv
*priv
)
167 struct device
*dev
= &priv
->pdev
->dev
;
169 dbg_reg(dev
, priv
, REGCTL1
);
170 dbg_reg(dev
, priv
, REGCTL2
);
171 dbg_reg(dev
, priv
, INTERRUPT_THRES
);
172 dbg_reg(dev
, priv
, INTERRUPT_MASK
);
173 dbg_reg(dev
, priv
, INTERRUPT_STATUS
);
174 dbg_reg(dev
, priv
, CONTROLLER_STATUS
);
175 dbg_reg(dev
, priv
, FIFO_DATA
);
176 dbg_reg(dev
, priv
, ANALOG_CONTROL
);
177 dbg_reg(dev
, priv
, AUX_DATA
);
178 dbg_reg(dev
, priv
, DEBOUNCE_CNTR_STAT
);
179 dbg_reg(dev
, priv
, SCAN_CNTR_STAT
);
180 dbg_reg(dev
, priv
, REM_CNTR_STAT
);
181 dbg_reg(dev
, priv
, SETTLING_TIMER_STAT
);
182 dbg_reg(dev
, priv
, SPARE_REG
);
183 dbg_reg(dev
, priv
, SOFT_BYPASS_CONTROL
);
184 dbg_reg(dev
, priv
, SOFT_BYPASS_DATA
);
187 static irqreturn_t
iproc_touchscreen_interrupt(int irq
, void *data
)
189 struct platform_device
*pdev
= data
;
190 struct iproc_ts_priv
*priv
= platform_get_drvdata(pdev
);
196 bool needs_sync
= false;
198 regmap_read(priv
->regmap
, INTERRUPT_STATUS
, &intr_status
);
199 intr_status
&= TS_PEN_INTR_MASK
| TS_FIFO_INTR_MASK
;
200 if (intr_status
== 0)
203 /* Clear all interrupt status bits, write-1-clear */
204 regmap_write(priv
->regmap
, INTERRUPT_STATUS
, intr_status
);
206 if (intr_status
& TS_PEN_INTR_MASK
) {
207 regmap_read(priv
->regmap
, CONTROLLER_STATUS
, &priv
->pen_status
);
208 if (priv
->pen_status
& TS_PEN_DOWN
)
209 priv
->pen_status
= PEN_DOWN_STATUS
;
211 priv
->pen_status
= PEN_UP_STATUS
;
213 input_report_key(priv
->idev
, BTN_TOUCH
, priv
->pen_status
);
216 dev_dbg(&priv
->pdev
->dev
,
217 "pen up-down (%d)\n", priv
->pen_status
);
220 /* coordinates in FIFO exceed the threshold */
221 if (intr_status
& TS_FIFO_INTR_MASK
) {
222 for (i
= 0; i
< priv
->cfg_params
.fifo_threshold
; i
++) {
223 regmap_read(priv
->regmap
, FIFO_DATA
, &raw_coordinate
);
224 if (raw_coordinate
== INVALID_COORD
)
228 * The x and y coordinate are 16 bits each
229 * with the x in the lower 16 bits and y in the
232 x
= (raw_coordinate
>> X_COORD_SHIFT
) &
234 y
= (raw_coordinate
>> Y_COORD_SHIFT
) &
237 /* We only want to retain the 12 msb of the 16 */
238 x
= (x
>> 4) & 0x0FFF;
239 y
= (y
>> 4) & 0x0FFF;
241 /* Adjust x y according to LCD tsc mount angle. */
242 if (priv
->cfg_params
.invert_x
)
243 x
= priv
->cfg_params
.max_x
- x
;
245 if (priv
->cfg_params
.invert_y
)
246 y
= priv
->cfg_params
.max_y
- y
;
248 input_report_abs(priv
->idev
, ABS_X
, x
);
249 input_report_abs(priv
->idev
, ABS_Y
, y
);
252 dev_dbg(&priv
->pdev
->dev
, "xy (0x%x 0x%x)\n", x
, y
);
257 input_sync(priv
->idev
);
262 static int iproc_ts_start(struct input_dev
*idev
)
267 struct iproc_ts_priv
*priv
= input_get_drvdata(idev
);
270 error
= clk_prepare_enable(priv
->tsc_clk
);
272 dev_err(&priv
->pdev
->dev
, "%s clk_prepare_enable failed %d\n",
278 * Interrupt is generated when:
279 * FIFO reaches the int_th value, and pen event(up/down)
281 val
= TS_PEN_INTR_MASK
| TS_FIFO_INTR_MASK
;
282 regmap_update_bits(priv
->regmap
, INTERRUPT_MASK
, val
, val
);
284 val
= priv
->cfg_params
.fifo_threshold
;
285 regmap_write(priv
->regmap
, INTERRUPT_THRES
, val
);
287 /* Initialize control reg1 */
289 val
|= priv
->cfg_params
.scanning_period
<< SCANNING_PERIOD_SHIFT
;
290 val
|= priv
->cfg_params
.debounce_timeout
<< DEBOUNCE_TIMEOUT_SHIFT
;
291 val
|= priv
->cfg_params
.settling_timeout
<< SETTLING_TIMEOUT_SHIFT
;
292 val
|= priv
->cfg_params
.touch_timeout
<< TOUCH_TIMEOUT_SHIFT
;
293 regmap_write(priv
->regmap
, REGCTL1
, val
);
295 /* Try to clear all interrupt status */
296 val
= TS_FIFO_INTR_MASK
| TS_PEN_INTR_MASK
;
297 regmap_update_bits(priv
->regmap
, INTERRUPT_STATUS
, val
, val
);
299 /* Initialize control reg2 */
300 val
= TS_CONTROLLER_EN_BIT
| TS_WIRE_MODE_BIT
;
301 val
|= priv
->cfg_params
.average_data
<< TS_CONTROLLER_AVGDATA_SHIFT
;
303 mask
= (TS_CONTROLLER_AVGDATA_MASK
);
304 mask
|= (TS_CONTROLLER_PWR_LDO
| /* PWR up LDO */
305 TS_CONTROLLER_PWR_ADC
| /* PWR up ADC */
306 TS_CONTROLLER_PWR_BGP
| /* PWR up BGP */
307 TS_CONTROLLER_PWR_TS
); /* PWR up TS */
309 regmap_update_bits(priv
->regmap
, REGCTL2
, mask
, val
);
316 static void iproc_ts_stop(struct input_dev
*dev
)
319 struct iproc_ts_priv
*priv
= input_get_drvdata(dev
);
322 * Disable FIFO int_th and pen event(up/down)Interrupts only
323 * as the interrupt mask register is shared between ADC, TS and
326 val
= TS_PEN_INTR_MASK
| TS_FIFO_INTR_MASK
;
327 regmap_update_bits(priv
->regmap
, INTERRUPT_MASK
, val
, 0);
329 /* Only power down touch screen controller */
330 val
= TS_CONTROLLER_PWR_TS
;
331 regmap_update_bits(priv
->regmap
, REGCTL2
, val
, val
);
333 clk_disable(priv
->tsc_clk
);
336 static int iproc_get_tsc_config(struct device
*dev
, struct iproc_ts_priv
*priv
)
338 struct device_node
*np
= dev
->of_node
;
341 priv
->cfg_params
= iproc_default_config
;
346 if (of_property_read_u32(np
, "scanning_period", &val
) >= 0) {
347 if (val
< 1 || val
> 256) {
348 dev_err(dev
, "scanning_period (%u) must be [1-256]\n",
352 priv
->cfg_params
.scanning_period
= val
;
355 if (of_property_read_u32(np
, "debounce_timeout", &val
) >= 0) {
357 dev_err(dev
, "debounce_timeout (%u) must be [0-255]\n",
361 priv
->cfg_params
.debounce_timeout
= val
;
364 if (of_property_read_u32(np
, "settling_timeout", &val
) >= 0) {
366 dev_err(dev
, "settling_timeout (%u) must be [0-11]\n",
370 priv
->cfg_params
.settling_timeout
= val
;
373 if (of_property_read_u32(np
, "touch_timeout", &val
) >= 0) {
375 dev_err(dev
, "touch_timeout (%u) must be [0-255]\n",
379 priv
->cfg_params
.touch_timeout
= val
;
382 if (of_property_read_u32(np
, "average_data", &val
) >= 0) {
384 dev_err(dev
, "average_data (%u) must be [0-8]\n", val
);
387 priv
->cfg_params
.average_data
= val
;
390 if (of_property_read_u32(np
, "fifo_threshold", &val
) >= 0) {
392 dev_err(dev
, "fifo_threshold (%u)) must be [0-31]\n",
396 priv
->cfg_params
.fifo_threshold
= val
;
399 /* Parse optional properties. */
400 of_property_read_u32(np
, "touchscreen-size-x", &priv
->cfg_params
.max_x
);
401 of_property_read_u32(np
, "touchscreen-size-y", &priv
->cfg_params
.max_y
);
403 of_property_read_u32(np
, "touchscreen-fuzz-x",
404 &priv
->cfg_params
.fuzz_x
);
405 of_property_read_u32(np
, "touchscreen-fuzz-y",
406 &priv
->cfg_params
.fuzz_y
);
408 priv
->cfg_params
.invert_x
=
409 of_property_read_bool(np
, "touchscreen-inverted-x");
410 priv
->cfg_params
.invert_y
=
411 of_property_read_bool(np
, "touchscreen-inverted-y");
416 static int iproc_ts_probe(struct platform_device
*pdev
)
418 struct iproc_ts_priv
*priv
;
419 struct input_dev
*idev
;
423 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
427 /* touchscreen controller memory mapped regs via syscon*/
428 priv
->regmap
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
430 if (IS_ERR(priv
->regmap
)) {
431 error
= PTR_ERR(priv
->regmap
);
432 dev_err(&pdev
->dev
, "unable to map I/O memory:%d\n", error
);
436 priv
->tsc_clk
= devm_clk_get(&pdev
->dev
, "tsc_clk");
437 if (IS_ERR(priv
->tsc_clk
)) {
438 error
= PTR_ERR(priv
->tsc_clk
);
440 "failed getting clock tsc_clk: %d\n", error
);
445 error
= iproc_get_tsc_config(&pdev
->dev
, priv
);
447 dev_err(&pdev
->dev
, "get_tsc_config failed: %d\n", error
);
451 idev
= devm_input_allocate_device(&pdev
->dev
);
453 dev_err(&pdev
->dev
, "failed to allocate input device\n");
458 priv
->pen_status
= PEN_UP_STATUS
;
460 /* Set input device info */
461 idev
->name
= IPROC_TS_NAME
;
462 idev
->dev
.parent
= &pdev
->dev
;
464 idev
->id
.bustype
= BUS_HOST
;
465 idev
->id
.vendor
= SERIO_UNKNOWN
;
466 idev
->id
.product
= 0;
467 idev
->id
.version
= 0;
469 idev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
470 __set_bit(BTN_TOUCH
, idev
->keybit
);
472 input_set_abs_params(idev
, ABS_X
, X_MIN
, priv
->cfg_params
.max_x
,
473 priv
->cfg_params
.fuzz_x
, 0);
474 input_set_abs_params(idev
, ABS_Y
, Y_MIN
, priv
->cfg_params
.max_y
,
475 priv
->cfg_params
.fuzz_y
, 0);
477 idev
->open
= iproc_ts_start
;
478 idev
->close
= iproc_ts_stop
;
480 input_set_drvdata(idev
, priv
);
481 platform_set_drvdata(pdev
, priv
);
484 irq
= platform_get_irq(pdev
, 0);
488 error
= devm_request_irq(&pdev
->dev
, irq
,
489 iproc_touchscreen_interrupt
,
490 IRQF_SHARED
, IPROC_TS_NAME
, pdev
);
494 error
= input_register_device(priv
->idev
);
497 "failed to register input device: %d\n", error
);
504 static const struct of_device_id iproc_ts_of_match
[] = {
505 {.compatible
= "brcm,iproc-touchscreen", },
508 MODULE_DEVICE_TABLE(of
, iproc_ts_of_match
);
510 static struct platform_driver iproc_ts_driver
= {
511 .probe
= iproc_ts_probe
,
513 .name
= IPROC_TS_NAME
,
514 .of_match_table
= iproc_ts_of_match
,
518 module_platform_driver(iproc_ts_driver
);
520 MODULE_DESCRIPTION("IPROC Touchscreen driver");
521 MODULE_AUTHOR("Broadcom");
522 MODULE_LICENSE("GPL v2");