1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Himax hx83112b touchscreens
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 int himax_read_mcu(struct himax_ts_data *ts, u32 address, u32 *dst)
137 error = himax_bus_read(ts, address, dst, sizeof(dst));
144 static void himax_reset(struct himax_ts_data *ts)
146 gpiod_set_value_cansleep(ts->gpiod_rst, 1);
148 /* Delay copied from downstream driver */
150 gpiod_set_value_cansleep(ts->gpiod_rst, 0);
153 * The downstream driver doesn't contain this delay but is seems safer
154 * to include it. The range is just a guess that seems to work well.
156 usleep_range(1000, 1100);
159 static int himax_read_product_id(struct himax_ts_data *ts, u32 *product_id)
163 error = himax_read_mcu(ts, HIMAX_REG_ADDR_ICID, product_id);
171 static int himax_check_product_id(struct himax_ts_data *ts)
176 error = himax_read_product_id(ts, &product_id);
180 dev_dbg(&ts->client->dev, "Product id: %x\n", product_id);
182 if (product_id == ts->chip->id)
185 dev_err(&ts->client->dev, "Unknown product id: %x\n",
190 static int himax_input_register(struct himax_ts_data *ts)
194 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
195 if (!ts->input_dev) {
196 dev_err(&ts->client->dev, "Failed to allocate input device\n");
200 ts->input_dev->name = "Himax Touchscreen";
202 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
203 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
204 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
205 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 200, 0, 0);
207 touchscreen_parse_properties(ts->input_dev, true, &ts->props);
209 error = input_mt_init_slots(ts->input_dev, HIMAX_MAX_POINTS,
210 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
212 dev_err(&ts->client->dev,
213 "Failed to initialize MT slots: %d\n", error);
217 error = input_register_device(ts->input_dev);
219 dev_err(&ts->client->dev,
220 "Failed to register input device: %d\n", error);
227 static u8 himax_event_get_num_points(const struct himax_event *event)
229 if (event->num_points == 0xff)
232 return event->num_points & 0x0f;
235 static bool himax_process_event_point(struct himax_ts_data *ts,
236 const struct himax_event *event,
239 const struct himax_event_point *point = &event->points[point_index];
240 u16 x = be16_to_cpu(point->x);
241 u16 y = be16_to_cpu(point->y);
242 u8 w = event->majors[point_index];
244 if (x == HIMAX_INVALID_COORD || y == HIMAX_INVALID_COORD)
247 input_mt_slot(ts->input_dev, point_index);
248 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
249 touchscreen_report_pos(ts->input_dev, &ts->props, x, y, true);
250 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
251 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
255 static void himax_process_event(struct himax_ts_data *ts,
256 const struct himax_event *event)
259 int num_points_left = himax_event_get_num_points(event);
261 for (i = 0; i < HIMAX_MAX_POINTS && num_points_left > 0; i++) {
262 if (himax_process_event_point(ts, event, i))
266 input_mt_sync_frame(ts->input_dev);
267 input_sync(ts->input_dev);
270 static bool himax_verify_checksum(struct himax_ts_data *ts,
271 const struct himax_event *event)
273 u8 *data = (u8 *)event;
277 for (i = 0; i < sizeof(*event); i++)
280 if ((checksum & 0x00ff) != 0) {
281 dev_err(&ts->client->dev, "Wrong event checksum: %04x\n",
289 static int himax_read_events(struct himax_ts_data *ts,
290 struct himax_event *event, size_t length)
292 return regmap_raw_read(ts->regmap, HIMAX_AHB_ADDR_EVENT_STACK, event,
296 static int hx83100a_read_events(struct himax_ts_data *ts,
297 struct himax_event *event, size_t length)
299 return himax_bus_read(ts, HX83100A_REG_FW_EVENT_STACK, event, length);
302 static int himax_handle_input(struct himax_ts_data *ts)
305 struct himax_event event;
307 error = ts->chip->read_events(ts, &event, sizeof(event));
309 dev_err(&ts->client->dev, "Failed to read input event: %d\n",
315 * Only process the current event when it has a valid checksum but
316 * don't consider it a fatal error when it doesn't.
318 if (himax_verify_checksum(ts, &event))
319 himax_process_event(ts, &event);
324 static irqreturn_t himax_irq_handler(int irq, void *dev_id)
327 struct himax_ts_data *ts = dev_id;
329 error = himax_handle_input(ts);
336 static int himax_probe(struct i2c_client *client)
339 struct device *dev = &client->dev;
340 struct himax_ts_data *ts;
342 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
343 dev_err(dev, "I2C check functionality failed\n");
347 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
351 i2c_set_clientdata(client, ts);
353 ts->chip = i2c_get_match_data(client);
355 ts->regmap = devm_regmap_init_i2c(client, &himax_regmap_config);
356 error = PTR_ERR_OR_ZERO(ts->regmap);
358 dev_err(dev, "Failed to initialize regmap: %d\n", error);
362 ts->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
363 error = PTR_ERR_OR_ZERO(ts->gpiod_rst);
365 dev_err(dev, "Failed to get reset GPIO: %d\n", error);
371 if (ts->chip->check_id) {
372 error = himax_check_product_id(ts);
377 error = himax_input_register(ts);
381 error = devm_request_threaded_irq(dev, client->irq, NULL,
382 himax_irq_handler, IRQF_ONESHOT,
390 static int himax_suspend(struct device *dev)
392 struct himax_ts_data *ts = dev_get_drvdata(dev);
394 disable_irq(ts->client->irq);
398 static int himax_resume(struct device *dev)
400 struct himax_ts_data *ts = dev_get_drvdata(dev);
402 enable_irq(ts->client->irq);
406 static DEFINE_SIMPLE_DEV_PM_OPS(himax_pm_ops, himax_suspend, himax_resume);
408 static const struct himax_chip hx83100a_chip = {
409 .read_events = hx83100a_read_events,
412 static const struct himax_chip hx83112b_chip = {
414 .check_id = himax_check_product_id,
415 .read_events = himax_read_events,
418 static const struct i2c_device_id himax_ts_id[] = {
419 { "hx83100a", (kernel_ulong_t)&hx83100a_chip },
420 { "hx83112b", (kernel_ulong_t)&hx83112b_chip },
423 MODULE_DEVICE_TABLE(i2c, himax_ts_id);
426 static const struct of_device_id himax_of_match[] = {
427 { .compatible = "himax,hx83100a", .data = &hx83100a_chip },
428 { .compatible = "himax,hx83112b", .data = &hx83112b_chip },
431 MODULE_DEVICE_TABLE(of, himax_of_match);
434 static struct i2c_driver himax_ts_driver = {
435 .probe = himax_probe,
436 .id_table = himax_ts_id,
438 .name = "Himax-hx83112b-TS",
439 .of_match_table = of_match_ptr(himax_of_match),
440 .pm = pm_sleep_ptr(&himax_pm_ops),
443 module_i2c_driver(himax_ts_driver);
446 MODULE_DESCRIPTION("Himax hx83112b touchscreen driver");
447 MODULE_LICENSE("GPL");