blob: c17a1c3eb3cad47899fca4f0e8c3db56bc3caab7 [file] [log] [blame]
Marc Dietrich162c7d82011-09-27 19:00:40 +02001/*
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
Julian Andres Klode79740352011-09-27 19:00:39 +020015#include <linux/module.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020016#include <linux/slab.h>
17#include <linux/input.h>
18#include <linux/delay.h>
Marc Dietrichf686e9a2011-08-24 20:23:07 +020019#include <linux/platform_device.h>
Marc Dietrich162c7d82011-09-27 19:00:40 +020020
Marc Dietrich32890b92011-05-19 16:34:42 +020021#include "nvec-keytable.h"
22#include "nvec.h"
23
Marc Dietrich93eff832013-01-27 17:43:43 +010024enum kbd_subcmds {
25 CNFG_WAKE = 3,
26 CNFG_WAKE_KEY_REPORTING,
27 SET_LEDS = 0xed,
28 ENABLE_KBD = 0xf4,
29 DISABLE_KBD,
30};
Marc Dietrich32890b92011-05-19 16:34:42 +020031
32static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
Marc Dietrich162c7d82011-09-27 19:00:40 +020033 + ARRAY_SIZE(extcode_tab_us102)];
Marc Dietrich32890b92011-05-19 16:34:42 +020034
35struct nvec_keys {
36 struct input_dev *input;
37 struct notifier_block notifier;
38 struct nvec_chip *nvec;
Julian Andres Klodeff169c12011-09-27 19:00:54 +020039 bool caps_lock;
Marc Dietrich32890b92011-05-19 16:34:42 +020040};
41
42static struct nvec_keys keys_dev;
43
Julian Andres Klodeff169c12011-09-27 19:00:54 +020044static void nvec_kbd_toggle_led(void)
45{
Marc Dietrich93eff832013-01-27 17:43:43 +010046 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
47
Julian Andres Klodeff169c12011-09-27 19:00:54 +020048 keys_dev.caps_lock = !keys_dev.caps_lock;
49
50 if (keys_dev.caps_lock)
Marc Dietrich93eff832013-01-27 17:43:43 +010051 /* 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));
Julian Andres Klodeff169c12011-09-27 19:00:54 +020055}
56
Marc Dietrich32890b92011-05-19 16:34:42 +020057static int nvec_keys_notifier(struct notifier_block *nb,
Marc Dietrich162c7d82011-09-27 19:00:40 +020058 unsigned long event_type, void *data)
Marc Dietrich32890b92011-05-19 16:34:42 +020059{
60 int code, state;
61 unsigned char *msg = (unsigned char *)data;
62
63 if (event_type == NVEC_KB_EVT) {
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020064 int _size = (msg[0] & (3 << 5)) >> 5;
Marc Dietrich32890b92011-05-19 16:34:42 +020065
66/* power on/off button */
Colin Brophy88d94e32011-07-15 13:08:16 +010067 if (_size == NVEC_VAR_SIZE)
Marc Dietrich32890b92011-05-19 16:34:42 +020068 return NOTIFY_STOP;
69
Colin Brophy88d94e32011-07-15 13:08:16 +010070 if (_size == NVEC_3BYTES)
Marc Dietrich32890b92011-05-19 16:34:42 +020071 msg++;
72
73 code = msg[1] & 0x7f;
74 state = msg[1] & 0x80;
75
Julian Andres Klodeff169c12011-09-27 19:00:54 +020076 if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
77 nvec_kbd_toggle_led();
78
Marc Dietrich162c7d82011-09-27 19:00:40 +020079 input_report_key(keys_dev.input, code_tabs[_size][code],
80 !state);
Marc Dietrich32890b92011-05-19 16:34:42 +020081 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,
Marc Dietrich162c7d82011-09-27 19:00:40 +020090 unsigned int code, int value)
Marc Dietrich32890b92011-05-19 16:34:42 +020091{
Marc Dietrich32890b92011-05-19 16:34:42 +020092 struct nvec_chip *nvec = keys_dev.nvec;
Marc Dietrich93eff832013-01-27 17:43:43 +010093 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
Marc Dietrich32890b92011-05-19 16:34:42 +020094
Colin Brophy88d94e32011-07-15 13:08:16 +010095 if (type == EV_REP)
Marc Dietrich32890b92011-05-19 16:34:42 +020096 return 0;
97
Colin Brophy88d94e32011-07-15 13:08:16 +010098 if (type != EV_LED)
Marc Dietrich32890b92011-05-19 16:34:42 +020099 return -1;
100
Colin Brophy88d94e32011-07-15 13:08:16 +0100101 if (code != LED_CAPSL)
Marc Dietrich32890b92011-05-19 16:34:42 +0200102 return -1;
103
104 buf[2] = !!value;
105 nvec_write_async(nvec, buf, sizeof(buf));
106
107 return 0;
108}
109
Bill Pemberton46620802012-11-19 13:22:07 -0500110static int nvec_kbd_probe(struct platform_device *pdev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200111{
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200112 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
Marc Dietrich32890b92011-05-19 16:34:42 +0200113 int i, j, err;
114 struct input_dev *idev;
Marc Dietrich93eff832013-01-27 17:43:43 +0100115 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 };
Marc Dietrich32890b92011-05-19 16:34:42 +0200120
121 j = 0;
122
Colin Brophy88d94e32011-07-15 13:08:16 +0100123 for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
Marc Dietrich32890b92011-05-19 16:34:42 +0200124 keycodes[j++] = code_tab_102us[i];
125
Colin Brophy88d94e32011-07-15 13:08:16 +0100126 for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
127 keycodes[j++] = extcode_tab_us102[i];
Marc Dietrich32890b92011-05-19 16:34:42 +0200128
Leon Romanovsky167bf092013-05-14 12:22:07 +0300129 idev = devm_input_allocate_device(&pdev->dev);
Ilya Petrov97cc2652011-09-27 19:00:44 +0200130 idev->name = "nvec keyboard";
131 idev->phys = "nvec";
Marc Dietrich32890b92011-05-19 16:34:42 +0200132 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
Colin Brophy88d94e32011-07-15 13:08:16 +0100139 for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
Marc Dietrich32890b92011-05-19 16:34:42 +0200140 set_bit(keycodes[i], idev->keybit);
141
142 clear_bit(0, idev->keybit);
143 err = input_register_device(idev);
Colin Brophy88d94e32011-07-15 13:08:16 +0100144 if (err)
Leon Romanovsky167bf092013-05-14 12:22:07 +0300145 return err;
Marc Dietrich32890b92011-05-19 16:34:42 +0200146
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 */
Marc Dietrich93eff832013-01-27 17:43:43 +0100153 nvec_write_async(nvec, enable_kbd, 2);
Marc Dietrich32890b92011-05-19 16:34:42 +0200154
Marc Dietrich93eff832013-01-27 17:43:43 +0100155 /* 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);
Marc Dietrich32890b92011-05-19 16:34:42 +0200159
Julian Andres Klodeff169c12011-09-27 19:00:54 +0200160 /* Disable caps lock LED */
Marc Dietrich93eff832013-01-27 17:43:43 +0100161 nvec_write_async(nvec, clear_leds, sizeof(clear_leds));
Julian Andres Klodeff169c12011-09-27 19:00:54 +0200162
Marc Dietrich32890b92011-05-19 16:34:42 +0200163 return 0;
Marc Dietrich32890b92011-05-19 16:34:42 +0200164}
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200165
Bill Pemberton1a6a8a82012-11-19 13:26:41 -0500166static int nvec_kbd_remove(struct platform_device *pdev)
Marc Dietrich3cdde3a2012-06-24 23:25:21 +0200167{
Marc Dietrichd398f562013-04-29 23:14:52 +0200168 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
Marc Dietrich3cdde3a2012-06-24 23:25:21 +0200176 return 0;
177}
178
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200179static struct platform_driver nvec_kbd_driver = {
Marc Dietrich162c7d82011-09-27 19:00:40 +0200180 .probe = nvec_kbd_probe,
Bill Pemberton44b90a32012-11-19 13:20:55 -0500181 .remove = nvec_kbd_remove,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200182 .driver = {
183 .name = "nvec-kbd",
184 .owner = THIS_MODULE,
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200185 },
186};
187
Marc Dietrich9891b1c2012-06-24 23:25:18 +0200188module_platform_driver(nvec_kbd_driver);
Marc Dietrich162c7d82011-09-27 19:00:40 +0200189
190MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
191MODULE_DESCRIPTION("NVEC keyboard driver");
Marc Dietrich38dc92e2013-04-29 23:14:50 +0200192MODULE_ALIAS("platform:nvec-kbd");
Marc Dietrich162c7d82011-09-27 19:00:40 +0200193MODULE_LICENSE("GPL");