1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/bitfield.h> 4 #include <linux/bits.h> 5 #include <linux/delay.h> 6 #include <linux/i2c.h> 7 #include <linux/input.h> 8 #include <linux/input/mt.h> 9 #include <linux/input/touchscreen.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/property.h> 13 #include <linux/regulator/consumer.h> 14 15 #define IST30XX_REG_STATUS 0x20 16 #define IST30XX_REG_CHIPID (0x40000000 | IST3038C_DIRECT_ACCESS) 17 18 #define IST30XX_WHOAMI 0x30003000 19 #define IST30XXA_WHOAMI 0x300a300a 20 #define IST30XXB_WHOAMI 0x300b300b 21 #define IST3038_WHOAMI 0x30383038 22 23 #define IST3032C_WHOAMI 0x32c 24 #define IST3038C_WHOAMI 0x38c 25 #define IST3038H_WHOAMI 0x38d 26 27 #define IST3038B_REG_CHIPID 0x30 28 #define IST3038B_WHOAMI 0x30380b 29 30 #define IST3038C_HIB_ACCESS (0x800B << 16) 31 #define IST3038C_DIRECT_ACCESS BIT(31) 32 #define IST3038C_REG_CHIPID (0x40001000 | IST3038C_DIRECT_ACCESS) 33 #define IST3038C_REG_HIB_BASE 0x30000100 34 #define IST3038C_REG_TOUCH_STATUS (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS) 35 #define IST3038C_REG_TOUCH_COORD (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x8) 36 #define IST3038C_REG_INTR_MESSAGE (IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x4) 37 #define IST3038C_CHIP_ON_DELAY_MS 60 38 #define IST3038C_I2C_RETRY_COUNT 3 39 #define IST3038C_MAX_FINGER_NUM 10 40 #define IST3038C_X_MASK GENMASK(23, 12) 41 #define IST3038C_Y_MASK GENMASK(11, 0) 42 #define IST3038C_AREA_MASK GENMASK(27, 24) 43 #define IST3038C_FINGER_COUNT_MASK GENMASK(15, 12) 44 #define IST3038C_FINGER_STATUS_MASK GENMASK(9, 0) 45 #define IST3032C_KEY_STATUS_MASK GENMASK(20, 16) 46 47 struct imagis_properties { 48 unsigned int interrupt_msg_cmd; 49 unsigned int touch_coord_cmd; 50 unsigned int whoami_cmd; 51 unsigned int whoami_val; 52 bool protocol_b; 53 bool touch_keys_supported; 54 }; 55 56 struct imagis_ts { 57 struct i2c_client *client; 58 const struct imagis_properties *tdata; 59 struct input_dev *input_dev; 60 struct touchscreen_properties prop; 61 struct regulator_bulk_data supplies[2]; 62 u32 keycodes[5]; 63 int num_keycodes; 64 }; 65 66 static int imagis_i2c_read_reg(struct imagis_ts *ts, 67 unsigned int reg, u32 *data) 68 { 69 __be32 ret_be; 70 __be32 reg_be = cpu_to_be32(reg); 71 struct i2c_msg msg[] = { 72 { 73 .addr = ts->client->addr, 74 .flags = 0, 75 .buf = (unsigned char *)®_be, 76 .len = sizeof(reg_be), 77 }, { 78 .addr = ts->client->addr, 79 .flags = I2C_M_RD, 80 .buf = (unsigned char *)&ret_be, 81 .len = sizeof(ret_be), 82 }, 83 }; 84 int ret, error; 85 int retry = IST3038C_I2C_RETRY_COUNT; 86 87 /* Retry in case the controller fails to respond */ 88 do { 89 ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg)); 90 if (ret == ARRAY_SIZE(msg)) { 91 *data = be32_to_cpu(ret_be); 92 return 0; 93 } 94 95 error = ret < 0 ? ret : -EIO; 96 dev_err(&ts->client->dev, 97 "%s - i2c_transfer failed: %d (%d)\n", 98 __func__, error, ret); 99 } while (--retry); 100 101 return error; 102 } 103 104 static irqreturn_t imagis_interrupt(int irq, void *dev_id) 105 { 106 struct imagis_ts *ts = dev_id; 107 u32 intr_message, finger_status; 108 unsigned int finger_count, finger_pressed, key_pressed; 109 int i; 110 int error; 111 112 error = imagis_i2c_read_reg(ts, ts->tdata->interrupt_msg_cmd, &intr_message); 113 if (error) { 114 dev_err(&ts->client->dev, 115 "failed to read the interrupt message: %d\n", error); 116 goto out; 117 } 118 119 finger_count = FIELD_GET(IST3038C_FINGER_COUNT_MASK, intr_message); 120 if (finger_count > IST3038C_MAX_FINGER_NUM) { 121 dev_err(&ts->client->dev, 122 "finger count %d is more than maximum supported\n", 123 finger_count); 124 goto out; 125 } 126 127 finger_pressed = FIELD_GET(IST3038C_FINGER_STATUS_MASK, intr_message); 128 129 for (i = 0; i < finger_count; i++) { 130 if (ts->tdata->protocol_b) 131 error = imagis_i2c_read_reg(ts, 132 ts->tdata->touch_coord_cmd + (i * 4), 133 &finger_status); 134 else 135 error = imagis_i2c_read_reg(ts, 136 ts->tdata->touch_coord_cmd, &finger_status); 137 if (error) { 138 dev_err(&ts->client->dev, 139 "failed to read coordinates for finger %d: %d\n", 140 i, error); 141 goto out; 142 } 143 144 input_mt_slot(ts->input_dev, i); 145 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, 146 finger_pressed & BIT(i)); 147 touchscreen_report_pos(ts->input_dev, &ts->prop, 148 FIELD_GET(IST3038C_X_MASK, finger_status), 149 FIELD_GET(IST3038C_Y_MASK, finger_status), 150 true); 151 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, 152 FIELD_GET(IST3038C_AREA_MASK, finger_status)); 153 } 154 155 key_pressed = FIELD_GET(IST3032C_KEY_STATUS_MASK, intr_message); 156 157 for (int i = 0; i < ts->num_keycodes; i++) 158 input_report_key(ts->input_dev, ts->keycodes[i], 159 key_pressed & BIT(i)); 160 161 input_mt_sync_frame(ts->input_dev); 162 input_sync(ts->input_dev); 163 164 out: 165 return IRQ_HANDLED; 166 } 167 168 static void imagis_power_off(void *_ts) 169 { 170 struct imagis_ts *ts = _ts; 171 172 regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies); 173 } 174 175 static int imagis_power_on(struct imagis_ts *ts) 176 { 177 int error; 178 179 error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies); 180 if (error) 181 return error; 182 183 msleep(IST3038C_CHIP_ON_DELAY_MS); 184 185 return 0; 186 } 187 188 static int imagis_start(struct imagis_ts *ts) 189 { 190 int error; 191 192 error = imagis_power_on(ts); 193 if (error) 194 return error; 195 196 enable_irq(ts->client->irq); 197 198 return 0; 199 } 200 201 static int imagis_stop(struct imagis_ts *ts) 202 { 203 disable_irq(ts->client->irq); 204 205 imagis_power_off(ts); 206 207 return 0; 208 } 209 210 static int imagis_input_open(struct input_dev *dev) 211 { 212 struct imagis_ts *ts = input_get_drvdata(dev); 213 214 return imagis_start(ts); 215 } 216 217 static void imagis_input_close(struct input_dev *dev) 218 { 219 struct imagis_ts *ts = input_get_drvdata(dev); 220 221 imagis_stop(ts); 222 } 223 224 static int imagis_init_input_dev(struct imagis_ts *ts) 225 { 226 struct input_dev *input_dev; 227 int error; 228 229 input_dev = devm_input_allocate_device(&ts->client->dev); 230 if (!input_dev) 231 return -ENOMEM; 232 233 ts->input_dev = input_dev; 234 235 input_dev->name = "Imagis capacitive touchscreen"; 236 input_dev->phys = "input/ts"; 237 input_dev->id.bustype = BUS_I2C; 238 input_dev->open = imagis_input_open; 239 input_dev->close = imagis_input_close; 240 241 input_set_drvdata(input_dev, ts); 242 243 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X); 244 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y); 245 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 16, 0, 0); 246 if (ts->tdata->touch_keys_supported) { 247 ts->num_keycodes = of_property_read_variable_u32_array( 248 ts->client->dev.of_node, "linux,keycodes", 249 ts->keycodes, 0, ARRAY_SIZE(ts->keycodes)); 250 if (ts->num_keycodes <= 0) { 251 ts->keycodes[0] = KEY_APPSELECT; 252 ts->keycodes[1] = KEY_BACK; 253 ts->num_keycodes = 2; 254 } 255 256 input_dev->keycodemax = ts->num_keycodes; 257 input_dev->keycodesize = sizeof(ts->keycodes[0]); 258 input_dev->keycode = ts->keycodes; 259 } 260 261 for (int i = 0; i < ts->num_keycodes; i++) 262 input_set_capability(input_dev, EV_KEY, ts->keycodes[i]); 263 264 touchscreen_parse_properties(input_dev, true, &ts->prop); 265 if (!ts->prop.max_x || !ts->prop.max_y) { 266 dev_err(&ts->client->dev, 267 "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n"); 268 return -EINVAL; 269 } 270 271 error = input_mt_init_slots(input_dev, 272 IST3038C_MAX_FINGER_NUM, 273 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 274 if (error) { 275 dev_err(&ts->client->dev, 276 "Failed to initialize MT slots: %d", error); 277 return error; 278 } 279 280 error = input_register_device(input_dev); 281 if (error) { 282 dev_err(&ts->client->dev, 283 "Failed to register input device: %d", error); 284 return error; 285 } 286 287 return 0; 288 } 289 290 static int imagis_init_regulators(struct imagis_ts *ts) 291 { 292 struct i2c_client *client = ts->client; 293 294 ts->supplies[0].supply = "vdd"; 295 ts->supplies[1].supply = "vddio"; 296 return devm_regulator_bulk_get(&client->dev, 297 ARRAY_SIZE(ts->supplies), 298 ts->supplies); 299 } 300 301 static int imagis_probe(struct i2c_client *i2c) 302 { 303 struct device *dev = &i2c->dev; 304 struct imagis_ts *ts; 305 int chip_id, error; 306 307 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 308 if (!ts) 309 return -ENOMEM; 310 311 ts->client = i2c; 312 313 ts->tdata = device_get_match_data(dev); 314 if (!ts->tdata) { 315 dev_err(dev, "missing chip data\n"); 316 return -EINVAL; 317 } 318 319 error = imagis_init_regulators(ts); 320 if (error) { 321 dev_err(dev, "regulator init error: %d\n", error); 322 return error; 323 } 324 325 error = imagis_power_on(ts); 326 if (error) { 327 dev_err(dev, "failed to enable regulators: %d\n", error); 328 return error; 329 } 330 331 error = devm_add_action_or_reset(dev, imagis_power_off, ts); 332 if (error) { 333 dev_err(dev, "failed to install poweroff action: %d\n", error); 334 return error; 335 } 336 337 error = imagis_i2c_read_reg(ts, ts->tdata->whoami_cmd, &chip_id); 338 if (error) { 339 dev_err(dev, "chip ID read failure: %d\n", error); 340 return error; 341 } 342 343 if (chip_id != ts->tdata->whoami_val) { 344 dev_err(dev, "unknown chip ID: 0x%x\n", chip_id); 345 return -EINVAL; 346 } 347 348 error = devm_request_threaded_irq(dev, i2c->irq, 349 NULL, imagis_interrupt, 350 IRQF_ONESHOT | IRQF_NO_AUTOEN, 351 "imagis-touchscreen", ts); 352 if (error) { 353 dev_err(dev, "IRQ %d allocation failure: %d\n", 354 i2c->irq, error); 355 return error; 356 } 357 358 error = imagis_init_input_dev(ts); 359 if (error) 360 return error; 361 362 return 0; 363 } 364 365 static int imagis_suspend(struct device *dev) 366 { 367 struct i2c_client *client = to_i2c_client(dev); 368 struct imagis_ts *ts = i2c_get_clientdata(client); 369 int retval = 0; 370 371 mutex_lock(&ts->input_dev->mutex); 372 373 if (input_device_enabled(ts->input_dev)) 374 retval = imagis_stop(ts); 375 376 mutex_unlock(&ts->input_dev->mutex); 377 378 return retval; 379 } 380 381 static int imagis_resume(struct device *dev) 382 { 383 struct i2c_client *client = to_i2c_client(dev); 384 struct imagis_ts *ts = i2c_get_clientdata(client); 385 int retval = 0; 386 387 mutex_lock(&ts->input_dev->mutex); 388 389 if (input_device_enabled(ts->input_dev)) 390 retval = imagis_start(ts); 391 392 mutex_unlock(&ts->input_dev->mutex); 393 394 return retval; 395 } 396 397 static DEFINE_SIMPLE_DEV_PM_OPS(imagis_pm_ops, imagis_suspend, imagis_resume); 398 399 #ifdef CONFIG_OF 400 static const struct imagis_properties imagis_3032c_data = { 401 .interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE, 402 .touch_coord_cmd = IST3038C_REG_TOUCH_COORD, 403 .whoami_cmd = IST3038C_REG_CHIPID, 404 .whoami_val = IST3032C_WHOAMI, 405 .touch_keys_supported = true, 406 .protocol_b = true, 407 }; 408 409 static const struct imagis_properties imagis_3038_data = { 410 .interrupt_msg_cmd = IST30XX_REG_STATUS, 411 .touch_coord_cmd = IST30XX_REG_STATUS, 412 .whoami_cmd = IST30XX_REG_CHIPID, 413 .whoami_val = IST3038_WHOAMI, 414 .touch_keys_supported = true, 415 }; 416 417 static const struct imagis_properties imagis_3038b_data = { 418 .interrupt_msg_cmd = IST30XX_REG_STATUS, 419 .touch_coord_cmd = IST30XX_REG_STATUS, 420 .whoami_cmd = IST3038B_REG_CHIPID, 421 .whoami_val = IST3038B_WHOAMI, 422 }; 423 424 static const struct imagis_properties imagis_3038c_data = { 425 .interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE, 426 .touch_coord_cmd = IST3038C_REG_TOUCH_COORD, 427 .whoami_cmd = IST3038C_REG_CHIPID, 428 .whoami_val = IST3038C_WHOAMI, 429 .protocol_b = true, 430 }; 431 432 static const struct imagis_properties imagis_3038h_data = { 433 .interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE, 434 .touch_coord_cmd = IST3038C_REG_TOUCH_COORD, 435 .whoami_cmd = IST3038C_REG_CHIPID, 436 .whoami_val = IST3038H_WHOAMI, 437 }; 438 439 static const struct of_device_id imagis_of_match[] = { 440 { .compatible = "imagis,ist3032c", .data = &imagis_3032c_data }, 441 { .compatible = "imagis,ist3038", .data = &imagis_3038_data }, 442 { .compatible = "imagis,ist3038b", .data = &imagis_3038b_data }, 443 { .compatible = "imagis,ist3038c", .data = &imagis_3038c_data }, 444 { .compatible = "imagis,ist3038h", .data = &imagis_3038h_data }, 445 { }, 446 }; 447 MODULE_DEVICE_TABLE(of, imagis_of_match); 448 #endif 449 450 static struct i2c_driver imagis_ts_driver = { 451 .driver = { 452 .name = "imagis-touchscreen", 453 .pm = pm_sleep_ptr(&imagis_pm_ops), 454 .of_match_table = of_match_ptr(imagis_of_match), 455 }, 456 .probe = imagis_probe, 457 }; 458 459 module_i2c_driver(imagis_ts_driver); 460 461 MODULE_DESCRIPTION("Imagis IST3038C Touchscreen Driver"); 462 MODULE_AUTHOR("Markuss Broks <markuss.broks@gmail.com>"); 463 MODULE_LICENSE("GPL"); 464