xref: /linux/drivers/input/keyboard/jornada720_kbd.c (revision 5be1f9d82fa73c199ebeee2866dbac83e419c897)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/input/keyboard/jornada720_kbd.c
4  *
5  * HP Jornada 720 keyboard platform driver
6  *
7  * Copyright (C) 2006/2007 Kristoffer Ericson <Kristoffer.Ericson@Gmail.com>
8  *
9  *    Copyright (C) 2006 jornada 720 kbd driver by
10 		Filip Zyzniewsk <Filip.Zyzniewski@tefnet.plX
11  *     based on (C) 2004 jornada 720 kbd driver by
12 		Alex Lange <chicken@handhelds.org>
13  */
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/interrupt.h>
17 #include <linux/input.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 
23 #include <mach/jornada720.h>
24 
25 MODULE_AUTHOR("Kristoffer Ericson <Kristoffer.Ericson@gmail.com>");
26 MODULE_DESCRIPTION("HP Jornada 710/720/728 keyboard driver");
27 MODULE_LICENSE("GPL v2");
28 
29 static unsigned short jornada_std_keymap[128] = {					/* ROW */
30 	0, KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7,		/* #1  */
31 	KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE,	/*  -> */
32 	0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9,		/* #2  */
33 	KEY_0, KEY_MINUS, KEY_EQUAL,0, 0, 0,						/*  -> */
34 	0, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O,		/* #3  */
35 	KEY_P, KEY_BACKSLASH, KEY_BACKSPACE, 0, 0, 0,					/*  -> */
36 	0, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L,		/* #4  */
37 	KEY_SEMICOLON, KEY_LEFTBRACE, KEY_RIGHTBRACE, 0, 0, 0,				/*  -> */
38 	0, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA,			/* #5  */
39 	KEY_DOT, KEY_KPMINUS, KEY_APOSTROPHE, KEY_ENTER, 0, 0,0,			/*  -> */
40 	0, KEY_TAB, 0, KEY_LEFTSHIFT, 0, KEY_APOSTROPHE, 0, 0, 0, 0,			/* #6  */
41 	KEY_UP, 0, KEY_RIGHTSHIFT, 0, 0, 0,0, 0, 0, 0, 0, KEY_LEFTALT, KEY_GRAVE,	/*  -> */
42 	0, 0, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0, 0,0, KEY_KPASTERISK,		/*  -> */
43 	KEY_LEFTCTRL, 0, KEY_SPACE, 0, 0, 0, KEY_SLASH, KEY_DELETE, 0, 0,		/*  -> */
44 	0, 0, 0, KEY_POWER,								/*  -> */
45 };
46 
47 struct jornadakbd {
48 	unsigned short keymap[ARRAY_SIZE(jornada_std_keymap)];
49 	struct input_dev *input;
50 };
51 
52 static irqreturn_t jornada720_kbd_interrupt(int irq, void *dev_id)
53 {
54 	struct platform_device *pdev = dev_id;
55 	struct jornadakbd *jornadakbd = platform_get_drvdata(pdev);
56 	struct input_dev *input = jornadakbd->input;
57 	u8 count, kbd_data, scan_code;
58 
59 	/* startup ssp with spinlock */
60 	jornada_ssp_start();
61 
62 	if (jornada_ssp_inout(GETSCANKEYCODE) != TXDUMMY) {
63 		dev_dbg(&pdev->dev,
64 			"GetKeycode command failed with ETIMEDOUT, flushed bus\n");
65 	} else {
66 		/* How many keycodes are waiting for us? */
67 		count = jornada_ssp_byte(TXDUMMY);
68 
69 		/* Lets drag them out one at a time */
70 		while (count--) {
71 			/* Exchange TxDummy for location (keymap[kbddata]) */
72 			kbd_data = jornada_ssp_byte(TXDUMMY);
73 			scan_code = kbd_data & 0x7f;
74 
75 			input_event(input, EV_MSC, MSC_SCAN, scan_code);
76 			input_report_key(input, jornadakbd->keymap[scan_code],
77 					 !(kbd_data & 0x80));
78 			input_sync(input);
79 		}
80 	}
81 
82 	/* release spinlock and turn off ssp */
83 	jornada_ssp_end();
84 
85 	return IRQ_HANDLED;
86 };
87 
88 static int jornada720_kbd_probe(struct platform_device *pdev)
89 {
90 	struct jornadakbd *jornadakbd;
91 	struct input_dev *input_dev;
92 	int i, err, irq;
93 
94 	irq = platform_get_irq(pdev, 0);
95 	if (irq <= 0)
96 		return irq < 0 ? irq : -EINVAL;
97 
98 	jornadakbd = devm_kzalloc(&pdev->dev, sizeof(*jornadakbd), GFP_KERNEL);
99 	input_dev = devm_input_allocate_device(&pdev->dev);
100 	if (!jornadakbd || !input_dev)
101 		return -ENOMEM;
102 
103 	platform_set_drvdata(pdev, jornadakbd);
104 
105 	memcpy(jornadakbd->keymap, jornada_std_keymap,
106 		sizeof(jornada_std_keymap));
107 	jornadakbd->input = input_dev;
108 
109 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
110 	input_dev->name = "HP Jornada 720 keyboard";
111 	input_dev->phys = "jornadakbd/input0";
112 	input_dev->keycode = jornadakbd->keymap;
113 	input_dev->keycodesize = sizeof(unsigned short);
114 	input_dev->keycodemax = ARRAY_SIZE(jornada_std_keymap);
115 	input_dev->id.bustype = BUS_HOST;
116 	input_dev->dev.parent = &pdev->dev;
117 
118 	for (i = 0; i < ARRAY_SIZE(jornadakbd->keymap); i++)
119 		__set_bit(jornadakbd->keymap[i], input_dev->keybit);
120 	__clear_bit(KEY_RESERVED, input_dev->keybit);
121 
122 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
123 
124 	err = devm_request_irq(&pdev->dev, irq, jornada720_kbd_interrupt,
125 			       IRQF_TRIGGER_FALLING, "jornadakbd", pdev);
126 	if (err) {
127 		dev_err(&pdev->dev, "unable to grab IRQ%d: %d\n", irq, err);
128 		return err;
129 	}
130 
131 	return input_register_device(jornadakbd->input);
132 };
133 
134 /* work with hotplug and coldplug */
135 MODULE_ALIAS("platform:jornada720_kbd");
136 
137 static struct platform_driver jornada720_kbd_driver = {
138 	.driver  = {
139 		.name    = "jornada720_kbd",
140 	 },
141 	.probe   = jornada720_kbd_probe,
142 };
143 module_platform_driver(jornada720_kbd_driver);
144