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