1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 22aa2cb9eSKristoffer Ericson /* 32aa2cb9eSKristoffer Ericson * drivers/input/keyboard/jornada680_kbd.c 42aa2cb9eSKristoffer Ericson * 52aa2cb9eSKristoffer Ericson * HP Jornada 620/660/680/690 scan keyboard platform driver 62aa2cb9eSKristoffer Ericson * Copyright (C) 2007 Kristoffer Ericson <Kristoffer.Ericson@gmail.com> 72aa2cb9eSKristoffer Ericson * 82aa2cb9eSKristoffer Ericson * Based on hp680_keyb.c 92aa2cb9eSKristoffer Ericson * Copyright (C) 2006 Paul Mundt 102aa2cb9eSKristoffer Ericson * Copyright (C) 2005 Andriy Skulysh 112aa2cb9eSKristoffer Ericson * Split from drivers/input/keyboard/hp600_keyb.c 122aa2cb9eSKristoffer Ericson * Copyright (C) 2000 Yaegashi Takeshi (hp6xx kbd scan routine and translation table) 132aa2cb9eSKristoffer Ericson * Copyright (C) 2000 Niibe Yutaka (HP620 Keyb translation table) 142aa2cb9eSKristoffer Ericson */ 152aa2cb9eSKristoffer Ericson 1650525cb3SHimangi Saraogi #include <linux/device.h> 172aa2cb9eSKristoffer Ericson #include <linux/input.h> 184c64681eSKristoffer Ericson #include <linux/interrupt.h> 194c64681eSKristoffer Ericson #include <linux/jiffies.h> 202aa2cb9eSKristoffer Ericson #include <linux/kernel.h> 212aa2cb9eSKristoffer Ericson #include <linux/module.h> 222aa2cb9eSKristoffer Ericson #include <linux/platform_device.h> 235a0e3ad6STejun Heo #include <linux/slab.h> 242aa2cb9eSKristoffer Ericson 252aa2cb9eSKristoffer Ericson #include <asm/delay.h> 262aa2cb9eSKristoffer Ericson #include <asm/io.h> 272aa2cb9eSKristoffer Ericson 282aa2cb9eSKristoffer Ericson #define PCCR 0xa4000104 292aa2cb9eSKristoffer Ericson #define PDCR 0xa4000106 302aa2cb9eSKristoffer Ericson #define PECR 0xa4000108 312aa2cb9eSKristoffer Ericson #define PFCR 0xa400010a 322aa2cb9eSKristoffer Ericson #define PCDR 0xa4000124 332aa2cb9eSKristoffer Ericson #define PDDR 0xa4000126 342aa2cb9eSKristoffer Ericson #define PEDR 0xa4000128 352aa2cb9eSKristoffer Ericson #define PFDR 0xa400012a 362aa2cb9eSKristoffer Ericson #define PGDR 0xa400012c 372aa2cb9eSKristoffer Ericson #define PHDR 0xa400012e 382aa2cb9eSKristoffer Ericson #define PJDR 0xa4000130 392aa2cb9eSKristoffer Ericson #define PKDR 0xa4000132 402aa2cb9eSKristoffer Ericson #define PLDR 0xa4000134 412aa2cb9eSKristoffer Ericson 422aa2cb9eSKristoffer Ericson static const unsigned short jornada_scancodes[] = { 434c64681eSKristoffer Ericson /* PTD1 */ KEY_CAPSLOCK, KEY_MACRO, KEY_LEFTCTRL, 0, KEY_ESC, KEY_KP5, 0, 0, /* 1 -> 8 */ 444c64681eSKristoffer Ericson KEY_F1, KEY_F2, KEY_F3, KEY_F8, KEY_F7, KEY_F6, KEY_F4, KEY_F5, /* 9 -> 16 */ 452aa2cb9eSKristoffer Ericson /* PTD5 */ KEY_SLASH, KEY_APOSTROPHE, KEY_ENTER, 0, KEY_Z, 0, 0, 0, /* 17 -> 24 */ 462aa2cb9eSKristoffer Ericson KEY_X, KEY_C, KEY_V, KEY_DOT, KEY_COMMA, KEY_M, KEY_B, KEY_N, /* 25 -> 32 */ 474c64681eSKristoffer Ericson /* PTD7 */ KEY_KP2, KEY_KP6, KEY_KP3, 0, 0, 0, 0, 0, /* 33 -> 40 */ 484c64681eSKristoffer Ericson KEY_F10, KEY_RO, KEY_F9, KEY_KP4, KEY_NUMLOCK, KEY_SCROLLLOCK, KEY_LEFTALT, KEY_HANJA, /* 41 -> 48 */ 494c64681eSKristoffer Ericson /* PTE0 */ KEY_KATAKANA, KEY_KP0, KEY_GRAVE, 0, KEY_FINANCE, 0, 0, 0, /* 49 -> 56 */ 504c64681eSKristoffer Ericson KEY_KPMINUS, KEY_HIRAGANA, KEY_SPACE, KEY_KPDOT, KEY_VOLUMEUP, 249, 0, 0, /* 57 -> 64 */ 512aa2cb9eSKristoffer Ericson /* PTE1 */ KEY_SEMICOLON, KEY_RIGHTBRACE, KEY_BACKSLASH, 0, KEY_A, 0, 0, 0, /* 65 -> 72 */ 522aa2cb9eSKristoffer Ericson KEY_S, KEY_D, KEY_F, KEY_L, KEY_K, KEY_J, KEY_G, KEY_H, /* 73 -> 80 */ 532aa2cb9eSKristoffer Ericson /* PTE3 */ KEY_KP8, KEY_LEFTMETA, KEY_RIGHTSHIFT, 0, KEY_TAB, 0, 0, 0, /* 81 -> 88 */ 544c64681eSKristoffer Ericson 0, KEY_LEFTSHIFT, KEY_KP7, KEY_KP9, KEY_KP1, KEY_F11, KEY_KPPLUS, KEY_KPASTERISK, /* 89 -> 96 */ 552aa2cb9eSKristoffer Ericson /* PTE6 */ KEY_P, KEY_LEFTBRACE, KEY_BACKSPACE, 0, KEY_Q, 0, 0, 0, /* 97 -> 104 */ 564c64681eSKristoffer Ericson KEY_W, KEY_E, KEY_R, KEY_O, KEY_I, KEY_U, KEY_T, KEY_Y, /* 105 -> 112 */ 572aa2cb9eSKristoffer Ericson /* PTE7 */ KEY_0, KEY_MINUS, KEY_EQUAL, 0, KEY_1, 0, 0, 0, /* 113 -> 120 */ 582aa2cb9eSKristoffer Ericson KEY_2, KEY_3, KEY_4, KEY_9, KEY_8, KEY_7, KEY_5, KEY_6, /* 121 -> 128 */ 592aa2cb9eSKristoffer Ericson /* **** */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 602aa2cb9eSKristoffer Ericson 0, 0, 0, 0, 0 612aa2cb9eSKristoffer Ericson }; 622aa2cb9eSKristoffer Ericson 632aa2cb9eSKristoffer Ericson #define JORNADA_SCAN_SIZE 18 642aa2cb9eSKristoffer Ericson 652aa2cb9eSKristoffer Ericson struct jornadakbd { 66*c028c44fSDmitry Torokhov struct input_dev *input; 672aa2cb9eSKristoffer Ericson unsigned short keymap[ARRAY_SIZE(jornada_scancodes)]; 682aa2cb9eSKristoffer Ericson unsigned char length; 692aa2cb9eSKristoffer Ericson unsigned char old_scan[JORNADA_SCAN_SIZE]; 702aa2cb9eSKristoffer Ericson unsigned char new_scan[JORNADA_SCAN_SIZE]; 712aa2cb9eSKristoffer Ericson }; 722aa2cb9eSKristoffer Ericson 732aa2cb9eSKristoffer Ericson static void jornada_parse_kbd(struct jornadakbd *jornadakbd) 742aa2cb9eSKristoffer Ericson { 75*c028c44fSDmitry Torokhov struct input_dev *input_dev = jornadakbd->input; 762aa2cb9eSKristoffer Ericson unsigned short *keymap = jornadakbd->keymap; 772aa2cb9eSKristoffer Ericson unsigned int sync_me = 0; 782aa2cb9eSKristoffer Ericson unsigned int i, j; 792aa2cb9eSKristoffer Ericson 802aa2cb9eSKristoffer Ericson for (i = 0; i < JORNADA_SCAN_SIZE; i++) { 812aa2cb9eSKristoffer Ericson unsigned char new = jornadakbd->new_scan[i]; 822aa2cb9eSKristoffer Ericson unsigned char old = jornadakbd->old_scan[i]; 832aa2cb9eSKristoffer Ericson unsigned int xor = new ^ old; 842aa2cb9eSKristoffer Ericson 852aa2cb9eSKristoffer Ericson if (xor == 0) 862aa2cb9eSKristoffer Ericson continue; 872aa2cb9eSKristoffer Ericson 882aa2cb9eSKristoffer Ericson for (j = 0; j < 8; j++) { 892aa2cb9eSKristoffer Ericson unsigned int bit = 1 << j; 902aa2cb9eSKristoffer Ericson if (xor & bit) { 912aa2cb9eSKristoffer Ericson unsigned int scancode = (i << 3) + j; 922aa2cb9eSKristoffer Ericson input_event(input_dev, 932aa2cb9eSKristoffer Ericson EV_MSC, MSC_SCAN, scancode); 942aa2cb9eSKristoffer Ericson input_report_key(input_dev, 952aa2cb9eSKristoffer Ericson keymap[scancode], 962aa2cb9eSKristoffer Ericson !(new & bit)); 972aa2cb9eSKristoffer Ericson sync_me = 1; 982aa2cb9eSKristoffer Ericson } 992aa2cb9eSKristoffer Ericson } 1002aa2cb9eSKristoffer Ericson } 1012aa2cb9eSKristoffer Ericson 1022aa2cb9eSKristoffer Ericson if (sync_me) 1032aa2cb9eSKristoffer Ericson input_sync(input_dev); 1042aa2cb9eSKristoffer Ericson } 1052aa2cb9eSKristoffer Ericson 1062aa2cb9eSKristoffer Ericson static void jornada_scan_keyb(unsigned char *s) 1072aa2cb9eSKristoffer Ericson { 1082aa2cb9eSKristoffer Ericson int i; 1092aa2cb9eSKristoffer Ericson unsigned short ec_static, dc_static; /* = UINT16_t */ 1102aa2cb9eSKristoffer Ericson unsigned char matrix_switch[] = { 1112aa2cb9eSKristoffer Ericson 0xfd, 0xff, /* PTD1 PD(1) */ 1122aa2cb9eSKristoffer Ericson 0xdf, 0xff, /* PTD5 PD(5) */ 1132aa2cb9eSKristoffer Ericson 0x7f, 0xff, /* PTD7 PD(7) */ 1142aa2cb9eSKristoffer Ericson 0xff, 0xfe, /* PTE0 PE(0) */ 1152aa2cb9eSKristoffer Ericson 0xff, 0xfd, /* PTE1 PE(1) */ 1162aa2cb9eSKristoffer Ericson 0xff, 0xf7, /* PTE3 PE(3) */ 1172aa2cb9eSKristoffer Ericson 0xff, 0xbf, /* PTE6 PE(6) */ 1182aa2cb9eSKristoffer Ericson 0xff, 0x7f, /* PTE7 PE(7) */ 1192aa2cb9eSKristoffer Ericson }, *t = matrix_switch; 1202aa2cb9eSKristoffer Ericson /* PD(x) : 1212aa2cb9eSKristoffer Ericson 1. 0xcc0c & (1~(1 << (2*(x)+1))))) 1222aa2cb9eSKristoffer Ericson 2. (0xf0cf & 0xfffff) */ 1232aa2cb9eSKristoffer Ericson /* PE(x) : 1242aa2cb9eSKristoffer Ericson 1. 0xcc0c & 0xffff 1252aa2cb9eSKristoffer Ericson 2. 0xf0cf & (1~(1 << (2*(x)+1))))) */ 1262aa2cb9eSKristoffer Ericson unsigned short matrix_PDE[] = { 1272aa2cb9eSKristoffer Ericson 0xcc04, 0xf0cf, /* PD(1) */ 1282aa2cb9eSKristoffer Ericson 0xc40c, 0xf0cf, /* PD(5) */ 1292aa2cb9eSKristoffer Ericson 0x4c0c, 0xf0cf, /* PD(7) */ 1302aa2cb9eSKristoffer Ericson 0xcc0c, 0xf0cd, /* PE(0) */ 1312aa2cb9eSKristoffer Ericson 0xcc0c, 0xf0c7, /* PE(1) */ 1322aa2cb9eSKristoffer Ericson 0xcc0c, 0xf04f, /* PE(3) */ 1332aa2cb9eSKristoffer Ericson 0xcc0c, 0xd0cf, /* PE(6) */ 1342aa2cb9eSKristoffer Ericson 0xcc0c, 0x70cf, /* PE(7) */ 1352aa2cb9eSKristoffer Ericson }, *y = matrix_PDE; 1362aa2cb9eSKristoffer Ericson 1372aa2cb9eSKristoffer Ericson /* Save these control reg bits */ 1380af28408SPaul Mundt dc_static = (__raw_readw(PDCR) & (~0xcc0c)); 1390af28408SPaul Mundt ec_static = (__raw_readw(PECR) & (~0xf0cf)); 1402aa2cb9eSKristoffer Ericson 1412aa2cb9eSKristoffer Ericson for (i = 0; i < 8; i++) { 1422aa2cb9eSKristoffer Ericson /* disable output for all but the one we want to scan */ 1430af28408SPaul Mundt __raw_writew((dc_static | *y++), PDCR); 1440af28408SPaul Mundt __raw_writew((ec_static | *y++), PECR); 1452aa2cb9eSKristoffer Ericson udelay(5); 1462aa2cb9eSKristoffer Ericson 1472aa2cb9eSKristoffer Ericson /* Get scanline row */ 1480af28408SPaul Mundt __raw_writeb(*t++, PDDR); 1490af28408SPaul Mundt __raw_writeb(*t++, PEDR); 1502aa2cb9eSKristoffer Ericson udelay(50); 1512aa2cb9eSKristoffer Ericson 1522aa2cb9eSKristoffer Ericson /* Read data */ 1530af28408SPaul Mundt *s++ = __raw_readb(PCDR); 1540af28408SPaul Mundt *s++ = __raw_readb(PFDR); 1552aa2cb9eSKristoffer Ericson } 1562aa2cb9eSKristoffer Ericson /* Scan no lines */ 1570af28408SPaul Mundt __raw_writeb(0xff, PDDR); 1580af28408SPaul Mundt __raw_writeb(0xff, PEDR); 1592aa2cb9eSKristoffer Ericson 1602aa2cb9eSKristoffer Ericson /* Enable all scanlines */ 1610af28408SPaul Mundt __raw_writew((dc_static | (0x5555 & 0xcc0c)),PDCR); 1620af28408SPaul Mundt __raw_writew((ec_static | (0x5555 & 0xf0cf)),PECR); 1632aa2cb9eSKristoffer Ericson 1642aa2cb9eSKristoffer Ericson /* Ignore extra keys and events */ 1650af28408SPaul Mundt *s++ = __raw_readb(PGDR); 1660af28408SPaul Mundt *s++ = __raw_readb(PHDR); 1672aa2cb9eSKristoffer Ericson } 1682aa2cb9eSKristoffer Ericson 169*c028c44fSDmitry Torokhov static void jornadakbd680_poll(struct input_dev *input) 1702aa2cb9eSKristoffer Ericson { 171*c028c44fSDmitry Torokhov struct jornadakbd *jornadakbd = input_get_drvdata(input); 1722aa2cb9eSKristoffer Ericson 1732aa2cb9eSKristoffer Ericson jornada_scan_keyb(jornadakbd->new_scan); 1742aa2cb9eSKristoffer Ericson jornada_parse_kbd(jornadakbd); 1752aa2cb9eSKristoffer Ericson memcpy(jornadakbd->old_scan, jornadakbd->new_scan, JORNADA_SCAN_SIZE); 1762aa2cb9eSKristoffer Ericson } 1772aa2cb9eSKristoffer Ericson 1785298cc4cSBill Pemberton static int jornada680kbd_probe(struct platform_device *pdev) 1792aa2cb9eSKristoffer Ericson { 1802aa2cb9eSKristoffer Ericson struct jornadakbd *jornadakbd; 1812aa2cb9eSKristoffer Ericson struct input_dev *input_dev; 1822aa2cb9eSKristoffer Ericson int i, error; 1832aa2cb9eSKristoffer Ericson 18450525cb3SHimangi Saraogi jornadakbd = devm_kzalloc(&pdev->dev, sizeof(struct jornadakbd), 18550525cb3SHimangi Saraogi GFP_KERNEL); 1862aa2cb9eSKristoffer Ericson if (!jornadakbd) 1872aa2cb9eSKristoffer Ericson return -ENOMEM; 1882aa2cb9eSKristoffer Ericson 189*c028c44fSDmitry Torokhov input_dev = devm_input_allocate_device(&pdev->dev); 190*c028c44fSDmitry Torokhov if (!input_dev) { 191*c028c44fSDmitry Torokhov dev_err(&pdev->dev, "failed to allocate input device\n"); 19250525cb3SHimangi Saraogi return -ENOMEM; 1932aa2cb9eSKristoffer Ericson } 1942aa2cb9eSKristoffer Ericson 195*c028c44fSDmitry Torokhov jornadakbd->input = input_dev; 1962aa2cb9eSKristoffer Ericson 1972aa2cb9eSKristoffer Ericson memcpy(jornadakbd->keymap, jornada_scancodes, 1982aa2cb9eSKristoffer Ericson sizeof(jornadakbd->keymap)); 1992aa2cb9eSKristoffer Ericson 200*c028c44fSDmitry Torokhov input_set_drvdata(input_dev, jornadakbd); 2012aa2cb9eSKristoffer Ericson input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP); 2022aa2cb9eSKristoffer Ericson input_dev->name = "HP Jornada 680 keyboard"; 2032aa2cb9eSKristoffer Ericson input_dev->phys = "jornadakbd/input0"; 2042aa2cb9eSKristoffer Ericson input_dev->keycode = jornadakbd->keymap; 2052aa2cb9eSKristoffer Ericson input_dev->keycodesize = sizeof(unsigned short); 2062aa2cb9eSKristoffer Ericson input_dev->keycodemax = ARRAY_SIZE(jornada_scancodes); 2072aa2cb9eSKristoffer Ericson input_dev->id.bustype = BUS_HOST; 2082aa2cb9eSKristoffer Ericson 2092aa2cb9eSKristoffer Ericson for (i = 0; i < 128; i++) 2102aa2cb9eSKristoffer Ericson if (jornadakbd->keymap[i]) 2112aa2cb9eSKristoffer Ericson __set_bit(jornadakbd->keymap[i], input_dev->keybit); 2122aa2cb9eSKristoffer Ericson __clear_bit(KEY_RESERVED, input_dev->keybit); 2132aa2cb9eSKristoffer Ericson 2142aa2cb9eSKristoffer Ericson input_set_capability(input_dev, EV_MSC, MSC_SCAN); 2152aa2cb9eSKristoffer Ericson 216*c028c44fSDmitry Torokhov error = input_setup_polling(input_dev, jornadakbd680_poll); 21750525cb3SHimangi Saraogi if (error) { 218*c028c44fSDmitry Torokhov dev_err(&pdev->dev, "failed to set up polling\n"); 219*c028c44fSDmitry Torokhov return error; 220*c028c44fSDmitry Torokhov } 221*c028c44fSDmitry Torokhov 222*c028c44fSDmitry Torokhov input_set_poll_interval(input_dev, 50 /* msec */); 223*c028c44fSDmitry Torokhov 224*c028c44fSDmitry Torokhov error = input_register_device(input_dev); 225*c028c44fSDmitry Torokhov if (error) { 226*c028c44fSDmitry Torokhov dev_err(&pdev->dev, "failed to register input device\n"); 2272aa2cb9eSKristoffer Ericson return error; 2282aa2cb9eSKristoffer Ericson } 2292aa2cb9eSKristoffer Ericson 2302aa2cb9eSKristoffer Ericson return 0; 2312aa2cb9eSKristoffer Ericson } 2322aa2cb9eSKristoffer Ericson 2332aa2cb9eSKristoffer Ericson static struct platform_driver jornada680kbd_driver = { 2342aa2cb9eSKristoffer Ericson .driver = { 2352aa2cb9eSKristoffer Ericson .name = "jornada680_kbd", 2362aa2cb9eSKristoffer Ericson }, 2372aa2cb9eSKristoffer Ericson .probe = jornada680kbd_probe, 2382aa2cb9eSKristoffer Ericson }; 2395146c84fSJJ Ding module_platform_driver(jornada680kbd_driver); 2402aa2cb9eSKristoffer Ericson 2412aa2cb9eSKristoffer Ericson MODULE_AUTHOR("Kristoffer Ericson <kristoffer.ericson@gmail.com>"); 2422aa2cb9eSKristoffer Ericson MODULE_DESCRIPTION("HP Jornada 620/660/680/690 Keyboard Driver"); 2432aa2cb9eSKristoffer Ericson MODULE_LICENSE("GPL v2"); 244d7b5247bSKay Sievers MODULE_ALIAS("platform:jornada680_kbd"); 245