xref: /linux/drivers/input/keyboard/opencores-kbd.c (revision 0bec8b7e5ca1a629f26173691526432f9d7cf8c1)
180503b23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2422b552dSJavier Herrero /*
3422b552dSJavier Herrero  * OpenCores Keyboard Controller Driver
4422b552dSJavier Herrero  * http://www.opencores.org/project,keyboardcontroller
5422b552dSJavier Herrero  *
6422b552dSJavier Herrero  * Copyright 2007-2009 HV Sistemas S.L.
7422b552dSJavier Herrero  */
8422b552dSJavier Herrero 
9422b552dSJavier Herrero #include <linux/input.h>
10422b552dSJavier Herrero #include <linux/interrupt.h>
11422b552dSJavier Herrero #include <linux/io.h>
12422b552dSJavier Herrero #include <linux/ioport.h>
13422b552dSJavier Herrero #include <linux/kernel.h>
14422b552dSJavier Herrero #include <linux/module.h>
15422b552dSJavier Herrero #include <linux/platform_device.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
17422b552dSJavier Herrero 
18422b552dSJavier Herrero struct opencores_kbd {
19422b552dSJavier Herrero 	struct input_dev *input;
20422b552dSJavier Herrero 	void __iomem *addr;
21422b552dSJavier Herrero 	int irq;
22422b552dSJavier Herrero 	unsigned short keycodes[128];
23422b552dSJavier Herrero };
24422b552dSJavier Herrero 
25422b552dSJavier Herrero static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
26422b552dSJavier Herrero {
27422b552dSJavier Herrero 	struct opencores_kbd *opencores_kbd = dev_id;
28422b552dSJavier Herrero 	struct input_dev *input = opencores_kbd->input;
29422b552dSJavier Herrero 	unsigned char c;
30422b552dSJavier Herrero 
31422b552dSJavier Herrero 	c = readb(opencores_kbd->addr);
32422b552dSJavier Herrero 	input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
33422b552dSJavier Herrero 	input_sync(input);
34422b552dSJavier Herrero 
35422b552dSJavier Herrero 	return IRQ_HANDLED;
36422b552dSJavier Herrero }
37422b552dSJavier Herrero 
385298cc4cSBill Pemberton static int opencores_kbd_probe(struct platform_device *pdev)
39422b552dSJavier Herrero {
40422b552dSJavier Herrero 	struct input_dev *input;
41422b552dSJavier Herrero 	struct opencores_kbd *opencores_kbd;
42422b552dSJavier Herrero 	struct resource *res;
43422b552dSJavier Herrero 	int irq, i, error;
44422b552dSJavier Herrero 
45422b552dSJavier Herrero 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
46422b552dSJavier Herrero 	if (!res) {
47422b552dSJavier Herrero 		dev_err(&pdev->dev, "missing board memory resource\n");
48422b552dSJavier Herrero 		return -EINVAL;
49422b552dSJavier Herrero 	}
50422b552dSJavier Herrero 
51422b552dSJavier Herrero 	irq = platform_get_irq(pdev, 0);
52*0bec8b7eSStephen Boyd 	if (irq < 0)
53422b552dSJavier Herrero 		return -EINVAL;
54422b552dSJavier Herrero 
55848d4793SPramod Gurav 	opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd),
56848d4793SPramod Gurav 				     GFP_KERNEL);
57848d4793SPramod Gurav 	if (!opencores_kbd)
58848d4793SPramod Gurav 		return -ENOMEM;
59422b552dSJavier Herrero 
60848d4793SPramod Gurav 	input = devm_input_allocate_device(&pdev->dev);
61848d4793SPramod Gurav 	if (!input) {
62848d4793SPramod Gurav 		dev_err(&pdev->dev, "failed to allocate input device\n");
63848d4793SPramod Gurav 		return -ENOMEM;
64422b552dSJavier Herrero 	}
65422b552dSJavier Herrero 
66422b552dSJavier Herrero 	opencores_kbd->input = input;
67848d4793SPramod Gurav 
68848d4793SPramod Gurav 	opencores_kbd->addr = devm_ioremap_resource(&pdev->dev, res);
69848d4793SPramod Gurav 	if (IS_ERR(opencores_kbd->addr))
7042b63e60SDmitry Torokhov 		return PTR_ERR(opencores_kbd->addr);
71422b552dSJavier Herrero 
72422b552dSJavier Herrero 	input->name = pdev->name;
73422b552dSJavier Herrero 	input->phys = "opencores-kbd/input0";
74422b552dSJavier Herrero 
75422b552dSJavier Herrero 	input->id.bustype = BUS_HOST;
76422b552dSJavier Herrero 	input->id.vendor = 0x0001;
77422b552dSJavier Herrero 	input->id.product = 0x0001;
78422b552dSJavier Herrero 	input->id.version = 0x0100;
79422b552dSJavier Herrero 
80422b552dSJavier Herrero 	input->keycode = opencores_kbd->keycodes;
81422b552dSJavier Herrero 	input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
82422b552dSJavier Herrero 	input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
83422b552dSJavier Herrero 
84422b552dSJavier Herrero 	__set_bit(EV_KEY, input->evbit);
85422b552dSJavier Herrero 
86422b552dSJavier Herrero 	for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
87422b552dSJavier Herrero 		/*
88422b552dSJavier Herrero 		 * OpenCores controller happens to have scancodes match
89422b552dSJavier Herrero 		 * our KEY_* definitions.
90422b552dSJavier Herrero 		 */
91422b552dSJavier Herrero 		opencores_kbd->keycodes[i] = i;
92422b552dSJavier Herrero 		__set_bit(opencores_kbd->keycodes[i], input->keybit);
93422b552dSJavier Herrero 	}
94422b552dSJavier Herrero 	__clear_bit(KEY_RESERVED, input->keybit);
95422b552dSJavier Herrero 
96848d4793SPramod Gurav 	error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr,
97848d4793SPramod Gurav 				 IRQF_TRIGGER_RISING,
98848d4793SPramod Gurav 				 pdev->name, opencores_kbd);
99422b552dSJavier Herrero 	if (error) {
100422b552dSJavier Herrero 		dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
101848d4793SPramod Gurav 		return error;
102422b552dSJavier Herrero 	}
103422b552dSJavier Herrero 
104422b552dSJavier Herrero 	error = input_register_device(input);
105422b552dSJavier Herrero 	if (error) {
106422b552dSJavier Herrero 		dev_err(&pdev->dev, "unable to register input device\n");
107422b552dSJavier Herrero 		return error;
108422b552dSJavier Herrero 	}
109422b552dSJavier Herrero 
110422b552dSJavier Herrero 	return 0;
111422b552dSJavier Herrero }
112422b552dSJavier Herrero 
113422b552dSJavier Herrero static struct platform_driver opencores_kbd_device_driver = {
114422b552dSJavier Herrero 	.probe    = opencores_kbd_probe,
115422b552dSJavier Herrero 	.driver   = {
116422b552dSJavier Herrero 		.name = "opencores-kbd",
117422b552dSJavier Herrero 	},
118422b552dSJavier Herrero };
1195146c84fSJJ Ding module_platform_driver(opencores_kbd_device_driver);
120422b552dSJavier Herrero 
121422b552dSJavier Herrero MODULE_LICENSE("GPL");
122422b552dSJavier Herrero MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
123422b552dSJavier Herrero MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
124