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 | |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 24 | enum kbd_subcmds { |
| 25 | CNFG_WAKE = 3, |
| 26 | CNFG_WAKE_KEY_REPORTING, |
| 27 | SET_LEDS = 0xed, |
| 28 | ENABLE_KBD = 0xf4, |
| 29 | DISABLE_KBD, |
| 30 | }; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 31 | |
| 32 | static unsigned char keycodes[ARRAY_SIZE(code_tab_102us) |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 33 | + ARRAY_SIZE(extcode_tab_us102)]; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 34 | |
| 35 | struct nvec_keys { |
| 36 | struct input_dev *input; |
| 37 | struct notifier_block notifier; |
| 38 | struct nvec_chip *nvec; |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 39 | bool caps_lock; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static struct nvec_keys keys_dev; |
| 43 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 44 | static void nvec_kbd_toggle_led(void) |
| 45 | { |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 46 | char buf[] = { NVEC_KBD, SET_LEDS, 0 }; |
| 47 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 48 | keys_dev.caps_lock = !keys_dev.caps_lock; |
| 49 | |
| 50 | if (keys_dev.caps_lock) |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 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)); |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 57 | static int nvec_keys_notifier(struct notifier_block *nb, |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 58 | unsigned long event_type, void *data) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 59 | { |
| 60 | int code, state; |
| 61 | unsigned char *msg = (unsigned char *)data; |
| 62 | |
| 63 | if (event_type == NVEC_KB_EVT) { |
Julian Andres Klode | 0cab4cb | 2011-09-27 19:00:51 +0200 | [diff] [blame] | 64 | int _size = (msg[0] & (3 << 5)) >> 5; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 65 | |
| 66 | /* power on/off button */ |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 67 | if (_size == NVEC_VAR_SIZE) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 68 | return NOTIFY_STOP; |
| 69 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 70 | if (_size == NVEC_3BYTES) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 71 | msg++; |
| 72 | |
| 73 | code = msg[1] & 0x7f; |
| 74 | state = msg[1] & 0x80; |
| 75 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 76 | if (code_tabs[_size][code] == KEY_CAPSLOCK && state) |
| 77 | nvec_kbd_toggle_led(); |
| 78 | |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 79 | input_report_key(keys_dev.input, code_tabs[_size][code], |
| 80 | !state); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 81 | input_sync(keys_dev.input); |
| 82 | |
| 83 | return NOTIFY_STOP; |
| 84 | } |
| 85 | |
| 86 | return NOTIFY_DONE; |
| 87 | } |
| 88 | |
| 89 | static int nvec_kbd_event(struct input_dev *dev, unsigned int type, |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 90 | unsigned int code, int value) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 91 | { |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 92 | struct nvec_chip *nvec = keys_dev.nvec; |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 93 | char buf[] = { NVEC_KBD, SET_LEDS, 0 }; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 94 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 95 | if (type == EV_REP) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 98 | if (type != EV_LED) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 99 | return -1; |
| 100 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 101 | if (code != LED_CAPSL) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 102 | return -1; |
| 103 | |
| 104 | buf[2] = !!value; |
| 105 | nvec_write_async(nvec, buf, sizeof(buf)); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Bill Pemberton | 4662080 | 2012-11-19 13:22:07 -0500 | [diff] [blame] | 110 | static int nvec_kbd_probe(struct platform_device *pdev) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 111 | { |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 112 | struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 113 | int i, j, err; |
| 114 | struct input_dev *idev; |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 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 }; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 120 | |
| 121 | j = 0; |
| 122 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 123 | for (i = 0; i < ARRAY_SIZE(code_tab_102us); ++i) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 124 | keycodes[j++] = code_tab_102us[i]; |
| 125 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 126 | for (i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i) |
| 127 | keycodes[j++] = extcode_tab_us102[i]; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 128 | |
Leon Romanovsky | 167bf09 | 2013-05-14 12:22:07 +0300 | [diff] [blame] | 129 | idev = devm_input_allocate_device(&pdev->dev); |
Ilya Petrov | 97cc265 | 2011-09-27 19:00:44 +0200 | [diff] [blame] | 130 | idev->name = "nvec keyboard"; |
| 131 | idev->phys = "nvec"; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 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 | |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 139 | for (i = 0; i < ARRAY_SIZE(keycodes); ++i) |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 140 | set_bit(keycodes[i], idev->keybit); |
| 141 | |
| 142 | clear_bit(0, idev->keybit); |
| 143 | err = input_register_device(idev); |
Colin Brophy | 88d94e3 | 2011-07-15 13:08:16 +0100 | [diff] [blame] | 144 | if (err) |
Leon Romanovsky | 167bf09 | 2013-05-14 12:22:07 +0300 | [diff] [blame] | 145 | return err; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 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 */ |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 153 | nvec_write_async(nvec, enable_kbd, 2); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 154 | |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 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); |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 159 | |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 160 | /* Disable caps lock LED */ |
Marc Dietrich | 93eff83 | 2013-01-27 17:43:43 +0100 | [diff] [blame] | 161 | nvec_write_async(nvec, clear_leds, sizeof(clear_leds)); |
Julian Andres Klode | ff169c1 | 2011-09-27 19:00:54 +0200 | [diff] [blame] | 162 | |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 163 | return 0; |
Marc Dietrich | 32890b9 | 2011-05-19 16:34:42 +0200 | [diff] [blame] | 164 | } |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 165 | |
Bill Pemberton | 1a6a8a8 | 2012-11-19 13:26:41 -0500 | [diff] [blame] | 166 | static int nvec_kbd_remove(struct platform_device *pdev) |
Marc Dietrich | 3cdde3a | 2012-06-24 23:25:21 +0200 | [diff] [blame] | 167 | { |
Marc Dietrich | d398f56 | 2013-04-29 23:14:52 +0200 | [diff] [blame] | 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 | |
Marc Dietrich | 3cdde3a | 2012-06-24 23:25:21 +0200 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 179 | static struct platform_driver nvec_kbd_driver = { |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 180 | .probe = nvec_kbd_probe, |
Bill Pemberton | 44b90a3 | 2012-11-19 13:20:55 -0500 | [diff] [blame] | 181 | .remove = nvec_kbd_remove, |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 182 | .driver = { |
| 183 | .name = "nvec-kbd", |
Marc Dietrich | f686e9a | 2011-08-24 20:23:07 +0200 | [diff] [blame] | 184 | }, |
| 185 | }; |
| 186 | |
Marc Dietrich | 9891b1c | 2012-06-24 23:25:18 +0200 | [diff] [blame] | 187 | module_platform_driver(nvec_kbd_driver); |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 188 | |
| 189 | MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>"); |
| 190 | MODULE_DESCRIPTION("NVEC keyboard driver"); |
Marc Dietrich | 38dc92e | 2013-04-29 23:14:50 +0200 | [diff] [blame] | 191 | MODULE_ALIAS("platform:nvec-kbd"); |
Marc Dietrich | 162c7d8 | 2011-09-27 19:00:40 +0200 | [diff] [blame] | 192 | MODULE_LICENSE("GPL"); |