tsc200x-core.c 15 KB

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