Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Driver for ChipOne icn8505 i2c touchscreen controller
0004  *
0005  * Copyright (c) 2015-2018 Red Hat Inc.
0006  *
0007  * Red Hat authors:
0008  * Hans de Goede <hdegoede@redhat.com>
0009  */
0010 
0011 #include <asm/unaligned.h>
0012 #include <linux/acpi.h>
0013 #include <linux/crc32.h>
0014 #include <linux/delay.h>
0015 #include <linux/firmware.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/i2c.h>
0018 #include <linux/input.h>
0019 #include <linux/input/mt.h>
0020 #include <linux/input/touchscreen.h>
0021 #include <linux/module.h>
0022 
0023 /* Normal operation mode defines */
0024 #define ICN8505_REG_ADDR_WIDTH      16
0025 
0026 #define ICN8505_REG_POWER       0x0004
0027 #define ICN8505_REG_TOUCHDATA       0x1000
0028 #define ICN8505_REG_CONFIGDATA      0x8000
0029 
0030 /* ICN8505_REG_POWER commands */
0031 #define ICN8505_POWER_ACTIVE        0x00
0032 #define ICN8505_POWER_MONITOR       0x01
0033 #define ICN8505_POWER_HIBERNATE     0x02
0034 /*
0035  * The Android driver uses these to turn on/off the charger filter, but the
0036  * filter is way too aggressive making e.g. onscreen keyboards unusable.
0037  */
0038 #define ICN8505_POWER_ENA_CHARGER_MODE  0x55
0039 #define ICN8505_POWER_DIS_CHARGER_MODE  0x66
0040 
0041 #define ICN8505_MAX_TOUCHES     10
0042 
0043 /* Programming mode defines */
0044 #define ICN8505_PROG_I2C_ADDR       0x30
0045 #define ICN8505_PROG_REG_ADDR_WIDTH 24
0046 
0047 #define MAX_FW_UPLOAD_TRIES     3
0048 
0049 struct icn8505_touch {
0050     u8 slot;
0051     u8 x[2];
0052     u8 y[2];
0053     u8 pressure;    /* Seems more like finger width then pressure really */
0054     u8 event;
0055 /* The difference between 2 and 3 is unclear */
0056 #define ICN8505_EVENT_NO_DATA   1 /* No finger seen yet since wakeup */
0057 #define ICN8505_EVENT_UPDATE1   2 /* New or updated coordinates */
0058 #define ICN8505_EVENT_UPDATE2   3 /* New or updated coordinates */
0059 #define ICN8505_EVENT_END   4 /* Finger lifted */
0060 } __packed;
0061 
0062 struct icn8505_touch_data {
0063     u8 softbutton;
0064     u8 touch_count;
0065     struct icn8505_touch touches[ICN8505_MAX_TOUCHES];
0066 } __packed;
0067 
0068 struct icn8505_data {
0069     struct i2c_client *client;
0070     struct input_dev *input;
0071     struct gpio_desc *wake_gpio;
0072     struct touchscreen_properties prop;
0073     char firmware_name[32];
0074 };
0075 
0076 static int icn8505_read_xfer(struct i2c_client *client, u16 i2c_addr,
0077                  int reg_addr, int reg_addr_width,
0078                  void *data, int len, bool silent)
0079 {
0080     u8 buf[3];
0081     int i, ret;
0082     struct i2c_msg msg[2] = {
0083         {
0084             .addr = i2c_addr,
0085             .buf = buf,
0086             .len = reg_addr_width / 8,
0087         },
0088         {
0089             .addr = i2c_addr,
0090             .flags = I2C_M_RD,
0091             .buf = data,
0092             .len = len,
0093         }
0094     };
0095 
0096     for (i = 0; i < (reg_addr_width / 8); i++)
0097         buf[i] = (reg_addr >> (reg_addr_width - (i + 1) * 8)) & 0xff;
0098 
0099     ret = i2c_transfer(client->adapter, msg, 2);
0100     if (ret != ARRAY_SIZE(msg)) {
0101         if (ret >= 0)
0102             ret = -EIO;
0103         if (!silent)
0104             dev_err(&client->dev,
0105                 "Error reading addr %#x reg %#x: %d\n",
0106                 i2c_addr, reg_addr, ret);
0107         return ret;
0108     }
0109 
0110     return 0;
0111 }
0112 
0113 static int icn8505_write_xfer(struct i2c_client *client, u16 i2c_addr,
0114                   int reg_addr, int reg_addr_width,
0115                   const void *data, int len, bool silent)
0116 {
0117     u8 buf[3 + 32]; /* 3 bytes for 24 bit reg-addr + 32 bytes max len */
0118     int i, ret;
0119     struct i2c_msg msg = {
0120         .addr = i2c_addr,
0121         .buf = buf,
0122         .len = reg_addr_width / 8 + len,
0123     };
0124 
0125     if (WARN_ON(len > 32))
0126         return -EINVAL;
0127 
0128     for (i = 0; i < (reg_addr_width / 8); i++)
0129         buf[i] = (reg_addr >> (reg_addr_width - (i + 1) * 8)) & 0xff;
0130 
0131     memcpy(buf + reg_addr_width / 8, data, len);
0132 
0133     ret = i2c_transfer(client->adapter, &msg, 1);
0134     if (ret != 1) {
0135         if (ret >= 0)
0136             ret = -EIO;
0137         if (!silent)
0138             dev_err(&client->dev,
0139                 "Error writing addr %#x reg %#x: %d\n",
0140                 i2c_addr, reg_addr, ret);
0141         return ret;
0142     }
0143 
0144     return 0;
0145 }
0146 
0147 static int icn8505_read_data(struct icn8505_data *icn8505, int reg,
0148                  void *buf, int len)
0149 {
0150     return icn8505_read_xfer(icn8505->client, icn8505->client->addr, reg,
0151                  ICN8505_REG_ADDR_WIDTH, buf, len, false);
0152 }
0153 
0154 static int icn8505_read_reg_silent(struct icn8505_data *icn8505, int reg)
0155 {
0156     u8 buf;
0157     int error;
0158 
0159     error = icn8505_read_xfer(icn8505->client, icn8505->client->addr, reg,
0160                   ICN8505_REG_ADDR_WIDTH, &buf, 1, true);
0161     if (error)
0162         return error;
0163 
0164     return buf;
0165 }
0166 
0167 static int icn8505_write_reg(struct icn8505_data *icn8505, int reg, u8 val)
0168 {
0169     return icn8505_write_xfer(icn8505->client, icn8505->client->addr, reg,
0170                   ICN8505_REG_ADDR_WIDTH, &val, 1, false);
0171 }
0172 
0173 static int icn8505_read_prog_data(struct icn8505_data *icn8505, int reg,
0174                   void *buf, int len)
0175 {
0176     return icn8505_read_xfer(icn8505->client, ICN8505_PROG_I2C_ADDR, reg,
0177                  ICN8505_PROG_REG_ADDR_WIDTH, buf, len, false);
0178 }
0179 
0180 static int icn8505_write_prog_data(struct icn8505_data *icn8505, int reg,
0181                    const void *buf, int len)
0182 {
0183     return icn8505_write_xfer(icn8505->client, ICN8505_PROG_I2C_ADDR, reg,
0184                   ICN8505_PROG_REG_ADDR_WIDTH, buf, len, false);
0185 }
0186 
0187 static int icn8505_write_prog_reg(struct icn8505_data *icn8505, int reg, u8 val)
0188 {
0189     return icn8505_write_xfer(icn8505->client, ICN8505_PROG_I2C_ADDR, reg,
0190                   ICN8505_PROG_REG_ADDR_WIDTH, &val, 1, false);
0191 }
0192 
0193 /*
0194  * Note this function uses a number of magic register addresses and values,
0195  * there are deliberately no defines for these because the algorithm is taken
0196  * from the icn85xx Android driver and I do not want to make up possibly wrong
0197  * names for the addresses and/or values.
0198  */
0199 static int icn8505_try_fw_upload(struct icn8505_data *icn8505,
0200                  const struct firmware *fw)
0201 {
0202     struct device *dev = &icn8505->client->dev;
0203     size_t offset, count;
0204     int error;
0205     u8 buf[4];
0206     u32 crc;
0207 
0208     /* Put the controller in programming mode */
0209     error = icn8505_write_prog_reg(icn8505, 0xcc3355, 0x5a);
0210     if (error)
0211         return error;
0212 
0213     usleep_range(2000, 5000);
0214 
0215     error = icn8505_write_prog_reg(icn8505, 0x040400, 0x01);
0216     if (error)
0217         return error;
0218 
0219     usleep_range(2000, 5000);
0220 
0221     error = icn8505_read_prog_data(icn8505, 0x040002, buf, 1);
0222     if (error)
0223         return error;
0224 
0225     if (buf[0] != 0x85) {
0226         dev_err(dev, "Failed to enter programming mode\n");
0227         return -ENODEV;
0228     }
0229 
0230     usleep_range(1000, 5000);
0231 
0232     /* Enable CRC mode */
0233     error = icn8505_write_prog_reg(icn8505, 0x40028, 1);
0234     if (error)
0235         return error;
0236 
0237     /* Send the firmware to SRAM */
0238     for (offset = 0; offset < fw->size; offset += count) {
0239         count = min_t(size_t, fw->size - offset, 32);
0240         error = icn8505_write_prog_data(icn8505, offset,
0241                           fw->data + offset, count);
0242         if (error)
0243             return error;
0244     }
0245 
0246     /* Disable CRC mode */
0247     error = icn8505_write_prog_reg(icn8505, 0x40028, 0);
0248     if (error)
0249         return error;
0250 
0251     /* Get and check length and CRC */
0252     error = icn8505_read_prog_data(icn8505, 0x40034, buf, 2);
0253     if (error)
0254         return error;
0255 
0256     if (get_unaligned_le16(buf) != fw->size) {
0257         dev_warn(dev, "Length mismatch after uploading fw\n");
0258         return -EIO;
0259     }
0260 
0261     error = icn8505_read_prog_data(icn8505, 0x4002c, buf, 4);
0262     if (error)
0263         return error;
0264 
0265     crc = crc32_be(0, fw->data, fw->size);
0266     if (get_unaligned_le32(buf) != crc) {
0267         dev_warn(dev, "CRC mismatch after uploading fw\n");
0268         return -EIO;
0269     }
0270 
0271     /* Boot controller from SRAM */
0272     error = icn8505_write_prog_reg(icn8505, 0x40400, 0x03);
0273     if (error)
0274         return error;
0275 
0276     usleep_range(2000, 5000);
0277     return 0;
0278 }
0279 
0280 static int icn8505_upload_fw(struct icn8505_data *icn8505)
0281 {
0282     struct device *dev = &icn8505->client->dev;
0283     const struct firmware *fw;
0284     int i, error;
0285 
0286     /*
0287      * Always load the firmware, even if we don't need it at boot, we
0288      * we may need it at resume. Having loaded it once will make the
0289      * firmware class code cache it at suspend/resume.
0290      */
0291     error = firmware_request_platform(&fw, icn8505->firmware_name, dev);
0292     if (error) {
0293         dev_err(dev, "Firmware request error %d\n", error);
0294         return error;
0295     }
0296 
0297     /* Check if the controller is not already up and running */
0298     if (icn8505_read_reg_silent(icn8505, 0x000a) == 0x85)
0299         goto success;
0300 
0301     for (i = 1; i <= MAX_FW_UPLOAD_TRIES; i++) {
0302         error = icn8505_try_fw_upload(icn8505, fw);
0303         if (!error)
0304             goto success;
0305 
0306         dev_err(dev, "Failed to upload firmware: %d (attempt %d/%d)\n",
0307             error, i, MAX_FW_UPLOAD_TRIES);
0308         usleep_range(2000, 5000);
0309     }
0310 
0311 success:
0312     release_firmware(fw);
0313     return error;
0314 }
0315 
0316 static bool icn8505_touch_active(u8 event)
0317 {
0318     return event == ICN8505_EVENT_UPDATE1 ||
0319            event == ICN8505_EVENT_UPDATE2;
0320 }
0321 
0322 static irqreturn_t icn8505_irq(int irq, void *dev_id)
0323 {
0324     struct icn8505_data *icn8505 = dev_id;
0325     struct device *dev = &icn8505->client->dev;
0326     struct icn8505_touch_data touch_data;
0327     int i, error;
0328 
0329     error = icn8505_read_data(icn8505, ICN8505_REG_TOUCHDATA,
0330                   &touch_data, sizeof(touch_data));
0331     if (error) {
0332         dev_err(dev, "Error reading touch data: %d\n", error);
0333         return IRQ_HANDLED;
0334     }
0335 
0336     if (touch_data.touch_count > ICN8505_MAX_TOUCHES) {
0337         dev_warn(dev, "Too many touches %d > %d\n",
0338              touch_data.touch_count, ICN8505_MAX_TOUCHES);
0339         touch_data.touch_count = ICN8505_MAX_TOUCHES;
0340     }
0341 
0342     for (i = 0; i < touch_data.touch_count; i++) {
0343         struct icn8505_touch *touch = &touch_data.touches[i];
0344         bool act = icn8505_touch_active(touch->event);
0345 
0346         input_mt_slot(icn8505->input, touch->slot);
0347         input_mt_report_slot_state(icn8505->input, MT_TOOL_FINGER, act);
0348         if (!act)
0349             continue;
0350 
0351         touchscreen_report_pos(icn8505->input, &icn8505->prop,
0352                        get_unaligned_le16(touch->x),
0353                        get_unaligned_le16(touch->y),
0354                        true);
0355     }
0356 
0357     input_mt_sync_frame(icn8505->input);
0358     input_report_key(icn8505->input, KEY_LEFTMETA,
0359              touch_data.softbutton == 1);
0360     input_sync(icn8505->input);
0361 
0362     return IRQ_HANDLED;
0363 }
0364 
0365 static int icn8505_probe_acpi(struct icn8505_data *icn8505, struct device *dev)
0366 {
0367     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0368     const char *subsys = "unknown";
0369     struct acpi_device *adev;
0370     union acpi_object *obj;
0371     acpi_status status;
0372 
0373     adev = ACPI_COMPANION(dev);
0374     if (!adev)
0375         return -ENODEV;
0376 
0377     status = acpi_evaluate_object(adev->handle, "_SUB", NULL, &buffer);
0378     if (ACPI_SUCCESS(status)) {
0379         obj = buffer.pointer;
0380         if (obj->type == ACPI_TYPE_STRING)
0381             subsys = obj->string.pointer;
0382         else
0383             dev_warn(dev, "Warning ACPI _SUB did not return a string\n");
0384     } else {
0385         dev_warn(dev, "Warning ACPI _SUB failed: %#x\n", status);
0386         buffer.pointer = NULL;
0387     }
0388 
0389     snprintf(icn8505->firmware_name, sizeof(icn8505->firmware_name),
0390          "chipone/icn8505-%s.fw", subsys);
0391 
0392     kfree(buffer.pointer);
0393     return 0;
0394 }
0395 
0396 static int icn8505_probe(struct i2c_client *client)
0397 {
0398     struct device *dev = &client->dev;
0399     struct icn8505_data *icn8505;
0400     struct input_dev *input;
0401     __le16 resolution[2];
0402     int error;
0403 
0404     if (!client->irq) {
0405         dev_err(dev, "No irq specified\n");
0406         return -EINVAL;
0407     }
0408 
0409     icn8505 = devm_kzalloc(dev, sizeof(*icn8505), GFP_KERNEL);
0410     if (!icn8505)
0411         return -ENOMEM;
0412 
0413     input = devm_input_allocate_device(dev);
0414     if (!input)
0415         return -ENOMEM;
0416 
0417     input->name = client->name;
0418     input->id.bustype = BUS_I2C;
0419 
0420     input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
0421     input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
0422     input_set_capability(input, EV_KEY, KEY_LEFTMETA);
0423 
0424     icn8505->client = client;
0425     icn8505->input = input;
0426     input_set_drvdata(input, icn8505);
0427 
0428     error = icn8505_probe_acpi(icn8505, dev);
0429     if (error)
0430         return error;
0431 
0432     error = icn8505_upload_fw(icn8505);
0433     if (error)
0434         return error;
0435 
0436     error = icn8505_read_data(icn8505, ICN8505_REG_CONFIGDATA,
0437                 resolution, sizeof(resolution));
0438     if (error) {
0439         dev_err(dev, "Error reading resolution: %d\n", error);
0440         return error;
0441     }
0442 
0443     input_set_abs_params(input, ABS_MT_POSITION_X, 0,
0444                  le16_to_cpu(resolution[0]) - 1, 0, 0);
0445     input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
0446                  le16_to_cpu(resolution[1]) - 1, 0, 0);
0447 
0448     touchscreen_parse_properties(input, true, &icn8505->prop);
0449     if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
0450         !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
0451         dev_err(dev, "Error touchscreen-size-x and/or -y missing\n");
0452         return -EINVAL;
0453     }
0454 
0455     error = input_mt_init_slots(input, ICN8505_MAX_TOUCHES,
0456                   INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
0457     if (error)
0458         return error;
0459 
0460     error = devm_request_threaded_irq(dev, client->irq, NULL, icn8505_irq,
0461                     IRQF_ONESHOT, client->name, icn8505);
0462     if (error) {
0463         dev_err(dev, "Error requesting irq: %d\n", error);
0464         return error;
0465     }
0466 
0467     error = input_register_device(input);
0468     if (error)
0469         return error;
0470 
0471     i2c_set_clientdata(client, icn8505);
0472     return 0;
0473 }
0474 
0475 static int __maybe_unused icn8505_suspend(struct device *dev)
0476 {
0477     struct icn8505_data *icn8505 = i2c_get_clientdata(to_i2c_client(dev));
0478 
0479     disable_irq(icn8505->client->irq);
0480 
0481     icn8505_write_reg(icn8505, ICN8505_REG_POWER, ICN8505_POWER_HIBERNATE);
0482 
0483     return 0;
0484 }
0485 
0486 static int __maybe_unused icn8505_resume(struct device *dev)
0487 {
0488     struct icn8505_data *icn8505 = i2c_get_clientdata(to_i2c_client(dev));
0489     int error;
0490 
0491     error = icn8505_upload_fw(icn8505);
0492     if (error)
0493         return error;
0494 
0495     enable_irq(icn8505->client->irq);
0496     return 0;
0497 }
0498 
0499 static SIMPLE_DEV_PM_OPS(icn8505_pm_ops, icn8505_suspend, icn8505_resume);
0500 
0501 static const struct acpi_device_id icn8505_acpi_match[] = {
0502     { "CHPN0001" },
0503     { }
0504 };
0505 MODULE_DEVICE_TABLE(acpi, icn8505_acpi_match);
0506 
0507 static struct i2c_driver icn8505_driver = {
0508     .driver = {
0509         .name   = "chipone_icn8505",
0510         .pm = &icn8505_pm_ops,
0511         .acpi_match_table = icn8505_acpi_match,
0512     },
0513     .probe_new = icn8505_probe,
0514 };
0515 
0516 module_i2c_driver(icn8505_driver);
0517 
0518 MODULE_DESCRIPTION("ChipOne icn8505 I2C Touchscreen Driver");
0519 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0520 MODULE_LICENSE("GPL");