Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * TSC2005 touchscreen driver |
| 3 | * |
| 4 | * Copyright (C) 2006-2010 Nokia Corporation |
| 5 | * |
| 6 | * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com> |
| 7 | * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/input.h> |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 28 | #include <linux/input/touchscreen.h> |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/delay.h> |
Dmitry Torokhov | 3ff8ff5 | 2011-03-16 22:08:26 -0700 | [diff] [blame] | 31 | #include <linux/pm.h> |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 32 | #include <linux/of.h> |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 33 | #include <linux/spi/spi.h> |
| 34 | #include <linux/spi/tsc2005.h> |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 35 | #include <linux/regulator/consumer.h> |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 36 | #include <linux/regmap.h> |
Sebastian Reichel | d257f29 | 2015-07-27 17:28:58 -0700 | [diff] [blame^] | 37 | #include <linux/gpio/consumer.h> |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * The touchscreen interface operates as follows: |
| 41 | * |
| 42 | * 1) Pen is pressed against the touchscreen. |
| 43 | * 2) TSC2005 performs AD conversion. |
| 44 | * 3) After the conversion is done TSC2005 drives DAV line down. |
| 45 | * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled. |
| 46 | * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2 |
| 47 | * values. |
| 48 | * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up |
| 49 | * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms). |
| 50 | * 7) When the penup timer expires, there have not been touch or DAV interrupts |
| 51 | * during the last 40ms which means the pen has been lifted. |
| 52 | * |
| 53 | * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond |
| 54 | * after a configurable period (in ms) of activity. If esd_timeout is 0, the |
| 55 | * watchdog is disabled. |
| 56 | */ |
| 57 | |
| 58 | /* control byte 1 */ |
| 59 | #define TSC2005_CMD 0x80 |
| 60 | #define TSC2005_CMD_NORMAL 0x00 |
| 61 | #define TSC2005_CMD_STOP 0x01 |
| 62 | #define TSC2005_CMD_12BIT 0x04 |
| 63 | |
| 64 | /* control byte 0 */ |
Sebastian Reichel | a636df9 | 2015-07-27 17:26:38 -0700 | [diff] [blame] | 65 | #define TSC2005_REG_READ 0x01 /* R/W access */ |
| 66 | #define TSC2005_REG_PND0 0x02 /* Power Not Down Control */ |
| 67 | #define TSC2005_REG_X (0x0 << 3) |
| 68 | #define TSC2005_REG_Y (0x1 << 3) |
| 69 | #define TSC2005_REG_Z1 (0x2 << 3) |
| 70 | #define TSC2005_REG_Z2 (0x3 << 3) |
| 71 | #define TSC2005_REG_AUX (0x4 << 3) |
| 72 | #define TSC2005_REG_TEMP1 (0x5 << 3) |
| 73 | #define TSC2005_REG_TEMP2 (0x6 << 3) |
| 74 | #define TSC2005_REG_STATUS (0x7 << 3) |
| 75 | #define TSC2005_REG_AUX_HIGH (0x8 << 3) |
| 76 | #define TSC2005_REG_AUX_LOW (0x9 << 3) |
| 77 | #define TSC2005_REG_TEMP_HIGH (0xA << 3) |
| 78 | #define TSC2005_REG_TEMP_LOW (0xB << 3) |
| 79 | #define TSC2005_REG_CFR0 (0xC << 3) |
| 80 | #define TSC2005_REG_CFR1 (0xD << 3) |
| 81 | #define TSC2005_REG_CFR2 (0xE << 3) |
| 82 | #define TSC2005_REG_CONV_FUNC (0xF << 3) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* configuration register 0 */ |
| 85 | #define TSC2005_CFR0_PRECHARGE_276US 0x0040 |
| 86 | #define TSC2005_CFR0_STABTIME_1MS 0x0300 |
| 87 | #define TSC2005_CFR0_CLOCK_1MHZ 0x1000 |
| 88 | #define TSC2005_CFR0_RESOLUTION12 0x2000 |
| 89 | #define TSC2005_CFR0_PENMODE 0x8000 |
| 90 | #define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \ |
| 91 | TSC2005_CFR0_CLOCK_1MHZ | \ |
| 92 | TSC2005_CFR0_RESOLUTION12 | \ |
| 93 | TSC2005_CFR0_PRECHARGE_276US | \ |
| 94 | TSC2005_CFR0_PENMODE) |
| 95 | |
| 96 | /* bits common to both read and write of configuration register 0 */ |
| 97 | #define TSC2005_CFR0_RW_MASK 0x3fff |
| 98 | |
| 99 | /* configuration register 1 */ |
| 100 | #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003 |
| 101 | #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS |
| 102 | |
| 103 | /* configuration register 2 */ |
| 104 | #define TSC2005_CFR2_MAVE_Z 0x0004 |
| 105 | #define TSC2005_CFR2_MAVE_Y 0x0008 |
| 106 | #define TSC2005_CFR2_MAVE_X 0x0010 |
| 107 | #define TSC2005_CFR2_AVG_7 0x0800 |
| 108 | #define TSC2005_CFR2_MEDIUM_15 0x3000 |
| 109 | #define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \ |
| 110 | TSC2005_CFR2_MAVE_Y | \ |
| 111 | TSC2005_CFR2_MAVE_Z | \ |
| 112 | TSC2005_CFR2_MEDIUM_15 | \ |
| 113 | TSC2005_CFR2_AVG_7) |
| 114 | |
| 115 | #define MAX_12BIT 0xfff |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 116 | #define TSC2005_DEF_X_FUZZ 4 |
| 117 | #define TSC2005_DEF_Y_FUZZ 8 |
| 118 | #define TSC2005_DEF_P_FUZZ 2 |
| 119 | #define TSC2005_DEF_RESISTOR 280 |
| 120 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 121 | #define TSC2005_SPI_MAX_SPEED_HZ 10000000 |
| 122 | #define TSC2005_PENUP_TIME_MS 40 |
| 123 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 124 | static const struct regmap_range tsc2005_writable_ranges[] = { |
| 125 | regmap_reg_range(TSC2005_REG_AUX_HIGH, TSC2005_REG_CFR2), |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 128 | static const struct regmap_access_table tsc2005_writable_table = { |
| 129 | .yes_ranges = tsc2005_writable_ranges, |
| 130 | .n_yes_ranges = ARRAY_SIZE(tsc2005_writable_ranges), |
| 131 | }; |
| 132 | |
| 133 | static struct regmap_config tsc2005_regmap_config = { |
| 134 | .reg_bits = 8, |
| 135 | .val_bits = 16, |
| 136 | .reg_stride = 0x08, |
| 137 | .max_register = 0x78, |
| 138 | .read_flag_mask = TSC2005_REG_READ, |
| 139 | .write_flag_mask = TSC2005_REG_PND0, |
| 140 | .wr_table = &tsc2005_writable_table, |
| 141 | .use_single_rw = true, |
| 142 | }; |
| 143 | |
| 144 | struct tsc2005_data { |
| 145 | u16 x; |
| 146 | u16 y; |
| 147 | u16 z1; |
| 148 | u16 z2; |
| 149 | } __packed; |
| 150 | #define TSC2005_DATA_REGS 4 |
| 151 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 152 | struct tsc2005 { |
| 153 | struct spi_device *spi; |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 154 | struct regmap *regmap; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 155 | |
| 156 | struct input_dev *idev; |
| 157 | char phys[32]; |
| 158 | |
| 159 | struct mutex mutex; |
| 160 | |
| 161 | /* raw copy of previous x,y,z */ |
| 162 | int in_x; |
| 163 | int in_y; |
| 164 | int in_z1; |
| 165 | int in_z2; |
| 166 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 167 | spinlock_t lock; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 168 | struct timer_list penup_timer; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 169 | |
| 170 | unsigned int esd_timeout; |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 171 | struct delayed_work esd_work; |
| 172 | unsigned long last_valid_interrupt; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 173 | |
| 174 | unsigned int x_plate_ohm; |
| 175 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 176 | bool opened; |
| 177 | bool suspended; |
Dmitry Torokhov | c8b6846 | 2011-03-16 22:10:46 -0700 | [diff] [blame] | 178 | |
| 179 | bool pen_down; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 180 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 181 | struct regulator *vio; |
| 182 | |
Sebastian Reichel | d257f29 | 2015-07-27 17:28:58 -0700 | [diff] [blame^] | 183 | struct gpio_desc *reset_gpio; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 184 | void (*set_reset)(bool enable); |
| 185 | }; |
| 186 | |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 187 | static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 188 | { |
Dmitry Torokhov | 9a6e180 | 2011-03-16 22:10:52 -0700 | [diff] [blame] | 189 | u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd; |
| 190 | struct spi_transfer xfer = { |
| 191 | .tx_buf = &tx, |
| 192 | .len = 1, |
| 193 | .bits_per_word = 8, |
| 194 | }; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 195 | struct spi_message msg; |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 196 | int error; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 197 | |
| 198 | spi_message_init(&msg); |
| 199 | spi_message_add_tail(&xfer, &msg); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 200 | |
| 201 | error = spi_sync(ts->spi, &msg); |
| 202 | if (error) { |
| 203 | dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n", |
| 204 | __func__, cmd, error); |
| 205 | return error; |
| 206 | } |
| 207 | |
| 208 | return 0; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 211 | static void tsc2005_update_pen_state(struct tsc2005 *ts, |
| 212 | int x, int y, int pressure) |
| 213 | { |
| 214 | if (pressure) { |
| 215 | input_report_abs(ts->idev, ABS_X, x); |
| 216 | input_report_abs(ts->idev, ABS_Y, y); |
| 217 | input_report_abs(ts->idev, ABS_PRESSURE, pressure); |
| 218 | if (!ts->pen_down) { |
| 219 | input_report_key(ts->idev, BTN_TOUCH, !!pressure); |
Dmitry Torokhov | c8b6846 | 2011-03-16 22:10:46 -0700 | [diff] [blame] | 220 | ts->pen_down = true; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 221 | } |
| 222 | } else { |
| 223 | input_report_abs(ts->idev, ABS_PRESSURE, 0); |
| 224 | if (ts->pen_down) { |
| 225 | input_report_key(ts->idev, BTN_TOUCH, 0); |
Dmitry Torokhov | c8b6846 | 2011-03-16 22:10:46 -0700 | [diff] [blame] | 226 | ts->pen_down = false; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | input_sync(ts->idev); |
| 230 | dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y, |
| 231 | pressure); |
| 232 | } |
| 233 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 234 | static irqreturn_t tsc2005_irq_thread(int irq, void *_ts) |
| 235 | { |
| 236 | struct tsc2005 *ts = _ts; |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 237 | unsigned long flags; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 238 | unsigned int pressure; |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 239 | struct tsc2005_data tsdata; |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 240 | int error; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 241 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 242 | /* read the coordinates */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 243 | error = regmap_bulk_read(ts->regmap, TSC2005_REG_X, &tsdata, |
| 244 | TSC2005_DATA_REGS); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 245 | if (unlikely(error)) |
| 246 | goto out; |
| 247 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 248 | /* validate position */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 249 | if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT)) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 250 | goto out; |
| 251 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 252 | /* Skip reading if the pressure components are out of range */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 253 | if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT)) |
| 254 | goto out; |
| 255 | if (unlikely(tsdata.z1 >= tsdata.z2)) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 256 | goto out; |
| 257 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 258 | /* |
| 259 | * Skip point if this is a pen down with the exact same values as |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 260 | * the value before pen-up - that implies SPI fed us stale data |
| 261 | */ |
| 262 | if (!ts->pen_down && |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 263 | ts->in_x == tsdata.x && ts->in_y == tsdata.y && |
| 264 | ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) { |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 265 | goto out; |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 266 | } |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 267 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 268 | /* |
| 269 | * At this point we are happy we have a valid and useful reading. |
| 270 | * Remember it for later comparisons. We may now begin downsampling. |
| 271 | */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 272 | ts->in_x = tsdata.x; |
| 273 | ts->in_y = tsdata.y; |
| 274 | ts->in_z1 = tsdata.z1; |
| 275 | ts->in_z2 = tsdata.z2; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 276 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 277 | /* Compute touch pressure resistance using equation #1 */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 278 | pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 279 | pressure = pressure * ts->x_plate_ohm / 4096; |
| 280 | if (unlikely(pressure > MAX_12BIT)) |
| 281 | goto out; |
| 282 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 283 | spin_lock_irqsave(&ts->lock, flags); |
| 284 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 285 | tsc2005_update_pen_state(ts, tsdata.x, tsdata.y, pressure); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 286 | mod_timer(&ts->penup_timer, |
| 287 | jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS)); |
| 288 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 289 | spin_unlock_irqrestore(&ts->lock, flags); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 290 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 291 | ts->last_valid_interrupt = jiffies; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 292 | out: |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 293 | return IRQ_HANDLED; |
| 294 | } |
| 295 | |
| 296 | static void tsc2005_penup_timer(unsigned long data) |
| 297 | { |
| 298 | struct tsc2005 *ts = (struct tsc2005 *)data; |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 299 | unsigned long flags; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 300 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 301 | spin_lock_irqsave(&ts->lock, flags); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 302 | tsc2005_update_pen_state(ts, 0, 0, 0); |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 303 | spin_unlock_irqrestore(&ts->lock, flags); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void tsc2005_start_scan(struct tsc2005 *ts) |
| 307 | { |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 308 | regmap_write(ts->regmap, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE); |
| 309 | regmap_write(ts->regmap, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE); |
| 310 | regmap_write(ts->regmap, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 311 | tsc2005_cmd(ts, TSC2005_CMD_NORMAL); |
| 312 | } |
| 313 | |
| 314 | static void tsc2005_stop_scan(struct tsc2005 *ts) |
| 315 | { |
| 316 | tsc2005_cmd(ts, TSC2005_CMD_STOP); |
| 317 | } |
| 318 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 319 | static void tsc2005_set_reset(struct tsc2005 *ts, bool enable) |
| 320 | { |
Sebastian Reichel | d257f29 | 2015-07-27 17:28:58 -0700 | [diff] [blame^] | 321 | if (ts->reset_gpio) |
| 322 | gpiod_set_value_cansleep(ts->reset_gpio, enable); |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 323 | else if (ts->set_reset) |
| 324 | ts->set_reset(enable); |
| 325 | } |
| 326 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 327 | /* must be called with ts->mutex held */ |
| 328 | static void __tsc2005_disable(struct tsc2005 *ts) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 329 | { |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 330 | tsc2005_stop_scan(ts); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 331 | |
| 332 | disable_irq(ts->spi->irq); |
| 333 | del_timer_sync(&ts->penup_timer); |
| 334 | |
| 335 | cancel_delayed_work_sync(&ts->esd_work); |
| 336 | |
| 337 | enable_irq(ts->spi->irq); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 340 | /* must be called with ts->mutex held */ |
| 341 | static void __tsc2005_enable(struct tsc2005 *ts) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 342 | { |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 343 | tsc2005_start_scan(ts); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 344 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 345 | if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) { |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 346 | ts->last_valid_interrupt = jiffies; |
| 347 | schedule_delayed_work(&ts->esd_work, |
Aaro Koskinen | 9034279 | 2011-03-23 23:45:11 -0700 | [diff] [blame] | 348 | round_jiffies_relative( |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 349 | msecs_to_jiffies(ts->esd_timeout))); |
| 350 | } |
| 351 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 354 | static ssize_t tsc2005_selftest_show(struct device *dev, |
| 355 | struct device_attribute *attr, |
| 356 | char *buf) |
| 357 | { |
Sebastian Reichel | 80b46aa | 2015-07-27 17:28:37 -0700 | [diff] [blame] | 358 | struct tsc2005 *ts = dev_get_drvdata(dev); |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 359 | unsigned int temp_high; |
| 360 | unsigned int temp_high_orig; |
| 361 | unsigned int temp_high_test; |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 362 | bool success = true; |
| 363 | int error; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 364 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 365 | mutex_lock(&ts->mutex); |
| 366 | |
| 367 | /* |
| 368 | * Test TSC2005 communications via temp high register. |
| 369 | */ |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 370 | __tsc2005_disable(ts); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 371 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 372 | error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high_orig); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 373 | if (error) { |
| 374 | dev_warn(dev, "selftest failed: read error %d\n", error); |
| 375 | success = false; |
| 376 | goto out; |
| 377 | } |
| 378 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 379 | temp_high_test = (temp_high_orig - 1) & MAX_12BIT; |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 380 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 381 | error = regmap_write(ts->regmap, TSC2005_REG_TEMP_HIGH, temp_high_test); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 382 | if (error) { |
| 383 | dev_warn(dev, "selftest failed: write error %d\n", error); |
| 384 | success = false; |
| 385 | goto out; |
| 386 | } |
| 387 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 388 | error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 389 | if (error) { |
| 390 | dev_warn(dev, "selftest failed: read error %d after write\n", |
| 391 | error); |
| 392 | success = false; |
| 393 | goto out; |
| 394 | } |
| 395 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 396 | if (temp_high != temp_high_test) { |
| 397 | dev_warn(dev, "selftest failed: %d != %d\n", |
| 398 | temp_high, temp_high_test); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 399 | success = false; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* hardware reset */ |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 403 | tsc2005_set_reset(ts, false); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 404 | usleep_range(100, 500); /* only 10us required */ |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 405 | tsc2005_set_reset(ts, true); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 406 | |
| 407 | if (!success) |
| 408 | goto out; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 409 | |
| 410 | /* test that the reset really happened */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 411 | error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 412 | if (error) { |
| 413 | dev_warn(dev, "selftest failed: read error %d after reset\n", |
| 414 | error); |
| 415 | success = false; |
| 416 | goto out; |
| 417 | } |
| 418 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 419 | if (temp_high != temp_high_orig) { |
| 420 | dev_warn(dev, "selftest failed after reset: %d != %d\n", |
| 421 | temp_high, temp_high_orig); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 422 | success = false; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 425 | out: |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 426 | __tsc2005_enable(ts); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 427 | mutex_unlock(&ts->mutex); |
| 428 | |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 429 | return sprintf(buf, "%d\n", success); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 430 | } |
Dmitry Torokhov | 8dbcc43 | 2011-03-16 22:10:37 -0700 | [diff] [blame] | 431 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 432 | static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL); |
| 433 | |
Dmitry Torokhov | 8dbcc43 | 2011-03-16 22:10:37 -0700 | [diff] [blame] | 434 | static struct attribute *tsc2005_attrs[] = { |
Dmitry Torokhov | 8dbcc43 | 2011-03-16 22:10:37 -0700 | [diff] [blame] | 435 | &dev_attr_selftest.attr, |
| 436 | NULL |
| 437 | }; |
| 438 | |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 439 | static umode_t tsc2005_attr_is_visible(struct kobject *kobj, |
Dmitry Torokhov | 8dbcc43 | 2011-03-16 22:10:37 -0700 | [diff] [blame] | 440 | struct attribute *attr, int n) |
| 441 | { |
| 442 | struct device *dev = container_of(kobj, struct device, kobj); |
Sebastian Reichel | 80b46aa | 2015-07-27 17:28:37 -0700 | [diff] [blame] | 443 | struct tsc2005 *ts = dev_get_drvdata(dev); |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 444 | umode_t mode = attr->mode; |
Dmitry Torokhov | 8dbcc43 | 2011-03-16 22:10:37 -0700 | [diff] [blame] | 445 | |
| 446 | if (attr == &dev_attr_selftest.attr) { |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 447 | if (!ts->set_reset && !ts->reset_gpio) |
Dmitry Torokhov | 8dbcc43 | 2011-03-16 22:10:37 -0700 | [diff] [blame] | 448 | mode = 0; |
| 449 | } |
| 450 | |
| 451 | return mode; |
| 452 | } |
| 453 | |
| 454 | static const struct attribute_group tsc2005_attr_group = { |
| 455 | .is_visible = tsc2005_attr_is_visible, |
| 456 | .attrs = tsc2005_attrs, |
| 457 | }; |
| 458 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 459 | static void tsc2005_esd_work(struct work_struct *work) |
| 460 | { |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 461 | struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work); |
Dmitry Torokhov | 71f8004 | 2011-03-16 22:11:25 -0700 | [diff] [blame] | 462 | int error; |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 463 | unsigned int r; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 464 | |
Aaro Koskinen | a0fa220 | 2011-03-23 23:48:19 -0700 | [diff] [blame] | 465 | if (!mutex_trylock(&ts->mutex)) { |
| 466 | /* |
| 467 | * If the mutex is taken, it means that disable or enable is in |
| 468 | * progress. In that case just reschedule the work. If the work |
| 469 | * is not needed, it will be canceled by disable. |
| 470 | */ |
| 471 | goto reschedule; |
| 472 | } |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 473 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 474 | if (time_is_after_jiffies(ts->last_valid_interrupt + |
| 475 | msecs_to_jiffies(ts->esd_timeout))) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 476 | goto out; |
| 477 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 478 | /* We should be able to read register without disabling interrupts. */ |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 479 | error = regmap_read(ts->regmap, TSC2005_REG_CFR0, &r); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 480 | if (!error && |
| 481 | !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) { |
| 482 | goto out; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 485 | /* |
| 486 | * If we could not read our known value from configuration register 0 |
| 487 | * then we should reset the controller as if from power-up and start |
| 488 | * scanning again. |
| 489 | */ |
| 490 | dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n"); |
| 491 | |
| 492 | disable_irq(ts->spi->irq); |
| 493 | del_timer_sync(&ts->penup_timer); |
| 494 | |
| 495 | tsc2005_update_pen_state(ts, 0, 0, 0); |
| 496 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 497 | tsc2005_set_reset(ts, false); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 498 | usleep_range(100, 500); /* only 10us required */ |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 499 | tsc2005_set_reset(ts, true); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 500 | |
| 501 | enable_irq(ts->spi->irq); |
| 502 | tsc2005_start_scan(ts); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 503 | |
| 504 | out: |
Aaro Koskinen | a0fa220 | 2011-03-23 23:48:19 -0700 | [diff] [blame] | 505 | mutex_unlock(&ts->mutex); |
| 506 | reschedule: |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 507 | /* re-arm the watchdog */ |
| 508 | schedule_delayed_work(&ts->esd_work, |
Aaro Koskinen | 9034279 | 2011-03-23 23:45:11 -0700 | [diff] [blame] | 509 | round_jiffies_relative( |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 510 | msecs_to_jiffies(ts->esd_timeout))); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int tsc2005_open(struct input_dev *input) |
| 514 | { |
| 515 | struct tsc2005 *ts = input_get_drvdata(input); |
| 516 | |
| 517 | mutex_lock(&ts->mutex); |
| 518 | |
Dmitry Torokhov | 5cb81d1 | 2011-03-16 22:11:41 -0700 | [diff] [blame] | 519 | if (!ts->suspended) |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 520 | __tsc2005_enable(ts); |
| 521 | |
| 522 | ts->opened = true; |
| 523 | |
| 524 | mutex_unlock(&ts->mutex); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static void tsc2005_close(struct input_dev *input) |
| 530 | { |
| 531 | struct tsc2005 *ts = input_get_drvdata(input); |
| 532 | |
| 533 | mutex_lock(&ts->mutex); |
| 534 | |
Dmitry Torokhov | 5cb81d1 | 2011-03-16 22:11:41 -0700 | [diff] [blame] | 535 | if (!ts->suspended) |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 536 | __tsc2005_disable(ts); |
| 537 | |
| 538 | ts->opened = false; |
| 539 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 540 | mutex_unlock(&ts->mutex); |
| 541 | } |
| 542 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 543 | static int tsc2005_probe(struct spi_device *spi) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 544 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 545 | const struct tsc2005_platform_data *pdata = dev_get_platdata(&spi->dev); |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 546 | struct device_node *np = spi->dev.of_node; |
| 547 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 548 | struct tsc2005 *ts; |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 549 | struct input_dev *input_dev; |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 550 | unsigned int max_x = MAX_12BIT; |
| 551 | unsigned int max_y = MAX_12BIT; |
| 552 | unsigned int max_p = MAX_12BIT; |
| 553 | unsigned int fudge_x = TSC2005_DEF_X_FUZZ; |
| 554 | unsigned int fudge_y = TSC2005_DEF_Y_FUZZ; |
| 555 | unsigned int fudge_p = TSC2005_DEF_P_FUZZ; |
| 556 | unsigned int x_plate_ohm = TSC2005_DEF_RESISTOR; |
| 557 | unsigned int esd_timeout; |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 558 | int error; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 559 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 560 | if (!np && !pdata) { |
Sebastian Reichel | 6e51c85 | 2014-04-25 21:50:13 -0700 | [diff] [blame] | 561 | dev_err(&spi->dev, "no platform data\n"); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 562 | return -ENODEV; |
| 563 | } |
| 564 | |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 565 | if (spi->irq <= 0) { |
Sebastian Reichel | 6e51c85 | 2014-04-25 21:50:13 -0700 | [diff] [blame] | 566 | dev_err(&spi->dev, "no irq\n"); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 567 | return -ENODEV; |
| 568 | } |
| 569 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 570 | if (pdata) { |
| 571 | fudge_x = pdata->ts_x_fudge; |
| 572 | fudge_y = pdata->ts_y_fudge; |
| 573 | fudge_p = pdata->ts_pressure_fudge; |
| 574 | max_x = pdata->ts_x_max; |
| 575 | max_y = pdata->ts_y_max; |
| 576 | max_p = pdata->ts_pressure_max; |
| 577 | x_plate_ohm = pdata->ts_x_plate_ohm; |
| 578 | esd_timeout = pdata->esd_timeout_ms; |
| 579 | } else { |
| 580 | x_plate_ohm = TSC2005_DEF_RESISTOR; |
| 581 | of_property_read_u32(np, "ti,x-plate-ohms", &x_plate_ohm); |
| 582 | esd_timeout = 0; |
| 583 | of_property_read_u32(np, "ti,esd-recovery-timeout-ms", |
| 584 | &esd_timeout); |
| 585 | } |
| 586 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 587 | spi->mode = SPI_MODE_0; |
| 588 | spi->bits_per_word = 8; |
| 589 | if (!spi->max_speed_hz) |
| 590 | spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 591 | |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 592 | error = spi_setup(spi); |
| 593 | if (error) |
| 594 | return error; |
| 595 | |
Sebastian Reichel | 99e8325 | 2014-04-25 21:50:21 -0700 | [diff] [blame] | 596 | ts = devm_kzalloc(&spi->dev, sizeof(*ts), GFP_KERNEL); |
| 597 | if (!ts) |
| 598 | return -ENOMEM; |
| 599 | |
| 600 | input_dev = devm_input_allocate_device(&spi->dev); |
| 601 | if (!input_dev) |
| 602 | return -ENOMEM; |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 603 | |
| 604 | ts->spi = spi; |
| 605 | ts->idev = input_dev; |
| 606 | |
Sebastian Reichel | 273cf48 | 2015-07-27 17:27:25 -0700 | [diff] [blame] | 607 | ts->regmap = devm_regmap_init_spi(spi, &tsc2005_regmap_config); |
| 608 | if (IS_ERR(ts->regmap)) |
| 609 | return PTR_ERR(ts->regmap); |
| 610 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 611 | ts->x_plate_ohm = x_plate_ohm; |
| 612 | ts->esd_timeout = esd_timeout; |
| 613 | |
Sebastian Reichel | d257f29 | 2015-07-27 17:28:58 -0700 | [diff] [blame^] | 614 | ts->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", |
| 615 | GPIOD_OUT_HIGH); |
| 616 | if (IS_ERR(ts->reset_gpio)) { |
| 617 | error = PTR_ERR(ts->reset_gpio); |
| 618 | dev_err(&spi->dev, "error acquiring reset gpio: %d\n", error); |
| 619 | return error; |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 620 | } |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 621 | |
Sebastian Reichel | d257f29 | 2015-07-27 17:28:58 -0700 | [diff] [blame^] | 622 | ts->vio = devm_regulator_get_optional(&spi->dev, "vio"); |
| 623 | if (IS_ERR(ts->vio)) { |
| 624 | error = PTR_ERR(ts->vio); |
| 625 | dev_err(&spi->dev, "vio regulator missing (%d)", error); |
| 626 | return error; |
| 627 | } |
| 628 | |
| 629 | if (!ts->reset_gpio && pdata) |
| 630 | ts->set_reset = pdata->set_reset; |
| 631 | |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 632 | mutex_init(&ts->mutex); |
| 633 | |
Dmitry Torokhov | 80cc2f0 | 2011-03-16 22:11:08 -0700 | [diff] [blame] | 634 | spin_lock_init(&ts->lock); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 635 | setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 636 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 637 | INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 638 | |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 639 | snprintf(ts->phys, sizeof(ts->phys), |
| 640 | "%s/input-ts", dev_name(&spi->dev)); |
| 641 | |
| 642 | input_dev->name = "TSC2005 touchscreen"; |
| 643 | input_dev->phys = ts->phys; |
| 644 | input_dev->id.bustype = BUS_SPI; |
| 645 | input_dev->dev.parent = &spi->dev; |
| 646 | input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY); |
| 647 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 648 | |
| 649 | input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0); |
| 650 | input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0); |
| 651 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0); |
| 652 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 653 | if (np) |
Dmitry Torokhov | 4200e83 | 2015-07-06 15:18:24 -0700 | [diff] [blame] | 654 | touchscreen_parse_properties(input_dev, false); |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 655 | |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 656 | input_dev->open = tsc2005_open; |
| 657 | input_dev->close = tsc2005_close; |
| 658 | |
| 659 | input_set_drvdata(input_dev, ts); |
| 660 | |
| 661 | /* Ensure the touchscreen is off */ |
| 662 | tsc2005_stop_scan(ts); |
| 663 | |
Sebastian Reichel | 99e8325 | 2014-04-25 21:50:21 -0700 | [diff] [blame] | 664 | error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, |
| 665 | tsc2005_irq_thread, |
| 666 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 667 | "tsc2005", ts); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 668 | if (error) { |
| 669 | dev_err(&spi->dev, "Failed to request irq, err: %d\n", error); |
Sebastian Reichel | 99e8325 | 2014-04-25 21:50:21 -0700 | [diff] [blame] | 670 | return error; |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 673 | /* enable regulator for DT */ |
| 674 | if (ts->vio) { |
| 675 | error = regulator_enable(ts->vio); |
| 676 | if (error) |
| 677 | return error; |
| 678 | } |
| 679 | |
Sebastian Reichel | 80b46aa | 2015-07-27 17:28:37 -0700 | [diff] [blame] | 680 | dev_set_drvdata(&spi->dev, ts); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 681 | error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group); |
| 682 | if (error) { |
| 683 | dev_err(&spi->dev, |
| 684 | "Failed to create sysfs attributes, err: %d\n", error); |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 685 | goto disable_regulator; |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | error = input_register_device(ts->idev); |
| 689 | if (error) { |
| 690 | dev_err(&spi->dev, |
| 691 | "Failed to register input device, err: %d\n", error); |
| 692 | goto err_remove_sysfs; |
| 693 | } |
| 694 | |
Geert Uytterhoeven | ddca6a3 | 2011-03-21 02:37:07 -0700 | [diff] [blame] | 695 | irq_set_irq_wake(spi->irq, 1); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 696 | return 0; |
| 697 | |
| 698 | err_remove_sysfs: |
| 699 | sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group); |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 700 | disable_regulator: |
| 701 | if (ts->vio) |
| 702 | regulator_disable(ts->vio); |
Dmitry Torokhov | 99bb892 | 2011-03-16 22:09:38 -0700 | [diff] [blame] | 703 | return error; |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 706 | static int tsc2005_remove(struct spi_device *spi) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 707 | { |
Sebastian Reichel | 80b46aa | 2015-07-27 17:28:37 -0700 | [diff] [blame] | 708 | struct tsc2005 *ts = dev_get_drvdata(&spi->dev); |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 709 | |
Sebastian Reichel | 99e8325 | 2014-04-25 21:50:21 -0700 | [diff] [blame] | 710 | sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 711 | |
Sebastian Reichel | a38cfeb | 2014-05-28 23:57:29 -0700 | [diff] [blame] | 712 | if (ts->vio) |
| 713 | regulator_disable(ts->vio); |
| 714 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 718 | static int __maybe_unused tsc2005_suspend(struct device *dev) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 719 | { |
Sebastian Reichel | 80b46aa | 2015-07-27 17:28:37 -0700 | [diff] [blame] | 720 | struct tsc2005 *ts = dev_get_drvdata(dev); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 721 | |
| 722 | mutex_lock(&ts->mutex); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 723 | |
Dmitry Torokhov | 5cb81d1 | 2011-03-16 22:11:41 -0700 | [diff] [blame] | 724 | if (!ts->suspended && ts->opened) |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 725 | __tsc2005_disable(ts); |
| 726 | |
| 727 | ts->suspended = true; |
| 728 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 729 | mutex_unlock(&ts->mutex); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
Jingoo Han | 02b6a58 | 2014-11-02 00:04:14 -0700 | [diff] [blame] | 734 | static int __maybe_unused tsc2005_resume(struct device *dev) |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 735 | { |
Sebastian Reichel | 80b46aa | 2015-07-27 17:28:37 -0700 | [diff] [blame] | 736 | struct tsc2005 *ts = dev_get_drvdata(dev); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 737 | |
| 738 | mutex_lock(&ts->mutex); |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 739 | |
Dmitry Torokhov | 5cb81d1 | 2011-03-16 22:11:41 -0700 | [diff] [blame] | 740 | if (ts->suspended && ts->opened) |
Dmitry Torokhov | 0b950d3 | 2011-03-16 22:11:34 -0700 | [diff] [blame] | 741 | __tsc2005_enable(ts); |
| 742 | |
| 743 | ts->suspended = false; |
| 744 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 745 | mutex_unlock(&ts->mutex); |
| 746 | |
| 747 | return 0; |
| 748 | } |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 749 | |
Dmitry Torokhov | 3ff8ff5 | 2011-03-16 22:08:26 -0700 | [diff] [blame] | 750 | static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume); |
| 751 | |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 752 | static struct spi_driver tsc2005_driver = { |
Dmitry Torokhov | 3ff8ff5 | 2011-03-16 22:08:26 -0700 | [diff] [blame] | 753 | .driver = { |
| 754 | .name = "tsc2005", |
| 755 | .owner = THIS_MODULE, |
| 756 | .pm = &tsc2005_pm_ops, |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 757 | }, |
Dmitry Torokhov | 3ff8ff5 | 2011-03-16 22:08:26 -0700 | [diff] [blame] | 758 | .probe = tsc2005_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 759 | .remove = tsc2005_remove, |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 760 | }; |
| 761 | |
Axel Lin | ca83922 | 2012-03-16 23:05:26 -0700 | [diff] [blame] | 762 | module_spi_driver(tsc2005_driver); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 763 | |
| 764 | MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>"); |
Dmitry Torokhov | b88aa49 | 2011-03-16 22:09:03 -0700 | [diff] [blame] | 765 | MODULE_DESCRIPTION("TSC2005 Touchscreen Driver"); |
Lauri Leukkunen | 37bd446 | 2011-03-16 22:07:36 -0700 | [diff] [blame] | 766 | MODULE_LICENSE("GPL"); |
Pali Rohár | 938789f | 2013-02-16 22:01:44 -0800 | [diff] [blame] | 767 | MODULE_ALIAS("spi:tsc2005"); |