tsc200x-core.c 14 KB

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