xref: /openbmc/linux/drivers/input/keyboard/amikbd.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2000-2001 Vojtech Pavlik
4  *
5  *  Based on the work of:
6  *	Hamish Macdonald
7  */
8 
9 /*
10  * Amiga keyboard driver for Linux/m68k
11  */
12 
13 /*
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/input.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/keyboard.h>
22 #include <linux/platform_device.h>
23 
24 #include <asm/amigaints.h>
25 #include <asm/amigahw.h>
26 #include <asm/irq.h>
27 
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION("Amiga keyboard driver");
30 MODULE_LICENSE("GPL");
31 
32 #ifdef CONFIG_HW_CONSOLE
33 static unsigned char amikbd_keycode[0x78] __initdata = {
34 	[0]	 = KEY_GRAVE,
35 	[1]	 = KEY_1,
36 	[2]	 = KEY_2,
37 	[3]	 = KEY_3,
38 	[4]	 = KEY_4,
39 	[5]	 = KEY_5,
40 	[6]	 = KEY_6,
41 	[7]	 = KEY_7,
42 	[8]	 = KEY_8,
43 	[9]	 = KEY_9,
44 	[10]	 = KEY_0,
45 	[11]	 = KEY_MINUS,
46 	[12]	 = KEY_EQUAL,
47 	[13]	 = KEY_BACKSLASH,
48 	[15]	 = KEY_KP0,
49 	[16]	 = KEY_Q,
50 	[17]	 = KEY_W,
51 	[18]	 = KEY_E,
52 	[19]	 = KEY_R,
53 	[20]	 = KEY_T,
54 	[21]	 = KEY_Y,
55 	[22]	 = KEY_U,
56 	[23]	 = KEY_I,
57 	[24]	 = KEY_O,
58 	[25]	 = KEY_P,
59 	[26]	 = KEY_LEFTBRACE,
60 	[27]	 = KEY_RIGHTBRACE,
61 	[29]	 = KEY_KP1,
62 	[30]	 = KEY_KP2,
63 	[31]	 = KEY_KP3,
64 	[32]	 = KEY_A,
65 	[33]	 = KEY_S,
66 	[34]	 = KEY_D,
67 	[35]	 = KEY_F,
68 	[36]	 = KEY_G,
69 	[37]	 = KEY_H,
70 	[38]	 = KEY_J,
71 	[39]	 = KEY_K,
72 	[40]	 = KEY_L,
73 	[41]	 = KEY_SEMICOLON,
74 	[42]	 = KEY_APOSTROPHE,
75 	[43]	 = KEY_BACKSLASH,
76 	[45]	 = KEY_KP4,
77 	[46]	 = KEY_KP5,
78 	[47]	 = KEY_KP6,
79 	[48]	 = KEY_102ND,
80 	[49]	 = KEY_Z,
81 	[50]	 = KEY_X,
82 	[51]	 = KEY_C,
83 	[52]	 = KEY_V,
84 	[53]	 = KEY_B,
85 	[54]	 = KEY_N,
86 	[55]	 = KEY_M,
87 	[56]	 = KEY_COMMA,
88 	[57]	 = KEY_DOT,
89 	[58]	 = KEY_SLASH,
90 	[60]	 = KEY_KPDOT,
91 	[61]	 = KEY_KP7,
92 	[62]	 = KEY_KP8,
93 	[63]	 = KEY_KP9,
94 	[64]	 = KEY_SPACE,
95 	[65]	 = KEY_BACKSPACE,
96 	[66]	 = KEY_TAB,
97 	[67]	 = KEY_KPENTER,
98 	[68]	 = KEY_ENTER,
99 	[69]	 = KEY_ESC,
100 	[70]	 = KEY_DELETE,
101 	[74]	 = KEY_KPMINUS,
102 	[76]	 = KEY_UP,
103 	[77]	 = KEY_DOWN,
104 	[78]	 = KEY_RIGHT,
105 	[79]	 = KEY_LEFT,
106 	[80]	 = KEY_F1,
107 	[81]	 = KEY_F2,
108 	[82]	 = KEY_F3,
109 	[83]	 = KEY_F4,
110 	[84]	 = KEY_F5,
111 	[85]	 = KEY_F6,
112 	[86]	 = KEY_F7,
113 	[87]	 = KEY_F8,
114 	[88]	 = KEY_F9,
115 	[89]	 = KEY_F10,
116 	[90]	 = KEY_KPLEFTPAREN,
117 	[91]	 = KEY_KPRIGHTPAREN,
118 	[92]	 = KEY_KPSLASH,
119 	[93]	 = KEY_KPASTERISK,
120 	[94]	 = KEY_KPPLUS,
121 	[95]	 = KEY_HELP,
122 	[96]	 = KEY_LEFTSHIFT,
123 	[97]	 = KEY_RIGHTSHIFT,
124 	[98]	 = KEY_CAPSLOCK,
125 	[99]	 = KEY_LEFTCTRL,
126 	[100]	 = KEY_LEFTALT,
127 	[101]	 = KEY_RIGHTALT,
128 	[102]	 = KEY_LEFTMETA,
129 	[103]	 = KEY_RIGHTMETA
130 };
131 
132 static void __init amikbd_init_console_keymaps(void)
133 {
134 	/* We can spare 512 bytes on stack for temp_map in init path. */
135 	unsigned short temp_map[NR_KEYS];
136 	int i, j;
137 
138 	for (i = 0; i < MAX_NR_KEYMAPS; i++) {
139 		if (!key_maps[i])
140 			continue;
141 		memset(temp_map, 0, sizeof(temp_map));
142 		for (j = 0; j < 0x78; j++) {
143 			if (!amikbd_keycode[j])
144 				continue;
145 			temp_map[j] = key_maps[i][amikbd_keycode[j]];
146 		}
147 		for (j = 0; j < NR_KEYS; j++) {
148 			if (!temp_map[j])
149 				temp_map[j] = 0xf200;
150 		}
151 		memcpy(key_maps[i], temp_map, sizeof(temp_map));
152 	}
153 }
154 #else /* !CONFIG_HW_CONSOLE */
155 static inline void amikbd_init_console_keymaps(void) {}
156 #endif /* !CONFIG_HW_CONSOLE */
157 
158 static const char *amikbd_messages[8] = {
159 	[0] = KERN_ALERT "amikbd: Ctrl-Amiga-Amiga reset warning!!\n",
160 	[1] = KERN_WARNING "amikbd: keyboard lost sync\n",
161 	[2] = KERN_WARNING "amikbd: keyboard buffer overflow\n",
162 	[3] = KERN_WARNING "amikbd: keyboard controller failure\n",
163 	[4] = KERN_ERR "amikbd: keyboard selftest failure\n",
164 	[5] = KERN_INFO "amikbd: initiate power-up key stream\n",
165 	[6] = KERN_INFO "amikbd: terminate power-up key stream\n",
166 	[7] = KERN_WARNING "amikbd: keyboard interrupt\n"
167 };
168 
169 static irqreturn_t amikbd_interrupt(int irq, void *data)
170 {
171 	struct input_dev *dev = data;
172 	unsigned char scancode, down;
173 
174 	scancode = ~ciaa.sdr;		/* get and invert scancode (keyboard is active low) */
175 	ciaa.cra |= 0x40;		/* switch SP pin to output for handshake */
176 	udelay(85);			/* wait until 85 us have expired */
177 	ciaa.cra &= ~0x40;		/* switch CIA serial port to input mode */
178 
179 	down = !(scancode & 1);		/* lowest bit is release bit */
180 	scancode >>= 1;
181 
182 	if (scancode < 0x78) {		/* scancodes < 0x78 are keys */
183 		if (scancode == 98) {	/* CapsLock is a toggle switch key on Amiga */
184 			input_report_key(dev, scancode, 1);
185 			input_report_key(dev, scancode, 0);
186 		} else {
187 			input_report_key(dev, scancode, down);
188 		}
189 
190 		input_sync(dev);
191 	} else				/* scancodes >= 0x78 are error codes */
192 		printk(amikbd_messages[scancode - 0x78]);
193 
194 	return IRQ_HANDLED;
195 }
196 
197 static int __init amikbd_probe(struct platform_device *pdev)
198 {
199 	struct input_dev *dev;
200 	int i, err;
201 
202 	dev = input_allocate_device();
203 	if (!dev) {
204 		dev_err(&pdev->dev, "Not enough memory for input device\n");
205 		return -ENOMEM;
206 	}
207 
208 	dev->name = pdev->name;
209 	dev->phys = "amikbd/input0";
210 	dev->id.bustype = BUS_AMIGA;
211 	dev->id.vendor = 0x0001;
212 	dev->id.product = 0x0001;
213 	dev->id.version = 0x0100;
214 	dev->dev.parent = &pdev->dev;
215 
216 	dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
217 
218 	for (i = 0; i < 0x78; i++)
219 		set_bit(i, dev->keybit);
220 
221 	amikbd_init_console_keymaps();
222 
223 	ciaa.cra &= ~0x41;	 /* serial data in, turn off TA */
224 	err = request_irq(IRQ_AMIGA_CIAA_SP, amikbd_interrupt, 0, "amikbd",
225 			  dev);
226 	if (err)
227 		goto fail2;
228 
229 	err = input_register_device(dev);
230 	if (err)
231 		goto fail3;
232 
233 	platform_set_drvdata(pdev, dev);
234 
235 	return 0;
236 
237  fail3:	free_irq(IRQ_AMIGA_CIAA_SP, dev);
238  fail2:	input_free_device(dev);
239 	return err;
240 }
241 
242 static int __exit amikbd_remove(struct platform_device *pdev)
243 {
244 	struct input_dev *dev = platform_get_drvdata(pdev);
245 
246 	free_irq(IRQ_AMIGA_CIAA_SP, dev);
247 	input_unregister_device(dev);
248 	return 0;
249 }
250 
251 static struct platform_driver amikbd_driver = {
252 	.remove = __exit_p(amikbd_remove),
253 	.driver   = {
254 		.name	= "amiga-keyboard",
255 	},
256 };
257 
258 module_platform_driver_probe(amikbd_driver, amikbd_probe);
259 
260 MODULE_ALIAS("platform:amiga-keyboard");
261