at32psif.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (C) 2007 Atmel Corporation
  3. *
  4. * Driver for the AT32AP700X PS/2 controller (PSIF).
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/serio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/platform_device.h>
  21. /* PSIF register offsets */
  22. #define PSIF_CR 0x00
  23. #define PSIF_RHR 0x04
  24. #define PSIF_THR 0x08
  25. #define PSIF_SR 0x10
  26. #define PSIF_IER 0x14
  27. #define PSIF_IDR 0x18
  28. #define PSIF_IMR 0x1c
  29. #define PSIF_PSR 0x24
  30. /* Bitfields in control register. */
  31. #define PSIF_CR_RXDIS_OFFSET 1
  32. #define PSIF_CR_RXDIS_SIZE 1
  33. #define PSIF_CR_RXEN_OFFSET 0
  34. #define PSIF_CR_RXEN_SIZE 1
  35. #define PSIF_CR_SWRST_OFFSET 15
  36. #define PSIF_CR_SWRST_SIZE 1
  37. #define PSIF_CR_TXDIS_OFFSET 9
  38. #define PSIF_CR_TXDIS_SIZE 1
  39. #define PSIF_CR_TXEN_OFFSET 8
  40. #define PSIF_CR_TXEN_SIZE 1
  41. /* Bitfields in interrupt disable, enable, mask and status register. */
  42. #define PSIF_NACK_OFFSET 8
  43. #define PSIF_NACK_SIZE 1
  44. #define PSIF_OVRUN_OFFSET 5
  45. #define PSIF_OVRUN_SIZE 1
  46. #define PSIF_PARITY_OFFSET 9
  47. #define PSIF_PARITY_SIZE 1
  48. #define PSIF_RXRDY_OFFSET 4
  49. #define PSIF_RXRDY_SIZE 1
  50. #define PSIF_TXEMPTY_OFFSET 1
  51. #define PSIF_TXEMPTY_SIZE 1
  52. #define PSIF_TXRDY_OFFSET 0
  53. #define PSIF_TXRDY_SIZE 1
  54. /* Bitfields in prescale register. */
  55. #define PSIF_PSR_PRSCV_OFFSET 0
  56. #define PSIF_PSR_PRSCV_SIZE 12
  57. /* Bitfields in receive hold register. */
  58. #define PSIF_RHR_RXDATA_OFFSET 0
  59. #define PSIF_RHR_RXDATA_SIZE 8
  60. /* Bitfields in transmit hold register. */
  61. #define PSIF_THR_TXDATA_OFFSET 0
  62. #define PSIF_THR_TXDATA_SIZE 8
  63. /* Bit manipulation macros */
  64. #define PSIF_BIT(name) \
  65. (1 << PSIF_##name##_OFFSET)
  66. #define PSIF_BF(name, value) \
  67. (((value) & ((1 << PSIF_##name##_SIZE) - 1)) \
  68. << PSIF_##name##_OFFSET)
  69. #define PSIF_BFEXT(name, value) \
  70. (((value) >> PSIF_##name##_OFFSET) \
  71. & ((1 << PSIF_##name##_SIZE) - 1))
  72. #define PSIF_BFINS(name, value, old) \
  73. (((old) & ~(((1 << PSIF_##name##_SIZE) - 1) \
  74. << PSIF_##name##_OFFSET)) \
  75. | PSIF_BF(name, value))
  76. /* Register access macros */
  77. #define psif_readl(port, reg) \
  78. __raw_readl((port)->regs + PSIF_##reg)
  79. #define psif_writel(port, reg, value) \
  80. __raw_writel((value), (port)->regs + PSIF_##reg)
  81. struct psif {
  82. struct platform_device *pdev;
  83. struct clk *pclk;
  84. struct serio *io;
  85. void __iomem *regs;
  86. unsigned int irq;
  87. unsigned int open;
  88. /* Prevent concurrent writes to PSIF THR. */
  89. spinlock_t lock;
  90. };
  91. static irqreturn_t psif_interrupt(int irq, void *_ptr)
  92. {
  93. struct psif *psif = _ptr;
  94. int retval = IRQ_NONE;
  95. unsigned int io_flags = 0;
  96. unsigned long status;
  97. status = psif_readl(psif, SR);
  98. if (status & PSIF_BIT(RXRDY)) {
  99. unsigned char val = (unsigned char) psif_readl(psif, RHR);
  100. if (status & PSIF_BIT(PARITY))
  101. io_flags |= SERIO_PARITY;
  102. if (status & PSIF_BIT(OVRUN))
  103. dev_err(&psif->pdev->dev, "overrun read error\n");
  104. serio_interrupt(psif->io, val, io_flags);
  105. retval = IRQ_HANDLED;
  106. }
  107. return retval;
  108. }
  109. static int psif_write(struct serio *io, unsigned char val)
  110. {
  111. struct psif *psif = io->port_data;
  112. unsigned long flags;
  113. int timeout = 10;
  114. int retval = 0;
  115. spin_lock_irqsave(&psif->lock, flags);
  116. while (!(psif_readl(psif, SR) & PSIF_BIT(TXEMPTY)) && timeout--)
  117. msleep(10);
  118. if (timeout >= 0) {
  119. psif_writel(psif, THR, val);
  120. } else {
  121. dev_dbg(&psif->pdev->dev, "timeout writing to THR\n");
  122. retval = -EBUSY;
  123. }
  124. spin_unlock_irqrestore(&psif->lock, flags);
  125. return retval;
  126. }
  127. static int psif_open(struct serio *io)
  128. {
  129. struct psif *psif = io->port_data;
  130. int retval;
  131. retval = clk_enable(psif->pclk);
  132. if (retval)
  133. goto out;
  134. psif_writel(psif, CR, PSIF_BIT(CR_TXEN) | PSIF_BIT(CR_RXEN));
  135. psif_writel(psif, IER, PSIF_BIT(RXRDY));
  136. psif->open = 1;
  137. out:
  138. return retval;
  139. }
  140. static void psif_close(struct serio *io)
  141. {
  142. struct psif *psif = io->port_data;
  143. psif->open = 0;
  144. psif_writel(psif, IDR, ~0UL);
  145. psif_writel(psif, CR, PSIF_BIT(CR_TXDIS) | PSIF_BIT(CR_RXDIS));
  146. clk_disable(psif->pclk);
  147. }
  148. static void psif_set_prescaler(struct psif *psif)
  149. {
  150. unsigned long prscv;
  151. unsigned long rate = clk_get_rate(psif->pclk);
  152. /* PRSCV = Pulse length (100 us) * PSIF module frequency. */
  153. prscv = 100 * (rate / 1000000UL);
  154. if (prscv > ((1<<PSIF_PSR_PRSCV_SIZE) - 1)) {
  155. prscv = (1<<PSIF_PSR_PRSCV_SIZE) - 1;
  156. dev_dbg(&psif->pdev->dev, "pclk too fast, "
  157. "prescaler set to max\n");
  158. }
  159. clk_enable(psif->pclk);
  160. psif_writel(psif, PSR, prscv);
  161. clk_disable(psif->pclk);
  162. }
  163. static int __init psif_probe(struct platform_device *pdev)
  164. {
  165. struct resource *regs;
  166. struct psif *psif;
  167. struct serio *io;
  168. struct clk *pclk;
  169. int irq;
  170. int ret;
  171. psif = kzalloc(sizeof(struct psif), GFP_KERNEL);
  172. if (!psif) {
  173. dev_dbg(&pdev->dev, "out of memory\n");
  174. ret = -ENOMEM;
  175. goto out;
  176. }
  177. psif->pdev = pdev;
  178. io = kzalloc(sizeof(struct serio), GFP_KERNEL);
  179. if (!io) {
  180. dev_dbg(&pdev->dev, "out of memory\n");
  181. ret = -ENOMEM;
  182. goto out_free_psif;
  183. }
  184. psif->io = io;
  185. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  186. if (!regs) {
  187. dev_dbg(&pdev->dev, "no mmio resources defined\n");
  188. ret = -ENOMEM;
  189. goto out_free_io;
  190. }
  191. psif->regs = ioremap(regs->start, regs->end - regs->start + 1);
  192. if (!psif->regs) {
  193. ret = -ENOMEM;
  194. dev_dbg(&pdev->dev, "could not map I/O memory\n");
  195. goto out_free_io;
  196. }
  197. pclk = clk_get(&pdev->dev, "pclk");
  198. if (IS_ERR(pclk)) {
  199. dev_dbg(&pdev->dev, "could not get peripheral clock\n");
  200. ret = PTR_ERR(pclk);
  201. goto out_iounmap;
  202. }
  203. psif->pclk = pclk;
  204. /* Reset the PSIF to enter at a known state. */
  205. ret = clk_enable(pclk);
  206. if (ret) {
  207. dev_dbg(&pdev->dev, "could not enable pclk\n");
  208. goto out_put_clk;
  209. }
  210. psif_writel(psif, CR, PSIF_BIT(CR_SWRST));
  211. clk_disable(pclk);
  212. irq = platform_get_irq(pdev, 0);
  213. if (irq < 0) {
  214. dev_dbg(&pdev->dev, "could not get irq\n");
  215. ret = -ENXIO;
  216. goto out_put_clk;
  217. }
  218. ret = request_irq(irq, psif_interrupt, IRQF_SHARED, "at32psif", psif);
  219. if (ret) {
  220. dev_dbg(&pdev->dev, "could not request irq %d\n", irq);
  221. goto out_put_clk;
  222. }
  223. psif->irq = irq;
  224. io->id.type = SERIO_8042;
  225. io->write = psif_write;
  226. io->open = psif_open;
  227. io->close = psif_close;
  228. snprintf(io->name, sizeof(io->name), "AVR32 PS/2 port%d", pdev->id);
  229. snprintf(io->phys, sizeof(io->phys), "at32psif/serio%d", pdev->id);
  230. io->port_data = psif;
  231. io->dev.parent = &pdev->dev;
  232. psif_set_prescaler(psif);
  233. spin_lock_init(&psif->lock);
  234. serio_register_port(psif->io);
  235. platform_set_drvdata(pdev, psif);
  236. dev_info(&pdev->dev, "Atmel AVR32 PSIF PS/2 driver on 0x%08x irq %d\n",
  237. (int)psif->regs, psif->irq);
  238. return 0;
  239. out_put_clk:
  240. clk_put(psif->pclk);
  241. out_iounmap:
  242. iounmap(psif->regs);
  243. out_free_io:
  244. kfree(io);
  245. out_free_psif:
  246. kfree(psif);
  247. out:
  248. return ret;
  249. }
  250. static int __exit psif_remove(struct platform_device *pdev)
  251. {
  252. struct psif *psif = platform_get_drvdata(pdev);
  253. psif_writel(psif, IDR, ~0UL);
  254. psif_writel(psif, CR, PSIF_BIT(CR_TXDIS) | PSIF_BIT(CR_RXDIS));
  255. serio_unregister_port(psif->io);
  256. iounmap(psif->regs);
  257. free_irq(psif->irq, psif);
  258. clk_put(psif->pclk);
  259. kfree(psif);
  260. platform_set_drvdata(pdev, NULL);
  261. return 0;
  262. }
  263. #ifdef CONFIG_PM
  264. static int psif_suspend(struct platform_device *pdev, pm_message_t state)
  265. {
  266. struct psif *psif = platform_get_drvdata(pdev);
  267. if (psif->open) {
  268. psif_writel(psif, CR, PSIF_BIT(CR_RXDIS) | PSIF_BIT(CR_TXDIS));
  269. clk_disable(psif->pclk);
  270. }
  271. return 0;
  272. }
  273. static int psif_resume(struct platform_device *pdev)
  274. {
  275. struct psif *psif = platform_get_drvdata(pdev);
  276. if (psif->open) {
  277. clk_enable(psif->pclk);
  278. psif_set_prescaler(psif);
  279. psif_writel(psif, CR, PSIF_BIT(CR_RXEN) | PSIF_BIT(CR_TXEN));
  280. }
  281. return 0;
  282. }
  283. #else
  284. #define psif_suspend NULL
  285. #define psif_resume NULL
  286. #endif
  287. static struct platform_driver psif_driver = {
  288. .remove = __exit_p(psif_remove),
  289. .driver = {
  290. .name = "atmel_psif",
  291. },
  292. .suspend = psif_suspend,
  293. .resume = psif_resume,
  294. };
  295. static int __init psif_init(void)
  296. {
  297. return platform_driver_probe(&psif_driver, psif_probe);
  298. }
  299. static void __exit psif_exit(void)
  300. {
  301. platform_driver_unregister(&psif_driver);
  302. }
  303. module_init(psif_init);
  304. module_exit(psif_exit);
  305. MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");
  306. MODULE_DESCRIPTION("Atmel AVR32 PSIF PS/2 driver");
  307. MODULE_LICENSE("GPL");