altera_ps2.c 3.6 KB

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