Merge branch 'topic/jack' into for-linus
[pandora-kernel.git] / drivers / input / keyboard / opencores-kbd.c
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         struct resource *addr_res;
22         void __iomem *addr;
23         int irq;
24         unsigned short keycodes[128];
25 };
26
27 static irqreturn_t opencores_kbd_isr(int irq, void *dev_id)
28 {
29         struct opencores_kbd *opencores_kbd = dev_id;
30         struct input_dev *input = opencores_kbd->input;
31         unsigned char c;
32
33         c = readb(opencores_kbd->addr);
34         input_report_key(input, c & 0x7f, c & 0x80 ? 0 : 1);
35         input_sync(input);
36
37         return IRQ_HANDLED;
38 }
39
40 static int __devinit opencores_kbd_probe(struct platform_device *pdev)
41 {
42         struct input_dev *input;
43         struct opencores_kbd *opencores_kbd;
44         struct resource *res;
45         int irq, i, error;
46
47         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
48         if (!res) {
49                 dev_err(&pdev->dev, "missing board memory resource\n");
50                 return -EINVAL;
51         }
52
53         irq = platform_get_irq(pdev, 0);
54         if (irq < 0) {
55                 dev_err(&pdev->dev, "missing board IRQ resource\n");
56                 return -EINVAL;
57         }
58
59         opencores_kbd = kzalloc(sizeof(*opencores_kbd), GFP_KERNEL);
60         input = input_allocate_device();
61         if (!opencores_kbd || !input) {
62                 dev_err(&pdev->dev, "failed to allocate device structures\n");
63                 error = -ENOMEM;
64                 goto err_free_mem;
65         }
66
67         opencores_kbd->addr_res = res;
68         res = request_mem_region(res->start, resource_size(res), pdev->name);
69         if (!res) {
70                 dev_err(&pdev->dev, "failed to request I/O memory\n");
71                 error = -EBUSY;
72                 goto err_free_mem;
73         }
74
75         opencores_kbd->addr = ioremap(res->start, resource_size(res));
76         if (!opencores_kbd->addr) {
77                 dev_err(&pdev->dev, "failed to remap I/O memory\n");
78                 error = -ENXIO;
79                 goto err_rel_mem;
80         }
81
82         opencores_kbd->input = input;
83         opencores_kbd->irq = irq;
84
85         input->name = pdev->name;
86         input->phys = "opencores-kbd/input0";
87         input->dev.parent = &pdev->dev;
88
89         input_set_drvdata(input, opencores_kbd);
90
91         input->id.bustype = BUS_HOST;
92         input->id.vendor = 0x0001;
93         input->id.product = 0x0001;
94         input->id.version = 0x0100;
95
96         input->keycode = opencores_kbd->keycodes;
97         input->keycodesize = sizeof(opencores_kbd->keycodes[0]);
98         input->keycodemax = ARRAY_SIZE(opencores_kbd->keycodes);
99
100         __set_bit(EV_KEY, input->evbit);
101
102         for (i = 0; i < ARRAY_SIZE(opencores_kbd->keycodes); i++) {
103                 /*
104                  * OpenCores controller happens to have scancodes match
105                  * our KEY_* definitions.
106                  */
107                 opencores_kbd->keycodes[i] = i;
108                 __set_bit(opencores_kbd->keycodes[i], input->keybit);
109         }
110         __clear_bit(KEY_RESERVED, input->keybit);
111
112         error = request_irq(irq, &opencores_kbd_isr,
113                             IRQF_TRIGGER_RISING, pdev->name, opencores_kbd);
114         if (error) {
115                 dev_err(&pdev->dev, "unable to claim irq %d\n", irq);
116                 goto err_unmap_mem;
117         }
118
119         error = input_register_device(input);
120         if (error) {
121                 dev_err(&pdev->dev, "unable to register input device\n");
122                 goto err_free_irq;
123         }
124
125         platform_set_drvdata(pdev, opencores_kbd);
126
127         return 0;
128
129  err_free_irq:
130         free_irq(irq, opencores_kbd);
131  err_unmap_mem:
132         iounmap(opencores_kbd->addr);
133  err_rel_mem:
134         release_mem_region(res->start, resource_size(res));
135  err_free_mem:
136         input_free_device(input);
137         kfree(opencores_kbd);
138
139         return error;
140 }
141
142 static int __devexit opencores_kbd_remove(struct platform_device *pdev)
143 {
144         struct opencores_kbd *opencores_kbd = platform_get_drvdata(pdev);
145
146         free_irq(opencores_kbd->irq, opencores_kbd);
147
148         iounmap(opencores_kbd->addr);
149         release_mem_region(opencores_kbd->addr_res->start,
150                 resource_size(opencores_kbd->addr_res));
151         input_unregister_device(opencores_kbd->input);
152         kfree(opencores_kbd);
153
154         platform_set_drvdata(pdev, NULL);
155
156         return 0;
157 }
158
159 static struct platform_driver opencores_kbd_device_driver = {
160         .probe    = opencores_kbd_probe,
161         .remove   = __devexit_p(opencores_kbd_remove),
162         .driver   = {
163                 .name = "opencores-kbd",
164         },
165 };
166
167 static int __init opencores_kbd_init(void)
168 {
169         return platform_driver_register(&opencores_kbd_device_driver);
170 }
171 module_init(opencores_kbd_init);
172
173 static void __exit opencores_kbd_exit(void)
174 {
175         platform_driver_unregister(&opencores_kbd_device_driver);
176 }
177 module_exit(opencores_kbd_exit);
178
179 MODULE_LICENSE("GPL");
180 MODULE_AUTHOR("Javier Herrero <jherrero@hvsistemas.es>");
181 MODULE_DESCRIPTION("Keyboard driver for OpenCores Keyboard Controller");