tsc2005.c 18 KB

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