Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-block.git] / drivers / staging / nvec / nvec_kbd.c
CommitLineData
162c7d8c
MD
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
7974035c 15#include <linux/module.h>
32890b98
MD
16#include <linux/slab.h>
17#include <linux/input.h>
18#include <linux/delay.h>
f686e9af 19#include <linux/platform_device.h>
162c7d8c 20
32890b98
MD
21#include "nvec-keytable.h"
22#include "nvec.h"
23
93eff83f
MD
24enum kbd_subcmds {
25 CNFG_WAKE = 3,
26 CNFG_WAKE_KEY_REPORTING,
27 SET_LEDS = 0xed,
28 ENABLE_KBD = 0xf4,
29 DISABLE_KBD,
30};
32890b98
MD
31
32static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
162c7d8c 33 + ARRAY_SIZE(extcode_tab_us102)];
32890b98
MD
34
35struct nvec_keys {
36 struct input_dev *input;
37 struct notifier_block notifier;
38 struct nvec_chip *nvec;
ff169c14 39 bool caps_lock;
32890b98
MD
40};
41
42static struct nvec_keys keys_dev;
43
ff169c14
JAK
44static void nvec_kbd_toggle_led(void)
45{
93eff83f
MD
46 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
47
ff169c14
JAK
48 keys_dev.caps_lock = !keys_dev.caps_lock;
49
50 if (keys_dev.caps_lock)
93eff83f
MD
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));
ff169c14
JAK
55}
56
32890b98 57static int nvec_keys_notifier(struct notifier_block *nb,
162c7d8c 58 unsigned long event_type, void *data)
32890b98
MD
59{
60 int code, state;
61 unsigned char *msg = (unsigned char *)data;
62
63 if (event_type == NVEC_KB_EVT) {
0cab4cb8 64 int _size = (msg[0] & (3 << 5)) >> 5;
32890b98
MD
65
66/* power on/off button */
88d94e30 67 if (_size == NVEC_VAR_SIZE)
32890b98
MD
68 return NOTIFY_STOP;
69
88d94e30 70 if (_size == NVEC_3BYTES)
32890b98
MD
71 msg++;
72
73 code = msg[1] & 0x7f;
74 state = msg[1] & 0x80;
75
ff169c14
JAK
76 if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
77 nvec_kbd_toggle_led();
78
162c7d8c
MD
79 input_report_key(keys_dev.input, code_tabs[_size][code],
80 !state);
32890b98
MD
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,
162c7d8c 90 unsigned int code, int value)
32890b98 91{
32890b98 92 struct nvec_chip *nvec = keys_dev.nvec;
93eff83f 93 char buf[] = { NVEC_KBD, SET_LEDS, 0 };
32890b98 94
88d94e30 95 if (type == EV_REP)
32890b98
MD
96 return 0;
97
88d94e30 98 if (type != EV_LED)
32890b98
MD
99 return -1;
100
88d94e30 101 if (code != LED_CAPSL)
32890b98
MD
102 return -1;
103
104 buf[2] = !!value;
105 nvec_write_async(nvec, buf, sizeof(buf));
106
107 return 0;
108}
109
46620803 110static int nvec_kbd_probe(struct platform_device *pdev)
32890b98 111{
f686e9af 112 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
32890b98
MD
113 int i, j, err;
114 struct input_dev *idev;
93eff83f
MD
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 };
32890b98
MD
120
121 j = 0;
122
88d94e30 123 for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
32890b98
MD
124 keycodes[j++] = code_tab_102us[i];
125
88d94e30
CB
126 for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
127 keycodes[j++] = extcode_tab_us102[i];
32890b98 128
167bf09e 129 idev = devm_input_allocate_device(&pdev->dev);
97cc2657
IP
130 idev->name = "nvec keyboard";
131 idev->phys = "nvec";
32890b98
MD
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
88d94e30 139 for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
32890b98
MD
140 set_bit(keycodes[i], idev->keybit);
141
142 clear_bit(0, idev->keybit);
143 err = input_register_device(idev);
88d94e30 144 if (err)
167bf09e 145 return err;
32890b98
MD
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 */
93eff83f 153 nvec_write_async(nvec, enable_kbd, 2);
32890b98 154
93eff83f
MD
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);
32890b98 159
ff169c14 160 /* Disable caps lock LED */
93eff83f 161 nvec_write_async(nvec, clear_leds, sizeof(clear_leds));
ff169c14 162
32890b98 163 return 0;
32890b98 164}
f686e9af 165
1a6a8a84 166static int nvec_kbd_remove(struct platform_device *pdev)
3cdde3a3 167{
d398f56e
MD
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
3cdde3a3
MD
176 return 0;
177}
178
f686e9af 179static struct platform_driver nvec_kbd_driver = {
162c7d8c 180 .probe = nvec_kbd_probe,
44b90a3f 181 .remove = nvec_kbd_remove,
162c7d8c
MD
182 .driver = {
183 .name = "nvec-kbd",
184 .owner = THIS_MODULE,
f686e9af
MD
185 },
186};
187
9891b1ce 188module_platform_driver(nvec_kbd_driver);
162c7d8c
MD
189
190MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
191MODULE_DESCRIPTION("NVEC keyboard driver");
38dc92ed 192MODULE_ALIAS("platform:nvec-kbd");
162c7d8c 193MODULE_LICENSE("GPL");