Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * TSC2004/TSC2005 touchscreen driver core
0004  *
0005  * Copyright (C) 2006-2010 Nokia Corporation
0006  * Copyright (C) 2015 QWERTY Embedded Design
0007  * Copyright (C) 2015 EMAC Inc.
0008  *
0009  * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
0010  * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/input.h>
0016 #include <linux/input/touchscreen.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/delay.h>
0019 #include <linux/pm.h>
0020 #include <linux/of.h>
0021 #include <linux/regulator/consumer.h>
0022 #include <linux/regmap.h>
0023 #include <linux/gpio/consumer.h>
0024 #include "tsc200x-core.h"
0025 
0026 /*
0027  * The touchscreen interface operates as follows:
0028  *
0029  * 1) Pen is pressed against the touchscreen.
0030  * 2) TSC200X performs AD conversion.
0031  * 3) After the conversion is done TSC200X drives DAV line down.
0032  * 4) GPIO IRQ is received and tsc200x_irq_thread() is scheduled.
0033  * 5) tsc200x_irq_thread() queues up a transfer to fetch the x, y, z1, z2
0034  *    values.
0035  * 6) tsc200x_irq_thread() reports coordinates to input layer and sets up
0036  *    tsc200x_penup_timer() to be called after TSC200X_PENUP_TIME_MS (40ms).
0037  * 7) When the penup timer expires, there have not been touch or DAV interrupts
0038  *    during the last 40ms which means the pen has been lifted.
0039  *
0040  * ESD recovery via a hardware reset is done if the TSC200X doesn't respond
0041  * after a configurable period (in ms) of activity. If esd_timeout is 0, the
0042  * watchdog is disabled.
0043  */
0044 
0045 static const struct regmap_range tsc200x_writable_ranges[] = {
0046     regmap_reg_range(TSC200X_REG_AUX_HIGH, TSC200X_REG_CFR2),
0047 };
0048 
0049 static const struct regmap_access_table tsc200x_writable_table = {
0050     .yes_ranges = tsc200x_writable_ranges,
0051     .n_yes_ranges = ARRAY_SIZE(tsc200x_writable_ranges),
0052 };
0053 
0054 const struct regmap_config tsc200x_regmap_config = {
0055     .reg_bits = 8,
0056     .val_bits = 16,
0057     .reg_stride = 0x08,
0058     .max_register = 0x78,
0059     .read_flag_mask = TSC200X_REG_READ,
0060     .write_flag_mask = TSC200X_REG_PND0,
0061     .wr_table = &tsc200x_writable_table,
0062     .use_single_read = true,
0063     .use_single_write = true,
0064 };
0065 EXPORT_SYMBOL_GPL(tsc200x_regmap_config);
0066 
0067 struct tsc200x_data {
0068     u16 x;
0069     u16 y;
0070     u16 z1;
0071     u16 z2;
0072 } __packed;
0073 #define TSC200X_DATA_REGS 4
0074 
0075 struct tsc200x {
0076     struct device           *dev;
0077     struct regmap       *regmap;
0078     __u16                   bustype;
0079 
0080     struct input_dev    *idev;
0081     char            phys[32];
0082 
0083     struct mutex        mutex;
0084 
0085     /* raw copy of previous x,y,z */
0086     int         in_x;
0087     int         in_y;
0088     int                     in_z1;
0089     int         in_z2;
0090 
0091     struct touchscreen_properties prop;
0092 
0093     spinlock_t      lock;
0094     struct timer_list   penup_timer;
0095 
0096     unsigned int        esd_timeout;
0097     struct delayed_work esd_work;
0098     unsigned long       last_valid_interrupt;
0099 
0100     unsigned int        x_plate_ohm;
0101 
0102     bool            opened;
0103     bool            suspended;
0104 
0105     bool            pen_down;
0106 
0107     struct regulator    *vio;
0108 
0109     struct gpio_desc    *reset_gpio;
0110     int         (*tsc200x_cmd)(struct device *dev, u8 cmd);
0111     int         irq;
0112 };
0113 
0114 static void tsc200x_update_pen_state(struct tsc200x *ts,
0115                      int x, int y, int pressure)
0116 {
0117     if (pressure) {
0118         touchscreen_report_pos(ts->idev, &ts->prop, x, y, false);
0119         input_report_abs(ts->idev, ABS_PRESSURE, pressure);
0120         if (!ts->pen_down) {
0121             input_report_key(ts->idev, BTN_TOUCH, !!pressure);
0122             ts->pen_down = true;
0123         }
0124     } else {
0125         input_report_abs(ts->idev, ABS_PRESSURE, 0);
0126         if (ts->pen_down) {
0127             input_report_key(ts->idev, BTN_TOUCH, 0);
0128             ts->pen_down = false;
0129         }
0130     }
0131     input_sync(ts->idev);
0132     dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
0133         pressure);
0134 }
0135 
0136 static irqreturn_t tsc200x_irq_thread(int irq, void *_ts)
0137 {
0138     struct tsc200x *ts = _ts;
0139     unsigned long flags;
0140     unsigned int pressure;
0141     struct tsc200x_data tsdata;
0142     int error;
0143 
0144     /* read the coordinates */
0145     error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata,
0146                  TSC200X_DATA_REGS);
0147     if (unlikely(error))
0148         goto out;
0149 
0150     /* validate position */
0151     if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))
0152         goto out;
0153 
0154     /* Skip reading if the pressure components are out of range */
0155     if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))
0156         goto out;
0157     if (unlikely(tsdata.z1 >= tsdata.z2))
0158         goto out;
0159 
0160        /*
0161     * Skip point if this is a pen down with the exact same values as
0162     * the value before pen-up - that implies SPI fed us stale data
0163     */
0164     if (!ts->pen_down &&
0165         ts->in_x == tsdata.x && ts->in_y == tsdata.y &&
0166         ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {
0167         goto out;
0168     }
0169 
0170     /*
0171      * At this point we are happy we have a valid and useful reading.
0172      * Remember it for later comparisons. We may now begin downsampling.
0173      */
0174     ts->in_x = tsdata.x;
0175     ts->in_y = tsdata.y;
0176     ts->in_z1 = tsdata.z1;
0177     ts->in_z2 = tsdata.z2;
0178 
0179     /* Compute touch pressure resistance using equation #1 */
0180     pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1;
0181     pressure = pressure * ts->x_plate_ohm / 4096;
0182     if (unlikely(pressure > MAX_12BIT))
0183         goto out;
0184 
0185     spin_lock_irqsave(&ts->lock, flags);
0186 
0187     tsc200x_update_pen_state(ts, tsdata.x, tsdata.y, pressure);
0188     mod_timer(&ts->penup_timer,
0189           jiffies + msecs_to_jiffies(TSC200X_PENUP_TIME_MS));
0190 
0191     spin_unlock_irqrestore(&ts->lock, flags);
0192 
0193     ts->last_valid_interrupt = jiffies;
0194 out:
0195     return IRQ_HANDLED;
0196 }
0197 
0198 static void tsc200x_penup_timer(struct timer_list *t)
0199 {
0200     struct tsc200x *ts = from_timer(ts, t, penup_timer);
0201     unsigned long flags;
0202 
0203     spin_lock_irqsave(&ts->lock, flags);
0204     tsc200x_update_pen_state(ts, 0, 0, 0);
0205     spin_unlock_irqrestore(&ts->lock, flags);
0206 }
0207 
0208 static void tsc200x_start_scan(struct tsc200x *ts)
0209 {
0210     regmap_write(ts->regmap, TSC200X_REG_CFR0, TSC200X_CFR0_INITVALUE);
0211     regmap_write(ts->regmap, TSC200X_REG_CFR1, TSC200X_CFR1_INITVALUE);
0212     regmap_write(ts->regmap, TSC200X_REG_CFR2, TSC200X_CFR2_INITVALUE);
0213     ts->tsc200x_cmd(ts->dev, TSC200X_CMD_NORMAL);
0214 }
0215 
0216 static void tsc200x_stop_scan(struct tsc200x *ts)
0217 {
0218     ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP);
0219 }
0220 
0221 static void tsc200x_reset(struct tsc200x *ts)
0222 {
0223     if (ts->reset_gpio) {
0224         gpiod_set_value_cansleep(ts->reset_gpio, 1);
0225         usleep_range(100, 500); /* only 10us required */
0226         gpiod_set_value_cansleep(ts->reset_gpio, 0);
0227     }
0228 }
0229 
0230 /* must be called with ts->mutex held */
0231 static void __tsc200x_disable(struct tsc200x *ts)
0232 {
0233     tsc200x_stop_scan(ts);
0234 
0235     disable_irq(ts->irq);
0236     del_timer_sync(&ts->penup_timer);
0237 
0238     cancel_delayed_work_sync(&ts->esd_work);
0239 
0240     enable_irq(ts->irq);
0241 }
0242 
0243 /* must be called with ts->mutex held */
0244 static void __tsc200x_enable(struct tsc200x *ts)
0245 {
0246     tsc200x_start_scan(ts);
0247 
0248     if (ts->esd_timeout && ts->reset_gpio) {
0249         ts->last_valid_interrupt = jiffies;
0250         schedule_delayed_work(&ts->esd_work,
0251                 round_jiffies_relative(
0252                     msecs_to_jiffies(ts->esd_timeout)));
0253     }
0254 }
0255 
0256 static ssize_t tsc200x_selftest_show(struct device *dev,
0257                      struct device_attribute *attr,
0258                      char *buf)
0259 {
0260     struct tsc200x *ts = dev_get_drvdata(dev);
0261     unsigned int temp_high;
0262     unsigned int temp_high_orig;
0263     unsigned int temp_high_test;
0264     bool success = true;
0265     int error;
0266 
0267     mutex_lock(&ts->mutex);
0268 
0269     /*
0270      * Test TSC200X communications via temp high register.
0271      */
0272     __tsc200x_disable(ts);
0273 
0274     error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig);
0275     if (error) {
0276         dev_warn(dev, "selftest failed: read error %d\n", error);
0277         success = false;
0278         goto out;
0279     }
0280 
0281     temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
0282 
0283     error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test);
0284     if (error) {
0285         dev_warn(dev, "selftest failed: write error %d\n", error);
0286         success = false;
0287         goto out;
0288     }
0289 
0290     error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
0291     if (error) {
0292         dev_warn(dev, "selftest failed: read error %d after write\n",
0293              error);
0294         success = false;
0295         goto out;
0296     }
0297 
0298     if (temp_high != temp_high_test) {
0299         dev_warn(dev, "selftest failed: %d != %d\n",
0300              temp_high, temp_high_test);
0301         success = false;
0302     }
0303 
0304     /* hardware reset */
0305     tsc200x_reset(ts);
0306 
0307     if (!success)
0308         goto out;
0309 
0310     /* test that the reset really happened */
0311     error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
0312     if (error) {
0313         dev_warn(dev, "selftest failed: read error %d after reset\n",
0314              error);
0315         success = false;
0316         goto out;
0317     }
0318 
0319     if (temp_high != temp_high_orig) {
0320         dev_warn(dev, "selftest failed after reset: %d != %d\n",
0321              temp_high, temp_high_orig);
0322         success = false;
0323     }
0324 
0325 out:
0326     __tsc200x_enable(ts);
0327     mutex_unlock(&ts->mutex);
0328 
0329     return sprintf(buf, "%d\n", success);
0330 }
0331 
0332 static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL);
0333 
0334 static struct attribute *tsc200x_attrs[] = {
0335     &dev_attr_selftest.attr,
0336     NULL
0337 };
0338 
0339 static umode_t tsc200x_attr_is_visible(struct kobject *kobj,
0340                       struct attribute *attr, int n)
0341 {
0342     struct device *dev = kobj_to_dev(kobj);
0343     struct tsc200x *ts = dev_get_drvdata(dev);
0344     umode_t mode = attr->mode;
0345 
0346     if (attr == &dev_attr_selftest.attr) {
0347         if (!ts->reset_gpio)
0348             mode = 0;
0349     }
0350 
0351     return mode;
0352 }
0353 
0354 static const struct attribute_group tsc200x_attr_group = {
0355     .is_visible = tsc200x_attr_is_visible,
0356     .attrs      = tsc200x_attrs,
0357 };
0358 
0359 static void tsc200x_esd_work(struct work_struct *work)
0360 {
0361     struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work);
0362     int error;
0363     unsigned int r;
0364 
0365     if (!mutex_trylock(&ts->mutex)) {
0366         /*
0367          * If the mutex is taken, it means that disable or enable is in
0368          * progress. In that case just reschedule the work. If the work
0369          * is not needed, it will be canceled by disable.
0370          */
0371         goto reschedule;
0372     }
0373 
0374     if (time_is_after_jiffies(ts->last_valid_interrupt +
0375                   msecs_to_jiffies(ts->esd_timeout)))
0376         goto out;
0377 
0378     /* We should be able to read register without disabling interrupts. */
0379     error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r);
0380     if (!error &&
0381         !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) {
0382         goto out;
0383     }
0384 
0385     /*
0386      * If we could not read our known value from configuration register 0
0387      * then we should reset the controller as if from power-up and start
0388      * scanning again.
0389      */
0390     dev_info(ts->dev, "TSC200X not responding - resetting\n");
0391 
0392     disable_irq(ts->irq);
0393     del_timer_sync(&ts->penup_timer);
0394 
0395     tsc200x_update_pen_state(ts, 0, 0, 0);
0396 
0397     tsc200x_reset(ts);
0398 
0399     enable_irq(ts->irq);
0400     tsc200x_start_scan(ts);
0401 
0402 out:
0403     mutex_unlock(&ts->mutex);
0404 reschedule:
0405     /* re-arm the watchdog */
0406     schedule_delayed_work(&ts->esd_work,
0407                   round_jiffies_relative(
0408                     msecs_to_jiffies(ts->esd_timeout)));
0409 }
0410 
0411 static int tsc200x_open(struct input_dev *input)
0412 {
0413     struct tsc200x *ts = input_get_drvdata(input);
0414 
0415     mutex_lock(&ts->mutex);
0416 
0417     if (!ts->suspended)
0418         __tsc200x_enable(ts);
0419 
0420     ts->opened = true;
0421 
0422     mutex_unlock(&ts->mutex);
0423 
0424     return 0;
0425 }
0426 
0427 static void tsc200x_close(struct input_dev *input)
0428 {
0429     struct tsc200x *ts = input_get_drvdata(input);
0430 
0431     mutex_lock(&ts->mutex);
0432 
0433     if (!ts->suspended)
0434         __tsc200x_disable(ts);
0435 
0436     ts->opened = false;
0437 
0438     mutex_unlock(&ts->mutex);
0439 }
0440 
0441 int tsc200x_probe(struct device *dev, int irq, const struct input_id *tsc_id,
0442           struct regmap *regmap,
0443           int (*tsc200x_cmd)(struct device *dev, u8 cmd))
0444 {
0445     struct tsc200x *ts;
0446     struct input_dev *input_dev;
0447     u32 x_plate_ohm;
0448     u32 esd_timeout;
0449     int error;
0450 
0451     if (irq <= 0) {
0452         dev_err(dev, "no irq\n");
0453         return -ENODEV;
0454     }
0455 
0456     if (IS_ERR(regmap))
0457         return PTR_ERR(regmap);
0458 
0459     if (!tsc200x_cmd) {
0460         dev_err(dev, "no cmd function\n");
0461         return -ENODEV;
0462     }
0463 
0464     ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
0465     if (!ts)
0466         return -ENOMEM;
0467 
0468     input_dev = devm_input_allocate_device(dev);
0469     if (!input_dev)
0470         return -ENOMEM;
0471 
0472     ts->irq = irq;
0473     ts->dev = dev;
0474     ts->idev = input_dev;
0475     ts->regmap = regmap;
0476     ts->tsc200x_cmd = tsc200x_cmd;
0477 
0478     error = device_property_read_u32(dev, "ti,x-plate-ohms", &x_plate_ohm);
0479     ts->x_plate_ohm = error ? TSC200X_DEF_RESISTOR : x_plate_ohm;
0480 
0481     error = device_property_read_u32(dev, "ti,esd-recovery-timeout-ms",
0482                      &esd_timeout);
0483     ts->esd_timeout = error ? 0 : esd_timeout;
0484 
0485     ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0486     if (IS_ERR(ts->reset_gpio)) {
0487         error = PTR_ERR(ts->reset_gpio);
0488         dev_err(dev, "error acquiring reset gpio: %d\n", error);
0489         return error;
0490     }
0491 
0492     ts->vio = devm_regulator_get(dev, "vio");
0493     if (IS_ERR(ts->vio)) {
0494         error = PTR_ERR(ts->vio);
0495         dev_err(dev, "error acquiring vio regulator: %d", error);
0496         return error;
0497     }
0498 
0499     mutex_init(&ts->mutex);
0500 
0501     spin_lock_init(&ts->lock);
0502     timer_setup(&ts->penup_timer, tsc200x_penup_timer, 0);
0503 
0504     INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work);
0505 
0506     snprintf(ts->phys, sizeof(ts->phys),
0507          "%s/input-ts", dev_name(dev));
0508 
0509     if (tsc_id->product == 2004) {
0510         input_dev->name = "TSC200X touchscreen";
0511     } else {
0512         input_dev->name = devm_kasprintf(dev, GFP_KERNEL,
0513                          "TSC%04d touchscreen",
0514                          tsc_id->product);
0515         if (!input_dev->name)
0516             return -ENOMEM;
0517     }
0518 
0519     input_dev->phys = ts->phys;
0520     input_dev->id = *tsc_id;
0521 
0522     input_dev->open = tsc200x_open;
0523     input_dev->close = tsc200x_close;
0524 
0525     input_set_drvdata(input_dev, ts);
0526 
0527     __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
0528     input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
0529 
0530     input_set_abs_params(input_dev, ABS_X,
0531                  0, MAX_12BIT, TSC200X_DEF_X_FUZZ, 0);
0532     input_set_abs_params(input_dev, ABS_Y,
0533                  0, MAX_12BIT, TSC200X_DEF_Y_FUZZ, 0);
0534     input_set_abs_params(input_dev, ABS_PRESSURE,
0535                  0, MAX_12BIT, TSC200X_DEF_P_FUZZ, 0);
0536 
0537     touchscreen_parse_properties(input_dev, false, &ts->prop);
0538 
0539     /* Ensure the touchscreen is off */
0540     tsc200x_stop_scan(ts);
0541 
0542     error = devm_request_threaded_irq(dev, irq, NULL,
0543                       tsc200x_irq_thread,
0544                       IRQF_TRIGGER_RISING | IRQF_ONESHOT,
0545                       "tsc200x", ts);
0546     if (error) {
0547         dev_err(dev, "Failed to request irq, err: %d\n", error);
0548         return error;
0549     }
0550 
0551     error = regulator_enable(ts->vio);
0552     if (error)
0553         return error;
0554 
0555     dev_set_drvdata(dev, ts);
0556     error = sysfs_create_group(&dev->kobj, &tsc200x_attr_group);
0557     if (error) {
0558         dev_err(dev,
0559             "Failed to create sysfs attributes, err: %d\n", error);
0560         goto disable_regulator;
0561     }
0562 
0563     error = input_register_device(ts->idev);
0564     if (error) {
0565         dev_err(dev,
0566             "Failed to register input device, err: %d\n", error);
0567         goto err_remove_sysfs;
0568     }
0569 
0570     irq_set_irq_wake(irq, 1);
0571     return 0;
0572 
0573 err_remove_sysfs:
0574     sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);
0575 disable_regulator:
0576     regulator_disable(ts->vio);
0577     return error;
0578 }
0579 EXPORT_SYMBOL_GPL(tsc200x_probe);
0580 
0581 void tsc200x_remove(struct device *dev)
0582 {
0583     struct tsc200x *ts = dev_get_drvdata(dev);
0584 
0585     sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);
0586 
0587     regulator_disable(ts->vio);
0588 }
0589 EXPORT_SYMBOL_GPL(tsc200x_remove);
0590 
0591 static int __maybe_unused tsc200x_suspend(struct device *dev)
0592 {
0593     struct tsc200x *ts = dev_get_drvdata(dev);
0594 
0595     mutex_lock(&ts->mutex);
0596 
0597     if (!ts->suspended && ts->opened)
0598         __tsc200x_disable(ts);
0599 
0600     ts->suspended = true;
0601 
0602     mutex_unlock(&ts->mutex);
0603 
0604     return 0;
0605 }
0606 
0607 static int __maybe_unused tsc200x_resume(struct device *dev)
0608 {
0609     struct tsc200x *ts = dev_get_drvdata(dev);
0610 
0611     mutex_lock(&ts->mutex);
0612 
0613     if (ts->suspended && ts->opened)
0614         __tsc200x_enable(ts);
0615 
0616     ts->suspended = false;
0617 
0618     mutex_unlock(&ts->mutex);
0619 
0620     return 0;
0621 }
0622 
0623 SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume);
0624 EXPORT_SYMBOL_GPL(tsc200x_pm_ops);
0625 
0626 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
0627 MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core");
0628 MODULE_LICENSE("GPL");