1 /*
2  * Touch Screen driver for EETI's I2C connected touch screen panels
3  *   Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
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 <ujjwal@kenati.com>
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_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->client->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->client->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 device *dev = &client->dev;
163 	struct eeti_ts_platform_data *pdata = dev_get_platdata(dev);
164 	struct eeti_ts *eeti;
165 	struct input_dev *input;
166 	unsigned int irq_flags;
167 	int error;
168 
169 	/*
170 	 * In contrast to what's described in the datasheet, there seems
171 	 * to be no way of probing the presence of that device using I2C
172 	 * commands. So we need to blindly believe it is there, and wait
173 	 * for interrupts to occur.
174 	 */
175 
176 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
177 	if (!eeti) {
178 		dev_err(dev, "failed to allocate driver data\n");
179 		return -ENOMEM;
180 	}
181 
182 	mutex_init(&eeti->mutex);
183 
184 	input = devm_input_allocate_device(dev);
185 	if (!input) {
186 		dev_err(dev, "Failed to allocate input device.\n");
187 		return -ENOMEM;
188 	}
189 
190 	input_set_capability(input, EV_KEY, BTN_TOUCH);
191 
192 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
193 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
194 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
195 
196 	input->name = client->name;
197 	input->id.bustype = BUS_I2C;
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 
205 	error = devm_gpio_request_one(dev, pdata->irq_gpio, GPIOF_IN,
206 				      client->name);
207 	if (error)
208 		return error;
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 	error = input_register_device(input);
220 	if (error)
221 		return error;
222 
223 	error = devm_request_irq(dev, client->irq, eeti_ts_isr, irq_flags,
224 				 client->name, eeti);
225 	if (error) {
226 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
227 			error);
228 		return error;
229 	}
230 
231 	/*
232 	 * Disable the device for now. It will be enabled once the
233 	 * input device is opened.
234 	 */
235 	eeti_ts_stop(eeti);
236 
237 	return 0;
238 }
239 
240 static int __maybe_unused eeti_ts_suspend(struct device *dev)
241 {
242 	struct i2c_client *client = to_i2c_client(dev);
243 	struct eeti_ts *eeti = i2c_get_clientdata(client);
244 	struct input_dev *input_dev = eeti->input;
245 
246 	mutex_lock(&input_dev->mutex);
247 
248 	if (input_dev->users)
249 		eeti_ts_stop(eeti);
250 
251 	mutex_unlock(&input_dev->mutex);
252 
253 	if (device_may_wakeup(&client->dev))
254 		enable_irq_wake(client->irq);
255 
256 	return 0;
257 }
258 
259 static int __maybe_unused eeti_ts_resume(struct device *dev)
260 {
261 	struct i2c_client *client = to_i2c_client(dev);
262 	struct eeti_ts *eeti = i2c_get_clientdata(client);
263 	struct input_dev *input_dev = eeti->input;
264 
265 	if (device_may_wakeup(&client->dev))
266 		disable_irq_wake(client->irq);
267 
268 	mutex_lock(&input_dev->mutex);
269 
270 	if (input_dev->users)
271 		eeti_ts_start(eeti);
272 
273 	mutex_unlock(&input_dev->mutex);
274 
275 	return 0;
276 }
277 
278 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
279 
280 static const struct i2c_device_id eeti_ts_id[] = {
281 	{ "eeti_ts", 0 },
282 	{ }
283 };
284 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
285 
286 static struct i2c_driver eeti_ts_driver = {
287 	.driver = {
288 		.name = "eeti_ts",
289 		.pm = &eeti_ts_pm,
290 	},
291 	.probe = eeti_ts_probe,
292 	.id_table = eeti_ts_id,
293 };
294 
295 module_i2c_driver(eeti_ts_driver);
296 
297 MODULE_DESCRIPTION("EETI Touchscreen driver");
298 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
299 MODULE_LICENSE("GPL");
300