tsc2005.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * TSC2005 touchscreen driver
  3. *
  4. * Copyright (C) 2006-2010 Nokia Corporation
  5. *
  6. * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
  7. * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/input.h>
  27. #include <linux/input/touchscreen.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/pm.h>
  31. #include <linux/of.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/spi/tsc2005.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <linux/regmap.h>
  36. #include <linux/gpio/consumer.h>
  37. /*
  38. * The touchscreen interface operates as follows:
  39. *
  40. * 1) Pen is pressed against the touchscreen.
  41. * 2) TSC2005 performs AD conversion.
  42. * 3) After the conversion is done TSC2005 drives DAV line down.
  43. * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled.
  44. * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2
  45. * values.
  46. * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up
  47. * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms).
  48. * 7) When the penup timer expires, there have not been touch or DAV interrupts
  49. * during the last 40ms which means the pen has been lifted.
  50. *
  51. * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond
  52. * after a configurable period (in ms) of activity. If esd_timeout is 0, the
  53. * watchdog is disabled.
  54. */
  55. /* control byte 1 */
  56. #define TSC2005_CMD 0x80
  57. #define TSC2005_CMD_NORMAL 0x00
  58. #define TSC2005_CMD_STOP 0x01
  59. #define TSC2005_CMD_12BIT 0x04
  60. /* control byte 0 */
  61. #define TSC2005_REG_READ 0x01 /* R/W access */
  62. #define TSC2005_REG_PND0 0x02 /* Power Not Down Control */
  63. #define TSC2005_REG_X (0x0 << 3)
  64. #define TSC2005_REG_Y (0x1 << 3)
  65. #define TSC2005_REG_Z1 (0x2 << 3)
  66. #define TSC2005_REG_Z2 (0x3 << 3)
  67. #define TSC2005_REG_AUX (0x4 << 3)
  68. #define TSC2005_REG_TEMP1 (0x5 << 3)
  69. #define TSC2005_REG_TEMP2 (0x6 << 3)
  70. #define TSC2005_REG_STATUS (0x7 << 3)
  71. #define TSC2005_REG_AUX_HIGH (0x8 << 3)
  72. #define TSC2005_REG_AUX_LOW (0x9 << 3)
  73. #define TSC2005_REG_TEMP_HIGH (0xA << 3)
  74. #define TSC2005_REG_TEMP_LOW (0xB << 3)
  75. #define TSC2005_REG_CFR0 (0xC << 3)
  76. #define TSC2005_REG_CFR1 (0xD << 3)
  77. #define TSC2005_REG_CFR2 (0xE << 3)
  78. #define TSC2005_REG_CONV_FUNC (0xF << 3)
  79. /* configuration register 0 */
  80. #define TSC2005_CFR0_PRECHARGE_276US 0x0040
  81. #define TSC2005_CFR0_STABTIME_1MS 0x0300
  82. #define TSC2005_CFR0_CLOCK_1MHZ 0x1000
  83. #define TSC2005_CFR0_RESOLUTION12 0x2000
  84. #define TSC2005_CFR0_PENMODE 0x8000
  85. #define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \
  86. TSC2005_CFR0_CLOCK_1MHZ | \
  87. TSC2005_CFR0_RESOLUTION12 | \
  88. TSC2005_CFR0_PRECHARGE_276US | \
  89. TSC2005_CFR0_PENMODE)
  90. /* bits common to both read and write of configuration register 0 */
  91. #define TSC2005_CFR0_RW_MASK 0x3fff
  92. /* configuration register 1 */
  93. #define TSC2005_CFR1_BATCHDELAY_4MS 0x0003
  94. #define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS
  95. /* configuration register 2 */
  96. #define TSC2005_CFR2_MAVE_Z 0x0004
  97. #define TSC2005_CFR2_MAVE_Y 0x0008
  98. #define TSC2005_CFR2_MAVE_X 0x0010
  99. #define TSC2005_CFR2_AVG_7 0x0800
  100. #define TSC2005_CFR2_MEDIUM_15 0x3000
  101. #define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \
  102. TSC2005_CFR2_MAVE_Y | \
  103. TSC2005_CFR2_MAVE_Z | \
  104. TSC2005_CFR2_MEDIUM_15 | \
  105. TSC2005_CFR2_AVG_7)
  106. #define MAX_12BIT 0xfff
  107. #define TSC2005_DEF_X_FUZZ 4
  108. #define TSC2005_DEF_Y_FUZZ 8
  109. #define TSC2005_DEF_P_FUZZ 2
  110. #define TSC2005_DEF_RESISTOR 280
  111. #define TSC2005_SPI_MAX_SPEED_HZ 10000000
  112. #define TSC2005_PENUP_TIME_MS 40
  113. static const struct regmap_range tsc2005_writable_ranges[] = {
  114. regmap_reg_range(TSC2005_REG_AUX_HIGH, TSC2005_REG_CFR2),
  115. };
  116. static const struct regmap_access_table tsc2005_writable_table = {
  117. .yes_ranges = tsc2005_writable_ranges,
  118. .n_yes_ranges = ARRAY_SIZE(tsc2005_writable_ranges),
  119. };
  120. static struct regmap_config tsc2005_regmap_config = {
  121. .reg_bits = 8,
  122. .val_bits = 16,
  123. .reg_stride = 0x08,
  124. .max_register = 0x78,
  125. .read_flag_mask = TSC2005_REG_READ,
  126. .write_flag_mask = TSC2005_REG_PND0,
  127. .wr_table = &tsc2005_writable_table,
  128. .use_single_rw = true,
  129. };
  130. struct tsc2005_data {
  131. u16 x;
  132. u16 y;
  133. u16 z1;
  134. u16 z2;
  135. } __packed;
  136. #define TSC2005_DATA_REGS 4
  137. struct tsc2005 {
  138. struct spi_device *spi;
  139. struct regmap *regmap;
  140. struct input_dev *idev;
  141. char phys[32];
  142. struct mutex mutex;
  143. /* raw copy of previous x,y,z */
  144. int in_x;
  145. int in_y;
  146. int in_z1;
  147. int in_z2;
  148. spinlock_t lock;
  149. struct timer_list penup_timer;
  150. unsigned int esd_timeout;
  151. struct delayed_work esd_work;
  152. unsigned long last_valid_interrupt;
  153. unsigned int x_plate_ohm;
  154. bool opened;
  155. bool suspended;
  156. bool pen_down;
  157. struct regulator *vio;
  158. struct gpio_desc *reset_gpio;
  159. void (*set_reset)(bool enable);
  160. };
  161. static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
  162. {
  163. u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
  164. struct spi_transfer xfer = {
  165. .tx_buf = &tx,
  166. .len = 1,
  167. .bits_per_word = 8,
  168. };
  169. struct spi_message msg;
  170. int error;
  171. spi_message_init(&msg);
  172. spi_message_add_tail(&xfer, &msg);
  173. error = spi_sync(ts->spi, &msg);
  174. if (error) {
  175. dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n",
  176. __func__, cmd, error);
  177. return error;
  178. }
  179. return 0;
  180. }
  181. static void tsc2005_update_pen_state(struct tsc2005 *ts,
  182. int x, int y, int pressure)
  183. {
  184. if (pressure) {
  185. input_report_abs(ts->idev, ABS_X, x);
  186. input_report_abs(ts->idev, ABS_Y, y);
  187. input_report_abs(ts->idev, ABS_PRESSURE, pressure);
  188. if (!ts->pen_down) {
  189. input_report_key(ts->idev, BTN_TOUCH, !!pressure);
  190. ts->pen_down = true;
  191. }
  192. } else {
  193. input_report_abs(ts->idev, ABS_PRESSURE, 0);
  194. if (ts->pen_down) {
  195. input_report_key(ts->idev, BTN_TOUCH, 0);
  196. ts->pen_down = false;
  197. }
  198. }
  199. input_sync(ts->idev);
  200. dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
  201. pressure);
  202. }
  203. static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
  204. {
  205. struct tsc2005 *ts = _ts;
  206. unsigned long flags;
  207. unsigned int pressure;
  208. struct tsc2005_data tsdata;
  209. int error;
  210. /* read the coordinates */
  211. error = regmap_bulk_read(ts->regmap, TSC2005_REG_X, &tsdata,
  212. TSC2005_DATA_REGS);
  213. if (unlikely(error))
  214. goto out;
  215. /* validate position */
  216. if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))
  217. goto out;
  218. /* Skip reading if the pressure components are out of range */
  219. if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))
  220. goto out;
  221. if (unlikely(tsdata.z1 >= tsdata.z2))
  222. goto out;
  223. /*
  224. * Skip point if this is a pen down with the exact same values as
  225. * the value before pen-up - that implies SPI fed us stale data
  226. */
  227. if (!ts->pen_down &&
  228. ts->in_x == tsdata.x && ts->in_y == tsdata.y &&
  229. ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {
  230. goto out;
  231. }
  232. /*
  233. * At this point we are happy we have a valid and useful reading.
  234. * Remember it for later comparisons. We may now begin downsampling.
  235. */
  236. ts->in_x = tsdata.x;
  237. ts->in_y = tsdata.y;
  238. ts->in_z1 = tsdata.z1;
  239. ts->in_z2 = tsdata.z2;
  240. /* Compute touch pressure resistance using equation #1 */
  241. pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1;
  242. pressure = pressure * ts->x_plate_ohm / 4096;
  243. if (unlikely(pressure > MAX_12BIT))
  244. goto out;
  245. spin_lock_irqsave(&ts->lock, flags);
  246. tsc2005_update_pen_state(ts, tsdata.x, tsdata.y, pressure);
  247. mod_timer(&ts->penup_timer,
  248. jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
  249. spin_unlock_irqrestore(&ts->lock, flags);
  250. ts->last_valid_interrupt = jiffies;
  251. out:
  252. return IRQ_HANDLED;
  253. }
  254. static void tsc2005_penup_timer(unsigned long data)
  255. {
  256. struct tsc2005 *ts = (struct tsc2005 *)data;
  257. unsigned long flags;
  258. spin_lock_irqsave(&ts->lock, flags);
  259. tsc2005_update_pen_state(ts, 0, 0, 0);
  260. spin_unlock_irqrestore(&ts->lock, flags);
  261. }
  262. static void tsc2005_start_scan(struct tsc2005 *ts)
  263. {
  264. regmap_write(ts->regmap, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
  265. regmap_write(ts->regmap, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
  266. regmap_write(ts->regmap, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
  267. tsc2005_cmd(ts, TSC2005_CMD_NORMAL);
  268. }
  269. static void tsc2005_stop_scan(struct tsc2005 *ts)
  270. {
  271. tsc2005_cmd(ts, TSC2005_CMD_STOP);
  272. }
  273. static void tsc2005_set_reset(struct tsc2005 *ts, bool enable)
  274. {
  275. if (ts->reset_gpio)
  276. gpiod_set_value_cansleep(ts->reset_gpio, enable);
  277. else if (ts->set_reset)
  278. ts->set_reset(enable);
  279. }
  280. /* must be called with ts->mutex held */
  281. static void __tsc2005_disable(struct tsc2005 *ts)
  282. {
  283. tsc2005_stop_scan(ts);
  284. disable_irq(ts->spi->irq);
  285. del_timer_sync(&ts->penup_timer);
  286. cancel_delayed_work_sync(&ts->esd_work);
  287. enable_irq(ts->spi->irq);
  288. }
  289. /* must be called with ts->mutex held */
  290. static void __tsc2005_enable(struct tsc2005 *ts)
  291. {
  292. tsc2005_start_scan(ts);
  293. if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) {
  294. ts->last_valid_interrupt = jiffies;
  295. schedule_delayed_work(&ts->esd_work,
  296. round_jiffies_relative(
  297. msecs_to_jiffies(ts->esd_timeout)));
  298. }
  299. }
  300. static ssize_t tsc2005_selftest_show(struct device *dev,
  301. struct device_attribute *attr,
  302. char *buf)
  303. {
  304. struct tsc2005 *ts = dev_get_drvdata(dev);
  305. unsigned int temp_high;
  306. unsigned int temp_high_orig;
  307. unsigned int temp_high_test;
  308. bool success = true;
  309. int error;
  310. mutex_lock(&ts->mutex);
  311. /*
  312. * Test TSC2005 communications via temp high register.
  313. */
  314. __tsc2005_disable(ts);
  315. error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
  316. if (error) {
  317. dev_warn(dev, "selftest failed: read error %d\n", error);
  318. success = false;
  319. goto out;
  320. }
  321. temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
  322. error = regmap_write(ts->regmap, TSC2005_REG_TEMP_HIGH, temp_high_test);
  323. if (error) {
  324. dev_warn(dev, "selftest failed: write error %d\n", error);
  325. success = false;
  326. goto out;
  327. }
  328. error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high);
  329. if (error) {
  330. dev_warn(dev, "selftest failed: read error %d after write\n",
  331. error);
  332. success = false;
  333. goto out;
  334. }
  335. if (temp_high != temp_high_test) {
  336. dev_warn(dev, "selftest failed: %d != %d\n",
  337. temp_high, temp_high_test);
  338. success = false;
  339. }
  340. /* hardware reset */
  341. tsc2005_set_reset(ts, false);
  342. usleep_range(100, 500); /* only 10us required */
  343. tsc2005_set_reset(ts, true);
  344. if (!success)
  345. goto out;
  346. /* test that the reset really happened */
  347. error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high);
  348. if (error) {
  349. dev_warn(dev, "selftest failed: read error %d after reset\n",
  350. error);
  351. success = false;
  352. goto out;
  353. }
  354. if (temp_high != temp_high_orig) {
  355. dev_warn(dev, "selftest failed after reset: %d != %d\n",
  356. temp_high, temp_high_orig);
  357. success = false;
  358. }
  359. out:
  360. __tsc2005_enable(ts);
  361. mutex_unlock(&ts->mutex);
  362. return sprintf(buf, "%d\n", success);
  363. }
  364. static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
  365. static struct attribute *tsc2005_attrs[] = {
  366. &dev_attr_selftest.attr,
  367. NULL
  368. };
  369. static umode_t tsc2005_attr_is_visible(struct kobject *kobj,
  370. struct attribute *attr, int n)
  371. {
  372. struct device *dev = container_of(kobj, struct device, kobj);
  373. struct tsc2005 *ts = dev_get_drvdata(dev);
  374. umode_t mode = attr->mode;
  375. if (attr == &dev_attr_selftest.attr) {
  376. if (!ts->set_reset && !ts->reset_gpio)
  377. mode = 0;
  378. }
  379. return mode;
  380. }
  381. static const struct attribute_group tsc2005_attr_group = {
  382. .is_visible = tsc2005_attr_is_visible,
  383. .attrs = tsc2005_attrs,
  384. };
  385. static void tsc2005_esd_work(struct work_struct *work)
  386. {
  387. struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);
  388. int error;
  389. unsigned int r;
  390. if (!mutex_trylock(&ts->mutex)) {
  391. /*
  392. * If the mutex is taken, it means that disable or enable is in
  393. * progress. In that case just reschedule the work. If the work
  394. * is not needed, it will be canceled by disable.
  395. */
  396. goto reschedule;
  397. }
  398. if (time_is_after_jiffies(ts->last_valid_interrupt +
  399. msecs_to_jiffies(ts->esd_timeout)))
  400. goto out;
  401. /* We should be able to read register without disabling interrupts. */
  402. error = regmap_read(ts->regmap, TSC2005_REG_CFR0, &r);
  403. if (!error &&
  404. !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
  405. goto out;
  406. }
  407. /*
  408. * If we could not read our known value from configuration register 0
  409. * then we should reset the controller as if from power-up and start
  410. * scanning again.
  411. */
  412. dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
  413. disable_irq(ts->spi->irq);
  414. del_timer_sync(&ts->penup_timer);
  415. tsc2005_update_pen_state(ts, 0, 0, 0);
  416. tsc2005_set_reset(ts, false);
  417. usleep_range(100, 500); /* only 10us required */
  418. tsc2005_set_reset(ts, true);
  419. enable_irq(ts->spi->irq);
  420. tsc2005_start_scan(ts);
  421. out:
  422. mutex_unlock(&ts->mutex);
  423. reschedule:
  424. /* re-arm the watchdog */
  425. schedule_delayed_work(&ts->esd_work,
  426. round_jiffies_relative(
  427. msecs_to_jiffies(ts->esd_timeout)));
  428. }
  429. static int tsc2005_open(struct input_dev *input)
  430. {
  431. struct tsc2005 *ts = input_get_drvdata(input);
  432. mutex_lock(&ts->mutex);
  433. if (!ts->suspended)
  434. __tsc2005_enable(ts);
  435. ts->opened = true;
  436. mutex_unlock(&ts->mutex);
  437. return 0;
  438. }
  439. static void tsc2005_close(struct input_dev *input)
  440. {
  441. struct tsc2005 *ts = input_get_drvdata(input);
  442. mutex_lock(&ts->mutex);
  443. if (!ts->suspended)
  444. __tsc2005_disable(ts);
  445. ts->opened = false;
  446. mutex_unlock(&ts->mutex);
  447. }
  448. static int tsc2005_probe(struct spi_device *spi)
  449. {
  450. const struct tsc2005_platform_data *pdata = dev_get_platdata(&spi->dev);
  451. struct device_node *np = spi->dev.of_node;
  452. struct tsc2005 *ts;
  453. struct input_dev *input_dev;
  454. unsigned int max_x = MAX_12BIT;
  455. unsigned int max_y = MAX_12BIT;
  456. unsigned int max_p = MAX_12BIT;
  457. unsigned int fudge_x = TSC2005_DEF_X_FUZZ;
  458. unsigned int fudge_y = TSC2005_DEF_Y_FUZZ;
  459. unsigned int fudge_p = TSC2005_DEF_P_FUZZ;
  460. unsigned int x_plate_ohm = TSC2005_DEF_RESISTOR;
  461. unsigned int esd_timeout;
  462. int error;
  463. if (!np && !pdata) {
  464. dev_err(&spi->dev, "no platform data\n");
  465. return -ENODEV;
  466. }
  467. if (spi->irq <= 0) {
  468. dev_err(&spi->dev, "no irq\n");
  469. return -ENODEV;
  470. }
  471. if (pdata) {
  472. fudge_x = pdata->ts_x_fudge;
  473. fudge_y = pdata->ts_y_fudge;
  474. fudge_p = pdata->ts_pressure_fudge;
  475. max_x = pdata->ts_x_max;
  476. max_y = pdata->ts_y_max;
  477. max_p = pdata->ts_pressure_max;
  478. x_plate_ohm = pdata->ts_x_plate_ohm;
  479. esd_timeout = pdata->esd_timeout_ms;
  480. } else {
  481. x_plate_ohm = TSC2005_DEF_RESISTOR;
  482. of_property_read_u32(np, "ti,x-plate-ohms", &x_plate_ohm);
  483. esd_timeout = 0;
  484. of_property_read_u32(np, "ti,esd-recovery-timeout-ms",
  485. &esd_timeout);
  486. }
  487. spi->mode = SPI_MODE_0;
  488. spi->bits_per_word = 8;
  489. if (!spi->max_speed_hz)
  490. spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
  491. error = spi_setup(spi);
  492. if (error)
  493. return error;
  494. ts = devm_kzalloc(&spi->dev, sizeof(*ts), GFP_KERNEL);
  495. if (!ts)
  496. return -ENOMEM;
  497. input_dev = devm_input_allocate_device(&spi->dev);
  498. if (!input_dev)
  499. return -ENOMEM;
  500. ts->spi = spi;
  501. ts->idev = input_dev;
  502. ts->regmap = devm_regmap_init_spi(spi, &tsc2005_regmap_config);
  503. if (IS_ERR(ts->regmap))
  504. return PTR_ERR(ts->regmap);
  505. ts->x_plate_ohm = x_plate_ohm;
  506. ts->esd_timeout = esd_timeout;
  507. ts->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset",
  508. GPIOD_OUT_HIGH);
  509. if (IS_ERR(ts->reset_gpio)) {
  510. error = PTR_ERR(ts->reset_gpio);
  511. dev_err(&spi->dev, "error acquiring reset gpio: %d\n", error);
  512. return error;
  513. }
  514. ts->vio = devm_regulator_get_optional(&spi->dev, "vio");
  515. if (IS_ERR(ts->vio)) {
  516. error = PTR_ERR(ts->vio);
  517. dev_err(&spi->dev, "vio regulator missing (%d)", error);
  518. return error;
  519. }
  520. if (!ts->reset_gpio && pdata)
  521. ts->set_reset = pdata->set_reset;
  522. mutex_init(&ts->mutex);
  523. spin_lock_init(&ts->lock);
  524. setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
  525. INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
  526. snprintf(ts->phys, sizeof(ts->phys),
  527. "%s/input-ts", dev_name(&spi->dev));
  528. input_dev->name = "TSC2005 touchscreen";
  529. input_dev->phys = ts->phys;
  530. input_dev->id.bustype = BUS_SPI;
  531. input_dev->dev.parent = &spi->dev;
  532. input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
  533. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  534. input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
  535. input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
  536. input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
  537. if (np)
  538. touchscreen_parse_properties(input_dev, false);
  539. input_dev->open = tsc2005_open;
  540. input_dev->close = tsc2005_close;
  541. input_set_drvdata(input_dev, ts);
  542. /* Ensure the touchscreen is off */
  543. tsc2005_stop_scan(ts);
  544. error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
  545. tsc2005_irq_thread,
  546. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  547. "tsc2005", ts);
  548. if (error) {
  549. dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
  550. return error;
  551. }
  552. /* enable regulator for DT */
  553. if (ts->vio) {
  554. error = regulator_enable(ts->vio);
  555. if (error)
  556. return error;
  557. }
  558. dev_set_drvdata(&spi->dev, ts);
  559. error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
  560. if (error) {
  561. dev_err(&spi->dev,
  562. "Failed to create sysfs attributes, err: %d\n", error);
  563. goto disable_regulator;
  564. }
  565. error = input_register_device(ts->idev);
  566. if (error) {
  567. dev_err(&spi->dev,
  568. "Failed to register input device, err: %d\n", error);
  569. goto err_remove_sysfs;
  570. }
  571. irq_set_irq_wake(spi->irq, 1);
  572. return 0;
  573. err_remove_sysfs:
  574. sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
  575. disable_regulator:
  576. if (ts->vio)
  577. regulator_disable(ts->vio);
  578. return error;
  579. }
  580. static int tsc2005_remove(struct spi_device *spi)
  581. {
  582. struct tsc2005 *ts = dev_get_drvdata(&spi->dev);
  583. sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
  584. if (ts->vio)
  585. regulator_disable(ts->vio);
  586. return 0;
  587. }
  588. static int __maybe_unused tsc2005_suspend(struct device *dev)
  589. {
  590. struct tsc2005 *ts = dev_get_drvdata(dev);
  591. mutex_lock(&ts->mutex);
  592. if (!ts->suspended && ts->opened)
  593. __tsc2005_disable(ts);
  594. ts->suspended = true;
  595. mutex_unlock(&ts->mutex);
  596. return 0;
  597. }
  598. static int __maybe_unused tsc2005_resume(struct device *dev)
  599. {
  600. struct tsc2005 *ts = dev_get_drvdata(dev);
  601. mutex_lock(&ts->mutex);
  602. if (ts->suspended && ts->opened)
  603. __tsc2005_enable(ts);
  604. ts->suspended = false;
  605. mutex_unlock(&ts->mutex);
  606. return 0;
  607. }
  608. static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
  609. static struct spi_driver tsc2005_driver = {
  610. .driver = {
  611. .name = "tsc2005",
  612. .owner = THIS_MODULE,
  613. .pm = &tsc2005_pm_ops,
  614. },
  615. .probe = tsc2005_probe,
  616. .remove = tsc2005_remove,
  617. };
  618. module_spi_driver(tsc2005_driver);
  619. MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
  620. MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
  621. MODULE_LICENSE("GPL");
  622. MODULE_ALIAS("spi:tsc2005");