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