wm97xx-core.c 22 KB

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