1 /* 2 * Chromium OS Matrix Keyboard 3 * 4 * Copyright (c) 2012 The Chromium OS Authors. 5 * See file CREDITS for list of people who contributed to this 6 * project. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of 11 * the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 * MA 02111-1307 USA 22 */ 23 24 #include <common.h> 25 #include <cros_ec.h> 26 #include <fdtdec.h> 27 #include <input.h> 28 #include <key_matrix.h> 29 #include <stdio_dev.h> 30 31 DECLARE_GLOBAL_DATA_PTR; 32 33 enum { 34 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */ 35 }; 36 37 static struct keyb { 38 struct cros_ec_dev *dev; /* The CROS_EC device */ 39 struct input_config input; /* The input layer */ 40 struct key_matrix matrix; /* The key matrix layer */ 41 int key_rows; /* Number of keyboard rows */ 42 int key_cols; /* Number of keyboard columns */ 43 unsigned int repeat_delay_ms; /* Time before autorepeat starts */ 44 unsigned int repeat_rate_ms; /* Autorepeat rate in ms */ 45 int ghost_filter; /* 1 to enable ghost filter, else 0 */ 46 int inited; /* 1 if keyboard is ready */ 47 } config; 48 49 50 /** 51 * Check the keyboard controller and return a list of key matrix positions 52 * for which a key is pressed 53 * 54 * @param config Keyboard config 55 * @param keys List of keys that we have detected 56 * @param max_count Maximum number of keys to return 57 * @return number of pressed keys, 0 for none 58 */ 59 static int check_for_keys(struct keyb *config, 60 struct key_matrix_key *keys, int max_count) 61 { 62 struct key_matrix_key *key; 63 struct mbkp_keyscan scan; 64 unsigned int row, col, bit, data; 65 int num_keys; 66 67 if (cros_ec_scan_keyboard(config->dev, &scan)) { 68 debug("%s: keyboard scan failed\n", __func__); 69 return -1; 70 } 71 72 for (col = num_keys = bit = 0; col < config->matrix.num_cols; 73 col++) { 74 for (row = 0; row < config->matrix.num_rows; row++) { 75 unsigned int mask = 1 << (bit & 7); 76 77 data = scan.data[bit / 8]; 78 if ((data & mask) && num_keys < max_count) { 79 key = keys + num_keys++; 80 key->row = row; 81 key->col = col; 82 key->valid = 1; 83 } 84 bit++; 85 } 86 } 87 88 return num_keys; 89 } 90 91 /** 92 * Test if keys are available to be read 93 * 94 * @return 0 if no keys available, 1 if keys are available 95 */ 96 static int kbd_tstc(void) 97 { 98 /* Just get input to do this for us */ 99 return config.inited ? input_tstc(&config.input) : 0; 100 } 101 102 /** 103 * Read a key 104 * 105 * @return ASCII key code, or 0 if no key, or -1 if error 106 */ 107 static int kbd_getc(void) 108 { 109 /* Just get input to do this for us */ 110 return config.inited ? input_getc(&config.input) : 0; 111 } 112 113 /** 114 * Check the keyboard, and send any keys that are pressed. 115 * 116 * This is called by input_tstc() and input_getc() when they need more 117 * characters 118 * 119 * @param input Input configuration 120 * @return 1, to indicate that we have something to look at 121 */ 122 int cros_ec_kbc_check(struct input_config *input) 123 { 124 static struct key_matrix_key last_keys[KBC_MAX_KEYS]; 125 static int last_num_keys; 126 struct key_matrix_key keys[KBC_MAX_KEYS]; 127 int keycodes[KBC_MAX_KEYS]; 128 int num_keys, num_keycodes; 129 int irq_pending, sent; 130 131 /* 132 * Loop until the EC has no more keyscan records, or we have 133 * received at least one character. This means we know that tstc() 134 * will always return non-zero if keys have been pressed. 135 * 136 * Without this loop, a key release (which generates no new ascii 137 * characters) will cause us to exit this function, and just tstc() 138 * may return 0 before all keys have been read from the EC. 139 */ 140 do { 141 irq_pending = cros_ec_interrupt_pending(config.dev); 142 if (irq_pending) { 143 num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS); 144 last_num_keys = num_keys; 145 memcpy(last_keys, keys, sizeof(keys)); 146 } else { 147 /* 148 * EC doesn't want to be asked, so use keys from last 149 * time. 150 */ 151 num_keys = last_num_keys; 152 memcpy(keys, last_keys, sizeof(keys)); 153 } 154 155 if (num_keys < 0) 156 return -1; 157 num_keycodes = key_matrix_decode(&config.matrix, keys, 158 num_keys, keycodes, KBC_MAX_KEYS); 159 sent = input_send_keycodes(input, keycodes, num_keycodes); 160 } while (irq_pending && !sent); 161 162 return 1; 163 } 164 165 /** 166 * Decode MBKP keyboard details from the device tree 167 * 168 * @param blob Device tree blob 169 * @param node Node to decode from 170 * @param config Configuration data read from fdt 171 * @return 0 if ok, -1 on error 172 */ 173 static int cros_ec_keyb_decode_fdt(const void *blob, int node, 174 struct keyb *config) 175 { 176 /* 177 * Get keyboard rows and columns - at present we are limited to 178 * 8 columns by the protocol (one byte per row scan) 179 */ 180 config->key_rows = fdtdec_get_int(blob, node, "google,key-rows", 0); 181 config->key_cols = fdtdec_get_int(blob, node, "google,key-columns", 0); 182 if (!config->key_rows || !config->key_cols || 183 config->key_rows * config->key_cols / 8 184 > CROS_EC_KEYSCAN_COLS) { 185 debug("%s: Invalid key matrix size %d x %d\n", __func__, 186 config->key_rows, config->key_cols); 187 return -1; 188 } 189 config->repeat_delay_ms = fdtdec_get_int(blob, node, 190 "google,repeat-delay-ms", 0); 191 config->repeat_rate_ms = fdtdec_get_int(blob, node, 192 "google,repeat-rate-ms", 0); 193 config->ghost_filter = fdtdec_get_bool(blob, node, 194 "google,ghost-filter"); 195 return 0; 196 } 197 198 /** 199 * Set up the keyboard. This is called by the stdio device handler. 200 * 201 * We want to do this init when the keyboard is actually used rather than 202 * at start-up, since keyboard input may not currently be selected. 203 * 204 * @return 0 if ok, -1 on error 205 */ 206 static int cros_ec_init_keyboard(void) 207 { 208 const void *blob = gd->fdt_blob; 209 int node; 210 211 config.dev = board_get_cros_ec_dev(); 212 if (!config.dev) { 213 debug("%s: no cros_ec device: cannot init keyboard\n", 214 __func__); 215 return -1; 216 } 217 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB); 218 if (node < 0) { 219 debug("%s: Node not found\n", __func__); 220 return -1; 221 } 222 if (cros_ec_keyb_decode_fdt(blob, node, &config)) 223 return -1; 224 input_set_delays(&config.input, config.repeat_delay_ms, 225 config.repeat_rate_ms); 226 if (key_matrix_init(&config.matrix, config.key_rows, 227 config.key_cols, config.ghost_filter)) { 228 debug("%s: cannot init key matrix\n", __func__); 229 return -1; 230 } 231 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) { 232 debug("%s: Could not decode key matrix from fdt\n", __func__); 233 return -1; 234 } 235 config.inited = 1; 236 debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows, 237 config.key_cols); 238 239 return 0; 240 } 241 242 int drv_keyboard_init(void) 243 { 244 struct stdio_dev dev; 245 246 if (input_init(&config.input, 0)) { 247 debug("%s: Cannot set up input\n", __func__); 248 return -1; 249 } 250 config.input.read_keys = cros_ec_kbc_check; 251 252 memset(&dev, '\0', sizeof(dev)); 253 strcpy(dev.name, "cros-ec-keyb"); 254 dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; 255 dev.getc = kbd_getc; 256 dev.tstc = kbd_tstc; 257 dev.start = cros_ec_init_keyboard; 258 259 /* Register the device. cros_ec_init_keyboard() will be called soon */ 260 return input_stdio_register(&dev); 261 } 262