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