blob: 4f81e6eb74b6782c18c79c75d423031a02b75016 [file] [log] [blame]
Haojian Zhuang866a98a2009-12-15 16:06:17 -05001/*
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>
Haojian Zhuang2e57d562012-09-21 18:06:52 +080013#include <linux/of.h>
Haojian Zhuang866a98a2009-12-15 16:06:17 -050014#include <linux/platform_device.h>
15#include <linux/i2c.h>
16#include <linux/input.h>
17#include <linux/mfd/88pm860x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Haojian Zhuang866a98a2009-12-15 16:06:17 -050019
20#define MEAS_LEN (8)
21#define ACCURATE_BIT (12)
22
23/* touch register */
24#define MEAS_EN3 (0x52)
25
26#define MEAS_TSIX_1 (0x8D)
27#define MEAS_TSIX_2 (0x8E)
28#define MEAS_TSIY_1 (0x8F)
29#define MEAS_TSIY_2 (0x90)
30#define MEAS_TSIZ1_1 (0x91)
31#define MEAS_TSIZ1_2 (0x92)
32#define MEAS_TSIZ2_1 (0x93)
33#define MEAS_TSIZ2_2 (0x94)
34
35/* bit definitions of touch */
36#define MEAS_PD_EN (1 << 3)
37#define MEAS_TSIX_EN (1 << 4)
38#define MEAS_TSIY_EN (1 << 5)
39#define MEAS_TSIZ1_EN (1 << 6)
40#define MEAS_TSIZ2_EN (1 << 7)
41
42struct pm860x_touch {
43 struct input_dev *idev;
44 struct i2c_client *i2c;
45 struct pm860x_chip *chip;
46 int irq;
47 int res_x; /* resistor of Xplate */
48};
49
50static irqreturn_t pm860x_touch_handler(int irq, void *data)
51{
52 struct pm860x_touch *touch = data;
53 struct pm860x_chip *chip = touch->chip;
54 unsigned char buf[MEAS_LEN];
55 int x, y, pen_down;
56 int z1, z2, rt = 0;
57 int ret;
58
Haojian Zhuang866a98a2009-12-15 16:06:17 -050059 ret = pm860x_bulk_read(touch->i2c, MEAS_TSIX_1, MEAS_LEN, buf);
60 if (ret < 0)
61 goto out;
62
63 pen_down = buf[1] & (1 << 6);
64 x = ((buf[0] & 0xFF) << 4) | (buf[1] & 0x0F);
65 y = ((buf[2] & 0xFF) << 4) | (buf[3] & 0x0F);
66 z1 = ((buf[4] & 0xFF) << 4) | (buf[5] & 0x0F);
67 z2 = ((buf[6] & 0xFF) << 4) | (buf[7] & 0x0F);
68
69 if (pen_down) {
70 if ((x != 0) && (z1 != 0) && (touch->res_x != 0)) {
71 rt = z2 / z1 - 1;
72 rt = (rt * touch->res_x * x) >> ACCURATE_BIT;
73 dev_dbg(chip->dev, "z1:%d, z2:%d, rt:%d\n",
74 z1, z2, rt);
75 }
76 input_report_abs(touch->idev, ABS_X, x);
77 input_report_abs(touch->idev, ABS_Y, y);
78 input_report_abs(touch->idev, ABS_PRESSURE, rt);
79 input_report_key(touch->idev, BTN_TOUCH, 1);
80 dev_dbg(chip->dev, "pen down at [%d, %d].\n", x, y);
81 } else {
82 input_report_abs(touch->idev, ABS_PRESSURE, 0);
83 input_report_key(touch->idev, BTN_TOUCH, 0);
84 dev_dbg(chip->dev, "pen release\n");
85 }
86 input_sync(touch->idev);
Haojian Zhuang866a98a2009-12-15 16:06:17 -050087
88out:
89 return IRQ_HANDLED;
90}
91
92static int pm860x_touch_open(struct input_dev *dev)
93{
94 struct pm860x_touch *touch = input_get_drvdata(dev);
Haojian Zhuang866a98a2009-12-15 16:06:17 -050095 int data, ret;
96
97 data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
98 | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
99 ret = pm860x_set_bits(touch->i2c, MEAS_EN3, data, data);
100 if (ret < 0)
101 goto out;
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500102 return 0;
103out:
104 return ret;
105}
106
107static void pm860x_touch_close(struct input_dev *dev)
108{
109 struct pm860x_touch *touch = input_get_drvdata(dev);
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500110 int data;
111
112 data = MEAS_PD_EN | MEAS_TSIX_EN | MEAS_TSIY_EN
113 | MEAS_TSIZ1_EN | MEAS_TSIZ2_EN;
114 pm860x_set_bits(touch->i2c, MEAS_EN3, data, 0);
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500115}
116
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800117#ifdef CONFIG_OF
118static int __devinit pm860x_touch_dt_init(struct platform_device *pdev,
119 int *res_x)
120{
121 struct device_node *np = pdev->dev.parent->of_node;
122 if (!np)
123 return -ENODEV;
124 np = of_find_node_by_name(np, "touch");
125 if (!np) {
126 dev_err(&pdev->dev, "Can't find touch node\n");
127 return -EINVAL;
128 }
129 of_property_read_u32(np, "marvell,88pm860x-resistor-X", res_x);
130 return 0;
131}
132#else
133#define pm860x_touch_dt_init(x, y) (-1)
134#endif
135
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500136static int __devinit pm860x_touch_probe(struct platform_device *pdev)
137{
138 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800139 struct pm860x_touch_pdata *pdata = pdev->dev.platform_data;
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500140 struct pm860x_touch *touch;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800141 int irq, ret, res_x = 0;
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500142
143 irq = platform_get_irq(pdev, 0);
144 if (irq < 0) {
145 dev_err(&pdev->dev, "No IRQ resource!\n");
146 return -EINVAL;
147 }
148
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800149 if (pm860x_touch_dt_init(pdev, &res_x)) {
150 if (pdata)
151 res_x = pdata->res_x;
152 else {
153 dev_err(&pdev->dev, "failed to get platform data\n");
154 return -EINVAL;
155 }
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500156 }
157
158 touch = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
159 if (touch == NULL)
160 return -ENOMEM;
161 dev_set_drvdata(&pdev->dev, touch);
162
163 touch->idev = input_allocate_device();
164 if (touch->idev == NULL) {
165 dev_err(&pdev->dev, "Failed to allocate input device!\n");
166 ret = -ENOMEM;
167 goto out;
168 }
169
170 touch->idev->name = "88pm860x-touch";
171 touch->idev->phys = "88pm860x/input0";
172 touch->idev->id.bustype = BUS_I2C;
173 touch->idev->dev.parent = &pdev->dev;
174 touch->idev->open = pm860x_touch_open;
175 touch->idev->close = pm860x_touch_close;
176 touch->chip = chip;
177 touch->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800178 touch->irq = irq;
179 touch->res_x = res_x;
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500180 input_set_drvdata(touch->idev, touch);
181
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500182 ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
183 IRQF_ONESHOT, "touch", touch);
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500184 if (ret < 0)
185 goto out_irq;
186
187 __set_bit(EV_ABS, touch->idev->evbit);
188 __set_bit(ABS_X, touch->idev->absbit);
189 __set_bit(ABS_Y, touch->idev->absbit);
190 __set_bit(ABS_PRESSURE, touch->idev->absbit);
191 __set_bit(EV_SYN, touch->idev->evbit);
192 __set_bit(EV_KEY, touch->idev->evbit);
193 __set_bit(BTN_TOUCH, touch->idev->keybit);
194
195 input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
196 input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
197 input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
198 0, 0);
199
200 ret = input_register_device(touch->idev);
201 if (ret < 0) {
202 dev_err(chip->dev, "Failed to register touch!\n");
203 goto out_rg;
204 }
205
206 platform_set_drvdata(pdev, touch);
207 return 0;
208out_rg:
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500209 free_irq(touch->irq, touch);
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500210out_irq:
211 input_free_device(touch->idev);
212out:
213 kfree(touch);
214 return ret;
215}
216
217static int __devexit pm860x_touch_remove(struct platform_device *pdev)
218{
219 struct pm860x_touch *touch = platform_get_drvdata(pdev);
220
221 input_unregister_device(touch->idev);
Haojian Zhuang2afa62e2010-02-08 05:02:00 -0500222 free_irq(touch->irq, touch);
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500223 platform_set_drvdata(pdev, NULL);
224 kfree(touch);
225 return 0;
226}
227
228static struct platform_driver pm860x_touch_driver = {
229 .driver = {
230 .name = "88pm860x-touch",
231 .owner = THIS_MODULE,
232 },
233 .probe = pm860x_touch_probe,
234 .remove = __devexit_p(pm860x_touch_remove),
235};
JJ Dingcdcc96e2011-11-29 11:14:13 -0800236module_platform_driver(pm860x_touch_driver);
Haojian Zhuang866a98a2009-12-15 16:06:17 -0500237
238MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
239MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
240MODULE_LICENSE("GPL");
241MODULE_ALIAS("platform:88pm860x-touch");
242