1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Himax hx83112b touchscreens
5 * Copyright (C) 2022 Job Noorman <job@noorman.info>
7 * This code is based on "Himax Android Driver Sample Code for QCT platform":
9 * Copyright (C) 2017 Himax Corporation.
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/input/mt.h>
18 #include <linux/input/touchscreen.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/regmap.h>
23 #define HIMAX_ID_83112B 0x83112b
25 #define HIMAX_MAX_POINTS 10
27 #define HIMAX_REG_CFG_SET_ADDR 0x00
28 #define HIMAX_REG_CFG_INIT_READ 0x0c
29 #define HIMAX_REG_CFG_READ_VALUE 0x08
30 #define HIMAX_REG_READ_EVENT 0x30
32 #define HIMAX_CFG_PRODUCT_ID 0x900000d0
34 #define HIMAX_INVALID_COORD 0xffff
36 struct himax_event_point {
42 struct himax_event_point points[HIMAX_MAX_POINTS];
43 u8 majors[HIMAX_MAX_POINTS];
50 static_assert(sizeof(struct himax_event) == 56);
52 struct himax_ts_data {
53 struct gpio_desc *gpiod_rst;
54 struct input_dev *input_dev;
55 struct i2c_client *client;
56 struct regmap *regmap;
57 struct touchscreen_properties props;
60 static const struct regmap_config himax_regmap_config = {
63 .val_format_endian = REGMAP_ENDIAN_LITTLE,
66 static int himax_read_config(struct himax_ts_data *ts, u32 address, u32 *dst)
70 error = regmap_write(ts->regmap, HIMAX_REG_CFG_SET_ADDR, address);
74 error = regmap_write(ts->regmap, HIMAX_REG_CFG_INIT_READ, 0x0);
78 error = regmap_read(ts->regmap, HIMAX_REG_CFG_READ_VALUE, dst);
85 static void himax_reset(struct himax_ts_data *ts)
87 gpiod_set_value_cansleep(ts->gpiod_rst, 1);
89 /* Delay copied from downstream driver */
91 gpiod_set_value_cansleep(ts->gpiod_rst, 0);
94 * The downstream driver doesn't contain this delay but is seems safer
95 * to include it. The range is just a guess that seems to work well.
97 usleep_range(1000, 1100);
100 static int himax_read_product_id(struct himax_ts_data *ts, u32 *product_id)
104 error = himax_read_config(ts, HIMAX_CFG_PRODUCT_ID, product_id);
112 static int himax_check_product_id(struct himax_ts_data *ts)
117 error = himax_read_product_id(ts, &product_id);
121 dev_dbg(&ts->client->dev, "Product id: %x\n", product_id);
123 switch (product_id) {
124 case HIMAX_ID_83112B:
128 dev_err(&ts->client->dev,
129 "Unknown product id: %x\n", product_id);
134 static int himax_input_register(struct himax_ts_data *ts)
138 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
139 if (!ts->input_dev) {
140 dev_err(&ts->client->dev, "Failed to allocate input device\n");
144 ts->input_dev->name = "Himax Touchscreen";
146 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
147 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
148 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 200, 0, 0);
149 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 200, 0, 0);
151 touchscreen_parse_properties(ts->input_dev, true, &ts->props);
153 error = input_mt_init_slots(ts->input_dev, HIMAX_MAX_POINTS,
154 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
156 dev_err(&ts->client->dev,
157 "Failed to initialize MT slots: %d\n", error);
161 error = input_register_device(ts->input_dev);
163 dev_err(&ts->client->dev,
164 "Failed to register input device: %d\n", error);
171 static u8 himax_event_get_num_points(const struct himax_event *event)
173 if (event->num_points == 0xff)
176 return event->num_points & 0x0f;
179 static bool himax_process_event_point(struct himax_ts_data *ts,
180 const struct himax_event *event,
183 const struct himax_event_point *point = &event->points[point_index];
184 u16 x = be16_to_cpu(point->x);
185 u16 y = be16_to_cpu(point->y);
186 u8 w = event->majors[point_index];
188 if (x == HIMAX_INVALID_COORD || y == HIMAX_INVALID_COORD)
191 input_mt_slot(ts->input_dev, point_index);
192 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
193 touchscreen_report_pos(ts->input_dev, &ts->props, x, y, true);
194 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, w);
195 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, w);
199 static void himax_process_event(struct himax_ts_data *ts,
200 const struct himax_event *event)
203 int num_points_left = himax_event_get_num_points(event);
205 for (i = 0; i < HIMAX_MAX_POINTS && num_points_left > 0; i++) {
206 if (himax_process_event_point(ts, event, i))
210 input_mt_sync_frame(ts->input_dev);
211 input_sync(ts->input_dev);
214 static bool himax_verify_checksum(struct himax_ts_data *ts,
215 const struct himax_event *event)
217 u8 *data = (u8 *)event;
221 for (i = 0; i < sizeof(*event); i++)
224 if ((checksum & 0x00ff) != 0) {
225 dev_err(&ts->client->dev, "Wrong event checksum: %04x\n",
233 static int himax_handle_input(struct himax_ts_data *ts)
236 struct himax_event event;
238 error = regmap_raw_read(ts->regmap, HIMAX_REG_READ_EVENT, &event,
241 dev_err(&ts->client->dev, "Failed to read input event: %d\n",
247 * Only process the current event when it has a valid checksum but
248 * don't consider it a fatal error when it doesn't.
250 if (himax_verify_checksum(ts, &event))
251 himax_process_event(ts, &event);
256 static irqreturn_t himax_irq_handler(int irq, void *dev_id)
259 struct himax_ts_data *ts = dev_id;
261 error = himax_handle_input(ts);
268 static int himax_probe(struct i2c_client *client)
271 struct device *dev = &client->dev;
272 struct himax_ts_data *ts;
274 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
275 dev_err(dev, "I2C check functionality failed\n");
279 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
283 i2c_set_clientdata(client, ts);
286 ts->regmap = devm_regmap_init_i2c(client, &himax_regmap_config);
287 error = PTR_ERR_OR_ZERO(ts->regmap);
289 dev_err(dev, "Failed to initialize regmap: %d\n", error);
293 ts->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
294 error = PTR_ERR_OR_ZERO(ts->gpiod_rst);
296 dev_err(dev, "Failed to get reset GPIO: %d\n", error);
302 error = himax_check_product_id(ts);
306 error = himax_input_register(ts);
310 error = devm_request_threaded_irq(dev, client->irq, NULL,
311 himax_irq_handler, IRQF_ONESHOT,
319 static int himax_suspend(struct device *dev)
321 struct himax_ts_data *ts = dev_get_drvdata(dev);
323 disable_irq(ts->client->irq);
327 static int himax_resume(struct device *dev)
329 struct himax_ts_data *ts = dev_get_drvdata(dev);
331 enable_irq(ts->client->irq);
335 static DEFINE_SIMPLE_DEV_PM_OPS(himax_pm_ops, himax_suspend, himax_resume);
337 static const struct i2c_device_id himax_ts_id[] = {
341 MODULE_DEVICE_TABLE(i2c, himax_ts_id);
344 static const struct of_device_id himax_of_match[] = {
345 { .compatible = "himax,hx83112b" },
348 MODULE_DEVICE_TABLE(of, himax_of_match);
351 static struct i2c_driver himax_ts_driver = {
352 .probe = himax_probe,
353 .id_table = himax_ts_id,
355 .name = "Himax-hx83112b-TS",
356 .of_match_table = of_match_ptr(himax_of_match),
357 .pm = pm_sleep_ptr(&himax_pm_ops),
360 module_i2c_driver(himax_ts_driver);
362 MODULE_AUTHOR("Job Noorman <job@noorman.info>");
363 MODULE_DESCRIPTION("Himax hx83112b touchscreen driver");
364 MODULE_LICENSE("GPL");