wm97xx-core.c 22 KB

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