blob: f8502bb291767c539d1a204ed34bc09d3e90c4b0 [file] [log] [blame]
Javier Herrero422b5522009-09-16 01:06:42 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Javier Herrero422b5522009-09-16 01:06:42 -070018
19struct opencores_kbd {
20 struct input_dev *input;
Javier Herrero422b5522009-09-16 01:06:42 -070021 void __iomem *addr;
22 int irq;
23 unsigned short keycodes[128];
24};
25
26static 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
Bill Pemberton5298cc42012-11-23 21:38:25 -080039static int opencores_kbd_probe(struct platform_device *pdev)
Javier Herrero422b5522009-09-16 01:06:42 -070040{
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
Pramod Gurav848d4792014-10-07 09:06:31 -070058 opencores_kbd = devm_kzalloc(&pdev->dev, sizeof(*opencores_kbd),
59 GFP_KERNEL);
60 if (!opencores_kbd)
61 return -ENOMEM;
Javier Herrero422b5522009-09-16 01:06:42 -070062
Pramod Gurav848d4792014-10-07 09:06:31 -070063 input = devm_input_allocate_device(&pdev->dev);
64 if (!input) {
65 dev_err(&pdev->dev, "failed to allocate input device\n");
66 return -ENOMEM;
Javier Herrero422b5522009-09-16 01:06:42 -070067 }
68
69 opencores_kbd->input = input;
Pramod Gurav848d4792014-10-07 09:06:31 -070070
71 opencores_kbd->addr = devm_ioremap_resource(&pdev->dev, res);
72 if (IS_ERR(opencores_kbd->addr))
Dmitry Torokhov42b63e62014-10-24 14:27:25 -070073 return PTR_ERR(opencores_kbd->addr);
Javier Herrero422b5522009-09-16 01:06:42 -070074
75 input->name = pdev->name;
76 input->phys = "opencores-kbd/input0";
Javier Herrero422b5522009-09-16 01:06:42 -070077
78 input_set_drvdata(input, opencores_kbd);
79
80 input->id.bustype = BUS_HOST;
81 input->id.vendor = 0x0001;
82 input->id.product = 0x0001;
83 input->id.version = 0x0100;
84
85 input->keycode = opencores_kbd->keycodes;
86 input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
87 input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
88
89 __set_bit(EV_KEY, input->evbit);
90
91 for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
92 /*
93 * OpenCores controller happens to have scancodes match
94 * our KEY_* definitions.
95 */
96 opencores_kbd->keycodes[i] = i;
97 __set_bit(opencores_kbd->keycodes[i], input->keybit);
98 }
99 __clear_bit(KEY_RESERVED, input->keybit);
100
Pramod Gurav848d4792014-10-07 09:06:31 -0700101 error = devm_request_irq(&pdev->dev, irq, &opencores_kbd_isr,
102 IRQF_TRIGGER_RISING,
103 pdev->name, opencores_kbd);
Javier Herrero422b5522009-09-16 01:06:42 -0700104 if (error) {
105 dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
Pramod Gurav848d4792014-10-07 09:06:31 -0700106 return error;
Javier Herrero422b5522009-09-16 01:06:42 -0700107 }
108
109 error = input_register_device(input);
110 if (error) {
111 dev_err(&pdev->dev, "unable to register input device\n");
Pramod Gurav848d4792014-10-07 09:06:31 -0700112 return error;
Javier Herrero422b5522009-09-16 01:06:42 -0700113 }
114
115 platform_set_drvdata(pdev, opencores_kbd);
116
117 return 0;
Javier Herrero422b5522009-09-16 01:06:42 -0700118}
119
120static struct platform_driver opencores_kbd_device_driver = {
121 .probe = opencores_kbd_probe,
Javier Herrero422b5522009-09-16 01:06:42 -0700122 .driver = {
123 .name = "opencores-kbd",
124 },
125};
JJ Ding5146c842011-11-29 11:08:39 -0800126module_platform_driver(opencores_kbd_device_driver);
Javier Herrero422b5522009-09-16 01:06:42 -0700127
128MODULE_LICENSE("GPL");
129MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
130MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");