Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 2 | /* |
| 3 | * TSC2004/TSC2005 touchscreen driver core |
| 4 | * |
| 5 | * Copyright (C) 2006-2010 Nokia Corporation |
| 6 | * Copyright (C) 2015 QWERTY Embedded Design |
| 7 | * Copyright (C) 2015 EMAC Inc. |
| 8 | * |
| 9 | * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com> |
| 10 | * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com> |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/input.h> |
| 16 | #include <linux/input/touchscreen.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/pm.h> |
| 20 | #include <linux/of.h> |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 21 | #include <linux/regulator/consumer.h> |
| 22 | #include <linux/regmap.h> |
| 23 | #include <linux/gpio/consumer.h> |
| 24 | #include "tsc200x-core.h" |
| 25 | |
| 26 | /* |
| 27 | * The touchscreen interface operates as follows: |
| 28 | * |
| 29 | * 1) Pen is pressed against the touchscreen. |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 30 | * 2) TSC200X performs AD conversion. |
| 31 | * 3) After the conversion is done TSC200X drives DAV line down. |
| 32 | * 4) GPIO IRQ is received and tsc200x_irq_thread() is scheduled. |
| 33 | * 5) tsc200x_irq_thread() queues up a transfer to fetch the x, y, z1, z2 |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 34 | * values. |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 35 | * 6) tsc200x_irq_thread() reports coordinates to input layer and sets up |
| 36 | * tsc200x_penup_timer() to be called after TSC200X_PENUP_TIME_MS (40ms). |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 37 | * 7) When the penup timer expires, there have not been touch or DAV interrupts |
| 38 | * during the last 40ms which means the pen has been lifted. |
| 39 | * |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 40 | * ESD recovery via a hardware reset is done if the TSC200X doesn't respond |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 41 | * after a configurable period (in ms) of activity. If esd_timeout is 0, the |
| 42 | * watchdog is disabled. |
| 43 | */ |
| 44 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 45 | static const struct regmap_range tsc200x_writable_ranges[] = { |
| 46 | regmap_reg_range(TSC200X_REG_AUX_HIGH, TSC200X_REG_CFR2), |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 49 | static const struct regmap_access_table tsc200x_writable_table = { |
| 50 | .yes_ranges = tsc200x_writable_ranges, |
| 51 | .n_yes_ranges = ARRAY_SIZE(tsc200x_writable_ranges), |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | const struct regmap_config tsc200x_regmap_config = { |
| 55 | .reg_bits = 8, |
| 56 | .val_bits = 16, |
| 57 | .reg_stride = 0x08, |
| 58 | .max_register = 0x78, |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 59 | .read_flag_mask = TSC200X_REG_READ, |
| 60 | .write_flag_mask = TSC200X_REG_PND0, |
| 61 | .wr_table = &tsc200x_writable_table, |
David Frey | 1c96a2f | 2018-09-01 09:50:41 -0700 | [diff] [blame] | 62 | .use_single_read = true, |
| 63 | .use_single_write = true, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 64 | }; |
| 65 | EXPORT_SYMBOL_GPL(tsc200x_regmap_config); |
| 66 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 67 | struct tsc200x_data { |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 68 | u16 x; |
| 69 | u16 y; |
| 70 | u16 z1; |
| 71 | u16 z2; |
| 72 | } __packed; |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 73 | #define TSC200X_DATA_REGS 4 |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 74 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 75 | struct tsc200x { |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 76 | struct device *dev; |
| 77 | struct regmap *regmap; |
| 78 | __u16 bustype; |
| 79 | |
| 80 | struct input_dev *idev; |
| 81 | char phys[32]; |
| 82 | |
| 83 | struct mutex mutex; |
| 84 | |
| 85 | /* raw copy of previous x,y,z */ |
| 86 | int in_x; |
| 87 | int in_y; |
| 88 | int in_z1; |
| 89 | int in_z2; |
| 90 | |
| 91 | spinlock_t lock; |
| 92 | struct timer_list penup_timer; |
| 93 | |
| 94 | unsigned int esd_timeout; |
| 95 | struct delayed_work esd_work; |
| 96 | unsigned long last_valid_interrupt; |
| 97 | |
| 98 | unsigned int x_plate_ohm; |
| 99 | |
| 100 | bool opened; |
| 101 | bool suspended; |
| 102 | |
| 103 | bool pen_down; |
| 104 | |
| 105 | struct regulator *vio; |
| 106 | |
| 107 | struct gpio_desc *reset_gpio; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 108 | int (*tsc200x_cmd)(struct device *dev, u8 cmd); |
| 109 | int irq; |
| 110 | }; |
| 111 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 112 | static void tsc200x_update_pen_state(struct tsc200x *ts, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 113 | int x, int y, int pressure) |
| 114 | { |
| 115 | if (pressure) { |
| 116 | input_report_abs(ts->idev, ABS_X, x); |
| 117 | input_report_abs(ts->idev, ABS_Y, y); |
| 118 | input_report_abs(ts->idev, ABS_PRESSURE, pressure); |
| 119 | if (!ts->pen_down) { |
| 120 | input_report_key(ts->idev, BTN_TOUCH, !!pressure); |
| 121 | ts->pen_down = true; |
| 122 | } |
| 123 | } else { |
| 124 | input_report_abs(ts->idev, ABS_PRESSURE, 0); |
| 125 | if (ts->pen_down) { |
| 126 | input_report_key(ts->idev, BTN_TOUCH, 0); |
| 127 | ts->pen_down = false; |
| 128 | } |
| 129 | } |
| 130 | input_sync(ts->idev); |
| 131 | dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y, |
| 132 | pressure); |
| 133 | } |
| 134 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 135 | static irqreturn_t tsc200x_irq_thread(int irq, void *_ts) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 136 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 137 | struct tsc200x *ts = _ts; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 138 | unsigned long flags; |
| 139 | unsigned int pressure; |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 140 | struct tsc200x_data tsdata; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 141 | int error; |
| 142 | |
| 143 | /* read the coordinates */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 144 | error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata, |
| 145 | TSC200X_DATA_REGS); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 146 | if (unlikely(error)) |
| 147 | goto out; |
| 148 | |
| 149 | /* validate position */ |
| 150 | if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT)) |
| 151 | goto out; |
| 152 | |
| 153 | /* Skip reading if the pressure components are out of range */ |
| 154 | if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT)) |
| 155 | goto out; |
| 156 | if (unlikely(tsdata.z1 >= tsdata.z2)) |
| 157 | goto out; |
| 158 | |
| 159 | /* |
| 160 | * Skip point if this is a pen down with the exact same values as |
| 161 | * the value before pen-up - that implies SPI fed us stale data |
| 162 | */ |
| 163 | if (!ts->pen_down && |
| 164 | ts->in_x == tsdata.x && ts->in_y == tsdata.y && |
| 165 | ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) { |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * At this point we are happy we have a valid and useful reading. |
| 171 | * Remember it for later comparisons. We may now begin downsampling. |
| 172 | */ |
| 173 | ts->in_x = tsdata.x; |
| 174 | ts->in_y = tsdata.y; |
| 175 | ts->in_z1 = tsdata.z1; |
| 176 | ts->in_z2 = tsdata.z2; |
| 177 | |
| 178 | /* Compute touch pressure resistance using equation #1 */ |
| 179 | pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1; |
| 180 | pressure = pressure * ts->x_plate_ohm / 4096; |
| 181 | if (unlikely(pressure > MAX_12BIT)) |
| 182 | goto out; |
| 183 | |
| 184 | spin_lock_irqsave(&ts->lock, flags); |
| 185 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 186 | tsc200x_update_pen_state(ts, tsdata.x, tsdata.y, pressure); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 187 | mod_timer(&ts->penup_timer, |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 188 | jiffies + msecs_to_jiffies(TSC200X_PENUP_TIME_MS)); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 189 | |
| 190 | spin_unlock_irqrestore(&ts->lock, flags); |
| 191 | |
| 192 | ts->last_valid_interrupt = jiffies; |
| 193 | out: |
| 194 | return IRQ_HANDLED; |
| 195 | } |
| 196 | |
Kees Cook | ee03e3f | 2017-10-24 09:44:48 -0700 | [diff] [blame] | 197 | static void tsc200x_penup_timer(struct timer_list *t) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 198 | { |
Kees Cook | ee03e3f | 2017-10-24 09:44:48 -0700 | [diff] [blame] | 199 | struct tsc200x *ts = from_timer(ts, t, penup_timer); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 200 | unsigned long flags; |
| 201 | |
| 202 | spin_lock_irqsave(&ts->lock, flags); |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 203 | tsc200x_update_pen_state(ts, 0, 0, 0); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 204 | spin_unlock_irqrestore(&ts->lock, flags); |
| 205 | } |
| 206 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 207 | static void tsc200x_start_scan(struct tsc200x *ts) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 208 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 209 | regmap_write(ts->regmap, TSC200X_REG_CFR0, TSC200X_CFR0_INITVALUE); |
| 210 | regmap_write(ts->regmap, TSC200X_REG_CFR1, TSC200X_CFR1_INITVALUE); |
| 211 | regmap_write(ts->regmap, TSC200X_REG_CFR2, TSC200X_CFR2_INITVALUE); |
| 212 | ts->tsc200x_cmd(ts->dev, TSC200X_CMD_NORMAL); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 215 | static void tsc200x_stop_scan(struct tsc200x *ts) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 216 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 217 | ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 220 | static void tsc200x_reset(struct tsc200x *ts) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 221 | { |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 222 | if (ts->reset_gpio) { |
| 223 | gpiod_set_value_cansleep(ts->reset_gpio, 1); |
| 224 | usleep_range(100, 500); /* only 10us required */ |
| 225 | gpiod_set_value_cansleep(ts->reset_gpio, 0); |
| 226 | } |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* must be called with ts->mutex held */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 230 | static void __tsc200x_disable(struct tsc200x *ts) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 231 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 232 | tsc200x_stop_scan(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 233 | |
| 234 | disable_irq(ts->irq); |
| 235 | del_timer_sync(&ts->penup_timer); |
| 236 | |
| 237 | cancel_delayed_work_sync(&ts->esd_work); |
| 238 | |
| 239 | enable_irq(ts->irq); |
| 240 | } |
| 241 | |
| 242 | /* must be called with ts->mutex held */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 243 | static void __tsc200x_enable(struct tsc200x *ts) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 244 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 245 | tsc200x_start_scan(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 246 | |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 247 | if (ts->esd_timeout && ts->reset_gpio) { |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 248 | ts->last_valid_interrupt = jiffies; |
| 249 | schedule_delayed_work(&ts->esd_work, |
| 250 | round_jiffies_relative( |
| 251 | msecs_to_jiffies(ts->esd_timeout))); |
| 252 | } |
| 253 | } |
| 254 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 255 | static ssize_t tsc200x_selftest_show(struct device *dev, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 256 | struct device_attribute *attr, |
| 257 | char *buf) |
| 258 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 259 | struct tsc200x *ts = dev_get_drvdata(dev); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 260 | unsigned int temp_high; |
| 261 | unsigned int temp_high_orig; |
| 262 | unsigned int temp_high_test; |
| 263 | bool success = true; |
| 264 | int error; |
| 265 | |
| 266 | mutex_lock(&ts->mutex); |
| 267 | |
| 268 | /* |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 269 | * Test TSC200X communications via temp high register. |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 270 | */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 271 | __tsc200x_disable(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 272 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 273 | error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 274 | if (error) { |
| 275 | dev_warn(dev, "selftest failed: read error %d\n", error); |
| 276 | success = false; |
| 277 | goto out; |
| 278 | } |
| 279 | |
| 280 | temp_high_test = (temp_high_orig - 1) & MAX_12BIT; |
| 281 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 282 | error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 283 | if (error) { |
| 284 | dev_warn(dev, "selftest failed: write error %d\n", error); |
| 285 | success = false; |
| 286 | goto out; |
| 287 | } |
| 288 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 289 | error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 290 | if (error) { |
| 291 | dev_warn(dev, "selftest failed: read error %d after write\n", |
| 292 | error); |
| 293 | success = false; |
| 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | if (temp_high != temp_high_test) { |
| 298 | dev_warn(dev, "selftest failed: %d != %d\n", |
| 299 | temp_high, temp_high_test); |
| 300 | success = false; |
| 301 | } |
| 302 | |
| 303 | /* hardware reset */ |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 304 | tsc200x_reset(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 305 | |
| 306 | if (!success) |
| 307 | goto out; |
| 308 | |
| 309 | /* test that the reset really happened */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 310 | error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 311 | if (error) { |
| 312 | dev_warn(dev, "selftest failed: read error %d after reset\n", |
| 313 | error); |
| 314 | success = false; |
| 315 | goto out; |
| 316 | } |
| 317 | |
| 318 | if (temp_high != temp_high_orig) { |
| 319 | dev_warn(dev, "selftest failed after reset: %d != %d\n", |
| 320 | temp_high, temp_high_orig); |
| 321 | success = false; |
| 322 | } |
| 323 | |
| 324 | out: |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 325 | __tsc200x_enable(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 326 | mutex_unlock(&ts->mutex); |
| 327 | |
| 328 | return sprintf(buf, "%d\n", success); |
| 329 | } |
| 330 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 331 | static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 332 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 333 | static struct attribute *tsc200x_attrs[] = { |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 334 | &dev_attr_selftest.attr, |
| 335 | NULL |
| 336 | }; |
| 337 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 338 | static umode_t tsc200x_attr_is_visible(struct kobject *kobj, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 339 | struct attribute *attr, int n) |
| 340 | { |
| 341 | struct device *dev = container_of(kobj, struct device, kobj); |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 342 | struct tsc200x *ts = dev_get_drvdata(dev); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 343 | umode_t mode = attr->mode; |
| 344 | |
| 345 | if (attr == &dev_attr_selftest.attr) { |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 346 | if (!ts->reset_gpio) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 347 | mode = 0; |
| 348 | } |
| 349 | |
| 350 | return mode; |
| 351 | } |
| 352 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 353 | static const struct attribute_group tsc200x_attr_group = { |
| 354 | .is_visible = tsc200x_attr_is_visible, |
| 355 | .attrs = tsc200x_attrs, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 356 | }; |
| 357 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 358 | static void tsc200x_esd_work(struct work_struct *work) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 359 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 360 | struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 361 | int error; |
| 362 | unsigned int r; |
| 363 | |
| 364 | if (!mutex_trylock(&ts->mutex)) { |
| 365 | /* |
| 366 | * If the mutex is taken, it means that disable or enable is in |
| 367 | * progress. In that case just reschedule the work. If the work |
| 368 | * is not needed, it will be canceled by disable. |
| 369 | */ |
| 370 | goto reschedule; |
| 371 | } |
| 372 | |
| 373 | if (time_is_after_jiffies(ts->last_valid_interrupt + |
| 374 | msecs_to_jiffies(ts->esd_timeout))) |
| 375 | goto out; |
| 376 | |
| 377 | /* We should be able to read register without disabling interrupts. */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 378 | error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 379 | if (!error && |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 380 | !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) { |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * If we could not read our known value from configuration register 0 |
| 386 | * then we should reset the controller as if from power-up and start |
| 387 | * scanning again. |
| 388 | */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 389 | dev_info(ts->dev, "TSC200X not responding - resetting\n"); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 390 | |
| 391 | disable_irq(ts->irq); |
| 392 | del_timer_sync(&ts->penup_timer); |
| 393 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 394 | tsc200x_update_pen_state(ts, 0, 0, 0); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 395 | |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 396 | tsc200x_reset(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 397 | |
| 398 | enable_irq(ts->irq); |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 399 | tsc200x_start_scan(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 400 | |
| 401 | out: |
| 402 | mutex_unlock(&ts->mutex); |
| 403 | reschedule: |
| 404 | /* re-arm the watchdog */ |
| 405 | schedule_delayed_work(&ts->esd_work, |
| 406 | round_jiffies_relative( |
| 407 | msecs_to_jiffies(ts->esd_timeout))); |
| 408 | } |
| 409 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 410 | static int tsc200x_open(struct input_dev *input) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 411 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 412 | struct tsc200x *ts = input_get_drvdata(input); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 413 | |
| 414 | mutex_lock(&ts->mutex); |
| 415 | |
| 416 | if (!ts->suspended) |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 417 | __tsc200x_enable(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 418 | |
| 419 | ts->opened = true; |
| 420 | |
| 421 | mutex_unlock(&ts->mutex); |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 426 | static void tsc200x_close(struct input_dev *input) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 427 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 428 | struct tsc200x *ts = input_get_drvdata(input); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 429 | |
| 430 | mutex_lock(&ts->mutex); |
| 431 | |
| 432 | if (!ts->suspended) |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 433 | __tsc200x_disable(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 434 | |
| 435 | ts->opened = false; |
| 436 | |
| 437 | mutex_unlock(&ts->mutex); |
| 438 | } |
| 439 | |
Michael Welling | e9003c9 | 2016-07-20 10:02:07 -0700 | [diff] [blame] | 440 | int tsc200x_probe(struct device *dev, int irq, const struct input_id *tsc_id, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 441 | struct regmap *regmap, |
| 442 | int (*tsc200x_cmd)(struct device *dev, u8 cmd)) |
| 443 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 444 | struct tsc200x *ts; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 445 | struct input_dev *input_dev; |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 446 | u32 x_plate_ohm; |
| 447 | u32 esd_timeout; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 448 | int error; |
| 449 | |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 450 | if (irq <= 0) { |
| 451 | dev_err(dev, "no irq\n"); |
| 452 | return -ENODEV; |
| 453 | } |
| 454 | |
| 455 | if (IS_ERR(regmap)) |
| 456 | return PTR_ERR(regmap); |
| 457 | |
| 458 | if (!tsc200x_cmd) { |
| 459 | dev_err(dev, "no cmd function\n"); |
| 460 | return -ENODEV; |
| 461 | } |
| 462 | |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 463 | ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); |
| 464 | if (!ts) |
| 465 | return -ENOMEM; |
| 466 | |
| 467 | input_dev = devm_input_allocate_device(dev); |
| 468 | if (!input_dev) |
| 469 | return -ENOMEM; |
| 470 | |
| 471 | ts->irq = irq; |
| 472 | ts->dev = dev; |
| 473 | ts->idev = input_dev; |
| 474 | ts->regmap = regmap; |
| 475 | ts->tsc200x_cmd = tsc200x_cmd; |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 476 | |
| 477 | error = device_property_read_u32(dev, "ti,x-plate-ohms", &x_plate_ohm); |
| 478 | ts->x_plate_ohm = error ? TSC200X_DEF_RESISTOR : x_plate_ohm; |
| 479 | |
| 480 | error = device_property_read_u32(dev, "ti,esd-recovery-timeout-ms", |
| 481 | &esd_timeout); |
| 482 | ts->esd_timeout = error ? 0 : esd_timeout; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 483 | |
| 484 | ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
| 485 | if (IS_ERR(ts->reset_gpio)) { |
| 486 | error = PTR_ERR(ts->reset_gpio); |
| 487 | dev_err(dev, "error acquiring reset gpio: %d\n", error); |
| 488 | return error; |
| 489 | } |
| 490 | |
Dmitry Torokhov | f7bf6f5 | 2017-02-10 15:30:29 -0800 | [diff] [blame] | 491 | ts->vio = devm_regulator_get(dev, "vio"); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 492 | if (IS_ERR(ts->vio)) { |
| 493 | error = PTR_ERR(ts->vio); |
Dmitry Torokhov | f7bf6f5 | 2017-02-10 15:30:29 -0800 | [diff] [blame] | 494 | dev_err(dev, "error acquiring vio regulator: %d", error); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 495 | return error; |
| 496 | } |
| 497 | |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 498 | mutex_init(&ts->mutex); |
| 499 | |
| 500 | spin_lock_init(&ts->lock); |
Kees Cook | ee03e3f | 2017-10-24 09:44:48 -0700 | [diff] [blame] | 501 | timer_setup(&ts->penup_timer, tsc200x_penup_timer, 0); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 502 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 503 | INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 504 | |
| 505 | snprintf(ts->phys, sizeof(ts->phys), |
| 506 | "%s/input-ts", dev_name(dev)); |
| 507 | |
Michael Welling | e9003c9 | 2016-07-20 10:02:07 -0700 | [diff] [blame] | 508 | if (tsc_id->product == 2004) { |
| 509 | input_dev->name = "TSC200X touchscreen"; |
| 510 | } else { |
| 511 | input_dev->name = devm_kasprintf(dev, GFP_KERNEL, |
| 512 | "TSC%04d touchscreen", |
| 513 | tsc_id->product); |
| 514 | if (!input_dev->name) |
| 515 | return -ENOMEM; |
| 516 | } |
| 517 | |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 518 | input_dev->phys = ts->phys; |
Michael Welling | e9003c9 | 2016-07-20 10:02:07 -0700 | [diff] [blame] | 519 | input_dev->id = *tsc_id; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 520 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 521 | input_dev->open = tsc200x_open; |
| 522 | input_dev->close = tsc200x_close; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 523 | |
| 524 | input_set_drvdata(input_dev, ts); |
| 525 | |
Martin Kepplinger | eca2539 | 2017-11-07 16:20:49 -0800 | [diff] [blame] | 526 | __set_bit(INPUT_PROP_DIRECT, input_dev->propbit); |
Dmitry Torokhov | d0d8949 | 2017-02-10 15:18:07 -0800 | [diff] [blame] | 527 | input_set_capability(input_dev, EV_KEY, BTN_TOUCH); |
| 528 | |
| 529 | input_set_abs_params(input_dev, ABS_X, |
| 530 | 0, MAX_12BIT, TSC200X_DEF_X_FUZZ, 0); |
| 531 | input_set_abs_params(input_dev, ABS_Y, |
| 532 | 0, MAX_12BIT, TSC200X_DEF_Y_FUZZ, 0); |
| 533 | input_set_abs_params(input_dev, ABS_PRESSURE, |
| 534 | 0, MAX_12BIT, TSC200X_DEF_P_FUZZ, 0); |
| 535 | |
| 536 | touchscreen_parse_properties(input_dev, false, NULL); |
| 537 | |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 538 | /* Ensure the touchscreen is off */ |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 539 | tsc200x_stop_scan(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 540 | |
| 541 | error = devm_request_threaded_irq(dev, irq, NULL, |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 542 | tsc200x_irq_thread, |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 543 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 544 | "tsc200x", ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 545 | if (error) { |
| 546 | dev_err(dev, "Failed to request irq, err: %d\n", error); |
| 547 | return error; |
| 548 | } |
| 549 | |
Dmitry Torokhov | f7bf6f5 | 2017-02-10 15:30:29 -0800 | [diff] [blame] | 550 | error = regulator_enable(ts->vio); |
| 551 | if (error) |
| 552 | return error; |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 553 | |
| 554 | dev_set_drvdata(dev, ts); |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 555 | error = sysfs_create_group(&dev->kobj, &tsc200x_attr_group); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 556 | if (error) { |
| 557 | dev_err(dev, |
| 558 | "Failed to create sysfs attributes, err: %d\n", error); |
| 559 | goto disable_regulator; |
| 560 | } |
| 561 | |
| 562 | error = input_register_device(ts->idev); |
| 563 | if (error) { |
| 564 | dev_err(dev, |
| 565 | "Failed to register input device, err: %d\n", error); |
| 566 | goto err_remove_sysfs; |
| 567 | } |
| 568 | |
| 569 | irq_set_irq_wake(irq, 1); |
| 570 | return 0; |
| 571 | |
| 572 | err_remove_sysfs: |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 573 | sysfs_remove_group(&dev->kobj, &tsc200x_attr_group); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 574 | disable_regulator: |
Dmitry Torokhov | f7bf6f5 | 2017-02-10 15:30:29 -0800 | [diff] [blame] | 575 | regulator_disable(ts->vio); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 576 | return error; |
| 577 | } |
| 578 | EXPORT_SYMBOL_GPL(tsc200x_probe); |
| 579 | |
| 580 | int tsc200x_remove(struct device *dev) |
| 581 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 582 | struct tsc200x *ts = dev_get_drvdata(dev); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 583 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 584 | sysfs_remove_group(&dev->kobj, &tsc200x_attr_group); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 585 | |
Dmitry Torokhov | f7bf6f5 | 2017-02-10 15:30:29 -0800 | [diff] [blame] | 586 | regulator_disable(ts->vio); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | EXPORT_SYMBOL_GPL(tsc200x_remove); |
| 591 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 592 | static int __maybe_unused tsc200x_suspend(struct device *dev) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 593 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 594 | struct tsc200x *ts = dev_get_drvdata(dev); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 595 | |
| 596 | mutex_lock(&ts->mutex); |
| 597 | |
| 598 | if (!ts->suspended && ts->opened) |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 599 | __tsc200x_disable(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 600 | |
| 601 | ts->suspended = true; |
| 602 | |
| 603 | mutex_unlock(&ts->mutex); |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 608 | static int __maybe_unused tsc200x_resume(struct device *dev) |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 609 | { |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 610 | struct tsc200x *ts = dev_get_drvdata(dev); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 611 | |
| 612 | mutex_lock(&ts->mutex); |
| 613 | |
| 614 | if (ts->suspended && ts->opened) |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 615 | __tsc200x_enable(ts); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 616 | |
| 617 | ts->suspended = false; |
| 618 | |
| 619 | mutex_unlock(&ts->mutex); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 624 | SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 625 | EXPORT_SYMBOL_GPL(tsc200x_pm_ops); |
| 626 | |
| 627 | MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>"); |
Michael Welling | ef3b98c | 2015-11-02 17:51:49 -0800 | [diff] [blame] | 628 | MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core"); |
Michael Welling | 6ac2438 | 2015-11-02 17:45:51 -0800 | [diff] [blame] | 629 | MODULE_LICENSE("GPL"); |