Loading...
1/*
2 * Touchscreen driver for Marvell 88PM860x
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/i2c.h>
15#include <linux/input.h>
16#include <linux/mfd/88pm860x.h>
17#include <linux/slab.h>
18
19#define MEAS_LEN (8)
20#define ACCURATE_BIT (12)
21
22/* touch register */
23#define MEAS_EN3 (0x52)
24
25#define MEAS_TSIX_1 (0x8D)
26#define MEAS_TSIX_2 (0x8E)
27#define MEAS_TSIY_1 (0x8F)
28#define MEAS_TSIY_2 (0x90)
29#define MEAS_TSIZ1_1 (0x91)
30#define MEAS_TSIZ1_2 (0x92)
31#define MEAS_TSIZ2_1 (0x93)
32#define MEAS_TSIZ2_2 (0x94)
33
34/* bit definitions of touch */
35#define MEAS_PD_EN (1 << 3)
36#define MEAS_TSIX_EN (1 << 4)
37#define MEAS_TSIY_EN (1 << 5)
38#define MEAS_TSIZ1_EN (1 << 6)
39#define MEAS_TSIZ2_EN (1 << 7)
40
41struct pm860x_touch {
42 struct input_dev *idev;
43 struct i2c_client *i2c;
44 struct pm860x_chip *chip;
45 int irq;
46 int res_x; /* resistor of Xplate */
47};
48
49static irqreturn_t pm860x_touch_handler(int irq, void *data)
50{
51 struct pm860x_touch *touch = data;
52 struct pm860x_chip *chip = touch->chip;
53 unsigned char buf[MEAS_LEN];
54 int x, y, pen_down;
55 int z1, z2, rt = 0;
56 int ret;
57
58 ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
59 if (ret < 0)
60 goto out;
61
62 pen_down = buf[1] & (1 << 6);
63 x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
64 y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
65 z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
66 z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
67
68 if (pen_down) {
69 if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
70 rt = z2 / z1 - 1;
71 rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
72 dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
73 z1, z2, rt);
74 }
75 input_report_abs(touch->idev, ABS_X, x);
76 input_report_abs(touch->idev, ABS_Y, y);
77 input_report_abs(touch->idev, ABS_PRESSURE, rt);
78 input_report_key(touch->idev, BTN_TOUCH, 1);
79 dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
80 } else {
81 input_report_abs(touch->idev, ABS_PRESSURE, 0);
82 input_report_key(touch->idev, BTN_TOUCH, 0);
83 dev_dbg(chip->dev, "pen release\n");
84 }
85 input_sync(touch->idev);
86
87out:
88 return IRQ_HANDLED;
89}
90
91static int pm860x_touch_open(struct input_dev *dev)
92{
93 struct pm860x_touch *touch = input_get_drvdata(dev);
94 int data, ret;
95
96 data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
97 | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
98 ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
99 if (ret < 0)
100 goto out;
101 return 0;
102out:
103 return ret;
104}
105
106static void pm860x_touch_close(struct input_dev *dev)
107{
108 struct pm860x_touch *touch = input_get_drvdata(dev);
109 int data;
110
111 data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
112 | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
113 pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
114}
115
116static int __devinit pm860x_touch_probe(struct platform_device *pdev)
117{
118 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
119 struct pm860x_platform_data *pm860x_pdata = \
120 pdev->dev.parent->platform_data;
121 struct pm860x_touch_pdata *pdata = NULL;
122 struct pm860x_touch *touch;
123 int irq, ret;
124
125 irq = platform_get_irq(pdev, 0);
126 if (irq < 0) {
127 dev_err(&pdev->dev, "No IRQ resource!\n");
128 return -EINVAL;
129 }
130
131 if (!pm860x_pdata) {
132 dev_err(&pdev->dev, "platform data is missing\n");
133 return -EINVAL;
134 }
135
136 pdata = pm860x_pdata->touch;
137 if (!pdata) {
138 dev_err(&pdev->dev, "touchscreen data is missing\n");
139 return -EINVAL;
140 }
141
142 touch = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
143 if (touch == NULL)
144 return -ENOMEM;
145 dev_set_drvdata(&pdev->dev, touch);
146
147 touch->idev = input_allocate_device();
148 if (touch->idev == NULL) {
149 dev_err(&pdev->dev, "Failed to allocate input device!\n");
150 ret = -ENOMEM;
151 goto out;
152 }
153
154 touch->idev->name = "88pm860x-touch";
155 touch->idev->phys = "88pm860x/input0";
156 touch->idev->id.bustype = BUS_I2C;
157 touch->idev->dev.parent = &pdev->dev;
158 touch->idev->open = pm860x_touch_open;
159 touch->idev->close = pm860x_touch_close;
160 touch->chip = chip;
161 touch->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
162 touch->irq = irq + chip->irq_base;
163 touch->res_x = pdata->res_x;
164 input_set_drvdata(touch->idev, touch);
165
166 ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
167 IRQF_ONESHOT, "touch", touch);
168 if (ret < 0)
169 goto out_irq;
170
171 __set_bit(EV_ABS, touch->idev->evbit);
172 __set_bit(ABS_X, touch->idev->absbit);
173 __set_bit(ABS_Y, touch->idev->absbit);
174 __set_bit(ABS_PRESSURE, touch->idev->absbit);
175 __set_bit(EV_SYN, touch->idev->evbit);
176 __set_bit(EV_KEY, touch->idev->evbit);
177 __set_bit(BTN_TOUCH, touch->idev->keybit);
178
179 input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
180 input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
181 input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
182 0, 0);
183
184 ret = input_register_device(touch->idev);
185 if (ret < 0) {
186 dev_err(chip->dev, "Failed to register touch!\n");
187 goto out_rg;
188 }
189
190 platform_set_drvdata(pdev, touch);
191 return 0;
192out_rg:
193 free_irq(touch->irq, touch);
194out_irq:
195 input_free_device(touch->idev);
196out:
197 kfree(touch);
198 return ret;
199}
200
201static int __devexit pm860x_touch_remove(struct platform_device *pdev)
202{
203 struct pm860x_touch *touch = platform_get_drvdata(pdev);
204
205 input_unregister_device(touch->idev);
206 free_irq(touch->irq, touch);
207 platform_set_drvdata(pdev, NULL);
208 kfree(touch);
209 return 0;
210}
211
212static struct platform_driver pm860x_touch_driver = {
213 .driver = {
214 .name = "88pm860x-touch",
215 .owner = THIS_MODULE,
216 },
217 .probe = pm860x_touch_probe,
218 .remove = __devexit_p(pm860x_touch_remove),
219};
220
221static int __init pm860x_touch_init(void)
222{
223 return platform_driver_register(&pm860x_touch_driver);
224}
225module_init(pm860x_touch_init);
226
227static void __exit pm860x_touch_exit(void)
228{
229 platform_driver_unregister(&pm860x_touch_driver);
230}
231module_exit(pm860x_touch_exit);
232
233MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
234MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
235MODULE_LICENSE("GPL");
236MODULE_ALIAS("platform:88pm860x-touch");
237
1/*
2 * Touchscreen driver for Marvell 88PM860x
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/i2c.h>
16#include <linux/input.h>
17#include <linux/mfd/88pm860x.h>
18#include <linux/slab.h>
19#include <linux/device.h>
20
21#define MEAS_LEN (8)
22#define ACCURATE_BIT (12)
23
24/* touch register */
25#define MEAS_EN3 (0x52)
26
27#define MEAS_TSIX_1 (0x8D)
28#define MEAS_TSIX_2 (0x8E)
29#define MEAS_TSIY_1 (0x8F)
30#define MEAS_TSIY_2 (0x90)
31#define MEAS_TSIZ1_1 (0x91)
32#define MEAS_TSIZ1_2 (0x92)
33#define MEAS_TSIZ2_1 (0x93)
34#define MEAS_TSIZ2_2 (0x94)
35
36/* bit definitions of touch */
37#define MEAS_PD_EN (1 << 3)
38#define MEAS_TSIX_EN (1 << 4)
39#define MEAS_TSIY_EN (1 << 5)
40#define MEAS_TSIZ1_EN (1 << 6)
41#define MEAS_TSIZ2_EN (1 << 7)
42
43struct pm860x_touch {
44 struct input_dev *idev;
45 struct i2c_client *i2c;
46 struct pm860x_chip *chip;
47 int irq;
48 int res_x; /* resistor of Xplate */
49};
50
51static irqreturn_t pm860x_touch_handler(int irq, void *data)
52{
53 struct pm860x_touch *touch = data;
54 struct pm860x_chip *chip = touch->chip;
55 unsigned char buf[MEAS_LEN];
56 int x, y, pen_down;
57 int z1, z2, rt = 0;
58 int ret;
59
60 ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
61 if (ret < 0)
62 goto out;
63
64 pen_down = buf[1] & (1 << 6);
65 x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
66 y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
67 z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
68 z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
69
70 if (pen_down) {
71 if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
72 rt = z2 / z1 - 1;
73 rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
74 dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
75 z1, z2, rt);
76 }
77 input_report_abs(touch->idev, ABS_X, x);
78 input_report_abs(touch->idev, ABS_Y, y);
79 input_report_abs(touch->idev, ABS_PRESSURE, rt);
80 input_report_key(touch->idev, BTN_TOUCH, 1);
81 dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
82 } else {
83 input_report_abs(touch->idev, ABS_PRESSURE, 0);
84 input_report_key(touch->idev, BTN_TOUCH, 0);
85 dev_dbg(chip->dev, "pen release\n");
86 }
87 input_sync(touch->idev);
88
89out:
90 return IRQ_HANDLED;
91}
92
93static int pm860x_touch_open(struct input_dev *dev)
94{
95 struct pm860x_touch *touch = input_get_drvdata(dev);
96 int data, ret;
97
98 data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
99 | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
100 ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
101 if (ret < 0)
102 goto out;
103 return 0;
104out:
105 return ret;
106}
107
108static void pm860x_touch_close(struct input_dev *dev)
109{
110 struct pm860x_touch *touch = input_get_drvdata(dev);
111 int data;
112
113 data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
114 | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
115 pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
116}
117
118#ifdef CONFIG_OF
119static int pm860x_touch_dt_init(struct platform_device *pdev,
120 struct pm860x_chip *chip,
121 int *res_x)
122{
123 struct device_node *np = pdev->dev.parent->of_node;
124 struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
125 : chip->companion;
126 int data, n, ret;
127 if (!np)
128 return -ENODEV;
129 np = of_find_node_by_name(np, "touch");
130 if (!np) {
131 dev_err(&pdev->dev, "Can't find touch node\n");
132 return -EINVAL;
133 }
134 /* set GPADC MISC1 register */
135 data = 0;
136 if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-prebias", &n))
137 data |= (n << 1) & PM8607_GPADC_PREBIAS_MASK;
138 if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-slot-cycle", &n))
139 data |= (n << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
140 if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-off-scale", &n))
141 data |= (n << 5) & PM8607_GPADC_OFF_SCALE_MASK;
142 if (!of_property_read_u32(np, "marvell,88pm860x-gpadc-sw-cal", &n))
143 data |= (n << 7) & PM8607_GPADC_SW_CAL_MASK;
144 if (data) {
145 ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
146 if (ret < 0)
147 return -EINVAL;
148 }
149 /* set tsi prebias time */
150 if (!of_property_read_u32(np, "marvell,88pm860x-tsi-prebias", &data)) {
151 ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
152 if (ret < 0)
153 return -EINVAL;
154 }
155 /* set prebias & prechg time of pen detect */
156 data = 0;
157 if (!of_property_read_u32(np, "marvell,88pm860x-pen-prebias", &n))
158 data |= n & PM8607_PD_PREBIAS_MASK;
159 if (!of_property_read_u32(np, "marvell,88pm860x-pen-prechg", &n))
160 data |= n & PM8607_PD_PRECHG_MASK;
161 if (data) {
162 ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
163 if (ret < 0)
164 return -EINVAL;
165 }
166 of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
167 return 0;
168}
169#else
170#define pm860x_touch_dt_init(x, y, z) (-1)
171#endif
172
173static int pm860x_touch_probe(struct platform_device *pdev)
174{
175 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
176 struct pm860x_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
177 struct pm860x_touch *touch;
178 struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
179 : chip->companion;
180 int irq, ret, res_x = 0, data = 0;
181
182 irq = platform_get_irq(pdev, 0);
183 if (irq < 0) {
184 dev_err(&pdev->dev, "No IRQ resource!\n");
185 return -EINVAL;
186 }
187
188 if (pm860x_touch_dt_init(pdev, chip, &res_x)) {
189 if (pdata) {
190 /* set GPADC MISC1 register */
191 data = 0;
192 data |= (pdata->gpadc_prebias << 1)
193 & PM8607_GPADC_PREBIAS_MASK;
194 data |= (pdata->slot_cycle << 3)
195 & PM8607_GPADC_SLOT_CYCLE_MASK;
196 data |= (pdata->off_scale << 5)
197 & PM8607_GPADC_OFF_SCALE_MASK;
198 data |= (pdata->sw_cal << 7)
199 & PM8607_GPADC_SW_CAL_MASK;
200 if (data) {
201 ret = pm860x_reg_write(i2c,
202 PM8607_GPADC_MISC1, data);
203 if (ret < 0)
204 return -EINVAL;
205 }
206 /* set tsi prebias time */
207 if (pdata->tsi_prebias) {
208 data = pdata->tsi_prebias;
209 ret = pm860x_reg_write(i2c,
210 PM8607_TSI_PREBIAS, data);
211 if (ret < 0)
212 return -EINVAL;
213 }
214 /* set prebias & prechg time of pen detect */
215 data = 0;
216 data |= pdata->pen_prebias
217 & PM8607_PD_PREBIAS_MASK;
218 data |= (pdata->pen_prechg << 5)
219 & PM8607_PD_PRECHG_MASK;
220 if (data) {
221 ret = pm860x_reg_write(i2c,
222 PM8607_PD_PREBIAS, data);
223 if (ret < 0)
224 return -EINVAL;
225 }
226 res_x = pdata->res_x;
227 } else {
228 dev_err(&pdev->dev, "failed to get platform data\n");
229 return -EINVAL;
230 }
231 }
232 /* enable GPADC */
233 ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1, PM8607_GPADC_EN,
234 PM8607_GPADC_EN);
235 if (ret)
236 return ret;
237
238 touch = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_touch),
239 GFP_KERNEL);
240 if (!touch)
241 return -ENOMEM;
242
243 platform_set_drvdata(pdev, touch);
244
245 touch->idev = devm_input_allocate_device(&pdev->dev);
246 if (!touch->idev) {
247 dev_err(&pdev->dev, "Failed to allocate input device!\n");
248 return -ENOMEM;
249 }
250
251 touch->idev->name = "88pm860x-touch";
252 touch->idev->phys = "88pm860x/input0";
253 touch->idev->id.bustype = BUS_I2C;
254 touch->idev->dev.parent = &pdev->dev;
255 touch->idev->open = pm860x_touch_open;
256 touch->idev->close = pm860x_touch_close;
257 touch->chip = chip;
258 touch->i2c = i2c;
259 touch->irq = irq;
260 touch->res_x = res_x;
261 input_set_drvdata(touch->idev, touch);
262
263 ret = devm_request_threaded_irq(&pdev->dev, touch->irq, NULL,
264 pm860x_touch_handler, IRQF_ONESHOT,
265 "touch", touch);
266 if (ret < 0)
267 return ret;
268
269 __set_bit(EV_ABS, touch->idev->evbit);
270 __set_bit(ABS_X, touch->idev->absbit);
271 __set_bit(ABS_Y, touch->idev->absbit);
272 __set_bit(ABS_PRESSURE, touch->idev->absbit);
273 __set_bit(EV_SYN, touch->idev->evbit);
274 __set_bit(EV_KEY, touch->idev->evbit);
275 __set_bit(BTN_TOUCH, touch->idev->keybit);
276
277 input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
278 input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
279 input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
280 0, 0);
281
282 ret = input_register_device(touch->idev);
283 if (ret < 0) {
284 dev_err(chip->dev, "Failed to register touch!\n");
285 return ret;
286 }
287
288 platform_set_drvdata(pdev, touch);
289 return 0;
290}
291
292static struct platform_driver pm860x_touch_driver = {
293 .driver = {
294 .name = "88pm860x-touch",
295 },
296 .probe = pm860x_touch_probe,
297};
298module_platform_driver(pm860x_touch_driver);
299
300MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
301MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
302MODULE_LICENSE("GPL");
303MODULE_ALIAS("platform:88pm860x-touch");
304