xref: /linux/drivers/input/keyboard/jornada680_kbd.c (revision c95baf12f5077419db01313ab61c2aac007d40cd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/input/keyboard/jornada680_kbd.c
4  *
5  * HP Jornada 620/660/680/690 scan keyboard platform driver
6  *  Copyright (C) 2007  Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
7  *
8  * Based on hp680_keyb.c
9  *  Copyright (C) 2006 Paul Mundt
10  *  Copyright (C) 2005 Andriy Skulysh
11  * Split from drivers/input/keyboard/hp600_keyb.c
12  *  Copyright (C) 2000 Yaegashi Takeshi (hp6xx kbd scan routine and translation table)
13  *  Copyright (C) 2000 Niibe Yutaka (HP620 Keyb translation table)
14  */
15 
16 #include <linux/device.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 
25 #include <asm/delay.h>
26 #include <asm/io.h>
27 
28 #define PCCR 0xa4000104
29 #define PDCR 0xa4000106
30 #define PECR 0xa4000108
31 #define PFCR 0xa400010a
32 #define PCDR 0xa4000124
33 #define PDDR 0xa4000126
34 #define PEDR 0xa4000128
35 #define PFDR 0xa400012a
36 #define PGDR 0xa400012c
37 #define PHDR 0xa400012e
38 #define PJDR 0xa4000130
39 #define PKDR 0xa4000132
40 #define PLDR 0xa4000134
41 
42 static const unsigned short jornada_scancodes[] = {
43 /* PTD1 */	KEY_CAPSLOCK, KEY_MACRO, KEY_LEFTCTRL, 0, KEY_ESC, KEY_KP5, 0, 0,			/*  1  -> 8   */
44 		KEY_F1, KEY_F2, KEY_F3, KEY_F8, KEY_F7, KEY_F6, KEY_F4, KEY_F5,				/*  9  -> 16  */
45 /* PTD5 */	KEY_SLASH, KEY_APOSTROPHE, KEY_ENTER, 0, KEY_Z, 0, 0, 0,				/*  17 -> 24  */
46 		KEY_X, KEY_C, KEY_V, KEY_DOT, KEY_COMMA, KEY_M, KEY_B, KEY_N,				/*  25 -> 32  */
47 /* PTD7 */	KEY_KP2, KEY_KP6, KEY_KP3, 0, 0, 0, 0, 0,						/*  33 -> 40  */
48 		KEY_F10, KEY_RO, KEY_F9, KEY_KP4, KEY_NUMLOCK, KEY_SCROLLLOCK, KEY_LEFTALT, KEY_HANJA,	/*  41 -> 48  */
49 /* PTE0 */	KEY_KATAKANA, KEY_KP0, KEY_GRAVE, 0, KEY_FINANCE, 0, 0, 0,				/*  49 -> 56  */
50 		KEY_KPMINUS, KEY_HIRAGANA, KEY_SPACE, KEY_KPDOT, KEY_VOLUMEUP, 249, 0, 0,		/*  57 -> 64  */
51 /* PTE1 */	KEY_SEMICOLON, KEY_RIGHTBRACE, KEY_BACKSLASH, 0, KEY_A, 0, 0, 0,			/*  65 -> 72  */
52 		KEY_S, KEY_D, KEY_F, KEY_L, KEY_K, KEY_J, KEY_G, KEY_H,					/*  73 -> 80  */
53 /* PTE3 */	KEY_KP8, KEY_LEFTMETA, KEY_RIGHTSHIFT, 0, KEY_TAB, 0, 0, 0,				/*  81 -> 88  */
54 		0, KEY_LEFTSHIFT, KEY_KP7, KEY_KP9, KEY_KP1, KEY_F11, KEY_KPPLUS, KEY_KPASTERISK,	/*  89 -> 96  */
55 /* PTE6 */	KEY_P, KEY_LEFTBRACE, KEY_BACKSPACE, 0, KEY_Q, 0, 0, 0,					/*  97 -> 104 */
56 		KEY_W, KEY_E, KEY_R, KEY_O, KEY_I, KEY_U, KEY_T, KEY_Y,					/* 105 -> 112 */
57 /* PTE7 */	KEY_0, KEY_MINUS, KEY_EQUAL, 0, KEY_1, 0, 0, 0,						/* 113 -> 120 */
58 		KEY_2, KEY_3, KEY_4, KEY_9, KEY_8, KEY_7, KEY_5, KEY_6,					/* 121 -> 128 */
59 /* **** */	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 		0, 0, 0, 0, 0
61 };
62 
63 #define JORNADA_SCAN_SIZE	18
64 
65 struct jornadakbd {
66 	struct input_dev *input;
67 	unsigned short keymap[ARRAY_SIZE(jornada_scancodes)];
68 	unsigned char length;
69 	unsigned char old_scan[JORNADA_SCAN_SIZE];
70 	unsigned char new_scan[JORNADA_SCAN_SIZE];
71 };
72 
jornada_parse_kbd(struct jornadakbd * jornadakbd)73 static void jornada_parse_kbd(struct jornadakbd *jornadakbd)
74 {
75 	struct input_dev *input_dev = jornadakbd->input;
76 	unsigned short *keymap = jornadakbd->keymap;
77 	unsigned int sync_me = 0;
78 	unsigned int i, j;
79 
80 	for (i = 0; i < JORNADA_SCAN_SIZE; i++) {
81 		unsigned char new = jornadakbd->new_scan[i];
82 		unsigned char old = jornadakbd->old_scan[i];
83 		unsigned int xor = new ^ old;
84 
85 		if (xor == 0)
86 			continue;
87 
88 		for (j = 0; j < 8; j++) {
89 			unsigned int bit = 1 << j;
90 			if (xor & bit) {
91 				unsigned int scancode = (i << 3) + j;
92 				input_event(input_dev,
93 					    EV_MSC, MSC_SCAN, scancode);
94 				input_report_key(input_dev,
95 						 keymap[scancode],
96 						 !(new & bit));
97 				sync_me = 1;
98 			}
99 		}
100 	}
101 
102 	if (sync_me)
103 	    input_sync(input_dev);
104 }
105 
jornada_scan_keyb(unsigned char * s)106 static void jornada_scan_keyb(unsigned char *s)
107 {
108 	int i;
109 	unsigned short ec_static, dc_static; /* = UINT16_t */
110 	unsigned char matrix_switch[] = {
111 		0xfd, 0xff,   /* PTD1 PD(1) */
112 		0xdf, 0xff,   /* PTD5 PD(5) */
113 		0x7f, 0xff,   /* PTD7 PD(7) */
114 		0xff, 0xfe,   /* PTE0 PE(0) */
115 		0xff, 0xfd,   /* PTE1 PE(1) */
116 		0xff, 0xf7,   /* PTE3 PE(3) */
117 		0xff, 0xbf,   /* PTE6 PE(6) */
118 		0xff, 0x7f,   /* PTE7 PE(7) */
119 	}, *t = matrix_switch;
120 	/* PD(x) :
121 	1.   0xcc0c & (1~(1 << (2*(x)+1)))))
122 	2.   (0xf0cf & 0xfffff) */
123 	/* PE(x) :
124 	1.   0xcc0c & 0xffff
125 	2.   0xf0cf & (1~(1 << (2*(x)+1))))) */
126 	unsigned short matrix_PDE[] = {
127 		0xcc04, 0xf0cf,  /* PD(1) */
128 		0xc40c, 0xf0cf,	 /* PD(5) */
129 		0x4c0c, 0xf0cf,  /* PD(7) */
130 		0xcc0c, 0xf0cd,  /* PE(0) */
131 		0xcc0c, 0xf0c7,	 /* PE(1) */
132 		0xcc0c, 0xf04f,  /* PE(3) */
133 		0xcc0c, 0xd0cf,	 /* PE(6) */
134 		0xcc0c, 0x70cf,	 /* PE(7) */
135 	}, *y = matrix_PDE;
136 
137 	/* Save these control reg bits */
138 	dc_static = (__raw_readw(PDCR) & (~0xcc0c));
139 	ec_static = (__raw_readw(PECR) & (~0xf0cf));
140 
141 	for (i = 0; i < 8; i++) {
142 		/* disable output for all but the one we want to scan */
143 		__raw_writew((dc_static | *y++), PDCR);
144 		__raw_writew((ec_static | *y++), PECR);
145 		udelay(5);
146 
147 		/* Get scanline row */
148 		__raw_writeb(*t++, PDDR);
149 		__raw_writeb(*t++, PEDR);
150 		udelay(50);
151 
152 		/* Read data */
153 		*s++ = __raw_readb(PCDR);
154 		*s++ = __raw_readb(PFDR);
155 	}
156 	/* Scan no lines */
157 	__raw_writeb(0xff, PDDR);
158 	__raw_writeb(0xff, PEDR);
159 
160 	/* Enable all scanlines */
161 	__raw_writew((dc_static | (0x5555 & 0xcc0c)),PDCR);
162 	__raw_writew((ec_static | (0x5555 & 0xf0cf)),PECR);
163 
164 	/* Ignore extra keys and events */
165 	*s++ = __raw_readb(PGDR);
166 	*s++ = __raw_readb(PHDR);
167 }
168 
jornadakbd680_poll(struct input_dev * input)169 static void jornadakbd680_poll(struct input_dev *input)
170 {
171 	struct jornadakbd *jornadakbd = input_get_drvdata(input);
172 
173 	jornada_scan_keyb(jornadakbd->new_scan);
174 	jornada_parse_kbd(jornadakbd);
175 	memcpy(jornadakbd->old_scan, jornadakbd->new_scan, JORNADA_SCAN_SIZE);
176 }
177 
jornada680kbd_probe(struct platform_device * pdev)178 static int jornada680kbd_probe(struct platform_device *pdev)
179 {
180 	struct jornadakbd *jornadakbd;
181 	struct input_dev *input_dev;
182 	int i, error;
183 
184 	jornadakbd = devm_kzalloc(&pdev->dev, sizeof(struct jornadakbd),
185 				  GFP_KERNEL);
186 	if (!jornadakbd)
187 		return -ENOMEM;
188 
189 	input_dev = devm_input_allocate_device(&pdev->dev);
190 	if (!input_dev) {
191 		dev_err(&pdev->dev, "failed to allocate input device\n");
192 		return -ENOMEM;
193 	}
194 
195 	jornadakbd->input = input_dev;
196 
197 	memcpy(jornadakbd->keymap, jornada_scancodes,
198 		sizeof(jornadakbd->keymap));
199 
200 	input_set_drvdata(input_dev, jornadakbd);
201 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
202 	input_dev->name = "HP Jornada 680 keyboard";
203 	input_dev->phys = "jornadakbd/input0";
204 	input_dev->keycode = jornadakbd->keymap;
205 	input_dev->keycodesize = sizeof(unsigned short);
206 	input_dev->keycodemax = ARRAY_SIZE(jornada_scancodes);
207 	input_dev->id.bustype = BUS_HOST;
208 
209 	for (i = 0; i < 128; i++)
210 		if (jornadakbd->keymap[i])
211 			__set_bit(jornadakbd->keymap[i], input_dev->keybit);
212 	__clear_bit(KEY_RESERVED, input_dev->keybit);
213 
214 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
215 
216 	error = input_setup_polling(input_dev, jornadakbd680_poll);
217 	if (error) {
218 		dev_err(&pdev->dev, "failed to set up polling\n");
219 		return error;
220 	}
221 
222 	input_set_poll_interval(input_dev, 50 /* msec */);
223 
224 	error = input_register_device(input_dev);
225 	if (error) {
226 		dev_err(&pdev->dev, "failed to register input device\n");
227 		return error;
228 	}
229 
230 	return 0;
231 }
232 
233 static struct platform_driver jornada680kbd_driver = {
234 	.driver	= {
235 		.name	= "jornada680_kbd",
236 	},
237 	.probe	= jornada680kbd_probe,
238 };
239 module_platform_driver(jornada680kbd_driver);
240 
241 MODULE_AUTHOR("Kristoffer Ericson <kristoffer.ericson@gmail.com>");
242 MODULE_DESCRIPTION("HP Jornada 620/660/680/690 Keyboard Driver");
243 MODULE_LICENSE("GPL v2");
244 MODULE_ALIAS("platform:jornada680_kbd");
245