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