1 #include <linux/module.h> 2 #include <linux/i2c.h> 3 #include <linux/interrupt.h> 4 #include <linux/slab.h> 5 #include <linux/input.h> 6 #include <linux/input/mt.h> 7 #include <linux/input/touchscreen.h> 8 #include <linux/delay.h> 9 #include <linux/workqueue.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/of_device.h> 12 #include <asm/unaligned.h> 13 14 #define ILI210X_TOUCHES 2 15 #define ILI251X_TOUCHES 10 16 #define DEFAULT_POLL_PERIOD 20 17 18 /* Touchscreen commands */ 19 #define REG_TOUCHDATA 0x10 20 #define REG_PANEL_INFO 0x20 21 #define REG_FIRMWARE_VERSION 0x40 22 #define REG_CALIBRATE 0xcc 23 24 struct firmware_version { 25 u8 id; 26 u8 major; 27 u8 minor; 28 } __packed; 29 30 enum ili2xxx_model { 31 MODEL_ILI210X, 32 MODEL_ILI251X, 33 }; 34 35 struct ili210x { 36 struct i2c_client *client; 37 struct input_dev *input; 38 unsigned int poll_period; 39 struct delayed_work dwork; 40 struct gpio_desc *reset_gpio; 41 struct touchscreen_properties prop; 42 enum ili2xxx_model model; 43 unsigned int max_touches; 44 }; 45 46 static int ili210x_read_reg(struct i2c_client *client, u8 reg, void *buf, 47 size_t len) 48 { 49 struct ili210x *priv = i2c_get_clientdata(client); 50 struct i2c_msg msg[2] = { 51 { 52 .addr = client->addr, 53 .flags = 0, 54 .len = 1, 55 .buf = ®, 56 }, 57 { 58 .addr = client->addr, 59 .flags = I2C_M_RD, 60 .len = len, 61 .buf = buf, 62 } 63 }; 64 65 if (priv->model == MODEL_ILI251X) { 66 if (i2c_transfer(client->adapter, msg, 1) != 1) { 67 dev_err(&client->dev, "i2c transfer failed\n"); 68 return -EIO; 69 } 70 71 usleep_range(5000, 5500); 72 73 if (i2c_transfer(client->adapter, msg + 1, 1) != 1) { 74 dev_err(&client->dev, "i2c transfer failed\n"); 75 return -EIO; 76 } 77 } else { 78 if (i2c_transfer(client->adapter, msg, 2) != 2) { 79 dev_err(&client->dev, "i2c transfer failed\n"); 80 return -EIO; 81 } 82 } 83 84 return 0; 85 } 86 87 static int ili210x_read(struct i2c_client *client, void *buf, size_t len) 88 { 89 struct i2c_msg msg = { 90 .addr = client->addr, 91 .flags = I2C_M_RD, 92 .len = len, 93 .buf = buf, 94 }; 95 96 if (i2c_transfer(client->adapter, &msg, 1) != 1) { 97 dev_err(&client->dev, "i2c transfer failed\n"); 98 return -EIO; 99 } 100 101 return 0; 102 } 103 104 static bool ili210x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata, 105 unsigned int finger, 106 unsigned int *x, unsigned int *y) 107 { 108 if (finger >= ILI210X_TOUCHES) 109 return false; 110 111 if (touchdata[0] & BIT(finger)) 112 return false; 113 114 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0); 115 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2); 116 117 return true; 118 } 119 120 static bool ili251x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata, 121 unsigned int finger, 122 unsigned int *x, unsigned int *y) 123 { 124 if (finger >= ILI251X_TOUCHES) 125 return false; 126 127 *x = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0); 128 if (!(*x & BIT(15))) /* Touch indication */ 129 return false; 130 131 *x &= 0x3fff; 132 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2); 133 134 return true; 135 } 136 137 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata) 138 { 139 struct input_dev *input = priv->input; 140 int i; 141 bool contact = false, touch = false; 142 unsigned int x = 0, y = 0; 143 144 for (i = 0; i < priv->max_touches; i++) { 145 if (priv->model == MODEL_ILI210X) { 146 touch = ili210x_touchdata_to_coords(priv, touchdata, 147 i, &x, &y); 148 } else if (priv->model == MODEL_ILI251X) { 149 touch = ili251x_touchdata_to_coords(priv, touchdata, 150 i, &x, &y); 151 if (touch) 152 contact = true; 153 } 154 155 input_mt_slot(input, i); 156 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch); 157 if (!touch) 158 continue; 159 touchscreen_report_pos(input, &priv->prop, x, y, 160 true); 161 } 162 163 input_mt_report_pointer_emulation(input, false); 164 input_sync(input); 165 166 if (priv->model == MODEL_ILI210X) 167 contact = touchdata[0] & 0xf3; 168 169 return contact; 170 } 171 172 static void ili210x_work(struct work_struct *work) 173 { 174 struct ili210x *priv = container_of(work, struct ili210x, 175 dwork.work); 176 struct i2c_client *client = priv->client; 177 u8 touchdata[64] = { 0 }; 178 bool touch; 179 int error = -EINVAL; 180 181 if (priv->model == MODEL_ILI210X) { 182 error = ili210x_read_reg(client, REG_TOUCHDATA, 183 touchdata, sizeof(touchdata)); 184 } else if (priv->model == MODEL_ILI251X) { 185 error = ili210x_read_reg(client, REG_TOUCHDATA, 186 touchdata, 31); 187 if (!error && touchdata[0] == 2) 188 error = ili210x_read(client, &touchdata[31], 20); 189 } 190 191 if (error) { 192 dev_err(&client->dev, 193 "Unable to get touchdata, err = %d\n", error); 194 return; 195 } 196 197 touch = ili210x_report_events(priv, touchdata); 198 199 if (touch) 200 schedule_delayed_work(&priv->dwork, 201 msecs_to_jiffies(priv->poll_period)); 202 } 203 204 static irqreturn_t ili210x_irq(int irq, void *irq_data) 205 { 206 struct ili210x *priv = irq_data; 207 208 schedule_delayed_work(&priv->dwork, 0); 209 210 return IRQ_HANDLED; 211 } 212 213 static ssize_t ili210x_calibrate(struct device *dev, 214 struct device_attribute *attr, 215 const char *buf, size_t count) 216 { 217 struct i2c_client *client = to_i2c_client(dev); 218 struct ili210x *priv = i2c_get_clientdata(client); 219 unsigned long calibrate; 220 int rc; 221 u8 cmd = REG_CALIBRATE; 222 223 if (kstrtoul(buf, 10, &calibrate)) 224 return -EINVAL; 225 226 if (calibrate > 1) 227 return -EINVAL; 228 229 if (calibrate) { 230 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd)); 231 if (rc != sizeof(cmd)) 232 return -EIO; 233 } 234 235 return count; 236 } 237 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate); 238 239 static struct attribute *ili210x_attributes[] = { 240 &dev_attr_calibrate.attr, 241 NULL, 242 }; 243 244 static const struct attribute_group ili210x_attr_group = { 245 .attrs = ili210x_attributes, 246 }; 247 248 static void ili210x_power_down(void *data) 249 { 250 struct gpio_desc *reset_gpio = data; 251 252 gpiod_set_value_cansleep(reset_gpio, 1); 253 } 254 255 static void ili210x_cancel_work(void *data) 256 { 257 struct ili210x *priv = data; 258 259 cancel_delayed_work_sync(&priv->dwork); 260 } 261 262 static int ili210x_i2c_probe(struct i2c_client *client, 263 const struct i2c_device_id *id) 264 { 265 struct device *dev = &client->dev; 266 struct ili210x *priv; 267 struct gpio_desc *reset_gpio; 268 struct input_dev *input; 269 struct firmware_version firmware; 270 enum ili2xxx_model model; 271 int error; 272 273 model = (enum ili2xxx_model)id->driver_data; 274 275 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver"); 276 277 if (client->irq <= 0) { 278 dev_err(dev, "No IRQ!\n"); 279 return -EINVAL; 280 } 281 282 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 283 if (IS_ERR(reset_gpio)) 284 return PTR_ERR(reset_gpio); 285 286 if (reset_gpio) { 287 error = devm_add_action_or_reset(dev, ili210x_power_down, 288 reset_gpio); 289 if (error) 290 return error; 291 292 usleep_range(50, 100); 293 gpiod_set_value_cansleep(reset_gpio, 0); 294 msleep(100); 295 } 296 297 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 298 if (!priv) 299 return -ENOMEM; 300 301 input = devm_input_allocate_device(dev); 302 if (!input) 303 return -ENOMEM; 304 305 priv->client = client; 306 priv->input = input; 307 priv->poll_period = DEFAULT_POLL_PERIOD; 308 INIT_DELAYED_WORK(&priv->dwork, ili210x_work); 309 priv->reset_gpio = reset_gpio; 310 priv->model = model; 311 if (model == MODEL_ILI210X) 312 priv->max_touches = ILI210X_TOUCHES; 313 if (model == MODEL_ILI251X) 314 priv->max_touches = ILI251X_TOUCHES; 315 316 i2c_set_clientdata(client, priv); 317 318 /* Get firmware version */ 319 error = ili210x_read_reg(client, REG_FIRMWARE_VERSION, 320 &firmware, sizeof(firmware)); 321 if (error) { 322 dev_err(dev, "Failed to get firmware version, err: %d\n", 323 error); 324 return error; 325 } 326 327 /* Setup input device */ 328 input->name = "ILI210x Touchscreen"; 329 input->id.bustype = BUS_I2C; 330 input->dev.parent = dev; 331 332 /* Multi touch */ 333 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 0xffff, 0, 0); 334 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 0xffff, 0, 0); 335 touchscreen_parse_properties(input, true, &priv->prop); 336 input_mt_init_slots(input, priv->max_touches, INPUT_MT_DIRECT); 337 338 error = devm_add_action(dev, ili210x_cancel_work, priv); 339 if (error) 340 return error; 341 342 error = devm_request_irq(dev, client->irq, ili210x_irq, 0, 343 client->name, priv); 344 if (error) { 345 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n", 346 error); 347 return error; 348 } 349 350 error = devm_device_add_group(dev, &ili210x_attr_group); 351 if (error) { 352 dev_err(dev, "Unable to create sysfs attributes, err: %d\n", 353 error); 354 return error; 355 } 356 357 error = input_register_device(priv->input); 358 if (error) { 359 dev_err(dev, "Cannot register input device, err: %d\n", error); 360 return error; 361 } 362 363 device_init_wakeup(dev, 1); 364 365 dev_dbg(dev, 366 "ILI210x initialized (IRQ: %d), firmware version %d.%d.%d", 367 client->irq, firmware.id, firmware.major, firmware.minor); 368 369 return 0; 370 } 371 372 static int __maybe_unused ili210x_i2c_suspend(struct device *dev) 373 { 374 struct i2c_client *client = to_i2c_client(dev); 375 376 if (device_may_wakeup(&client->dev)) 377 enable_irq_wake(client->irq); 378 379 return 0; 380 } 381 382 static int __maybe_unused ili210x_i2c_resume(struct device *dev) 383 { 384 struct i2c_client *client = to_i2c_client(dev); 385 386 if (device_may_wakeup(&client->dev)) 387 disable_irq_wake(client->irq); 388 389 return 0; 390 } 391 392 static SIMPLE_DEV_PM_OPS(ili210x_i2c_pm, 393 ili210x_i2c_suspend, ili210x_i2c_resume); 394 395 static const struct i2c_device_id ili210x_i2c_id[] = { 396 { "ili210x", MODEL_ILI210X }, 397 { "ili251x", MODEL_ILI251X }, 398 { } 399 }; 400 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id); 401 402 static const struct of_device_id ili210x_dt_ids[] = { 403 { .compatible = "ilitek,ili210x", .data = (void *)MODEL_ILI210X }, 404 { .compatible = "ilitek,ili251x", .data = (void *)MODEL_ILI251X }, 405 { }, 406 }; 407 MODULE_DEVICE_TABLE(of, ili210x_dt_ids); 408 409 static struct i2c_driver ili210x_ts_driver = { 410 .driver = { 411 .name = "ili210x_i2c", 412 .pm = &ili210x_i2c_pm, 413 .of_match_table = ili210x_dt_ids, 414 }, 415 .id_table = ili210x_i2c_id, 416 .probe = ili210x_i2c_probe, 417 }; 418 419 module_i2c_driver(ili210x_ts_driver); 420 421 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); 422 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver"); 423 MODULE_LICENSE("GPL"); 424