1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for Himax hx83112b touchscreens 4 * 5 * Copyright (C) 2022 Job Noorman <job@noorman.info> 6 * 7 * HX83100A support 8 * Copyright (C) 2024 Felix Kaechele <felix@kaechele.ca> 9 * 10 * This code is based on "Himax Android Driver Sample Code for QCT platform": 11 * 12 * Copyright (C) 2017 Himax Corporation. 13 */ 14 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> 25 26 #define HIMAX_MAX_POINTS 10 27 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 34 35 #define HIMAX_AHB_CMD_ACCESS_DIRECTION_READ 0x00 36 #define HIMAX_AHB_CMD_INCR4 0x10 37 #define HIMAX_AHB_CMD_CONTI 0x31 38 39 #define HIMAX_REG_ADDR_ICID 0x900000d0 40 41 #define HX83100A_REG_FW_EVENT_STACK 0x90060000 42 43 #define HIMAX_INVALID_COORD 0xffff 44 45 struct himax_event_point { 46 __be16 x; 47 __be16 y; 48 } __packed; 49 50 struct himax_event { 51 struct himax_event_point points[HIMAX_MAX_POINTS]; 52 u8 majors[HIMAX_MAX_POINTS]; 53 u8 pad0[2]; 54 u8 num_points; 55 u8 pad1[2]; 56 u8 checksum_fix; 57 } __packed; 58 59 static_assert(sizeof(struct himax_event) == 56); 60 61 struct himax_ts_data; 62 struct himax_chip { 63 u32 id; 64 int (*check_id)(struct himax_ts_data *ts); 65 int (*read_events)(struct himax_ts_data *ts, struct himax_event *event, 66 size_t length); 67 }; 68 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; 76 }; 77 78 static const struct regmap_config himax_regmap_config = { 79 .reg_bits = 8, 80 .val_bits = 32, 81 .val_format_endian = REGMAP_ENDIAN_LITTLE, 82 }; 83 84 static int himax_bus_enable_burst(struct himax_ts_data *ts) 85 { 86 int error; 87 88 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_CONTI, 89 HIMAX_AHB_CMD_CONTI); 90 if (error) 91 return error; 92 93 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_INCR4, 94 HIMAX_AHB_CMD_INCR4); 95 if (error) 96 return error; 97 98 return 0; 99 } 100 101 static int himax_bus_read(struct himax_ts_data *ts, u32 address, void *dst, 102 size_t length) 103 { 104 int error; 105 106 if (length > 4) { 107 error = himax_bus_enable_burst(ts); 108 if (error) 109 return error; 110 } 111 112 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_BYTE_0, address); 113 if (error) 114 return error; 115 116 error = regmap_write(ts->regmap, HIMAX_AHB_ADDR_ACCESS_DIRECTION, 117 HIMAX_AHB_CMD_ACCESS_DIRECTION_READ); 118 if (error) 119 return error; 120 121 if (length > 4) 122 error = regmap_noinc_read(ts->regmap, HIMAX_AHB_ADDR_RDATA_BYTE_0, 123 dst, length); 124 else 125 error = regmap_read(ts->regmap, HIMAX_AHB_ADDR_RDATA_BYTE_0, 126 dst); 127 if (error) 128 return error; 129 130 return 0; 131 } 132 133 static void himax_reset(struct himax_ts_data *ts) 134 { 135 gpiod_set_value_cansleep(ts->gpiod_rst, 1); 136 137 /* Delay copied from downstream driver */ 138 msleep(20); 139 gpiod_set_value_cansleep(ts->gpiod_rst, 0); 140 141 /* 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. 144 */ 145 usleep_range(1000, 1100); 146 } 147 148 static int himax_read_product_id(struct himax_ts_data *ts, u32 *product_id) 149 { 150 int error; 151 152 error = himax_bus_read(ts, HIMAX_REG_ADDR_ICID, product_id, 153 sizeof(*product_id)); 154 if (error) 155 return error; 156 157 *product_id >>= 8; 158 return 0; 159 } 160 161 static int himax_check_product_id(struct himax_ts_data *ts) 162 { 163 int error; 164 u32 product_id; 165 166 error = himax_read_product_id(ts, &product_id); 167 if (error) 168 return error; 169 170 dev_dbg(&ts->client->dev, "Product id: %x\n", product_id); 171 172 if (product_id == ts->chip->id) 173 return 0; 174 175 dev_err(&ts->client->dev, "Unknown product id: %x\n", 176 product_id); 177 return -EINVAL; 178 } 179 180 static int himax_input_register(struct himax_ts_data *ts) 181 { 182 int error; 183 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"); 187 return -ENOMEM; 188 } 189 190 ts->input_dev->name = "Himax Touchscreen"; 191 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); 196 197 touchscreen_parse_properties(ts->input_dev, true, &ts->props); 198 199 error = input_mt_init_slots(ts->input_dev, HIMAX_MAX_POINTS, 200 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 201 if (error) { 202 dev_err(&ts->client->dev, 203 "Failed to initialize MT slots: %d\n", error); 204 return error; 205 } 206 207 error = input_register_device(ts->input_dev); 208 if (error) { 209 dev_err(&ts->client->dev, 210 "Failed to register input device: %d\n", error); 211 return error; 212 } 213 214 return 0; 215 } 216 217 static u8 himax_event_get_num_points(const struct himax_event *event) 218 { 219 if (event->num_points == 0xff) 220 return 0; 221 else 222 return event->num_points & 0x0f; 223 } 224 225 static bool himax_process_event_point(struct himax_ts_data *ts, 226 const struct himax_event *event, 227 int point_index) 228 { 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]; 233 234 if (x == HIMAX_INVALID_COORD || y == HIMAX_INVALID_COORD) 235 return false; 236 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); 242 return true; 243 } 244 245 static void himax_process_event(struct himax_ts_data *ts, 246 const struct himax_event *event) 247 { 248 int i; 249 int num_points_left = himax_event_get_num_points(event); 250 251 for (i = 0; i < HIMAX_MAX_POINTS && num_points_left > 0; i++) { 252 if (himax_process_event_point(ts, event, i)) 253 num_points_left--; 254 } 255 256 input_mt_sync_frame(ts->input_dev); 257 input_sync(ts->input_dev); 258 } 259 260 static bool himax_verify_checksum(struct himax_ts_data *ts, 261 const struct himax_event *event) 262 { 263 u8 *data = (u8 *)event; 264 int i; 265 u16 checksum = 0; 266 267 for (i = 0; i < sizeof(*event); i++) 268 checksum += data[i]; 269 270 if ((checksum & 0x00ff) != 0) { 271 dev_err(&ts->client->dev, "Wrong event checksum: %04x\n", 272 checksum); 273 return false; 274 } 275 276 return true; 277 } 278 279 static int himax_read_events(struct himax_ts_data *ts, 280 struct himax_event *event, size_t length) 281 { 282 return regmap_raw_read(ts->regmap, HIMAX_AHB_ADDR_EVENT_STACK, event, 283 length); 284 } 285 286 static int hx83100a_read_events(struct himax_ts_data *ts, 287 struct himax_event *event, size_t length) 288 { 289 return himax_bus_read(ts, HX83100A_REG_FW_EVENT_STACK, event, length); 290 }; 291 292 static int himax_handle_input(struct himax_ts_data *ts) 293 { 294 int error; 295 struct himax_event event; 296 297 error = ts->chip->read_events(ts, &event, sizeof(event)); 298 if (error) { 299 dev_err(&ts->client->dev, "Failed to read input event: %d\n", 300 error); 301 return error; 302 } 303 304 /* 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. 307 */ 308 if (himax_verify_checksum(ts, &event)) 309 himax_process_event(ts, &event); 310 311 return 0; 312 } 313 314 static irqreturn_t himax_irq_handler(int irq, void *dev_id) 315 { 316 int error; 317 struct himax_ts_data *ts = dev_id; 318 319 error = himax_handle_input(ts); 320 if (error) 321 return IRQ_NONE; 322 323 return IRQ_HANDLED; 324 } 325 326 static int himax_probe(struct i2c_client *client) 327 { 328 int error; 329 struct device *dev = &client->dev; 330 struct himax_ts_data *ts; 331 332 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 333 dev_err(dev, "I2C check functionality failed\n"); 334 return -ENXIO; 335 } 336 337 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 338 if (!ts) 339 return -ENOMEM; 340 341 i2c_set_clientdata(client, ts); 342 ts->client = client; 343 ts->chip = i2c_get_match_data(client); 344 345 ts->regmap = devm_regmap_init_i2c(client, &himax_regmap_config); 346 error = PTR_ERR_OR_ZERO(ts->regmap); 347 if (error) { 348 dev_err(dev, "Failed to initialize regmap: %d\n", error); 349 return error; 350 } 351 352 ts->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 353 error = PTR_ERR_OR_ZERO(ts->gpiod_rst); 354 if (error) { 355 dev_err(dev, "Failed to get reset GPIO: %d\n", error); 356 return error; 357 } 358 359 himax_reset(ts); 360 361 if (ts->chip->check_id) { 362 error = himax_check_product_id(ts); 363 if (error) 364 return error; 365 } 366 367 error = himax_input_register(ts); 368 if (error) 369 return error; 370 371 error = devm_request_threaded_irq(dev, client->irq, NULL, 372 himax_irq_handler, IRQF_ONESHOT, 373 client->name, ts); 374 if (error) 375 return error; 376 377 return 0; 378 } 379 380 static int himax_suspend(struct device *dev) 381 { 382 struct himax_ts_data *ts = dev_get_drvdata(dev); 383 384 disable_irq(ts->client->irq); 385 return 0; 386 } 387 388 static int himax_resume(struct device *dev) 389 { 390 struct himax_ts_data *ts = dev_get_drvdata(dev); 391 392 enable_irq(ts->client->irq); 393 return 0; 394 } 395 396 static DEFINE_SIMPLE_DEV_PM_OPS(himax_pm_ops, himax_suspend, himax_resume); 397 398 static const struct himax_chip hx83100a_chip = { 399 .read_events = hx83100a_read_events, 400 }; 401 402 static const struct himax_chip hx83112b_chip = { 403 .id = 0x83112b, 404 .check_id = himax_check_product_id, 405 .read_events = himax_read_events, 406 }; 407 408 static const struct i2c_device_id himax_ts_id[] = { 409 { "hx83100a", (kernel_ulong_t)&hx83100a_chip }, 410 { "hx83112b", (kernel_ulong_t)&hx83112b_chip }, 411 { /* sentinel */ } 412 }; 413 MODULE_DEVICE_TABLE(i2c, himax_ts_id); 414 415 #ifdef CONFIG_OF 416 static const struct of_device_id himax_of_match[] = { 417 { .compatible = "himax,hx83100a", .data = &hx83100a_chip }, 418 { .compatible = "himax,hx83112b", .data = &hx83112b_chip }, 419 { /* sentinel */ } 420 }; 421 MODULE_DEVICE_TABLE(of, himax_of_match); 422 #endif 423 424 static struct i2c_driver himax_ts_driver = { 425 .probe = himax_probe, 426 .id_table = himax_ts_id, 427 .driver = { 428 .name = "Himax-hx83112b-TS", 429 .of_match_table = of_match_ptr(himax_of_match), 430 .pm = pm_sleep_ptr(&himax_pm_ops), 431 }, 432 }; 433 module_i2c_driver(himax_ts_driver); 434 435 MODULE_AUTHOR("Job Noorman <job@noorman.info>"); 436 MODULE_DESCRIPTION("Himax hx83112b touchscreen driver"); 437 MODULE_LICENSE("GPL"); 438