tsc200x-core.c 14 KB

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