1/*
2 * nvec_kbd: keyboard driver for a NVIDIA compliant embedded controller
3 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
5 *
6 * Authors:  Pierre-Hugues Husson <phhusson@free.fr>
7 *           Marc Dietrich <marvin24@gmx.de>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License.  See the file "COPYING" in the main directory of this archive
11 * for more details.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/input.h>
18#include <linux/delay.h>
19#include <linux/platform_device.h>
20
21#include "nvec-keytable.h"
22#include "nvec.h"
23
24enum kbd_subcmds {
25	CNFG_WAKE = 3,
26	CNFG_WAKE_KEY_REPORTING,
27	SET_LEDS = 0xed,
28	ENABLE_KBD = 0xf4,
29	DISABLE_KBD,
30};
31
32static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
33			      + ARRAY_SIZE(extcode_tab_us102)];
34
35struct nvec_keys {
36	struct input_dev *input;
37	struct notifier_block notifier;
38	struct nvec_chip *nvec;
39	bool caps_lock;
40};
41
42static struct nvec_keys keys_dev;
43
44static void nvec_kbd_toggle_led(void)
45{
46	char buf[] = { NVEC_KBD, SET_LEDS, 0 };
47
48	keys_dev.caps_lock = !keys_dev.caps_lock;
49
50	if (keys_dev.caps_lock)
51		/* should be BIT(0) only, firmware bug? */
52		buf[2] = BIT(0) | BIT(1) | BIT(2);
53
54	nvec_write_async(keys_dev.nvec, buf, sizeof(buf));
55}
56
57static int nvec_keys_notifier(struct notifier_block *nb,
58			      unsigned long event_type, void *data)
59{
60	int code, state;
61	unsigned char *msg = (unsigned char *)data;
62
63	if (event_type == NVEC_KB_EVT) {
64		int _size = (msg[0] & (3 << 5)) >> 5;
65
66/* power on/off button */
67		if (_size == NVEC_VAR_SIZE)
68			return NOTIFY_STOP;
69
70		if (_size == NVEC_3BYTES)
71			msg++;
72
73		code = msg[1] & 0x7f;
74		state = msg[1] & 0x80;
75
76		if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
77			nvec_kbd_toggle_led();
78
79		input_report_key(keys_dev.input, code_tabs[_size][code],
80				 !state);
81		input_sync(keys_dev.input);
82
83		return NOTIFY_STOP;
84	}
85
86	return NOTIFY_DONE;
87}
88
89static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
90			  unsigned int code, int value)
91{
92	struct nvec_chip *nvec = keys_dev.nvec;
93	char buf[] = { NVEC_KBD, SET_LEDS, 0 };
94
95	if (type == EV_REP)
96		return 0;
97
98	if (type != EV_LED)
99		return -1;
100
101	if (code != LED_CAPSL)
102		return -1;
103
104	buf[2] = !!value;
105	nvec_write_async(nvec, buf, sizeof(buf));
106
107	return 0;
108}
109
110static int nvec_kbd_probe(struct platform_device *pdev)
111{
112	struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
113	int i, j, err;
114	struct input_dev *idev;
115	char	clear_leds[] = { NVEC_KBD, SET_LEDS, 0 },
116		enable_kbd[] = { NVEC_KBD, ENABLE_KBD },
117		cnfg_wake[] = { NVEC_KBD, CNFG_WAKE, true, true },
118		cnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
119						true };
120
121	j = 0;
122
123	for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
124		keycodes[j++] = code_tab_102us[i];
125
126	for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
127		keycodes[j++] = extcode_tab_us102[i];
128
129	idev = devm_input_allocate_device(&pdev->dev);
130	idev->name = "nvec keyboard";
131	idev->phys = "nvec";
132	idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_LED);
133	idev->ledbit[0] = BIT_MASK(LED_CAPSL);
134	idev->event = nvec_kbd_event;
135	idev->keycode = keycodes;
136	idev->keycodesize = sizeof(unsigned char);
137	idev->keycodemax = ARRAY_SIZE(keycodes);
138
139	for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
140		set_bit(keycodes[i], idev->keybit);
141
142	clear_bit(0, idev->keybit);
143	err = input_register_device(idev);
144	if (err)
145		return err;
146
147	keys_dev.input = idev;
148	keys_dev.notifier.notifier_call = nvec_keys_notifier;
149	keys_dev.nvec = nvec;
150	nvec_register_notifier(nvec, &keys_dev.notifier, 0);
151
152	/* Enable keyboard */
153	nvec_write_async(nvec, enable_kbd, 2);
154
155	/* configures wake on special keys */
156	nvec_write_async(nvec, cnfg_wake, 4);
157	/* enable wake key reporting */
158	nvec_write_async(nvec, cnfg_wake_key_reporting, 3);
159
160	/* Disable caps lock LED */
161	nvec_write_async(nvec, clear_leds, sizeof(clear_leds));
162
163	return 0;
164}
165
166static int nvec_kbd_remove(struct platform_device *pdev)
167{
168	struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
169	char disable_kbd[] = { NVEC_KBD, DISABLE_KBD },
170	     uncnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
171						false };
172	nvec_write_async(nvec, uncnfg_wake_key_reporting, 3);
173	nvec_write_async(nvec, disable_kbd, 2);
174	nvec_unregister_notifier(nvec, &keys_dev.notifier);
175
176	return 0;
177}
178
179static struct platform_driver nvec_kbd_driver = {
180	.probe  = nvec_kbd_probe,
181	.remove = nvec_kbd_remove,
182	.driver = {
183		.name = "nvec-kbd",
184	},
185};
186
187module_platform_driver(nvec_kbd_driver);
188
189MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
190MODULE_DESCRIPTION("NVEC keyboard driver");
191MODULE_ALIAS("platform:nvec-kbd");
192MODULE_LICENSE("GPL");
193