wm97xx-core.c 21 KB

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