Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[linux-2.6-microblaze.git] / drivers / input / touchscreen / 88pm860x-ts.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Touchscreen driver for Marvell 88PM860x
4  *
5  * Copyright (C) 2009 Marvell International Ltd.
6  *      Haojian Zhuang <haojian.zhuang@marvell.com>
7  */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/of.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>
17
18 #define MEAS_LEN                (8)
19 #define ACCURATE_BIT            (12)
20
21 /* touch register */
22 #define MEAS_EN3                (0x52)
23
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)
32
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)
39
40 struct pm860x_touch {
41         struct input_dev *idev;
42         struct i2c_client *i2c;
43         struct pm860x_chip *chip;
44         int irq;
45         int res_x;              /* resistor of Xplate */
46 };
47
48 static irqreturn_t pm860x_touch_handler(int irq, void *data)
49 {
50         struct pm860x_touch *touch = data;
51         struct pm860x_chip *chip = touch->chip;
52         unsigned char buf[MEAS_LEN];
53         int x, y, pen_down;
54         int z1, z2, rt = 0;
55         int ret;
56
57         ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
58         if (ret < 0)
59                 goto out;
60
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);
66
67         if (pen_down) {
68                 if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
69                         rt = z2 / z1 - 1;
70                         rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
71                         dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
72                                 z1, z2, rt);
73                 }
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);
79         } else {
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");
83         }
84         input_sync(touch->idev);
85
86 out:
87         return IRQ_HANDLED;
88 }
89
90 static int pm860x_touch_open(struct input_dev *dev)
91 {
92         struct pm860x_touch *touch = input_get_drvdata(dev);
93         int data, ret;
94
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);
98         if (ret < 0)
99                 goto out;
100         return 0;
101 out:
102         return ret;
103 }
104
105 static void pm860x_touch_close(struct input_dev *dev)
106 {
107         struct pm860x_touch *touch = input_get_drvdata(dev);
108         int data;
109
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);
113 }
114
115 #ifdef CONFIG_OF
116 static int pm860x_touch_dt_init(struct platform_device *pdev,
117                                           struct pm860x_chip *chip,
118                                           int *res_x)
119 {
120         struct device_node *np = pdev->dev.parent->of_node;
121         struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
122                                  : chip->companion;
123         int data, n, ret;
124         if (!np)
125                 return -ENODEV;
126         np = of_get_child_by_name(np, "touch");
127         if (!np) {
128                 dev_err(&pdev->dev, "Can't find touch node\n");
129                 return -EINVAL;
130         }
131         /* set GPADC MISC1 register */
132         data = 0;
133         if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
134                 data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
135         if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
136                 data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
137         if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
138                 data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
139         if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
140                 data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
141         if (data) {
142                 ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
143                 if (ret < 0)
144                         goto err_put_node;
145         }
146         /* set tsi prebias time */
147         if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
148                 ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
149                 if (ret < 0)
150                         goto err_put_node;
151         }
152         /* set prebias & prechg time of pen detect */
153         data = 0;
154         if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
155                 data |= n & PM8607_PD_PREBIAS_MASK;
156         if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
157                 data |= n & PM8607_PD_PRECHG_MASK;
158         if (data) {
159                 ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
160                 if (ret < 0)
161                         goto err_put_node;
162         }
163         of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
164
165         of_node_put(np);
166
167         return 0;
168
169 err_put_node:
170         of_node_put(np);
171
172         return -EINVAL;
173 }
174 #else
175 #define pm860x_touch_dt_init(x, y, z)   (-1)
176 #endif
177
178 static int pm860x_touch_probe(struct platform_device *pdev)
179 {
180         struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
181         struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
182         struct pm860x_touch *touch;
183         struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
184                                  : chip->companion;
185         int irq, ret, res_x = 0, data = 0;
186
187         irq = platform_get_irq(pdev, 0);
188         if (irq < 0)
189                 return -EINVAL;
190
191         if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
192                 if (pdata) {
193                         /* set GPADC MISC1 register */
194                         data = 0;
195                         data |= (pdata->gpadc_prebias << 1)
196                                 & PM8607_GPADC_PREBIAS_MASK;
197                         data |= (pdata->slot_cycle << 3)
198                                 & PM8607_GPADC_SLOT_CYCLE_MASK;
199                         data |= (pdata->off_scale << 5)
200                                 & PM8607_GPADC_OFF_SCALE_MASK;
201                         data |= (pdata->sw_cal << 7)
202                                 & PM8607_GPADC_SW_CAL_MASK;
203                         if (data) {
204                                 ret = pm860x_reg_write(i2c,
205                                         PM8607_GPADC_MISC1, data);
206                                 if (ret < 0)
207                                         return -EINVAL;
208                         }
209                         /* set tsi prebias time */
210                         if (pdata->tsi_prebias) {
211                                 data = pdata->tsi_prebias;
212                                 ret = pm860x_reg_write(i2c,
213                                         PM8607_TSI_PREBIAS, data);
214                                 if (ret < 0)
215                                         return -EINVAL;
216                         }
217                         /* set prebias & prechg time of pen detect */
218                         data = 0;
219                         data |= pdata->pen_prebias
220                                 & PM8607_PD_PREBIAS_MASK;
221                         data |= (pdata->pen_prechg << 5)
222                                 & PM8607_PD_PRECHG_MASK;
223                         if (data) {
224                                 ret = pm860x_reg_write(i2c,
225                                         PM8607_PD_PREBIAS, data);
226                                 if (ret < 0)
227                                         return -EINVAL;
228                         }
229                         res_x = pdata->res_x;
230                 } else {
231                         dev_err(&pdev->dev, "failed to get platform data\n");
232                         return -EINVAL;
233                 }
234         }
235         /* enable GPADC */
236         ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
237                               PM8607_GPADC_EN);
238         if (ret)
239                 return ret;
240
241         touch = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_touch),
242                              GFP_KERNEL);
243         if (!touch)
244                 return -ENOMEM;
245
246         touch->idev = devm_input_allocate_device(&pdev->dev);
247         if (!touch->idev) {
248                 dev_err(&pdev->dev, "Failed to allocate input device!\n");
249                 return -ENOMEM;
250         }
251
252         touch->idev->name = "88pm860x-touch";
253         touch->idev->phys = "88pm860x/input0";
254         touch->idev->id.bustype = BUS_I2C;
255         touch->idev->dev.parent = &pdev->dev;
256         touch->idev->open = pm860x_touch_open;
257         touch->idev->close = pm860x_touch_close;
258         touch->chip = chip;
259         touch->i2c = i2c;
260         touch->irq = irq;
261         touch->res_x = res_x;
262         input_set_drvdata(touch->idev, touch);
263
264         ret = devm_request_threaded_irq(&pdev->dev, touch->irq, NULL,
265                                         pm860x_touch_handler, IRQF_ONESHOT,
266                                         "touch", touch);
267         if (ret < 0)
268                 return ret;
269
270         __set_bit(EV_ABS, touch->idev->evbit);
271         __set_bit(ABS_X, touch->idev->absbit);
272         __set_bit(ABS_Y, touch->idev->absbit);
273         __set_bit(ABS_PRESSURE, touch->idev->absbit);
274         __set_bit(EV_SYN, touch->idev->evbit);
275         __set_bit(EV_KEY, touch->idev->evbit);
276         __set_bit(BTN_TOUCH, touch->idev->keybit);
277
278         input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
279         input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
280         input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
281                                 0, 0);
282
283         ret = input_register_device(touch->idev);
284         if (ret < 0) {
285                 dev_err(chip->dev, "Failed to register touch!\n");
286                 return ret;
287         }
288
289         return 0;
290 }
291
292 static struct platform_driver pm860x_touch_driver = {
293         .driver = {
294                 .name   = "88pm860x-touch",
295         },
296         .probe  = pm860x_touch_probe,
297 };
298 module_platform_driver(pm860x_touch_driver);
299
300 MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
301 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
302 MODULE_LICENSE("GPL");
303 MODULE_ALIAS("platform:88pm860x-touch");
304