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