altera_ps2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Altera University Program PS2 controller driver
  3. *
  4. * Copyright (C) 2008 Thomas Chou <thomas@wytron.com.tw>
  5. *
  6. * Based on sa1111ps2.c, which is:
  7. * Copyright (C) 2002 Russell King
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/input.h>
  16. #include <linux/serio.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #define DRV_NAME "altera_ps2"
  23. struct ps2if {
  24. struct serio *io;
  25. struct resource *iomem_res;
  26. void __iomem *base;
  27. unsigned irq;
  28. };
  29. /*
  30. * Read all bytes waiting in the PS2 port. There should be
  31. * at the most one, but we loop for safety.
  32. */
  33. static irqreturn_t altera_ps2_rxint(int irq, void *dev_id)
  34. {
  35. struct ps2if *ps2if = dev_id;
  36. unsigned int status;
  37. int handled = IRQ_NONE;
  38. while ((status = readl(ps2if->base)) & 0xffff0000) {
  39. serio_interrupt(ps2if->io, status & 0xff, 0);
  40. handled = IRQ_HANDLED;
  41. }
  42. return handled;
  43. }
  44. /*
  45. * Write a byte to the PS2 port.
  46. */
  47. static int altera_ps2_write(struct serio *io, unsigned char val)
  48. {
  49. struct ps2if *ps2if = io->port_data;
  50. writel(val, ps2if->base);
  51. return 0;
  52. }
  53. static int altera_ps2_open(struct serio *io)
  54. {
  55. struct ps2if *ps2if = io->port_data;
  56. /* clear fifo */
  57. while (readl(ps2if->base) & 0xffff0000)
  58. /* empty */;
  59. writel(1, ps2if->base + 4); /* enable rx irq */
  60. return 0;
  61. }
  62. static void altera_ps2_close(struct serio *io)
  63. {
  64. struct ps2if *ps2if = io->port_data;
  65. writel(0, ps2if->base); /* disable rx irq */
  66. }
  67. /*
  68. * Add one device to this driver.
  69. */
  70. static int __devinit altera_ps2_probe(struct platform_device *pdev)
  71. {
  72. struct ps2if *ps2if;
  73. struct serio *serio;
  74. int error, irq;
  75. ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
  76. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  77. if (!ps2if || !serio) {
  78. error = -ENOMEM;
  79. goto err_free_mem;
  80. }
  81. serio->id.type = SERIO_8042;
  82. serio->write = altera_ps2_write;
  83. serio->open = altera_ps2_open;
  84. serio->close = altera_ps2_close;
  85. strlcpy(serio->name, dev_name(&pdev->dev), sizeof(serio->name));
  86. strlcpy(serio->phys, dev_name(&pdev->dev), sizeof(serio->phys));
  87. serio->port_data = ps2if;
  88. serio->dev.parent = &pdev->dev;
  89. ps2if->io = serio;
  90. ps2if->iomem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  91. if (ps2if->iomem_res == NULL) {
  92. error = -ENOENT;
  93. goto err_free_mem;
  94. }
  95. irq = platform_get_irq(pdev, 0);
  96. if (irq < 0) {
  97. error = -ENXIO;
  98. goto err_free_mem;
  99. }
  100. ps2if->irq = irq;
  101. if (!request_mem_region(ps2if->iomem_res->start,
  102. resource_size(ps2if->iomem_res), pdev->name)) {
  103. error = -EBUSY;
  104. goto err_free_mem;
  105. }
  106. ps2if->base = ioremap(ps2if->iomem_res->start,
  107. resource_size(ps2if->iomem_res));
  108. if (!ps2if->base) {
  109. error = -ENOMEM;
  110. goto err_free_res;
  111. }
  112. error = request_irq(ps2if->irq, altera_ps2_rxint, 0, pdev->name, ps2if);
  113. if (error) {
  114. dev_err(&pdev->dev, "could not allocate IRQ %d: %d\n",
  115. ps2if->irq, error);
  116. goto err_unmap;
  117. }
  118. dev_info(&pdev->dev, "base %p, irq %d\n", ps2if->base, ps2if->irq);
  119. serio_register_port(ps2if->io);
  120. platform_set_drvdata(pdev, ps2if);
  121. return 0;
  122. err_unmap:
  123. iounmap(ps2if->base);
  124. err_free_res:
  125. release_mem_region(ps2if->iomem_res->start,
  126. resource_size(ps2if->iomem_res));
  127. err_free_mem:
  128. kfree(ps2if);
  129. kfree(serio);
  130. return error;
  131. }
  132. /*
  133. * Remove one device from this driver.
  134. */
  135. static int __devexit altera_ps2_remove(struct platform_device *pdev)
  136. {
  137. struct ps2if *ps2if = platform_get_drvdata(pdev);
  138. platform_set_drvdata(pdev, NULL);
  139. serio_unregister_port(ps2if->io);
  140. free_irq(ps2if->irq, ps2if);
  141. iounmap(ps2if->base);
  142. release_mem_region(ps2if->iomem_res->start,
  143. resource_size(ps2if->iomem_res));
  144. kfree(ps2if);
  145. return 0;
  146. }
  147. #ifdef CONFIG_OF
  148. static const struct of_device_id altera_ps2_match[] = {
  149. { .compatible = "ALTR,ps2-1.0", },
  150. {},
  151. };
  152. MODULE_DEVICE_TABLE(of, altera_ps2_match);
  153. #endif /* CONFIG_OF */
  154. /*
  155. * Our device driver structure
  156. */
  157. static struct platform_driver altera_ps2_driver = {
  158. .probe = altera_ps2_probe,
  159. .remove = __devexit_p(altera_ps2_remove),
  160. .driver = {
  161. .name = DRV_NAME,
  162. .owner = THIS_MODULE,
  163. .of_match_table = of_match_ptr(altera_ps2_match),
  164. },
  165. };
  166. module_platform_driver(altera_ps2_driver);
  167. MODULE_DESCRIPTION("Altera University Program PS2 controller driver");
  168. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  169. MODULE_LICENSE("GPL");
  170. MODULE_ALIAS("platform:" DRV_NAME);