Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 #include <linux/delay.h>
0004 #include <linux/i2c.h>
0005 #include <linux/input.h>
0006 #include <linux/input/mt.h>
0007 #include <linux/input/touchscreen.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/irq.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/slab.h>
0015 
0016 /* Register Map */
0017 
0018 #define ZINITIX_SWRESET_CMD         0x0000
0019 #define ZINITIX_WAKEUP_CMD          0x0001
0020 
0021 #define ZINITIX_IDLE_CMD            0x0004
0022 #define ZINITIX_SLEEP_CMD           0x0005
0023 
0024 #define ZINITIX_CLEAR_INT_STATUS_CMD        0x0003
0025 #define ZINITIX_CALIBRATE_CMD           0x0006
0026 #define ZINITIX_SAVE_STATUS_CMD         0x0007
0027 #define ZINITIX_SAVE_CALIBRATION_CMD        0x0008
0028 #define ZINITIX_RECALL_FACTORY_CMD      0x000f
0029 
0030 #define ZINITIX_THRESHOLD           0x0020
0031 
0032 #define ZINITIX_LARGE_PALM_REJECT_AREA_TH   0x003F
0033 
0034 #define ZINITIX_DEBUG_REG           0x0115 /* 0~7 */
0035 
0036 #define ZINITIX_TOUCH_MODE          0x0010
0037 #define ZINITIX_CHIP_REVISION           0x0011
0038 #define ZINITIX_FIRMWARE_VERSION        0x0012
0039 
0040 #define ZINITIX_USB_DETECT          0x116
0041 
0042 #define ZINITIX_MINOR_FW_VERSION        0x0121
0043 
0044 #define ZINITIX_VENDOR_ID           0x001C
0045 #define ZINITIX_HW_ID               0x0014
0046 
0047 #define ZINITIX_DATA_VERSION_REG        0x0013
0048 #define ZINITIX_SUPPORTED_FINGER_NUM        0x0015
0049 #define ZINITIX_EEPROM_INFO         0x0018
0050 #define ZINITIX_INITIAL_TOUCH_MODE      0x0019
0051 
0052 #define ZINITIX_TOTAL_NUMBER_OF_X       0x0060
0053 #define ZINITIX_TOTAL_NUMBER_OF_Y       0x0061
0054 
0055 #define ZINITIX_DELAY_RAW_FOR_HOST      0x007f
0056 
0057 #define ZINITIX_BUTTON_SUPPORTED_NUM        0x00B0
0058 #define ZINITIX_BUTTON_SENSITIVITY      0x00B2
0059 #define ZINITIX_DUMMY_BUTTON_SENSITIVITY    0X00C8
0060 
0061 #define ZINITIX_X_RESOLUTION            0x00C0
0062 #define ZINITIX_Y_RESOLUTION            0x00C1
0063 
0064 #define ZINITIX_POINT_STATUS_REG        0x0080
0065 #define ZINITIX_ICON_STATUS_REG         0x00AA
0066 
0067 #define ZINITIX_POINT_COORD_REG         (ZINITIX_POINT_STATUS_REG + 2)
0068 
0069 #define ZINITIX_AFE_FREQUENCY           0x0100
0070 #define ZINITIX_DND_N_COUNT         0x0122
0071 #define ZINITIX_DND_U_COUNT         0x0135
0072 
0073 #define ZINITIX_RAWDATA_REG         0x0200
0074 
0075 #define ZINITIX_EEPROM_INFO_REG         0x0018
0076 
0077 #define ZINITIX_INT_ENABLE_FLAG         0x00f0
0078 #define ZINITIX_PERIODICAL_INTERRUPT_INTERVAL   0x00f1
0079 
0080 #define ZINITIX_BTN_WIDTH           0x016d
0081 
0082 #define ZINITIX_CHECKSUM_RESULT         0x012c
0083 
0084 #define ZINITIX_INIT_FLASH          0x01d0
0085 #define ZINITIX_WRITE_FLASH         0x01d1
0086 #define ZINITIX_READ_FLASH          0x01d2
0087 
0088 #define ZINITIX_INTERNAL_FLAG_02        0x011e
0089 #define ZINITIX_INTERNAL_FLAG_03        0x011f
0090 
0091 #define ZINITIX_I2C_CHECKSUM_WCNT       0x016a
0092 #define ZINITIX_I2C_CHECKSUM_RESULT     0x016c
0093 
0094 /* Interrupt & status register flags */
0095 
0096 #define BIT_PT_CNT_CHANGE           BIT(0)
0097 #define BIT_DOWN                BIT(1)
0098 #define BIT_MOVE                BIT(2)
0099 #define BIT_UP                  BIT(3)
0100 #define BIT_PALM                BIT(4)
0101 #define BIT_PALM_REJECT             BIT(5)
0102 #define BIT_RESERVED_0              BIT(6)
0103 #define BIT_RESERVED_1              BIT(7)
0104 #define BIT_WEIGHT_CHANGE           BIT(8)
0105 #define BIT_PT_NO_CHANGE            BIT(9)
0106 #define BIT_REJECT              BIT(10)
0107 #define BIT_PT_EXIST                BIT(11)
0108 #define BIT_RESERVED_2              BIT(12)
0109 #define BIT_ERROR               BIT(13)
0110 #define BIT_DEBUG               BIT(14)
0111 #define BIT_ICON_EVENT              BIT(15)
0112 
0113 #define SUB_BIT_EXIST               BIT(0)
0114 #define SUB_BIT_DOWN                BIT(1)
0115 #define SUB_BIT_MOVE                BIT(2)
0116 #define SUB_BIT_UP              BIT(3)
0117 #define SUB_BIT_UPDATE              BIT(4)
0118 #define SUB_BIT_WAIT                BIT(5)
0119 
0120 #define DEFAULT_TOUCH_POINT_MODE        2
0121 #define MAX_SUPPORTED_FINGER_NUM        5
0122 
0123 #define CHIP_ON_DELAY               15 // ms
0124 #define FIRMWARE_ON_DELAY           40 // ms
0125 
0126 struct point_coord {
0127     __le16  x;
0128     __le16  y;
0129     u8  width;
0130     u8  sub_status;
0131     // currently unused, but needed as padding:
0132     u8  minor_width;
0133     u8  angle;
0134 };
0135 
0136 struct touch_event {
0137     __le16  status;
0138     u8  finger_mask;
0139     u8  time_stamp;
0140     struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM];
0141 };
0142 
0143 struct bt541_ts_data {
0144     struct i2c_client *client;
0145     struct input_dev *input_dev;
0146     struct touchscreen_properties prop;
0147     struct regulator_bulk_data supplies[2];
0148     u32 zinitix_mode;
0149 };
0150 
0151 static int zinitix_read_data(struct i2c_client *client,
0152                  u16 reg, void *values, size_t length)
0153 {
0154     __le16 reg_le = cpu_to_le16(reg);
0155     int ret;
0156 
0157     /* A single i2c_transfer() transaction does not work here. */
0158     ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
0159     if (ret != sizeof(reg_le))
0160         return ret < 0 ? ret : -EIO;
0161 
0162     ret = i2c_master_recv(client, (u8 *)values, length);
0163     if (ret != length)
0164         return ret < 0 ? ret : -EIO;
0165 
0166     return 0;
0167 }
0168 
0169 static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value)
0170 {
0171     __le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)};
0172     int ret;
0173 
0174     ret = i2c_master_send(client, (u8 *)packet, sizeof(packet));
0175     if (ret != sizeof(packet))
0176         return ret < 0 ? ret : -EIO;
0177 
0178     return 0;
0179 }
0180 
0181 static int zinitix_write_cmd(struct i2c_client *client, u16 reg)
0182 {
0183     __le16 reg_le = cpu_to_le16(reg);
0184     int ret;
0185 
0186     ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
0187     if (ret != sizeof(reg_le))
0188         return ret < 0 ? ret : -EIO;
0189 
0190     return 0;
0191 }
0192 
0193 static int zinitix_init_touch(struct bt541_ts_data *bt541)
0194 {
0195     struct i2c_client *client = bt541->client;
0196     int i;
0197     int error;
0198 
0199     error = zinitix_write_cmd(client, ZINITIX_SWRESET_CMD);
0200     if (error) {
0201         dev_err(&client->dev, "Failed to write reset command\n");
0202         return error;
0203     }
0204 
0205     error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG, 0x0);
0206     if (error) {
0207         dev_err(&client->dev,
0208             "Failed to reset interrupt enable flag\n");
0209         return error;
0210     }
0211 
0212     /* initialize */
0213     error = zinitix_write_u16(client, ZINITIX_X_RESOLUTION,
0214                   bt541->prop.max_x);
0215     if (error)
0216         return error;
0217 
0218     error = zinitix_write_u16(client, ZINITIX_Y_RESOLUTION,
0219                   bt541->prop.max_y);
0220     if (error)
0221         return error;
0222 
0223     error = zinitix_write_u16(client, ZINITIX_SUPPORTED_FINGER_NUM,
0224                   MAX_SUPPORTED_FINGER_NUM);
0225     if (error)
0226         return error;
0227 
0228     error = zinitix_write_u16(client, ZINITIX_INITIAL_TOUCH_MODE,
0229                   bt541->zinitix_mode);
0230     if (error)
0231         return error;
0232 
0233     error = zinitix_write_u16(client, ZINITIX_TOUCH_MODE,
0234                   bt541->zinitix_mode);
0235     if (error)
0236         return error;
0237 
0238     error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG,
0239                   BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE |
0240                     BIT_UP);
0241     if (error)
0242         return error;
0243 
0244     /* clear queue */
0245     for (i = 0; i < 10; i++) {
0246         zinitix_write_cmd(client, ZINITIX_CLEAR_INT_STATUS_CMD);
0247         udelay(10);
0248     }
0249 
0250     return 0;
0251 }
0252 
0253 static int zinitix_init_regulators(struct bt541_ts_data *bt541)
0254 {
0255     struct device *dev = &bt541->client->dev;
0256     int error;
0257 
0258     /*
0259      * Some older device trees have erroneous names for the regulators,
0260      * so check if "vddo" is present and in that case use these names.
0261      * Else use the proper supply names on the component.
0262      */
0263     if (of_find_property(dev->of_node, "vddo-supply", NULL)) {
0264         bt541->supplies[0].supply = "vdd";
0265         bt541->supplies[1].supply = "vddo";
0266     } else {
0267         /* Else use the proper supply names */
0268         bt541->supplies[0].supply = "vcca";
0269         bt541->supplies[1].supply = "vdd";
0270     }
0271     error = devm_regulator_bulk_get(dev,
0272                     ARRAY_SIZE(bt541->supplies),
0273                     bt541->supplies);
0274     if (error < 0) {
0275         dev_err(dev, "Failed to get regulators: %d\n", error);
0276         return error;
0277     }
0278 
0279     return 0;
0280 }
0281 
0282 static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
0283 {
0284     int error;
0285     struct i2c_client *client = bt541->client;
0286 
0287     error = zinitix_write_u16(client, 0xc000, 0x0001);
0288     if (error) {
0289         dev_err(&client->dev,
0290             "Failed to send power sequence(vendor cmd enable)\n");
0291         return error;
0292     }
0293     udelay(10);
0294 
0295     error = zinitix_write_cmd(client, 0xc004);
0296     if (error) {
0297         dev_err(&client->dev,
0298             "Failed to send power sequence (intn clear)\n");
0299         return error;
0300     }
0301     udelay(10);
0302 
0303     error = zinitix_write_u16(client, 0xc002, 0x0001);
0304     if (error) {
0305         dev_err(&client->dev,
0306             "Failed to send power sequence (nvm init)\n");
0307         return error;
0308     }
0309     mdelay(2);
0310 
0311     error = zinitix_write_u16(client, 0xc001, 0x0001);
0312     if (error) {
0313         dev_err(&client->dev,
0314             "Failed to send power sequence (program start)\n");
0315         return error;
0316     }
0317     msleep(FIRMWARE_ON_DELAY);
0318 
0319     return 0;
0320 }
0321 
0322 static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
0323                   const struct point_coord *p)
0324 {
0325     u16 x, y;
0326 
0327     if (unlikely(!(p->sub_status &
0328                (SUB_BIT_UP | SUB_BIT_DOWN | SUB_BIT_MOVE)))) {
0329         dev_dbg(&bt541->client->dev, "unknown finger event %#02x\n",
0330             p->sub_status);
0331         return;
0332     }
0333 
0334     x = le16_to_cpu(p->x);
0335     y = le16_to_cpu(p->y);
0336 
0337     input_mt_slot(bt541->input_dev, slot);
0338     if (input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER,
0339                        !(p->sub_status & SUB_BIT_UP))) {
0340         touchscreen_report_pos(bt541->input_dev,
0341                        &bt541->prop, x, y, true);
0342         input_report_abs(bt541->input_dev,
0343                  ABS_MT_TOUCH_MAJOR, p->width);
0344         dev_dbg(&bt541->client->dev, "finger %d %s (%u, %u)\n",
0345             slot, p->sub_status & SUB_BIT_DOWN ? "down" : "move",
0346             x, y);
0347     } else {
0348         dev_dbg(&bt541->client->dev, "finger %d up (%u, %u)\n",
0349             slot, x, y);
0350     }
0351 }
0352 
0353 static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
0354 {
0355     struct bt541_ts_data *bt541 = bt541_handler;
0356     struct i2c_client *client = bt541->client;
0357     struct touch_event touch_event;
0358     unsigned long finger_mask;
0359     int error;
0360     int i;
0361 
0362     memset(&touch_event, 0, sizeof(struct touch_event));
0363 
0364     error = zinitix_read_data(bt541->client, ZINITIX_POINT_STATUS_REG,
0365                   &touch_event, sizeof(struct touch_event));
0366     if (error) {
0367         dev_err(&client->dev, "Failed to read in touchpoint struct\n");
0368         goto out;
0369     }
0370 
0371     finger_mask = touch_event.finger_mask;
0372     for_each_set_bit(i, &finger_mask, MAX_SUPPORTED_FINGER_NUM) {
0373         const struct point_coord *p = &touch_event.point_coord[i];
0374 
0375         /* Only process contacts that are actually reported */
0376         if (p->sub_status & SUB_BIT_EXIST)
0377             zinitix_report_finger(bt541, i, p);
0378     }
0379 
0380     input_mt_sync_frame(bt541->input_dev);
0381     input_sync(bt541->input_dev);
0382 
0383 out:
0384     zinitix_write_cmd(bt541->client, ZINITIX_CLEAR_INT_STATUS_CMD);
0385     return IRQ_HANDLED;
0386 }
0387 
0388 static int zinitix_start(struct bt541_ts_data *bt541)
0389 {
0390     int error;
0391 
0392     error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
0393                       bt541->supplies);
0394     if (error) {
0395         dev_err(&bt541->client->dev,
0396             "Failed to enable regulators: %d\n", error);
0397         return error;
0398     }
0399 
0400     msleep(CHIP_ON_DELAY);
0401 
0402     error = zinitix_send_power_on_sequence(bt541);
0403     if (error) {
0404         dev_err(&bt541->client->dev,
0405             "Error while sending power-on sequence: %d\n", error);
0406         return error;
0407     }
0408 
0409     error = zinitix_init_touch(bt541);
0410     if (error) {
0411         dev_err(&bt541->client->dev,
0412             "Error while configuring touch IC\n");
0413         return error;
0414     }
0415 
0416     enable_irq(bt541->client->irq);
0417 
0418     return 0;
0419 }
0420 
0421 static int zinitix_stop(struct bt541_ts_data *bt541)
0422 {
0423     int error;
0424 
0425     disable_irq(bt541->client->irq);
0426 
0427     error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
0428                        bt541->supplies);
0429     if (error) {
0430         dev_err(&bt541->client->dev,
0431             "Failed to disable regulators: %d\n", error);
0432         return error;
0433     }
0434 
0435     return 0;
0436 }
0437 
0438 static int zinitix_input_open(struct input_dev *dev)
0439 {
0440     struct bt541_ts_data *bt541 = input_get_drvdata(dev);
0441 
0442     return zinitix_start(bt541);
0443 }
0444 
0445 static void zinitix_input_close(struct input_dev *dev)
0446 {
0447     struct bt541_ts_data *bt541 = input_get_drvdata(dev);
0448 
0449     zinitix_stop(bt541);
0450 }
0451 
0452 static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
0453 {
0454     struct input_dev *input_dev;
0455     int error;
0456 
0457     input_dev = devm_input_allocate_device(&bt541->client->dev);
0458     if (!input_dev) {
0459         dev_err(&bt541->client->dev,
0460             "Failed to allocate input device.");
0461         return -ENOMEM;
0462     }
0463 
0464     input_set_drvdata(input_dev, bt541);
0465     bt541->input_dev = input_dev;
0466 
0467     input_dev->name = "Zinitix Capacitive TouchScreen";
0468     input_dev->phys = "input/ts";
0469     input_dev->id.bustype = BUS_I2C;
0470     input_dev->open = zinitix_input_open;
0471     input_dev->close = zinitix_input_close;
0472 
0473     input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
0474     input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
0475     input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
0476     input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
0477 
0478     touchscreen_parse_properties(input_dev, true, &bt541->prop);
0479     if (!bt541->prop.max_x || !bt541->prop.max_y) {
0480         dev_err(&bt541->client->dev,
0481             "Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
0482         return -EINVAL;
0483     }
0484 
0485     error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
0486                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
0487     if (error) {
0488         dev_err(&bt541->client->dev,
0489             "Failed to initialize MT slots: %d", error);
0490         return error;
0491     }
0492 
0493     error = input_register_device(input_dev);
0494     if (error) {
0495         dev_err(&bt541->client->dev,
0496             "Failed to register input device: %d", error);
0497         return error;
0498     }
0499 
0500     return 0;
0501 }
0502 
0503 static int zinitix_ts_probe(struct i2c_client *client)
0504 {
0505     struct bt541_ts_data *bt541;
0506     int error;
0507 
0508     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0509         dev_err(&client->dev,
0510             "Failed to assert adapter's support for plain I2C.\n");
0511         return -ENXIO;
0512     }
0513 
0514     bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
0515     if (!bt541)
0516         return -ENOMEM;
0517 
0518     bt541->client = client;
0519     i2c_set_clientdata(client, bt541);
0520 
0521     error = zinitix_init_regulators(bt541);
0522     if (error) {
0523         dev_err(&client->dev,
0524             "Failed to initialize regulators: %d\n", error);
0525         return error;
0526     }
0527 
0528     error = devm_request_threaded_irq(&client->dev, client->irq,
0529                       NULL, zinitix_ts_irq_handler,
0530                       IRQF_ONESHOT | IRQF_NO_AUTOEN,
0531                       client->name, bt541);
0532     if (error) {
0533         dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
0534         return error;
0535     }
0536 
0537     error = zinitix_init_input_dev(bt541);
0538     if (error) {
0539         dev_err(&client->dev,
0540             "Failed to initialize input device: %d\n", error);
0541         return error;
0542     }
0543 
0544     error = device_property_read_u32(&client->dev, "zinitix,mode",
0545                      &bt541->zinitix_mode);
0546     if (error < 0) {
0547         /* fall back to mode 2 */
0548         bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
0549     }
0550 
0551     if (bt541->zinitix_mode != 2) {
0552         /*
0553          * If there are devices that don't support mode 2, support
0554          * for other modes (0, 1) will be needed.
0555          */
0556         dev_err(&client->dev,
0557             "Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
0558             bt541->zinitix_mode);
0559         return -EINVAL;
0560     }
0561 
0562     return 0;
0563 }
0564 
0565 static int __maybe_unused zinitix_suspend(struct device *dev)
0566 {
0567     struct i2c_client *client = to_i2c_client(dev);
0568     struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
0569 
0570     mutex_lock(&bt541->input_dev->mutex);
0571 
0572     if (input_device_enabled(bt541->input_dev))
0573         zinitix_stop(bt541);
0574 
0575     mutex_unlock(&bt541->input_dev->mutex);
0576 
0577     return 0;
0578 }
0579 
0580 static int __maybe_unused zinitix_resume(struct device *dev)
0581 {
0582     struct i2c_client *client = to_i2c_client(dev);
0583     struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
0584     int ret = 0;
0585 
0586     mutex_lock(&bt541->input_dev->mutex);
0587 
0588     if (input_device_enabled(bt541->input_dev))
0589         ret = zinitix_start(bt541);
0590 
0591     mutex_unlock(&bt541->input_dev->mutex);
0592 
0593     return ret;
0594 }
0595 
0596 static SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
0597 
0598 #ifdef CONFIG_OF
0599 static const struct of_device_id zinitix_of_match[] = {
0600     { .compatible = "zinitix,bt402" },
0601     { .compatible = "zinitix,bt403" },
0602     { .compatible = "zinitix,bt404" },
0603     { .compatible = "zinitix,bt412" },
0604     { .compatible = "zinitix,bt413" },
0605     { .compatible = "zinitix,bt431" },
0606     { .compatible = "zinitix,bt432" },
0607     { .compatible = "zinitix,bt531" },
0608     { .compatible = "zinitix,bt532" },
0609     { .compatible = "zinitix,bt538" },
0610     { .compatible = "zinitix,bt541" },
0611     { .compatible = "zinitix,bt548" },
0612     { .compatible = "zinitix,bt554" },
0613     { .compatible = "zinitix,at100" },
0614     { }
0615 };
0616 MODULE_DEVICE_TABLE(of, zinitix_of_match);
0617 #endif
0618 
0619 static struct i2c_driver zinitix_ts_driver = {
0620     .probe_new = zinitix_ts_probe,
0621     .driver = {
0622         .name = "Zinitix-TS",
0623         .pm = &zinitix_pm_ops,
0624         .of_match_table = of_match_ptr(zinitix_of_match),
0625     },
0626 };
0627 module_i2c_driver(zinitix_ts_driver);
0628 
0629 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
0630 MODULE_DESCRIPTION("Zinitix touchscreen driver");
0631 MODULE_LICENSE("GPL v2");