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 	bool running;
45 };
46 
47 #define EETI_TS_BITDEPTH	(11)
48 #define EETI_MAXVAL		((1 << (EETI_TS_BITDEPTH + 1)) - 1)
49 
50 #define REPORT_BIT_PRESSED	BIT(0)
51 #define REPORT_BIT_AD0		BIT(1)
52 #define REPORT_BIT_AD1		BIT(2)
53 #define REPORT_BIT_HAS_PRESSURE	BIT(6)
54 #define REPORT_RES_BITS(v)	(((v) >> 1) + EETI_TS_BITDEPTH)
55 
56 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
57 {
58 	unsigned int res;
59 	u16 x, y;
60 
61 	res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
62 
63 	x = get_unaligned_be16(&buf[1]);
64 	y = get_unaligned_be16(&buf[3]);
65 
66 	/* fix the range to 11 bits */
67 	x >>= res - EETI_TS_BITDEPTH;
68 	y >>= res - EETI_TS_BITDEPTH;
69 
70 	if (buf[0] & REPORT_BIT_HAS_PRESSURE)
71 		input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
72 
73 	touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
74 	input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
75 	input_sync(eeti->input);
76 }
77 
78 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
79 {
80 	struct eeti_ts *eeti = dev_id;
81 	int len;
82 	int error;
83 	char buf[6];
84 
85 	do {
86 		len = i2c_master_recv(eeti->client, buf, sizeof(buf));
87 		if (len != sizeof(buf)) {
88 			error = len < 0 ? len : -EIO;
89 			dev_err(&eeti->client->dev,
90 				"failed to read touchscreen data: %d\n",
91 				error);
92 			break;
93 		}
94 
95 		if (buf[0] & 0x80) {
96 			/* Motion packet */
97 			eeti_ts_report_event(eeti, buf);
98 		}
99 	} while (eeti->running &&
100 		 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
101 
102 	return IRQ_HANDLED;
103 }
104 
105 static void eeti_ts_start(struct eeti_ts *eeti)
106 {
107 	eeti->running = true;
108 	wmb();
109 	enable_irq(eeti->client->irq);
110 }
111 
112 static void eeti_ts_stop(struct eeti_ts *eeti)
113 {
114 	eeti->running = false;
115 	wmb();
116 	disable_irq(eeti->client->irq);
117 }
118 
119 static int eeti_ts_open(struct input_dev *dev)
120 {
121 	struct eeti_ts *eeti = input_get_drvdata(dev);
122 
123 	eeti_ts_start(eeti);
124 
125 	return 0;
126 }
127 
128 static void eeti_ts_close(struct input_dev *dev)
129 {
130 	struct eeti_ts *eeti = input_get_drvdata(dev);
131 
132 	eeti_ts_stop(eeti);
133 }
134 
135 static int eeti_ts_probe(struct i2c_client *client,
136 			 const struct i2c_device_id *idp)
137 {
138 	struct device *dev = &client->dev;
139 	struct eeti_ts *eeti;
140 	struct input_dev *input;
141 	int error;
142 
143 	/*
144 	 * In contrast to what's described in the datasheet, there seems
145 	 * to be no way of probing the presence of that device using I2C
146 	 * commands. So we need to blindly believe it is there, and wait
147 	 * for interrupts to occur.
148 	 */
149 
150 	eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
151 	if (!eeti) {
152 		dev_err(dev, "failed to allocate driver data\n");
153 		return -ENOMEM;
154 	}
155 
156 	input = devm_input_allocate_device(dev);
157 	if (!input) {
158 		dev_err(dev, "Failed to allocate input device.\n");
159 		return -ENOMEM;
160 	}
161 
162 	input_set_capability(input, EV_KEY, BTN_TOUCH);
163 
164 	input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
165 	input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
166 	input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
167 
168 	touchscreen_parse_properties(input, false, &eeti->props);
169 
170 	input->name = client->name;
171 	input->id.bustype = BUS_I2C;
172 	input->open = eeti_ts_open;
173 	input->close = eeti_ts_close;
174 
175 	eeti->client = client;
176 	eeti->input = input;
177 
178 	eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
179 	if (IS_ERR(eeti->attn_gpio))
180 		return PTR_ERR(eeti->attn_gpio);
181 
182 	i2c_set_clientdata(client, eeti);
183 	input_set_drvdata(input, eeti);
184 
185 	error = devm_request_threaded_irq(dev, client->irq,
186 					  NULL, eeti_ts_isr,
187 					  IRQF_ONESHOT,
188 					  client->name, eeti);
189 	if (error) {
190 		dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
191 			error);
192 		return error;
193 	}
194 
195 	/*
196 	 * Disable the device for now. It will be enabled once the
197 	 * input device is opened.
198 	 */
199 	eeti_ts_stop(eeti);
200 
201 	error = input_register_device(input);
202 	if (error)
203 		return error;
204 
205 	return 0;
206 }
207 
208 static int __maybe_unused eeti_ts_suspend(struct device *dev)
209 {
210 	struct i2c_client *client = to_i2c_client(dev);
211 	struct eeti_ts *eeti = i2c_get_clientdata(client);
212 	struct input_dev *input_dev = eeti->input;
213 
214 	mutex_lock(&input_dev->mutex);
215 
216 	if (input_dev->users)
217 		eeti_ts_stop(eeti);
218 
219 	mutex_unlock(&input_dev->mutex);
220 
221 	if (device_may_wakeup(&client->dev))
222 		enable_irq_wake(client->irq);
223 
224 	return 0;
225 }
226 
227 static int __maybe_unused eeti_ts_resume(struct device *dev)
228 {
229 	struct i2c_client *client = to_i2c_client(dev);
230 	struct eeti_ts *eeti = i2c_get_clientdata(client);
231 	struct input_dev *input_dev = eeti->input;
232 
233 	if (device_may_wakeup(&client->dev))
234 		disable_irq_wake(client->irq);
235 
236 	mutex_lock(&input_dev->mutex);
237 
238 	if (input_dev->users)
239 		eeti_ts_start(eeti);
240 
241 	mutex_unlock(&input_dev->mutex);
242 
243 	return 0;
244 }
245 
246 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
247 
248 static const struct i2c_device_id eeti_ts_id[] = {
249 	{ "eeti_ts", 0 },
250 	{ }
251 };
252 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
253 
254 #ifdef CONFIG_OF
255 static const struct of_device_id of_eeti_ts_match[] = {
256 	{ .compatible = "eeti,exc3000-i2c", },
257 	{ }
258 };
259 #endif
260 
261 static struct i2c_driver eeti_ts_driver = {
262 	.driver = {
263 		.name = "eeti_ts",
264 		.pm = &eeti_ts_pm,
265 		.of_match_table = of_match_ptr(of_eeti_ts_match),
266 	},
267 	.probe = eeti_ts_probe,
268 	.id_table = eeti_ts_id,
269 };
270 
271 module_i2c_driver(eeti_ts_driver);
272 
273 MODULE_DESCRIPTION("EETI Touchscreen driver");
274 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
275 MODULE_LICENSE("GPL");
276