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 	int irq, i, error;
43 
44 	irq = platform_get_irq(pdev, 0);
45 	if (irq < 0)
46 		return -EINVAL;
47 
48 	opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd),
49 				     GFP_KERNEL);
50 	if (!opencores_kbd)
51 		return -ENOMEM;
52 
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 
59 	opencores_kbd->input = input;
60 
61 	opencores_kbd->addr = devm_platform_ioremap_resource(pdev, 0);
62 	if (IS_ERR(opencores_kbd->addr))
63 		return PTR_ERR(opencores_kbd->addr);
64 
65 	input->name = pdev->name;
66 	input->phys = "opencores-kbd/input0";
67 
68 	input->id.bustype = BUS_HOST;
69 	input->id.vendor = 0x0001;
70 	input->id.product = 0x0001;
71 	input->id.version = 0x0100;
72 
73 	input->keycode = opencores_kbd->keycodes;
74 	input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
75 	input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
76 
77 	__set_bit(EV_KEY, input->evbit);
78 
79 	for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
80 		/*
81 		 * OpenCores controller happens to have scancodes match
82 		 * our KEY_* definitions.
83 		 */
84 		opencores_kbd->keycodes[i] = i;
85 		__set_bit(opencores_kbd->keycodes[i], input->keybit);
86 	}
87 	__clear_bit(KEY_RESERVED, input->keybit);
88 
89 	error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr,
90 				 IRQF_TRIGGER_RISING,
91 				 pdev->name, opencores_kbd);
92 	if (error) {
93 		dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
94 		return error;
95 	}
96 
97 	error = input_register_device(input);
98 	if (error) {
99 		dev_err(&pdev->dev, "unable to register input device\n");
100 		return error;
101 	}
102 
103 	return 0;
104 }
105 
106 static struct platform_driver opencores_kbd_device_driver = {
107 	.probe    = opencores_kbd_probe,
108 	.driver   = {
109 		.name = "opencores-kbd",
110 	},
111 };
112 module_platform_driver(opencores_kbd_device_driver);
113 
114 MODULE_LICENSE("GPL");
115 MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
116 MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
117