atmel-wm97xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Atmel AT91 and AVR32 continuous touch screen driver for Wolfson WM97xx AC97
  3. * codecs.
  4. *
  5. * Copyright (C) 2008 - 2009 Atmel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/irq.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/wm97xx.h>
  19. #include <linux/timer.h>
  20. #include <linux/gpio.h>
  21. #include <linux/io.h>
  22. #include <linux/slab.h>
  23. #define AC97C_ICA 0x10
  24. #define AC97C_CBRHR 0x30
  25. #define AC97C_CBSR 0x38
  26. #define AC97C_CBMR 0x3c
  27. #define AC97C_IER 0x54
  28. #define AC97C_IDR 0x58
  29. #define AC97C_RXRDY (1 << 4)
  30. #define AC97C_OVRUN (1 << 5)
  31. #define AC97C_CMR_SIZE_20 (0 << 16)
  32. #define AC97C_CMR_SIZE_18 (1 << 16)
  33. #define AC97C_CMR_SIZE_16 (2 << 16)
  34. #define AC97C_CMR_SIZE_10 (3 << 16)
  35. #define AC97C_CMR_CEM_LITTLE (1 << 18)
  36. #define AC97C_CMR_CEM_BIG (0 << 18)
  37. #define AC97C_CMR_CENA (1 << 21)
  38. #define AC97C_INT_CBEVT (1 << 4)
  39. #define AC97C_SR_CAEVT (1 << 3)
  40. #define AC97C_CH_MASK(slot) \
  41. (0x7 << (3 * (slot - 3)))
  42. #define AC97C_CH_ASSIGN(slot, channel) \
  43. (AC97C_CHANNEL_##channel << (3 * (slot - 3)))
  44. #define AC97C_CHANNEL_NONE 0x0
  45. #define AC97C_CHANNEL_B 0x2
  46. #define ac97c_writel(chip, reg, val) \
  47. __raw_writel((val), (chip)->regs + AC97C_##reg)
  48. #define ac97c_readl(chip, reg) \
  49. __raw_readl((chip)->regs + AC97C_##reg)
  50. #ifdef CONFIG_CPU_AT32AP700X
  51. #define ATMEL_WM97XX_AC97C_IOMEM (0xfff02800)
  52. #define ATMEL_WM97XX_AC97C_IRQ (29)
  53. #define ATMEL_WM97XX_GPIO_DEFAULT (32+16) /* Pin 16 on port B. */
  54. #else
  55. #error Unknown CPU, this driver only supports AT32AP700X CPUs.
  56. #endif
  57. struct continuous {
  58. u16 id; /* codec id */
  59. u8 code; /* continuous code */
  60. u8 reads; /* number of coord reads per read cycle */
  61. u32 speed; /* number of coords per second */
  62. };
  63. #define WM_READS(sp) ((sp / HZ) + 1)
  64. static const struct continuous cinfo[] = {
  65. {WM9705_ID2, 0, WM_READS(94), 94},
  66. {WM9705_ID2, 1, WM_READS(188), 188},
  67. {WM9705_ID2, 2, WM_READS(375), 375},
  68. {WM9705_ID2, 3, WM_READS(750), 750},
  69. {WM9712_ID2, 0, WM_READS(94), 94},
  70. {WM9712_ID2, 1, WM_READS(188), 188},
  71. {WM9712_ID2, 2, WM_READS(375), 375},
  72. {WM9712_ID2, 3, WM_READS(750), 750},
  73. {WM9713_ID2, 0, WM_READS(94), 94},
  74. {WM9713_ID2, 1, WM_READS(120), 120},
  75. {WM9713_ID2, 2, WM_READS(154), 154},
  76. {WM9713_ID2, 3, WM_READS(188), 188},
  77. };
  78. /* Continuous speed index. */
  79. static int sp_idx;
  80. /*
  81. * Pen sampling frequency (Hz) in continuous mode.
  82. */
  83. static int cont_rate = 188;
  84. module_param(cont_rate, int, 0);
  85. MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
  86. /*
  87. * Pen down detection.
  88. *
  89. * This driver can either poll or use an interrupt to indicate a pen down
  90. * event. If the irq request fails then it will fall back to polling mode.
  91. */
  92. static int pen_int = 1;
  93. module_param(pen_int, int, 0);
  94. MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");
  95. /*
  96. * Pressure readback.
  97. *
  98. * Set to 1 to read back pen down pressure.
  99. */
  100. static int pressure;
  101. module_param(pressure, int, 0);
  102. MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
  103. /*
  104. * AC97 touch data slot.
  105. *
  106. * Touch screen readback data ac97 slot.
  107. */
  108. static int ac97_touch_slot = 5;
  109. module_param(ac97_touch_slot, int, 0);
  110. MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
  111. /*
  112. * GPIO line number.
  113. *
  114. * Set to GPIO number where the signal from the WM97xx device is hooked up.
  115. */
  116. static int atmel_gpio_line = ATMEL_WM97XX_GPIO_DEFAULT;
  117. module_param(atmel_gpio_line, int, 0);
  118. MODULE_PARM_DESC(atmel_gpio_line, "GPIO line number connected to WM97xx");
  119. struct atmel_wm97xx {
  120. struct wm97xx *wm;
  121. struct timer_list pen_timer;
  122. void __iomem *regs;
  123. unsigned long ac97c_irq;
  124. unsigned long gpio_pen;
  125. unsigned long gpio_irq;
  126. unsigned short x;
  127. unsigned short y;
  128. };
  129. static irqreturn_t atmel_wm97xx_channel_b_interrupt(int irq, void *dev_id)
  130. {
  131. struct atmel_wm97xx *atmel_wm97xx = dev_id;
  132. struct wm97xx *wm = atmel_wm97xx->wm;
  133. int status = ac97c_readl(atmel_wm97xx, CBSR);
  134. irqreturn_t retval = IRQ_NONE;
  135. if (status & AC97C_OVRUN) {
  136. dev_dbg(&wm->touch_dev->dev, "AC97C overrun\n");
  137. ac97c_readl(atmel_wm97xx, CBRHR);
  138. retval = IRQ_HANDLED;
  139. } else if (status & AC97C_RXRDY) {
  140. u16 data;
  141. u16 value;
  142. u16 source;
  143. u16 pen_down;
  144. data = ac97c_readl(atmel_wm97xx, CBRHR);
  145. value = data & 0x0fff;
  146. source = data & WM97XX_ADCSEL_MASK;
  147. pen_down = (data & WM97XX_PEN_DOWN) >> 8;
  148. if (source == WM97XX_ADCSEL_X)
  149. atmel_wm97xx->x = value;
  150. if (source == WM97XX_ADCSEL_Y)
  151. atmel_wm97xx->y = value;
  152. if (!pressure && source == WM97XX_ADCSEL_Y) {
  153. input_report_abs(wm->input_dev, ABS_X, atmel_wm97xx->x);
  154. input_report_abs(wm->input_dev, ABS_Y, atmel_wm97xx->y);
  155. input_report_key(wm->input_dev, BTN_TOUCH, pen_down);
  156. input_sync(wm->input_dev);
  157. } else if (pressure && source == WM97XX_ADCSEL_PRES) {
  158. input_report_abs(wm->input_dev, ABS_X, atmel_wm97xx->x);
  159. input_report_abs(wm->input_dev, ABS_Y, atmel_wm97xx->y);
  160. input_report_abs(wm->input_dev, ABS_PRESSURE, value);
  161. input_report_key(wm->input_dev, BTN_TOUCH, value);
  162. input_sync(wm->input_dev);
  163. }
  164. retval = IRQ_HANDLED;
  165. }
  166. return retval;
  167. }
  168. static void atmel_wm97xx_acc_pen_up(struct wm97xx *wm)
  169. {
  170. struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(wm->touch_dev);
  171. struct input_dev *input_dev = wm->input_dev;
  172. int pen_down = gpio_get_value(atmel_wm97xx->gpio_pen);
  173. if (pen_down != 0) {
  174. mod_timer(&atmel_wm97xx->pen_timer,
  175. jiffies + msecs_to_jiffies(1));
  176. } else {
  177. if (pressure)
  178. input_report_abs(input_dev, ABS_PRESSURE, 0);
  179. input_report_key(input_dev, BTN_TOUCH, 0);
  180. input_sync(input_dev);
  181. }
  182. }
  183. static void atmel_wm97xx_pen_timer(unsigned long data)
  184. {
  185. atmel_wm97xx_acc_pen_up((struct wm97xx *)data);
  186. }
  187. static int atmel_wm97xx_acc_startup(struct wm97xx *wm)
  188. {
  189. struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(wm->touch_dev);
  190. int idx = 0;
  191. if (wm->ac97 == NULL)
  192. return -ENODEV;
  193. for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
  194. if (wm->id != cinfo[idx].id)
  195. continue;
  196. sp_idx = idx;
  197. if (cont_rate <= cinfo[idx].speed)
  198. break;
  199. }
  200. wm->acc_rate = cinfo[sp_idx].code;
  201. wm->acc_slot = ac97_touch_slot;
  202. dev_info(&wm->touch_dev->dev, "atmel accelerated touchscreen driver, "
  203. "%d samples/sec\n", cinfo[sp_idx].speed);
  204. if (pen_int) {
  205. unsigned long reg;
  206. wm->pen_irq = atmel_wm97xx->gpio_irq;
  207. switch (wm->id) {
  208. case WM9712_ID2: /* Fall through. */
  209. case WM9713_ID2:
  210. /*
  211. * Use GPIO 13 (PEN_DOWN) to assert GPIO line 3
  212. * (PENDOWN).
  213. */
  214. wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
  215. WM97XX_GPIO_POL_HIGH,
  216. WM97XX_GPIO_STICKY,
  217. WM97XX_GPIO_WAKE);
  218. wm97xx_config_gpio(wm, WM97XX_GPIO_3, WM97XX_GPIO_OUT,
  219. WM97XX_GPIO_POL_HIGH,
  220. WM97XX_GPIO_NOTSTICKY,
  221. WM97XX_GPIO_NOWAKE);
  222. case WM9705_ID2: /* Fall through. */
  223. /*
  224. * Enable touch data slot in AC97 controller channel B.
  225. */
  226. reg = ac97c_readl(atmel_wm97xx, ICA);
  227. reg &= ~AC97C_CH_MASK(wm->acc_slot);
  228. reg |= AC97C_CH_ASSIGN(wm->acc_slot, B);
  229. ac97c_writel(atmel_wm97xx, ICA, reg);
  230. /*
  231. * Enable channel and interrupt for RXRDY and OVERRUN.
  232. */
  233. ac97c_writel(atmel_wm97xx, CBMR, AC97C_CMR_CENA
  234. | AC97C_CMR_CEM_BIG
  235. | AC97C_CMR_SIZE_16
  236. | AC97C_OVRUN
  237. | AC97C_RXRDY);
  238. /* Dummy read to empty RXRHR. */
  239. ac97c_readl(atmel_wm97xx, CBRHR);
  240. /*
  241. * Enable interrupt for channel B in the AC97
  242. * controller.
  243. */
  244. ac97c_writel(atmel_wm97xx, IER, AC97C_INT_CBEVT);
  245. break;
  246. default:
  247. dev_err(&wm->touch_dev->dev, "pen down irq not "
  248. "supported on this device\n");
  249. pen_int = 0;
  250. break;
  251. }
  252. }
  253. return 0;
  254. }
  255. static void atmel_wm97xx_acc_shutdown(struct wm97xx *wm)
  256. {
  257. if (pen_int) {
  258. struct atmel_wm97xx *atmel_wm97xx =
  259. platform_get_drvdata(wm->touch_dev);
  260. unsigned long ica;
  261. switch (wm->id & 0xffff) {
  262. case WM9705_ID2: /* Fall through. */
  263. case WM9712_ID2: /* Fall through. */
  264. case WM9713_ID2:
  265. /* Disable slot and turn off channel B interrupts. */
  266. ica = ac97c_readl(atmel_wm97xx, ICA);
  267. ica &= ~AC97C_CH_MASK(wm->acc_slot);
  268. ac97c_writel(atmel_wm97xx, ICA, ica);
  269. ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);
  270. ac97c_writel(atmel_wm97xx, CBMR, 0);
  271. wm->pen_irq = 0;
  272. break;
  273. default:
  274. dev_err(&wm->touch_dev->dev, "unknown codec\n");
  275. break;
  276. }
  277. }
  278. }
  279. static void atmel_wm97xx_irq_enable(struct wm97xx *wm, int enable)
  280. {
  281. /* Intentionally left empty. */
  282. }
  283. static struct wm97xx_mach_ops atmel_mach_ops = {
  284. .acc_enabled = 1,
  285. .acc_pen_up = atmel_wm97xx_acc_pen_up,
  286. .acc_startup = atmel_wm97xx_acc_startup,
  287. .acc_shutdown = atmel_wm97xx_acc_shutdown,
  288. .irq_enable = atmel_wm97xx_irq_enable,
  289. .irq_gpio = WM97XX_GPIO_3,
  290. };
  291. static int __init atmel_wm97xx_probe(struct platform_device *pdev)
  292. {
  293. struct wm97xx *wm = platform_get_drvdata(pdev);
  294. struct atmel_wm97xx *atmel_wm97xx;
  295. int ret;
  296. atmel_wm97xx = kzalloc(sizeof(struct atmel_wm97xx), GFP_KERNEL);
  297. if (!atmel_wm97xx) {
  298. dev_dbg(&pdev->dev, "out of memory\n");
  299. return -ENOMEM;
  300. }
  301. atmel_wm97xx->wm = wm;
  302. atmel_wm97xx->regs = (void *)ATMEL_WM97XX_AC97C_IOMEM;
  303. atmel_wm97xx->ac97c_irq = ATMEL_WM97XX_AC97C_IRQ;
  304. atmel_wm97xx->gpio_pen = atmel_gpio_line;
  305. atmel_wm97xx->gpio_irq = gpio_to_irq(atmel_wm97xx->gpio_pen);
  306. setup_timer(&atmel_wm97xx->pen_timer, atmel_wm97xx_pen_timer,
  307. (unsigned long)wm);
  308. ret = request_irq(atmel_wm97xx->ac97c_irq,
  309. atmel_wm97xx_channel_b_interrupt,
  310. IRQF_SHARED, "atmel-wm97xx-ch-b", atmel_wm97xx);
  311. if (ret) {
  312. dev_dbg(&pdev->dev, "could not request ac97c irq\n");
  313. goto err;
  314. }
  315. platform_set_drvdata(pdev, atmel_wm97xx);
  316. ret = wm97xx_register_mach_ops(wm, &atmel_mach_ops);
  317. if (ret)
  318. goto err_irq;
  319. return ret;
  320. err_irq:
  321. free_irq(atmel_wm97xx->ac97c_irq, atmel_wm97xx);
  322. err:
  323. kfree(atmel_wm97xx);
  324. return ret;
  325. }
  326. static int __exit atmel_wm97xx_remove(struct platform_device *pdev)
  327. {
  328. struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);
  329. struct wm97xx *wm = atmel_wm97xx->wm;
  330. ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);
  331. free_irq(atmel_wm97xx->ac97c_irq, atmel_wm97xx);
  332. del_timer_sync(&atmel_wm97xx->pen_timer);
  333. wm97xx_unregister_mach_ops(wm);
  334. kfree(atmel_wm97xx);
  335. return 0;
  336. }
  337. #ifdef CONFIG_PM_SLEEP
  338. static int atmel_wm97xx_suspend(struct device *dev)
  339. {
  340. struct platform_device *pdev = to_platform_device(dev);
  341. struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);
  342. ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);
  343. disable_irq(atmel_wm97xx->gpio_irq);
  344. del_timer_sync(&atmel_wm97xx->pen_timer);
  345. return 0;
  346. }
  347. static int atmel_wm97xx_resume(struct device *dev)
  348. {
  349. struct platform_device *pdev = to_platform_device(dev);
  350. struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);
  351. struct wm97xx *wm = atmel_wm97xx->wm;
  352. if (wm->input_dev->users) {
  353. enable_irq(atmel_wm97xx->gpio_irq);
  354. ac97c_writel(atmel_wm97xx, IER, AC97C_INT_CBEVT);
  355. }
  356. return 0;
  357. }
  358. #endif
  359. static SIMPLE_DEV_PM_OPS(atmel_wm97xx_pm_ops,
  360. atmel_wm97xx_suspend, atmel_wm97xx_resume);
  361. static struct platform_driver atmel_wm97xx_driver = {
  362. .remove = __exit_p(atmel_wm97xx_remove),
  363. .driver = {
  364. .name = "wm97xx-touch",
  365. .pm = &atmel_wm97xx_pm_ops,
  366. },
  367. };
  368. module_platform_driver_probe(atmel_wm97xx_driver, atmel_wm97xx_probe);
  369. MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
  370. MODULE_DESCRIPTION("wm97xx continuous touch driver for Atmel AT91 and AVR32");
  371. MODULE_LICENSE("GPL");