blob: 420e33c49e58c7251f00023ceb47914ed37e8c69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2000-2001 Vojtech Pavlik
3 *
4 * Based on the work of:
5 * Hamish Macdonald
6 */
7
8/*
9 * Amiga keyboard driver for Linux/m68k
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/input.h>
31#include <linux/delay.h>
32#include <linux/interrupt.h>
Roman Zippel5fa28ea2006-06-23 02:04:56 -070033#include <linux/keyboard.h>
Geert Uytterhoeven5121c712009-04-05 13:10:56 +020034#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/amigaints.h>
37#include <asm/amigahw.h>
38#include <asm/irq.h>
39
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION("Amiga keyboard driver");
42MODULE_LICENSE("GPL");
43
Geert Uytterhoeven545e6252014-12-03 14:52:58 -080044#ifdef CONFIG_HW_CONSOLE
Roman Zippel5fa28ea2006-06-23 02:04:56 -070045static unsigned char amikbd_keycode[0x78] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 [0] = KEY_GRAVE,
47 [1] = KEY_1,
48 [2] = KEY_2,
49 [3] = KEY_3,
50 [4] = KEY_4,
51 [5] = KEY_5,
52 [6] = KEY_6,
53 [7] = KEY_7,
54 [8] = KEY_8,
55 [9] = KEY_9,
56 [10] = KEY_0,
57 [11] = KEY_MINUS,
58 [12] = KEY_EQUAL,
59 [13] = KEY_BACKSLASH,
60 [15] = KEY_KP0,
61 [16] = KEY_Q,
62 [17] = KEY_W,
63 [18] = KEY_E,
64 [19] = KEY_R,
65 [20] = KEY_T,
66 [21] = KEY_Y,
67 [22] = KEY_U,
68 [23] = KEY_I,
69 [24] = KEY_O,
70 [25] = KEY_P,
71 [26] = KEY_LEFTBRACE,
72 [27] = KEY_RIGHTBRACE,
73 [29] = KEY_KP1,
74 [30] = KEY_KP2,
75 [31] = KEY_KP3,
76 [32] = KEY_A,
77 [33] = KEY_S,
78 [34] = KEY_D,
79 [35] = KEY_F,
80 [36] = KEY_G,
81 [37] = KEY_H,
82 [38] = KEY_J,
83 [39] = KEY_K,
84 [40] = KEY_L,
85 [41] = KEY_SEMICOLON,
86 [42] = KEY_APOSTROPHE,
87 [43] = KEY_BACKSLASH,
88 [45] = KEY_KP4,
89 [46] = KEY_KP5,
90 [47] = KEY_KP6,
91 [48] = KEY_102ND,
92 [49] = KEY_Z,
93 [50] = KEY_X,
94 [51] = KEY_C,
95 [52] = KEY_V,
96 [53] = KEY_B,
97 [54] = KEY_N,
98 [55] = KEY_M,
99 [56] = KEY_COMMA,
100 [57] = KEY_DOT,
101 [58] = KEY_SLASH,
102 [60] = KEY_KPDOT,
103 [61] = KEY_KP7,
104 [62] = KEY_KP8,
105 [63] = KEY_KP9,
106 [64] = KEY_SPACE,
107 [65] = KEY_BACKSPACE,
108 [66] = KEY_TAB,
109 [67] = KEY_KPENTER,
110 [68] = KEY_ENTER,
111 [69] = KEY_ESC,
112 [70] = KEY_DELETE,
113 [74] = KEY_KPMINUS,
114 [76] = KEY_UP,
115 [77] = KEY_DOWN,
116 [78] = KEY_RIGHT,
117 [79] = KEY_LEFT,
118 [80] = KEY_F1,
119 [81] = KEY_F2,
120 [82] = KEY_F3,
121 [83] = KEY_F4,
122 [84] = KEY_F5,
123 [85] = KEY_F6,
124 [86] = KEY_F7,
125 [87] = KEY_F8,
126 [88] = KEY_F9,
127 [89] = KEY_F10,
128 [90] = KEY_KPLEFTPAREN,
129 [91] = KEY_KPRIGHTPAREN,
130 [92] = KEY_KPSLASH,
131 [93] = KEY_KPASTERISK,
132 [94] = KEY_KPPLUS,
133 [95] = KEY_HELP,
134 [96] = KEY_LEFTSHIFT,
135 [97] = KEY_RIGHTSHIFT,
136 [98] = KEY_CAPSLOCK,
137 [99] = KEY_LEFTCTRL,
138 [100] = KEY_LEFTALT,
139 [101] = KEY_RIGHTALT,
140 [102] = KEY_LEFTMETA,
141 [103] = KEY_RIGHTMETA
142};
143
Geert Uytterhoeven545e6252014-12-03 14:52:58 -0800144static void __init amikbd_init_console_keymaps(void)
145{
Geert Uytterhoevene147af42014-12-03 14:59:49 -0800146 /* We can spare 512 bytes on stack for temp_map in init path. */
147 unsigned short temp_map[NR_KEYS];
Geert Uytterhoeven545e6252014-12-03 14:52:58 -0800148 int i, j;
149
150 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
Geert Uytterhoeven545e6252014-12-03 14:52:58 -0800151 if (!key_maps[i])
152 continue;
153 memset(temp_map, 0, sizeof(temp_map));
154 for (j = 0; j < 0x78; j++) {
155 if (!amikbd_keycode[j])
156 continue;
157 temp_map[j] = key_maps[i][amikbd_keycode[j]];
158 }
159 for (j = 0; j < NR_KEYS; j++) {
160 if (!temp_map[j])
161 temp_map[j] = 0xf200;
162 }
163 memcpy(key_maps[i], temp_map, sizeof(temp_map));
164 }
165}
166#else /* !CONFIG_HW_CONSOLE */
167static inline void amikbd_init_console_keymaps(void) {}
168#endif /* !CONFIG_HW_CONSOLE */
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static const char *amikbd_messages[8] = {
171 [0] = KERN_ALERT "amikbd: Ctrl-Amiga-Amiga reset warning!!\n",
172 [1] = KERN_WARNING "amikbd: keyboard lost sync\n",
173 [2] = KERN_WARNING "amikbd: keyboard buffer overflow\n",
174 [3] = KERN_WARNING "amikbd: keyboard controller failure\n",
175 [4] = KERN_ERR "amikbd: keyboard selftest failure\n",
176 [5] = KERN_INFO "amikbd: initiate power-up key stream\n",
177 [6] = KERN_INFO "amikbd: terminate power-up key stream\n",
178 [7] = KERN_WARNING "amikbd: keyboard interrupt\n"
179};
180
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200181static irqreturn_t amikbd_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200183 struct input_dev *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned char scancode, down;
185
186 scancode = ~ciaa.sdr; /* get and invert scancode (keyboard is active low) */
187 ciaa.cra |= 0x40; /* switch SP pin to output for handshake */
188 udelay(85); /* wait until 85 us have expired */
189 ciaa.cra &= ~0x40; /* switch CIA serial port to input mode */
190
191 down = !(scancode & 1); /* lowest bit is release bit */
192 scancode >>= 1;
193
194 if (scancode < 0x78) { /* scancodes < 0x78 are keys */
Roman Zippel5fa28ea2006-06-23 02:04:56 -0700195 if (scancode == 98) { /* CapsLock is a toggle switch key on Amiga */
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200196 input_report_key(dev, scancode, 1);
197 input_report_key(dev, scancode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 } else {
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200199 input_report_key(dev, scancode, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500201
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200202 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else /* scancodes >= 0x78 are error codes */
204 printk(amikbd_messages[scancode - 0x78]);
205
206 return IRQ_HANDLED;
207}
208
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200209static int __init amikbd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200211 struct input_dev *dev;
Geert Uytterhoeven545e6252014-12-03 14:52:58 -0800212 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200214 dev = input_allocate_device();
215 if (!dev) {
216 dev_err(&pdev->dev, "Not enough memory for input device\n");
217 return -ENOMEM;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200220 dev->name = pdev->name;
221 dev->phys = "amikbd/input0";
222 dev->id.bustype = BUS_AMIGA;
223 dev->id.vendor = 0x0001;
224 dev->id.product = 0x0001;
225 dev->id.version = 0x0100;
226 dev->dev.parent = &pdev->dev;
Dmitry Torokhov3c42f0c2005-09-15 02:01:45 -0500227
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200228 dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 for (i = 0; i < 0x78; i++)
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200231 set_bit(i, dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Geert Uytterhoeven545e6252014-12-03 14:52:58 -0800233 amikbd_init_console_keymaps();
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ciaa.cra &= ~0x41; /* serial data in, turn off TA */
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200236 err = request_irq(IRQ_AMIGA_CIAA_SP, amikbd_interrupt, 0, "amikbd",
237 dev);
238 if (err)
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500239 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200241 err = input_register_device(dev);
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500242 if (err)
243 goto fail3;
244
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200245 platform_set_drvdata(pdev, dev);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500248
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200249 fail3: free_irq(IRQ_AMIGA_CIAA_SP, dev);
250 fail2: input_free_device(dev);
Dmitry Torokhov2b03b602006-11-05 22:39:56 -0500251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200254static int __exit amikbd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200256 struct input_dev *dev = platform_get_drvdata(pdev);
257
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200258 free_irq(IRQ_AMIGA_CIAA_SP, dev);
259 input_unregister_device(dev);
260 return 0;
261}
262
263static struct platform_driver amikbd_driver = {
264 .remove = __exit_p(amikbd_remove),
265 .driver = {
266 .name = "amiga-keyboard",
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200267 },
268};
Dmitry Torokhovd3d25802012-01-10 15:08:01 -0800269
Sachin Kamate67d5412013-03-17 21:27:11 -0700270module_platform_driver_probe(amikbd_driver, amikbd_probe);
Geert Uytterhoeven5121c712009-04-05 13:10:56 +0200271
272MODULE_ALIAS("platform:amiga-keyboard");