Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / input / touchscreen / tsc210x_ts.c
1 /*
2  * tsc210x_ts.c - touchscreen input device for TI TSC210x chips
3  *
4  * Copyright (c) 2006-2007 Andrzej Zaborowski  <balrog@zabor.org>
5  *
6  * This package is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This package is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/input.h>
25 #include <linux/init.h>
26 #include <linux/platform_device.h>
27
28 #include <linux/spi/tsc210x.h>
29
30
31 /*
32  * The sensor ADC on tsc210x chips is most often used with the smart
33  * touchscreen controller.   Those controllers can be made to improve
34  * sample quality directly by multi-sampling and by taking the mean or
35  * median of various numbers of samples.  They also take X, Y, and
36  * pressure measurements automatically, so this driver has relatively
37  * little to do.
38  *
39  * There are a few chips in this family that don't have quite the same
40  * touchscreen interface, e.g. no "median" mode.
41  */
42
43 static void tsc210x_touch(void *context, int touching)
44 {
45         struct input_dev *dev = context;
46
47         if (!touching) {
48                 input_report_abs(dev, ABS_X, 0);
49                 input_report_abs(dev, ABS_Y, 0);
50                 input_report_abs(dev, ABS_PRESSURE, 0);
51                 input_sync(dev);
52         }
53
54         input_report_key(dev, BTN_TOUCH, touching);
55 }
56
57 static void tsc210x_coords(void *context, int x, int y, int z1, int z2)
58 {
59         struct input_dev *dev = context;
60         int p;
61
62         /* Calculate the touch resistance a la equation #1 */
63         if (z1 != 0)
64                 p = x * (z2 - z1) / (z1 << 4);
65         else
66                 p = 1;
67
68         input_report_abs(dev, ABS_X, x);
69         input_report_abs(dev, ABS_Y, y);
70         input_report_abs(dev, ABS_PRESSURE, p);
71         input_sync(dev);
72 }
73
74 static int tsc210x_ts_probe(struct platform_device *pdev)
75 {
76         int status;
77         struct input_dev *dev;
78
79         dev = input_allocate_device();
80         if (!dev)
81                 return -ENOMEM;
82
83         status = tsc210x_touch_cb(pdev->dev.parent, tsc210x_touch, dev);
84         if (status) {
85                 input_free_device(dev);
86                 return status;
87         }
88
89         status = tsc210x_coords_cb(pdev->dev.parent, tsc210x_coords, dev);
90         if (status) {
91                 tsc210x_touch_cb(pdev->dev.parent, NULL, NULL);
92                 input_free_device(dev);
93                 return status;
94         }
95
96         dev->name = "TSC210x Touchscreen";
97         dev->dev.parent = &pdev->dev;
98         dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
99         dev->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
100         dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
101         dev->phys = "tsc210x/input0";
102         dev->id.bustype = BUS_HOST;
103         dev->id.vendor = 0x0001;
104         dev->id.product = 0x2100;
105         dev->id.version = 0x0001;
106
107         status = input_register_device(dev);
108         if (status) {
109                 tsc210x_coords_cb(pdev->dev.parent, NULL, NULL);
110                 tsc210x_touch_cb(pdev->dev.parent, NULL, NULL);
111                 input_free_device(dev);
112                 return status;
113         }
114
115         platform_set_drvdata(pdev, dev);
116         printk(KERN_INFO "TSC210x touchscreen initialised\n");
117         return 0;
118 }
119
120 static int __exit tsc210x_ts_remove(struct platform_device *pdev)
121 {
122         struct input_dev *dev = platform_get_drvdata(pdev);
123
124         tsc210x_touch_cb(pdev->dev.parent, NULL, NULL);
125         tsc210x_coords_cb(pdev->dev.parent, NULL, NULL);
126         platform_set_drvdata(pdev, NULL);
127         input_unregister_device(dev);
128         input_free_device(dev);
129
130         return 0;
131 }
132
133 static struct platform_driver tsc210x_ts_driver = {
134         .probe          = tsc210x_ts_probe,
135         .remove         = __exit_p(tsc210x_ts_remove),
136         /* Nothing to do on suspend/resume */
137         .driver         = {
138                 .name   = "tsc210x-ts",
139                 .owner  = THIS_MODULE,
140         },
141 };
142
143 static int __init tsc210x_ts_init(void)
144 {
145         /* can't use driver_probe() here since the parent device
146          * gets registered "late"
147          */
148         return platform_driver_register(&tsc210x_ts_driver);
149 }
150 module_init(tsc210x_ts_init);
151
152 static void __exit tsc210x_ts_exit(void)
153 {
154         platform_driver_unregister(&tsc210x_ts_driver);
155 }
156 module_exit(tsc210x_ts_exit);
157
158 MODULE_AUTHOR("Andrzej Zaborowski");
159 MODULE_DESCRIPTION("Touchscreen input driver for TI TSC2101/2102.");
160 MODULE_LICENSE("GPL");