1 // SPDX-License-Identifier: GPL-2.0-only
3 * Touchscreen driver for Marvell 88PM860x
5 * Copyright (C) 2009 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/i2c.h>
13 #include <linux/input.h>
14 #include <linux/mfd/88pm860x.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
19 #define ACCURATE_BIT (12)
22 #define MEAS_EN3 (0x52)
24 #define MEAS_TSIX_1 (0x8D)
25 #define MEAS_TSIX_2 (0x8E)
26 #define MEAS_TSIY_1 (0x8F)
27 #define MEAS_TSIY_2 (0x90)
28 #define MEAS_TSIZ1_1 (0x91)
29 #define MEAS_TSIZ1_2 (0x92)
30 #define MEAS_TSIZ2_1 (0x93)
31 #define MEAS_TSIZ2_2 (0x94)
33 /* bit definitions of touch */
34 #define MEAS_PD_EN (1 << 3)
35 #define MEAS_TSIX_EN (1 << 4)
36 #define MEAS_TSIY_EN (1 << 5)
37 #define MEAS_TSIZ1_EN (1 << 6)
38 #define MEAS_TSIZ2_EN (1 << 7)
41 struct input_dev
*idev
;
42 struct i2c_client
*i2c
;
43 struct pm860x_chip
*chip
;
45 int res_x
; /* resistor of Xplate */
48 static irqreturn_t
pm860x_touch_handler(int irq
, void *data
)
50 struct pm860x_touch
*touch
= data
;
51 struct pm860x_chip
*chip
= touch
->chip
;
52 unsigned char buf
[MEAS_LEN
];
57 ret
= pm860x_bulk_read(touch
->i2c
, MEAS_TSIX_1
, MEAS_LEN
, buf
);
61 pen_down
= buf
[1] & (1 << 6);
62 x
= ((buf
[0] & 0xFF) << 4) | (buf
[1] & 0x0F);
63 y
= ((buf
[2] & 0xFF) << 4) | (buf
[3] & 0x0F);
64 z1
= ((buf
[4] & 0xFF) << 4) | (buf
[5] & 0x0F);
65 z2
= ((buf
[6] & 0xFF) << 4) | (buf
[7] & 0x0F);
68 if ((x
!= 0) && (z1
!= 0) && (touch
->res_x
!= 0)) {
70 rt
= (rt
* touch
->res_x
* x
) >> ACCURATE_BIT
;
71 dev_dbg(chip
->dev
, "z1:%d, z2:%d, rt:%d\n",
74 input_report_abs(touch
->idev
, ABS_X
, x
);
75 input_report_abs(touch
->idev
, ABS_Y
, y
);
76 input_report_abs(touch
->idev
, ABS_PRESSURE
, rt
);
77 input_report_key(touch
->idev
, BTN_TOUCH
, 1);
78 dev_dbg(chip
->dev
, "pen down at [%d, %d].\n", x
, y
);
80 input_report_abs(touch
->idev
, ABS_PRESSURE
, 0);
81 input_report_key(touch
->idev
, BTN_TOUCH
, 0);
82 dev_dbg(chip
->dev
, "pen release\n");
84 input_sync(touch
->idev
);
90 static int pm860x_touch_open(struct input_dev
*dev
)
92 struct pm860x_touch
*touch
= input_get_drvdata(dev
);
95 data
= MEAS_PD_EN
| MEAS_TSIX_EN
| MEAS_TSIY_EN
96 | MEAS_TSIZ1_EN
| MEAS_TSIZ2_EN
;
97 ret
= pm860x_set_bits(touch
->i2c
, MEAS_EN3
, data
, data
);
105 static void pm860x_touch_close(struct input_dev
*dev
)
107 struct pm860x_touch
*touch
= input_get_drvdata(dev
);
110 data
= MEAS_PD_EN
| MEAS_TSIX_EN
| MEAS_TSIY_EN
111 | MEAS_TSIZ1_EN
| MEAS_TSIZ2_EN
;
112 pm860x_set_bits(touch
->i2c
, MEAS_EN3
, data
, 0);
116 static int pm860x_touch_dt_init(struct platform_device
*pdev
,
117 struct pm860x_chip
*chip
,
120 struct i2c_client
*i2c
= (chip
->id
== CHIP_PM8607
) ? chip
->client \
123 if (!pdev
->dev
.parent
->of_node
)
126 struct device_node
*np
__free(device_node
) =
127 of_get_child_by_name(pdev
->dev
.parent
->of_node
, "touch");
129 dev_err(&pdev
->dev
, "Can't find touch node\n");
132 /* set GPADC MISC1 register */
134 if (!of_property_read_u32(np
, "marvell,88pm860x-gpadc-prebias", &n
))
135 data
|= (n
<< 1) & PM8607_GPADC_PREBIAS_MASK
;
136 if (!of_property_read_u32(np
, "marvell,88pm860x-gpadc-slot-cycle", &n
))
137 data
|= (n
<< 3) & PM8607_GPADC_SLOT_CYCLE_MASK
;
138 if (!of_property_read_u32(np
, "marvell,88pm860x-gpadc-off-scale", &n
))
139 data
|= (n
<< 5) & PM8607_GPADC_OFF_SCALE_MASK
;
140 if (!of_property_read_u32(np
, "marvell,88pm860x-gpadc-sw-cal", &n
))
141 data
|= (n
<< 7) & PM8607_GPADC_SW_CAL_MASK
;
143 ret
= pm860x_reg_write(i2c
, PM8607_GPADC_MISC1
, data
);
147 /* set tsi prebias time */
148 if (!of_property_read_u32(np
, "marvell,88pm860x-tsi-prebias", &data
)) {
149 ret
= pm860x_reg_write(i2c
, PM8607_TSI_PREBIAS
, data
);
153 /* set prebias & prechg time of pen detect */
155 if (!of_property_read_u32(np
, "marvell,88pm860x-pen-prebias", &n
))
156 data
|= n
& PM8607_PD_PREBIAS_MASK
;
157 if (!of_property_read_u32(np
, "marvell,88pm860x-pen-prechg", &n
))
158 data
|= n
& PM8607_PD_PRECHG_MASK
;
160 ret
= pm860x_reg_write(i2c
, PM8607_PD_PREBIAS
, data
);
164 of_property_read_u32(np
, "marvell,88pm860x-resistor-X", res_x
);
169 #define pm860x_touch_dt_init(x, y, z) (-1)
172 static int pm860x_touch_probe(struct platform_device
*pdev
)
174 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
175 struct pm860x_touch_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
176 struct pm860x_touch
*touch
;
177 struct i2c_client
*i2c
= (chip
->id
== CHIP_PM8607
) ? chip
->client \
179 int irq
, ret
, res_x
= 0, data
= 0;
181 irq
= platform_get_irq(pdev
, 0);
185 if (pm860x_touch_dt_init(pdev
, chip
, &res_x
)) {
187 /* set GPADC MISC1 register */
189 data
|= (pdata
->gpadc_prebias
<< 1)
190 & PM8607_GPADC_PREBIAS_MASK
;
191 data
|= (pdata
->slot_cycle
<< 3)
192 & PM8607_GPADC_SLOT_CYCLE_MASK
;
193 data
|= (pdata
->off_scale
<< 5)
194 & PM8607_GPADC_OFF_SCALE_MASK
;
195 data
|= (pdata
->sw_cal
<< 7)
196 & PM8607_GPADC_SW_CAL_MASK
;
198 ret
= pm860x_reg_write(i2c
,
199 PM8607_GPADC_MISC1
, data
);
203 /* set tsi prebias time */
204 if (pdata
->tsi_prebias
) {
205 data
= pdata
->tsi_prebias
;
206 ret
= pm860x_reg_write(i2c
,
207 PM8607_TSI_PREBIAS
, data
);
211 /* set prebias & prechg time of pen detect */
213 data
|= pdata
->pen_prebias
214 & PM8607_PD_PREBIAS_MASK
;
215 data
|= (pdata
->pen_prechg
<< 5)
216 & PM8607_PD_PRECHG_MASK
;
218 ret
= pm860x_reg_write(i2c
,
219 PM8607_PD_PREBIAS
, data
);
223 res_x
= pdata
->res_x
;
225 dev_err(&pdev
->dev
, "failed to get platform data\n");
230 ret
= pm860x_set_bits(i2c
, PM8607_GPADC_MISC1
, PM8607_GPADC_EN
,
235 touch
= devm_kzalloc(&pdev
->dev
, sizeof(struct pm860x_touch
),
240 touch
->idev
= devm_input_allocate_device(&pdev
->dev
);
242 dev_err(&pdev
->dev
, "Failed to allocate input device!\n");
246 touch
->idev
->name
= "88pm860x-touch";
247 touch
->idev
->phys
= "88pm860x/input0";
248 touch
->idev
->id
.bustype
= BUS_I2C
;
249 touch
->idev
->dev
.parent
= &pdev
->dev
;
250 touch
->idev
->open
= pm860x_touch_open
;
251 touch
->idev
->close
= pm860x_touch_close
;
255 touch
->res_x
= res_x
;
256 input_set_drvdata(touch
->idev
, touch
);
258 ret
= devm_request_threaded_irq(&pdev
->dev
, touch
->irq
, NULL
,
259 pm860x_touch_handler
, IRQF_ONESHOT
,
264 __set_bit(EV_ABS
, touch
->idev
->evbit
);
265 __set_bit(ABS_X
, touch
->idev
->absbit
);
266 __set_bit(ABS_Y
, touch
->idev
->absbit
);
267 __set_bit(ABS_PRESSURE
, touch
->idev
->absbit
);
268 __set_bit(EV_SYN
, touch
->idev
->evbit
);
269 __set_bit(EV_KEY
, touch
->idev
->evbit
);
270 __set_bit(BTN_TOUCH
, touch
->idev
->keybit
);
272 input_set_abs_params(touch
->idev
, ABS_X
, 0, 1 << ACCURATE_BIT
, 0, 0);
273 input_set_abs_params(touch
->idev
, ABS_Y
, 0, 1 << ACCURATE_BIT
, 0, 0);
274 input_set_abs_params(touch
->idev
, ABS_PRESSURE
, 0, 1 << ACCURATE_BIT
,
277 ret
= input_register_device(touch
->idev
);
279 dev_err(chip
->dev
, "Failed to register touch!\n");
286 static struct platform_driver pm860x_touch_driver
= {
288 .name
= "88pm860x-touch",
290 .probe
= pm860x_touch_probe
,
292 module_platform_driver(pm860x_touch_driver
);
294 MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
295 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
296 MODULE_LICENSE("GPL");
297 MODULE_ALIAS("platform:88pm860x-touch");