tsc2005.c 18 KB

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