Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / staging / nvec / nvec_kbd.c
blobc17a1c3eb3cad47899fca4f0e8c3db56bc3caab7
1 /*
2 * nvec_kbd: keyboard driver for a NVIDIA compliant embedded controller
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
6 * Authors: Pierre-Hugues Husson <phhusson@free.fr>
7 * Marc Dietrich <marvin24@gmx.de>
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.
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>
21 #include "nvec-keytable.h"
22 #include "nvec.h"
24 enum kbd_subcmds {
25 CNFG_WAKE = 3,
26 CNFG_WAKE_KEY_REPORTING,
27 SET_LEDS = 0xed,
28 ENABLE_KBD = 0xf4,
29 DISABLE_KBD,
32 static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
33 + ARRAY_SIZE(extcode_tab_us102)];
35 struct nvec_keys {
36 struct input_dev *input;
37 struct notifier_block notifier;
38 struct nvec_chip *nvec;
39 bool caps_lock;
42 static struct nvec_keys keys_dev;
44 static void nvec_kbd_toggle_led(void)
46 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
48 keys_dev.caps_lock = !keys_dev.caps_lock;
50 if (keys_dev.caps_lock)
51 /* should be BIT(0) only, firmware bug? */
52 buf[2] = BIT(0) | BIT(1) | BIT(2);
54 nvec_write_async(keys_dev.nvec, buf, sizeof(buf));
57 static int nvec_keys_notifier(struct notifier_block *nb,
58 unsigned long event_type, void *data)
60 int code, state;
61 unsigned char *msg = (unsigned char *)data;
63 if (event_type == NVEC_KB_EVT) {
64 int _size = (msg[0] & (3 << 5)) >> 5;
66 /* power on/off button */
67 if (_size == NVEC_VAR_SIZE)
68 return NOTIFY_STOP;
70 if (_size == NVEC_3BYTES)
71 msg++;
73 code = msg[1] & 0x7f;
74 state = msg[1] & 0x80;
76 if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
77 nvec_kbd_toggle_led();
79 input_report_key(keys_dev.input, code_tabs[_size][code],
80 !state);
81 input_sync(keys_dev.input);
83 return NOTIFY_STOP;
86 return NOTIFY_DONE;
89 static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
90 unsigned int code, int value)
92 struct nvec_chip *nvec = keys_dev.nvec;
93 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
95 if (type == EV_REP)
96 return 0;
98 if (type != EV_LED)
99 return -1;
101 if (code != LED_CAPSL)
102 return -1;
104 buf[2] = !!value;
105 nvec_write_async(nvec, buf, sizeof(buf));
107 return 0;
110 static int nvec_kbd_probe(struct platform_device *pdev)
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 };
121 j = 0;
123 for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
124 keycodes[j++] = code_tab_102us[i];
126 for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
127 keycodes[j++] = extcode_tab_us102[i];
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);
139 for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
140 set_bit(keycodes[i], idev->keybit);
142 clear_bit(0, idev->keybit);
143 err = input_register_device(idev);
144 if (err)
145 return err;
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);
152 /* Enable keyboard */
153 nvec_write_async(nvec, enable_kbd, 2);
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);
160 /* Disable caps lock LED */
161 nvec_write_async(nvec, clear_leds, sizeof(clear_leds));
163 return 0;
166 static int nvec_kbd_remove(struct platform_device *pdev)
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);
176 return 0;
179 static struct platform_driver nvec_kbd_driver = {
180 .probe = nvec_kbd_probe,
181 .remove = nvec_kbd_remove,
182 .driver = {
183 .name = "nvec-kbd",
184 .owner = THIS_MODULE,
188 module_platform_driver(nvec_kbd_driver);
190 MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
191 MODULE_DESCRIPTION("NVEC keyboard driver");
192 MODULE_ALIAS("platform:nvec-kbd");
193 MODULE_LICENSE("GPL");