]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/touchscreen/88pm860x-ts.c
mfd: Use genirq in 88pm860x
[net-next-2.6.git] / drivers / input / touchscreen / 88pm860x-ts.c
CommitLineData
866a98ae
HZ
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
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
40struct 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
48static 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
866a98ae
HZ
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);
866a98ae
HZ
85
86out:
87 return IRQ_HANDLED;
88}
89
90static int pm860x_touch_open(struct input_dev *dev)
91{
92 struct pm860x_touch *touch = input_get_drvdata(dev);
866a98ae
HZ
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;
866a98ae
HZ
100 return 0;
101out:
102 return ret;
103}
104
105static void pm860x_touch_close(struct input_dev *dev)
106{
107 struct pm860x_touch *touch = input_get_drvdata(dev);
866a98ae
HZ
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);
866a98ae
HZ
113}
114
115static int __devinit pm860x_touch_probe(struct platform_device *pdev)
116{
117 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
118 struct pm860x_platform_data *pm860x_pdata = \
119 pdev->dev.parent->platform_data;
120 struct pm860x_touch_pdata *pdata = NULL;
121 struct pm860x_touch *touch;
122 int irq, ret;
123
124 irq = platform_get_irq(pdev, 0);
125 if (irq < 0) {
126 dev_err(&pdev->dev, "No IRQ resource!\n");
127 return -EINVAL;
128 }
129
130 if (!pm860x_pdata) {
131 dev_err(&pdev->dev, "platform data is missing\n");
132 return -EINVAL;
133 }
134
135 pdata = pm860x_pdata->touch;
136 if (!pdata) {
137 dev_err(&pdev->dev, "touchscreen data is missing\n");
138 return -EINVAL;
139 }
140
141 touch = kzalloc(sizeof(struct pm860x_touch), GFP_KERNEL);
142 if (touch == NULL)
143 return -ENOMEM;
144 dev_set_drvdata(&pdev->dev, touch);
145
146 touch->idev = input_allocate_device();
147 if (touch->idev == NULL) {
148 dev_err(&pdev->dev, "Failed to allocate input device!\n");
149 ret = -ENOMEM;
150 goto out;
151 }
152
153 touch->idev->name = "88pm860x-touch";
154 touch->idev->phys = "88pm860x/input0";
155 touch->idev->id.bustype = BUS_I2C;
156 touch->idev->dev.parent = &pdev->dev;
157 touch->idev->open = pm860x_touch_open;
158 touch->idev->close = pm860x_touch_close;
159 touch->chip = chip;
160 touch->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
2afa62ea 161 touch->irq = irq + chip->irq_base;
866a98ae
HZ
162 touch->res_x = pdata->res_x;
163 input_set_drvdata(touch->idev, touch);
164
2afa62ea
HZ
165 ret = request_threaded_irq(touch->irq, NULL, pm860x_touch_handler,
166 IRQF_ONESHOT, "touch", touch);
866a98ae
HZ
167 if (ret < 0)
168 goto out_irq;
169
170 __set_bit(EV_ABS, touch->idev->evbit);
171 __set_bit(ABS_X, touch->idev->absbit);
172 __set_bit(ABS_Y, touch->idev->absbit);
173 __set_bit(ABS_PRESSURE, touch->idev->absbit);
174 __set_bit(EV_SYN, touch->idev->evbit);
175 __set_bit(EV_KEY, touch->idev->evbit);
176 __set_bit(BTN_TOUCH, touch->idev->keybit);
177
178 input_set_abs_params(touch->idev, ABS_X, 0, 1 << ACCURATE_BIT, 0, 0);
179 input_set_abs_params(touch->idev, ABS_Y, 0, 1 << ACCURATE_BIT, 0, 0);
180 input_set_abs_params(touch->idev, ABS_PRESSURE, 0, 1 << ACCURATE_BIT,
181 0, 0);
182
183 ret = input_register_device(touch->idev);
184 if (ret < 0) {
185 dev_err(chip->dev, "Failed to register touch!\n");
186 goto out_rg;
187 }
188
189 platform_set_drvdata(pdev, touch);
190 return 0;
191out_rg:
2afa62ea 192 free_irq(touch->irq, touch);
866a98ae
HZ
193out_irq:
194 input_free_device(touch->idev);
195out:
196 kfree(touch);
197 return ret;
198}
199
200static int __devexit pm860x_touch_remove(struct platform_device *pdev)
201{
202 struct pm860x_touch *touch = platform_get_drvdata(pdev);
203
204 input_unregister_device(touch->idev);
2afa62ea 205 free_irq(touch->irq, touch);
866a98ae
HZ
206 platform_set_drvdata(pdev, NULL);
207 kfree(touch);
208 return 0;
209}
210
211static struct platform_driver pm860x_touch_driver = {
212 .driver = {
213 .name = "88pm860x-touch",
214 .owner = THIS_MODULE,
215 },
216 .probe = pm860x_touch_probe,
217 .remove = __devexit_p(pm860x_touch_remove),
218};
219
220static int __init pm860x_touch_init(void)
221{
222 return platform_driver_register(&pm860x_touch_driver);
223}
224module_init(pm860x_touch_init);
225
226static void __exit pm860x_touch_exit(void)
227{
228 platform_driver_unregister(&pm860x_touch_driver);
229}
230module_exit(pm860x_touch_exit);
231
232MODULE_DESCRIPTION("Touchscreen driver for Marvell Semiconductor 88PM860x");
233MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
234MODULE_LICENSE("GPL");
235MODULE_ALIAS("platform:88pm860x-touch");
236