tsc2005.c 20 KB

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