at32psif.c 8.2 KB

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