1 /*
2  * Touch Screen driver for EETI's I2C connected touch screen panels
3  *   Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
4  *
5  * See EETI's software guide for the protocol specification:
6  *   http://home.eeti.com.tw/documentation.html
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/kernel.h>
29 #include <linux/input.h>
30 #include <linux/input/touchscreen.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2c.h>
33 #include <linux/timer.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/of.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
38 
39 struct eeti_ts {
40 	struct i2c_client *client;
41 	struct input_dev *input;
42 	struct gpio_desc *attn_gpio;
43 	struct touchscreen_properties props;
44 	struct mutex mutex;
45 	bool running;
46 };
47 
48 #define EETI_TS_BITDEPTH	(11)
49 #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
50 
51 #define REPORT_BIT_PRESSED	BIT(0)
52 #define REPORT_BIT_AD0		BIT(1)
53 #define REPORT_BIT_AD1		BIT(2)
54 #define REPORT_BIT_HAS_PRESSURE	BIT(6)
55 #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
56 
57 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
58 {
59 	unsigned int res;
60 	u16 x, y;
61 
62 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
63 
64 	x = get_unaligned_be16(&buf[1]);
65 	y = get_unaligned_be16(&buf[3]);
66 
67 	/* fix the range to 11 bits */
68 	x >>= res - EETI_TS_BITDEPTH;
69 	y >>= res - EETI_TS_BITDEPTH;
70 
71 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
72 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
73 
74 	touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
75 	input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
76 	input_sync(eeti->input);
77 }
78 
79 static int eeti_ts_read(struct eeti_ts *eeti)
80 {
81 	int len, error;
82 	char buf[6];
83 
84 	len = i2c_master_recv(eeti->client, buf, sizeof(buf));
85 	if (len != sizeof(buf)) {
86 		error = len < 0 ? len : -EIO;
87 		dev_err(&eeti->client->dev,
88 			"failed to read touchscreen data: %d\n",
89 			error);
90 		return error;
91 	}
92 
93 	/* Motion packet */
94 	if (buf[0] & 0x80)
95 		eeti_ts_report_event(eeti, buf);
96 
97 	return 0;
98 }
99 
100 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
101 {
102 	struct eeti_ts *eeti = dev_id;
103 	int error;
104 
105 	mutex_lock(&eeti->mutex);
106 
107 	do {
108 		/*
109 		 * If we have attention GPIO, trust it. Otherwise we'll read
110 		 * once and exit. We assume that in this case we are using
111 		 * level triggered interrupt and it will get raised again
112 		 * if/when there is more data.
113 		 */
114 		if (eeti->attn_gpio &&
115 		    !gpiod_get_value_cansleep(eeti->attn_gpio)) {
116 			break;
117 		}
118 
119 		error = eeti_ts_read(eeti);
120 		if (error)
121 			break;
122 
123 	} while (eeti->running && eeti->attn_gpio);
124 
125 	mutex_unlock(&eeti->mutex);
126 	return IRQ_HANDLED;
127 }
128 
129 static void eeti_ts_start(struct eeti_ts *eeti)
130 {
131 	mutex_lock(&eeti->mutex);
132 
133 	eeti->running = true;
134 	enable_irq(eeti->client->irq);
135 
136 	/*
137 	 * Kick the controller in case we are using edge interrupt and
138 	 * we missed our edge while interrupt was disabled. We expect
139 	 * the attention GPIO to be wired in this case.
140 	 */
141 	if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio))
142 		eeti_ts_read(eeti);
143 
144 	mutex_unlock(&eeti->mutex);
145 }
146 
147 static void eeti_ts_stop(struct eeti_ts *eeti)
148 {
149 	/*
150 	 * Not locking here, just setting a flag and expect that the
151 	 * interrupt thread will notice the flag eventually.
152 	 */
153 	eeti->running = false;
154 	wmb();
155 	disable_irq(eeti->client->irq);
156 }
157 
158 static int eeti_ts_open(struct input_dev *dev)
159 {
160 	struct eeti_ts *eeti = input_get_drvdata(dev);
161 
162 	eeti_ts_start(eeti);
163 
164 	return 0;
165 }
166 
167 static void eeti_ts_close(struct input_dev *dev)
168 {
169 	struct eeti_ts *eeti = input_get_drvdata(dev);
170 
171 	eeti_ts_stop(eeti);
172 }
173 
174 static int eeti_ts_probe(struct i2c_client *client,
175 			 const struct i2c_device_id *idp)
176 {
177 	struct device *dev = &client->dev;
178 	struct eeti_ts *eeti;
179 	struct input_dev *input;
180 	int error;
181 
182 	/*
183 	 * In contrast to what's described in the datasheet, there seems
184 	 * to be no way of probing the presence of that device using I2C
185 	 * commands. So we need to blindly believe it is there, and wait
186 	 * for interrupts to occur.
187 	 */
188 
189 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
190 	if (!eeti) {
191 		dev_err(dev, "failed to allocate driver data\n");
192 		return -ENOMEM;
193 	}
194 
195 	mutex_init(&eeti->mutex);
196 
197 	input = devm_input_allocate_device(dev);
198 	if (!input) {
199 		dev_err(dev, "Failed to allocate input device.\n");
200 		return -ENOMEM;
201 	}
202 
203 	input_set_capability(input, EV_KEY, BTN_TOUCH);
204 
205 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
206 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
207 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
208 
209 	touchscreen_parse_properties(input, false, &eeti->props);
210 
211 	input->name = client->name;
212 	input->id.bustype = BUS_I2C;
213 	input->open = eeti_ts_open;
214 	input->close = eeti_ts_close;
215 
216 	eeti->client = client;
217 	eeti->input = input;
218 
219 	eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
220 	if (IS_ERR(eeti->attn_gpio))
221 		return PTR_ERR(eeti->attn_gpio);
222 
223 	i2c_set_clientdata(client, eeti);
224 	input_set_drvdata(input, eeti);
225 
226 	error = devm_request_threaded_irq(dev, client->irq,
227 					  NULL, eeti_ts_isr,
228 					  IRQF_ONESHOT,
229 					  client->name, eeti);
230 	if (error) {
231 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
232 			error);
233 		return error;
234 	}
235 
236 	/*
237 	 * Disable the device for now. It will be enabled once the
238 	 * input device is opened.
239 	 */
240 	eeti_ts_stop(eeti);
241 
242 	error = input_register_device(input);
243 	if (error)
244 		return error;
245 
246 	return 0;
247 }
248 
249 static int __maybe_unused eeti_ts_suspend(struct device *dev)
250 {
251 	struct i2c_client *client = to_i2c_client(dev);
252 	struct eeti_ts *eeti = i2c_get_clientdata(client);
253 	struct input_dev *input_dev = eeti->input;
254 
255 	mutex_lock(&input_dev->mutex);
256 
257 	if (input_dev->users)
258 		eeti_ts_stop(eeti);
259 
260 	mutex_unlock(&input_dev->mutex);
261 
262 	if (device_may_wakeup(&client->dev))
263 		enable_irq_wake(client->irq);
264 
265 	return 0;
266 }
267 
268 static int __maybe_unused eeti_ts_resume(struct device *dev)
269 {
270 	struct i2c_client *client = to_i2c_client(dev);
271 	struct eeti_ts *eeti = i2c_get_clientdata(client);
272 	struct input_dev *input_dev = eeti->input;
273 
274 	if (device_may_wakeup(&client->dev))
275 		disable_irq_wake(client->irq);
276 
277 	mutex_lock(&input_dev->mutex);
278 
279 	if (input_dev->users)
280 		eeti_ts_start(eeti);
281 
282 	mutex_unlock(&input_dev->mutex);
283 
284 	return 0;
285 }
286 
287 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
288 
289 static const struct i2c_device_id eeti_ts_id[] = {
290 	{ "eeti_ts", 0 },
291 	{ }
292 };
293 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
294 
295 #ifdef CONFIG_OF
296 static const struct of_device_id of_eeti_ts_match[] = {
297 	{ .compatible = "eeti,exc3000-i2c", },
298 	{ }
299 };
300 #endif
301 
302 static struct i2c_driver eeti_ts_driver = {
303 	.driver = {
304 		.name = "eeti_ts",
305 		.pm = &eeti_ts_pm,
306 		.of_match_table = of_match_ptr(of_eeti_ts_match),
307 	},
308 	.probe = eeti_ts_probe,
309 	.id_table = eeti_ts_id,
310 };
311 
312 module_i2c_driver(eeti_ts_driver);
313 
314 MODULE_DESCRIPTION("EETI Touchscreen driver");
315 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
316 MODULE_LICENSE("GPL");
317