Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2016, Jelle van der Waa <jelle@vdwaa.nl>
0004  */
0005 
0006 #include <linux/delay.h>
0007 #include <linux/i2c.h>
0008 #include <linux/input.h>
0009 #include <linux/input/mt.h>
0010 #include <linux/input/touchscreen.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <asm/unaligned.h>
0015 
0016 #define ZET6223_MAX_FINGERS     16
0017 #define ZET6223_MAX_PKT_SIZE        (3 + 4 * ZET6223_MAX_FINGERS)
0018 
0019 #define ZET6223_CMD_INFO        0xB2
0020 #define ZET6223_CMD_INFO_LENGTH     17
0021 #define ZET6223_VALID_PACKET        0x3c
0022 
0023 #define ZET6223_POWER_ON_DELAY_MSEC 30
0024 
0025 struct zet6223_ts {
0026     struct i2c_client *client;
0027     struct input_dev *input;
0028     struct regulator *vcc;
0029     struct regulator *vio;
0030     struct touchscreen_properties prop;
0031     struct regulator_bulk_data supplies[2];
0032     u16 max_x;
0033     u16 max_y;
0034     u8 fingernum;
0035 };
0036 
0037 static int zet6223_start(struct input_dev *dev)
0038 {
0039     struct zet6223_ts *ts = input_get_drvdata(dev);
0040 
0041     enable_irq(ts->client->irq);
0042 
0043     return 0;
0044 }
0045 
0046 static void zet6223_stop(struct input_dev *dev)
0047 {
0048     struct zet6223_ts *ts = input_get_drvdata(dev);
0049 
0050     disable_irq(ts->client->irq);
0051 }
0052 
0053 static irqreturn_t zet6223_irq(int irq, void *dev_id)
0054 {
0055     struct zet6223_ts *ts = dev_id;
0056     u16 finger_bits;
0057 
0058     /*
0059      * First 3 bytes are an identifier, two bytes of finger data.
0060      * X, Y data per finger is 4 bytes.
0061      */
0062     u8 bufsize = 3 + 4 * ts->fingernum;
0063     u8 buf[ZET6223_MAX_PKT_SIZE];
0064     int i;
0065     int ret;
0066     int error;
0067 
0068     ret = i2c_master_recv(ts->client, buf, bufsize);
0069     if (ret != bufsize) {
0070         error = ret < 0 ? ret : -EIO;
0071         dev_err_ratelimited(&ts->client->dev,
0072                     "Error reading input data: %d\n", error);
0073         return IRQ_HANDLED;
0074     }
0075 
0076     if (buf[0] != ZET6223_VALID_PACKET)
0077         return IRQ_HANDLED;
0078 
0079     finger_bits = get_unaligned_be16(buf + 1);
0080     for (i = 0; i < ts->fingernum; i++) {
0081         if (!(finger_bits & BIT(15 - i)))
0082             continue;
0083 
0084         input_mt_slot(ts->input, i);
0085         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
0086         input_event(ts->input, EV_ABS, ABS_MT_POSITION_X,
0087                 ((buf[i + 3] >> 4) << 8) + buf[i + 4]);
0088         input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y,
0089                 ((buf[i + 3] & 0xF) << 8) + buf[i + 5]);
0090     }
0091 
0092     input_mt_sync_frame(ts->input);
0093     input_sync(ts->input);
0094 
0095     return IRQ_HANDLED;
0096 }
0097 
0098 static void zet6223_power_off(void *_ts)
0099 {
0100     struct zet6223_ts *ts = _ts;
0101 
0102     regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
0103 }
0104 
0105 static int zet6223_power_on(struct zet6223_ts *ts)
0106 {
0107     struct device *dev = &ts->client->dev;
0108     int error;
0109 
0110     ts->supplies[0].supply = "vio";
0111     ts->supplies[1].supply = "vcc";
0112 
0113     error = devm_regulator_bulk_get(dev, ARRAY_SIZE(ts->supplies),
0114                     ts->supplies);
0115     if (error)
0116         return error;
0117 
0118     error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
0119     if (error)
0120         return error;
0121 
0122     msleep(ZET6223_POWER_ON_DELAY_MSEC);
0123 
0124     error = devm_add_action_or_reset(dev, zet6223_power_off, ts);
0125     if (error) {
0126         dev_err(dev, "failed to install poweroff action: %d\n", error);
0127         return error;
0128     }
0129 
0130     return 0;
0131 }
0132 
0133 static int zet6223_query_device(struct zet6223_ts *ts)
0134 {
0135     u8 buf[ZET6223_CMD_INFO_LENGTH];
0136     u8 cmd = ZET6223_CMD_INFO;
0137     int ret;
0138     int error;
0139 
0140     ret = i2c_master_send(ts->client, &cmd, sizeof(cmd));
0141     if (ret != sizeof(cmd)) {
0142         error = ret < 0 ? ret : -EIO;
0143         dev_err(&ts->client->dev,
0144             "touchpanel info cmd failed: %d\n", error);
0145         return error;
0146     }
0147 
0148     ret = i2c_master_recv(ts->client, buf, sizeof(buf));
0149     if (ret != sizeof(buf)) {
0150         error = ret < 0 ? ret : -EIO;
0151         dev_err(&ts->client->dev,
0152             "failed to retrieve touchpanel info: %d\n", error);
0153         return error;
0154     }
0155 
0156     ts->fingernum = buf[15] & 0x7F;
0157     if (ts->fingernum > ZET6223_MAX_FINGERS) {
0158         dev_warn(&ts->client->dev,
0159              "touchpanel reports %d fingers, limiting to %d\n",
0160              ts->fingernum, ZET6223_MAX_FINGERS);
0161         ts->fingernum = ZET6223_MAX_FINGERS;
0162     }
0163 
0164     ts->max_x = get_unaligned_le16(&buf[8]);
0165     ts->max_y = get_unaligned_le16(&buf[10]);
0166 
0167     return 0;
0168 }
0169 
0170 static int zet6223_probe(struct i2c_client *client,
0171              const struct i2c_device_id *id)
0172 {
0173     struct device *dev = &client->dev;
0174     struct zet6223_ts *ts;
0175     struct input_dev *input;
0176     int error;
0177 
0178     if (!client->irq) {
0179         dev_err(dev, "no irq specified\n");
0180         return -EINVAL;
0181     }
0182 
0183     ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
0184     if (!ts)
0185         return -ENOMEM;
0186 
0187     ts->client = client;
0188 
0189     error = zet6223_power_on(ts);
0190     if (error)
0191         return error;
0192 
0193     error = zet6223_query_device(ts);
0194     if (error)
0195         return error;
0196 
0197     ts->input = input = devm_input_allocate_device(dev);
0198     if (!input)
0199         return -ENOMEM;
0200 
0201     input_set_drvdata(input, ts);
0202 
0203     input->name = client->name;
0204     input->id.bustype = BUS_I2C;
0205     input->open = zet6223_start;
0206     input->close = zet6223_stop;
0207 
0208     input_set_abs_params(input, ABS_MT_POSITION_X, 0, ts->max_x, 0, 0);
0209     input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ts->max_y, 0, 0);
0210 
0211     touchscreen_parse_properties(input, true, &ts->prop);
0212 
0213     error = input_mt_init_slots(input, ts->fingernum,
0214                     INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
0215     if (error)
0216         return error;
0217 
0218     error = devm_request_threaded_irq(dev, client->irq, NULL, zet6223_irq,
0219                       IRQF_ONESHOT, client->name, ts);
0220     if (error) {
0221         dev_err(dev, "failed to request irq %d: %d\n",
0222             client->irq, error);
0223         return error;
0224     }
0225 
0226     zet6223_stop(input);
0227 
0228     error = input_register_device(input);
0229     if (error)
0230         return error;
0231 
0232     return 0;
0233 }
0234 
0235 static const struct of_device_id zet6223_of_match[] = {
0236     { .compatible = "zeitec,zet6223" },
0237     { }
0238 };
0239 MODULE_DEVICE_TABLE(of, zet6223_of_match);
0240 
0241 static const struct i2c_device_id zet6223_id[] = {
0242     { "zet6223", 0},
0243     { }
0244 };
0245 MODULE_DEVICE_TABLE(i2c, zet6223_id);
0246 
0247 static struct i2c_driver zet6223_driver = {
0248     .driver = {
0249         .name = "zet6223",
0250         .of_match_table = zet6223_of_match,
0251     },
0252     .probe = zet6223_probe,
0253     .id_table = zet6223_id
0254 };
0255 module_i2c_driver(zet6223_driver);
0256 
0257 MODULE_AUTHOR("Jelle van der Waa <jelle@vdwaa.nl>");
0258 MODULE_DESCRIPTION("ZEITEC zet622x I2C touchscreen driver");
0259 MODULE_LICENSE("GPL");