wm97xx-core.c 20 KB

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