1 /*
2  * Touch Screen driver for EETI's I2C connected touch screen panels
3  *   Copyright (c) 2009 Daniel Mack <[email protected]>
4  *
5  * See EETI's software guide for the protocol specification:
6  *   http://home.eeti.com.tw/web20/eg/guide.htm
7  *
8  * Based on migor_ts.c
9  *   Copyright (c) 2008 Magnus Damm
10  *   Copyright (c) 2007 Ujjwal Pande <[email protected]>
11  *
12  * This file is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU  General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This file is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/kernel.h>
30 #include <linux/input.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2c.h>
33 #include <linux/timer.h>
34 #include <linux/gpio.h>
35 #include <linux/input/eeti_ts.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
38 
39 static bool flip_x;
40 module_param(flip_x, bool, 0644);
41 MODULE_PARM_DESC(flip_x, "flip x coordinate");
42 
43 static bool flip_y;
44 module_param(flip_y, bool, 0644);
45 MODULE_PARM_DESC(flip_y, "flip y coordinate");
46 
47 struct eeti_ts {
48 	struct i2c_client *client;
49 	struct input_dev *input;
50 	struct work_struct work;
51 	struct mutex mutex;
52 	int irq_gpio, irq, irq_active_high;
53 };
54 
55 #define EETI_TS_BITDEPTH	(11)
56 #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
57 
58 #define REPORT_BIT_PRESSED	BIT(0)
59 #define REPORT_BIT_AD0		BIT(1)
60 #define REPORT_BIT_AD1		BIT(2)
61 #define REPORT_BIT_HAS_PRESSURE	BIT(6)
62 #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
63 
64 static inline int eeti_ts_irq_active(struct eeti_ts *eeti)
65 {
66 	return gpio_get_value(eeti->irq_gpio) == eeti->irq_active_high;
67 }
68 
69 static void eeti_ts_read(struct work_struct *work)
70 {
71 	char buf[6];
72 	unsigned int x, y, res, pressed, to = 100;
73 	struct eeti_ts *eeti =
74 		container_of(work, struct eeti_ts, work);
75 
76 	mutex_lock(&eeti->mutex);
77 
78 	while (eeti_ts_irq_active(eeti) && --to)
79 		i2c_master_recv(eeti->client, buf, sizeof(buf));
80 
81 	if (!to) {
82 		dev_err(&eeti->client->dev,
83 			"unable to clear IRQ - line stuck?\n");
84 		goto out;
85 	}
86 
87 	/* drop non-report packets */
88 	if (!(buf[0] & 0x80))
89 		goto out;
90 
91 	pressed = buf[0] & REPORT_BIT_PRESSED;
92 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
93 
94 	x = get_unaligned_be16(&buf[1]);
95 	y = get_unaligned_be16(&buf[3]);
96 
97 	/* fix the range to 11 bits */
98 	x >>= res - EETI_TS_BITDEPTH;
99 	y >>= res - EETI_TS_BITDEPTH;
100 
101 	if (flip_x)
102 		x = EETI_MAXVAL - x;
103 
104 	if (flip_y)
105 		y = EETI_MAXVAL - y;
106 
107 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
108 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
109 
110 	input_report_abs(eeti->input, ABS_X, x);
111 	input_report_abs(eeti->input, ABS_Y, y);
112 	input_report_key(eeti->input, BTN_TOUCH, !!pressed);
113 	input_sync(eeti->input);
114 
115 out:
116 	mutex_unlock(&eeti->mutex);
117 }
118 
119 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
120 {
121 	struct eeti_ts *eeti = dev_id;
122 
123 	 /* postpone I2C transactions as we are atomic */
124 	schedule_work(&eeti->work);
125 
126 	return IRQ_HANDLED;
127 }
128 
129 static void eeti_ts_start(struct eeti_ts *eeti)
130 {
131 	enable_irq(eeti->irq);
132 
133 	/* Read the events once to arm the IRQ */
134 	eeti_ts_read(&eeti->work);
135 }
136 
137 static void eeti_ts_stop(struct eeti_ts *eeti)
138 {
139 	disable_irq(eeti->irq);
140 	cancel_work_sync(&eeti->work);
141 }
142 
143 static int eeti_ts_open(struct input_dev *dev)
144 {
145 	struct eeti_ts *eeti = input_get_drvdata(dev);
146 
147 	eeti_ts_start(eeti);
148 
149 	return 0;
150 }
151 
152 static void eeti_ts_close(struct input_dev *dev)
153 {
154 	struct eeti_ts *eeti = input_get_drvdata(dev);
155 
156 	eeti_ts_stop(eeti);
157 }
158 
159 static int eeti_ts_probe(struct i2c_client *client,
160 				   const struct i2c_device_id *idp)
161 {
162 	struct eeti_ts_platform_data *pdata = dev_get_platdata(&client->dev);
163 	struct eeti_ts *eeti;
164 	struct input_dev *input;
165 	unsigned int irq_flags;
166 	int err = -ENOMEM;
167 
168 	/*
169 	 * In contrast to what's described in the datasheet, there seems
170 	 * to be no way of probing the presence of that device using I2C
171 	 * commands. So we need to blindly believe it is there, and wait
172 	 * for interrupts to occur.
173 	 */
174 
175 	eeti = kzalloc(sizeof(*eeti), GFP_KERNEL);
176 	if (!eeti) {
177 		dev_err(&client->dev, "failed to allocate driver data\n");
178 		return -ENOMEM;
179 	}
180 
181 	mutex_init(&eeti->mutex);
182 
183 	input = input_allocate_device();
184 	if (!input) {
185 		dev_err(&client->dev, "Failed to allocate input device.\n");
186 		goto err1;
187 	}
188 
189 	input_set_capability(input, EV_KEY, BTN_TOUCH);
190 
191 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
192 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
193 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
194 
195 	input->name = client->name;
196 	input->id.bustype = BUS_I2C;
197 	input->dev.parent = &client->dev;
198 	input->open = eeti_ts_open;
199 	input->close = eeti_ts_close;
200 
201 	eeti->client = client;
202 	eeti->input = input;
203 	eeti->irq_gpio = pdata->irq_gpio;
204 	eeti->irq = gpio_to_irq(pdata->irq_gpio);
205 
206 	err = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
207 	if (err < 0)
208 		goto err1;
209 
210 	eeti->irq_active_high = pdata->irq_active_high;
211 
212 	irq_flags = eeti->irq_active_high ?
213 		IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
214 
215 	INIT_WORK(&eeti->work, eeti_ts_read);
216 	i2c_set_clientdata(client, eeti);
217 	input_set_drvdata(input, eeti);
218 
219 	err = input_register_device(input);
220 	if (err)
221 		goto err2;
222 
223 	err = request_irq(eeti->irq, eeti_ts_isr, irq_flags,
224 			  client->name, eeti);
225 	if (err) {
226 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
227 		goto err3;
228 	}
229 
230 	/*
231 	 * Disable the device for now. It will be enabled once the
232 	 * input device is opened.
233 	 */
234 	eeti_ts_stop(eeti);
235 
236 	return 0;
237 
238 err3:
239 	input_unregister_device(input);
240 	input = NULL; /* so we dont try to free it below */
241 err2:
242 	gpio_free(pdata->irq_gpio);
243 err1:
244 	input_free_device(input);
245 	kfree(eeti);
246 	return err;
247 }
248 
249 static int eeti_ts_remove(struct i2c_client *client)
250 {
251 	struct eeti_ts *eeti = i2c_get_clientdata(client);
252 
253 	free_irq(eeti->irq, eeti);
254 	/*
255 	 * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
256 	 * so that device still works if we reload the driver.
257 	 */
258 	enable_irq(eeti->irq);
259 
260 	input_unregister_device(eeti->input);
261 	kfree(eeti);
262 
263 	return 0;
264 }
265 
266 static int __maybe_unused eeti_ts_suspend(struct device *dev)
267 {
268 	struct i2c_client *client = to_i2c_client(dev);
269 	struct eeti_ts *eeti = i2c_get_clientdata(client);
270 	struct input_dev *input_dev = eeti->input;
271 
272 	mutex_lock(&input_dev->mutex);
273 
274 	if (input_dev->users)
275 		eeti_ts_stop(eeti);
276 
277 	mutex_unlock(&input_dev->mutex);
278 
279 	if (device_may_wakeup(&client->dev))
280 		enable_irq_wake(eeti->irq);
281 
282 	return 0;
283 }
284 
285 static int __maybe_unused eeti_ts_resume(struct device *dev)
286 {
287 	struct i2c_client *client = to_i2c_client(dev);
288 	struct eeti_ts *eeti = i2c_get_clientdata(client);
289 	struct input_dev *input_dev = eeti->input;
290 
291 	if (device_may_wakeup(&client->dev))
292 		disable_irq_wake(eeti->irq);
293 
294 	mutex_lock(&input_dev->mutex);
295 
296 	if (input_dev->users)
297 		eeti_ts_start(eeti);
298 
299 	mutex_unlock(&input_dev->mutex);
300 
301 	return 0;
302 }
303 
304 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
305 
306 static const struct i2c_device_id eeti_ts_id[] = {
307 	{ "eeti_ts", 0 },
308 	{ }
309 };
310 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
311 
312 static struct i2c_driver eeti_ts_driver = {
313 	.driver = {
314 		.name = "eeti_ts",
315 		.pm = &eeti_ts_pm,
316 	},
317 	.probe = eeti_ts_probe,
318 	.remove = eeti_ts_remove,
319 	.id_table = eeti_ts_id,
320 };
321 
322 module_i2c_driver(eeti_ts_driver);
323 
324 MODULE_DESCRIPTION("EETI Touchscreen driver");
325 MODULE_AUTHOR("Daniel Mack <[email protected]>");
326 MODULE_LICENSE("GPL");
327