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