wm97xx-core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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/mfd/wm97xx.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/wm97xx.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/io.h>
  51. #include <linux/slab.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.rst for more details.
  68. */
  69. static int abs_x[3] = {150, 4000, 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] = {200, 4000, 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. int rc = 0;
  116. int timeout = 0;
  117. /* get codec */
  118. mutex_lock(&wm->codec_mutex);
  119. /* When the touchscreen is not in use, we may have to power up
  120. * the AUX ADC before we can use sample the AUX inputs->
  121. */
  122. if (wm->id == WM9713_ID2 &&
  123. (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
  124. power_adc = 1;
  125. wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
  126. }
  127. /* Prepare the codec for AUX reading */
  128. wm->codec->aux_prepare(wm);
  129. /* Turn polling mode on to read AUX ADC */
  130. wm->pen_probably_down = 1;
  131. while (rc != RC_VALID && timeout++ < 5)
  132. rc = wm->codec->poll_sample(wm, adcsel, &auxval);
  133. if (power_adc)
  134. wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
  135. wm->codec->dig_restore(wm);
  136. wm->pen_probably_down = 0;
  137. if (timeout >= 5) {
  138. dev_err(wm->dev,
  139. "timeout reading auxadc %d, disabling digitiser\n",
  140. adcsel);
  141. wm->codec->dig_enable(wm, false);
  142. }
  143. mutex_unlock(&wm->codec_mutex);
  144. return (rc == RC_VALID ? auxval & 0xfff : -EBUSY);
  145. }
  146. EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
  147. /**
  148. * wm97xx_get_gpio - Get the status of a codec GPIO.
  149. * @wm: wm97xx device.
  150. * @gpio: gpio
  151. *
  152. * Get the status of a codec GPIO pin
  153. */
  154. enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
  155. {
  156. u16 status;
  157. enum wm97xx_gpio_status ret;
  158. mutex_lock(&wm->codec_mutex);
  159. status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  160. if (status & gpio)
  161. ret = WM97XX_GPIO_HIGH;
  162. else
  163. ret = WM97XX_GPIO_LOW;
  164. mutex_unlock(&wm->codec_mutex);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
  168. /**
  169. * wm97xx_set_gpio - Set the status of a codec GPIO.
  170. * @wm: wm97xx device.
  171. * @gpio: gpio
  172. *
  173. *
  174. * Set the status of a codec GPIO pin
  175. */
  176. void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
  177. enum wm97xx_gpio_status status)
  178. {
  179. u16 reg;
  180. mutex_lock(&wm->codec_mutex);
  181. reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  182. if (status == WM97XX_GPIO_HIGH)
  183. reg |= gpio;
  184. else
  185. reg &= ~gpio;
  186. if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
  187. wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
  188. else
  189. wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
  190. mutex_unlock(&wm->codec_mutex);
  191. }
  192. EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
  193. /*
  194. * Codec GPIO pin configuration, this sets pin direction, polarity,
  195. * stickyness and wake up.
  196. */
  197. void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
  198. enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
  199. enum wm97xx_gpio_wake wake)
  200. {
  201. u16 reg;
  202. mutex_lock(&wm->codec_mutex);
  203. reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
  204. if (pol == WM97XX_GPIO_POL_HIGH)
  205. reg |= gpio;
  206. else
  207. reg &= ~gpio;
  208. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
  209. reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
  210. if (sticky == WM97XX_GPIO_STICKY)
  211. reg |= gpio;
  212. else
  213. reg &= ~gpio;
  214. wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
  215. reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
  216. if (wake == WM97XX_GPIO_WAKE)
  217. reg |= gpio;
  218. else
  219. reg &= ~gpio;
  220. wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
  221. reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
  222. if (dir == WM97XX_GPIO_IN)
  223. reg |= gpio;
  224. else
  225. reg &= ~gpio;
  226. wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
  227. mutex_unlock(&wm->codec_mutex);
  228. }
  229. EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
  230. /*
  231. * Configure the WM97XX_PRP value to use while system is suspended.
  232. * If a value other than 0 is set then WM97xx pen detection will be
  233. * left enabled in the configured mode while the system is in suspend,
  234. * the device has users and suspend has not been disabled via the
  235. * wakeup sysfs entries.
  236. *
  237. * @wm: WM97xx device to configure
  238. * @mode: WM97XX_PRP value to configure while suspended
  239. */
  240. void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
  241. {
  242. wm->suspend_mode = mode;
  243. device_init_wakeup(&wm->input_dev->dev, mode != 0);
  244. }
  245. EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
  246. /*
  247. * Handle a pen down interrupt.
  248. */
  249. static void wm97xx_pen_irq_worker(struct work_struct *work)
  250. {
  251. struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
  252. int pen_was_down = wm->pen_is_down;
  253. /* do we need to enable the touch panel reader */
  254. if (wm->id == WM9705_ID2) {
  255. if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
  256. WM97XX_PEN_DOWN)
  257. wm->pen_is_down = 1;
  258. else
  259. wm->pen_is_down = 0;
  260. } else {
  261. u16 status, pol;
  262. mutex_lock(&wm->codec_mutex);
  263. status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  264. pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
  265. if (WM97XX_GPIO_13 & pol & status) {
  266. wm->pen_is_down = 1;
  267. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
  268. ~WM97XX_GPIO_13);
  269. } else {
  270. wm->pen_is_down = 0;
  271. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
  272. WM97XX_GPIO_13);
  273. }
  274. if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
  275. wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
  276. ~WM97XX_GPIO_13) << 1);
  277. else
  278. wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
  279. ~WM97XX_GPIO_13);
  280. mutex_unlock(&wm->codec_mutex);
  281. }
  282. /* If the system is not using continuous mode or it provides a
  283. * pen down operation then we need to schedule polls while the
  284. * pen is down. Otherwise the machine driver is responsible
  285. * for scheduling reads.
  286. */
  287. if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
  288. if (wm->pen_is_down && !pen_was_down) {
  289. /* Data is not available immediately on pen down */
  290. queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
  291. }
  292. /* Let ts_reader report the pen up for debounce. */
  293. if (!wm->pen_is_down && pen_was_down)
  294. wm->pen_is_down = 1;
  295. }
  296. if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
  297. wm->mach_ops->acc_pen_up(wm);
  298. wm->mach_ops->irq_enable(wm, 1);
  299. }
  300. /*
  301. * Codec PENDOWN irq handler
  302. *
  303. * We have to disable the codec interrupt in the handler because it
  304. * can take up to 1ms to clear the interrupt source. We schedule a task
  305. * in a work queue to do the actual interaction with the chip. The
  306. * interrupt is then enabled again in the slow handler when the source
  307. * has been cleared.
  308. */
  309. static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
  310. {
  311. struct wm97xx *wm = dev_id;
  312. if (!work_pending(&wm->pen_event_work)) {
  313. wm->mach_ops->irq_enable(wm, 0);
  314. queue_work(wm->ts_workq, &wm->pen_event_work);
  315. }
  316. return IRQ_HANDLED;
  317. }
  318. /*
  319. * initialise pen IRQ handler and workqueue
  320. */
  321. static int wm97xx_init_pen_irq(struct wm97xx *wm)
  322. {
  323. u16 reg;
  324. /* If an interrupt is supplied an IRQ enable operation must also be
  325. * provided. */
  326. BUG_ON(!wm->mach_ops->irq_enable);
  327. if (request_irq(wm->pen_irq, wm97xx_pen_interrupt, IRQF_SHARED,
  328. "wm97xx-pen", wm)) {
  329. dev_err(wm->dev,
  330. "Failed to register pen down interrupt, polling");
  331. wm->pen_irq = 0;
  332. return -EINVAL;
  333. }
  334. /* Configure GPIO as interrupt source on WM971x */
  335. if (wm->id != WM9705_ID2) {
  336. BUG_ON(!wm->mach_ops->irq_gpio);
  337. reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
  338. wm97xx_reg_write(wm, AC97_MISC_AFE,
  339. reg & ~(wm->mach_ops->irq_gpio));
  340. reg = wm97xx_reg_read(wm, 0x5a);
  341. wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
  342. }
  343. return 0;
  344. }
  345. static int wm97xx_read_samples(struct wm97xx *wm)
  346. {
  347. struct wm97xx_data data;
  348. int rc;
  349. mutex_lock(&wm->codec_mutex);
  350. if (wm->mach_ops && wm->mach_ops->acc_enabled)
  351. rc = wm->mach_ops->acc_pen_down(wm);
  352. else
  353. rc = wm->codec->poll_touch(wm, &data);
  354. if (rc & RC_PENUP) {
  355. if (wm->pen_is_down) {
  356. wm->pen_is_down = 0;
  357. dev_dbg(wm->dev, "pen up\n");
  358. input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
  359. input_report_key(wm->input_dev, BTN_TOUCH, 0);
  360. input_sync(wm->input_dev);
  361. } else if (!(rc & RC_AGAIN)) {
  362. /* We need high frequency updates only while
  363. * pen is down, the user never will be able to
  364. * touch screen faster than a few times per
  365. * second... On the other hand, when the user
  366. * is actively working with the touchscreen we
  367. * don't want to lose the quick response. So we
  368. * will slowly increase sleep time after the
  369. * pen is up and quicky restore it to ~one task
  370. * switch when pen is down again.
  371. */
  372. if (wm->ts_reader_interval < HZ / 10)
  373. wm->ts_reader_interval++;
  374. }
  375. } else if (rc & RC_VALID) {
  376. dev_dbg(wm->dev,
  377. "pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
  378. data.x >> 12, data.x & 0xfff, data.y >> 12,
  379. data.y & 0xfff, data.p >> 12, data.p & 0xfff);
  380. if (abs_x[0] > (data.x & 0xfff) ||
  381. abs_x[1] < (data.x & 0xfff) ||
  382. abs_y[0] > (data.y & 0xfff) ||
  383. abs_y[1] < (data.y & 0xfff)) {
  384. dev_dbg(wm->dev, "Measurement out of range, dropping it\n");
  385. rc = RC_AGAIN;
  386. goto out;
  387. }
  388. input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
  389. input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
  390. input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
  391. input_report_key(wm->input_dev, BTN_TOUCH, 1);
  392. input_sync(wm->input_dev);
  393. wm->pen_is_down = 1;
  394. wm->ts_reader_interval = wm->ts_reader_min_interval;
  395. } else if (rc & RC_PENDOWN) {
  396. dev_dbg(wm->dev, "pen down\n");
  397. wm->pen_is_down = 1;
  398. wm->ts_reader_interval = wm->ts_reader_min_interval;
  399. }
  400. out:
  401. mutex_unlock(&wm->codec_mutex);
  402. return rc;
  403. }
  404. /*
  405. * The touchscreen sample reader.
  406. */
  407. static void wm97xx_ts_reader(struct work_struct *work)
  408. {
  409. int rc;
  410. struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
  411. BUG_ON(!wm->codec);
  412. do {
  413. rc = wm97xx_read_samples(wm);
  414. } while (rc & RC_AGAIN);
  415. if (wm->pen_is_down || !wm->pen_irq)
  416. queue_delayed_work(wm->ts_workq, &wm->ts_reader,
  417. wm->ts_reader_interval);
  418. }
  419. /**
  420. * wm97xx_ts_input_open - Open the touch screen input device.
  421. * @idev: Input device to be opened.
  422. *
  423. * Called by the input sub system to open a wm97xx touchscreen device.
  424. * Starts the touchscreen thread and touch digitiser.
  425. */
  426. static int wm97xx_ts_input_open(struct input_dev *idev)
  427. {
  428. struct wm97xx *wm = input_get_drvdata(idev);
  429. wm->ts_workq = alloc_ordered_workqueue("kwm97xx", 0);
  430. if (wm->ts_workq == NULL) {
  431. dev_err(wm->dev,
  432. "Failed to create workqueue\n");
  433. return -EINVAL;
  434. }
  435. /* start digitiser */
  436. if (wm->mach_ops && wm->mach_ops->acc_enabled)
  437. wm->codec->acc_enable(wm, 1);
  438. wm->codec->dig_enable(wm, 1);
  439. INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
  440. INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
  441. wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
  442. if (wm->ts_reader_min_interval < 1)
  443. wm->ts_reader_min_interval = 1;
  444. wm->ts_reader_interval = wm->ts_reader_min_interval;
  445. wm->pen_is_down = 0;
  446. if (wm->pen_irq)
  447. wm97xx_init_pen_irq(wm);
  448. else
  449. dev_err(wm->dev, "No IRQ specified\n");
  450. /* If we either don't have an interrupt for pen down events or
  451. * failed to acquire it then we need to poll.
  452. */
  453. if (wm->pen_irq == 0)
  454. queue_delayed_work(wm->ts_workq, &wm->ts_reader,
  455. wm->ts_reader_interval);
  456. return 0;
  457. }
  458. /**
  459. * wm97xx_ts_input_close - Close the touch screen input device.
  460. * @idev: Input device to be closed.
  461. *
  462. * Called by the input sub system to close a wm97xx touchscreen
  463. * device. Kills the touchscreen thread and stops the touch
  464. * digitiser.
  465. */
  466. static void wm97xx_ts_input_close(struct input_dev *idev)
  467. {
  468. struct wm97xx *wm = input_get_drvdata(idev);
  469. u16 reg;
  470. if (wm->pen_irq) {
  471. /* Return the interrupt to GPIO usage (disabling it) */
  472. if (wm->id != WM9705_ID2) {
  473. BUG_ON(!wm->mach_ops->irq_gpio);
  474. reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
  475. wm97xx_reg_write(wm, AC97_MISC_AFE,
  476. reg | wm->mach_ops->irq_gpio);
  477. }
  478. free_irq(wm->pen_irq, wm);
  479. }
  480. wm->pen_is_down = 0;
  481. /* Balance out interrupt disables/enables */
  482. if (cancel_work_sync(&wm->pen_event_work))
  483. wm->mach_ops->irq_enable(wm, 1);
  484. /* ts_reader rearms itself so we need to explicitly stop it
  485. * before we destroy the workqueue.
  486. */
  487. cancel_delayed_work_sync(&wm->ts_reader);
  488. destroy_workqueue(wm->ts_workq);
  489. /* stop digitiser */
  490. wm->codec->dig_enable(wm, 0);
  491. if (wm->mach_ops && wm->mach_ops->acc_enabled)
  492. wm->codec->acc_enable(wm, 0);
  493. }
  494. static int wm97xx_register_touch(struct wm97xx *wm)
  495. {
  496. struct wm97xx_pdata *pdata = dev_get_platdata(wm->dev);
  497. int ret;
  498. wm->input_dev = devm_input_allocate_device(wm->dev);
  499. if (wm->input_dev == NULL)
  500. return -ENOMEM;
  501. /* set up touch configuration */
  502. wm->input_dev->name = "wm97xx touchscreen";
  503. wm->input_dev->phys = "wm97xx";
  504. wm->input_dev->open = wm97xx_ts_input_open;
  505. wm->input_dev->close = wm97xx_ts_input_close;
  506. __set_bit(EV_ABS, wm->input_dev->evbit);
  507. __set_bit(EV_KEY, wm->input_dev->evbit);
  508. __set_bit(BTN_TOUCH, wm->input_dev->keybit);
  509. input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
  510. abs_x[2], 0);
  511. input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
  512. abs_y[2], 0);
  513. input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
  514. abs_p[2], 0);
  515. input_set_drvdata(wm->input_dev, wm);
  516. wm->input_dev->dev.parent = wm->dev;
  517. ret = input_register_device(wm->input_dev);
  518. if (ret)
  519. return ret;
  520. /*
  521. * register our extended touch device (for machine specific
  522. * extensions)
  523. */
  524. wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
  525. if (!wm->touch_dev) {
  526. ret = -ENOMEM;
  527. goto touch_err;
  528. }
  529. platform_set_drvdata(wm->touch_dev, wm);
  530. wm->touch_dev->dev.parent = wm->dev;
  531. wm->touch_dev->dev.platform_data = pdata;
  532. ret = platform_device_add(wm->touch_dev);
  533. if (ret < 0)
  534. goto touch_reg_err;
  535. return 0;
  536. touch_reg_err:
  537. platform_device_put(wm->touch_dev);
  538. touch_err:
  539. input_unregister_device(wm->input_dev);
  540. wm->input_dev = NULL;
  541. return ret;
  542. }
  543. static void wm97xx_unregister_touch(struct wm97xx *wm)
  544. {
  545. platform_device_unregister(wm->touch_dev);
  546. input_unregister_device(wm->input_dev);
  547. wm->input_dev = NULL;
  548. }
  549. static int _wm97xx_probe(struct wm97xx *wm)
  550. {
  551. int id = 0;
  552. mutex_init(&wm->codec_mutex);
  553. dev_set_drvdata(wm->dev, wm);
  554. /* check that we have a supported codec */
  555. id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
  556. if (id != WM97XX_ID1) {
  557. dev_err(wm->dev,
  558. "Device with vendor %04x is not a wm97xx\n", id);
  559. return -ENODEV;
  560. }
  561. wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
  562. wm->variant = WM97xx_GENERIC;
  563. dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
  564. switch (wm->id & 0xff) {
  565. #ifdef CONFIG_TOUCHSCREEN_WM9705
  566. case 0x05:
  567. wm->codec = &wm9705_codec;
  568. break;
  569. #endif
  570. #ifdef CONFIG_TOUCHSCREEN_WM9712
  571. case 0x12:
  572. wm->codec = &wm9712_codec;
  573. break;
  574. #endif
  575. #ifdef CONFIG_TOUCHSCREEN_WM9713
  576. case 0x13:
  577. wm->codec = &wm9713_codec;
  578. break;
  579. #endif
  580. default:
  581. dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
  582. wm->id & 0xff);
  583. return -ENODEV;
  584. }
  585. /* set up physical characteristics */
  586. wm->codec->phy_init(wm);
  587. /* load gpio cache */
  588. wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
  589. wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
  590. wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
  591. wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
  592. wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
  593. wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
  594. return wm97xx_register_touch(wm);
  595. }
  596. static void wm97xx_remove_battery(struct wm97xx *wm)
  597. {
  598. platform_device_unregister(wm->battery_dev);
  599. }
  600. static int wm97xx_add_battery(struct wm97xx *wm,
  601. struct wm97xx_batt_pdata *pdata)
  602. {
  603. int ret;
  604. wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
  605. if (!wm->battery_dev)
  606. return -ENOMEM;
  607. platform_set_drvdata(wm->battery_dev, wm);
  608. wm->battery_dev->dev.parent = wm->dev;
  609. wm->battery_dev->dev.platform_data = pdata;
  610. ret = platform_device_add(wm->battery_dev);
  611. if (ret)
  612. platform_device_put(wm->battery_dev);
  613. return ret;
  614. }
  615. static int wm97xx_probe(struct device *dev)
  616. {
  617. struct wm97xx *wm;
  618. int ret;
  619. struct wm97xx_pdata *pdata = dev_get_platdata(dev);
  620. wm = devm_kzalloc(dev, sizeof(struct wm97xx), GFP_KERNEL);
  621. if (!wm)
  622. return -ENOMEM;
  623. wm->dev = dev;
  624. wm->ac97 = to_ac97_t(dev);
  625. ret = _wm97xx_probe(wm);
  626. if (ret)
  627. return ret;
  628. ret = wm97xx_add_battery(wm, pdata ? pdata->batt_pdata : NULL);
  629. if (ret < 0)
  630. goto batt_err;
  631. return ret;
  632. batt_err:
  633. wm97xx_unregister_touch(wm);
  634. return ret;
  635. }
  636. static int wm97xx_remove(struct device *dev)
  637. {
  638. struct wm97xx *wm = dev_get_drvdata(dev);
  639. wm97xx_remove_battery(wm);
  640. wm97xx_unregister_touch(wm);
  641. return 0;
  642. }
  643. static int wm97xx_mfd_probe(struct platform_device *pdev)
  644. {
  645. struct wm97xx *wm;
  646. struct wm97xx_platform_data *mfd_pdata = dev_get_platdata(&pdev->dev);
  647. int ret;
  648. wm = devm_kzalloc(&pdev->dev, sizeof(struct wm97xx), GFP_KERNEL);
  649. if (!wm)
  650. return -ENOMEM;
  651. wm->dev = &pdev->dev;
  652. wm->ac97 = mfd_pdata->ac97;
  653. ret = _wm97xx_probe(wm);
  654. if (ret)
  655. return ret;
  656. ret = wm97xx_add_battery(wm, mfd_pdata->batt_pdata);
  657. if (ret < 0)
  658. goto batt_err;
  659. return ret;
  660. batt_err:
  661. wm97xx_unregister_touch(wm);
  662. return ret;
  663. }
  664. static int wm97xx_mfd_remove(struct platform_device *pdev)
  665. {
  666. return wm97xx_remove(&pdev->dev);
  667. }
  668. static int __maybe_unused wm97xx_suspend(struct device *dev)
  669. {
  670. struct wm97xx *wm = dev_get_drvdata(dev);
  671. u16 reg;
  672. int suspend_mode;
  673. if (device_may_wakeup(&wm->input_dev->dev))
  674. suspend_mode = wm->suspend_mode;
  675. else
  676. suspend_mode = 0;
  677. if (wm->input_dev->users)
  678. cancel_delayed_work_sync(&wm->ts_reader);
  679. /* Power down the digitiser (bypassing the cache for resume) */
  680. reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
  681. reg &= ~WM97XX_PRP_DET_DIG;
  682. if (wm->input_dev->users)
  683. reg |= suspend_mode;
  684. wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
  685. /* WM9713 has an additional power bit - turn it off if there
  686. * are no users or if suspend mode is zero. */
  687. if (wm->id == WM9713_ID2 &&
  688. (!wm->input_dev->users || !suspend_mode)) {
  689. reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
  690. wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
  691. }
  692. return 0;
  693. }
  694. static int __maybe_unused wm97xx_resume(struct device *dev)
  695. {
  696. struct wm97xx *wm = dev_get_drvdata(dev);
  697. /* restore digitiser and gpios */
  698. if (wm->id == WM9713_ID2) {
  699. wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
  700. wm97xx_reg_write(wm, 0x5a, wm->misc);
  701. if (wm->input_dev->users) {
  702. u16 reg;
  703. reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
  704. wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
  705. }
  706. }
  707. wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
  708. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
  709. wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
  710. wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
  711. wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
  712. wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
  713. wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
  714. wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
  715. if (wm->input_dev->users && !wm->pen_irq) {
  716. wm->ts_reader_interval = wm->ts_reader_min_interval;
  717. queue_delayed_work(wm->ts_workq, &wm->ts_reader,
  718. wm->ts_reader_interval);
  719. }
  720. return 0;
  721. }
  722. static SIMPLE_DEV_PM_OPS(wm97xx_pm_ops, wm97xx_suspend, wm97xx_resume);
  723. /*
  724. * Machine specific operations
  725. */
  726. int wm97xx_register_mach_ops(struct wm97xx *wm,
  727. struct wm97xx_mach_ops *mach_ops)
  728. {
  729. mutex_lock(&wm->codec_mutex);
  730. if (wm->mach_ops) {
  731. mutex_unlock(&wm->codec_mutex);
  732. return -EINVAL;
  733. }
  734. wm->mach_ops = mach_ops;
  735. mutex_unlock(&wm->codec_mutex);
  736. return 0;
  737. }
  738. EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
  739. void wm97xx_unregister_mach_ops(struct wm97xx *wm)
  740. {
  741. mutex_lock(&wm->codec_mutex);
  742. wm->mach_ops = NULL;
  743. mutex_unlock(&wm->codec_mutex);
  744. }
  745. EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
  746. static struct device_driver wm97xx_driver = {
  747. .name = "wm97xx-ts",
  748. #ifdef CONFIG_AC97_BUS
  749. .bus = &ac97_bus_type,
  750. #endif
  751. .owner = THIS_MODULE,
  752. .probe = wm97xx_probe,
  753. .remove = wm97xx_remove,
  754. .pm = &wm97xx_pm_ops,
  755. };
  756. static struct platform_driver wm97xx_mfd_driver = {
  757. .driver = {
  758. .name = "wm97xx-ts",
  759. .pm = &wm97xx_pm_ops,
  760. },
  761. .probe = wm97xx_mfd_probe,
  762. .remove = wm97xx_mfd_remove,
  763. };
  764. static int __init wm97xx_init(void)
  765. {
  766. int ret;
  767. ret = platform_driver_register(&wm97xx_mfd_driver);
  768. if (ret)
  769. return ret;
  770. if (IS_BUILTIN(CONFIG_AC97_BUS))
  771. ret = driver_register(&wm97xx_driver);
  772. return ret;
  773. }
  774. static void __exit wm97xx_exit(void)
  775. {
  776. if (IS_BUILTIN(CONFIG_AC97_BUS))
  777. driver_unregister(&wm97xx_driver);
  778. platform_driver_unregister(&wm97xx_mfd_driver);
  779. }
  780. module_init(wm97xx_init);
  781. module_exit(wm97xx_exit);
  782. /* Module information */
  783. MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
  784. MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
  785. MODULE_LICENSE("GPL");