1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Himax hx83112b touchscreens
5 * Copyright (C) 2022 Job Noorman <job@noorman.info>
8 * Copyright (C) 2024 Felix Kaechele <felix@kaechele.ca>
10 * This code is based on "Himax Android Driver Sample Code for QCT platform":
12 * Copyright (C) 2017 Himax Corporation.
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/gpio/consumer.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/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/regmap.h>
26 #define HIMAX_MAX_POINTS 10
28 #define HIMAX_AHB_ADDR_BYTE_0 0x00
29 #define HIMAX_AHB_ADDR_RDATA_BYTE_0 0x08
30 #define HIMAX_AHB_ADDR_ACCESS_DIRECTION 0x0c
31 #define HIMAX_AHB_ADDR_INCR4 0x0d
32 #define HIMAX_AHB_ADDR_CONTI 0x13
33 #define HIMAX_AHB_ADDR_EVENT_STACK 0x30
35 #define HIMAX_AHB_CMD_ACCESS_DIRECTION_READ 0x00
36 #define HIMAX_AHB_CMD_INCR4 0x10
37 #define HIMAX_AHB_CMD_CONTI 0x31
39 #define HIMAX_REG_ADDR_ICID 0x900000d0
41 #define HX83100A_REG_FW_EVENT_STACK 0x90060000
43 #define HIMAX_INVALID_COORD 0xffff
45 struct himax_event_point
{
51 struct himax_event_point points
[HIMAX_MAX_POINTS
];
52 u8 majors
[HIMAX_MAX_POINTS
];
59 static_assert(sizeof(struct himax_event
) == 56);
64 int (*check_id
)(struct himax_ts_data
*ts
);
65 int (*read_events
)(struct himax_ts_data
*ts
, struct himax_event
*event
,
69 struct himax_ts_data
{
70 const struct himax_chip
*chip
;
71 struct gpio_desc
*gpiod_rst
;
72 struct input_dev
*input_dev
;
73 struct i2c_client
*client
;
74 struct regmap
*regmap
;
75 struct touchscreen_properties props
;
78 static const struct regmap_config himax_regmap_config
= {
81 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
84 static int himax_bus_enable_burst(struct himax_ts_data
*ts
)
88 error
= regmap_write(ts
->regmap
, HIMAX_AHB_ADDR_CONTI
,
93 error
= regmap_write(ts
->regmap
, HIMAX_AHB_ADDR_INCR4
,
101 static int himax_bus_read(struct himax_ts_data
*ts
, u32 address
, void *dst
,
107 error
= himax_bus_enable_burst(ts
);
112 error
= regmap_write(ts
->regmap
, HIMAX_AHB_ADDR_BYTE_0
, address
);
116 error
= regmap_write(ts
->regmap
, HIMAX_AHB_ADDR_ACCESS_DIRECTION
,
117 HIMAX_AHB_CMD_ACCESS_DIRECTION_READ
);
122 error
= regmap_noinc_read(ts
->regmap
, HIMAX_AHB_ADDR_RDATA_BYTE_0
,
125 error
= regmap_read(ts
->regmap
, HIMAX_AHB_ADDR_RDATA_BYTE_0
,
133 static void himax_reset(struct himax_ts_data
*ts
)
135 gpiod_set_value_cansleep(ts
->gpiod_rst
, 1);
137 /* Delay copied from downstream driver */
139 gpiod_set_value_cansleep(ts
->gpiod_rst
, 0);
142 * The downstream driver doesn't contain this delay but is seems safer
143 * to include it. The range is just a guess that seems to work well.
145 usleep_range(1000, 1100);
148 static int himax_read_product_id(struct himax_ts_data
*ts
, u32
*product_id
)
152 error
= himax_bus_read(ts
, HIMAX_REG_ADDR_ICID
, product_id
,
153 sizeof(*product_id
));
161 static int himax_check_product_id(struct himax_ts_data
*ts
)
166 error
= himax_read_product_id(ts
, &product_id
);
170 dev_dbg(&ts
->client
->dev
, "Product id: %x\n", product_id
);
172 if (product_id
== ts
->chip
->id
)
175 dev_err(&ts
->client
->dev
, "Unknown product id: %x\n",
180 static int himax_input_register(struct himax_ts_data
*ts
)
184 ts
->input_dev
= devm_input_allocate_device(&ts
->client
->dev
);
185 if (!ts
->input_dev
) {
186 dev_err(&ts
->client
->dev
, "Failed to allocate input device\n");
190 ts
->input_dev
->name
= "Himax Touchscreen";
192 input_set_capability(ts
->input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
193 input_set_capability(ts
->input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
194 input_set_abs_params(ts
->input_dev
, ABS_MT_WIDTH_MAJOR
, 0, 200, 0, 0);
195 input_set_abs_params(ts
->input_dev
, ABS_MT_TOUCH_MAJOR
, 0, 200, 0, 0);
197 touchscreen_parse_properties(ts
->input_dev
, true, &ts
->props
);
199 error
= input_mt_init_slots(ts
->input_dev
, HIMAX_MAX_POINTS
,
200 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
202 dev_err(&ts
->client
->dev
,
203 "Failed to initialize MT slots: %d\n", error
);
207 error
= input_register_device(ts
->input_dev
);
209 dev_err(&ts
->client
->dev
,
210 "Failed to register input device: %d\n", error
);
217 static u8
himax_event_get_num_points(const struct himax_event
*event
)
219 if (event
->num_points
== 0xff)
222 return event
->num_points
& 0x0f;
225 static bool himax_process_event_point(struct himax_ts_data
*ts
,
226 const struct himax_event
*event
,
229 const struct himax_event_point
*point
= &event
->points
[point_index
];
230 u16 x
= be16_to_cpu(point
->x
);
231 u16 y
= be16_to_cpu(point
->y
);
232 u8 w
= event
->majors
[point_index
];
234 if (x
== HIMAX_INVALID_COORD
|| y
== HIMAX_INVALID_COORD
)
237 input_mt_slot(ts
->input_dev
, point_index
);
238 input_mt_report_slot_state(ts
->input_dev
, MT_TOOL_FINGER
, true);
239 touchscreen_report_pos(ts
->input_dev
, &ts
->props
, x
, y
, true);
240 input_report_abs(ts
->input_dev
, ABS_MT_TOUCH_MAJOR
, w
);
241 input_report_abs(ts
->input_dev
, ABS_MT_WIDTH_MAJOR
, w
);
245 static void himax_process_event(struct himax_ts_data
*ts
,
246 const struct himax_event
*event
)
249 int num_points_left
= himax_event_get_num_points(event
);
251 for (i
= 0; i
< HIMAX_MAX_POINTS
&& num_points_left
> 0; i
++) {
252 if (himax_process_event_point(ts
, event
, i
))
256 input_mt_sync_frame(ts
->input_dev
);
257 input_sync(ts
->input_dev
);
260 static bool himax_verify_checksum(struct himax_ts_data
*ts
,
261 const struct himax_event
*event
)
263 u8
*data
= (u8
*)event
;
267 for (i
= 0; i
< sizeof(*event
); i
++)
270 if ((checksum
& 0x00ff) != 0) {
271 dev_err(&ts
->client
->dev
, "Wrong event checksum: %04x\n",
279 static int himax_read_events(struct himax_ts_data
*ts
,
280 struct himax_event
*event
, size_t length
)
282 return regmap_raw_read(ts
->regmap
, HIMAX_AHB_ADDR_EVENT_STACK
, event
,
286 static int hx83100a_read_events(struct himax_ts_data
*ts
,
287 struct himax_event
*event
, size_t length
)
289 return himax_bus_read(ts
, HX83100A_REG_FW_EVENT_STACK
, event
, length
);
292 static int himax_handle_input(struct himax_ts_data
*ts
)
295 struct himax_event event
;
297 error
= ts
->chip
->read_events(ts
, &event
, sizeof(event
));
299 dev_err(&ts
->client
->dev
, "Failed to read input event: %d\n",
305 * Only process the current event when it has a valid checksum but
306 * don't consider it a fatal error when it doesn't.
308 if (himax_verify_checksum(ts
, &event
))
309 himax_process_event(ts
, &event
);
314 static irqreturn_t
himax_irq_handler(int irq
, void *dev_id
)
317 struct himax_ts_data
*ts
= dev_id
;
319 error
= himax_handle_input(ts
);
326 static int himax_probe(struct i2c_client
*client
)
329 struct device
*dev
= &client
->dev
;
330 struct himax_ts_data
*ts
;
332 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
333 dev_err(dev
, "I2C check functionality failed\n");
337 ts
= devm_kzalloc(dev
, sizeof(*ts
), GFP_KERNEL
);
341 i2c_set_clientdata(client
, ts
);
343 ts
->chip
= i2c_get_match_data(client
);
345 ts
->regmap
= devm_regmap_init_i2c(client
, &himax_regmap_config
);
346 error
= PTR_ERR_OR_ZERO(ts
->regmap
);
348 dev_err(dev
, "Failed to initialize regmap: %d\n", error
);
352 ts
->gpiod_rst
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
353 error
= PTR_ERR_OR_ZERO(ts
->gpiod_rst
);
355 dev_err(dev
, "Failed to get reset GPIO: %d\n", error
);
361 if (ts
->chip
->check_id
) {
362 error
= himax_check_product_id(ts
);
367 error
= himax_input_register(ts
);
371 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
372 himax_irq_handler
, IRQF_ONESHOT
,
380 static int himax_suspend(struct device
*dev
)
382 struct himax_ts_data
*ts
= dev_get_drvdata(dev
);
384 disable_irq(ts
->client
->irq
);
388 static int himax_resume(struct device
*dev
)
390 struct himax_ts_data
*ts
= dev_get_drvdata(dev
);
392 enable_irq(ts
->client
->irq
);
396 static DEFINE_SIMPLE_DEV_PM_OPS(himax_pm_ops
, himax_suspend
, himax_resume
);
398 static const struct himax_chip hx83100a_chip
= {
399 .read_events
= hx83100a_read_events
,
402 static const struct himax_chip hx83112b_chip
= {
404 .check_id
= himax_check_product_id
,
405 .read_events
= himax_read_events
,
408 static const struct i2c_device_id himax_ts_id
[] = {
409 { "hx83100a", (kernel_ulong_t
)&hx83100a_chip
},
410 { "hx83112b", (kernel_ulong_t
)&hx83112b_chip
},
413 MODULE_DEVICE_TABLE(i2c
, himax_ts_id
);
416 static const struct of_device_id himax_of_match
[] = {
417 { .compatible
= "himax,hx83100a", .data
= &hx83100a_chip
},
418 { .compatible
= "himax,hx83112b", .data
= &hx83112b_chip
},
421 MODULE_DEVICE_TABLE(of
, himax_of_match
);
424 static struct i2c_driver himax_ts_driver
= {
425 .probe
= himax_probe
,
426 .id_table
= himax_ts_id
,
428 .name
= "Himax-hx83112b-TS",
429 .of_match_table
= of_match_ptr(himax_of_match
),
430 .pm
= pm_sleep_ptr(&himax_pm_ops
),
433 module_i2c_driver(himax_ts_driver
);
435 MODULE_AUTHOR("Job Noorman <job@noorman.info>");
436 MODULE_DESCRIPTION("Himax hx83112b touchscreen driver");
437 MODULE_LICENSE("GPL");