xref: /linux/drivers/input/touchscreen/himax_hx852x.c (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Himax HX852x(ES) Touchscreen Driver
4  * Copyright (c) 2020-2024 Stephan Gerhold <stephan@gerhold.net>
5  * Copyright (c) 2020 Jonathan Albrieux <jonathan.albrieux@gmail.com>
6  *
7  * Based on the Himax Android Driver Sample Code Ver 0.3 for HMX852xES chipset:
8  * Copyright (c) 2014 Himax Corporation.
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/input/mt.h>
16 #include <linux/input/touchscreen.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/unaligned.h>
24 
25 #define HX852X_COORD_SIZE(fingers)	((fingers) * sizeof(struct hx852x_coord))
26 #define HX852X_WIDTH_SIZE(fingers)	ALIGN(fingers, 4)
27 #define HX852X_BUF_SIZE(fingers)	(HX852X_COORD_SIZE(fingers) + \
28 					 HX852X_WIDTH_SIZE(fingers) + \
29 					 sizeof(struct hx852x_touch_info))
30 
31 #define HX852X_MAX_FINGERS		12
32 #define HX852X_MAX_KEY_COUNT		4
33 #define HX852X_MAX_BUF_SIZE		HX852X_BUF_SIZE(HX852X_MAX_FINGERS)
34 
35 #define HX852X_TS_SLEEP_IN		0x80
36 #define HX852X_TS_SLEEP_OUT		0x81
37 #define HX852X_TS_SENSE_OFF		0x82
38 #define HX852X_TS_SENSE_ON		0x83
39 #define HX852X_READ_ONE_EVENT		0x85
40 #define HX852X_READ_ALL_EVENTS		0x86
41 #define HX852X_READ_LATEST_EVENT	0x87
42 #define HX852X_CLEAR_EVENT_STACK	0x88
43 
44 #define HX852X_REG_SRAM_SWITCH		0x8c
45 #define HX852X_REG_SRAM_ADDR		0x8b
46 #define HX852X_REG_FLASH_RPLACE		0x5a
47 
48 #define HX852X_SRAM_SWITCH_TEST_MODE	0x14
49 #define HX852X_SRAM_ADDR_CONFIG		0x7000
50 
51 struct hx852x {
52 	struct i2c_client *client;
53 	struct input_dev *input_dev;
54 	struct touchscreen_properties props;
55 	struct gpio_desc *reset_gpiod;
56 	struct regulator_bulk_data supplies[2];
57 	unsigned int max_fingers;
58 	unsigned int keycount;
59 	unsigned int keycodes[HX852X_MAX_KEY_COUNT];
60 };
61 
62 struct hx852x_config {
63 	u8 rx_num;
64 	u8 tx_num;
65 	u8 max_pt;
66 	u8 padding1[3];
67 	__be16 x_res;
68 	__be16 y_res;
69 	u8 padding2[2];
70 } __packed __aligned(4);
71 
72 struct hx852x_coord {
73 	__be16 x;
74 	__be16 y;
75 } __packed __aligned(4);
76 
77 struct hx852x_touch_info {
78 	u8 finger_num;
79 	__le16 finger_pressed;
80 	u8 padding;
81 } __packed __aligned(4);
82 
83 static int hx852x_i2c_read(struct hx852x *hx, u8 cmd, void *data, u16 len)
84 {
85 	struct i2c_client *client = hx->client;
86 	int error;
87 	int ret;
88 
89 	struct i2c_msg msg[] = {
90 		{
91 			.addr = client->addr,
92 			.flags = 0,
93 			.len = 1,
94 			.buf = &cmd,
95 		},
96 		{
97 			.addr = client->addr,
98 			.flags = I2C_M_RD,
99 			.len = len,
100 			.buf = data,
101 		},
102 	};
103 
104 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
105 	if (ret != ARRAY_SIZE(msg)) {
106 		error = ret < 0 ? ret : -EIO;
107 		dev_err(&client->dev, "failed to read %#x: %d\n", cmd, error);
108 		return error;
109 	}
110 
111 	return 0;
112 }
113 
114 static int hx852x_power_on(struct hx852x *hx)
115 {
116 	struct device *dev = &hx->client->dev;
117 	int error;
118 
119 	error = regulator_bulk_enable(ARRAY_SIZE(hx->supplies), hx->supplies);
120 	if (error) {
121 		dev_err(dev, "failed to enable regulators: %d\n", error);
122 		return error;
123 	}
124 
125 	gpiod_set_value_cansleep(hx->reset_gpiod, 1);
126 	msleep(20);
127 	gpiod_set_value_cansleep(hx->reset_gpiod, 0);
128 	msleep(50);
129 
130 	return 0;
131 }
132 
133 static int hx852x_start(struct hx852x *hx)
134 {
135 	struct device *dev = &hx->client->dev;
136 	int error;
137 
138 	error = i2c_smbus_write_byte(hx->client, HX852X_TS_SLEEP_OUT);
139 	if (error) {
140 		dev_err(dev, "failed to send TS_SLEEP_OUT: %d\n", error);
141 		return error;
142 	}
143 	msleep(30);
144 
145 	error = i2c_smbus_write_byte(hx->client, HX852X_TS_SENSE_ON);
146 	if (error) {
147 		dev_err(dev, "failed to send TS_SENSE_ON: %d\n", error);
148 		return error;
149 	}
150 	msleep(20);
151 
152 	return 0;
153 }
154 
155 static int hx852x_stop(struct hx852x *hx)
156 {
157 	struct device *dev = &hx->client->dev;
158 	int error;
159 
160 	error = i2c_smbus_write_byte(hx->client, HX852X_TS_SENSE_OFF);
161 	if (error) {
162 		dev_err(dev, "failed to send TS_SENSE_OFF: %d\n", error);
163 		return error;
164 	}
165 	msleep(20);
166 
167 	error = i2c_smbus_write_byte(hx->client, HX852X_TS_SLEEP_IN);
168 	if (error) {
169 		dev_err(dev, "failed to send TS_SLEEP_IN: %d\n", error);
170 		return error;
171 	}
172 	msleep(30);
173 
174 	return 0;
175 }
176 
177 static int hx852x_power_off(struct hx852x *hx)
178 {
179 	struct device *dev = &hx->client->dev;
180 	int error;
181 
182 	error = regulator_bulk_disable(ARRAY_SIZE(hx->supplies), hx->supplies);
183 	if (error) {
184 		dev_err(dev, "failed to disable regulators: %d\n", error);
185 		return error;
186 	}
187 
188 	return 0;
189 }
190 
191 static int hx852x_read_config(struct hx852x *hx)
192 {
193 	struct device *dev = &hx->client->dev;
194 	struct hx852x_config conf;
195 	int x_res, y_res;
196 	int error, error2;
197 
198 	error = hx852x_power_on(hx);
199 	if (error)
200 		return error;
201 
202 	/* Sensing must be turned on briefly to load the config */
203 	error = hx852x_start(hx);
204 	if (error)
205 		goto err_power_off;
206 
207 	error = hx852x_stop(hx);
208 	if (error)
209 		goto err_power_off;
210 
211 	error = i2c_smbus_write_byte_data(hx->client, HX852X_REG_SRAM_SWITCH,
212 					  HX852X_SRAM_SWITCH_TEST_MODE);
213 	if (error)
214 		goto err_power_off;
215 
216 	error = i2c_smbus_write_word_data(hx->client, HX852X_REG_SRAM_ADDR,
217 					  HX852X_SRAM_ADDR_CONFIG);
218 	if (error)
219 		goto err_test_mode;
220 
221 	error = hx852x_i2c_read(hx, HX852X_REG_FLASH_RPLACE, &conf, sizeof(conf));
222 	if (error)
223 		goto err_test_mode;
224 
225 	x_res = be16_to_cpu(conf.x_res);
226 	y_res = be16_to_cpu(conf.y_res);
227 	hx->max_fingers = (conf.max_pt & 0xf0) >> 4;
228 	dev_dbg(dev, "x res: %u, y res: %u, max fingers: %u\n",
229 		x_res, y_res, hx->max_fingers);
230 
231 	if (hx->max_fingers > HX852X_MAX_FINGERS) {
232 		dev_err(dev, "max supported fingers: %u, found: %u\n",
233 			HX852X_MAX_FINGERS, hx->max_fingers);
234 		error = -EINVAL;
235 		goto err_test_mode;
236 	}
237 
238 	if (x_res && y_res) {
239 		input_set_abs_params(hx->input_dev, ABS_MT_POSITION_X, 0, x_res - 1, 0, 0);
240 		input_set_abs_params(hx->input_dev, ABS_MT_POSITION_Y, 0, y_res - 1, 0, 0);
241 	}
242 
243 err_test_mode:
244 	error2 = i2c_smbus_write_byte_data(hx->client, HX852X_REG_SRAM_SWITCH, 0);
245 	error = error ?: error2;
246 err_power_off:
247 	error2 = hx852x_power_off(hx);
248 	return error ?: error2;
249 }
250 
251 static int hx852x_handle_events(struct hx852x *hx)
252 {
253 	/*
254 	 * The event packets have variable size, depending on the amount of
255 	 * supported fingers (hx->max_fingers). They are laid out as follows:
256 	 *  - struct hx852x_coord[hx->max_fingers]: Coordinates for each finger
257 	 *  - u8[ALIGN(hx->max_fingers, 4)]: Touch width for each finger
258 	 *      with padding for 32-bit alignment
259 	 *  - struct hx852x_touch_info
260 	 *
261 	 * Load everything into a 32-bit aligned buffer so the coordinates
262 	 * can be assigned directly, without using get_unaligned_*().
263 	 */
264 	u8 buf[HX852X_MAX_BUF_SIZE] __aligned(4);
265 	struct hx852x_coord *coord = (struct hx852x_coord *)buf;
266 	u8 *width = &buf[HX852X_COORD_SIZE(hx->max_fingers)];
267 	struct hx852x_touch_info *info = (struct hx852x_touch_info *)
268 		&width[HX852X_WIDTH_SIZE(hx->max_fingers)];
269 	unsigned long finger_pressed, key_pressed;
270 	unsigned int i, x, y, w;
271 	int error;
272 
273 	error = hx852x_i2c_read(hx, HX852X_READ_ALL_EVENTS, buf,
274 				HX852X_BUF_SIZE(hx->max_fingers));
275 	if (error)
276 		return error;
277 
278 	finger_pressed = get_unaligned_le16(&info->finger_pressed);
279 	key_pressed = finger_pressed >> HX852X_MAX_FINGERS;
280 
281 	/* All bits are set when no touch is detected */
282 	if (info->finger_num == 0xff || !(info->finger_num & 0x0f))
283 		finger_pressed = 0;
284 	if (key_pressed == 0xf)
285 		key_pressed = 0;
286 
287 	for_each_set_bit(i, &finger_pressed, hx->max_fingers) {
288 		x = be16_to_cpu(coord[i].x);
289 		y = be16_to_cpu(coord[i].y);
290 		w = width[i];
291 
292 		input_mt_slot(hx->input_dev, i);
293 		input_mt_report_slot_state(hx->input_dev, MT_TOOL_FINGER, 1);
294 		touchscreen_report_pos(hx->input_dev, &hx->props, x, y, true);
295 		input_report_abs(hx->input_dev, ABS_MT_TOUCH_MAJOR, w);
296 	}
297 	input_mt_sync_frame(hx->input_dev);
298 
299 	for (i = 0; i < hx->keycount; i++)
300 		input_report_key(hx->input_dev, hx->keycodes[i], key_pressed & BIT(i));
301 
302 	input_sync(hx->input_dev);
303 	return 0;
304 }
305 
306 static irqreturn_t hx852x_interrupt(int irq, void *ptr)
307 {
308 	struct hx852x *hx = ptr;
309 	int error;
310 
311 	error = hx852x_handle_events(hx);
312 	if (error) {
313 		dev_err_ratelimited(&hx->client->dev,
314 				    "failed to handle events: %d\n", error);
315 		return IRQ_NONE;
316 	}
317 
318 	return IRQ_HANDLED;
319 }
320 
321 static int hx852x_input_open(struct input_dev *dev)
322 {
323 	struct hx852x *hx = input_get_drvdata(dev);
324 	int error;
325 
326 	error = hx852x_power_on(hx);
327 	if (error)
328 		return error;
329 
330 	error = hx852x_start(hx);
331 	if (error) {
332 		hx852x_power_off(hx);
333 		return error;
334 	}
335 
336 	enable_irq(hx->client->irq);
337 	return 0;
338 }
339 
340 static void hx852x_input_close(struct input_dev *dev)
341 {
342 	struct hx852x *hx = input_get_drvdata(dev);
343 
344 	hx852x_stop(hx);
345 	disable_irq(hx->client->irq);
346 	hx852x_power_off(hx);
347 }
348 
349 static int hx852x_parse_properties(struct hx852x *hx)
350 {
351 	struct device *dev = &hx->client->dev;
352 	int error, count;
353 
354 	count = device_property_count_u32(dev, "linux,keycodes");
355 	if (count == -EINVAL) {
356 		/* Property does not exist, keycodes are optional */
357 		return 0;
358 	} else if (count < 0) {
359 		dev_err(dev, "Failed to read linux,keycodes: %d\n", count);
360 		return count;
361 	} else if (count > HX852X_MAX_KEY_COUNT) {
362 		dev_err(dev, "max supported keys: %u, found: %u\n",
363 			HX852X_MAX_KEY_COUNT, hx->keycount);
364 		return -EINVAL;
365 	}
366 	hx->keycount = count;
367 
368 	error = device_property_read_u32_array(dev, "linux,keycodes",
369 					       hx->keycodes, hx->keycount);
370 	if (error) {
371 		dev_err(dev, "failed to read linux,keycodes: %d\n", error);
372 		return error;
373 	}
374 
375 	return 0;
376 }
377 
378 static int hx852x_probe(struct i2c_client *client)
379 {
380 	struct device *dev = &client->dev;
381 	struct hx852x *hx;
382 	int error, i;
383 
384 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
385 				     I2C_FUNC_SMBUS_WRITE_BYTE |
386 				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
387 				     I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
388 		dev_err(dev, "not all required i2c functionality supported\n");
389 		return -ENXIO;
390 	}
391 
392 	hx = devm_kzalloc(dev, sizeof(*hx), GFP_KERNEL);
393 	if (!hx)
394 		return -ENOMEM;
395 
396 	hx->client = client;
397 	hx->input_dev = devm_input_allocate_device(dev);
398 	if (!hx->input_dev)
399 		return -ENOMEM;
400 
401 	hx->input_dev->name = "Himax HX852x";
402 	hx->input_dev->id.bustype = BUS_I2C;
403 	hx->input_dev->open = hx852x_input_open;
404 	hx->input_dev->close = hx852x_input_close;
405 
406 	i2c_set_clientdata(client, hx);
407 	input_set_drvdata(hx->input_dev, hx);
408 
409 	hx->supplies[0].supply = "vcca";
410 	hx->supplies[1].supply = "vccd";
411 	error = devm_regulator_bulk_get(dev, ARRAY_SIZE(hx->supplies), hx->supplies);
412 	if (error)
413 		return dev_err_probe(dev, error, "failed to get regulators\n");
414 
415 	hx->reset_gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
416 	if (IS_ERR(hx->reset_gpiod))
417 		return dev_err_probe(dev, PTR_ERR(hx->reset_gpiod),
418 				     "failed to get reset gpio\n");
419 
420 	error = devm_request_threaded_irq(dev, client->irq, NULL, hx852x_interrupt,
421 					  IRQF_ONESHOT | IRQF_NO_AUTOEN, NULL, hx);
422 	if (error)
423 		return dev_err_probe(dev, error, "failed to request irq %d", client->irq);
424 
425 	error = hx852x_read_config(hx);
426 	if (error)
427 		return error;
428 
429 	input_set_capability(hx->input_dev, EV_ABS, ABS_MT_POSITION_X);
430 	input_set_capability(hx->input_dev, EV_ABS, ABS_MT_POSITION_Y);
431 	input_set_abs_params(hx->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
432 
433 	touchscreen_parse_properties(hx->input_dev, true, &hx->props);
434 	error = hx852x_parse_properties(hx);
435 	if (error)
436 		return error;
437 
438 	hx->input_dev->keycode = hx->keycodes;
439 	hx->input_dev->keycodemax = hx->keycount;
440 	hx->input_dev->keycodesize = sizeof(hx->keycodes[0]);
441 	for (i = 0; i < hx->keycount; i++)
442 		input_set_capability(hx->input_dev, EV_KEY, hx->keycodes[i]);
443 
444 	error = input_mt_init_slots(hx->input_dev, hx->max_fingers,
445 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
446 	if (error)
447 		return dev_err_probe(dev, error, "failed to init MT slots\n");
448 
449 	error = input_register_device(hx->input_dev);
450 	if (error)
451 		return dev_err_probe(dev, error, "failed to register input device\n");
452 
453 	return 0;
454 }
455 
456 static int hx852x_suspend(struct device *dev)
457 {
458 	struct hx852x *hx = dev_get_drvdata(dev);
459 
460 	guard(mutex)(&hx->input_dev->mutex);
461 
462 	if (input_device_enabled(hx->input_dev))
463 		return hx852x_stop(hx);
464 
465 	return 0;
466 }
467 
468 static int hx852x_resume(struct device *dev)
469 {
470 	struct hx852x *hx = dev_get_drvdata(dev);
471 
472 	guard(mutex)(&hx->input_dev->mutex);
473 
474 	if (input_device_enabled(hx->input_dev))
475 		return hx852x_start(hx);
476 
477 	return 0;
478 }
479 
480 static DEFINE_SIMPLE_DEV_PM_OPS(hx852x_pm_ops, hx852x_suspend, hx852x_resume);
481 
482 #ifdef CONFIG_OF
483 static const struct of_device_id hx852x_of_match[] = {
484 	{ .compatible = "himax,hx852es" },
485 	{ }
486 };
487 MODULE_DEVICE_TABLE(of, hx852x_of_match);
488 #endif
489 
490 static struct i2c_driver hx852x_driver = {
491 	.probe = hx852x_probe,
492 	.driver = {
493 		.name = "himax_hx852x",
494 		.pm = pm_sleep_ptr(&hx852x_pm_ops),
495 		.of_match_table = of_match_ptr(hx852x_of_match),
496 	},
497 };
498 module_i2c_driver(hx852x_driver);
499 
500 MODULE_DESCRIPTION("Himax HX852x(ES) Touchscreen Driver");
501 MODULE_AUTHOR("Jonathan Albrieux <jonathan.albrieux@gmail.com>");
502 MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>");
503 MODULE_LICENSE("GPL");
504