Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for ELAN eKTF2127 i2c touchscreen controller
0004  *
0005  * For this driver the layout of the Chipone icn8318 i2c
0006  * touchscreencontroller is used.
0007  *
0008  * Author:
0009  * Michel Verlaan <michel.verl@gmail.com>
0010  * Siebren Vroegindeweij <siebren.vroegindeweij@hotmail.com>
0011  *
0012  * Original chipone_icn8318 driver:
0013  * Hans de Goede <hdegoede@redhat.com>
0014  */
0015 
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/i2c.h>
0019 #include <linux/input.h>
0020 #include <linux/input/mt.h>
0021 #include <linux/input/touchscreen.h>
0022 #include <linux/module.h>
0023 #include <linux/of.h>
0024 #include <linux/delay.h>
0025 
0026 /* Packet header defines (first byte of data send / received) */
0027 #define EKTF2127_NOISE          0x40
0028 #define EKTF2127_RESPONSE       0x52
0029 #define EKTF2127_REQUEST        0x53
0030 #define EKTF2127_HELLO          0x55
0031 #define EKTF2127_REPORT2        0x5a
0032 #define EKTF2127_REPORT         0x5d
0033 #define EKTF2127_CALIB_DONE     0x66
0034 
0035 /* Register defines (second byte of data send / received) */
0036 #define EKTF2127_ENV_NOISY      0x41
0037 #define EKTF2127_HEIGHT         0x60
0038 #define EKTF2127_WIDTH          0x63
0039 
0040 /* 2 bytes header + 5 * 3 bytes coordinates + 3 bytes pressure info + footer */
0041 #define EKTF2127_TOUCH_REPORT_SIZE  21
0042 #define EKTF2127_MAX_TOUCHES        5
0043 
0044 struct ektf2127_ts {
0045     struct i2c_client *client;
0046     struct input_dev *input;
0047     struct gpio_desc *power_gpios;
0048     struct touchscreen_properties prop;
0049 };
0050 
0051 static void ektf2127_parse_coordinates(const u8 *buf, unsigned int touch_count,
0052                        struct input_mt_pos *touches)
0053 {
0054     int index = 0;
0055     int i;
0056 
0057     for (i = 0; i < touch_count; i++) {
0058         index = 2 + i * 3;
0059 
0060         touches[i].x = (buf[index] & 0x0f);
0061         touches[i].x <<= 8;
0062         touches[i].x |= buf[index + 2];
0063 
0064         touches[i].y = (buf[index] & 0xf0);
0065         touches[i].y <<= 4;
0066         touches[i].y |= buf[index + 1];
0067     }
0068 }
0069 
0070 static void ektf2127_report_event(struct ektf2127_ts *ts, const u8 *buf)
0071 {
0072     struct input_mt_pos touches[EKTF2127_MAX_TOUCHES];
0073     int slots[EKTF2127_MAX_TOUCHES];
0074     unsigned int touch_count, i;
0075 
0076     touch_count = buf[1] & 0x07;
0077     if (touch_count > EKTF2127_MAX_TOUCHES) {
0078         dev_err(&ts->client->dev,
0079             "Too many touches %d > %d\n",
0080             touch_count, EKTF2127_MAX_TOUCHES);
0081         touch_count = EKTF2127_MAX_TOUCHES;
0082     }
0083 
0084     ektf2127_parse_coordinates(buf, touch_count, touches);
0085     input_mt_assign_slots(ts->input, slots, touches,
0086                   touch_count, 0);
0087 
0088     for (i = 0; i < touch_count; i++) {
0089         input_mt_slot(ts->input, slots[i]);
0090         input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
0091         touchscreen_report_pos(ts->input, &ts->prop,
0092                        touches[i].x, touches[i].y, true);
0093     }
0094 
0095     input_mt_sync_frame(ts->input);
0096     input_sync(ts->input);
0097 }
0098 
0099 static void ektf2127_report2_contact(struct ektf2127_ts *ts, int slot,
0100                      const u8 *buf, bool active)
0101 {
0102     input_mt_slot(ts->input, slot);
0103     input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, active);
0104 
0105     if (active) {
0106         int x = (buf[0] & 0xf0) << 4 | buf[1];
0107         int y = (buf[0] & 0x0f) << 8 | buf[2];
0108 
0109         touchscreen_report_pos(ts->input, &ts->prop, x, y, true);
0110     }
0111 }
0112 
0113 static void ektf2127_report2_event(struct ektf2127_ts *ts, const u8 *buf)
0114 {
0115     ektf2127_report2_contact(ts, 0, &buf[1], !!(buf[7] & 2));
0116     ektf2127_report2_contact(ts, 1, &buf[4], !!(buf[7] & 4));
0117 
0118     input_mt_sync_frame(ts->input);
0119     input_sync(ts->input);
0120 }
0121 
0122 static irqreturn_t ektf2127_irq(int irq, void *dev_id)
0123 {
0124     struct ektf2127_ts *ts = dev_id;
0125     struct device *dev = &ts->client->dev;
0126     char buf[EKTF2127_TOUCH_REPORT_SIZE];
0127     int ret;
0128 
0129     ret = i2c_master_recv(ts->client, buf, EKTF2127_TOUCH_REPORT_SIZE);
0130     if (ret != EKTF2127_TOUCH_REPORT_SIZE) {
0131         dev_err(dev, "Error reading touch data: %d\n", ret);
0132         goto out;
0133     }
0134 
0135     switch (buf[0]) {
0136     case EKTF2127_REPORT:
0137         ektf2127_report_event(ts, buf);
0138         break;
0139 
0140     case EKTF2127_REPORT2:
0141         ektf2127_report2_event(ts, buf);
0142         break;
0143 
0144     case EKTF2127_NOISE:
0145         if (buf[1] == EKTF2127_ENV_NOISY)
0146             dev_dbg(dev, "Environment is electrically noisy\n");
0147         break;
0148 
0149     case EKTF2127_HELLO:
0150     case EKTF2127_CALIB_DONE:
0151         break;
0152 
0153     default:
0154         dev_err(dev, "Unexpected packet header byte %#02x\n", buf[0]);
0155         break;
0156     }
0157 
0158 out:
0159     return IRQ_HANDLED;
0160 }
0161 
0162 static int ektf2127_start(struct input_dev *dev)
0163 {
0164     struct ektf2127_ts *ts = input_get_drvdata(dev);
0165 
0166     enable_irq(ts->client->irq);
0167     gpiod_set_value_cansleep(ts->power_gpios, 1);
0168 
0169     return 0;
0170 }
0171 
0172 static void ektf2127_stop(struct input_dev *dev)
0173 {
0174     struct ektf2127_ts *ts = input_get_drvdata(dev);
0175 
0176     disable_irq(ts->client->irq);
0177     gpiod_set_value_cansleep(ts->power_gpios, 0);
0178 }
0179 
0180 static int __maybe_unused ektf2127_suspend(struct device *dev)
0181 {
0182     struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
0183 
0184     mutex_lock(&ts->input->mutex);
0185     if (input_device_enabled(ts->input))
0186         ektf2127_stop(ts->input);
0187     mutex_unlock(&ts->input->mutex);
0188 
0189     return 0;
0190 }
0191 
0192 static int __maybe_unused ektf2127_resume(struct device *dev)
0193 {
0194     struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
0195 
0196     mutex_lock(&ts->input->mutex);
0197     if (input_device_enabled(ts->input))
0198         ektf2127_start(ts->input);
0199     mutex_unlock(&ts->input->mutex);
0200 
0201     return 0;
0202 }
0203 
0204 static SIMPLE_DEV_PM_OPS(ektf2127_pm_ops, ektf2127_suspend,
0205              ektf2127_resume);
0206 
0207 static int ektf2127_query_dimension(struct i2c_client *client, bool width)
0208 {
0209     struct device *dev = &client->dev;
0210     const char *what = width ? "width" : "height";
0211     u8 what_code = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
0212     u8 buf[4];
0213     int ret;
0214     int error;
0215 
0216     /* Request dimension */
0217     buf[0] = EKTF2127_REQUEST;
0218     buf[1] = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
0219     buf[2] = 0x00;
0220     buf[3] = 0x00;
0221     ret = i2c_master_send(client, buf, sizeof(buf));
0222     if (ret != sizeof(buf)) {
0223         error = ret < 0 ? ret : -EIO;
0224         dev_err(dev, "Failed to request %s: %d\n", what, error);
0225         return error;
0226     }
0227 
0228     msleep(20);
0229 
0230     /* Read response */
0231     ret = i2c_master_recv(client, buf, sizeof(buf));
0232     if (ret != sizeof(buf)) {
0233         error = ret < 0 ? ret : -EIO;
0234         dev_err(dev, "Failed to receive %s data: %d\n", what, error);
0235         return error;
0236     }
0237 
0238     if (buf[0] != EKTF2127_RESPONSE || buf[1] != what_code) {
0239         dev_err(dev, "Unexpected %s data: %#02x %#02x\n",
0240             what, buf[0], buf[1]);
0241         return -EIO;
0242     }
0243 
0244     return (((buf[3] & 0xf0) << 4) | buf[2]) - 1;
0245 }
0246 
0247 static int ektf2127_probe(struct i2c_client *client,
0248               const struct i2c_device_id *id)
0249 {
0250     struct device *dev = &client->dev;
0251     struct ektf2127_ts *ts;
0252     struct input_dev *input;
0253     u8 buf[4];
0254     int max_x, max_y;
0255     int error;
0256 
0257     if (!client->irq) {
0258         dev_err(dev, "Error no irq specified\n");
0259         return -EINVAL;
0260     }
0261 
0262     ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
0263     if (!ts)
0264         return -ENOMEM;
0265 
0266     /* This requests the gpio *and* turns on the touchscreen controller */
0267     ts->power_gpios = devm_gpiod_get(dev, "power", GPIOD_OUT_HIGH);
0268     if (IS_ERR(ts->power_gpios)) {
0269         error = PTR_ERR(ts->power_gpios);
0270         if (error != -EPROBE_DEFER)
0271             dev_err(dev, "Error getting power gpio: %d\n", error);
0272         return error;
0273     }
0274 
0275     input = devm_input_allocate_device(dev);
0276     if (!input)
0277         return -ENOMEM;
0278 
0279     input->name = client->name;
0280     input->id.bustype = BUS_I2C;
0281     input->open = ektf2127_start;
0282     input->close = ektf2127_stop;
0283 
0284     ts->client = client;
0285 
0286     /* Read hello (ignore result, depends on initial power state) */
0287     msleep(20);
0288     i2c_master_recv(ts->client, buf, sizeof(buf));
0289 
0290     /* Read resolution from chip */
0291     max_x = ektf2127_query_dimension(client, true);
0292     if (max_x < 0)
0293         return max_x;
0294 
0295     max_y = ektf2127_query_dimension(client, false);
0296     if (max_y < 0)
0297         return max_y;
0298 
0299     input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
0300     input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
0301     touchscreen_parse_properties(input, true, &ts->prop);
0302 
0303     error = input_mt_init_slots(input, EKTF2127_MAX_TOUCHES,
0304                     INPUT_MT_DIRECT |
0305                     INPUT_MT_DROP_UNUSED |
0306                     INPUT_MT_TRACK);
0307     if (error)
0308         return error;
0309 
0310     ts->input = input;
0311     input_set_drvdata(input, ts);
0312 
0313     error = devm_request_threaded_irq(dev, client->irq,
0314                       NULL, ektf2127_irq,
0315                       IRQF_ONESHOT, client->name, ts);
0316     if (error) {
0317         dev_err(dev, "Error requesting irq: %d\n", error);
0318         return error;
0319     }
0320 
0321     /* Stop device till opened */
0322     ektf2127_stop(ts->input);
0323 
0324     error = input_register_device(input);
0325     if (error)
0326         return error;
0327 
0328     i2c_set_clientdata(client, ts);
0329 
0330     return 0;
0331 }
0332 
0333 #ifdef CONFIG_OF
0334 static const struct of_device_id ektf2127_of_match[] = {
0335     { .compatible = "elan,ektf2127" },
0336     { .compatible = "elan,ektf2132" },
0337     {}
0338 };
0339 MODULE_DEVICE_TABLE(of, ektf2127_of_match);
0340 #endif
0341 
0342 static const struct i2c_device_id ektf2127_i2c_id[] = {
0343     { "ektf2127", 0 },
0344     { "ektf2132", 0 },
0345     {}
0346 };
0347 MODULE_DEVICE_TABLE(i2c, ektf2127_i2c_id);
0348 
0349 static struct i2c_driver ektf2127_driver = {
0350     .driver = {
0351         .name   = "elan_ektf2127",
0352         .pm = &ektf2127_pm_ops,
0353         .of_match_table = of_match_ptr(ektf2127_of_match),
0354     },
0355     .probe = ektf2127_probe,
0356     .id_table = ektf2127_i2c_id,
0357 };
0358 module_i2c_driver(ektf2127_driver);
0359 
0360 MODULE_DESCRIPTION("ELAN eKTF2127/eKTF2132 I2C Touchscreen Driver");
0361 MODULE_AUTHOR("Michel Verlaan, Siebren Vroegindeweij");
0362 MODULE_LICENSE("GPL");