1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Himax HX852x(ES) Touchscreen Driver 4 * Copyright (c) 2020-2024 Stephan Gerhold <stephan@gerhold.net> 5 * Copyright (c) 2020 Jonathan Albrieux <jonathan.albrieux@gmail.com> 6 * 7 * Based on the Himax Android Driver Sample Code Ver 0.3 for HMX852xES chipset: 8 * Copyright (c) 2014 Himax Corporation. 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/i2c.h> 14 #include <linux/input.h> 15 #include <linux/input/mt.h> 16 #include <linux/input/touchscreen.h> 17 #include <linux/interrupt.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/of.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/unaligned.h> 23 24 #define HX852X_COORD_SIZE(fingers) ((fingers) * sizeof(struct hx852x_coord)) 25 #define HX852X_WIDTH_SIZE(fingers) ALIGN(fingers, 4) 26 #define HX852X_BUF_SIZE(fingers) (HX852X_COORD_SIZE(fingers) + \ 27 HX852X_WIDTH_SIZE(fingers) + \ 28 sizeof(struct hx852x_touch_info)) 29 30 #define HX852X_MAX_FINGERS 12 31 #define HX852X_MAX_KEY_COUNT 4 32 #define HX852X_MAX_BUF_SIZE HX852X_BUF_SIZE(HX852X_MAX_FINGERS) 33 34 #define HX852X_TS_SLEEP_IN 0x80 35 #define HX852X_TS_SLEEP_OUT 0x81 36 #define HX852X_TS_SENSE_OFF 0x82 37 #define HX852X_TS_SENSE_ON 0x83 38 #define HX852X_READ_ONE_EVENT 0x85 39 #define HX852X_READ_ALL_EVENTS 0x86 40 #define HX852X_READ_LATEST_EVENT 0x87 41 #define HX852X_CLEAR_EVENT_STACK 0x88 42 43 #define HX852X_REG_SRAM_SWITCH 0x8c 44 #define HX852X_REG_SRAM_ADDR 0x8b 45 #define HX852X_REG_FLASH_RPLACE 0x5a 46 47 #define HX852X_SRAM_SWITCH_TEST_MODE 0x14 48 #define HX852X_SRAM_ADDR_CONFIG 0x7000 49 50 struct hx852x { 51 struct i2c_client *client; 52 struct input_dev *input_dev; 53 struct touchscreen_properties props; 54 struct gpio_desc *reset_gpiod; 55 struct regulator_bulk_data supplies[2]; 56 unsigned int max_fingers; 57 unsigned int keycount; 58 unsigned int keycodes[HX852X_MAX_KEY_COUNT]; 59 }; 60 61 struct hx852x_config { 62 u8 rx_num; 63 u8 tx_num; 64 u8 max_pt; 65 u8 padding1[3]; 66 __be16 x_res; 67 __be16 y_res; 68 u8 padding2[2]; 69 } __packed __aligned(4); 70 71 struct hx852x_coord { 72 __be16 x; 73 __be16 y; 74 } __packed __aligned(4); 75 76 struct hx852x_touch_info { 77 u8 finger_num; 78 __le16 finger_pressed; 79 u8 padding; 80 } __packed __aligned(4); 81 82 static int hx852x_i2c_read(struct hx852x *hx, u8 cmd, void *data, u16 len) 83 { 84 struct i2c_client *client = hx->client; 85 int error; 86 int ret; 87 88 struct i2c_msg msg[] = { 89 { 90 .addr = client->addr, 91 .flags = 0, 92 .len = 1, 93 .buf = &cmd, 94 }, 95 { 96 .addr = client->addr, 97 .flags = I2C_M_RD, 98 .len = len, 99 .buf = data, 100 }, 101 }; 102 103 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 104 if (ret != ARRAY_SIZE(msg)) { 105 error = ret < 0 ? ret : -EIO; 106 dev_err(&client->dev, "failed to read %#x: %d\n", cmd, error); 107 return error; 108 } 109 110 return 0; 111 } 112 113 static int hx852x_power_on(struct hx852x *hx) 114 { 115 struct device *dev = &hx->client->dev; 116 int error; 117 118 error = regulator_bulk_enable(ARRAY_SIZE(hx->supplies), hx->supplies); 119 if (error) { 120 dev_err(dev, "failed to enable regulators: %d\n", error); 121 return error; 122 } 123 124 gpiod_set_value_cansleep(hx->reset_gpiod, 1); 125 msleep(20); 126 gpiod_set_value_cansleep(hx->reset_gpiod, 0); 127 msleep(50); 128 129 return 0; 130 } 131 132 static int hx852x_start(struct hx852x *hx) 133 { 134 struct device *dev = &hx->client->dev; 135 int error; 136 137 error = i2c_smbus_write_byte(hx->client, HX852X_TS_SLEEP_OUT); 138 if (error) { 139 dev_err(dev, "failed to send TS_SLEEP_OUT: %d\n", error); 140 return error; 141 } 142 msleep(30); 143 144 error = i2c_smbus_write_byte(hx->client, HX852X_TS_SENSE_ON); 145 if (error) { 146 dev_err(dev, "failed to send TS_SENSE_ON: %d\n", error); 147 return error; 148 } 149 msleep(20); 150 151 return 0; 152 } 153 154 static int hx852x_stop(struct hx852x *hx) 155 { 156 struct device *dev = &hx->client->dev; 157 int error; 158 159 error = i2c_smbus_write_byte(hx->client, HX852X_TS_SENSE_OFF); 160 if (error) { 161 dev_err(dev, "failed to send TS_SENSE_OFF: %d\n", error); 162 return error; 163 } 164 msleep(20); 165 166 error = i2c_smbus_write_byte(hx->client, HX852X_TS_SLEEP_IN); 167 if (error) { 168 dev_err(dev, "failed to send TS_SLEEP_IN: %d\n", error); 169 return error; 170 } 171 msleep(30); 172 173 return 0; 174 } 175 176 static int hx852x_power_off(struct hx852x *hx) 177 { 178 struct device *dev = &hx->client->dev; 179 int error; 180 181 error = regulator_bulk_disable(ARRAY_SIZE(hx->supplies), hx->supplies); 182 if (error) { 183 dev_err(dev, "failed to disable regulators: %d\n", error); 184 return error; 185 } 186 187 return 0; 188 } 189 190 static int hx852x_read_config(struct hx852x *hx) 191 { 192 struct device *dev = &hx->client->dev; 193 struct hx852x_config conf; 194 int x_res, y_res; 195 int error, error2; 196 197 error = hx852x_power_on(hx); 198 if (error) 199 return error; 200 201 /* Sensing must be turned on briefly to load the config */ 202 error = hx852x_start(hx); 203 if (error) 204 goto err_power_off; 205 206 error = hx852x_stop(hx); 207 if (error) 208 goto err_power_off; 209 210 error = i2c_smbus_write_byte_data(hx->client, HX852X_REG_SRAM_SWITCH, 211 HX852X_SRAM_SWITCH_TEST_MODE); 212 if (error) 213 goto err_power_off; 214 215 error = i2c_smbus_write_word_data(hx->client, HX852X_REG_SRAM_ADDR, 216 HX852X_SRAM_ADDR_CONFIG); 217 if (error) 218 goto err_test_mode; 219 220 error = hx852x_i2c_read(hx, HX852X_REG_FLASH_RPLACE, &conf, sizeof(conf)); 221 if (error) 222 goto err_test_mode; 223 224 x_res = be16_to_cpu(conf.x_res); 225 y_res = be16_to_cpu(conf.y_res); 226 hx->max_fingers = (conf.max_pt & 0xf0) >> 4; 227 dev_dbg(dev, "x res: %u, y res: %u, max fingers: %u\n", 228 x_res, y_res, hx->max_fingers); 229 230 if (hx->max_fingers > HX852X_MAX_FINGERS) { 231 dev_err(dev, "max supported fingers: %u, found: %u\n", 232 HX852X_MAX_FINGERS, hx->max_fingers); 233 error = -EINVAL; 234 goto err_test_mode; 235 } 236 237 if (x_res && y_res) { 238 input_set_abs_params(hx->input_dev, ABS_MT_POSITION_X, 0, x_res - 1, 0, 0); 239 input_set_abs_params(hx->input_dev, ABS_MT_POSITION_Y, 0, y_res - 1, 0, 0); 240 } 241 242 err_test_mode: 243 error2 = i2c_smbus_write_byte_data(hx->client, HX852X_REG_SRAM_SWITCH, 0); 244 error = error ?: error2; 245 err_power_off: 246 error2 = hx852x_power_off(hx); 247 return error ?: error2; 248 } 249 250 static int hx852x_handle_events(struct hx852x *hx) 251 { 252 /* 253 * The event packets have variable size, depending on the amount of 254 * supported fingers (hx->max_fingers). They are laid out as follows: 255 * - struct hx852x_coord[hx->max_fingers]: Coordinates for each finger 256 * - u8[ALIGN(hx->max_fingers, 4)]: Touch width for each finger 257 * with padding for 32-bit alignment 258 * - struct hx852x_touch_info 259 * 260 * Load everything into a 32-bit aligned buffer so the coordinates 261 * can be assigned directly, without using get_unaligned_*(). 262 */ 263 u8 buf[HX852X_MAX_BUF_SIZE] __aligned(4); 264 struct hx852x_coord *coord = (struct hx852x_coord *)buf; 265 u8 *width = &buf[HX852X_COORD_SIZE(hx->max_fingers)]; 266 struct hx852x_touch_info *info = (struct hx852x_touch_info *) 267 &width[HX852X_WIDTH_SIZE(hx->max_fingers)]; 268 unsigned long finger_pressed, key_pressed; 269 unsigned int i, x, y, w; 270 int error; 271 272 error = hx852x_i2c_read(hx, HX852X_READ_ALL_EVENTS, buf, 273 HX852X_BUF_SIZE(hx->max_fingers)); 274 if (error) 275 return error; 276 277 finger_pressed = get_unaligned_le16(&info->finger_pressed); 278 key_pressed = finger_pressed >> HX852X_MAX_FINGERS; 279 280 /* All bits are set when no touch is detected */ 281 if (info->finger_num == 0xff || !(info->finger_num & 0x0f)) 282 finger_pressed = 0; 283 if (key_pressed == 0xf) 284 key_pressed = 0; 285 286 for_each_set_bit(i, &finger_pressed, hx->max_fingers) { 287 x = be16_to_cpu(coord[i].x); 288 y = be16_to_cpu(coord[i].y); 289 w = width[i]; 290 291 input_mt_slot(hx->input_dev, i); 292 input_mt_report_slot_state(hx->input_dev, MT_TOOL_FINGER, 1); 293 touchscreen_report_pos(hx->input_dev, &hx->props, x, y, true); 294 input_report_abs(hx->input_dev, ABS_MT_TOUCH_MAJOR, w); 295 } 296 input_mt_sync_frame(hx->input_dev); 297 298 for (i = 0; i < hx->keycount; i++) 299 input_report_key(hx->input_dev, hx->keycodes[i], key_pressed & BIT(i)); 300 301 input_sync(hx->input_dev); 302 return 0; 303 } 304 305 static irqreturn_t hx852x_interrupt(int irq, void *ptr) 306 { 307 struct hx852x *hx = ptr; 308 int error; 309 310 error = hx852x_handle_events(hx); 311 if (error) { 312 dev_err_ratelimited(&hx->client->dev, 313 "failed to handle events: %d\n", error); 314 return IRQ_NONE; 315 } 316 317 return IRQ_HANDLED; 318 } 319 320 static int hx852x_input_open(struct input_dev *dev) 321 { 322 struct hx852x *hx = input_get_drvdata(dev); 323 int error; 324 325 error = hx852x_power_on(hx); 326 if (error) 327 return error; 328 329 error = hx852x_start(hx); 330 if (error) { 331 hx852x_power_off(hx); 332 return error; 333 } 334 335 enable_irq(hx->client->irq); 336 return 0; 337 } 338 339 static void hx852x_input_close(struct input_dev *dev) 340 { 341 struct hx852x *hx = input_get_drvdata(dev); 342 343 hx852x_stop(hx); 344 disable_irq(hx->client->irq); 345 hx852x_power_off(hx); 346 } 347 348 static int hx852x_parse_properties(struct hx852x *hx) 349 { 350 struct device *dev = &hx->client->dev; 351 int error, count; 352 353 count = device_property_count_u32(dev, "linux,keycodes"); 354 if (count == -EINVAL) { 355 /* Property does not exist, keycodes are optional */ 356 return 0; 357 } else if (count < 0) { 358 dev_err(dev, "Failed to read linux,keycodes: %d\n", count); 359 return count; 360 } else if (count > HX852X_MAX_KEY_COUNT) { 361 dev_err(dev, "max supported keys: %u, found: %u\n", 362 HX852X_MAX_KEY_COUNT, hx->keycount); 363 return -EINVAL; 364 } 365 hx->keycount = count; 366 367 error = device_property_read_u32_array(dev, "linux,keycodes", 368 hx->keycodes, hx->keycount); 369 if (error) { 370 dev_err(dev, "failed to read linux,keycodes: %d\n", error); 371 return error; 372 } 373 374 return 0; 375 } 376 377 static int hx852x_probe(struct i2c_client *client) 378 { 379 struct device *dev = &client->dev; 380 struct hx852x *hx; 381 int error, i; 382 383 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 384 I2C_FUNC_SMBUS_WRITE_BYTE | 385 I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 386 I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { 387 dev_err(dev, "not all required i2c functionality supported\n"); 388 return -ENXIO; 389 } 390 391 hx = devm_kzalloc(dev, sizeof(*hx), GFP_KERNEL); 392 if (!hx) 393 return -ENOMEM; 394 395 hx->client = client; 396 hx->input_dev = devm_input_allocate_device(dev); 397 if (!hx->input_dev) 398 return -ENOMEM; 399 400 hx->input_dev->name = "Himax HX852x"; 401 hx->input_dev->id.bustype = BUS_I2C; 402 hx->input_dev->open = hx852x_input_open; 403 hx->input_dev->close = hx852x_input_close; 404 405 i2c_set_clientdata(client, hx); 406 input_set_drvdata(hx->input_dev, hx); 407 408 hx->supplies[0].supply = "vcca"; 409 hx->supplies[1].supply = "vccd"; 410 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(hx->supplies), hx->supplies); 411 if (error) 412 return dev_err_probe(dev, error, "failed to get regulators\n"); 413 414 hx->reset_gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 415 if (IS_ERR(hx->reset_gpiod)) 416 return dev_err_probe(dev, PTR_ERR(hx->reset_gpiod), 417 "failed to get reset gpio\n"); 418 419 error = devm_request_threaded_irq(dev, client->irq, NULL, hx852x_interrupt, 420 IRQF_ONESHOT | IRQF_NO_AUTOEN, NULL, hx); 421 if (error) 422 return dev_err_probe(dev, error, "failed to request irq %d", client->irq); 423 424 error = hx852x_read_config(hx); 425 if (error) 426 return error; 427 428 input_set_capability(hx->input_dev, EV_ABS, ABS_MT_POSITION_X); 429 input_set_capability(hx->input_dev, EV_ABS, ABS_MT_POSITION_Y); 430 input_set_abs_params(hx->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 431 432 touchscreen_parse_properties(hx->input_dev, true, &hx->props); 433 error = hx852x_parse_properties(hx); 434 if (error) 435 return error; 436 437 hx->input_dev->keycode = hx->keycodes; 438 hx->input_dev->keycodemax = hx->keycount; 439 hx->input_dev->keycodesize = sizeof(hx->keycodes[0]); 440 for (i = 0; i < hx->keycount; i++) 441 input_set_capability(hx->input_dev, EV_KEY, hx->keycodes[i]); 442 443 error = input_mt_init_slots(hx->input_dev, hx->max_fingers, 444 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 445 if (error) 446 return dev_err_probe(dev, error, "failed to init MT slots\n"); 447 448 error = input_register_device(hx->input_dev); 449 if (error) 450 return dev_err_probe(dev, error, "failed to register input device\n"); 451 452 return 0; 453 } 454 455 static int hx852x_suspend(struct device *dev) 456 { 457 struct hx852x *hx = dev_get_drvdata(dev); 458 459 guard(mutex)(&hx->input_dev->mutex); 460 461 if (input_device_enabled(hx->input_dev)) 462 return hx852x_stop(hx); 463 464 return 0; 465 } 466 467 static int hx852x_resume(struct device *dev) 468 { 469 struct hx852x *hx = dev_get_drvdata(dev); 470 471 guard(mutex)(&hx->input_dev->mutex); 472 473 if (input_device_enabled(hx->input_dev)) 474 return hx852x_start(hx); 475 476 return 0; 477 } 478 479 static DEFINE_SIMPLE_DEV_PM_OPS(hx852x_pm_ops, hx852x_suspend, hx852x_resume); 480 481 #ifdef CONFIG_OF 482 static const struct of_device_id hx852x_of_match[] = { 483 { .compatible = "himax,hx852es" }, 484 { } 485 }; 486 MODULE_DEVICE_TABLE(of, hx852x_of_match); 487 #endif 488 489 static struct i2c_driver hx852x_driver = { 490 .probe = hx852x_probe, 491 .driver = { 492 .name = "himax_hx852x", 493 .pm = pm_sleep_ptr(&hx852x_pm_ops), 494 .of_match_table = of_match_ptr(hx852x_of_match), 495 }, 496 }; 497 module_i2c_driver(hx852x_driver); 498 499 MODULE_DESCRIPTION("Himax HX852x(ES) Touchscreen Driver"); 500 MODULE_AUTHOR("Jonathan Albrieux <jonathan.albrieux@gmail.com>"); 501 MODULE_AUTHOR("Stephan Gerhold <stephan@gerhold.net>"); 502 MODULE_LICENSE("GPL"); 503