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 /* 316 * Check the check bit of the last touch slot. The check bit is 317 * always present after touch point 1 for valid data, and then 318 * appears as the last byte after all other touch data. 319 */ 320 if (touch_cnt > 1) { 321 end_byte = touch_cnt * 5 + 2; 322 if (buf[end_byte] != CST3XX_TOUCH_DATA_CHK_VAL) { 323 dev_err(&client->dev, "cst3xx touch read failure\n"); 324 return; 325 } 326 } 327 328 /* Parse through the buffer to capture touch data. */ 329 for (i = 0; i < touch_cnt; i++) { 330 x = ((buf[idx + 1] << 4) | ((buf[idx + 3] >> 4) & 0x0f)); 331 y = ((buf[idx + 2] << 4) | (buf[idx + 3] & 0x0f)); 332 w = (buf[idx + 4] >> 3); 333 sw = (buf[idx] & 0x0f) >> 1; 334 finger_id = (buf[idx] >> 4) & 0x0f; 335 336 /* Sanity check we don't have more fingers than we expect */ 337 if (ts_data->chip->max_touch_num < finger_id) { 338 dev_err(&client->dev, "cst3xx touch read failure\n"); 339 break; 340 } 341 342 /* sw value of 0 means no touch, 0x03 means touch */ 343 if (sw == CST3XX_TOUCH_DATA_TOUCH_VAL) 344 cst3xx_report_contact(ts_data, finger_id, x, y, w); 345 346 idx += 5; 347 348 /* Skip the 2 bytes between point 1 and point 2 */ 349 if (i == 0) 350 idx += 2; 351 } 352 353 input_mt_sync_frame(ts_data->input_dev); 354 input_sync(ts_data->input_dev); 355 } 356 357 static int cst3xx_input_dev_int(struct i2c_client *client) 358 { 359 struct hynitron_ts_data *ts_data = i2c_get_clientdata(client); 360 int err; 361 362 ts_data->input_dev = devm_input_allocate_device(&client->dev); 363 if (!ts_data->input_dev) { 364 dev_err(&client->dev, "Failed to allocate input device\n"); 365 return -ENOMEM; 366 } 367 368 ts_data->input_dev->name = "Hynitron cst3xx Touchscreen"; 369 ts_data->input_dev->phys = "input/ts"; 370 ts_data->input_dev->id.bustype = BUS_I2C; 371 372 input_set_drvdata(ts_data->input_dev, ts_data); 373 374 input_set_capability(ts_data->input_dev, EV_ABS, ABS_MT_POSITION_X); 375 input_set_capability(ts_data->input_dev, EV_ABS, ABS_MT_POSITION_Y); 376 input_set_abs_params(ts_data->input_dev, ABS_MT_TOUCH_MAJOR, 377 0, 255, 0, 0); 378 379 touchscreen_parse_properties(ts_data->input_dev, true, &ts_data->prop); 380 381 if (!ts_data->prop.max_x || !ts_data->prop.max_y) { 382 dev_err(&client->dev, 383 "Invalid x/y (%d, %d), using defaults\n", 384 ts_data->prop.max_x, ts_data->prop.max_y); 385 ts_data->prop.max_x = 1152; 386 ts_data->prop.max_y = 1920; 387 input_abs_set_max(ts_data->input_dev, 388 ABS_MT_POSITION_X, ts_data->prop.max_x); 389 input_abs_set_max(ts_data->input_dev, 390 ABS_MT_POSITION_Y, ts_data->prop.max_y); 391 } 392 393 err = input_mt_init_slots(ts_data->input_dev, 394 ts_data->chip->max_touch_num, 395 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 396 if (err) { 397 dev_err(&client->dev, 398 "Failed to initialize input slots: %d\n", err); 399 return err; 400 } 401 402 err = input_register_device(ts_data->input_dev); 403 if (err) { 404 dev_err(&client->dev, 405 "Input device registration failed: %d\n", err); 406 return err; 407 } 408 409 return 0; 410 } 411 412 static int hyn_probe(struct i2c_client *client) 413 { 414 struct hynitron_ts_data *ts_data; 415 int err; 416 417 ts_data = devm_kzalloc(&client->dev, sizeof(*ts_data), GFP_KERNEL); 418 if (!ts_data) 419 return -ENOMEM; 420 421 ts_data->client = client; 422 i2c_set_clientdata(client, ts_data); 423 424 ts_data->chip = device_get_match_data(&client->dev); 425 if (!ts_data->chip) 426 return -EINVAL; 427 428 ts_data->reset_gpio = devm_gpiod_get(&client->dev, 429 "reset", GPIOD_OUT_LOW); 430 err = PTR_ERR_OR_ZERO(ts_data->reset_gpio); 431 if (err) { 432 dev_err(&client->dev, "request reset gpio failed: %d\n", err); 433 return err; 434 } 435 436 hyn_reset_proc(client, 60); 437 438 err = ts_data->chip->bootloader_enter(client); 439 if (err < 0) 440 return err; 441 442 err = ts_data->chip->init_input(client); 443 if (err < 0) 444 return err; 445 446 err = ts_data->chip->firmware_info(client); 447 if (err < 0) 448 return err; 449 450 err = devm_request_threaded_irq(&client->dev, client->irq, 451 NULL, hyn_interrupt_handler, 452 IRQF_ONESHOT, 453 "Hynitron Touch Int", client); 454 if (err) { 455 dev_err(&client->dev, "failed to request IRQ: %d\n", err); 456 return err; 457 } 458 459 return 0; 460 } 461 462 static const struct hynitron_ts_chip_data cst3xx_data = { 463 .max_touch_num = 5, 464 .ic_chkcode = 0xcaca0000, 465 .firmware_info = &cst3xx_firmware_info, 466 .bootloader_enter = &cst3xx_bootloader_enter, 467 .init_input = &cst3xx_input_dev_int, 468 .report_touch = &cst3xx_touch_report, 469 }; 470 471 static const struct i2c_device_id hyn_tpd_id[] = { 472 { .name = "hynitron_ts" }, 473 { /* sentinel */ }, 474 }; 475 MODULE_DEVICE_TABLE(i2c, hyn_tpd_id); 476 477 static const struct of_device_id hyn_dt_match[] = { 478 { .compatible = "hynitron,cst340", .data = &cst3xx_data }, 479 { /* sentinel */ }, 480 }; 481 MODULE_DEVICE_TABLE(of, hyn_dt_match); 482 483 static struct i2c_driver hynitron_i2c_driver = { 484 .driver = { 485 .name = "Hynitron-TS", 486 .of_match_table = hyn_dt_match, 487 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 488 }, 489 .id_table = hyn_tpd_id, 490 .probe = hyn_probe, 491 }; 492 493 module_i2c_driver(hynitron_i2c_driver); 494 495 MODULE_AUTHOR("Chris Morgan"); 496 MODULE_DESCRIPTION("Hynitron Touchscreen Driver"); 497 MODULE_LICENSE("GPL"); 498