Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 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 | |
Julian Andres Klode | 7974035 | 2011-09-27 19:00:39 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/delay.h> |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 20 | |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 21 | #include "nvec-keytable.h" |
| 22 | #include "nvec.h" |
| 23 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 24 | #define ACK_KBD_EVENT {'\x05', '\xed', '\x01'} |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 25 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame^] | 26 | static const char led_on[3] = "\x05\xed\x07"; |
| 27 | static const char led_off[3] = "\x05\xed\x00"; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 28 | static unsigned char keycodes[ARRAY_SIZE(code_tab_102us) |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 29 | + ARRAY_SIZE(extcode_tab_us102)]; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 30 | |
| 31 | struct nvec_keys { |
| 32 | struct input_dev *input; |
| 33 | struct notifier_block notifier; |
| 34 | struct nvec_chip *nvec; |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame^] | 35 | bool caps_lock; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static struct nvec_keys keys_dev; |
| 39 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame^] | 40 | static 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 Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 50 | static int nvec_keys_notifier(struct notifier_block *nb, |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 51 | unsigned long event_type, void *data) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 52 | { |
| 53 | int code, state; |
| 54 | unsigned char *msg = (unsigned char *)data; |
| 55 | |
| 56 | if (event_type == NVEC_KB_EVT) { |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 57 | int _size = (msg[0] & (3 << 5)) >> 5; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 58 | |
| 59 | /* power on/off button */ |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 60 | if (_size == NVEC_VAR_SIZE) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 61 | return NOTIFY_STOP; |
| 62 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 63 | if (_size == NVEC_3BYTES) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 64 | msg++; |
| 65 | |
| 66 | code = msg[1] & 0x7f; |
| 67 | state = msg[1] & 0x80; |
| 68 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame^] | 69 | if (code_tabs[_size][code] == KEY_CAPSLOCK && state) |
| 70 | nvec_kbd_toggle_led(); |
| 71 | |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 72 | input_report_key(keys_dev.input, code_tabs[_size][code], |
| 73 | !state); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 74 | input_sync(keys_dev.input); |
| 75 | |
| 76 | return NOTIFY_STOP; |
| 77 | } |
| 78 | |
| 79 | return NOTIFY_DONE; |
| 80 | } |
| 81 | |
| 82 | static int nvec_kbd_event(struct input_dev *dev, unsigned int type, |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 83 | unsigned int code, int value) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 84 | { |
| 85 | unsigned char buf[] = ACK_KBD_EVENT; |
| 86 | struct nvec_chip *nvec = keys_dev.nvec; |
| 87 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 88 | if (type == EV_REP) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 89 | return 0; |
| 90 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 91 | if (type != EV_LED) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 92 | return -1; |
| 93 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 94 | if (code != LED_CAPSL) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 95 | return -1; |
| 96 | |
| 97 | buf[2] = !!value; |
| 98 | nvec_write_async(nvec, buf, sizeof(buf)); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 103 | static int __devinit nvec_kbd_probe(struct platform_device *pdev) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 104 | { |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 105 | struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 106 | int i, j, err; |
| 107 | struct input_dev *idev; |
| 108 | |
| 109 | j = 0; |
| 110 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 111 | for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 112 | keycodes[j++] = code_tab_102us[i]; |
| 113 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 114 | for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i) |
| 115 | keycodes[j++] = extcode_tab_us102[i]; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 116 | |
| 117 | idev = input_allocate_device(); |
Ilya Petrov | 97cc265 | 2011-09-27 19:00:44 +0200 | [diff] [blame] | 118 | idev->name = "nvec keyboard"; |
| 119 | idev->phys = "nvec"; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 120 | 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 Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 127 | for (i = 0; i < ARRAY_SIZE(keycodes); ++i) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 128 | set_bit(keycodes[i], idev->keybit); |
| 129 | |
| 130 | clear_bit(0, idev->keybit); |
| 131 | err = input_register_device(idev); |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 132 | if (err) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 133 | 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 Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame^] | 152 | /* Disable caps lock LED */ |
| 153 | nvec_write_async(nvec, led_off, sizeof(led_off)); |
| 154 | |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | |
| 157 | fail: |
| 158 | input_free_device(idev); |
| 159 | return err; |
| 160 | } |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 161 | |
| 162 | static struct platform_driver nvec_kbd_driver = { |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 163 | .probe = nvec_kbd_probe, |
| 164 | .driver = { |
| 165 | .name = "nvec-kbd", |
| 166 | .owner = THIS_MODULE, |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 167 | }, |
| 168 | }; |
| 169 | |
| 170 | static int __init nvec_kbd_init(void) |
| 171 | { |
| 172 | return platform_driver_register(&nvec_kbd_driver); |
| 173 | } |
| 174 | |
| 175 | module_init(nvec_kbd_init); |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 176 | |
| 177 | MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>"); |
| 178 | MODULE_DESCRIPTION("NVEC keyboard driver"); |
| 179 | MODULE_LICENSE("GPL"); |