blob: a4ce5a740e2b1d49ae2e82a6d3df4e1458ce946e [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
Colin Brophy88d94e32011-07-15 13:08:16 +010024#define ACK_KBD_EVENT {'\x05', '\xed', '\x01'}
Marc Dietrich32890b92011-05-19 16:34:42 +020025
Julian Andres Klodeff169c12011-09-27 19:00:54 +020026static const char led_on[3] = "\x05\xed\x07";
27static const char led_off[3] = "\x05\xed\x00";
Marc Dietrich32890b92011-05-19 16:34:42 +020028static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
Marc Dietrich162c7d82011-09-27 19:00:40 +020029 + ARRAY_SIZE(extcode_tab_us102)];
Marc Dietrich32890b92011-05-19 16:34:42 +020030
31struct nvec_keys {
32 struct input_dev *input;
33 struct notifier_block notifier;
34 struct nvec_chip *nvec;
Julian Andres Klodeff169c12011-09-27 19:00:54 +020035 bool caps_lock;
Marc Dietrich32890b92011-05-19 16:34:42 +020036};
37
38static struct nvec_keys keys_dev;
39
Julian Andres Klodeff169c12011-09-27 19:00:54 +020040static void nvec_kbd_toggle_led(void)
41{
42 keys_dev.caps_lock = !keys_dev.caps_lock;
43
44 if (keys_dev.caps_lock)
45 nvec_write_async(keys_dev.nvec, led_on, sizeof(led_on));
46 else
47 nvec_write_async(keys_dev.nvec, led_off, sizeof(led_off));
48}
49
Marc Dietrich32890b92011-05-19 16:34:42 +020050static int nvec_keys_notifier(struct notifier_block *nb,
Marc Dietrich162c7d82011-09-27 19:00:40 +020051 unsigned long event_type, void *data)
Marc Dietrich32890b92011-05-19 16:34:42 +020052{
53 int code, state;
54 unsigned char *msg = (unsigned char *)data;
55
56 if (event_type == NVEC_KB_EVT) {
Julian Andres Klode0cab4cb2011-09-27 19:00:51 +020057 int _size = (msg[0] & (3 << 5)) >> 5;
Marc Dietrich32890b92011-05-19 16:34:42 +020058
59/* power on/off button */
Colin Brophy88d94e32011-07-15 13:08:16 +010060 if (_size == NVEC_VAR_SIZE)
Marc Dietrich32890b92011-05-19 16:34:42 +020061 return NOTIFY_STOP;
62
Colin Brophy88d94e32011-07-15 13:08:16 +010063 if (_size == NVEC_3BYTES)
Marc Dietrich32890b92011-05-19 16:34:42 +020064 msg++;
65
66 code = msg[1] & 0x7f;
67 state = msg[1] & 0x80;
68
Julian Andres Klodeff169c12011-09-27 19:00:54 +020069 if (code_tabs[_size][code] == KEY_CAPSLOCK && state)
70 nvec_kbd_toggle_led();
71
Marc Dietrich162c7d82011-09-27 19:00:40 +020072 input_report_key(keys_dev.input, code_tabs[_size][code],
73 !state);
Marc Dietrich32890b92011-05-19 16:34:42 +020074 input_sync(keys_dev.input);
75
76 return NOTIFY_STOP;
77 }
78
79 return NOTIFY_DONE;
80}
81
82static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
Marc Dietrich162c7d82011-09-27 19:00:40 +020083 unsigned int code, int value)
Marc Dietrich32890b92011-05-19 16:34:42 +020084{
85 unsigned char buf[] = ACK_KBD_EVENT;
86 struct nvec_chip *nvec = keys_dev.nvec;
87
Colin Brophy88d94e32011-07-15 13:08:16 +010088 if (type == EV_REP)
Marc Dietrich32890b92011-05-19 16:34:42 +020089 return 0;
90
Colin Brophy88d94e32011-07-15 13:08:16 +010091 if (type != EV_LED)
Marc Dietrich32890b92011-05-19 16:34:42 +020092 return -1;
93
Colin Brophy88d94e32011-07-15 13:08:16 +010094 if (code != LED_CAPSL)
Marc Dietrich32890b92011-05-19 16:34:42 +020095 return -1;
96
97 buf[2] = !!value;
98 nvec_write_async(nvec, buf, sizeof(buf));
99
100 return 0;
101}
102
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200103static int __devinit nvec_kbd_probe(struct platform_device *pdev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200104{
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200105 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
Marc Dietrich32890b92011-05-19 16:34:42 +0200106 int i, j, err;
107 struct input_dev *idev;
108
109 j = 0;
110
Colin Brophy88d94e32011-07-15 13:08:16 +0100111 for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
Marc Dietrich32890b92011-05-19 16:34:42 +0200112 keycodes[j++] = code_tab_102us[i];
113
Colin Brophy88d94e32011-07-15 13:08:16 +0100114 for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
115 keycodes[j++] = extcode_tab_us102[i];
Marc Dietrich32890b92011-05-19 16:34:42 +0200116
117 idev = input_allocate_device();
Ilya Petrov97cc2652011-09-27 19:00:44 +0200118 idev->name = "nvec keyboard";
119 idev->phys = "nvec";
Marc Dietrich32890b92011-05-19 16:34:42 +0200120 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_LED);
121 idev->ledbit[0] = BIT_MASK(LED_CAPSL);
122 idev->event = nvec_kbd_event;
123 idev->keycode = keycodes;
124 idev->keycodesize = sizeof(unsigned char);
125 idev->keycodemax = ARRAY_SIZE(keycodes);
126
Colin Brophy88d94e32011-07-15 13:08:16 +0100127 for (i = 0; i < ARRAY_SIZE(keycodes); ++i)
Marc Dietrich32890b92011-05-19 16:34:42 +0200128 set_bit(keycodes[i], idev->keybit);
129
130 clear_bit(0, idev->keybit);
131 err = input_register_device(idev);
Colin Brophy88d94e32011-07-15 13:08:16 +0100132 if (err)
Marc Dietrich32890b92011-05-19 16:34:42 +0200133 goto fail;
134
135 keys_dev.input = idev;
136 keys_dev.notifier.notifier_call = nvec_keys_notifier;
137 keys_dev.nvec = nvec;
138 nvec_register_notifier(nvec, &keys_dev.notifier, 0);
139
140 /* Enable keyboard */
141 nvec_write_async(nvec, "\x05\xf4", 2);
142
143 /* keyboard reset? */
144 nvec_write_async(nvec, "\x05\x03\x01\x01", 4);
145 nvec_write_async(nvec, "\x05\x04\x01", 3);
146 nvec_write_async(nvec, "\x06\x01\xff\x03", 4);
147/* FIXME
148 wait until keyboard reset is finished
149 or until we have a sync write */
150 mdelay(1000);
151
Julian Andres Klodeff169c12011-09-27 19:00:54 +0200152 /* Disable caps lock LED */
153 nvec_write_async(nvec, led_off, sizeof(led_off));
154
Marc Dietrich32890b92011-05-19 16:34:42 +0200155 return 0;
156
157fail:
158 input_free_device(idev);
159 return err;
160}
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200161
162static struct platform_driver nvec_kbd_driver = {
Marc Dietrich162c7d82011-09-27 19:00:40 +0200163 .probe = nvec_kbd_probe,
164 .driver = {
165 .name = "nvec-kbd",
166 .owner = THIS_MODULE,
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200167 },
168};
169
170static int __init nvec_kbd_init(void)
171{
172 return platform_driver_register(&nvec_kbd_driver);
173}
174
175module_init(nvec_kbd_init);
Marc Dietrich162c7d82011-09-27 19:00:40 +0200176
177MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
178MODULE_DESCRIPTION("NVEC keyboard driver");
179MODULE_LICENSE("GPL");