109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 271f8e38aSDmitry Torokhov #include <linux/delay.h> 371f8e38aSDmitry Torokhov #include <linux/gpio/consumer.h> 45c6a7a62SOlivier Sobrie #include <linux/i2c.h> 55c6a7a62SOlivier Sobrie #include <linux/input.h> 65c6a7a62SOlivier Sobrie #include <linux/input/mt.h> 7f67cc3e9SMarek Vasut #include <linux/input/touchscreen.h> 871f8e38aSDmitry Torokhov #include <linux/interrupt.h> 971f8e38aSDmitry Torokhov #include <linux/module.h> 1049588917SMarek Vasut #include <linux/of_device.h> 11b32fbeaeSSven Van Asbroeck #include <linux/sizes.h> 1271f8e38aSDmitry Torokhov #include <linux/slab.h> 13e3559442SMarek Vasut #include <asm/unaligned.h> 145c6a7a62SOlivier Sobrie 1571f8e38aSDmitry Torokhov #define ILI2XXX_POLL_PERIOD 20 165c6a7a62SOlivier Sobrie 17ef536abdSDmitry Torokhov #define ILI210X_DATA_SIZE 64 18ef536abdSDmitry Torokhov #define ILI211X_DATA_SIZE 43 19ef536abdSDmitry Torokhov #define ILI251X_DATA_SIZE1 31 20ef536abdSDmitry Torokhov #define ILI251X_DATA_SIZE2 20 21ef536abdSDmitry Torokhov 225c6a7a62SOlivier Sobrie /* Touchscreen commands */ 235c6a7a62SOlivier Sobrie #define REG_TOUCHDATA 0x10 245c6a7a62SOlivier Sobrie #define REG_PANEL_INFO 0x20 255c6a7a62SOlivier Sobrie #define REG_CALIBRATE 0xcc 265c6a7a62SOlivier Sobrie 27ef536abdSDmitry Torokhov struct ili2xxx_chip { 28ef536abdSDmitry Torokhov int (*read_reg)(struct i2c_client *client, u8 reg, 29ef536abdSDmitry Torokhov void *buf, size_t len); 30ef536abdSDmitry Torokhov int (*get_touch_data)(struct i2c_client *client, u8 *data); 31ef536abdSDmitry Torokhov bool (*parse_touch_data)(const u8 *data, unsigned int finger, 32*60159e9eSMarek Vasut unsigned int *x, unsigned int *y, 33*60159e9eSMarek Vasut unsigned int *z); 34ef536abdSDmitry Torokhov bool (*continue_polling)(const u8 *data, bool touch); 35ef536abdSDmitry Torokhov unsigned int max_touches; 36b32fbeaeSSven Van Asbroeck unsigned int resolution; 37cc12ba18SSven Van Asbroeck bool has_calibrate_reg; 38*60159e9eSMarek Vasut bool has_pressure_reg; 3949588917SMarek Vasut }; 4049588917SMarek Vasut 415c6a7a62SOlivier Sobrie struct ili210x { 425c6a7a62SOlivier Sobrie struct i2c_client *client; 435c6a7a62SOlivier Sobrie struct input_dev *input; 44201f3c80SMarek Vasut struct gpio_desc *reset_gpio; 45f67cc3e9SMarek Vasut struct touchscreen_properties prop; 46ef536abdSDmitry Torokhov const struct ili2xxx_chip *chip; 4771f8e38aSDmitry Torokhov bool stop; 485c6a7a62SOlivier Sobrie }; 495c6a7a62SOlivier Sobrie 50ef536abdSDmitry Torokhov static int ili210x_read_reg(struct i2c_client *client, 51ef536abdSDmitry Torokhov u8 reg, void *buf, size_t len) 525c6a7a62SOlivier Sobrie { 53ef536abdSDmitry Torokhov struct i2c_msg msg[] = { 545c6a7a62SOlivier Sobrie { 555c6a7a62SOlivier Sobrie .addr = client->addr, 565c6a7a62SOlivier Sobrie .flags = 0, 575c6a7a62SOlivier Sobrie .len = 1, 585c6a7a62SOlivier Sobrie .buf = ®, 595c6a7a62SOlivier Sobrie }, 605c6a7a62SOlivier Sobrie { 615c6a7a62SOlivier Sobrie .addr = client->addr, 625c6a7a62SOlivier Sobrie .flags = I2C_M_RD, 635c6a7a62SOlivier Sobrie .len = len, 645c6a7a62SOlivier Sobrie .buf = buf, 655c6a7a62SOlivier Sobrie } 665c6a7a62SOlivier Sobrie }; 67ef536abdSDmitry Torokhov int error, ret; 685c6a7a62SOlivier Sobrie 69ef536abdSDmitry Torokhov ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 70ef536abdSDmitry Torokhov if (ret != ARRAY_SIZE(msg)) { 71ef536abdSDmitry Torokhov error = ret < 0 ? ret : -EIO; 72ef536abdSDmitry Torokhov dev_err(&client->dev, "%s failed: %d\n", __func__, error); 73ef536abdSDmitry Torokhov return error; 7449588917SMarek Vasut } 7549588917SMarek Vasut 7649588917SMarek Vasut return 0; 7749588917SMarek Vasut } 7849588917SMarek Vasut 79ef536abdSDmitry Torokhov static int ili210x_read_touch_data(struct i2c_client *client, u8 *data) 8049588917SMarek Vasut { 81ef536abdSDmitry Torokhov return ili210x_read_reg(client, REG_TOUCHDATA, 82ef536abdSDmitry Torokhov data, ILI210X_DATA_SIZE); 8349588917SMarek Vasut } 845c6a7a62SOlivier Sobrie 85ef536abdSDmitry Torokhov static bool ili210x_touchdata_to_coords(const u8 *touchdata, 86e3559442SMarek Vasut unsigned int finger, 87*60159e9eSMarek Vasut unsigned int *x, unsigned int *y, 88*60159e9eSMarek Vasut unsigned int *z) 895c6a7a62SOlivier Sobrie { 90e3559442SMarek Vasut if (touchdata[0] & BIT(finger)) 91e3559442SMarek Vasut return false; 92e3559442SMarek Vasut 93e3559442SMarek Vasut *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0); 94e3559442SMarek Vasut *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2); 95e3559442SMarek Vasut 96e3559442SMarek Vasut return true; 97e3559442SMarek Vasut } 98e3559442SMarek Vasut 99ef536abdSDmitry Torokhov static bool ili210x_check_continue_polling(const u8 *data, bool touch) 100ef536abdSDmitry Torokhov { 101ef536abdSDmitry Torokhov return data[0] & 0xf3; 102ef536abdSDmitry Torokhov } 103ef536abdSDmitry Torokhov 104ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili210x_chip = { 105ef536abdSDmitry Torokhov .read_reg = ili210x_read_reg, 106ef536abdSDmitry Torokhov .get_touch_data = ili210x_read_touch_data, 107ef536abdSDmitry Torokhov .parse_touch_data = ili210x_touchdata_to_coords, 108ef536abdSDmitry Torokhov .continue_polling = ili210x_check_continue_polling, 109ef536abdSDmitry Torokhov .max_touches = 2, 110cc12ba18SSven Van Asbroeck .has_calibrate_reg = true, 111ef536abdSDmitry Torokhov }; 112ef536abdSDmitry Torokhov 113ef536abdSDmitry Torokhov static int ili211x_read_touch_data(struct i2c_client *client, u8 *data) 114ef536abdSDmitry Torokhov { 115ef536abdSDmitry Torokhov s16 sum = 0; 116ef536abdSDmitry Torokhov int error; 117ef536abdSDmitry Torokhov int ret; 118ef536abdSDmitry Torokhov int i; 119ef536abdSDmitry Torokhov 120ef536abdSDmitry Torokhov ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE); 121ef536abdSDmitry Torokhov if (ret != ILI211X_DATA_SIZE) { 122ef536abdSDmitry Torokhov error = ret < 0 ? ret : -EIO; 123ef536abdSDmitry Torokhov dev_err(&client->dev, "%s failed: %d\n", __func__, error); 124ef536abdSDmitry Torokhov return error; 125ef536abdSDmitry Torokhov } 126ef536abdSDmitry Torokhov 127ef536abdSDmitry Torokhov /* This chip uses custom checksum at the end of data */ 128ef536abdSDmitry Torokhov for (i = 0; i < ILI211X_DATA_SIZE - 1; i++) 129ef536abdSDmitry Torokhov sum = (sum + data[i]) & 0xff; 130ef536abdSDmitry Torokhov 131ef536abdSDmitry Torokhov if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) { 132ef536abdSDmitry Torokhov dev_err(&client->dev, 133ef536abdSDmitry Torokhov "CRC error (crc=0x%02x expected=0x%02x)\n", 134ef536abdSDmitry Torokhov sum, data[ILI211X_DATA_SIZE - 1]); 135ef536abdSDmitry Torokhov return -EIO; 136ef536abdSDmitry Torokhov } 137ef536abdSDmitry Torokhov 138ef536abdSDmitry Torokhov return 0; 139ef536abdSDmitry Torokhov } 140ef536abdSDmitry Torokhov 141ef536abdSDmitry Torokhov static bool ili211x_touchdata_to_coords(const u8 *touchdata, 142eb91ecc9SMarek Vasut unsigned int finger, 143*60159e9eSMarek Vasut unsigned int *x, unsigned int *y, 144*60159e9eSMarek Vasut unsigned int *z) 145eb91ecc9SMarek Vasut { 146eb91ecc9SMarek Vasut u32 data; 147eb91ecc9SMarek Vasut 148eb91ecc9SMarek Vasut data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0); 149eb91ecc9SMarek Vasut if (data == 0xffffffff) /* Finger up */ 150eb91ecc9SMarek Vasut return false; 151eb91ecc9SMarek Vasut 152eb91ecc9SMarek Vasut *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) | 153eb91ecc9SMarek Vasut touchdata[1 + (finger * 4) + 1]; 154eb91ecc9SMarek Vasut *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) | 155eb91ecc9SMarek Vasut touchdata[1 + (finger * 4) + 2]; 156eb91ecc9SMarek Vasut 157eb91ecc9SMarek Vasut return true; 158eb91ecc9SMarek Vasut } 159eb91ecc9SMarek Vasut 160ef536abdSDmitry Torokhov static bool ili211x_decline_polling(const u8 *data, bool touch) 161ef536abdSDmitry Torokhov { 162ef536abdSDmitry Torokhov return false; 163ef536abdSDmitry Torokhov } 164ef536abdSDmitry Torokhov 165ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili211x_chip = { 166ef536abdSDmitry Torokhov .read_reg = ili210x_read_reg, 167ef536abdSDmitry Torokhov .get_touch_data = ili211x_read_touch_data, 168ef536abdSDmitry Torokhov .parse_touch_data = ili211x_touchdata_to_coords, 169ef536abdSDmitry Torokhov .continue_polling = ili211x_decline_polling, 170ef536abdSDmitry Torokhov .max_touches = 10, 171b32fbeaeSSven Van Asbroeck .resolution = 2048, 172ef536abdSDmitry Torokhov }; 173ef536abdSDmitry Torokhov 174d0c5e7d4SLuca Weiss static bool ili212x_touchdata_to_coords(const u8 *touchdata, 175d0c5e7d4SLuca Weiss unsigned int finger, 176*60159e9eSMarek Vasut unsigned int *x, unsigned int *y, 177*60159e9eSMarek Vasut unsigned int *z) 178d0c5e7d4SLuca Weiss { 179d0c5e7d4SLuca Weiss u16 val; 180d0c5e7d4SLuca Weiss 181d0c5e7d4SLuca Weiss val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0); 182d0c5e7d4SLuca Weiss if (!(val & BIT(15))) /* Touch indication */ 183d0c5e7d4SLuca Weiss return false; 184d0c5e7d4SLuca Weiss 185d0c5e7d4SLuca Weiss *x = val & 0x3fff; 186d0c5e7d4SLuca Weiss *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2); 187d0c5e7d4SLuca Weiss 188d0c5e7d4SLuca Weiss return true; 189d0c5e7d4SLuca Weiss } 190d0c5e7d4SLuca Weiss 191d0c5e7d4SLuca Weiss static bool ili212x_check_continue_polling(const u8 *data, bool touch) 192d0c5e7d4SLuca Weiss { 193d0c5e7d4SLuca Weiss return touch; 194d0c5e7d4SLuca Weiss } 195d0c5e7d4SLuca Weiss 196d0c5e7d4SLuca Weiss static const struct ili2xxx_chip ili212x_chip = { 197d0c5e7d4SLuca Weiss .read_reg = ili210x_read_reg, 198d0c5e7d4SLuca Weiss .get_touch_data = ili210x_read_touch_data, 199d0c5e7d4SLuca Weiss .parse_touch_data = ili212x_touchdata_to_coords, 200d0c5e7d4SLuca Weiss .continue_polling = ili212x_check_continue_polling, 201d0c5e7d4SLuca Weiss .max_touches = 10, 202d0c5e7d4SLuca Weiss .has_calibrate_reg = true, 203d0c5e7d4SLuca Weiss }; 204d0c5e7d4SLuca Weiss 205ef536abdSDmitry Torokhov static int ili251x_read_reg(struct i2c_client *client, 206ef536abdSDmitry Torokhov u8 reg, void *buf, size_t len) 207ef536abdSDmitry Torokhov { 208ef536abdSDmitry Torokhov int error; 209ef536abdSDmitry Torokhov int ret; 210ef536abdSDmitry Torokhov 211ef536abdSDmitry Torokhov ret = i2c_master_send(client, ®, 1); 212ef536abdSDmitry Torokhov if (ret == 1) { 213ef536abdSDmitry Torokhov usleep_range(5000, 5500); 214ef536abdSDmitry Torokhov 215ef536abdSDmitry Torokhov ret = i2c_master_recv(client, buf, len); 216ef536abdSDmitry Torokhov if (ret == len) 217ef536abdSDmitry Torokhov return 0; 218ef536abdSDmitry Torokhov } 219ef536abdSDmitry Torokhov 220ef536abdSDmitry Torokhov error = ret < 0 ? ret : -EIO; 221ef536abdSDmitry Torokhov dev_err(&client->dev, "%s failed: %d\n", __func__, error); 222ef536abdSDmitry Torokhov return ret; 223ef536abdSDmitry Torokhov } 224ef536abdSDmitry Torokhov 225ef536abdSDmitry Torokhov static int ili251x_read_touch_data(struct i2c_client *client, u8 *data) 226ef536abdSDmitry Torokhov { 227ef536abdSDmitry Torokhov int error; 228ef536abdSDmitry Torokhov 229ef536abdSDmitry Torokhov error = ili251x_read_reg(client, REG_TOUCHDATA, 230ef536abdSDmitry Torokhov data, ILI251X_DATA_SIZE1); 231ef536abdSDmitry Torokhov if (!error && data[0] == 2) { 232ef536abdSDmitry Torokhov error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1, 233ef536abdSDmitry Torokhov ILI251X_DATA_SIZE2); 234ef536abdSDmitry Torokhov if (error >= 0 && error != ILI251X_DATA_SIZE2) 235ef536abdSDmitry Torokhov error = -EIO; 236ef536abdSDmitry Torokhov } 237ef536abdSDmitry Torokhov 238ef536abdSDmitry Torokhov return error; 239ef536abdSDmitry Torokhov } 240ef536abdSDmitry Torokhov 241ef536abdSDmitry Torokhov static bool ili251x_touchdata_to_coords(const u8 *touchdata, 24249588917SMarek Vasut unsigned int finger, 243*60159e9eSMarek Vasut unsigned int *x, unsigned int *y, 244*60159e9eSMarek Vasut unsigned int *z) 24549588917SMarek Vasut { 246ef536abdSDmitry Torokhov u16 val; 247ef536abdSDmitry Torokhov 248ef536abdSDmitry Torokhov val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0); 249ef536abdSDmitry Torokhov if (!(val & BIT(15))) /* Touch indication */ 25049588917SMarek Vasut return false; 25149588917SMarek Vasut 252ef536abdSDmitry Torokhov *x = val & 0x3fff; 25349588917SMarek Vasut *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2); 254*60159e9eSMarek Vasut *z = touchdata[1 + (finger * 5) + 4]; 25549588917SMarek Vasut 25649588917SMarek Vasut return true; 25749588917SMarek Vasut } 25849588917SMarek Vasut 259ef536abdSDmitry Torokhov static bool ili251x_check_continue_polling(const u8 *data, bool touch) 260ef536abdSDmitry Torokhov { 261ef536abdSDmitry Torokhov return touch; 262ef536abdSDmitry Torokhov } 263ef536abdSDmitry Torokhov 264ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili251x_chip = { 265ef536abdSDmitry Torokhov .read_reg = ili251x_read_reg, 266ef536abdSDmitry Torokhov .get_touch_data = ili251x_read_touch_data, 267ef536abdSDmitry Torokhov .parse_touch_data = ili251x_touchdata_to_coords, 268ef536abdSDmitry Torokhov .continue_polling = ili251x_check_continue_polling, 269ef536abdSDmitry Torokhov .max_touches = 10, 270cc12ba18SSven Van Asbroeck .has_calibrate_reg = true, 271*60159e9eSMarek Vasut .has_pressure_reg = true, 272ef536abdSDmitry Torokhov }; 273ef536abdSDmitry Torokhov 274e3559442SMarek Vasut static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata) 275e3559442SMarek Vasut { 276e3559442SMarek Vasut struct input_dev *input = priv->input; 2775c6a7a62SOlivier Sobrie int i; 278ef536abdSDmitry Torokhov bool contact = false, touch; 279*60159e9eSMarek Vasut unsigned int x = 0, y = 0, z = 0; 2805c6a7a62SOlivier Sobrie 281ef536abdSDmitry Torokhov for (i = 0; i < priv->chip->max_touches; i++) { 282*60159e9eSMarek Vasut touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z); 28349588917SMarek Vasut 284f67cc3e9SMarek Vasut input_mt_slot(input, i); 285ef536abdSDmitry Torokhov if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) { 286ef536abdSDmitry Torokhov touchscreen_report_pos(input, &priv->prop, x, y, true); 287*60159e9eSMarek Vasut if (priv->chip->has_pressure_reg) 288*60159e9eSMarek Vasut input_report_abs(input, ABS_MT_PRESSURE, z); 289ef536abdSDmitry Torokhov contact = true; 290ef536abdSDmitry Torokhov } 2915c6a7a62SOlivier Sobrie } 2925c6a7a62SOlivier Sobrie 2935c6a7a62SOlivier Sobrie input_mt_report_pointer_emulation(input, false); 2945c6a7a62SOlivier Sobrie input_sync(input); 295e3559442SMarek Vasut 29649588917SMarek Vasut return contact; 2975c6a7a62SOlivier Sobrie } 2985c6a7a62SOlivier Sobrie 29971f8e38aSDmitry Torokhov static irqreturn_t ili210x_irq(int irq, void *irq_data) 3005c6a7a62SOlivier Sobrie { 30171f8e38aSDmitry Torokhov struct ili210x *priv = irq_data; 3025c6a7a62SOlivier Sobrie struct i2c_client *client = priv->client; 303ef536abdSDmitry Torokhov const struct ili2xxx_chip *chip = priv->chip; 304ef536abdSDmitry Torokhov u8 touchdata[ILI210X_DATA_SIZE] = { 0 }; 305ef536abdSDmitry Torokhov bool keep_polling; 306e3559442SMarek Vasut bool touch; 30771f8e38aSDmitry Torokhov int error; 3085c6a7a62SOlivier Sobrie 30971f8e38aSDmitry Torokhov do { 310ef536abdSDmitry Torokhov error = chip->get_touch_data(client, touchdata); 3115c6a7a62SOlivier Sobrie if (error) { 3125c6a7a62SOlivier Sobrie dev_err(&client->dev, 313ef536abdSDmitry Torokhov "Unable to get touch data: %d\n", error); 31471f8e38aSDmitry Torokhov break; 3155c6a7a62SOlivier Sobrie } 3165c6a7a62SOlivier Sobrie 317e3559442SMarek Vasut touch = ili210x_report_events(priv, touchdata); 318ef536abdSDmitry Torokhov keep_polling = chip->continue_polling(touchdata, touch); 319ef536abdSDmitry Torokhov if (keep_polling) 32071f8e38aSDmitry Torokhov msleep(ILI2XXX_POLL_PERIOD); 321ef536abdSDmitry Torokhov } while (!priv->stop && keep_polling); 3225c6a7a62SOlivier Sobrie 3235c6a7a62SOlivier Sobrie return IRQ_HANDLED; 3245c6a7a62SOlivier Sobrie } 3255c6a7a62SOlivier Sobrie 3265c6a7a62SOlivier Sobrie static ssize_t ili210x_calibrate(struct device *dev, 3275c6a7a62SOlivier Sobrie struct device_attribute *attr, 3285c6a7a62SOlivier Sobrie const char *buf, size_t count) 3295c6a7a62SOlivier Sobrie { 3305c6a7a62SOlivier Sobrie struct i2c_client *client = to_i2c_client(dev); 3315c6a7a62SOlivier Sobrie struct ili210x *priv = i2c_get_clientdata(client); 3325c6a7a62SOlivier Sobrie unsigned long calibrate; 3335c6a7a62SOlivier Sobrie int rc; 3345c6a7a62SOlivier Sobrie u8 cmd = REG_CALIBRATE; 3355c6a7a62SOlivier Sobrie 3365c6a7a62SOlivier Sobrie if (kstrtoul(buf, 10, &calibrate)) 3375c6a7a62SOlivier Sobrie return -EINVAL; 3385c6a7a62SOlivier Sobrie 3395c6a7a62SOlivier Sobrie if (calibrate > 1) 3405c6a7a62SOlivier Sobrie return -EINVAL; 3415c6a7a62SOlivier Sobrie 3425c6a7a62SOlivier Sobrie if (calibrate) { 3435c6a7a62SOlivier Sobrie rc = i2c_master_send(priv->client, &cmd, sizeof(cmd)); 3445c6a7a62SOlivier Sobrie if (rc != sizeof(cmd)) 3455c6a7a62SOlivier Sobrie return -EIO; 3465c6a7a62SOlivier Sobrie } 3475c6a7a62SOlivier Sobrie 3485c6a7a62SOlivier Sobrie return count; 3495c6a7a62SOlivier Sobrie } 350b27c0d0cSDmitry Torokhov static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate); 3515c6a7a62SOlivier Sobrie 3525c6a7a62SOlivier Sobrie static struct attribute *ili210x_attributes[] = { 3535c6a7a62SOlivier Sobrie &dev_attr_calibrate.attr, 3545c6a7a62SOlivier Sobrie NULL, 3555c6a7a62SOlivier Sobrie }; 3565c6a7a62SOlivier Sobrie 357cc12ba18SSven Van Asbroeck static umode_t ili210x_calibrate_visible(struct kobject *kobj, 358cc12ba18SSven Van Asbroeck struct attribute *attr, int index) 359cc12ba18SSven Van Asbroeck { 360cc12ba18SSven Van Asbroeck struct device *dev = kobj_to_dev(kobj); 361cc12ba18SSven Van Asbroeck struct i2c_client *client = to_i2c_client(dev); 362cc12ba18SSven Van Asbroeck struct ili210x *priv = i2c_get_clientdata(client); 363cc12ba18SSven Van Asbroeck 364fbd1ec00SLuca Weiss return priv->chip->has_calibrate_reg ? attr->mode : 0; 365cc12ba18SSven Van Asbroeck } 366cc12ba18SSven Van Asbroeck 3675c6a7a62SOlivier Sobrie static const struct attribute_group ili210x_attr_group = { 3685c6a7a62SOlivier Sobrie .attrs = ili210x_attributes, 369cc12ba18SSven Van Asbroeck .is_visible = ili210x_calibrate_visible, 3705c6a7a62SOlivier Sobrie }; 3715c6a7a62SOlivier Sobrie 372201f3c80SMarek Vasut static void ili210x_power_down(void *data) 373201f3c80SMarek Vasut { 374201f3c80SMarek Vasut struct gpio_desc *reset_gpio = data; 375201f3c80SMarek Vasut 376201f3c80SMarek Vasut gpiod_set_value_cansleep(reset_gpio, 1); 377201f3c80SMarek Vasut } 378201f3c80SMarek Vasut 37971f8e38aSDmitry Torokhov static void ili210x_stop(void *data) 3801bdec5d9SMarek Vasut { 3811bdec5d9SMarek Vasut struct ili210x *priv = data; 3821bdec5d9SMarek Vasut 38371f8e38aSDmitry Torokhov /* Tell ISR to quit even if there is a contact. */ 38471f8e38aSDmitry Torokhov priv->stop = true; 3851bdec5d9SMarek Vasut } 3861bdec5d9SMarek Vasut 3875298cc4cSBill Pemberton static int ili210x_i2c_probe(struct i2c_client *client, 3885c6a7a62SOlivier Sobrie const struct i2c_device_id *id) 3895c6a7a62SOlivier Sobrie { 3905c6a7a62SOlivier Sobrie struct device *dev = &client->dev; 391ef536abdSDmitry Torokhov const struct ili2xxx_chip *chip; 3925c6a7a62SOlivier Sobrie struct ili210x *priv; 393201f3c80SMarek Vasut struct gpio_desc *reset_gpio; 3945c6a7a62SOlivier Sobrie struct input_dev *input; 3955c6a7a62SOlivier Sobrie int error; 396b32fbeaeSSven Van Asbroeck unsigned int max_xy; 3975c6a7a62SOlivier Sobrie 3985c6a7a62SOlivier Sobrie dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver"); 3995c6a7a62SOlivier Sobrie 400ef536abdSDmitry Torokhov chip = device_get_match_data(dev); 401ef536abdSDmitry Torokhov if (!chip && id) 402ef536abdSDmitry Torokhov chip = (const struct ili2xxx_chip *)id->driver_data; 403ef536abdSDmitry Torokhov if (!chip) { 404ef536abdSDmitry Torokhov dev_err(&client->dev, "unknown device model\n"); 405ef536abdSDmitry Torokhov return -ENODEV; 406ef536abdSDmitry Torokhov } 407ef536abdSDmitry Torokhov 4085c6a7a62SOlivier Sobrie if (client->irq <= 0) { 4095c6a7a62SOlivier Sobrie dev_err(dev, "No IRQ!\n"); 4105c6a7a62SOlivier Sobrie return -EINVAL; 4115c6a7a62SOlivier Sobrie } 4125c6a7a62SOlivier Sobrie 413201f3c80SMarek Vasut reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 414201f3c80SMarek Vasut if (IS_ERR(reset_gpio)) 415201f3c80SMarek Vasut return PTR_ERR(reset_gpio); 416201f3c80SMarek Vasut 417201f3c80SMarek Vasut if (reset_gpio) { 418201f3c80SMarek Vasut error = devm_add_action_or_reset(dev, ili210x_power_down, 419201f3c80SMarek Vasut reset_gpio); 420201f3c80SMarek Vasut if (error) 421201f3c80SMarek Vasut return error; 422201f3c80SMarek Vasut 423201f3c80SMarek Vasut usleep_range(50, 100); 424201f3c80SMarek Vasut gpiod_set_value_cansleep(reset_gpio, 0); 425201f3c80SMarek Vasut msleep(100); 426201f3c80SMarek Vasut } 427201f3c80SMarek Vasut 42812294577SMarek Vasut priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 42912294577SMarek Vasut if (!priv) 43012294577SMarek Vasut return -ENOMEM; 43112294577SMarek Vasut 43212294577SMarek Vasut input = devm_input_allocate_device(dev); 43312294577SMarek Vasut if (!input) 43412294577SMarek Vasut return -ENOMEM; 43512294577SMarek Vasut 43612294577SMarek Vasut priv->client = client; 43712294577SMarek Vasut priv->input = input; 43812294577SMarek Vasut priv->reset_gpio = reset_gpio; 439ef536abdSDmitry Torokhov priv->chip = chip; 44012294577SMarek Vasut i2c_set_clientdata(client, priv); 44112294577SMarek Vasut 4425c6a7a62SOlivier Sobrie /* Setup input device */ 4435c6a7a62SOlivier Sobrie input->name = "ILI210x Touchscreen"; 4445c6a7a62SOlivier Sobrie input->id.bustype = BUS_I2C; 4455c6a7a62SOlivier Sobrie 4465c6a7a62SOlivier Sobrie /* Multi touch */ 447b32fbeaeSSven Van Asbroeck max_xy = (chip->resolution ?: SZ_64K) - 1; 448b32fbeaeSSven Van Asbroeck input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0); 449b32fbeaeSSven Van Asbroeck input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0); 450*60159e9eSMarek Vasut if (priv->chip->has_pressure_reg) 451*60159e9eSMarek Vasut input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0); 452f67cc3e9SMarek Vasut touchscreen_parse_properties(input, true, &priv->prop); 45343f06a4cSDmitry Torokhov 454ef536abdSDmitry Torokhov error = input_mt_init_slots(input, priv->chip->max_touches, 455ef536abdSDmitry Torokhov INPUT_MT_DIRECT); 45643f06a4cSDmitry Torokhov if (error) { 45743f06a4cSDmitry Torokhov dev_err(dev, "Unable to set up slots, err: %d\n", error); 45843f06a4cSDmitry Torokhov return error; 45943f06a4cSDmitry Torokhov } 4605c6a7a62SOlivier Sobrie 46171f8e38aSDmitry Torokhov error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq, 46271f8e38aSDmitry Torokhov IRQF_ONESHOT, client->name, priv); 4635c6a7a62SOlivier Sobrie if (error) { 4645c6a7a62SOlivier Sobrie dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n", 4655c6a7a62SOlivier Sobrie error); 4661bdec5d9SMarek Vasut return error; 4675c6a7a62SOlivier Sobrie } 4685c6a7a62SOlivier Sobrie 46971f8e38aSDmitry Torokhov error = devm_add_action_or_reset(dev, ili210x_stop, priv); 47071f8e38aSDmitry Torokhov if (error) 47171f8e38aSDmitry Torokhov return error; 47271f8e38aSDmitry Torokhov 473576057bfSDmitry Torokhov error = devm_device_add_group(dev, &ili210x_attr_group); 4745c6a7a62SOlivier Sobrie if (error) { 4755c6a7a62SOlivier Sobrie dev_err(dev, "Unable to create sysfs attributes, err: %d\n", 4765c6a7a62SOlivier Sobrie error); 4771bdec5d9SMarek Vasut return error; 4785c6a7a62SOlivier Sobrie } 4795c6a7a62SOlivier Sobrie 4805c6a7a62SOlivier Sobrie error = input_register_device(priv->input); 4815c6a7a62SOlivier Sobrie if (error) { 482971bd8faSMasanari Iida dev_err(dev, "Cannot register input device, err: %d\n", error); 483576057bfSDmitry Torokhov return error; 4845c6a7a62SOlivier Sobrie } 4855c6a7a62SOlivier Sobrie 4865c6a7a62SOlivier Sobrie return 0; 4875c6a7a62SOlivier Sobrie } 4885c6a7a62SOlivier Sobrie 4895c6a7a62SOlivier Sobrie static const struct i2c_device_id ili210x_i2c_id[] = { 490ef536abdSDmitry Torokhov { "ili210x", (long)&ili210x_chip }, 491ef536abdSDmitry Torokhov { "ili2117", (long)&ili211x_chip }, 492d0c5e7d4SLuca Weiss { "ili2120", (long)&ili212x_chip }, 493ef536abdSDmitry Torokhov { "ili251x", (long)&ili251x_chip }, 4945c6a7a62SOlivier Sobrie { } 4955c6a7a62SOlivier Sobrie }; 4965c6a7a62SOlivier Sobrie MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id); 4975c6a7a62SOlivier Sobrie 498c5d0e4b5SMarek Vasut static const struct of_device_id ili210x_dt_ids[] = { 499ef536abdSDmitry Torokhov { .compatible = "ilitek,ili210x", .data = &ili210x_chip }, 500ef536abdSDmitry Torokhov { .compatible = "ilitek,ili2117", .data = &ili211x_chip }, 501d0c5e7d4SLuca Weiss { .compatible = "ilitek,ili2120", .data = &ili212x_chip }, 502ef536abdSDmitry Torokhov { .compatible = "ilitek,ili251x", .data = &ili251x_chip }, 503ef536abdSDmitry Torokhov { } 504c5d0e4b5SMarek Vasut }; 505c5d0e4b5SMarek Vasut MODULE_DEVICE_TABLE(of, ili210x_dt_ids); 506c5d0e4b5SMarek Vasut 5075c6a7a62SOlivier Sobrie static struct i2c_driver ili210x_ts_driver = { 5085c6a7a62SOlivier Sobrie .driver = { 5095c6a7a62SOlivier Sobrie .name = "ili210x_i2c", 510c5d0e4b5SMarek Vasut .of_match_table = ili210x_dt_ids, 5115c6a7a62SOlivier Sobrie }, 5125c6a7a62SOlivier Sobrie .id_table = ili210x_i2c_id, 5135c6a7a62SOlivier Sobrie .probe = ili210x_i2c_probe, 5145c6a7a62SOlivier Sobrie }; 5155c6a7a62SOlivier Sobrie 5165c6a7a62SOlivier Sobrie module_i2c_driver(ili210x_ts_driver); 5175c6a7a62SOlivier Sobrie 5185c6a7a62SOlivier Sobrie MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); 5195c6a7a62SOlivier Sobrie MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver"); 5205c6a7a62SOlivier Sobrie MODULE_LICENSE("GPL"); 521