1 // SPDX-License-Identifier: GPL-2.0
3 * nvec_kbd: keyboard driver for a NVIDIA compliant embedded controller
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/input.h>
14 #include <linux/delay.h>
15 #include <linux/platform_device.h>
17 #include "nvec-keytable.h"
22 CNFG_WAKE_KEY_REPORTING,
28 static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
29 + ARRAY_SIZE(extcode_tab_us102)];
32 struct input_dev *input;
33 struct notifier_block notifier;
34 struct nvec_chip *nvec;
38 static struct nvec_keys keys_dev;
40 static void nvec_kbd_toggle_led(void)
42 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
44 keys_dev.caps_lock = !keys_dev.caps_lock;
46 if (keys_dev.caps_lock)
47 /* should be BIT(0) only, firmware bug? */
48 buf[2] = BIT(0) | BIT(1) | BIT(2);
50 nvec_write_async(keys_dev.nvec, buf, sizeof(buf));
53 static int nvec_keys_notifier(struct notifier_block *nb,
54 unsigned long event_type, void *data)
57 unsigned char *msg = data;
59 if (event_type == NVEC_KB_EVT) {
60 int _size = (msg[0] & (3 << 5)) >> 5;
62 /* power on/off button */
63 if (_size == NVEC_VAR_SIZE)
66 if (_size == NVEC_3BYTES)
70 state = msg[1] & 0x80;
72 if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
73 nvec_kbd_toggle_led();
75 input_report_key(keys_dev.input, code_tabs[_size][code],
77 input_sync(keys_dev.input);
85 static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
86 unsigned int code, int value)
88 struct nvec_chip *nvec = keys_dev.nvec;
89 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
97 if (code != LED_CAPSL)
101 nvec_write_async(nvec, buf, sizeof(buf));
106 static int nvec_kbd_probe(struct platform_device *pdev)
108 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
110 struct input_dev *idev;
111 char clear_leds[] = { NVEC_KBD, SET_LEDS, 0 },
112 enable_kbd[] = { NVEC_KBD, ENABLE_KBD },
113 cnfg_wake[] = { NVEC_KBD, CNFG_WAKE, true, true },
114 cnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
119 for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
120 keycodes[j++] = code_tab_102us[i];
122 for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
123 keycodes[j++] = extcode_tab_us102[i];
125 idev = devm_input_allocate_device(&pdev->dev);
126 idev->name = "nvec keyboard";
128 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_LED);
129 idev->ledbit[0] = BIT_MASK(LED_CAPSL);
130 idev->event = nvec_kbd_event;
131 idev->keycode = keycodes;
132 idev->keycodesize = sizeof(unsigned char);
133 idev->keycodemax = ARRAY_SIZE(keycodes);
135 for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
136 set_bit(keycodes[i], idev->keybit);
138 clear_bit(0, idev->keybit);
139 err = input_register_device(idev);
143 keys_dev.input = idev;
144 keys_dev.notifier.notifier_call = nvec_keys_notifier;
145 keys_dev.nvec = nvec;
146 nvec_register_notifier(nvec, &keys_dev.notifier, 0);
148 /* Enable keyboard */
149 nvec_write_async(nvec, enable_kbd, 2);
151 /* configures wake on special keys */
152 nvec_write_async(nvec, cnfg_wake, 4);
153 /* enable wake key reporting */
154 nvec_write_async(nvec, cnfg_wake_key_reporting, 3);
156 /* Disable caps lock LED */
157 nvec_write_async(nvec, clear_leds, sizeof(clear_leds));
162 static int nvec_kbd_remove(struct platform_device *pdev)
164 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
165 char disable_kbd[] = { NVEC_KBD, DISABLE_KBD },
166 uncnfg_wake_key_reporting[] = { NVEC_KBD, CNFG_WAKE_KEY_REPORTING,
168 nvec_write_async(nvec, uncnfg_wake_key_reporting, 3);
169 nvec_write_async(nvec, disable_kbd, 2);
170 nvec_unregister_notifier(nvec, &keys_dev.notifier);
175 static struct platform_driver nvec_kbd_driver = {
176 .probe = nvec_kbd_probe,
177 .remove = nvec_kbd_remove,
183 module_platform_driver(nvec_kbd_driver);
186 MODULE_DESCRIPTION("NVEC keyboard driver");
187 MODULE_ALIAS("platform:nvec-kbd");
188 MODULE_LICENSE("GPL");