wm97xx-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * wm97xx-core.c -- Touch screen driver core for Wolfson WM9705, WM9712
  3. * and WM9713 AC97 Codecs.
  4. *
  5. * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. * Parts Copyright : Ian Molton <spyro@f2s.com>
  8. * Andrew Zabolotny <zap@homelink.ru>
  9. * Russell King <rmk@arm.linux.org.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * Notes:
  17. *
  18. * Features:
  19. * - supports WM9705, WM9712, WM9713
  20. * - polling mode
  21. * - continuous mode (arch-dependent)
  22. * - adjustable rpu/dpp settings
  23. * - adjustable pressure current
  24. * - adjustable sample settle delay
  25. * - 4 and 5 wire touchscreens (5 wire is WM9712 only)
  26. * - pen down detection
  27. * - battery monitor
  28. * - sample AUX adcs
  29. * - power management
  30. * - codec GPIO
  31. * - codec event notification
  32. * Todo
  33. * - Support for async sampling control for noisy LCDs.
  34. *
  35. */
  36. #include <linux/module.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/kernel.h>
  39. #include <linux/init.h>
  40. #include <linux/delay.h>
  41. #include <linux/string.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/pm.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/bitops.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/wm97xx.h>
  48. #include <linux/uaccess.h>
  49. #include <linux/io.h>
  50. #include <linux/slab.h>
  51. #define TS_NAME "wm97xx"
  52. #define WM_CORE_VERSION "1.00"
  53. #define DEFAULT_PRESSURE 0xb0c0
  54. /*
  55. * Touchscreen absolute values
  56. *
  57. * These parameters are used to help the input layer discard out of
  58. * range readings and reduce jitter etc.
  59. *
  60. * o min, max:- indicate the min and max values your touch screen returns
  61. * o fuzz:- use a higher number to reduce jitter
  62. *
  63. * The default values correspond to Mainstone II in QVGA mode
  64. *
  65. * Please read
  66. * Documentation/input/input-programming.txt for more details.
  67. */
  68. static int abs_x[3] = {150, 4000, 5};
  69. module_param_array(abs_x, int, NULL, 0);
  70. MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
  71. static int abs_y[3] = {200, 4000, 40};
  72. module_param_array(abs_y, int, NULL, 0);
  73. MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
  74. static int abs_p[3] = {0, 150, 4};
  75. module_param_array(abs_p, int, NULL, 0);
  76. MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
  77. /*
  78. * wm97xx IO access, all IO locking done by AC97 layer
  79. */
  80. int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
  81. {
  82. if (wm->ac97)
  83. return wm->ac97->bus->ops->read(wm->ac97, reg);
  84. else
  85. return -1;
  86. }
  87. EXPORT_SYMBOL_GPL(wm97xx_reg_read);
  88. void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
  89. {
  90. /* cache digitiser registers */
  91. if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
  92. wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
  93. /* cache gpio regs */
  94. if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
  95. wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
  96. /* wm9713 irq reg */
  97. if (reg == 0x5a)
  98. wm->misc = val;
  99. if (wm->ac97)
  100. wm->ac97->bus->ops->write(wm->ac97, reg, val);
  101. }
  102. EXPORT_SYMBOL_GPL(wm97xx_reg_write);
  103. /**
  104. * wm97xx_read_aux_adc - Read the aux adc.
  105. * @wm: wm97xx device.
  106. * @adcsel: codec ADC to be read
  107. *
  108. * Reads the selected AUX ADC.
  109. */
  110. int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
  111. {
  112. int power_adc = 0, auxval;
  113. u16 power = 0;
  114. int rc = 0;
  115. int timeout = 0;
  116. /* get codec */
  117. mutex_lock(&wm->codec_mutex);
  118. /* When the touchscreen is not in use, we may have to power up
  119. * the AUX ADC before we can use sample the AUX inputs->
  120. */
  121. if (wm->id == WM9713_ID2 &&
  122. (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
  123. power_adc = 1;
  124. wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
  125. }
  126. /* Prepare the codec for AUX reading */
  127. wm->codec->aux_prepare(wm);
  128. /* Turn polling mode on to read AUX ADC */
  129. wm->pen_probably_down = 1;
  130. while (rc != RC_VALID && timeout++ < 5)
  131. rc = wm->codec->poll_sample(wm, adcsel, &auxval);
  132. if (power_adc)
  133. wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
  134. wm->codec->dig_restore(wm);
  135. wm->pen_probably_down = 0;
  136. if (timeout >= 5) {
  137. dev_err(wm->dev,
  138. "timeout reading auxadc %d, disabling digitiser\n",
  139. adcsel);
  140. wm->codec->dig_enable(wm, false);
  141. }
  142. mutex_unlock(&wm->codec_mutex);
  143. return (rc == RC_VALID ? auxval & 0xfff : -EBUSY);
  144. }
  145. EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
  146. /**
  147. * wm97xx_get_gpio - Get the status of a codec GPIO.
  148. * @wm: wm97xx device.
  149. * @gpio: gpio
  150. *
  151. * Get the status of a codec GPIO pin
  152. */
  153. enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
  154. {
  155. u16 status;
  156. enum wm97xx_gpio_status ret;
  157. mutex_lock(&wm->codec_mutex);
  158. status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  159. if (status & gpio)
  160. ret = WM97XX_GPIO_HIGH;
  161. else
  162. ret = WM97XX_GPIO_LOW;
  163. mutex_unlock(&wm->codec_mutex);
  164. return ret;
  165. }
  166. EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
  167. /**
  168. * wm97xx_set_gpio - Set the status of a codec GPIO.
  169. * @wm: wm97xx device.
  170. * @gpio: gpio
  171. *
  172. *
  173. * Set the status of a codec GPIO pin
  174. */
  175. void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
  176. enum wm97xx_gpio_status status)
  177. {
  178. u16 reg;
  179. mutex_lock(&wm->codec_mutex);
  180. reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  181. if (status == WM97XX_GPIO_HIGH)
  182. reg |= gpio;
  183. else
  184. reg &= ~gpio;
  185. if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
  186. wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
  187. else
  188. wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
  189. mutex_unlock(&wm->codec_mutex);
  190. }
  191. EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
  192. /*
  193. * Codec GPIO pin configuration, this sets pin direction, polarity,
  194. * stickyness and wake up.
  195. */
  196. void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
  197. enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
  198. enum wm97xx_gpio_wake wake)
  199. {
  200. u16 reg;
  201. mutex_lock(&wm->codec_mutex);
  202. reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
  203. if (pol == WM97XX_GPIO_POL_HIGH)
  204. reg |= gpio;
  205. else
  206. reg &= ~gpio;
  207. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
  208. reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
  209. if (sticky == WM97XX_GPIO_STICKY)
  210. reg |= gpio;
  211. else
  212. reg &= ~gpio;
  213. wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
  214. reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
  215. if (wake == WM97XX_GPIO_WAKE)
  216. reg |= gpio;
  217. else
  218. reg &= ~gpio;
  219. wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
  220. reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
  221. if (dir == WM97XX_GPIO_IN)
  222. reg |= gpio;
  223. else
  224. reg &= ~gpio;
  225. wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
  226. mutex_unlock(&wm->codec_mutex);
  227. }
  228. EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
  229. /*
  230. * Configure the WM97XX_PRP value to use while system is suspended.
  231. * If a value other than 0 is set then WM97xx pen detection will be
  232. * left enabled in the configured mode while the system is in suspend,
  233. * the device has users and suspend has not been disabled via the
  234. * wakeup sysfs entries.
  235. *
  236. * @wm: WM97xx device to configure
  237. * @mode: WM97XX_PRP value to configure while suspended
  238. */
  239. void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
  240. {
  241. wm->suspend_mode = mode;
  242. device_init_wakeup(&wm->input_dev->dev, mode != 0);
  243. }
  244. EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
  245. /*
  246. * Handle a pen down interrupt.
  247. */
  248. static void wm97xx_pen_irq_worker(struct work_struct *work)
  249. {
  250. struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
  251. int pen_was_down = wm->pen_is_down;
  252. /* do we need to enable the touch panel reader */
  253. if (wm->id == WM9705_ID2) {
  254. if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
  255. WM97XX_PEN_DOWN)
  256. wm->pen_is_down = 1;
  257. else
  258. wm->pen_is_down = 0;
  259. } else {
  260. u16 status, pol;
  261. mutex_lock(&wm->codec_mutex);
  262. status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  263. pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
  264. if (WM97XX_GPIO_13 & pol & status) {
  265. wm->pen_is_down = 1;
  266. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
  267. ~WM97XX_GPIO_13);
  268. } else {
  269. wm->pen_is_down = 0;
  270. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
  271. WM97XX_GPIO_13);
  272. }
  273. if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
  274. wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
  275. ~WM97XX_GPIO_13) << 1);
  276. else
  277. wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
  278. ~WM97XX_GPIO_13);
  279. mutex_unlock(&wm->codec_mutex);
  280. }
  281. /* If the system is not using continuous mode or it provides a
  282. * pen down operation then we need to schedule polls while the
  283. * pen is down. Otherwise the machine driver is responsible
  284. * for scheduling reads.
  285. */
  286. if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
  287. if (wm->pen_is_down && !pen_was_down) {
  288. /* Data is not available immediately on pen down */
  289. queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
  290. }
  291. /* Let ts_reader report the pen up for debounce. */
  292. if (!wm->pen_is_down && pen_was_down)
  293. wm->pen_is_down = 1;
  294. }
  295. if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
  296. wm->mach_ops->acc_pen_up(wm);
  297. wm->mach_ops->irq_enable(wm, 1);
  298. }
  299. /*
  300. * Codec PENDOWN irq handler
  301. *
  302. * We have to disable the codec interrupt in the handler because it
  303. * can take up to 1ms to clear the interrupt source. We schedule a task
  304. * in a work queue to do the actual interaction with the chip. The
  305. * interrupt is then enabled again in the slow handler when the source
  306. * has been cleared.
  307. */
  308. static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
  309. {
  310. struct wm97xx *wm = dev_id;
  311. if (!work_pending(&wm->pen_event_work)) {
  312. wm->mach_ops->irq_enable(wm, 0);
  313. queue_work(wm->ts_workq, &wm->pen_event_work);
  314. }
  315. return IRQ_HANDLED;
  316. }
  317. /*
  318. * initialise pen IRQ handler and workqueue
  319. */
  320. static int wm97xx_init_pen_irq(struct wm97xx *wm)
  321. {
  322. u16 reg;
  323. /* If an interrupt is supplied an IRQ enable operation must also be
  324. * provided. */
  325. BUG_ON(!wm->mach_ops->irq_enable);
  326. if (request_irq(wm->pen_irq, wm97xx_pen_interrupt, IRQF_SHARED,
  327. "wm97xx-pen", wm)) {
  328. dev_err(wm->dev,
  329. "Failed to register pen down interrupt, polling");
  330. wm->pen_irq = 0;
  331. return -EINVAL;
  332. }
  333. /* Configure GPIO as interrupt source on WM971x */
  334. if (wm->id != WM9705_ID2) {
  335. BUG_ON(!wm->mach_ops->irq_gpio);
  336. reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
  337. wm97xx_reg_write(wm, AC97_MISC_AFE,
  338. reg & ~(wm->mach_ops->irq_gpio));
  339. reg = wm97xx_reg_read(wm, 0x5a);
  340. wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
  341. }
  342. return 0;
  343. }
  344. static int wm97xx_read_samples(struct wm97xx *wm)
  345. {
  346. struct wm97xx_data data;
  347. int rc;
  348. mutex_lock(&wm->codec_mutex);
  349. if (wm->mach_ops && wm->mach_ops->acc_enabled)
  350. rc = wm->mach_ops->acc_pen_down(wm);
  351. else
  352. rc = wm->codec->poll_touch(wm, &data);
  353. if (rc & RC_PENUP) {
  354. if (wm->pen_is_down) {
  355. wm->pen_is_down = 0;
  356. dev_dbg(wm->dev, "pen up\n");
  357. input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
  358. input_report_key(wm->input_dev, BTN_TOUCH, 0);
  359. input_sync(wm->input_dev);
  360. } else if (!(rc & RC_AGAIN)) {
  361. /* We need high frequency updates only while
  362. * pen is down, the user never will be able to
  363. * touch screen faster than a few times per
  364. * second... On the other hand, when the user
  365. * is actively working with the touchscreen we
  366. * don't want to lose the quick response. So we
  367. * will slowly increase sleep time after the
  368. * pen is up and quicky restore it to ~one task
  369. * switch when pen is down again.
  370. */
  371. if (wm->ts_reader_interval < HZ / 10)
  372. wm->ts_reader_interval++;
  373. }
  374. } else if (rc & RC_VALID) {
  375. dev_dbg(wm->dev,
  376. "pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
  377. data.x >> 12, data.x & 0xfff, data.y >> 12,
  378. data.y & 0xfff, data.p >> 12, data.p & 0xfff);
  379. if (abs_x[0] > (data.x & 0xfff) ||
  380. abs_x[1] < (data.x & 0xfff) ||
  381. abs_y[0] > (data.y & 0xfff) ||
  382. abs_y[1] < (data.y & 0xfff)) {
  383. dev_dbg(wm->dev, "Measurement out of range, dropping it\n");
  384. rc = RC_AGAIN;
  385. goto out;
  386. }
  387. input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
  388. input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
  389. input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
  390. input_report_key(wm->input_dev, BTN_TOUCH, 1);
  391. input_sync(wm->input_dev);
  392. wm->pen_is_down = 1;
  393. wm->ts_reader_interval = wm->ts_reader_min_interval;
  394. } else if (rc & RC_PENDOWN) {
  395. dev_dbg(wm->dev, "pen down\n");
  396. wm->pen_is_down = 1;
  397. wm->ts_reader_interval = wm->ts_reader_min_interval;
  398. }
  399. out:
  400. mutex_unlock(&wm->codec_mutex);
  401. return rc;
  402. }
  403. /*
  404. * The touchscreen sample reader.
  405. */
  406. static void wm97xx_ts_reader(struct work_struct *work)
  407. {
  408. int rc;
  409. struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
  410. BUG_ON(!wm->codec);
  411. do {
  412. rc = wm97xx_read_samples(wm);
  413. } while (rc & RC_AGAIN);
  414. if (wm->pen_is_down || !wm->pen_irq)
  415. queue_delayed_work(wm->ts_workq, &wm->ts_reader,
  416. wm->ts_reader_interval);
  417. }
  418. /**
  419. * wm97xx_ts_input_open - Open the touch screen input device.
  420. * @idev: Input device to be opened.
  421. *
  422. * Called by the input sub system to open a wm97xx touchscreen device.
  423. * Starts the touchscreen thread and touch digitiser.
  424. */
  425. static int wm97xx_ts_input_open(struct input_dev *idev)
  426. {
  427. struct wm97xx *wm = input_get_drvdata(idev);
  428. wm->ts_workq = create_singlethread_workqueue("kwm97xx");
  429. if (wm->ts_workq == NULL) {
  430. dev_err(wm->dev,
  431. "Failed to create workqueue\n");
  432. return -EINVAL;
  433. }
  434. /* start digitiser */
  435. if (wm->mach_ops && wm->mach_ops->acc_enabled)
  436. wm->codec->acc_enable(wm, 1);
  437. wm->codec->dig_enable(wm, 1);
  438. INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
  439. INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
  440. wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
  441. if (wm->ts_reader_min_interval < 1)
  442. wm->ts_reader_min_interval = 1;
  443. wm->ts_reader_interval = wm->ts_reader_min_interval;
  444. wm->pen_is_down = 0;
  445. if (wm->pen_irq)
  446. wm97xx_init_pen_irq(wm);
  447. else
  448. dev_err(wm->dev, "No IRQ specified\n");
  449. /* If we either don't have an interrupt for pen down events or
  450. * failed to acquire it then we need to poll.
  451. */
  452. if (wm->pen_irq == 0)
  453. queue_delayed_work(wm->ts_workq, &wm->ts_reader,
  454. wm->ts_reader_interval);
  455. return 0;
  456. }
  457. /**
  458. * wm97xx_ts_input_close - Close the touch screen input device.
  459. * @idev: Input device to be closed.
  460. *
  461. * Called by the input sub system to close a wm97xx touchscreen
  462. * device. Kills the touchscreen thread and stops the touch
  463. * digitiser.
  464. */
  465. static void wm97xx_ts_input_close(struct input_dev *idev)
  466. {
  467. struct wm97xx *wm = input_get_drvdata(idev);
  468. u16 reg;
  469. if (wm->pen_irq) {
  470. /* Return the interrupt to GPIO usage (disabling it) */
  471. if (wm->id != WM9705_ID2) {
  472. BUG_ON(!wm->mach_ops->irq_gpio);
  473. reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
  474. wm97xx_reg_write(wm, AC97_MISC_AFE,
  475. reg | wm->mach_ops->irq_gpio);
  476. }
  477. free_irq(wm->pen_irq, wm);
  478. }
  479. wm->pen_is_down = 0;
  480. /* Balance out interrupt disables/enables */
  481. if (cancel_work_sync(&wm->pen_event_work))
  482. wm->mach_ops->irq_enable(wm, 1);
  483. /* ts_reader rearms itself so we need to explicitly stop it
  484. * before we destroy the workqueue.
  485. */
  486. cancel_delayed_work_sync(&wm->ts_reader);
  487. destroy_workqueue(wm->ts_workq);
  488. /* stop digitiser */
  489. wm->codec->dig_enable(wm, 0);
  490. if (wm->mach_ops && wm->mach_ops->acc_enabled)
  491. wm->codec->acc_enable(wm, 0);
  492. }
  493. static int wm97xx_probe(struct device *dev)
  494. {
  495. struct wm97xx *wm;
  496. struct wm97xx_pdata *pdata = dev_get_platdata(dev);
  497. int ret = 0, id = 0;
  498. wm = kzalloc(sizeof(struct wm97xx), GFP_KERNEL);
  499. if (!wm)
  500. return -ENOMEM;
  501. mutex_init(&wm->codec_mutex);
  502. wm->dev = dev;
  503. dev_set_drvdata(dev, wm);
  504. wm->ac97 = to_ac97_t(dev);
  505. /* check that we have a supported codec */
  506. id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
  507. if (id != WM97XX_ID1) {
  508. dev_err(dev, "Device with vendor %04x is not a wm97xx\n", id);
  509. ret = -ENODEV;
  510. goto alloc_err;
  511. }
  512. wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
  513. wm->variant = WM97xx_GENERIC;
  514. dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
  515. switch (wm->id & 0xff) {
  516. #ifdef CONFIG_TOUCHSCREEN_WM9705
  517. case 0x05:
  518. wm->codec = &wm9705_codec;
  519. break;
  520. #endif
  521. #ifdef CONFIG_TOUCHSCREEN_WM9712
  522. case 0x12:
  523. wm->codec = &wm9712_codec;
  524. break;
  525. #endif
  526. #ifdef CONFIG_TOUCHSCREEN_WM9713
  527. case 0x13:
  528. wm->codec = &wm9713_codec;
  529. break;
  530. #endif
  531. default:
  532. dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
  533. wm->id & 0xff);
  534. ret = -ENODEV;
  535. goto alloc_err;
  536. }
  537. /* set up physical characteristics */
  538. wm->codec->phy_init(wm);
  539. /* load gpio cache */
  540. wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
  541. wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
  542. wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
  543. wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
  544. wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  545. wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
  546. wm->input_dev = input_allocate_device();
  547. if (wm->input_dev == NULL) {
  548. ret = -ENOMEM;
  549. goto alloc_err;
  550. }
  551. /* set up touch configuration */
  552. wm->input_dev->name = "wm97xx touchscreen";
  553. wm->input_dev->phys = "wm97xx";
  554. wm->input_dev->open = wm97xx_ts_input_open;
  555. wm->input_dev->close = wm97xx_ts_input_close;
  556. __set_bit(EV_ABS, wm->input_dev->evbit);
  557. __set_bit(EV_KEY, wm->input_dev->evbit);
  558. __set_bit(BTN_TOUCH, wm->input_dev->keybit);
  559. input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
  560. abs_x[2], 0);
  561. input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
  562. abs_y[2], 0);
  563. input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
  564. abs_p[2], 0);
  565. input_set_drvdata(wm->input_dev, wm);
  566. wm->input_dev->dev.parent = dev;
  567. ret = input_register_device(wm->input_dev);
  568. if (ret < 0)
  569. goto dev_alloc_err;
  570. /* register our battery device */
  571. wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
  572. if (!wm->battery_dev) {
  573. ret = -ENOMEM;
  574. goto batt_err;
  575. }
  576. platform_set_drvdata(wm->battery_dev, wm);
  577. wm->battery_dev->dev.parent = dev;
  578. wm->battery_dev->dev.platform_data = pdata;
  579. ret = platform_device_add(wm->battery_dev);
  580. if (ret < 0)
  581. goto batt_reg_err;
  582. /* register our extended touch device (for machine specific
  583. * extensions) */
  584. wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
  585. if (!wm->touch_dev) {
  586. ret = -ENOMEM;
  587. goto touch_err;
  588. }
  589. platform_set_drvdata(wm->touch_dev, wm);
  590. wm->touch_dev->dev.parent = dev;
  591. wm->touch_dev->dev.platform_data = pdata;
  592. ret = platform_device_add(wm->touch_dev);
  593. if (ret < 0)
  594. goto touch_reg_err;
  595. return ret;
  596. touch_reg_err:
  597. platform_device_put(wm->touch_dev);
  598. touch_err:
  599. platform_device_del(wm->battery_dev);
  600. batt_reg_err:
  601. platform_device_put(wm->battery_dev);
  602. batt_err:
  603. input_unregister_device(wm->input_dev);
  604. wm->input_dev = NULL;
  605. dev_alloc_err:
  606. input_free_device(wm->input_dev);
  607. alloc_err:
  608. kfree(wm);
  609. return ret;
  610. }
  611. static int wm97xx_remove(struct device *dev)
  612. {
  613. struct wm97xx *wm = dev_get_drvdata(dev);
  614. platform_device_unregister(wm->battery_dev);
  615. platform_device_unregister(wm->touch_dev);
  616. input_unregister_device(wm->input_dev);
  617. kfree(wm);
  618. return 0;
  619. }
  620. static int __maybe_unused wm97xx_suspend(struct device *dev)
  621. {
  622. struct wm97xx *wm = dev_get_drvdata(dev);
  623. u16 reg;
  624. int suspend_mode;
  625. if (device_may_wakeup(&wm->input_dev->dev))
  626. suspend_mode = wm->suspend_mode;
  627. else
  628. suspend_mode = 0;
  629. if (wm->input_dev->users)
  630. cancel_delayed_work_sync(&wm->ts_reader);
  631. /* Power down the digitiser (bypassing the cache for resume) */
  632. reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
  633. reg &= ~WM97XX_PRP_DET_DIG;
  634. if (wm->input_dev->users)
  635. reg |= suspend_mode;
  636. wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
  637. /* WM9713 has an additional power bit - turn it off if there
  638. * are no users or if suspend mode is zero. */
  639. if (wm->id == WM9713_ID2 &&
  640. (!wm->input_dev->users || !suspend_mode)) {
  641. reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
  642. wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
  643. }
  644. return 0;
  645. }
  646. static int __maybe_unused wm97xx_resume(struct device *dev)
  647. {
  648. struct wm97xx *wm = dev_get_drvdata(dev);
  649. /* restore digitiser and gpios */
  650. if (wm->id == WM9713_ID2) {
  651. wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
  652. wm97xx_reg_write(wm, 0x5a, wm->misc);
  653. if (wm->input_dev->users) {
  654. u16 reg;
  655. reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
  656. wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
  657. }
  658. }
  659. wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
  660. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
  661. wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
  662. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
  663. wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
  664. wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
  665. wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
  666. wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
  667. if (wm->input_dev->users && !wm->pen_irq) {
  668. wm->ts_reader_interval = wm->ts_reader_min_interval;
  669. queue_delayed_work(wm->ts_workq, &wm->ts_reader,
  670. wm->ts_reader_interval);
  671. }
  672. return 0;
  673. }
  674. static SIMPLE_DEV_PM_OPS(wm97xx_pm_ops, wm97xx_suspend, wm97xx_resume);
  675. /*
  676. * Machine specific operations
  677. */
  678. int wm97xx_register_mach_ops(struct wm97xx *wm,
  679. struct wm97xx_mach_ops *mach_ops)
  680. {
  681. mutex_lock(&wm->codec_mutex);
  682. if (wm->mach_ops) {
  683. mutex_unlock(&wm->codec_mutex);
  684. return -EINVAL;
  685. }
  686. wm->mach_ops = mach_ops;
  687. mutex_unlock(&wm->codec_mutex);
  688. return 0;
  689. }
  690. EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
  691. void wm97xx_unregister_mach_ops(struct wm97xx *wm)
  692. {
  693. mutex_lock(&wm->codec_mutex);
  694. wm->mach_ops = NULL;
  695. mutex_unlock(&wm->codec_mutex);
  696. }
  697. EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
  698. static struct device_driver wm97xx_driver = {
  699. .name = "wm97xx-ts",
  700. .bus = &ac97_bus_type,
  701. .owner = THIS_MODULE,
  702. .probe = wm97xx_probe,
  703. .remove = wm97xx_remove,
  704. .pm = &wm97xx_pm_ops,
  705. };
  706. static int __init wm97xx_init(void)
  707. {
  708. return driver_register(&wm97xx_driver);
  709. }
  710. static void __exit wm97xx_exit(void)
  711. {
  712. driver_unregister(&wm97xx_driver);
  713. }
  714. module_init(wm97xx_init);
  715. module_exit(wm97xx_exit);
  716. /* Module information */
  717. MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
  718. MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
  719. MODULE_LICENSE("GPL");