opencores-kbd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * OpenCores Keyboard Controller Driver
  3. * http://www.opencores.org/project,keyboardcontroller
  4. *
  5. * Copyright 2007-2009 HV Sistemas S.L.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/input.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/ioport.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. struct opencores_kbd {
  18. struct input_dev *input;
  19. void __iomem *addr;
  20. int irq;
  21. unsigned short keycodes[128];
  22. };
  23. static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
  24. {
  25. struct opencores_kbd *opencores_kbd = dev_id;
  26. struct input_dev *input = opencores_kbd->input;
  27. unsigned char c;
  28. c = readb(opencores_kbd->addr);
  29. input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
  30. input_sync(input);
  31. return IRQ_HANDLED;
  32. }
  33. static int opencores_kbd_probe(struct platform_device *pdev)
  34. {
  35. struct input_dev *input;
  36. struct opencores_kbd *opencores_kbd;
  37. struct resource *res;
  38. int irq, i, error;
  39. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  40. if (!res) {
  41. dev_err(&pdev->dev, "missing board memory resource\n");
  42. return -EINVAL;
  43. }
  44. irq = platform_get_irq(pdev, 0);
  45. if (irq < 0) {
  46. dev_err(&pdev->dev, "missing board IRQ resource\n");
  47. return -EINVAL;
  48. }
  49. opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd),
  50. GFP_KERNEL);
  51. if (!opencores_kbd)
  52. return -ENOMEM;
  53. input = devm_input_allocate_device(&pdev->dev);
  54. if (!input) {
  55. dev_err(&pdev->dev, "failed to allocate input device\n");
  56. return -ENOMEM;
  57. }
  58. opencores_kbd->input = input;
  59. opencores_kbd->addr = devm_ioremap_resource(&pdev->dev, res);
  60. if (IS_ERR(opencores_kbd->addr))
  61. return PTR_ERR(opencores_kbd->addr);
  62. input->name = pdev->name;
  63. input->phys = "opencores-kbd/input0";
  64. input->id.bustype = BUS_HOST;
  65. input->id.vendor = 0x0001;
  66. input->id.product = 0x0001;
  67. input->id.version = 0x0100;
  68. input->keycode = opencores_kbd->keycodes;
  69. input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
  70. input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
  71. __set_bit(EV_KEY, input->evbit);
  72. for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
  73. /*
  74. * OpenCores controller happens to have scancodes match
  75. * our KEY_* definitions.
  76. */
  77. opencores_kbd->keycodes[i] = i;
  78. __set_bit(opencores_kbd->keycodes[i], input->keybit);
  79. }
  80. __clear_bit(KEY_RESERVED, input->keybit);
  81. error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr,
  82. IRQF_TRIGGER_RISING,
  83. pdev->name, opencores_kbd);
  84. if (error) {
  85. dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
  86. return error;
  87. }
  88. error = input_register_device(input);
  89. if (error) {
  90. dev_err(&pdev->dev, "unable to register input device\n");
  91. return error;
  92. }
  93. return 0;
  94. }
  95. static struct platform_driver opencores_kbd_device_driver = {
  96. .probe = opencores_kbd_probe,
  97. .driver = {
  98. .name = "opencores-kbd",
  99. },
  100. };
  101. module_platform_driver(opencores_kbd_device_driver);
  102. MODULE_LICENSE("GPL");
  103. MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
  104. MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");