tsc2005.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. struct timer_list penup_timer;
  117. struct work_struct penup_work;
  118. unsigned int esd_timeout;
  119. struct timer_list esd_timer;
  120. struct work_struct esd_work;
  121. unsigned int x_plate_ohm;
  122. bool disabled;
  123. unsigned int disable_depth;
  124. unsigned int pen_down;
  125. void (*set_reset)(bool enable);
  126. };
  127. static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
  128. {
  129. u8 tx;
  130. struct spi_message msg;
  131. struct spi_transfer xfer = { 0 };
  132. tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
  133. xfer.tx_buf = &tx;
  134. xfer.rx_buf = NULL;
  135. xfer.len = 1;
  136. xfer.bits_per_word = 8;
  137. spi_message_init(&msg);
  138. spi_message_add_tail(&xfer, &msg);
  139. spi_sync(ts->spi, &msg);
  140. }
  141. static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
  142. {
  143. u32 tx;
  144. struct spi_message msg;
  145. struct spi_transfer xfer = { 0 };
  146. tx = (reg | TSC2005_REG_PND0) << 16;
  147. tx |= value;
  148. xfer.tx_buf = &tx;
  149. xfer.rx_buf = NULL;
  150. xfer.len = 4;
  151. xfer.bits_per_word = 24;
  152. spi_message_init(&msg);
  153. spi_message_add_tail(&xfer, &msg);
  154. spi_sync(ts->spi, &msg);
  155. }
  156. static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
  157. {
  158. rd->spi_tx = (reg | TSC2005_REG_READ) << 16;
  159. rd->spi_xfer.tx_buf = &rd->spi_tx;
  160. rd->spi_xfer.rx_buf = &rd->spi_rx;
  161. rd->spi_xfer.len = 4;
  162. rd->spi_xfer.bits_per_word = 24;
  163. rd->spi_xfer.cs_change = !last;
  164. }
  165. static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
  166. {
  167. struct spi_message msg;
  168. struct tsc2005_spi_rd spi_rd = { { 0 }, 0, 0 };
  169. tsc2005_setup_read(&spi_rd, reg, 1);
  170. spi_message_init(&msg);
  171. spi_message_add_tail(&spi_rd.spi_xfer, &msg);
  172. spi_sync(ts->spi, &msg);
  173. *value = spi_rd.spi_rx;
  174. }
  175. static void tsc2005_update_pen_state(struct tsc2005 *ts,
  176. int x, int y, int pressure)
  177. {
  178. if (pressure) {
  179. input_report_abs(ts->idev, ABS_X, x);
  180. input_report_abs(ts->idev, ABS_Y, y);
  181. input_report_abs(ts->idev, ABS_PRESSURE, pressure);
  182. if (!ts->pen_down) {
  183. input_report_key(ts->idev, BTN_TOUCH, !!pressure);
  184. ts->pen_down = 1;
  185. }
  186. } else {
  187. input_report_abs(ts->idev, ABS_PRESSURE, 0);
  188. if (ts->pen_down) {
  189. input_report_key(ts->idev, BTN_TOUCH, 0);
  190. ts->pen_down = 0;
  191. }
  192. }
  193. input_sync(ts->idev);
  194. dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
  195. pressure);
  196. }
  197. static irqreturn_t tsc2005_irq_handler(int irq, void *dev_id)
  198. {
  199. struct tsc2005 *ts = dev_id;
  200. /* update the penup timer only if it's pending */
  201. mod_timer_pending(&ts->penup_timer,
  202. jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
  203. return IRQ_WAKE_THREAD;
  204. }
  205. static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)
  206. {
  207. struct tsc2005 *ts = _ts;
  208. unsigned int pressure;
  209. u32 x;
  210. u32 y;
  211. u32 z1;
  212. u32 z2;
  213. mutex_lock(&ts->mutex);
  214. if (unlikely(ts->disable_depth))
  215. goto out;
  216. /* read the coordinates */
  217. spi_sync(ts->spi, &ts->spi_read_msg);
  218. x = ts->spi_x.spi_rx;
  219. y = ts->spi_y.spi_rx;
  220. z1 = ts->spi_z1.spi_rx;
  221. z2 = ts->spi_z2.spi_rx;
  222. /* validate position */
  223. if (unlikely(x > MAX_12BIT || y > MAX_12BIT))
  224. goto out;
  225. /* skip coords if the pressure components are out of range */
  226. if (unlikely(z1 == 0 || z2 > MAX_12BIT || z1 >= z2))
  227. goto out;
  228. /* skip point if this is a pen down with the exact same values as
  229. * the value before pen-up - that implies SPI fed us stale data
  230. */
  231. if (!ts->pen_down &&
  232. ts->in_x == x &&
  233. ts->in_y == y &&
  234. ts->in_z1 == z1 &&
  235. ts->in_z2 == z2)
  236. goto out;
  237. /* At this point we are happy we have a valid and useful reading.
  238. * Remember it for later comparisons. We may now begin downsampling
  239. */
  240. ts->in_x = x;
  241. ts->in_y = y;
  242. ts->in_z1 = z1;
  243. ts->in_z2 = z2;
  244. /* compute touch pressure resistance using equation #1 */
  245. pressure = x * (z2 - z1) / z1;
  246. pressure = pressure * ts->x_plate_ohm / 4096;
  247. if (unlikely(pressure > MAX_12BIT))
  248. goto out;
  249. tsc2005_update_pen_state(ts, x, y, pressure);
  250. /* set the penup timer */
  251. mod_timer(&ts->penup_timer,
  252. jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
  253. if (!ts->esd_timeout)
  254. goto out;
  255. /* update the watchdog timer */
  256. mod_timer(&ts->esd_timer,
  257. round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
  258. out:
  259. mutex_unlock(&ts->mutex);
  260. return IRQ_HANDLED;
  261. }
  262. static void tsc2005_penup_timer(unsigned long data)
  263. {
  264. struct tsc2005 *ts = (struct tsc2005 *)data;
  265. schedule_work(&ts->penup_work);
  266. }
  267. static void tsc2005_penup_work(struct work_struct *work)
  268. {
  269. struct tsc2005 *ts = container_of(work, struct tsc2005, penup_work);
  270. mutex_lock(&ts->mutex);
  271. tsc2005_update_pen_state(ts, 0, 0, 0);
  272. mutex_unlock(&ts->mutex);
  273. }
  274. static void tsc2005_start_scan(struct tsc2005 *ts)
  275. {
  276. tsc2005_write(ts, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);
  277. tsc2005_write(ts, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);
  278. tsc2005_write(ts, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);
  279. tsc2005_cmd(ts, TSC2005_CMD_NORMAL);
  280. }
  281. static void tsc2005_stop_scan(struct tsc2005 *ts)
  282. {
  283. tsc2005_cmd(ts, TSC2005_CMD_STOP);
  284. }
  285. /* must be called with mutex held */
  286. static void tsc2005_disable(struct tsc2005 *ts)
  287. {
  288. if (ts->disable_depth++ != 0)
  289. return;
  290. disable_irq(ts->spi->irq);
  291. if (ts->esd_timeout)
  292. del_timer_sync(&ts->esd_timer);
  293. del_timer_sync(&ts->penup_timer);
  294. tsc2005_stop_scan(ts);
  295. }
  296. /* must be called with mutex held */
  297. static void tsc2005_enable(struct tsc2005 *ts)
  298. {
  299. if (--ts->disable_depth != 0)
  300. return;
  301. tsc2005_start_scan(ts);
  302. enable_irq(ts->spi->irq);
  303. if (!ts->esd_timeout)
  304. return;
  305. mod_timer(&ts->esd_timer,
  306. round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
  307. }
  308. static ssize_t tsc2005_disable_show(struct device *dev,
  309. struct device_attribute *attr, char *buf)
  310. {
  311. struct spi_device *spi = to_spi_device(dev);
  312. struct tsc2005 *ts = spi_get_drvdata(spi);
  313. return sprintf(buf, "%u\n", ts->disabled);
  314. }
  315. static ssize_t tsc2005_disable_store(struct device *dev,
  316. struct device_attribute *attr,
  317. const char *buf, size_t count)
  318. {
  319. struct spi_device *spi = to_spi_device(dev);
  320. struct tsc2005 *ts = spi_get_drvdata(spi);
  321. unsigned long res;
  322. int i;
  323. if (strict_strtoul(buf, 10, &res) < 0)
  324. return -EINVAL;
  325. i = res ? 1 : 0;
  326. mutex_lock(&ts->mutex);
  327. if (i == ts->disabled)
  328. goto out;
  329. ts->disabled = i;
  330. if (i)
  331. tsc2005_disable(ts);
  332. else
  333. tsc2005_enable(ts);
  334. out:
  335. mutex_unlock(&ts->mutex);
  336. return count;
  337. }
  338. static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
  339. static ssize_t tsc2005_selftest_show(struct device *dev,
  340. struct device_attribute *attr,
  341. char *buf)
  342. {
  343. struct spi_device *spi = to_spi_device(dev);
  344. struct tsc2005 *ts = spi_get_drvdata(spi);
  345. u16 temp_high;
  346. u16 temp_high_orig;
  347. u16 temp_high_test;
  348. unsigned int result;
  349. if (!ts->set_reset) {
  350. dev_warn(&ts->spi->dev,
  351. "unable to selftest: no reset function\n");
  352. result = 0;
  353. goto out;
  354. }
  355. mutex_lock(&ts->mutex);
  356. /*
  357. * Test TSC2005 communications via temp high register.
  358. */
  359. tsc2005_disable(ts);
  360. result = 1;
  361. tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
  362. temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
  363. tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
  364. tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
  365. if (temp_high != temp_high_test) {
  366. dev_warn(dev, "selftest failed: %d != %d\n",
  367. temp_high, temp_high_test);
  368. result = 0;
  369. }
  370. /* hardware reset */
  371. ts->set_reset(0);
  372. usleep_range(100, 500); /* only 10us required */
  373. ts->set_reset(1);
  374. tsc2005_enable(ts);
  375. /* test that the reset really happened */
  376. tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
  377. if (temp_high != temp_high_orig) {
  378. dev_warn(dev, "selftest failed after reset: %d != %d\n",
  379. temp_high, temp_high_orig);
  380. result = 0;
  381. }
  382. mutex_unlock(&ts->mutex);
  383. out:
  384. return sprintf(buf, "%u\n", result);
  385. }
  386. static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
  387. static void tsc2005_esd_timer(unsigned long data)
  388. {
  389. struct tsc2005 *ts = (struct tsc2005 *)data;
  390. schedule_work(&ts->esd_work);
  391. }
  392. static void tsc2005_esd_work(struct work_struct *work)
  393. {
  394. struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
  395. u16 r;
  396. mutex_lock(&ts->mutex);
  397. if (ts->disable_depth)
  398. goto out;
  399. /*
  400. * If we cannot read our known value from configuration register 0 then
  401. * reset the controller as if from power-up and start scanning again.
  402. */
  403. tsc2005_read(ts, TSC2005_REG_CFR0, &r);
  404. if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) {
  405. dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
  406. ts->set_reset(0);
  407. tsc2005_update_pen_state(ts, 0, 0, 0);
  408. usleep_range(100, 500); /* only 10us required */
  409. ts->set_reset(1);
  410. tsc2005_start_scan(ts);
  411. }
  412. /* re-arm the watchdog */
  413. mod_timer(&ts->esd_timer,
  414. round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
  415. out:
  416. mutex_unlock(&ts->mutex);
  417. }
  418. static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
  419. {
  420. tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, 0);
  421. tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, 0);
  422. tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, 0);
  423. tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, 1);
  424. spi_message_init(&ts->spi_read_msg);
  425. spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
  426. spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
  427. spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
  428. spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
  429. }
  430. static struct attribute *tsc2005_attrs[] = {
  431. &dev_attr_disable.attr,
  432. &dev_attr_selftest.attr,
  433. NULL
  434. };
  435. static struct attribute_group tsc2005_attr_group = {
  436. .attrs = tsc2005_attrs,
  437. };
  438. static int __devinit tsc2005_setup(struct tsc2005 *ts,
  439. struct tsc2005_platform_data *pdata)
  440. {
  441. int r;
  442. int fudge_x;
  443. int fudge_y;
  444. int fudge_p;
  445. int p_max;
  446. int x_max;
  447. int y_max;
  448. mutex_init(&ts->mutex);
  449. tsc2005_setup_spi_xfer(ts);
  450. init_timer(&ts->penup_timer);
  451. setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
  452. INIT_WORK(&ts->penup_work, tsc2005_penup_work);
  453. fudge_x = pdata->ts_x_fudge ? : 4;
  454. fudge_y = pdata->ts_y_fudge ? : 8;
  455. fudge_p = pdata->ts_pressure_fudge ? : 2;
  456. x_max = pdata->ts_x_max ? : MAX_12BIT;
  457. y_max = pdata->ts_y_max ? : MAX_12BIT;
  458. p_max = pdata->ts_pressure_max ? : MAX_12BIT;
  459. ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
  460. ts->esd_timeout = pdata->esd_timeout_ms;
  461. ts->set_reset = pdata->set_reset;
  462. ts->idev = input_allocate_device();
  463. if (ts->idev == NULL)
  464. return -ENOMEM;
  465. ts->idev->name = "TSC2005 touchscreen";
  466. snprintf(ts->phys, sizeof(ts->phys), "%s/input-ts",
  467. dev_name(&ts->spi->dev));
  468. ts->idev->phys = ts->phys;
  469. ts->idev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
  470. ts->idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
  471. ts->idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  472. input_set_abs_params(ts->idev, ABS_X, 0, x_max, fudge_x, 0);
  473. input_set_abs_params(ts->idev, ABS_Y, 0, y_max, fudge_y, 0);
  474. input_set_abs_params(ts->idev, ABS_PRESSURE, 0, p_max, fudge_p, 0);
  475. r = request_threaded_irq(ts->spi->irq, tsc2005_irq_handler,
  476. tsc2005_irq_thread, IRQF_TRIGGER_RISING,
  477. "tsc2005", ts);
  478. if (r) {
  479. dev_err(&ts->spi->dev, "request_threaded_irq(): %d\n", r);
  480. goto err1;
  481. }
  482. set_irq_wake(ts->spi->irq, 1);
  483. r = input_register_device(ts->idev);
  484. if (r) {
  485. dev_err(&ts->spi->dev, "input_register_device(): %d\n", r);
  486. goto err2;
  487. }
  488. r = sysfs_create_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
  489. if (r)
  490. dev_warn(&ts->spi->dev, "sysfs entry creation failed: %d\n", r);
  491. tsc2005_start_scan(ts);
  492. if (!ts->esd_timeout || !ts->set_reset)
  493. goto done;
  494. /* start the optional ESD watchdog */
  495. setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts);
  496. INIT_WORK(&ts->esd_work, tsc2005_esd_work);
  497. mod_timer(&ts->esd_timer,
  498. round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
  499. done:
  500. return 0;
  501. err2:
  502. free_irq(ts->spi->irq, ts);
  503. err1:
  504. input_free_device(ts->idev);
  505. return r;
  506. }
  507. static int __devinit tsc2005_probe(struct spi_device *spi)
  508. {
  509. struct tsc2005_platform_data *pdata = spi->dev.platform_data;
  510. struct tsc2005 *ts;
  511. int r;
  512. if (spi->irq < 0) {
  513. dev_dbg(&spi->dev, "no irq\n");
  514. return -ENODEV;
  515. }
  516. if (!pdata) {
  517. dev_dbg(&spi->dev, "no platform data\n");
  518. return -ENODEV;
  519. }
  520. ts = kzalloc(sizeof(*ts), GFP_KERNEL);
  521. if (ts == NULL)
  522. return -ENOMEM;
  523. spi_set_drvdata(spi, ts);
  524. ts->spi = spi;
  525. spi->dev.power.power_state = PMSG_ON;
  526. spi->mode = SPI_MODE_0;
  527. spi->bits_per_word = 8;
  528. if (!spi->max_speed_hz)
  529. spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
  530. spi_setup(spi);
  531. r = tsc2005_setup(ts, pdata);
  532. if (r)
  533. kfree(ts);
  534. return r;
  535. }
  536. static int __devexit tsc2005_remove(struct spi_device *spi)
  537. {
  538. struct tsc2005 *ts = spi_get_drvdata(spi);
  539. mutex_lock(&ts->mutex);
  540. tsc2005_disable(ts);
  541. mutex_unlock(&ts->mutex);
  542. if (ts->esd_timeout)
  543. del_timer_sync(&ts->esd_timer);
  544. del_timer_sync(&ts->penup_timer);
  545. flush_work(&ts->esd_work);
  546. flush_work(&ts->penup_work);
  547. sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
  548. free_irq(ts->spi->irq, ts);
  549. input_unregister_device(ts->idev);
  550. kfree(ts);
  551. return 0;
  552. }
  553. #ifdef CONFIG_PM_SLEEP
  554. static int tsc2005_suspend(struct device *dev)
  555. {
  556. struct spi_device *spi = to_spi_device(dev);
  557. struct tsc2005 *ts = spi_get_drvdata(spi);
  558. mutex_lock(&ts->mutex);
  559. tsc2005_disable(ts);
  560. mutex_unlock(&ts->mutex);
  561. return 0;
  562. }
  563. static int tsc2005_resume(struct device *dev)
  564. {
  565. struct spi_device *spi = to_spi_device(dev);
  566. struct tsc2005 *ts = spi_get_drvdata(spi);
  567. mutex_lock(&ts->mutex);
  568. tsc2005_enable(ts);
  569. mutex_unlock(&ts->mutex);
  570. return 0;
  571. }
  572. #endif
  573. static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
  574. static struct spi_driver tsc2005_driver = {
  575. .driver = {
  576. .name = "tsc2005",
  577. .owner = THIS_MODULE,
  578. .pm = &tsc2005_pm_ops,
  579. },
  580. .probe = tsc2005_probe,
  581. .remove = __devexit_p(tsc2005_remove),
  582. };
  583. static int __init tsc2005_init(void)
  584. {
  585. return spi_register_driver(&tsc2005_driver);
  586. }
  587. module_init(tsc2005_init);
  588. static void __exit tsc2005_exit(void)
  589. {
  590. spi_unregister_driver(&tsc2005_driver);
  591. }
  592. module_exit(tsc2005_exit);
  593. MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
  594. MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
  595. MODULE_LICENSE("GPL");