altera_ps2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/input.h>
  15. #include <linux/serio.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #define DRV_NAME "altera_ps2"
  22. struct ps2if {
  23. struct serio *io;
  24. void __iomem *base;
  25. };
  26. /*
  27. * Read all bytes waiting in the PS2 port. There should be
  28. * at the most one, but we loop for safety.
  29. */
  30. static irqreturn_t altera_ps2_rxint(int irq, void *dev_id)
  31. {
  32. struct ps2if *ps2if = dev_id;
  33. unsigned int status;
  34. irqreturn_t handled = IRQ_NONE;
  35. while ((status = readl(ps2if->base)) & 0xffff0000) {
  36. serio_interrupt(ps2if->io, status & 0xff, 0);
  37. handled = IRQ_HANDLED;
  38. }
  39. return handled;
  40. }
  41. /*
  42. * Write a byte to the PS2 port.
  43. */
  44. static int altera_ps2_write(struct serio *io, unsigned char val)
  45. {
  46. struct ps2if *ps2if = io->port_data;
  47. writel(val, ps2if->base);
  48. return 0;
  49. }
  50. static int altera_ps2_open(struct serio *io)
  51. {
  52. struct ps2if *ps2if = io->port_data;
  53. /* clear fifo */
  54. while (readl(ps2if->base) & 0xffff0000)
  55. /* empty */;
  56. writel(1, ps2if->base + 4); /* enable rx irq */
  57. return 0;
  58. }
  59. static void altera_ps2_close(struct serio *io)
  60. {
  61. struct ps2if *ps2if = io->port_data;
  62. writel(0, ps2if->base + 4); /* disable rx irq */
  63. }
  64. /*
  65. * Add one device to this driver.
  66. */
  67. static int altera_ps2_probe(struct platform_device *pdev)
  68. {
  69. struct ps2if *ps2if;
  70. struct resource *res;
  71. struct serio *serio;
  72. int error, irq;
  73. ps2if = devm_kzalloc(&pdev->dev, sizeof(struct ps2if), GFP_KERNEL);
  74. if (!ps2if)
  75. return -ENOMEM;
  76. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  77. ps2if->base = devm_ioremap_resource(&pdev->dev, res);
  78. if (IS_ERR(ps2if->base))
  79. return PTR_ERR(ps2if->base);
  80. irq = platform_get_irq(pdev, 0);
  81. if (irq < 0)
  82. return -ENXIO;
  83. error = devm_request_irq(&pdev->dev, irq, altera_ps2_rxint, 0,
  84. pdev->name, ps2if);
  85. if (error) {
  86. dev_err(&pdev->dev, "could not request IRQ %d\n", irq);
  87. return error;
  88. }
  89. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  90. if (!serio)
  91. return -ENOMEM;
  92. serio->id.type = SERIO_8042;
  93. serio->write = altera_ps2_write;
  94. serio->open = altera_ps2_open;
  95. serio->close = altera_ps2_close;
  96. strlcpy(serio->name, dev_name(&pdev->dev), sizeof(serio->name));
  97. strlcpy(serio->phys, dev_name(&pdev->dev), sizeof(serio->phys));
  98. serio->port_data = ps2if;
  99. serio->dev.parent = &pdev->dev;
  100. ps2if->io = serio;
  101. dev_info(&pdev->dev, "base %p, irq %d\n", ps2if->base, irq);
  102. serio_register_port(ps2if->io);
  103. platform_set_drvdata(pdev, ps2if);
  104. return 0;
  105. }
  106. /*
  107. * Remove one device from this driver.
  108. */
  109. static int altera_ps2_remove(struct platform_device *pdev)
  110. {
  111. struct ps2if *ps2if = platform_get_drvdata(pdev);
  112. serio_unregister_port(ps2if->io);
  113. return 0;
  114. }
  115. #ifdef CONFIG_OF
  116. static const struct of_device_id altera_ps2_match[] = {
  117. { .compatible = "ALTR,ps2-1.0", },
  118. { .compatible = "altr,ps2-1.0", },
  119. {},
  120. };
  121. MODULE_DEVICE_TABLE(of, altera_ps2_match);
  122. #endif /* CONFIG_OF */
  123. /*
  124. * Our device driver structure
  125. */
  126. static struct platform_driver altera_ps2_driver = {
  127. .probe = altera_ps2_probe,
  128. .remove = altera_ps2_remove,
  129. .driver = {
  130. .name = DRV_NAME,
  131. .of_match_table = of_match_ptr(altera_ps2_match),
  132. },
  133. };
  134. module_platform_driver(altera_ps2_driver);
  135. MODULE_DESCRIPTION("Altera University Program PS2 controller driver");
  136. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  137. MODULE_LICENSE("GPL");
  138. MODULE_ALIAS("platform:" DRV_NAME);