tsc2005.c 18 KB

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