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