Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / keyboard / opencores-kbd.c
blobb0ea387414c1e2069585842b21293151bbeba389
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OpenCores Keyboard Controller Driver
4 * http://www.opencores.org/project,keyboardcontroller
6 * Copyright 2007-2009 HV Sistemas S.L.
7 */
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>
18 struct opencores_kbd {
19 struct input_dev *input;
20 void __iomem *addr;
21 int irq;
22 unsigned short keycodes[128];
25 static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
27 struct opencores_kbd *opencores_kbd = dev_id;
28 struct input_dev *input = opencores_kbd->input;
29 unsigned char c;
31 c = readb(opencores_kbd->addr);
32 input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
33 input_sync(input);
35 return IRQ_HANDLED;
38 static int opencores_kbd_probe(struct platform_device *pdev)
40 struct input_dev *input;
41 struct opencores_kbd *opencores_kbd;
42 struct resource *res;
43 int irq, i, error;
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;
51 irq = platform_get_irq(pdev, 0);
52 if (irq < 0)
53 return -EINVAL;
55 opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd),
56 GFP_KERNEL);
57 if (!opencores_kbd)
58 return -ENOMEM;
60 input = devm_input_allocate_device(&pdev->dev);
61 if (!input) {
62 dev_err(&pdev->dev, "failed to allocate input device\n");
63 return -ENOMEM;
66 opencores_kbd->input = input;
68 opencores_kbd->addr = devm_ioremap_resource(&pdev->dev, res);
69 if (IS_ERR(opencores_kbd->addr))
70 return PTR_ERR(opencores_kbd->addr);
72 input->name = pdev->name;
73 input->phys = "opencores-kbd/input0";
75 input->id.bustype = BUS_HOST;
76 input->id.vendor = 0x0001;
77 input->id.product = 0x0001;
78 input->id.version = 0x0100;
80 input->keycode = opencores_kbd->keycodes;
81 input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
82 input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
84 __set_bit(EV_KEY, input->evbit);
86 for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
88 * OpenCores controller happens to have scancodes match
89 * our KEY_* definitions.
91 opencores_kbd->keycodes[i] = i;
92 __set_bit(opencores_kbd->keycodes[i], input->keybit);
94 __clear_bit(KEY_RESERVED, input->keybit);
96 error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr,
97 IRQF_TRIGGER_RISING,
98 pdev->name, opencores_kbd);
99 if (error) {
100 dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
101 return error;
104 error = input_register_device(input);
105 if (error) {
106 dev_err(&pdev->dev, "unable to register input device\n");
107 return error;
110 return 0;
113 static struct platform_driver opencores_kbd_device_driver = {
114 .probe = opencores_kbd_probe,
115 .driver = {
116 .name = "opencores-kbd",
119 module_platform_driver(opencores_kbd_device_driver);
121 MODULE_LICENSE("GPL");
122 MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
123 MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");