1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for Hynitron cstxxx Touchscreen 4 * 5 * Copyright (c) 2022 Chris Morgan <macromorgan@hotmail.com> 6 * 7 * This code is based on hynitron_core.c authored by Hynitron. 8 * Note that no datasheet was available, so much of these registers 9 * are undocumented. This is essentially a cleaned-up version of the 10 * vendor driver with support removed for hardware I cannot test and 11 * device-specific functions replated with generic functions wherever 12 * possible. 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/module.h> 23 #include <linux/property.h> 24 #include <linux/unaligned.h> 25 26 /* Per chip data */ 27 struct hynitron_ts_chip_data { 28 unsigned int max_touch_num; 29 u32 ic_chkcode; 30 int (*firmware_info)(struct i2c_client *client); 31 int (*bootloader_enter)(struct i2c_client *client); 32 int (*init_input)(struct i2c_client *client); 33 void (*report_touch)(struct i2c_client *client); 34 }; 35 36 /* Data generic to all (supported and non-supported) controllers. */ 37 struct hynitron_ts_data { 38 const struct hynitron_ts_chip_data *chip; 39 struct i2c_client *client; 40 struct input_dev *input_dev; 41 struct touchscreen_properties prop; 42 struct gpio_desc *reset_gpio; 43 }; 44 45 /* 46 * Since I have no datasheet, these values are guessed and/or assumed 47 * based on observation and testing. 48 */ 49 #define CST3XX_FIRMWARE_INFO_START_CMD 0x01d1 50 #define CST3XX_FIRMWARE_INFO_END_CMD 0x09d1 51 #define CST3XX_FIRMWARE_CHK_CODE_REG 0xfcd1 52 #define CST3XX_FIRMWARE_VERSION_REG 0x08d2 53 #define CST3XX_FIRMWARE_VER_INVALID_VAL 0xa5a5a5a5 54 55 #define CST3XX_BOOTLDR_PROG_CMD 0xaa01a0 56 #define CST3XX_BOOTLDR_PROG_CHK_REG 0x02a0 57 #define CST3XX_BOOTLDR_CHK_VAL 0xac 58 59 #define CST3XX_TOUCH_DATA_PART_REG 0x00d0 60 #define CST3XX_TOUCH_DATA_FULL_REG 0x07d0 61 #define CST3XX_TOUCH_DATA_CHK_VAL 0xab 62 #define CST3XX_TOUCH_DATA_TOUCH_VAL 0x03 63 #define CST3XX_TOUCH_DATA_STOP_CMD 0xab00d0 64 #define CST3XX_TOUCH_COUNT_MASK GENMASK(6, 0) 65 66 67 /* 68 * Hard coded reset delay value of 20ms not IC dependent in 69 * vendor driver. 70 */ 71 static void hyn_reset_proc(struct i2c_client *client, int delay) 72 { 73 struct hynitron_ts_data *ts_data = i2c_get_clientdata(client); 74 75 gpiod_set_value_cansleep(ts_data->reset_gpio, 1); 76 msleep(20); 77 gpiod_set_value_cansleep(ts_data->reset_gpio, 0); 78 if (delay) 79 fsleep(delay * 1000); 80 } 81 82 static irqreturn_t hyn_interrupt_handler(int irq, void *dev_id) 83 { 84 struct i2c_client *client = dev_id; 85 struct hynitron_ts_data *ts_data = i2c_get_clientdata(client); 86 87 ts_data->chip->report_touch(client); 88 89 return IRQ_HANDLED; 90 } 91 92 /* 93 * The vendor driver would retry twice before failing to read or write 94 * to the i2c device. 95 */ 96 97 static int cst3xx_i2c_write(struct i2c_client *client, 98 unsigned char *buf, int len) 99 { 100 int ret; 101 int retries = 0; 102 103 while (retries < 2) { 104 ret = i2c_master_send(client, buf, len); 105 if (ret == len) 106 return 0; 107 if (ret <= 0) 108 retries++; 109 else 110 break; 111 } 112 113 return ret < 0 ? ret : -EIO; 114 } 115 116 static int cst3xx_i2c_read_register(struct i2c_client *client, u16 reg, 117 u8 *val, u16 len) 118 { 119 __le16 buf = cpu_to_le16(reg); 120 struct i2c_msg msgs[] = { 121 { 122 .addr = client->addr, 123 .flags = 0, 124 .len = 2, 125 .buf = (u8 *)&buf, 126 }, 127 { 128 .addr = client->addr, 129 .flags = I2C_M_RD, 130 .len = len, 131 .buf = val, 132 } 133 }; 134 int err; 135 int ret; 136 137 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 138 if (ret == ARRAY_SIZE(msgs)) 139 return 0; 140 141 err = ret < 0 ? ret : -EIO; 142 dev_err(&client->dev, "Error reading %d bytes from 0x%04x: %d (%d)\n", 143 len, reg, err, ret); 144 145 return err; 146 } 147 148 static int cst3xx_firmware_info(struct i2c_client *client) 149 { 150 struct hynitron_ts_data *ts_data = i2c_get_clientdata(client); 151 int err; 152 u32 tmp; 153 unsigned char buf[4]; 154 155 /* 156 * Tests suggest this command needed to read firmware regs. 157 */ 158 put_unaligned_le16(CST3XX_FIRMWARE_INFO_START_CMD, buf); 159 err = cst3xx_i2c_write(client, buf, 2); 160 if (err) 161 return err; 162 163 usleep_range(10000, 11000); 164 165 /* 166 * Read register for check-code to determine if device detected 167 * correctly. 168 */ 169 err = cst3xx_i2c_read_register(client, CST3XX_FIRMWARE_CHK_CODE_REG, 170 buf, 4); 171 if (err) 172 return err; 173 174 tmp = get_unaligned_le32(buf); 175 if ((tmp & 0xffff0000) != ts_data->chip->ic_chkcode) { 176 dev_err(&client->dev, "%s ic mismatch, chkcode is %u\n", 177 __func__, tmp); 178 return -ENODEV; 179 } 180 181 usleep_range(10000, 11000); 182 183 /* Read firmware version and test if firmware missing. */ 184 err = cst3xx_i2c_read_register(client, CST3XX_FIRMWARE_VERSION_REG, 185 buf, 4); 186 if (err) 187 return err; 188 189 tmp = get_unaligned_le32(buf); 190 if (tmp == CST3XX_FIRMWARE_VER_INVALID_VAL) { 191 dev_err(&client->dev, "Device firmware missing\n"); 192 return -ENODEV; 193 } 194 195 /* 196 * Tests suggest cmd required to exit reading firmware regs. 197 */ 198 put_unaligned_le16(CST3XX_FIRMWARE_INFO_END_CMD, buf); 199 err = cst3xx_i2c_write(client, buf, 2); 200 if (err) 201 return err; 202 203 usleep_range(5000, 6000); 204 205 return 0; 206 } 207 208 static int cst3xx_bootloader_enter(struct i2c_client *client) 209 { 210 int err; 211 u8 retry; 212 u32 tmp = 0; 213 unsigned char buf[3]; 214 215 for (retry = 0; retry < 5; retry++) { 216 hyn_reset_proc(client, (7 + retry)); 217 /* set cmd to enter program mode */ 218 put_unaligned_le24(CST3XX_BOOTLDR_PROG_CMD, buf); 219 err = cst3xx_i2c_write(client, buf, 3); 220 if (err) 221 continue; 222 223 usleep_range(2000, 2500); 224 225 /* check whether in program mode */ 226 err = cst3xx_i2c_read_register(client, 227 CST3XX_BOOTLDR_PROG_CHK_REG, 228 buf, 1); 229 if (err) 230 continue; 231 232 tmp = get_unaligned(buf); 233 if (tmp == CST3XX_BOOTLDR_CHK_VAL) 234 break; 235 } 236 237 if (tmp != CST3XX_BOOTLDR_CHK_VAL) { 238 dev_err(&client->dev, "%s unable to enter bootloader mode\n", 239 __func__); 240 return -ENODEV; 241 } 242 243 hyn_reset_proc(client, 40); 244 245 return 0; 246 } 247 248 static void cst3xx_report_contact(struct hynitron_ts_data *ts_data, 249 u8 id, unsigned int x, unsigned int y, u8 w) 250 { 251 input_mt_slot(ts_data->input_dev, id); 252 input_mt_report_slot_state(ts_data->input_dev, MT_TOOL_FINGER, 1); 253 touchscreen_report_pos(ts_data->input_dev, &ts_data->prop, x, y, true); 254 input_report_abs(ts_data->input_dev, ABS_MT_TOUCH_MAJOR, w); 255 } 256 257 static int cst3xx_finish_touch_read(struct i2c_client *client) 258 { 259 unsigned char buf[3]; 260 int err; 261 262 put_unaligned_le24(CST3XX_TOUCH_DATA_STOP_CMD, buf); 263 err = cst3xx_i2c_write(client, buf, 3); 264 if (err) { 265 dev_err(&client->dev, 266 "send read touch info ending failed: %d\n", err); 267 return err; 268 } 269 270 return 0; 271 } 272 273 /* 274 * Handle events from IRQ. Note that for cst3xx it appears that IRQ 275 * fires continuously while touched, otherwise once every 1500ms 276 * when not touched (assume touchscreen waking up periodically). 277 * Note buffer is sized for 5 fingers, if more needed buffer must 278 * be increased. The buffer contains 5 bytes for each touch point, 279 * a touch count byte, a check byte, and then a second check byte after 280 * all other touch points. 281 * 282 * For example 1 touch would look like this: 283 * touch1[5]:touch_count[1]:chk_byte[1] 284 * 285 * 3 touches would look like this: 286 * touch1[5]:touch_count[1]:chk_byte[1]:touch2[5]:touch3[5]:chk_byte[1] 287 */ 288 static void cst3xx_touch_report(struct i2c_client *client) 289 { 290 struct hynitron_ts_data *ts_data = i2c_get_clientdata(client); 291 u8 buf[28]; 292 u8 finger_id, sw, w; 293 unsigned int x, y; 294 unsigned int touch_cnt, end_byte; 295 unsigned int idx = 0; 296 unsigned int i; 297 int err; 298 299 /* Read and validate the first bits of input data. */ 300 err = cst3xx_i2c_read_register(client, CST3XX_TOUCH_DATA_PART_REG, 301 buf, 28); 302 if (err || 303 buf[6] != CST3XX_TOUCH_DATA_CHK_VAL || 304 buf[0] == CST3XX_TOUCH_DATA_CHK_VAL) { 305 dev_err(&client->dev, "cst3xx touch read failure\n"); 306 return; 307 } 308 309 /* Report to the device we're done reading the touch data. */ 310 err = cst3xx_finish_touch_read(client); 311 if (err) 312 return; 313 314 touch_cnt = buf[5] & CST3XX_TOUCH_COUNT_MASK; 315 if (touch_cnt > ts_data->chip->max_touch_num) { 316 dev_err(&client->dev, "cst3xx invalid touch count (%d vs %d max)\n", 317 touch_cnt, ts_data->chip->max_touch_num); 318 return; 319 } 320 321 /* 322 * Check the check bit of the last touch slot. The check bit is 323 * always present after touch point 1 for valid data, and then 324 * appears as the last byte after all other touch data. 325 */ 326 if (touch_cnt > 1) { 327 end_byte = touch_cnt * 5 + 2; 328 if (buf[end_byte] != CST3XX_TOUCH_DATA_CHK_VAL) { 329 dev_err(&client->dev, "cst3xx touch read failure\n"); 330 return; 331 } 332 } 333 334 /* Parse through the buffer to capture touch data. */ 335 for (i = 0; i < touch_cnt; i++) { 336 x = ((buf[idx + 1] << 4) | ((buf[idx + 3] >> 4) & 0x0f)); 337 y = ((buf[idx + 2] << 4) | (buf[idx + 3] & 0x0f)); 338 w = (buf[idx + 4] >> 3); 339 sw = (buf[idx] & 0x0f) >> 1; 340 finger_id = (buf[idx] >> 4) & 0x0f; 341 342 /* Sanity check we don't have more fingers than we expect */ 343 if (finger_id >= ts_data->chip->max_touch_num) { 344 dev_err(&client->dev, 345 "cst3xx invalid finger id %d\n", finger_id); 346 return; 347 } 348 349 /* sw value of 0 means no touch, 0x03 means touch */ 350 if (sw == CST3XX_TOUCH_DATA_TOUCH_VAL) 351 cst3xx_report_contact(ts_data, finger_id, x, y, w); 352 353 idx += 5; 354 355 /* Skip the 2 bytes between point 1 and point 2 */ 356 if (i == 0) 357 idx += 2; 358 } 359 360 input_mt_sync_frame(ts_data->input_dev); 361 input_sync(ts_data->input_dev); 362 } 363 364 static int cst3xx_input_dev_int(struct i2c_client *client) 365 { 366 struct hynitron_ts_data *ts_data = i2c_get_clientdata(client); 367 int err; 368 369 ts_data->input_dev = devm_input_allocate_device(&client->dev); 370 if (!ts_data->input_dev) { 371 dev_err(&client->dev, "Failed to allocate input device\n"); 372 return -ENOMEM; 373 } 374 375 ts_data->input_dev->name = "Hynitron cst3xx Touchscreen"; 376 ts_data->input_dev->phys = "input/ts"; 377 ts_data->input_dev->id.bustype = BUS_I2C; 378 379 input_set_drvdata(ts_data->input_dev, ts_data); 380 381 input_set_capability(ts_data->input_dev, EV_ABS, ABS_MT_POSITION_X); 382 input_set_capability(ts_data->input_dev, EV_ABS, ABS_MT_POSITION_Y); 383 input_set_abs_params(ts_data->input_dev, ABS_MT_TOUCH_MAJOR, 384 0, 255, 0, 0); 385 386 touchscreen_parse_properties(ts_data->input_dev, true, &ts_data->prop); 387 388 if (!ts_data->prop.max_x || !ts_data->prop.max_y) { 389 dev_err(&client->dev, 390 "Invalid x/y (%d, %d), using defaults\n", 391 ts_data->prop.max_x, ts_data->prop.max_y); 392 ts_data->prop.max_x = 1152; 393 ts_data->prop.max_y = 1920; 394 input_abs_set_max(ts_data->input_dev, 395 ABS_MT_POSITION_X, ts_data->prop.max_x); 396 input_abs_set_max(ts_data->input_dev, 397 ABS_MT_POSITION_Y, ts_data->prop.max_y); 398 } 399 400 err = input_mt_init_slots(ts_data->input_dev, 401 ts_data->chip->max_touch_num, 402 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 403 if (err) { 404 dev_err(&client->dev, 405 "Failed to initialize input slots: %d\n", err); 406 return err; 407 } 408 409 err = input_register_device(ts_data->input_dev); 410 if (err) { 411 dev_err(&client->dev, 412 "Input device registration failed: %d\n", err); 413 return err; 414 } 415 416 return 0; 417 } 418 419 static int hyn_probe(struct i2c_client *client) 420 { 421 struct hynitron_ts_data *ts_data; 422 int err; 423 424 ts_data = devm_kzalloc(&client->dev, sizeof(*ts_data), GFP_KERNEL); 425 if (!ts_data) 426 return -ENOMEM; 427 428 ts_data->client = client; 429 i2c_set_clientdata(client, ts_data); 430 431 ts_data->chip = device_get_match_data(&client->dev); 432 if (!ts_data->chip) 433 return -EINVAL; 434 435 ts_data->reset_gpio = devm_gpiod_get(&client->dev, 436 "reset", GPIOD_OUT_LOW); 437 err = PTR_ERR_OR_ZERO(ts_data->reset_gpio); 438 if (err) { 439 dev_err(&client->dev, "request reset gpio failed: %d\n", err); 440 return err; 441 } 442 443 hyn_reset_proc(client, 60); 444 445 err = ts_data->chip->bootloader_enter(client); 446 if (err < 0) 447 return err; 448 449 err = ts_data->chip->init_input(client); 450 if (err < 0) 451 return err; 452 453 err = ts_data->chip->firmware_info(client); 454 if (err < 0) 455 return err; 456 457 err = devm_request_threaded_irq(&client->dev, client->irq, 458 NULL, hyn_interrupt_handler, 459 IRQF_ONESHOT, 460 "Hynitron Touch Int", client); 461 if (err) { 462 dev_err(&client->dev, "failed to request IRQ: %d\n", err); 463 return err; 464 } 465 466 return 0; 467 } 468 469 static const struct hynitron_ts_chip_data cst3xx_data = { 470 .max_touch_num = 5, 471 .ic_chkcode = 0xcaca0000, 472 .firmware_info = &cst3xx_firmware_info, 473 .bootloader_enter = &cst3xx_bootloader_enter, 474 .init_input = &cst3xx_input_dev_int, 475 .report_touch = &cst3xx_touch_report, 476 }; 477 478 static const struct i2c_device_id hyn_tpd_id[] = { 479 { .name = "hynitron_ts" }, 480 { /* sentinel */ }, 481 }; 482 MODULE_DEVICE_TABLE(i2c, hyn_tpd_id); 483 484 static const struct of_device_id hyn_dt_match[] = { 485 { .compatible = "hynitron,cst340", .data = &cst3xx_data }, 486 { /* sentinel */ }, 487 }; 488 MODULE_DEVICE_TABLE(of, hyn_dt_match); 489 490 static struct i2c_driver hynitron_i2c_driver = { 491 .driver = { 492 .name = "Hynitron-TS", 493 .of_match_table = hyn_dt_match, 494 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 495 }, 496 .id_table = hyn_tpd_id, 497 .probe = hyn_probe, 498 }; 499 500 module_i2c_driver(hynitron_i2c_driver); 501 502 MODULE_AUTHOR("Chris Morgan"); 503 MODULE_DESCRIPTION("Hynitron Touchscreen Driver"); 504 MODULE_LICENSE("GPL"); 505