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
51struct 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
62struct 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
72struct hx852x_coord {
73 __be16 x;
74 __be16 y;
75} __packed __aligned(4);
76
77struct hx852x_touch_info {
78 u8 finger_num;
79 __le16 finger_pressed;
80 u8 padding;
81} __packed __aligned(4);
82
83static 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(adap: client->adapter, msgs: 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
114static 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), consumers: 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(desc: hx->reset_gpiod, value: 1);
126 msleep(msecs: 20);
127 gpiod_set_value_cansleep(desc: hx->reset_gpiod, value: 0);
128 msleep(msecs: 50);
129
130 return 0;
131}
132
133static int hx852x_start(struct hx852x *hx)
134{
135 struct device *dev = &hx->client->dev;
136 int error;
137
138 error = i2c_smbus_write_byte(client: 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(msecs: 30);
144
145 error = i2c_smbus_write_byte(client: 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(msecs: 20);
151
152 return 0;
153}
154
155static int hx852x_stop(struct hx852x *hx)
156{
157 struct device *dev = &hx->client->dev;
158 int error;
159
160 error = i2c_smbus_write_byte(client: 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(msecs: 20);
166
167 error = i2c_smbus_write_byte(client: 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(msecs: 30);
173
174 return 0;
175}
176
177static 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), consumers: 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
191static 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(client: 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(client: 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, data: &conf, len: 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(dev: hx->input_dev, ABS_MT_POSITION_X, min: 0, max: x_res - 1, fuzz: 0, flat: 0);
240 input_set_abs_params(dev: hx->input_dev, ABS_MT_POSITION_Y, min: 0, max: y_res - 1, fuzz: 0, flat: 0);
241 }
242
243err_test_mode:
244 error2 = i2c_smbus_write_byte_data(client: hx->client, HX852X_REG_SRAM_SWITCH, value: 0);
245 error = error ?: error2;
246err_power_off:
247 error2 = hx852x_power_off(hx);
248 return error ?: error2;
249}
250
251static 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, data: buf,
274 HX852X_BUF_SIZE(hx->max_fingers));
275 if (error)
276 return error;
277
278 finger_pressed = get_unaligned_le16(p: &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(dev: hx->input_dev, slot: i);
293 input_mt_report_slot_state(dev: hx->input_dev, MT_TOOL_FINGER, active: 1);
294 touchscreen_report_pos(input: hx->input_dev, prop: &hx->props, x, y, multitouch: true);
295 input_report_abs(dev: hx->input_dev, ABS_MT_TOUCH_MAJOR, value: w);
296 }
297 input_mt_sync_frame(dev: hx->input_dev);
298
299 for (i = 0; i < hx->keycount; i++)
300 input_report_key(dev: hx->input_dev, code: hx->keycodes[i], value: key_pressed & BIT(i));
301
302 input_sync(dev: hx->input_dev);
303 return 0;
304}
305
306static 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
321static 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(irq: hx->client->irq);
337 return 0;
338}
339
340static 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(irq: hx->client->irq);
346 hx852x_power_off(hx);
347}
348
349static 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, propname: "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, propname: "linux,keycodes",
369 val: hx->keycodes, nval: 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
378static 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(adap: 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, size: 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, data: hx);
407 input_set_drvdata(dev: hx->input_dev, data: 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), consumers: hx->supplies);
412 if (error)
413 return dev_err_probe(dev, err: error, fmt: "failed to get regulators\n");
414
415 hx->reset_gpiod = devm_gpiod_get(dev, con_id: "reset", flags: GPIOD_OUT_HIGH);
416 if (IS_ERR(ptr: hx->reset_gpiod))
417 return dev_err_probe(dev, err: PTR_ERR(ptr: hx->reset_gpiod),
418 fmt: "failed to get reset gpio\n");
419
420 error = devm_request_threaded_irq(dev, irq: client->irq, NULL, thread_fn: hx852x_interrupt,
421 IRQF_ONESHOT | IRQF_NO_AUTOEN, NULL, dev_id: hx);
422 if (error)
423 return dev_err_probe(dev, err: error, fmt: "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(dev: hx->input_dev, EV_ABS, ABS_MT_POSITION_X);
430 input_set_capability(dev: hx->input_dev, EV_ABS, ABS_MT_POSITION_Y);
431 input_set_abs_params(dev: hx->input_dev, ABS_MT_TOUCH_MAJOR, min: 0, max: 255, fuzz: 0, flat: 0);
432
433 touchscreen_parse_properties(input: hx->input_dev, multitouch: true, prop: &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(dev: hx->input_dev, EV_KEY, code: hx->keycodes[i]);
443
444 error = input_mt_init_slots(dev: hx->input_dev, num_slots: hx->max_fingers,
445 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
446 if (error)
447 return dev_err_probe(dev, err: error, fmt: "failed to init MT slots\n");
448
449 error = input_register_device(hx->input_dev);
450 if (error)
451 return dev_err_probe(dev, err: error, fmt: "failed to register input device\n");
452
453 return 0;
454}
455
456static int hx852x_suspend(struct device *dev)
457{
458 struct hx852x *hx = dev_get_drvdata(dev);
459
460 guard(mutex)(T: &hx->input_dev->mutex);
461
462 if (input_device_enabled(dev: hx->input_dev))
463 return hx852x_stop(hx);
464
465 return 0;
466}
467
468static int hx852x_resume(struct device *dev)
469{
470 struct hx852x *hx = dev_get_drvdata(dev);
471
472 guard(mutex)(T: &hx->input_dev->mutex);
473
474 if (input_device_enabled(dev: hx->input_dev))
475 return hx852x_start(hx);
476
477 return 0;
478}
479
480static DEFINE_SIMPLE_DEV_PM_OPS(hx852x_pm_ops, hx852x_suspend, hx852x_resume);
481
482#ifdef CONFIG_OF
483static const struct of_device_id hx852x_of_match[] = {
484 { .compatible = "himax,hx852es" },
485 { }
486};
487MODULE_DEVICE_TABLE(of, hx852x_of_match);
488#endif
489
490static 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};
498module_i2c_driver(hx852x_driver);
499
500MODULE_DESCRIPTION("Himax HX852x(ES) Touchscreen Driver");
501MODULE_AUTHOR("Jonathan Albrieux <jonathan.albrieux@gmail.com>");
502MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>");
503MODULE_LICENSE("GPL");
504

source code of linux/drivers/input/touchscreen/himax_hx852x.c