xref: /linux/drivers/input/keyboard/opencores-kbd.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
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 
opencores_kbd_isr(int irq,void * dev_id)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 
opencores_kbd_probe(struct platform_device * pdev)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 	int irq, i, error;
43422b552dSJavier Herrero 
44422b552dSJavier Herrero 	irq = platform_get_irq(pdev, 0);
450bec8b7eSStephen Boyd 	if (irq < 0)
46422b552dSJavier Herrero 		return -EINVAL;
47422b552dSJavier Herrero 
48848d4793SPramod Gurav 	opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd),
49848d4793SPramod Gurav 				     GFP_KERNEL);
50848d4793SPramod Gurav 	if (!opencores_kbd)
51848d4793SPramod Gurav 		return -ENOMEM;
52422b552dSJavier Herrero 
53848d4793SPramod Gurav 	input = devm_input_allocate_device(&pdev->dev);
54848d4793SPramod Gurav 	if (!input) {
55848d4793SPramod Gurav 		dev_err(&pdev->dev, "failed to allocate input device\n");
56848d4793SPramod Gurav 		return -ENOMEM;
57422b552dSJavier Herrero 	}
58422b552dSJavier Herrero 
59422b552dSJavier Herrero 	opencores_kbd->input = input;
60848d4793SPramod Gurav 
61*db984229SYangtao Li 	opencores_kbd->addr = devm_platform_ioremap_resource(pdev, 0);
62848d4793SPramod Gurav 	if (IS_ERR(opencores_kbd->addr))
6342b63e60SDmitry Torokhov 		return PTR_ERR(opencores_kbd->addr);
64422b552dSJavier Herrero 
65422b552dSJavier Herrero 	input->name = pdev->name;
66422b552dSJavier Herrero 	input->phys = "opencores-kbd/input0";
67422b552dSJavier Herrero 
68422b552dSJavier Herrero 	input->id.bustype = BUS_HOST;
69422b552dSJavier Herrero 	input->id.vendor = 0x0001;
70422b552dSJavier Herrero 	input->id.product = 0x0001;
71422b552dSJavier Herrero 	input->id.version = 0x0100;
72422b552dSJavier Herrero 
73422b552dSJavier Herrero 	input->keycode = opencores_kbd->keycodes;
74422b552dSJavier Herrero 	input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
75422b552dSJavier Herrero 	input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
76422b552dSJavier Herrero 
77422b552dSJavier Herrero 	__set_bit(EV_KEY, input->evbit);
78422b552dSJavier Herrero 
79422b552dSJavier Herrero 	for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
80422b552dSJavier Herrero 		/*
81422b552dSJavier Herrero 		 * OpenCores controller happens to have scancodes match
82422b552dSJavier Herrero 		 * our KEY_* definitions.
83422b552dSJavier Herrero 		 */
84422b552dSJavier Herrero 		opencores_kbd->keycodes[i] = i;
85422b552dSJavier Herrero 		__set_bit(opencores_kbd->keycodes[i], input->keybit);
86422b552dSJavier Herrero 	}
87422b552dSJavier Herrero 	__clear_bit(KEY_RESERVED, input->keybit);
88422b552dSJavier Herrero 
89848d4793SPramod Gurav 	error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr,
90848d4793SPramod Gurav 				 IRQF_TRIGGER_RISING,
91848d4793SPramod Gurav 				 pdev->name, opencores_kbd);
92422b552dSJavier Herrero 	if (error) {
93422b552dSJavier Herrero 		dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
94848d4793SPramod Gurav 		return error;
95422b552dSJavier Herrero 	}
96422b552dSJavier Herrero 
97422b552dSJavier Herrero 	error = input_register_device(input);
98422b552dSJavier Herrero 	if (error) {
99422b552dSJavier Herrero 		dev_err(&pdev->dev, "unable to register input device\n");
100422b552dSJavier Herrero 		return error;
101422b552dSJavier Herrero 	}
102422b552dSJavier Herrero 
103422b552dSJavier Herrero 	return 0;
104422b552dSJavier Herrero }
105422b552dSJavier Herrero 
106422b552dSJavier Herrero static struct platform_driver opencores_kbd_device_driver = {
107422b552dSJavier Herrero 	.probe    = opencores_kbd_probe,
108422b552dSJavier Herrero 	.driver   = {
109422b552dSJavier Herrero 		.name = "opencores-kbd",
110422b552dSJavier Herrero 	},
111422b552dSJavier Herrero };
1125146c84fSJJ Ding module_platform_driver(opencores_kbd_device_driver);
113422b552dSJavier Herrero 
114422b552dSJavier Herrero MODULE_LICENSE("GPL");
115422b552dSJavier Herrero MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
116422b552dSJavier Herrero MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");
117