xref: /linux/drivers/input/keyboard/jornada680_kbd.c (revision 5146c84f87c8aa3d115cea0d77ed3553df426752)
12aa2cb9eSKristoffer Ericson /*
22aa2cb9eSKristoffer Ericson  * drivers/input/keyboard/jornada680_kbd.c
32aa2cb9eSKristoffer Ericson  *
42aa2cb9eSKristoffer Ericson  * HP Jornada 620/660/680/690 scan keyboard platform driver
52aa2cb9eSKristoffer Ericson  *  Copyright (C) 2007  Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
62aa2cb9eSKristoffer Ericson  *
72aa2cb9eSKristoffer Ericson  * Based on hp680_keyb.c
82aa2cb9eSKristoffer Ericson  *  Copyright (C) 2006 Paul Mundt
92aa2cb9eSKristoffer Ericson  *  Copyright (C) 2005 Andriy Skulysh
102aa2cb9eSKristoffer Ericson  * Split from drivers/input/keyboard/hp600_keyb.c
112aa2cb9eSKristoffer Ericson  *  Copyright (C) 2000 Yaegashi Takeshi (hp6xx kbd scan routine and translation table)
122aa2cb9eSKristoffer Ericson  *  Copyright (C) 2000 Niibe Yutaka (HP620 Keyb translation table)
132aa2cb9eSKristoffer Ericson  *
142aa2cb9eSKristoffer Ericson  * This program is free software; you can redistribute it and/or modify it
152aa2cb9eSKristoffer Ericson  * under the terms of the GNU General Public License version 2 as
162aa2cb9eSKristoffer Ericson  * published by the Free Software Foundation.
172aa2cb9eSKristoffer Ericson  */
182aa2cb9eSKristoffer Ericson 
194c64681eSKristoffer Ericson #include <linux/init.h>
202aa2cb9eSKristoffer Ericson #include <linux/input.h>
214c64681eSKristoffer Ericson #include <linux/input-polldev.h>
224c64681eSKristoffer Ericson #include <linux/interrupt.h>
234c64681eSKristoffer Ericson #include <linux/jiffies.h>
242aa2cb9eSKristoffer Ericson #include <linux/kernel.h>
252aa2cb9eSKristoffer Ericson #include <linux/module.h>
262aa2cb9eSKristoffer Ericson #include <linux/platform_device.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
282aa2cb9eSKristoffer Ericson 
292aa2cb9eSKristoffer Ericson #include <asm/delay.h>
302aa2cb9eSKristoffer Ericson #include <asm/io.h>
312aa2cb9eSKristoffer Ericson 
322aa2cb9eSKristoffer Ericson #define PCCR 0xa4000104
332aa2cb9eSKristoffer Ericson #define PDCR 0xa4000106
342aa2cb9eSKristoffer Ericson #define PECR 0xa4000108
352aa2cb9eSKristoffer Ericson #define PFCR 0xa400010a
362aa2cb9eSKristoffer Ericson #define PCDR 0xa4000124
372aa2cb9eSKristoffer Ericson #define PDDR 0xa4000126
382aa2cb9eSKristoffer Ericson #define PEDR 0xa4000128
392aa2cb9eSKristoffer Ericson #define PFDR 0xa400012a
402aa2cb9eSKristoffer Ericson #define PGDR 0xa400012c
412aa2cb9eSKristoffer Ericson #define PHDR 0xa400012e
422aa2cb9eSKristoffer Ericson #define PJDR 0xa4000130
432aa2cb9eSKristoffer Ericson #define PKDR 0xa4000132
442aa2cb9eSKristoffer Ericson #define PLDR 0xa4000134
452aa2cb9eSKristoffer Ericson 
462aa2cb9eSKristoffer Ericson static const unsigned short jornada_scancodes[] = {
474c64681eSKristoffer Ericson /* PTD1 */	KEY_CAPSLOCK, KEY_MACRO, KEY_LEFTCTRL, 0, KEY_ESC, KEY_KP5, 0, 0,			/*  1  -> 8   */
484c64681eSKristoffer Ericson 		KEY_F1, KEY_F2, KEY_F3, KEY_F8, KEY_F7, KEY_F6, KEY_F4, KEY_F5,				/*  9  -> 16  */
492aa2cb9eSKristoffer Ericson /* PTD5 */	KEY_SLASH, KEY_APOSTROPHE, KEY_ENTER, 0, KEY_Z, 0, 0, 0,				/*  17 -> 24  */
502aa2cb9eSKristoffer Ericson 		KEY_X, KEY_C, KEY_V, KEY_DOT, KEY_COMMA, KEY_M, KEY_B, KEY_N,				/*  25 -> 32  */
514c64681eSKristoffer Ericson /* PTD7 */	KEY_KP2, KEY_KP6, KEY_KP3, 0, 0, 0, 0, 0,						/*  33 -> 40  */
524c64681eSKristoffer Ericson 		KEY_F10, KEY_RO, KEY_F9, KEY_KP4, KEY_NUMLOCK, KEY_SCROLLLOCK, KEY_LEFTALT, KEY_HANJA,	/*  41 -> 48  */
534c64681eSKristoffer Ericson /* PTE0 */	KEY_KATAKANA, KEY_KP0, KEY_GRAVE, 0, KEY_FINANCE, 0, 0, 0,				/*  49 -> 56  */
544c64681eSKristoffer Ericson 		KEY_KPMINUS, KEY_HIRAGANA, KEY_SPACE, KEY_KPDOT, KEY_VOLUMEUP, 249, 0, 0,		/*  57 -> 64  */
552aa2cb9eSKristoffer Ericson /* PTE1 */	KEY_SEMICOLON, KEY_RIGHTBRACE, KEY_BACKSLASH, 0, KEY_A, 0, 0, 0,			/*  65 -> 72  */
562aa2cb9eSKristoffer Ericson 		KEY_S, KEY_D, KEY_F, KEY_L, KEY_K, KEY_J, KEY_G, KEY_H,					/*  73 -> 80  */
572aa2cb9eSKristoffer Ericson /* PTE3 */	KEY_KP8, KEY_LEFTMETA, KEY_RIGHTSHIFT, 0, KEY_TAB, 0, 0, 0,				/*  81 -> 88  */
584c64681eSKristoffer Ericson 		0, KEY_LEFTSHIFT, KEY_KP7, KEY_KP9, KEY_KP1, KEY_F11, KEY_KPPLUS, KEY_KPASTERISK,	/*  89 -> 96  */
592aa2cb9eSKristoffer Ericson /* PTE6 */	KEY_P, KEY_LEFTBRACE, KEY_BACKSPACE, 0, KEY_Q, 0, 0, 0,					/*  97 -> 104 */
604c64681eSKristoffer Ericson 		KEY_W, KEY_E, KEY_R, KEY_O, KEY_I, KEY_U, KEY_T, KEY_Y,					/* 105 -> 112 */
612aa2cb9eSKristoffer Ericson /* PTE7 */	KEY_0, KEY_MINUS, KEY_EQUAL, 0, KEY_1, 0, 0, 0,						/* 113 -> 120 */
622aa2cb9eSKristoffer Ericson 		KEY_2, KEY_3, KEY_4, KEY_9, KEY_8, KEY_7, KEY_5, KEY_6,					/* 121 -> 128 */
632aa2cb9eSKristoffer Ericson /* **** */	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
642aa2cb9eSKristoffer Ericson 		0, 0, 0, 0, 0
652aa2cb9eSKristoffer Ericson };
662aa2cb9eSKristoffer Ericson 
672aa2cb9eSKristoffer Ericson #define JORNADA_SCAN_SIZE	18
682aa2cb9eSKristoffer Ericson 
692aa2cb9eSKristoffer Ericson struct jornadakbd {
702aa2cb9eSKristoffer Ericson 	struct input_polled_dev *poll_dev;
712aa2cb9eSKristoffer Ericson 	unsigned short keymap[ARRAY_SIZE(jornada_scancodes)];
722aa2cb9eSKristoffer Ericson 	unsigned char length;
732aa2cb9eSKristoffer Ericson 	unsigned char old_scan[JORNADA_SCAN_SIZE];
742aa2cb9eSKristoffer Ericson 	unsigned char new_scan[JORNADA_SCAN_SIZE];
752aa2cb9eSKristoffer Ericson };
762aa2cb9eSKristoffer Ericson 
772aa2cb9eSKristoffer Ericson static void jornada_parse_kbd(struct jornadakbd *jornadakbd)
782aa2cb9eSKristoffer Ericson {
792aa2cb9eSKristoffer Ericson 	struct input_dev *input_dev = jornadakbd->poll_dev->input;
802aa2cb9eSKristoffer Ericson 	unsigned short *keymap = jornadakbd->keymap;
812aa2cb9eSKristoffer Ericson 	unsigned int sync_me = 0;
822aa2cb9eSKristoffer Ericson 	unsigned int i, j;
832aa2cb9eSKristoffer Ericson 
842aa2cb9eSKristoffer Ericson 	for (i = 0; i < JORNADA_SCAN_SIZE; i++) {
852aa2cb9eSKristoffer Ericson 		unsigned char new = jornadakbd->new_scan[i];
862aa2cb9eSKristoffer Ericson 		unsigned char old = jornadakbd->old_scan[i];
872aa2cb9eSKristoffer Ericson 		unsigned int xor = new ^ old;
882aa2cb9eSKristoffer Ericson 
892aa2cb9eSKristoffer Ericson 		if (xor == 0)
902aa2cb9eSKristoffer Ericson 			continue;
912aa2cb9eSKristoffer Ericson 
922aa2cb9eSKristoffer Ericson 		for (j = 0; j < 8; j++) {
932aa2cb9eSKristoffer Ericson 			unsigned int bit = 1 << j;
942aa2cb9eSKristoffer Ericson 			if (xor & bit) {
952aa2cb9eSKristoffer Ericson 				unsigned int scancode = (i << 3) + j;
962aa2cb9eSKristoffer Ericson 				input_event(input_dev,
972aa2cb9eSKristoffer Ericson 					    EV_MSC, MSC_SCAN, scancode);
982aa2cb9eSKristoffer Ericson 				input_report_key(input_dev,
992aa2cb9eSKristoffer Ericson 						 keymap[scancode],
1002aa2cb9eSKristoffer Ericson 						 !(new & bit));
1012aa2cb9eSKristoffer Ericson 				sync_me = 1;
1022aa2cb9eSKristoffer Ericson 			}
1032aa2cb9eSKristoffer Ericson 		}
1042aa2cb9eSKristoffer Ericson 	}
1052aa2cb9eSKristoffer Ericson 
1062aa2cb9eSKristoffer Ericson 	if (sync_me)
1072aa2cb9eSKristoffer Ericson 	    input_sync(input_dev);
1082aa2cb9eSKristoffer Ericson }
1092aa2cb9eSKristoffer Ericson 
1102aa2cb9eSKristoffer Ericson static void jornada_scan_keyb(unsigned char *s)
1112aa2cb9eSKristoffer Ericson {
1122aa2cb9eSKristoffer Ericson 	int i;
1132aa2cb9eSKristoffer Ericson 	unsigned short ec_static, dc_static; /* = UINT16_t */
1142aa2cb9eSKristoffer Ericson 	unsigned char matrix_switch[] = {
1152aa2cb9eSKristoffer Ericson 		0xfd, 0xff,   /* PTD1 PD(1) */
1162aa2cb9eSKristoffer Ericson 		0xdf, 0xff,   /* PTD5 PD(5) */
1172aa2cb9eSKristoffer Ericson 		0x7f, 0xff,   /* PTD7 PD(7) */
1182aa2cb9eSKristoffer Ericson 		0xff, 0xfe,   /* PTE0 PE(0) */
1192aa2cb9eSKristoffer Ericson 		0xff, 0xfd,   /* PTE1 PE(1) */
1202aa2cb9eSKristoffer Ericson 		0xff, 0xf7,   /* PTE3 PE(3) */
1212aa2cb9eSKristoffer Ericson 		0xff, 0xbf,   /* PTE6 PE(6) */
1222aa2cb9eSKristoffer Ericson 		0xff, 0x7f,   /* PTE7 PE(7) */
1232aa2cb9eSKristoffer Ericson 	}, *t = matrix_switch;
1242aa2cb9eSKristoffer Ericson 	/* PD(x) :
1252aa2cb9eSKristoffer Ericson 	1.   0xcc0c & (1~(1 << (2*(x)+1)))))
1262aa2cb9eSKristoffer Ericson 	2.   (0xf0cf & 0xfffff) */
1272aa2cb9eSKristoffer Ericson 	/* PE(x) :
1282aa2cb9eSKristoffer Ericson 	1.   0xcc0c & 0xffff
1292aa2cb9eSKristoffer Ericson 	2.   0xf0cf & (1~(1 << (2*(x)+1))))) */
1302aa2cb9eSKristoffer Ericson 	unsigned short matrix_PDE[] = {
1312aa2cb9eSKristoffer Ericson 		0xcc04, 0xf0cf,  /* PD(1) */
1322aa2cb9eSKristoffer Ericson 		0xc40c, 0xf0cf,	 /* PD(5) */
1332aa2cb9eSKristoffer Ericson 		0x4c0c, 0xf0cf,  /* PD(7) */
1342aa2cb9eSKristoffer Ericson 		0xcc0c, 0xf0cd,  /* PE(0) */
1352aa2cb9eSKristoffer Ericson 		0xcc0c, 0xf0c7,	 /* PE(1) */
1362aa2cb9eSKristoffer Ericson 		0xcc0c, 0xf04f,  /* PE(3) */
1372aa2cb9eSKristoffer Ericson 		0xcc0c, 0xd0cf,	 /* PE(6) */
1382aa2cb9eSKristoffer Ericson 		0xcc0c, 0x70cf,	 /* PE(7) */
1392aa2cb9eSKristoffer Ericson 	}, *y = matrix_PDE;
1402aa2cb9eSKristoffer Ericson 
1412aa2cb9eSKristoffer Ericson 	/* Save these control reg bits */
1420af28408SPaul Mundt 	dc_static = (__raw_readw(PDCR) & (~0xcc0c));
1430af28408SPaul Mundt 	ec_static = (__raw_readw(PECR) & (~0xf0cf));
1442aa2cb9eSKristoffer Ericson 
1452aa2cb9eSKristoffer Ericson 	for (i = 0; i < 8; i++) {
1462aa2cb9eSKristoffer Ericson 		/* disable output for all but the one we want to scan */
1470af28408SPaul Mundt 		__raw_writew((dc_static | *y++), PDCR);
1480af28408SPaul Mundt 		__raw_writew((ec_static | *y++), PECR);
1492aa2cb9eSKristoffer Ericson 		udelay(5);
1502aa2cb9eSKristoffer Ericson 
1512aa2cb9eSKristoffer Ericson 		/* Get scanline row */
1520af28408SPaul Mundt 		__raw_writeb(*t++, PDDR);
1530af28408SPaul Mundt 		__raw_writeb(*t++, PEDR);
1542aa2cb9eSKristoffer Ericson 		udelay(50);
1552aa2cb9eSKristoffer Ericson 
1562aa2cb9eSKristoffer Ericson 		/* Read data */
1570af28408SPaul Mundt 		*s++ = __raw_readb(PCDR);
1580af28408SPaul Mundt 		*s++ = __raw_readb(PFDR);
1592aa2cb9eSKristoffer Ericson 	}
1602aa2cb9eSKristoffer Ericson 	/* Scan no lines */
1610af28408SPaul Mundt 	__raw_writeb(0xff, PDDR);
1620af28408SPaul Mundt 	__raw_writeb(0xff, PEDR);
1632aa2cb9eSKristoffer Ericson 
1642aa2cb9eSKristoffer Ericson 	/* Enable all scanlines */
1650af28408SPaul Mundt 	__raw_writew((dc_static | (0x5555 & 0xcc0c)),PDCR);
1660af28408SPaul Mundt 	__raw_writew((ec_static | (0x5555 & 0xf0cf)),PECR);
1672aa2cb9eSKristoffer Ericson 
1682aa2cb9eSKristoffer Ericson 	/* Ignore extra keys and events */
1690af28408SPaul Mundt 	*s++ = __raw_readb(PGDR);
1700af28408SPaul Mundt 	*s++ = __raw_readb(PHDR);
1712aa2cb9eSKristoffer Ericson }
1722aa2cb9eSKristoffer Ericson 
1732aa2cb9eSKristoffer Ericson static void jornadakbd680_poll(struct input_polled_dev *dev)
1742aa2cb9eSKristoffer Ericson {
1752aa2cb9eSKristoffer Ericson 	struct jornadakbd *jornadakbd = dev->private;
1762aa2cb9eSKristoffer Ericson 
1772aa2cb9eSKristoffer Ericson 	jornada_scan_keyb(jornadakbd->new_scan);
1782aa2cb9eSKristoffer Ericson 	jornada_parse_kbd(jornadakbd);
1792aa2cb9eSKristoffer Ericson 	memcpy(jornadakbd->old_scan, jornadakbd->new_scan, JORNADA_SCAN_SIZE);
1802aa2cb9eSKristoffer Ericson }
1812aa2cb9eSKristoffer Ericson 
1822aa2cb9eSKristoffer Ericson static int __devinit jornada680kbd_probe(struct platform_device *pdev)
1832aa2cb9eSKristoffer Ericson {
1842aa2cb9eSKristoffer Ericson 	struct jornadakbd *jornadakbd;
1852aa2cb9eSKristoffer Ericson 	struct input_polled_dev *poll_dev;
1862aa2cb9eSKristoffer Ericson 	struct input_dev *input_dev;
1872aa2cb9eSKristoffer Ericson 	int i, error;
1882aa2cb9eSKristoffer Ericson 
1892aa2cb9eSKristoffer Ericson 	jornadakbd = kzalloc(sizeof(struct jornadakbd), GFP_KERNEL);
1902aa2cb9eSKristoffer Ericson 	if (!jornadakbd)
1912aa2cb9eSKristoffer Ericson 		return -ENOMEM;
1922aa2cb9eSKristoffer Ericson 
1932aa2cb9eSKristoffer Ericson 	poll_dev = input_allocate_polled_device();
1942aa2cb9eSKristoffer Ericson 	if (!poll_dev) {
1952aa2cb9eSKristoffer Ericson 		error = -ENOMEM;
1962aa2cb9eSKristoffer Ericson 		goto failed;
1972aa2cb9eSKristoffer Ericson 	}
1982aa2cb9eSKristoffer Ericson 
1992aa2cb9eSKristoffer Ericson 	platform_set_drvdata(pdev, jornadakbd);
2002aa2cb9eSKristoffer Ericson 
2012aa2cb9eSKristoffer Ericson 	jornadakbd->poll_dev = poll_dev;
2022aa2cb9eSKristoffer Ericson 
2032aa2cb9eSKristoffer Ericson 	memcpy(jornadakbd->keymap, jornada_scancodes,
2042aa2cb9eSKristoffer Ericson 		sizeof(jornadakbd->keymap));
2052aa2cb9eSKristoffer Ericson 
2062aa2cb9eSKristoffer Ericson 	poll_dev->private = jornadakbd;
2072aa2cb9eSKristoffer Ericson 	poll_dev->poll = jornadakbd680_poll;
2082aa2cb9eSKristoffer Ericson 	poll_dev->poll_interval = 50; /* msec */
2092aa2cb9eSKristoffer Ericson 
2102aa2cb9eSKristoffer Ericson 	input_dev = poll_dev->input;
2112aa2cb9eSKristoffer Ericson 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
2122aa2cb9eSKristoffer Ericson 	input_dev->name = "HP Jornada 680 keyboard";
2132aa2cb9eSKristoffer Ericson 	input_dev->phys = "jornadakbd/input0";
2142aa2cb9eSKristoffer Ericson 	input_dev->keycode = jornadakbd->keymap;
2152aa2cb9eSKristoffer Ericson 	input_dev->keycodesize = sizeof(unsigned short);
2162aa2cb9eSKristoffer Ericson 	input_dev->keycodemax = ARRAY_SIZE(jornada_scancodes);
2172aa2cb9eSKristoffer Ericson 	input_dev->dev.parent = &pdev->dev;
2182aa2cb9eSKristoffer Ericson 	input_dev->id.bustype = BUS_HOST;
2192aa2cb9eSKristoffer Ericson 
2202aa2cb9eSKristoffer Ericson 	for (i = 0; i < 128; i++)
2212aa2cb9eSKristoffer Ericson 		if (jornadakbd->keymap[i])
2222aa2cb9eSKristoffer Ericson 			__set_bit(jornadakbd->keymap[i], input_dev->keybit);
2232aa2cb9eSKristoffer Ericson 	__clear_bit(KEY_RESERVED, input_dev->keybit);
2242aa2cb9eSKristoffer Ericson 
2252aa2cb9eSKristoffer Ericson 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
2262aa2cb9eSKristoffer Ericson 
2272aa2cb9eSKristoffer Ericson 	error = input_register_polled_device(jornadakbd->poll_dev);
2282aa2cb9eSKristoffer Ericson 	if (error)
2292aa2cb9eSKristoffer Ericson 		goto failed;
2302aa2cb9eSKristoffer Ericson 
2312aa2cb9eSKristoffer Ericson 	return 0;
2322aa2cb9eSKristoffer Ericson 
2332aa2cb9eSKristoffer Ericson  failed:
2342aa2cb9eSKristoffer Ericson 	printk(KERN_ERR "Jornadakbd: failed to register driver, error: %d\n",
2352aa2cb9eSKristoffer Ericson 		error);
2362aa2cb9eSKristoffer Ericson 	platform_set_drvdata(pdev, NULL);
2372aa2cb9eSKristoffer Ericson 	input_free_polled_device(poll_dev);
2382aa2cb9eSKristoffer Ericson 	kfree(jornadakbd);
2392aa2cb9eSKristoffer Ericson 	return error;
2402aa2cb9eSKristoffer Ericson 
2412aa2cb9eSKristoffer Ericson }
2422aa2cb9eSKristoffer Ericson 
2432aa2cb9eSKristoffer Ericson static int __devexit jornada680kbd_remove(struct platform_device *pdev)
2442aa2cb9eSKristoffer Ericson {
2452aa2cb9eSKristoffer Ericson 	struct jornadakbd *jornadakbd = platform_get_drvdata(pdev);
2462aa2cb9eSKristoffer Ericson 
2472aa2cb9eSKristoffer Ericson 	platform_set_drvdata(pdev, NULL);
2482aa2cb9eSKristoffer Ericson 	input_unregister_polled_device(jornadakbd->poll_dev);
2492aa2cb9eSKristoffer Ericson 	input_free_polled_device(jornadakbd->poll_dev);
2502aa2cb9eSKristoffer Ericson 	kfree(jornadakbd);
2512aa2cb9eSKristoffer Ericson 
2522aa2cb9eSKristoffer Ericson 	return 0;
2532aa2cb9eSKristoffer Ericson }
2542aa2cb9eSKristoffer Ericson 
2552aa2cb9eSKristoffer Ericson static struct platform_driver jornada680kbd_driver = {
2562aa2cb9eSKristoffer Ericson 	.driver	= {
2572aa2cb9eSKristoffer Ericson 		.name	= "jornada680_kbd",
258d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
2592aa2cb9eSKristoffer Ericson 	},
2602aa2cb9eSKristoffer Ericson 	.probe	= jornada680kbd_probe,
2612aa2cb9eSKristoffer Ericson 	.remove	= __devexit_p(jornada680kbd_remove),
2622aa2cb9eSKristoffer Ericson };
263*5146c84fSJJ Ding module_platform_driver(jornada680kbd_driver);
2642aa2cb9eSKristoffer Ericson 
2652aa2cb9eSKristoffer Ericson MODULE_AUTHOR("Kristoffer Ericson <kristoffer.ericson@gmail.com>");
2662aa2cb9eSKristoffer Ericson MODULE_DESCRIPTION("HP Jornada 620/660/680/690 Keyboard Driver");
2672aa2cb9eSKristoffer Ericson MODULE_LICENSE("GPL v2");
268d7b5247bSKay Sievers MODULE_ALIAS("platform:jornada680_kbd");
269