1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for ELAN eKTF2127 i2c touchscreen controller
4  *
5  * For this driver the layout of the Chipone icn8318 i2c
6  * touchscreencontroller is used.
7  *
8  * Author:
9  * Michel Verlaan <michel.verl@gmail.com>
10  * Siebren Vroegindeweij <siebren.vroegindeweij@hotmail.com>
11  *
12  * Original chipone_icn8318 driver:
13  * Hans de Goede <hdegoede@redhat.com>
14  */
15 
16 #include <linux/gpio/consumer.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/delay.h>
25 
26 /* Packet header defines (first byte of data send / received) */
27 #define EKTF2127_NOISE			0x40
28 #define EKTF2127_RESPONSE		0x52
29 #define EKTF2127_REQUEST		0x53
30 #define EKTF2127_HELLO			0x55
31 #define EKTF2127_REPORT			0x5d
32 #define EKTF2127_CALIB_DONE		0x66
33 
34 /* Register defines (second byte of data send / received) */
35 #define EKTF2127_ENV_NOISY		0x41
36 #define EKTF2127_HEIGHT			0x60
37 #define EKTF2127_WIDTH			0x63
38 
39 /* 2 bytes header + 5 * 3 bytes coordinates + 3 bytes pressure info + footer */
40 #define EKTF2127_TOUCH_REPORT_SIZE	21
41 #define EKTF2127_MAX_TOUCHES		5
42 
43 struct ektf2127_ts {
44 	struct i2c_client *client;
45 	struct input_dev *input;
46 	struct gpio_desc *power_gpios;
47 	struct touchscreen_properties prop;
48 };
49 
50 static void ektf2127_parse_coordinates(const u8 *buf, unsigned int touch_count,
51 				       struct input_mt_pos *touches)
52 {
53 	int index = 0;
54 	int i;
55 
56 	for (i = 0; i < touch_count; i++) {
57 		index = 2 + i * 3;
58 
59 		touches[i].x = (buf[index] & 0x0f);
60 		touches[i].x <<= 8;
61 		touches[i].x |= buf[index + 2];
62 
63 		touches[i].y = (buf[index] & 0xf0);
64 		touches[i].y <<= 4;
65 		touches[i].y |= buf[index + 1];
66 	}
67 }
68 
69 static void ektf2127_report_event(struct ektf2127_ts *ts, const u8 *buf)
70 {
71 	struct input_mt_pos touches[EKTF2127_MAX_TOUCHES];
72 	int slots[EKTF2127_MAX_TOUCHES];
73 	unsigned int touch_count, i;
74 
75 	touch_count = buf[1] & 0x07;
76 	if (touch_count > EKTF2127_MAX_TOUCHES) {
77 		dev_err(&ts->client->dev,
78 			"Too many touches %d > %d\n",
79 			touch_count, EKTF2127_MAX_TOUCHES);
80 		touch_count = EKTF2127_MAX_TOUCHES;
81 	}
82 
83 	ektf2127_parse_coordinates(buf, touch_count, touches);
84 	input_mt_assign_slots(ts->input, slots, touches,
85 			      touch_count, 0);
86 
87 	for (i = 0; i < touch_count; i++) {
88 		input_mt_slot(ts->input, slots[i]);
89 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
90 		touchscreen_report_pos(ts->input, &ts->prop,
91 				       touches[i].x, touches[i].y, true);
92 	}
93 
94 	input_mt_sync_frame(ts->input);
95 	input_sync(ts->input);
96 }
97 
98 static irqreturn_t ektf2127_irq(int irq, void *dev_id)
99 {
100 	struct ektf2127_ts *ts = dev_id;
101 	struct device *dev = &ts->client->dev;
102 	char buf[EKTF2127_TOUCH_REPORT_SIZE];
103 	int ret;
104 
105 	ret = i2c_master_recv(ts->client, buf, EKTF2127_TOUCH_REPORT_SIZE);
106 	if (ret != EKTF2127_TOUCH_REPORT_SIZE) {
107 		dev_err(dev, "Error reading touch data: %d\n", ret);
108 		goto out;
109 	}
110 
111 	switch (buf[0]) {
112 	case EKTF2127_REPORT:
113 		ektf2127_report_event(ts, buf);
114 		break;
115 
116 	case EKTF2127_NOISE:
117 		if (buf[1] == EKTF2127_ENV_NOISY)
118 			dev_dbg(dev, "Environment is electrically noisy\n");
119 		break;
120 
121 	case EKTF2127_HELLO:
122 	case EKTF2127_CALIB_DONE:
123 		break;
124 
125 	default:
126 		dev_err(dev, "Unexpected packet header byte %#02x\n", buf[0]);
127 		break;
128 	}
129 
130 out:
131 	return IRQ_HANDLED;
132 }
133 
134 static int ektf2127_start(struct input_dev *dev)
135 {
136 	struct ektf2127_ts *ts = input_get_drvdata(dev);
137 
138 	enable_irq(ts->client->irq);
139 	gpiod_set_value_cansleep(ts->power_gpios, 1);
140 
141 	return 0;
142 }
143 
144 static void ektf2127_stop(struct input_dev *dev)
145 {
146 	struct ektf2127_ts *ts = input_get_drvdata(dev);
147 
148 	disable_irq(ts->client->irq);
149 	gpiod_set_value_cansleep(ts->power_gpios, 0);
150 }
151 
152 static int __maybe_unused ektf2127_suspend(struct device *dev)
153 {
154 	struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
155 
156 	mutex_lock(&ts->input->mutex);
157 	if (ts->input->users)
158 		ektf2127_stop(ts->input);
159 	mutex_unlock(&ts->input->mutex);
160 
161 	return 0;
162 }
163 
164 static int __maybe_unused ektf2127_resume(struct device *dev)
165 {
166 	struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
167 
168 	mutex_lock(&ts->input->mutex);
169 	if (ts->input->users)
170 		ektf2127_start(ts->input);
171 	mutex_unlock(&ts->input->mutex);
172 
173 	return 0;
174 }
175 
176 static SIMPLE_DEV_PM_OPS(ektf2127_pm_ops, ektf2127_suspend,
177 			 ektf2127_resume);
178 
179 static int ektf2127_query_dimension(struct i2c_client *client, bool width)
180 {
181 	struct device *dev = &client->dev;
182 	const char *what = width ? "width" : "height";
183 	u8 what_code = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
184 	u8 buf[4];
185 	int ret;
186 	int error;
187 
188 	/* Request dimension */
189 	buf[0] = EKTF2127_REQUEST;
190 	buf[1] = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
191 	buf[2] = 0x00;
192 	buf[3] = 0x00;
193 	ret = i2c_master_send(client, buf, sizeof(buf));
194 	if (ret != sizeof(buf)) {
195 		error = ret < 0 ? ret : -EIO;
196 		dev_err(dev, "Failed to request %s: %d\n", what, error);
197 		return error;
198 	}
199 
200 	msleep(20);
201 
202 	/* Read response */
203 	ret = i2c_master_recv(client, buf, sizeof(buf));
204 	if (ret != sizeof(buf)) {
205 		error = ret < 0 ? ret : -EIO;
206 		dev_err(dev, "Failed to receive %s data: %d\n", what, error);
207 		return error;
208 	}
209 
210 	if (buf[0] != EKTF2127_RESPONSE || buf[1] != what_code) {
211 		dev_err(dev, "Unexpected %s data: %#02x %#02x\n",
212 			what, buf[0], buf[1]);
213 		return -EIO;
214 	}
215 
216 	return (((buf[3] & 0xf0) << 4) | buf[2]) - 1;
217 }
218 
219 static int ektf2127_probe(struct i2c_client *client,
220 			  const struct i2c_device_id *id)
221 {
222 	struct device *dev = &client->dev;
223 	struct ektf2127_ts *ts;
224 	struct input_dev *input;
225 	u8 buf[4];
226 	int max_x, max_y;
227 	int error;
228 
229 	if (!client->irq) {
230 		dev_err(dev, "Error no irq specified\n");
231 		return -EINVAL;
232 	}
233 
234 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
235 	if (!ts)
236 		return -ENOMEM;
237 
238 	/* This requests the gpio *and* turns on the touchscreen controller */
239 	ts->power_gpios = devm_gpiod_get(dev, "power", GPIOD_OUT_HIGH);
240 	if (IS_ERR(ts->power_gpios)) {
241 		error = PTR_ERR(ts->power_gpios);
242 		if (error != -EPROBE_DEFER)
243 			dev_err(dev, "Error getting power gpio: %d\n", error);
244 		return error;
245 	}
246 
247 	input = devm_input_allocate_device(dev);
248 	if (!input)
249 		return -ENOMEM;
250 
251 	input->name = client->name;
252 	input->id.bustype = BUS_I2C;
253 	input->open = ektf2127_start;
254 	input->close = ektf2127_stop;
255 
256 	ts->client = client;
257 
258 	/* Read hello (ignore result, depends on initial power state) */
259 	msleep(20);
260 	i2c_master_recv(ts->client, buf, sizeof(buf));
261 
262 	/* Read resolution from chip */
263 	max_x = ektf2127_query_dimension(client, true);
264 	if (max_x < 0)
265 		return max_x;
266 
267 	max_y = ektf2127_query_dimension(client, false);
268 	if (max_y < 0)
269 		return max_y;
270 
271 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
272 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
273 	touchscreen_parse_properties(input, true, &ts->prop);
274 
275 	error = input_mt_init_slots(input, EKTF2127_MAX_TOUCHES,
276 				    INPUT_MT_DIRECT |
277 					INPUT_MT_DROP_UNUSED |
278 					INPUT_MT_TRACK);
279 	if (error)
280 		return error;
281 
282 	ts->input = input;
283 	input_set_drvdata(input, ts);
284 
285 	error = devm_request_threaded_irq(dev, client->irq,
286 					  NULL, ektf2127_irq,
287 					  IRQF_ONESHOT, client->name, ts);
288 	if (error) {
289 		dev_err(dev, "Error requesting irq: %d\n", error);
290 		return error;
291 	}
292 
293 	/* Stop device till opened */
294 	ektf2127_stop(ts->input);
295 
296 	error = input_register_device(input);
297 	if (error)
298 		return error;
299 
300 	i2c_set_clientdata(client, ts);
301 
302 	return 0;
303 }
304 
305 #ifdef CONFIG_OF
306 static const struct of_device_id ektf2127_of_match[] = {
307 	{ .compatible = "elan,ektf2127" },
308 	{}
309 };
310 MODULE_DEVICE_TABLE(of, ektf2127_of_match);
311 #endif
312 
313 static const struct i2c_device_id ektf2127_i2c_id[] = {
314 	{ "ektf2127", 0 },
315 	{}
316 };
317 MODULE_DEVICE_TABLE(i2c, ektf2127_i2c_id);
318 
319 static struct i2c_driver ektf2127_driver = {
320 	.driver = {
321 		.name	= "elan_ektf2127",
322 		.pm	= &ektf2127_pm_ops,
323 		.of_match_table = of_match_ptr(ektf2127_of_match),
324 	},
325 	.probe = ektf2127_probe,
326 	.id_table = ektf2127_i2c_id,
327 };
328 module_i2c_driver(ektf2127_driver);
329 
330 MODULE_DESCRIPTION("ELAN eKTF2127 I2C Touchscreen Driver");
331 MODULE_AUTHOR("Michel Verlaan, Siebren Vroegindeweij");
332 MODULE_LICENSE("GPL");
333