altera_ps2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 serio *serio;
  68. int error, irq;
  69. ps2if = devm_kzalloc(&pdev->dev, sizeof(*ps2if), GFP_KERNEL);
  70. if (!ps2if)
  71. return -ENOMEM;
  72. ps2if->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  73. if (IS_ERR(ps2if->base))
  74. return PTR_ERR(ps2if->base);
  75. irq = platform_get_irq(pdev, 0);
  76. if (irq < 0)
  77. return -ENXIO;
  78. error = devm_request_irq(&pdev->dev, irq, altera_ps2_rxint, 0,
  79. pdev->name, ps2if);
  80. if (error) {
  81. dev_err(&pdev->dev, "could not request IRQ %d\n", irq);
  82. return error;
  83. }
  84. serio = kzalloc_obj(*serio);
  85. if (!serio)
  86. return -ENOMEM;
  87. serio->id.type = SERIO_8042;
  88. serio->write = altera_ps2_write;
  89. serio->open = altera_ps2_open;
  90. serio->close = altera_ps2_close;
  91. strscpy(serio->name, dev_name(&pdev->dev), sizeof(serio->name));
  92. strscpy(serio->phys, dev_name(&pdev->dev), sizeof(serio->phys));
  93. serio->port_data = ps2if;
  94. serio->dev.parent = &pdev->dev;
  95. ps2if->io = serio;
  96. dev_info(&pdev->dev, "base %p, irq %d\n", ps2if->base, irq);
  97. serio_register_port(ps2if->io);
  98. platform_set_drvdata(pdev, ps2if);
  99. return 0;
  100. }
  101. /*
  102. * Remove one device from this driver.
  103. */
  104. static void altera_ps2_remove(struct platform_device *pdev)
  105. {
  106. struct ps2if *ps2if = platform_get_drvdata(pdev);
  107. serio_unregister_port(ps2if->io);
  108. }
  109. #ifdef CONFIG_OF
  110. static const struct of_device_id altera_ps2_match[] = {
  111. { .compatible = "ALTR,ps2-1.0", },
  112. { .compatible = "altr,ps2-1.0", },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, altera_ps2_match);
  116. #endif /* CONFIG_OF */
  117. /*
  118. * Our device driver structure
  119. */
  120. static struct platform_driver altera_ps2_driver = {
  121. .probe = altera_ps2_probe,
  122. .remove = altera_ps2_remove,
  123. .driver = {
  124. .name = DRV_NAME,
  125. .of_match_table = of_match_ptr(altera_ps2_match),
  126. },
  127. };
  128. module_platform_driver(altera_ps2_driver);
  129. MODULE_DESCRIPTION("Altera University Program PS2 controller driver");
  130. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  131. MODULE_LICENSE("GPL");
  132. MODULE_ALIAS("platform:" DRV_NAME);