tsc2005.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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(jiffies +
  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 mode_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. mode_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. mutex_lock(&ts->mutex);
  399. if (time_is_after_jiffies(ts->last_valid_interrupt +
  400. msecs_to_jiffies(ts->esd_timeout)))
  401. goto out;
  402. /* We should be able to read register without disabling interrupts. */
  403. error = tsc2005_read(ts, TSC2005_REG_CFR0, &r);
  404. if (!error &&
  405. !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
  406. goto out;
  407. }
  408. /*
  409. * If we could not read our known value from configuration register 0
  410. * then we should reset the controller as if from power-up and start
  411. * scanning again.
  412. */
  413. dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
  414. disable_irq(ts->spi->irq);
  415. del_timer_sync(&ts->penup_timer);
  416. tsc2005_update_pen_state(ts, 0, 0, 0);
  417. ts->set_reset(false);
  418. usleep_range(100, 500); /* only 10us required */
  419. ts->set_reset(true);
  420. enable_irq(ts->spi->irq);
  421. tsc2005_start_scan(ts);
  422. out:
  423. /* re-arm the watchdog */
  424. schedule_delayed_work(&ts->esd_work,
  425. round_jiffies(jiffies +
  426. msecs_to_jiffies(ts->esd_timeout)));
  427. mutex_unlock(&ts->mutex);
  428. }
  429. static int tsc2005_open(struct input_dev *input)
  430. {
  431. struct tsc2005 *ts = input_get_drvdata(input);
  432. mutex_lock(&ts->mutex);
  433. if (!ts->suspended)
  434. __tsc2005_enable(ts);
  435. ts->opened = true;
  436. mutex_unlock(&ts->mutex);
  437. return 0;
  438. }
  439. static void tsc2005_close(struct input_dev *input)
  440. {
  441. struct tsc2005 *ts = input_get_drvdata(input);
  442. mutex_lock(&ts->mutex);
  443. if (!ts->suspended)
  444. __tsc2005_disable(ts);
  445. ts->opened = false;
  446. mutex_unlock(&ts->mutex);
  447. }
  448. static void __devinit tsc2005_setup_spi_xfer(struct tsc2005 *ts)
  449. {
  450. tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false);
  451. tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false);
  452. tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false);
  453. tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true);
  454. spi_message_init(&ts->spi_read_msg);
  455. spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg);
  456. spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg);
  457. spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg);
  458. spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg);
  459. }
  460. static int __devinit tsc2005_probe(struct spi_device *spi)
  461. {
  462. const struct tsc2005_platform_data *pdata = spi->dev.platform_data;
  463. struct tsc2005 *ts;
  464. struct input_dev *input_dev;
  465. unsigned int max_x, max_y, max_p;
  466. unsigned int fudge_x, fudge_y, fudge_p;
  467. int error;
  468. if (!pdata) {
  469. dev_dbg(&spi->dev, "no platform data\n");
  470. return -ENODEV;
  471. }
  472. fudge_x = pdata->ts_x_fudge ? : 4;
  473. fudge_y = pdata->ts_y_fudge ? : 8;
  474. fudge_p = pdata->ts_pressure_fudge ? : 2;
  475. max_x = pdata->ts_x_max ? : MAX_12BIT;
  476. max_y = pdata->ts_y_max ? : MAX_12BIT;
  477. max_p = pdata->ts_pressure_max ? : MAX_12BIT;
  478. if (spi->irq <= 0) {
  479. dev_dbg(&spi->dev, "no irq\n");
  480. return -ENODEV;
  481. }
  482. spi->mode = SPI_MODE_0;
  483. spi->bits_per_word = 8;
  484. if (!spi->max_speed_hz)
  485. spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ;
  486. error = spi_setup(spi);
  487. if (error)
  488. return error;
  489. ts = kzalloc(sizeof(*ts), GFP_KERNEL);
  490. input_dev = input_allocate_device();
  491. if (!ts || !input_dev) {
  492. error = -ENOMEM;
  493. goto err_free_mem;
  494. }
  495. ts->spi = spi;
  496. ts->idev = input_dev;
  497. ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280;
  498. ts->esd_timeout = pdata->esd_timeout_ms;
  499. ts->set_reset = pdata->set_reset;
  500. mutex_init(&ts->mutex);
  501. spin_lock_init(&ts->lock);
  502. setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
  503. INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
  504. tsc2005_setup_spi_xfer(ts);
  505. snprintf(ts->phys, sizeof(ts->phys),
  506. "%s/input-ts", dev_name(&spi->dev));
  507. input_dev->name = "TSC2005 touchscreen";
  508. input_dev->phys = ts->phys;
  509. input_dev->id.bustype = BUS_SPI;
  510. input_dev->dev.parent = &spi->dev;
  511. input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);
  512. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  513. input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);
  514. input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
  515. input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
  516. input_dev->open = tsc2005_open;
  517. input_dev->close = tsc2005_close;
  518. input_set_drvdata(input_dev, ts);
  519. /* Ensure the touchscreen is off */
  520. tsc2005_stop_scan(ts);
  521. error = request_threaded_irq(spi->irq, NULL, tsc2005_irq_thread,
  522. IRQF_TRIGGER_RISING, "tsc2005", ts);
  523. if (error) {
  524. dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);
  525. goto err_free_mem;
  526. }
  527. spi_set_drvdata(spi, ts);
  528. error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);
  529. if (error) {
  530. dev_err(&spi->dev,
  531. "Failed to create sysfs attributes, err: %d\n", error);
  532. goto err_clear_drvdata;
  533. }
  534. error = input_register_device(ts->idev);
  535. if (error) {
  536. dev_err(&spi->dev,
  537. "Failed to register input device, err: %d\n", error);
  538. goto err_remove_sysfs;
  539. }
  540. set_irq_wake(spi->irq, 1);
  541. return 0;
  542. err_remove_sysfs:
  543. sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);
  544. err_clear_drvdata:
  545. spi_set_drvdata(spi, NULL);
  546. free_irq(spi->irq, ts);
  547. err_free_mem:
  548. input_free_device(input_dev);
  549. kfree(ts);
  550. return error;
  551. }
  552. static int __devexit tsc2005_remove(struct spi_device *spi)
  553. {
  554. struct tsc2005 *ts = spi_get_drvdata(spi);
  555. sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
  556. free_irq(ts->spi->irq, ts);
  557. input_unregister_device(ts->idev);
  558. kfree(ts);
  559. spi_set_drvdata(spi, NULL);
  560. return 0;
  561. }
  562. #ifdef CONFIG_PM_SLEEP
  563. static int tsc2005_suspend(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. if (!ts->suspended && ts->opened)
  569. __tsc2005_disable(ts);
  570. ts->suspended = true;
  571. mutex_unlock(&ts->mutex);
  572. return 0;
  573. }
  574. static int tsc2005_resume(struct device *dev)
  575. {
  576. struct spi_device *spi = to_spi_device(dev);
  577. struct tsc2005 *ts = spi_get_drvdata(spi);
  578. mutex_lock(&ts->mutex);
  579. if (ts->suspended && ts->opened)
  580. __tsc2005_enable(ts);
  581. ts->suspended = false;
  582. mutex_unlock(&ts->mutex);
  583. return 0;
  584. }
  585. #endif
  586. static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);
  587. static struct spi_driver tsc2005_driver = {
  588. .driver = {
  589. .name = "tsc2005",
  590. .owner = THIS_MODULE,
  591. .pm = &tsc2005_pm_ops,
  592. },
  593. .probe = tsc2005_probe,
  594. .remove = __devexit_p(tsc2005_remove),
  595. };
  596. static int __init tsc2005_init(void)
  597. {
  598. return spi_register_driver(&tsc2005_driver);
  599. }
  600. module_init(tsc2005_init);
  601. static void __exit tsc2005_exit(void)
  602. {
  603. spi_unregister_driver(&tsc2005_driver);
  604. }
  605. module_exit(tsc2005_exit);
  606. MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
  607. MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");
  608. MODULE_LICENSE("GPL");