blob: d8c025b0f88cd4fe2c16712d1852111d782e08da [file] [log] [blame]
Lauri Leukkunen37bd4462011-03-16 22:07:36 -07001/*
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 Reichela38cfeb2014-05-28 23:57:29 -070028#include <linux/input/touchscreen.h>
Lauri Leukkunen37bd4462011-03-16 22:07:36 -070029#include <linux/interrupt.h>
30#include <linux/delay.h>
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -070031#include <linux/pm.h>
Sebastian Reichela38cfeb2014-05-28 23:57:29 -070032#include <linux/of.h>
33#include <linux/of_gpio.h>
Lauri Leukkunen37bd4462011-03-16 22:07:36 -070034#include <linux/spi/spi.h>
35#include <linux/spi/tsc2005.h>
Sebastian Reichela38cfeb2014-05-28 23:57:29 -070036#include <linux/regulator/consumer.h>
Lauri Leukkunen37bd4462011-03-16 22:07:36 -070037
38/*
39 * The touchscreen interface operates as follows:
40 *
41 * 1) Pen is pressed against the touchscreen.
42 * 2) TSC2005 performs AD conversion.
43 * 3) After the conversion is done TSC2005 drives DAV line down.
44 * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled.
45 * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2
46 * values.
47 * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up
48 * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms).
49 * 7) When the penup timer expires, there have not been touch or DAV interrupts
50 * during the last 40ms which means the pen has been lifted.
51 *
52 * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond
53 * after a configurable period (in ms) of activity. If esd_timeout is 0, the
54 * watchdog is disabled.
55 */
56
57/* control byte 1 */
58#define TSC2005_CMD 0x80
59#define TSC2005_CMD_NORMAL 0x00
60#define TSC2005_CMD_STOP 0x01
61#define TSC2005_CMD_12BIT 0x04
62
63/* control byte 0 */
64#define TSC2005_REG_READ 0x0001
65#define TSC2005_REG_PND0 0x0002
66#define TSC2005_REG_X 0x0000
67#define TSC2005_REG_Y 0x0008
68#define TSC2005_REG_Z1 0x0010
69#define TSC2005_REG_Z2 0x0018
70#define TSC2005_REG_TEMP_HIGH 0x0050
71#define TSC2005_REG_CFR0 0x0060
72#define TSC2005_REG_CFR1 0x0068
73#define TSC2005_REG_CFR2 0x0070
74
75/* configuration register 0 */
76#define TSC2005_CFR0_PRECHARGE_276US 0x0040
77#define TSC2005_CFR0_STABTIME_1MS 0x0300
78#define TSC2005_CFR0_CLOCK_1MHZ 0x1000
79#define TSC2005_CFR0_RESOLUTION12 0x2000
80#define TSC2005_CFR0_PENMODE 0x8000
81#define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \
82 TSC2005_CFR0_CLOCK_1MHZ | \
83 TSC2005_CFR0_RESOLUTION12 | \
84 TSC2005_CFR0_PRECHARGE_276US | \
85 TSC2005_CFR0_PENMODE)
86
87/* bits common to both read and write of configuration register 0 */
88#define TSC2005_CFR0_RW_MASK 0x3fff
89
90/* configuration register 1 */
91#define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
92#define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
93
94/* configuration register 2 */
95#define TSC2005_CFR2_MAVE_Z 0x0004
96#define TSC2005_CFR2_MAVE_Y 0x0008
97#define TSC2005_CFR2_MAVE_X 0x0010
98#define TSC2005_CFR2_AVG_7 0x0800
99#define TSC2005_CFR2_MEDIUM_15 0x3000
100#define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \
101 TSC2005_CFR2_MAVE_Y | \
102 TSC2005_CFR2_MAVE_Z | \
103 TSC2005_CFR2_MEDIUM_15 | \
104 TSC2005_CFR2_AVG_7)
105
106#define MAX_12BIT 0xfff
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700107#define TSC2005_DEF_X_FUZZ 4
108#define TSC2005_DEF_Y_FUZZ 8
109#define TSC2005_DEF_P_FUZZ 2
110#define TSC2005_DEF_RESISTOR 280
111
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700112#define TSC2005_SPI_MAX_SPEED_HZ 10000000
113#define TSC2005_PENUP_TIME_MS 40
114
115struct tsc2005_spi_rd {
116 struct spi_transfer spi_xfer;
117 u32 spi_tx;
118 u32 spi_rx;
119};
120
121struct tsc2005 {
122 struct spi_device *spi;
123
124 struct spi_message spi_read_msg;
125 struct tsc2005_spi_rd spi_x;
126 struct tsc2005_spi_rd spi_y;
127 struct tsc2005_spi_rd spi_z1;
128 struct tsc2005_spi_rd spi_z2;
129
130 struct input_dev *idev;
131 char phys[32];
132
133 struct mutex mutex;
134
135 /* raw copy of previous x,y,z */
136 int in_x;
137 int in_y;
138 int in_z1;
139 int in_z2;
140
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700141 spinlock_t lock;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700142 struct timer_list penup_timer;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700143
144 unsigned int esd_timeout;
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700145 struct delayed_work esd_work;
146 unsigned long last_valid_interrupt;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700147
148 unsigned int x_plate_ohm;
149
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700150 bool opened;
151 bool suspended;
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700152
153 bool pen_down;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700154
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700155 struct regulator *vio;
156
157 int reset_gpio;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700158 void (*set_reset)(bool enable);
159};
160
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700161static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700162{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700163 u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
164 struct spi_transfer xfer = {
165 .tx_buf = &tx,
166 .len = 1,
167 .bits_per_word = 8,
168 };
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700169 struct spi_message msg;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700170 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700171
172 spi_message_init(&msg);
173 spi_message_add_tail(&xfer, &msg);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700174
175 error = spi_sync(ts->spi, &msg);
176 if (error) {
177 dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n",
178 __func__, cmd, error);
179 return error;
180 }
181
182 return 0;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700183}
184
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700185static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700186{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700187 u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value;
188 struct spi_transfer xfer = {
189 .tx_buf = &tx,
190 .len = 4,
191 .bits_per_word = 24,
192 };
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700193 struct spi_message msg;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700194 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700195
196 spi_message_init(&msg);
197 spi_message_add_tail(&xfer, &msg);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700198
199 error = spi_sync(ts->spi, &msg);
200 if (error) {
201 dev_err(&ts->spi->dev,
202 "%s: failed, register: %x, value: %x, error: %d\n",
203 __func__, reg, value, error);
204 return error;
205 }
206
207 return 0;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700208}
209
210static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
211{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700212 memset(rd, 0, sizeof(*rd));
213
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700214 rd->spi_tx = (reg | TSC2005_REG_READ) << 16;
215 rd->spi_xfer.tx_buf = &rd->spi_tx;
216 rd->spi_xfer.rx_buf = &rd->spi_rx;
217 rd->spi_xfer.len = 4;
218 rd->spi_xfer.bits_per_word = 24;
219 rd->spi_xfer.cs_change = !last;
220}
221
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700222static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700223{
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700224 struct tsc2005_spi_rd spi_rd;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700225 struct spi_message msg;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700226 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700227
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700228 tsc2005_setup_read(&spi_rd, reg, true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700229
230 spi_message_init(&msg);
231 spi_message_add_tail(&spi_rd.spi_xfer, &msg);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700232
233 error = spi_sync(ts->spi, &msg);
234 if (error)
235 return error;
Dmitry Torokhov9a6e1802011-03-16 22:10:52 -0700236
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700237 *value = spi_rd.spi_rx;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700238 return 0;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700239}
240
241static void tsc2005_update_pen_state(struct tsc2005 *ts,
242 int x, int y, int pressure)
243{
244 if (pressure) {
245 input_report_abs(ts->idev, ABS_X, x);
246 input_report_abs(ts->idev, ABS_Y, y);
247 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
248 if (!ts->pen_down) {
249 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700250 ts->pen_down = true;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700251 }
252 } else {
253 input_report_abs(ts->idev, ABS_PRESSURE, 0);
254 if (ts->pen_down) {
255 input_report_key(ts->idev, BTN_TOUCH, 0);
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700256 ts->pen_down = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700257 }
258 }
259 input_sync(ts->idev);
260 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
261 pressure);
262}
263
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700264static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
265{
266 struct tsc2005 *ts = _ts;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700267 unsigned long flags;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700268 unsigned int pressure;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700269 u32 x, y;
270 u32 z1, z2;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700271 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700272
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700273 /* read the coordinates */
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700274 error = spi_sync(ts->spi, &ts->spi_read_msg);
275 if (unlikely(error))
276 goto out;
277
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700278 x = ts->spi_x.spi_rx;
279 y = ts->spi_y.spi_rx;
280 z1 = ts->spi_z1.spi_rx;
281 z2 = ts->spi_z2.spi_rx;
282
283 /* validate position */
284 if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
285 goto out;
286
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700287 /* Skip reading if the pressure components are out of range */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700288 if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
289 goto out;
290
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700291 /*
292 * Skip point if this is a pen down with the exact same values as
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700293 * the value before pen-up - that implies SPI fed us stale data
294 */
295 if (!ts->pen_down &&
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700296 ts->in_x == x && ts->in_y == y &&
297 ts->in_z1 == z1 && ts->in_z2 == z2) {
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700298 goto out;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700299 }
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700300
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700301 /*
302 * At this point we are happy we have a valid and useful reading.
303 * Remember it for later comparisons. We may now begin downsampling.
304 */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700305 ts->in_x = x;
306 ts->in_y = y;
307 ts->in_z1 = z1;
308 ts->in_z2 = z2;
309
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700310 /* Compute touch pressure resistance using equation #1 */
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700311 pressure = x * (z2 - z1) / z1;
312 pressure = pressure * ts->x_plate_ohm / 4096;
313 if (unlikely(pressure > MAX_12BIT))
314 goto out;
315
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700316 spin_lock_irqsave(&ts->lock, flags);
317
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700318 tsc2005_update_pen_state(ts, x, y, pressure);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700319 mod_timer(&ts->penup_timer,
320 jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
321
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700322 spin_unlock_irqrestore(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700323
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700324 ts->last_valid_interrupt = jiffies;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700325out:
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700326 return IRQ_HANDLED;
327}
328
329static void tsc2005_penup_timer(unsigned long data)
330{
331 struct tsc2005 *ts = (struct tsc2005 *)data;
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700332 unsigned long flags;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700333
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700334 spin_lock_irqsave(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700335 tsc2005_update_pen_state(ts, 0, 0, 0);
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700336 spin_unlock_irqrestore(&ts->lock, flags);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700337}
338
339static void tsc2005_start_scan(struct tsc2005 *ts)
340{
341 tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
342 tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
343 tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
344 tsc2005_cmd(ts, TSC2005_CMD_NORMAL);
345}
346
347static void tsc2005_stop_scan(struct tsc2005 *ts)
348{
349 tsc2005_cmd(ts, TSC2005_CMD_STOP);
350}
351
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700352static void tsc2005_set_reset(struct tsc2005 *ts, bool enable)
353{
354 if (ts->reset_gpio >= 0)
355 gpio_set_value(ts->reset_gpio, enable);
356 else if (ts->set_reset)
357 ts->set_reset(enable);
358}
359
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700360/* must be called with ts->mutex held */
361static void __tsc2005_disable(struct tsc2005 *ts)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700362{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700363 tsc2005_stop_scan(ts);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700364
365 disable_irq(ts->spi->irq);
366 del_timer_sync(&ts->penup_timer);
367
368 cancel_delayed_work_sync(&ts->esd_work);
369
370 enable_irq(ts->spi->irq);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700371}
372
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700373/* must be called with ts->mutex held */
374static void __tsc2005_enable(struct tsc2005 *ts)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700375{
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700376 tsc2005_start_scan(ts);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700377
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700378 if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) {
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700379 ts->last_valid_interrupt = jiffies;
380 schedule_delayed_work(&ts->esd_work,
Aaro Koskinen90342792011-03-23 23:45:11 -0700381 round_jiffies_relative(
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700382 msecs_to_jiffies(ts->esd_timeout)));
383 }
384
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700385}
386
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700387static ssize_t tsc2005_selftest_show(struct device *dev,
388 struct device_attribute *attr,
389 char *buf)
390{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700391 struct spi_device *spi = to_spi_device(dev);
392 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700393 u16 temp_high;
394 u16 temp_high_orig;
395 u16 temp_high_test;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700396 bool success = true;
397 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700398
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700399 mutex_lock(&ts->mutex);
400
401 /*
402 * Test TSC2005 communications via temp high register.
403 */
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700404 __tsc2005_disable(ts);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700405
406 error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
407 if (error) {
408 dev_warn(dev, "selftest failed: read error %d\n", error);
409 success = false;
410 goto out;
411 }
412
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700413 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700414
415 error = tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
416 if (error) {
417 dev_warn(dev, "selftest failed: write error %d\n", error);
418 success = false;
419 goto out;
420 }
421
422 error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
423 if (error) {
424 dev_warn(dev, "selftest failed: read error %d after write\n",
425 error);
426 success = false;
427 goto out;
428 }
429
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700430 if (temp_high != temp_high_test) {
431 dev_warn(dev, "selftest failed: %d != %d\n",
432 temp_high, temp_high_test);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700433 success = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700434 }
435
436 /* hardware reset */
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700437 tsc2005_set_reset(ts, false);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700438 usleep_range(100, 500); /* only 10us required */
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700439 tsc2005_set_reset(ts, true);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700440
441 if (!success)
442 goto out;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700443
444 /* test that the reset really happened */
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700445 error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
446 if (error) {
447 dev_warn(dev, "selftest failed: read error %d after reset\n",
448 error);
449 success = false;
450 goto out;
451 }
452
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700453 if (temp_high != temp_high_orig) {
454 dev_warn(dev, "selftest failed after reset: %d != %d\n",
455 temp_high, temp_high_orig);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700456 success = false;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700457 }
458
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700459out:
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700460 __tsc2005_enable(ts);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700461 mutex_unlock(&ts->mutex);
462
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700463 return sprintf(buf, "%d\n", success);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700464}
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700465
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700466static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
467
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700468static struct attribute *tsc2005_attrs[] = {
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700469 &dev_attr_selftest.attr,
470 NULL
471};
472
Al Viro587a1f12011-07-23 23:11:19 -0400473static umode_t tsc2005_attr_is_visible(struct kobject *kobj,
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700474 struct attribute *attr, int n)
475{
476 struct device *dev = container_of(kobj, struct device, kobj);
477 struct spi_device *spi = to_spi_device(dev);
478 struct tsc2005 *ts = spi_get_drvdata(spi);
Al Viro587a1f12011-07-23 23:11:19 -0400479 umode_t mode = attr->mode;
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700480
481 if (attr == &dev_attr_selftest.attr) {
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700482 if (!ts->set_reset && !ts->reset_gpio)
Dmitry Torokhov8dbcc432011-03-16 22:10:37 -0700483 mode = 0;
484 }
485
486 return mode;
487}
488
489static const struct attribute_group tsc2005_attr_group = {
490 .is_visible = tsc2005_attr_is_visible,
491 .attrs = tsc2005_attrs,
492};
493
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700494static void tsc2005_esd_work(struct work_struct *work)
495{
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700496 struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700497 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700498 u16 r;
499
Aaro Koskinena0fa2202011-03-23 23:48:19 -0700500 if (!mutex_trylock(&ts->mutex)) {
501 /*
502 * If the mutex is taken, it means that disable or enable is in
503 * progress. In that case just reschedule the work. If the work
504 * is not needed, it will be canceled by disable.
505 */
506 goto reschedule;
507 }
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700508
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700509 if (time_is_after_jiffies(ts->last_valid_interrupt +
510 msecs_to_jiffies(ts->esd_timeout)))
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700511 goto out;
512
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700513 /* We should be able to read register without disabling interrupts. */
Dmitry Torokhov71f80042011-03-16 22:11:25 -0700514 error = tsc2005_read(ts, TSC2005_REG_CFR0, &r);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700515 if (!error &&
516 !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
517 goto out;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700518 }
519
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700520 /*
521 * If we could not read our known value from configuration register 0
522 * then we should reset the controller as if from power-up and start
523 * scanning again.
524 */
525 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
526
527 disable_irq(ts->spi->irq);
528 del_timer_sync(&ts->penup_timer);
529
530 tsc2005_update_pen_state(ts, 0, 0, 0);
531
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700532 tsc2005_set_reset(ts, false);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700533 usleep_range(100, 500); /* only 10us required */
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700534 tsc2005_set_reset(ts, true);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700535
536 enable_irq(ts->spi->irq);
537 tsc2005_start_scan(ts);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700538
539out:
Aaro Koskinena0fa2202011-03-23 23:48:19 -0700540 mutex_unlock(&ts->mutex);
541reschedule:
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700542 /* re-arm the watchdog */
543 schedule_delayed_work(&ts->esd_work,
Aaro Koskinen90342792011-03-23 23:45:11 -0700544 round_jiffies_relative(
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700545 msecs_to_jiffies(ts->esd_timeout)));
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700546}
547
548static int tsc2005_open(struct input_dev *input)
549{
550 struct tsc2005 *ts = input_get_drvdata(input);
551
552 mutex_lock(&ts->mutex);
553
Dmitry Torokhov5cb81d12011-03-16 22:11:41 -0700554 if (!ts->suspended)
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700555 __tsc2005_enable(ts);
556
557 ts->opened = true;
558
559 mutex_unlock(&ts->mutex);
560
561 return 0;
562}
563
564static void tsc2005_close(struct input_dev *input)
565{
566 struct tsc2005 *ts = input_get_drvdata(input);
567
568 mutex_lock(&ts->mutex);
569
Dmitry Torokhov5cb81d12011-03-16 22:11:41 -0700570 if (!ts->suspended)
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700571 __tsc2005_disable(ts);
572
573 ts->opened = false;
574
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700575 mutex_unlock(&ts->mutex);
576}
577
Bill Pemberton5298cc42012-11-23 21:38:25 -0800578static void tsc2005_setup_spi_xfer(struct tsc2005 *ts)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700579{
Dmitry Torokhovc8b68462011-03-16 22:10:46 -0700580 tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false);
581 tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false);
582 tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false);
583 tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700584
585 spi_message_init(&ts->spi_read_msg);
586 spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
587 spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
588 spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
589 spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
590}
591
Bill Pemberton5298cc42012-11-23 21:38:25 -0800592static int tsc2005_probe(struct spi_device *spi)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700593{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800594 const struct tsc2005_platform_data *pdata = dev_get_platdata(&spi->dev);
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700595 struct device_node *np = spi->dev.of_node;
596
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700597 struct tsc2005 *ts;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700598 struct input_dev *input_dev;
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700599 unsigned int max_x = MAX_12BIT;
600 unsigned int max_y = MAX_12BIT;
601 unsigned int max_p = MAX_12BIT;
602 unsigned int fudge_x = TSC2005_DEF_X_FUZZ;
603 unsigned int fudge_y = TSC2005_DEF_Y_FUZZ;
604 unsigned int fudge_p = TSC2005_DEF_P_FUZZ;
605 unsigned int x_plate_ohm = TSC2005_DEF_RESISTOR;
606 unsigned int esd_timeout;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700607 int error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700608
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700609 if (!np && !pdata) {
Sebastian Reichel6e51c852014-04-25 21:50:13 -0700610 dev_err(&spi->dev, "no platform data\n");
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700611 return -ENODEV;
612 }
613
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700614 if (spi->irq <= 0) {
Sebastian Reichel6e51c852014-04-25 21:50:13 -0700615 dev_err(&spi->dev, "no irq\n");
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700616 return -ENODEV;
617 }
618
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700619 if (pdata) {
620 fudge_x = pdata->ts_x_fudge;
621 fudge_y = pdata->ts_y_fudge;
622 fudge_p = pdata->ts_pressure_fudge;
623 max_x = pdata->ts_x_max;
624 max_y = pdata->ts_y_max;
625 max_p = pdata->ts_pressure_max;
626 x_plate_ohm = pdata->ts_x_plate_ohm;
627 esd_timeout = pdata->esd_timeout_ms;
628 } else {
629 x_plate_ohm = TSC2005_DEF_RESISTOR;
630 of_property_read_u32(np, "ti,x-plate-ohms", &x_plate_ohm);
631 esd_timeout = 0;
632 of_property_read_u32(np, "ti,esd-recovery-timeout-ms",
633 &esd_timeout);
634 }
635
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700636 spi->mode = SPI_MODE_0;
637 spi->bits_per_word = 8;
638 if (!spi->max_speed_hz)
639 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700640
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700641 error = spi_setup(spi);
642 if (error)
643 return error;
644
Sebastian Reichel99e83252014-04-25 21:50:21 -0700645 ts = devm_kzalloc(&spi->dev, sizeof(*ts), GFP_KERNEL);
646 if (!ts)
647 return -ENOMEM;
648
649 input_dev = devm_input_allocate_device(&spi->dev);
650 if (!input_dev)
651 return -ENOMEM;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700652
653 ts->spi = spi;
654 ts->idev = input_dev;
655
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700656 ts->x_plate_ohm = x_plate_ohm;
657 ts->esd_timeout = esd_timeout;
658
659 if (np) {
660 ts->reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
661 if (ts->reset_gpio == -EPROBE_DEFER)
662 return ts->reset_gpio;
663 if (ts->reset_gpio < 0) {
664 dev_err(&spi->dev, "error acquiring reset gpio: %d\n",
665 ts->reset_gpio);
666 return ts->reset_gpio;
667 }
668
669 error = devm_gpio_request_one(&spi->dev, ts->reset_gpio, 0,
670 "reset-gpios");
671 if (error) {
672 dev_err(&spi->dev, "error requesting reset gpio: %d\n",
673 error);
674 return error;
675 }
676
677 ts->vio = devm_regulator_get(&spi->dev, "vio");
678 if (IS_ERR(ts->vio)) {
679 error = PTR_ERR(ts->vio);
680 dev_err(&spi->dev, "vio regulator missing (%d)", error);
681 return error;
682 }
683 } else {
684 ts->reset_gpio = -1;
685 ts->set_reset = pdata->set_reset;
686 }
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700687
688 mutex_init(&ts->mutex);
689
Dmitry Torokhov80cc2f02011-03-16 22:11:08 -0700690 spin_lock_init(&ts->lock);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700691 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700692
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700693 INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700694
695 tsc2005_setup_spi_xfer(ts);
696
697 snprintf(ts->phys, sizeof(ts->phys),
698 "%s/input-ts", dev_name(&spi->dev));
699
700 input_dev->name = "TSC2005 touchscreen";
701 input_dev->phys = ts->phys;
702 input_dev->id.bustype = BUS_SPI;
703 input_dev->dev.parent = &spi->dev;
704 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
705 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
706
707 input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
708 input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
709 input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
710
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700711 if (np)
Dmitry Torokhov7c494372015-06-01 10:35:16 -0700712 touchscreen_parse_of_params(input_dev, false);
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700713
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700714 input_dev->open = tsc2005_open;
715 input_dev->close = tsc2005_close;
716
717 input_set_drvdata(input_dev, ts);
718
719 /* Ensure the touchscreen is off */
720 tsc2005_stop_scan(ts);
721
Sebastian Reichel99e83252014-04-25 21:50:21 -0700722 error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
723 tsc2005_irq_thread,
724 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
725 "tsc2005", ts);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700726 if (error) {
727 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
Sebastian Reichel99e83252014-04-25 21:50:21 -0700728 return error;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700729 }
730
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700731 /* enable regulator for DT */
732 if (ts->vio) {
733 error = regulator_enable(ts->vio);
734 if (error)
735 return error;
736 }
737
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700738 spi_set_drvdata(spi, ts);
739 error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
740 if (error) {
741 dev_err(&spi->dev,
742 "Failed to create sysfs attributes, err: %d\n", error);
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700743 goto disable_regulator;
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700744 }
745
746 error = input_register_device(ts->idev);
747 if (error) {
748 dev_err(&spi->dev,
749 "Failed to register input device, err: %d\n", error);
750 goto err_remove_sysfs;
751 }
752
Geert Uytterhoevenddca6a32011-03-21 02:37:07 -0700753 irq_set_irq_wake(spi->irq, 1);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700754 return 0;
755
756err_remove_sysfs:
757 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700758disable_regulator:
759 if (ts->vio)
760 regulator_disable(ts->vio);
Dmitry Torokhov99bb8922011-03-16 22:09:38 -0700761 return error;
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700762}
763
Bill Pembertone2619cf2012-11-23 21:50:47 -0800764static int tsc2005_remove(struct spi_device *spi)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700765{
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700766 struct tsc2005 *ts = spi_get_drvdata(spi);
767
Sebastian Reichel99e83252014-04-25 21:50:21 -0700768 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700769
Sebastian Reichela38cfeb2014-05-28 23:57:29 -0700770 if (ts->vio)
771 regulator_disable(ts->vio);
772
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700773 return 0;
774}
775
Jingoo Han02b6a582014-11-02 00:04:14 -0700776static int __maybe_unused tsc2005_suspend(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700777{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700778 struct spi_device *spi = to_spi_device(dev);
779 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700780
781 mutex_lock(&ts->mutex);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700782
Dmitry Torokhov5cb81d12011-03-16 22:11:41 -0700783 if (!ts->suspended && ts->opened)
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700784 __tsc2005_disable(ts);
785
786 ts->suspended = true;
787
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700788 mutex_unlock(&ts->mutex);
789
790 return 0;
791}
792
Jingoo Han02b6a582014-11-02 00:04:14 -0700793static int __maybe_unused tsc2005_resume(struct device *dev)
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700794{
Dmitry Torokhov6b007d62011-03-16 22:08:08 -0700795 struct spi_device *spi = to_spi_device(dev);
796 struct tsc2005 *ts = spi_get_drvdata(spi);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700797
798 mutex_lock(&ts->mutex);
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700799
Dmitry Torokhov5cb81d12011-03-16 22:11:41 -0700800 if (ts->suspended && ts->opened)
Dmitry Torokhov0b950d32011-03-16 22:11:34 -0700801 __tsc2005_enable(ts);
802
803 ts->suspended = false;
804
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700805 mutex_unlock(&ts->mutex);
806
807 return 0;
808}
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700809
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700810static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
811
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700812static struct spi_driver tsc2005_driver = {
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700813 .driver = {
814 .name = "tsc2005",
815 .owner = THIS_MODULE,
816 .pm = &tsc2005_pm_ops,
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700817 },
Dmitry Torokhov3ff8ff52011-03-16 22:08:26 -0700818 .probe = tsc2005_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800819 .remove = tsc2005_remove,
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700820};
821
Axel Linca839222012-03-16 23:05:26 -0700822module_spi_driver(tsc2005_driver);
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700823
824MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
Dmitry Torokhovb88aa492011-03-16 22:09:03 -0700825MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
Lauri Leukkunen37bd4462011-03-16 22:07:36 -0700826MODULE_LICENSE("GPL");
Pali Rohár938789f2013-02-16 22:01:44 -0800827MODULE_ALIAS("spi:tsc2005");