blob: b7059ed8872e40b77eed45e33808ba4b135485c4 [file] [log] [blame]
Michael Welling6ac24382015-11-02 17:45:51 -08001/*
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>
30#include <linux/spi/tsc2005.h>
31#include <linux/regulator/consumer.h>
32#include <linux/regmap.h>
33#include <linux/gpio/consumer.h>
34#include "tsc200x-core.h"
35
36/*
37 * The touchscreen interface operates as follows:
38 *
39 * 1) Pen is pressed against the touchscreen.
Michael Wellingef3b98c2015-11-02 17:51:49 -080040 * 2) TSC200X performs AD conversion.
41 * 3) After the conversion is done TSC200X drives DAV line down.
42 * 4) GPIO IRQ is received and tsc200x_irq_thread() is scheduled.
43 * 5) tsc200x_irq_thread() queues up a transfer to fetch the x, y, z1, z2
Michael Welling6ac24382015-11-02 17:45:51 -080044 * values.
Michael Wellingef3b98c2015-11-02 17:51:49 -080045 * 6) tsc200x_irq_thread() reports coordinates to input layer and sets up
46 * tsc200x_penup_timer() to be called after TSC200X_PENUP_TIME_MS (40ms).
Michael Welling6ac24382015-11-02 17:45:51 -080047 * 7) When the penup timer expires, there have not been touch or DAV interrupts
48 * during the last 40ms which means the pen has been lifted.
49 *
Michael Wellingef3b98c2015-11-02 17:51:49 -080050 * ESD recovery via a hardware reset is done if the TSC200X doesn't respond
Michael Welling6ac24382015-11-02 17:45:51 -080051 * after a configurable period (in ms) of activity. If esd_timeout is 0, the
52 * watchdog is disabled.
53 */
54
Michael Wellingef3b98c2015-11-02 17:51:49 -080055static const struct regmap_range tsc200x_writable_ranges[] = {
56 regmap_reg_range(TSC200X_REG_AUX_HIGH, TSC200X_REG_CFR2),
Michael Welling6ac24382015-11-02 17:45:51 -080057};
58
Michael Wellingef3b98c2015-11-02 17:51:49 -080059static const struct regmap_access_table tsc200x_writable_table = {
60 .yes_ranges = tsc200x_writable_ranges,
61 .n_yes_ranges = ARRAY_SIZE(tsc200x_writable_ranges),
Michael Welling6ac24382015-11-02 17:45:51 -080062};
63
64const struct regmap_config tsc200x_regmap_config = {
65 .reg_bits = 8,
66 .val_bits = 16,
67 .reg_stride = 0x08,
68 .max_register = 0x78,
Michael Wellingef3b98c2015-11-02 17:51:49 -080069 .read_flag_mask = TSC200X_REG_READ,
70 .write_flag_mask = TSC200X_REG_PND0,
71 .wr_table = &tsc200x_writable_table,
Michael Welling6ac24382015-11-02 17:45:51 -080072 .use_single_rw = true,
73};
74EXPORT_SYMBOL_GPL(tsc200x_regmap_config);
75
Michael Wellingef3b98c2015-11-02 17:51:49 -080076struct tsc200x_data {
Michael Welling6ac24382015-11-02 17:45:51 -080077 u16 x;
78 u16 y;
79 u16 z1;
80 u16 z2;
81} __packed;
Michael Wellingef3b98c2015-11-02 17:51:49 -080082#define TSC200X_DATA_REGS 4
Michael Welling6ac24382015-11-02 17:45:51 -080083
Michael Wellingef3b98c2015-11-02 17:51:49 -080084struct tsc200x {
Michael Welling6ac24382015-11-02 17:45:51 -080085 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;
117 void (*set_reset)(bool enable);
118 int (*tsc200x_cmd)(struct device *dev, u8 cmd);
119 int irq;
120};
121
Michael Wellingef3b98c2015-11-02 17:51:49 -0800122static void tsc200x_update_pen_state(struct tsc200x *ts,
Michael Welling6ac24382015-11-02 17:45:51 -0800123 int x, int y, int pressure)
124{
125 if (pressure) {
126 input_report_abs(ts->idev, ABS_X, x);
127 input_report_abs(ts->idev, ABS_Y, y);
128 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
129 if (!ts->pen_down) {
130 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
131 ts->pen_down = true;
132 }
133 } else {
134 input_report_abs(ts->idev, ABS_PRESSURE, 0);
135 if (ts->pen_down) {
136 input_report_key(ts->idev, BTN_TOUCH, 0);
137 ts->pen_down = false;
138 }
139 }
140 input_sync(ts->idev);
141 dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
142 pressure);
143}
144
Michael Wellingef3b98c2015-11-02 17:51:49 -0800145static irqreturn_t tsc200x_irq_thread(int irq, void *_ts)
Michael Welling6ac24382015-11-02 17:45:51 -0800146{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800147 struct tsc200x *ts = _ts;
Michael Welling6ac24382015-11-02 17:45:51 -0800148 unsigned long flags;
149 unsigned int pressure;
Michael Wellingef3b98c2015-11-02 17:51:49 -0800150 struct tsc200x_data tsdata;
Michael Welling6ac24382015-11-02 17:45:51 -0800151 int error;
152
153 /* read the coordinates */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800154 error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata,
155 TSC200X_DATA_REGS);
Michael Welling6ac24382015-11-02 17:45:51 -0800156 if (unlikely(error))
157 goto out;
158
159 /* validate position */
160 if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))
161 goto out;
162
163 /* Skip reading if the pressure components are out of range */
164 if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))
165 goto out;
166 if (unlikely(tsdata.z1 >= tsdata.z2))
167 goto out;
168
169 /*
170 * Skip point if this is a pen down with the exact same values as
171 * the value before pen-up - that implies SPI fed us stale data
172 */
173 if (!ts->pen_down &&
174 ts->in_x == tsdata.x && ts->in_y == tsdata.y &&
175 ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {
176 goto out;
177 }
178
179 /*
180 * At this point we are happy we have a valid and useful reading.
181 * Remember it for later comparisons. We may now begin downsampling.
182 */
183 ts->in_x = tsdata.x;
184 ts->in_y = tsdata.y;
185 ts->in_z1 = tsdata.z1;
186 ts->in_z2 = tsdata.z2;
187
188 /* Compute touch pressure resistance using equation #1 */
189 pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1;
190 pressure = pressure * ts->x_plate_ohm / 4096;
191 if (unlikely(pressure > MAX_12BIT))
192 goto out;
193
194 spin_lock_irqsave(&ts->lock, flags);
195
Michael Wellingef3b98c2015-11-02 17:51:49 -0800196 tsc200x_update_pen_state(ts, tsdata.x, tsdata.y, pressure);
Michael Welling6ac24382015-11-02 17:45:51 -0800197 mod_timer(&ts->penup_timer,
Michael Wellingef3b98c2015-11-02 17:51:49 -0800198 jiffies + msecs_to_jiffies(TSC200X_PENUP_TIME_MS));
Michael Welling6ac24382015-11-02 17:45:51 -0800199
200 spin_unlock_irqrestore(&ts->lock, flags);
201
202 ts->last_valid_interrupt = jiffies;
203out:
204 return IRQ_HANDLED;
205}
206
Michael Wellingef3b98c2015-11-02 17:51:49 -0800207static void tsc200x_penup_timer(unsigned long data)
Michael Welling6ac24382015-11-02 17:45:51 -0800208{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800209 struct tsc200x *ts = (struct tsc200x *)data;
Michael Welling6ac24382015-11-02 17:45:51 -0800210 unsigned long flags;
211
212 spin_lock_irqsave(&ts->lock, flags);
Michael Wellingef3b98c2015-11-02 17:51:49 -0800213 tsc200x_update_pen_state(ts, 0, 0, 0);
Michael Welling6ac24382015-11-02 17:45:51 -0800214 spin_unlock_irqrestore(&ts->lock, flags);
215}
216
Michael Wellingef3b98c2015-11-02 17:51:49 -0800217static void tsc200x_start_scan(struct tsc200x *ts)
Michael Welling6ac24382015-11-02 17:45:51 -0800218{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800219 regmap_write(ts->regmap, TSC200X_REG_CFR0, TSC200X_CFR0_INITVALUE);
220 regmap_write(ts->regmap, TSC200X_REG_CFR1, TSC200X_CFR1_INITVALUE);
221 regmap_write(ts->regmap, TSC200X_REG_CFR2, TSC200X_CFR2_INITVALUE);
222 ts->tsc200x_cmd(ts->dev, TSC200X_CMD_NORMAL);
Michael Welling6ac24382015-11-02 17:45:51 -0800223}
224
Michael Wellingef3b98c2015-11-02 17:51:49 -0800225static void tsc200x_stop_scan(struct tsc200x *ts)
Michael Welling6ac24382015-11-02 17:45:51 -0800226{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800227 ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP);
Michael Welling6ac24382015-11-02 17:45:51 -0800228}
229
Michael Wellingef3b98c2015-11-02 17:51:49 -0800230static void tsc200x_set_reset(struct tsc200x *ts, bool enable)
Michael Welling6ac24382015-11-02 17:45:51 -0800231{
232 if (ts->reset_gpio)
233 gpiod_set_value_cansleep(ts->reset_gpio, enable);
234 else if (ts->set_reset)
235 ts->set_reset(enable);
236}
237
238/* must be called with ts->mutex held */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800239static void __tsc200x_disable(struct tsc200x *ts)
Michael Welling6ac24382015-11-02 17:45:51 -0800240{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800241 tsc200x_stop_scan(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800242
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 Wellingef3b98c2015-11-02 17:51:49 -0800252static void __tsc200x_enable(struct tsc200x *ts)
Michael Welling6ac24382015-11-02 17:45:51 -0800253{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800254 tsc200x_start_scan(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800255
256 if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) {
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 Wellingef3b98c2015-11-02 17:51:49 -0800264static ssize_t tsc200x_selftest_show(struct device *dev,
Michael Welling6ac24382015-11-02 17:45:51 -0800265 struct device_attribute *attr,
266 char *buf)
267{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800268 struct tsc200x *ts = dev_get_drvdata(dev);
Michael Welling6ac24382015-11-02 17:45:51 -0800269 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 Wellingef3b98c2015-11-02 17:51:49 -0800278 * Test TSC200X communications via temp high register.
Michael Welling6ac24382015-11-02 17:45:51 -0800279 */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800280 __tsc200x_disable(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800281
Michael Wellingef3b98c2015-11-02 17:51:49 -0800282 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig);
Michael Welling6ac24382015-11-02 17:45:51 -0800283 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 Wellingef3b98c2015-11-02 17:51:49 -0800291 error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test);
Michael Welling6ac24382015-11-02 17:45:51 -0800292 if (error) {
293 dev_warn(dev, "selftest failed: write error %d\n", error);
294 success = false;
295 goto out;
296 }
297
Michael Wellingef3b98c2015-11-02 17:51:49 -0800298 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
Michael Welling6ac24382015-11-02 17:45:51 -0800299 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 */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800313 tsc200x_set_reset(ts, false);
Michael Welling6ac24382015-11-02 17:45:51 -0800314 usleep_range(100, 500); /* only 10us required */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800315 tsc200x_set_reset(ts, true);
Michael Welling6ac24382015-11-02 17:45:51 -0800316
317 if (!success)
318 goto out;
319
320 /* test that the reset really happened */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800321 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
Michael Welling6ac24382015-11-02 17:45:51 -0800322 if (error) {
323 dev_warn(dev, "selftest failed: read error %d after reset\n",
324 error);
325 success = false;
326 goto out;
327 }
328
329 if (temp_high != temp_high_orig) {
330 dev_warn(dev, "selftest failed after reset: %d != %d\n",
331 temp_high, temp_high_orig);
332 success = false;
333 }
334
335out:
Michael Wellingef3b98c2015-11-02 17:51:49 -0800336 __tsc200x_enable(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800337 mutex_unlock(&ts->mutex);
338
339 return sprintf(buf, "%d\n", success);
340}
341
Michael Wellingef3b98c2015-11-02 17:51:49 -0800342static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL);
Michael Welling6ac24382015-11-02 17:45:51 -0800343
Michael Wellingef3b98c2015-11-02 17:51:49 -0800344static struct attribute *tsc200x_attrs[] = {
Michael Welling6ac24382015-11-02 17:45:51 -0800345 &dev_attr_selftest.attr,
346 NULL
347};
348
Michael Wellingef3b98c2015-11-02 17:51:49 -0800349static umode_t tsc200x_attr_is_visible(struct kobject *kobj,
Michael Welling6ac24382015-11-02 17:45:51 -0800350 struct attribute *attr, int n)
351{
352 struct device *dev = container_of(kobj, struct device, kobj);
Michael Wellingef3b98c2015-11-02 17:51:49 -0800353 struct tsc200x *ts = dev_get_drvdata(dev);
Michael Welling6ac24382015-11-02 17:45:51 -0800354 umode_t mode = attr->mode;
355
356 if (attr == &dev_attr_selftest.attr) {
357 if (!ts->set_reset && !ts->reset_gpio)
358 mode = 0;
359 }
360
361 return mode;
362}
363
Michael Wellingef3b98c2015-11-02 17:51:49 -0800364static const struct attribute_group tsc200x_attr_group = {
365 .is_visible = tsc200x_attr_is_visible,
366 .attrs = tsc200x_attrs,
Michael Welling6ac24382015-11-02 17:45:51 -0800367};
368
Michael Wellingef3b98c2015-11-02 17:51:49 -0800369static void tsc200x_esd_work(struct work_struct *work)
Michael Welling6ac24382015-11-02 17:45:51 -0800370{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800371 struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work);
Michael Welling6ac24382015-11-02 17:45:51 -0800372 int error;
373 unsigned int r;
374
375 if (!mutex_trylock(&ts->mutex)) {
376 /*
377 * If the mutex is taken, it means that disable or enable is in
378 * progress. In that case just reschedule the work. If the work
379 * is not needed, it will be canceled by disable.
380 */
381 goto reschedule;
382 }
383
384 if (time_is_after_jiffies(ts->last_valid_interrupt +
385 msecs_to_jiffies(ts->esd_timeout)))
386 goto out;
387
388 /* We should be able to read register without disabling interrupts. */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800389 error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r);
Michael Welling6ac24382015-11-02 17:45:51 -0800390 if (!error &&
Michael Wellingef3b98c2015-11-02 17:51:49 -0800391 !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) {
Michael Welling6ac24382015-11-02 17:45:51 -0800392 goto out;
393 }
394
395 /*
396 * If we could not read our known value from configuration register 0
397 * then we should reset the controller as if from power-up and start
398 * scanning again.
399 */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800400 dev_info(ts->dev, "TSC200X not responding - resetting\n");
Michael Welling6ac24382015-11-02 17:45:51 -0800401
402 disable_irq(ts->irq);
403 del_timer_sync(&ts->penup_timer);
404
Michael Wellingef3b98c2015-11-02 17:51:49 -0800405 tsc200x_update_pen_state(ts, 0, 0, 0);
Michael Welling6ac24382015-11-02 17:45:51 -0800406
Michael Wellingef3b98c2015-11-02 17:51:49 -0800407 tsc200x_set_reset(ts, false);
Michael Welling6ac24382015-11-02 17:45:51 -0800408 usleep_range(100, 500); /* only 10us required */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800409 tsc200x_set_reset(ts, true);
Michael Welling6ac24382015-11-02 17:45:51 -0800410
411 enable_irq(ts->irq);
Michael Wellingef3b98c2015-11-02 17:51:49 -0800412 tsc200x_start_scan(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800413
414out:
415 mutex_unlock(&ts->mutex);
416reschedule:
417 /* re-arm the watchdog */
418 schedule_delayed_work(&ts->esd_work,
419 round_jiffies_relative(
420 msecs_to_jiffies(ts->esd_timeout)));
421}
422
Michael Wellingef3b98c2015-11-02 17:51:49 -0800423static int tsc200x_open(struct input_dev *input)
Michael Welling6ac24382015-11-02 17:45:51 -0800424{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800425 struct tsc200x *ts = input_get_drvdata(input);
Michael Welling6ac24382015-11-02 17:45:51 -0800426
427 mutex_lock(&ts->mutex);
428
429 if (!ts->suspended)
Michael Wellingef3b98c2015-11-02 17:51:49 -0800430 __tsc200x_enable(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800431
432 ts->opened = true;
433
434 mutex_unlock(&ts->mutex);
435
436 return 0;
437}
438
Michael Wellingef3b98c2015-11-02 17:51:49 -0800439static void tsc200x_close(struct input_dev *input)
Michael Welling6ac24382015-11-02 17:45:51 -0800440{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800441 struct tsc200x *ts = input_get_drvdata(input);
Michael Welling6ac24382015-11-02 17:45:51 -0800442
443 mutex_lock(&ts->mutex);
444
445 if (!ts->suspended)
Michael Wellingef3b98c2015-11-02 17:51:49 -0800446 __tsc200x_disable(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800447
448 ts->opened = false;
449
450 mutex_unlock(&ts->mutex);
451}
452
Michael Wellinge9003c92016-07-20 10:02:07 -0700453int tsc200x_probe(struct device *dev, int irq, const struct input_id *tsc_id,
Michael Welling6ac24382015-11-02 17:45:51 -0800454 struct regmap *regmap,
455 int (*tsc200x_cmd)(struct device *dev, u8 cmd))
456{
457 const struct tsc2005_platform_data *pdata = dev_get_platdata(dev);
458 struct device_node *np = dev->of_node;
459
Michael Wellingef3b98c2015-11-02 17:51:49 -0800460 struct tsc200x *ts;
Michael Welling6ac24382015-11-02 17:45:51 -0800461 struct input_dev *input_dev;
462 unsigned int max_x = MAX_12BIT;
463 unsigned int max_y = MAX_12BIT;
464 unsigned int max_p = MAX_12BIT;
Michael Wellingef3b98c2015-11-02 17:51:49 -0800465 unsigned int fudge_x = TSC200X_DEF_X_FUZZ;
466 unsigned int fudge_y = TSC200X_DEF_Y_FUZZ;
467 unsigned int fudge_p = TSC200X_DEF_P_FUZZ;
468 unsigned int x_plate_ohm = TSC200X_DEF_RESISTOR;
Michael Welling6ac24382015-11-02 17:45:51 -0800469 unsigned int esd_timeout;
470 int error;
471
472 if (!np && !pdata) {
473 dev_err(dev, "no platform data\n");
474 return -ENODEV;
475 }
476
477 if (irq <= 0) {
478 dev_err(dev, "no irq\n");
479 return -ENODEV;
480 }
481
482 if (IS_ERR(regmap))
483 return PTR_ERR(regmap);
484
485 if (!tsc200x_cmd) {
486 dev_err(dev, "no cmd function\n");
487 return -ENODEV;
488 }
489
490 if (pdata) {
491 fudge_x = pdata->ts_x_fudge;
492 fudge_y = pdata->ts_y_fudge;
493 fudge_p = pdata->ts_pressure_fudge;
494 max_x = pdata->ts_x_max;
495 max_y = pdata->ts_y_max;
496 max_p = pdata->ts_pressure_max;
497 x_plate_ohm = pdata->ts_x_plate_ohm;
498 esd_timeout = pdata->esd_timeout_ms;
499 } else {
Michael Wellingef3b98c2015-11-02 17:51:49 -0800500 x_plate_ohm = TSC200X_DEF_RESISTOR;
Michael Welling6ac24382015-11-02 17:45:51 -0800501 of_property_read_u32(np, "ti,x-plate-ohms", &x_plate_ohm);
502 esd_timeout = 0;
503 of_property_read_u32(np, "ti,esd-recovery-timeout-ms",
504 &esd_timeout);
505 }
506
507 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
508 if (!ts)
509 return -ENOMEM;
510
511 input_dev = devm_input_allocate_device(dev);
512 if (!input_dev)
513 return -ENOMEM;
514
515 ts->irq = irq;
516 ts->dev = dev;
517 ts->idev = input_dev;
518 ts->regmap = regmap;
519 ts->tsc200x_cmd = tsc200x_cmd;
520 ts->x_plate_ohm = x_plate_ohm;
521 ts->esd_timeout = esd_timeout;
522
523 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
524 if (IS_ERR(ts->reset_gpio)) {
525 error = PTR_ERR(ts->reset_gpio);
526 dev_err(dev, "error acquiring reset gpio: %d\n", error);
527 return error;
528 }
529
530 ts->vio = devm_regulator_get_optional(dev, "vio");
531 if (IS_ERR(ts->vio)) {
532 error = PTR_ERR(ts->vio);
533 dev_err(dev, "vio regulator missing (%d)", error);
534 return error;
535 }
536
537 if (!ts->reset_gpio && pdata)
538 ts->set_reset = pdata->set_reset;
539
540 mutex_init(&ts->mutex);
541
542 spin_lock_init(&ts->lock);
Michael Wellingef3b98c2015-11-02 17:51:49 -0800543 setup_timer(&ts->penup_timer, tsc200x_penup_timer, (unsigned long)ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800544
Michael Wellingef3b98c2015-11-02 17:51:49 -0800545 INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work);
Michael Welling6ac24382015-11-02 17:45:51 -0800546
547 snprintf(ts->phys, sizeof(ts->phys),
548 "%s/input-ts", dev_name(dev));
549
Michael Wellinge9003c92016-07-20 10:02:07 -0700550 if (tsc_id->product == 2004) {
551 input_dev->name = "TSC200X touchscreen";
552 } else {
553 input_dev->name = devm_kasprintf(dev, GFP_KERNEL,
554 "TSC%04d touchscreen",
555 tsc_id->product);
556 if (!input_dev->name)
557 return -ENOMEM;
558 }
559
Michael Welling6ac24382015-11-02 17:45:51 -0800560 input_dev->phys = ts->phys;
Michael Wellinge9003c92016-07-20 10:02:07 -0700561 input_dev->id = *tsc_id;
Michael Welling6ac24382015-11-02 17:45:51 -0800562 input_dev->dev.parent = dev;
563 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
564 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
565
566 input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
567 input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
568 input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
569
570 if (np)
Hans de Goedeed7c9872016-07-15 14:05:29 -0700571 touchscreen_parse_properties(input_dev, false, NULL);
Michael Welling6ac24382015-11-02 17:45:51 -0800572
Michael Wellingef3b98c2015-11-02 17:51:49 -0800573 input_dev->open = tsc200x_open;
574 input_dev->close = tsc200x_close;
Michael Welling6ac24382015-11-02 17:45:51 -0800575
576 input_set_drvdata(input_dev, ts);
577
578 /* Ensure the touchscreen is off */
Michael Wellingef3b98c2015-11-02 17:51:49 -0800579 tsc200x_stop_scan(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800580
581 error = devm_request_threaded_irq(dev, irq, NULL,
Michael Wellingef3b98c2015-11-02 17:51:49 -0800582 tsc200x_irq_thread,
Michael Welling6ac24382015-11-02 17:45:51 -0800583 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Michael Wellingef3b98c2015-11-02 17:51:49 -0800584 "tsc200x", ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800585 if (error) {
586 dev_err(dev, "Failed to request irq, err: %d\n", error);
587 return error;
588 }
589
590 /* enable regulator for DT */
591 if (ts->vio) {
592 error = regulator_enable(ts->vio);
593 if (error)
594 return error;
595 }
596
597 dev_set_drvdata(dev, ts);
Michael Wellingef3b98c2015-11-02 17:51:49 -0800598 error = sysfs_create_group(&dev->kobj, &tsc200x_attr_group);
Michael Welling6ac24382015-11-02 17:45:51 -0800599 if (error) {
600 dev_err(dev,
601 "Failed to create sysfs attributes, err: %d\n", error);
602 goto disable_regulator;
603 }
604
605 error = input_register_device(ts->idev);
606 if (error) {
607 dev_err(dev,
608 "Failed to register input device, err: %d\n", error);
609 goto err_remove_sysfs;
610 }
611
612 irq_set_irq_wake(irq, 1);
613 return 0;
614
615err_remove_sysfs:
Michael Wellingef3b98c2015-11-02 17:51:49 -0800616 sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);
Michael Welling6ac24382015-11-02 17:45:51 -0800617disable_regulator:
618 if (ts->vio)
619 regulator_disable(ts->vio);
620 return error;
621}
622EXPORT_SYMBOL_GPL(tsc200x_probe);
623
624int tsc200x_remove(struct device *dev)
625{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800626 struct tsc200x *ts = dev_get_drvdata(dev);
Michael Welling6ac24382015-11-02 17:45:51 -0800627
Michael Wellingef3b98c2015-11-02 17:51:49 -0800628 sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);
Michael Welling6ac24382015-11-02 17:45:51 -0800629
630 if (ts->vio)
631 regulator_disable(ts->vio);
632
633 return 0;
634}
635EXPORT_SYMBOL_GPL(tsc200x_remove);
636
Michael Wellingef3b98c2015-11-02 17:51:49 -0800637static int __maybe_unused tsc200x_suspend(struct device *dev)
Michael Welling6ac24382015-11-02 17:45:51 -0800638{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800639 struct tsc200x *ts = dev_get_drvdata(dev);
Michael Welling6ac24382015-11-02 17:45:51 -0800640
641 mutex_lock(&ts->mutex);
642
643 if (!ts->suspended && ts->opened)
Michael Wellingef3b98c2015-11-02 17:51:49 -0800644 __tsc200x_disable(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800645
646 ts->suspended = true;
647
648 mutex_unlock(&ts->mutex);
649
650 return 0;
651}
652
Michael Wellingef3b98c2015-11-02 17:51:49 -0800653static int __maybe_unused tsc200x_resume(struct device *dev)
Michael Welling6ac24382015-11-02 17:45:51 -0800654{
Michael Wellingef3b98c2015-11-02 17:51:49 -0800655 struct tsc200x *ts = dev_get_drvdata(dev);
Michael Welling6ac24382015-11-02 17:45:51 -0800656
657 mutex_lock(&ts->mutex);
658
659 if (ts->suspended && ts->opened)
Michael Wellingef3b98c2015-11-02 17:51:49 -0800660 __tsc200x_enable(ts);
Michael Welling6ac24382015-11-02 17:45:51 -0800661
662 ts->suspended = false;
663
664 mutex_unlock(&ts->mutex);
665
666 return 0;
667}
668
Michael Wellingef3b98c2015-11-02 17:51:49 -0800669SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume);
Michael Welling6ac24382015-11-02 17:45:51 -0800670EXPORT_SYMBOL_GPL(tsc200x_pm_ops);
671
672MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
Michael Wellingef3b98c2015-11-02 17:51:49 -0800673MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core");
Michael Welling6ac24382015-11-02 17:45:51 -0800674MODULE_LICENSE("GPL");