Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 |
| 3 | * NVIDIA Corporation <www.nvidia.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <input.h> |
| 11 | #include <key_matrix.h> |
| 12 | #include <stdio_dev.h> |
| 13 | #include <tegra-kbc.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/arch/clock.h> |
| 16 | #include <asm/arch/funcmux.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 17 | #include <asm/arch-tegra/timer.h> |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 18 | #include <linux/input.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | enum { |
| 23 | KBC_MAX_GPIO = 24, |
| 24 | KBC_MAX_KPENT = 8, /* size of keypress entry queue */ |
| 25 | }; |
| 26 | |
| 27 | #define KBC_FIFO_TH_CNT_SHIFT 14 |
| 28 | #define KBC_DEBOUNCE_CNT_SHIFT 4 |
| 29 | #define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3) |
| 30 | #define KBC_CONTROL_KBC_EN (1 << 0) |
| 31 | #define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2) |
| 32 | #define KBC_KPENT_VALID (1 << 7) |
| 33 | #define KBC_ST_STATUS (1 << 3) |
| 34 | |
| 35 | enum { |
| 36 | KBC_DEBOUNCE_COUNT = 2, |
| 37 | KBC_REPEAT_RATE_MS = 30, |
| 38 | KBC_REPEAT_DELAY_MS = 240, |
| 39 | KBC_CLOCK_KHZ = 32, /* Keyboard uses a 32KHz clock */ |
| 40 | }; |
| 41 | |
| 42 | /* keyboard controller config and state */ |
| 43 | static struct keyb { |
| 44 | struct input_config input; /* The input layer */ |
| 45 | struct key_matrix matrix; /* The key matrix layer */ |
| 46 | |
| 47 | struct kbc_tegra *kbc; /* tegra keyboard controller */ |
| 48 | unsigned char inited; /* 1 if keyboard has been inited */ |
| 49 | unsigned char first_scan; /* 1 if this is our first key scan */ |
Allen Martin | 1ed0b51 | 2012-11-01 13:41:16 +0000 | [diff] [blame] | 50 | unsigned char created; /* 1 if driver has been created */ |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * After init we must wait a short time before polling the keyboard. |
| 54 | * This gives the tegra keyboard controller time to react after reset |
| 55 | * and lets us grab keys pressed during reset. |
| 56 | */ |
| 57 | unsigned int init_dly_ms; /* Delay before we can read keyboard */ |
| 58 | unsigned int start_time_ms; /* Time that we inited (in ms) */ |
| 59 | unsigned int last_poll_ms; /* Time we should last polled */ |
| 60 | unsigned int next_repeat_ms; /* Next time we repeat a key */ |
| 61 | } config; |
| 62 | |
| 63 | /** |
| 64 | * reads the keyboard fifo for current keypresses |
| 65 | * |
| 66 | * @param config Keyboard config |
| 67 | * @param fifo Place to put fifo results |
| 68 | * @param max_keycodes Maximum number of key codes to put in the fifo |
| 69 | * @return number of items put into fifo |
| 70 | */ |
| 71 | static int tegra_kbc_find_keys(struct keyb *config, int *fifo, |
| 72 | int max_keycodes) |
| 73 | { |
| 74 | struct key_matrix_key keys[KBC_MAX_KPENT], *key; |
| 75 | u32 kp_ent = 0; |
| 76 | int i; |
| 77 | |
| 78 | for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) { |
| 79 | /* Get next word */ |
| 80 | if (!(i & 3)) |
| 81 | kp_ent = readl(&config->kbc->kp_ent[i / 4]); |
| 82 | |
| 83 | key->valid = (kp_ent & KBC_KPENT_VALID) != 0; |
| 84 | key->row = (kp_ent >> 3) & 0xf; |
| 85 | key->col = kp_ent & 0x7; |
| 86 | |
| 87 | /* Shift to get next entry */ |
| 88 | kp_ent >>= 8; |
| 89 | } |
| 90 | return key_matrix_decode(&config->matrix, keys, KBC_MAX_KPENT, fifo, |
| 91 | max_keycodes); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Process all the keypress sequences in fifo and send key codes |
| 96 | * |
| 97 | * The fifo contains zero or more keypress sets. Each set |
| 98 | * consists of from 1-8 keycodes, representing the keycodes which |
| 99 | * were simultaneously pressed during that scan. |
| 100 | * |
| 101 | * This function works through each set and generates ASCII characters |
| 102 | * for each. Not that one set may produce more than one ASCII characters - |
| 103 | * for example holding down 'd' and 'f' at the same time will generate |
| 104 | * two ASCII characters. |
| 105 | * |
| 106 | * Note: if fifo_cnt is 0, we will tell the input layer that no keys are |
| 107 | * pressed. |
| 108 | * |
| 109 | * @param config Keyboard config |
| 110 | * @param fifo_cnt Number of entries in the keyboard fifo |
| 111 | */ |
| 112 | static void process_fifo(struct keyb *config, int fifo_cnt) |
| 113 | { |
| 114 | int fifo[KBC_MAX_KPENT]; |
| 115 | int cnt = 0; |
| 116 | |
| 117 | /* Always call input_send_keycodes() at least once */ |
| 118 | do { |
| 119 | if (fifo_cnt) |
| 120 | cnt = tegra_kbc_find_keys(config, fifo, KBC_MAX_KPENT); |
| 121 | |
| 122 | input_send_keycodes(&config->input, fifo, cnt); |
| 123 | } while (--fifo_cnt > 0); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check the keyboard controller and emit ASCII characters for any keys that |
| 128 | * are pressed. |
| 129 | * |
| 130 | * @param config Keyboard config |
| 131 | */ |
| 132 | static void check_for_keys(struct keyb *config) |
| 133 | { |
| 134 | int fifo_cnt; |
| 135 | |
| 136 | if (!config->first_scan && |
| 137 | get_timer(config->last_poll_ms) < KBC_REPEAT_RATE_MS) |
| 138 | return; |
| 139 | config->last_poll_ms = get_timer(0); |
| 140 | config->first_scan = 0; |
| 141 | |
| 142 | /* |
| 143 | * Once we get here we know the keyboard has been scanned. So if there |
| 144 | * scan waiting for us, we know that nothing is held down. |
| 145 | */ |
| 146 | fifo_cnt = (readl(&config->kbc->interrupt) >> 4) & 0xf; |
| 147 | process_fifo(config, fifo_cnt); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * In order to detect keys pressed on boot, wait for the hardware to |
| 152 | * complete scanning the keys. This includes time to transition from |
| 153 | * Wkup mode to Continous polling mode and the repoll time. We can |
| 154 | * deduct the time that's already elapsed. |
| 155 | * |
| 156 | * @param config Keyboard config |
| 157 | */ |
| 158 | static void kbd_wait_for_fifo_init(struct keyb *config) |
| 159 | { |
| 160 | if (!config->inited) { |
| 161 | unsigned long elapsed_time; |
| 162 | long delay_ms; |
| 163 | |
| 164 | elapsed_time = get_timer(config->start_time_ms); |
| 165 | delay_ms = config->init_dly_ms - elapsed_time; |
| 166 | if (delay_ms > 0) { |
| 167 | udelay(delay_ms * 1000); |
| 168 | debug("%s: delay %ldms\n", __func__, delay_ms); |
| 169 | } |
| 170 | |
| 171 | config->inited = 1; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Check the tegra keyboard, and send any keys that are pressed. |
| 177 | * |
| 178 | * This is called by input_tstc() and input_getc() when they need more |
| 179 | * characters |
| 180 | * |
| 181 | * @param input Input configuration |
| 182 | * @return 1, to indicate that we have something to look at |
| 183 | */ |
| 184 | int tegra_kbc_check(struct input_config *input) |
| 185 | { |
| 186 | kbd_wait_for_fifo_init(&config); |
| 187 | check_for_keys(&config); |
| 188 | |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Test if keys are available to be read |
| 194 | * |
| 195 | * @return 0 if no keys available, 1 if keys are available |
| 196 | */ |
| 197 | static int kbd_tstc(void) |
| 198 | { |
| 199 | /* Just get input to do this for us */ |
| 200 | return input_tstc(&config.input); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Read a key |
| 205 | * |
| 206 | * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key... |
| 207 | * |
| 208 | * @return ASCII key code, or 0 if no key, or -1 if error |
| 209 | */ |
| 210 | static int kbd_getc(void) |
| 211 | { |
| 212 | /* Just get input to do this for us */ |
| 213 | return input_getc(&config.input); |
| 214 | } |
| 215 | |
| 216 | /* configures keyboard GPIO registers to use the rows and columns */ |
| 217 | static void config_kbc_gpio(struct kbc_tegra *kbc) |
| 218 | { |
| 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < KBC_MAX_GPIO; i++) { |
| 222 | u32 row_cfg, col_cfg; |
| 223 | u32 r_shift = 5 * (i % 6); |
| 224 | u32 c_shift = 4 * (i % 8); |
| 225 | u32 r_mask = 0x1f << r_shift; |
| 226 | u32 c_mask = 0xf << c_shift; |
| 227 | u32 r_offs = i / 6; |
| 228 | u32 c_offs = i / 8; |
| 229 | |
| 230 | row_cfg = readl(&kbc->row_cfg[r_offs]); |
| 231 | col_cfg = readl(&kbc->col_cfg[c_offs]); |
| 232 | |
| 233 | row_cfg &= ~r_mask; |
| 234 | col_cfg &= ~c_mask; |
| 235 | |
| 236 | if (i < config.matrix.num_rows) { |
| 237 | row_cfg |= ((i << 1) | 1) << r_shift; |
| 238 | } else { |
| 239 | col_cfg |= (((i - config.matrix.num_rows) << 1) | 1) |
| 240 | << c_shift; |
| 241 | } |
| 242 | |
| 243 | writel(row_cfg, &kbc->row_cfg[r_offs]); |
| 244 | writel(col_cfg, &kbc->col_cfg[c_offs]); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Start up the keyboard device |
| 250 | */ |
| 251 | static void tegra_kbc_open(void) |
| 252 | { |
| 253 | struct kbc_tegra *kbc = config.kbc; |
| 254 | unsigned int scan_period; |
| 255 | u32 val; |
| 256 | |
| 257 | /* |
| 258 | * We will scan at twice the keyboard repeat rate, so that there is |
| 259 | * always a scan ready when we check it in check_for_keys(). |
| 260 | */ |
| 261 | scan_period = KBC_REPEAT_RATE_MS / 2; |
| 262 | writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly); |
| 263 | writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly); |
| 264 | /* |
| 265 | * Before reading from the keyboard we must wait for the init_dly |
| 266 | * plus the rpt_delay, plus 2ms for the row scan time. |
| 267 | */ |
| 268 | config.init_dly_ms = scan_period * 2 + 2; |
| 269 | |
| 270 | val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT; |
| 271 | val |= 1 << KBC_FIFO_TH_CNT_SHIFT; /* fifo interrupt threshold */ |
| 272 | val |= KBC_CONTROL_KBC_EN; /* enable */ |
| 273 | writel(val, &kbc->control); |
| 274 | |
| 275 | config.start_time_ms = get_timer(0); |
| 276 | config.last_poll_ms = config.next_repeat_ms = get_timer(0); |
| 277 | config.first_scan = 1; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Set up the tegra keyboard. This is called by the stdio device handler |
| 282 | * |
| 283 | * We want to do this init when the keyboard is actually used rather than |
| 284 | * at start-up, since keyboard input may not currently be selected. |
| 285 | * |
| 286 | * Once the keyboard starts there will be a period during which we must |
| 287 | * wait for the keyboard to init. We do this only when a key is first |
| 288 | * read - see kbd_wait_for_fifo_init(). |
| 289 | * |
| 290 | * @return 0 if ok, -ve on error |
| 291 | */ |
| 292 | static int init_tegra_keyboard(void) |
| 293 | { |
Allen Martin | 1ed0b51 | 2012-11-01 13:41:16 +0000 | [diff] [blame] | 294 | /* check if already created */ |
| 295 | if (config.created) |
| 296 | return 0; |
| 297 | |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 298 | #ifdef CONFIG_OF_CONTROL |
| 299 | int node; |
| 300 | |
| 301 | node = fdtdec_next_compatible(gd->fdt_blob, 0, |
| 302 | COMPAT_NVIDIA_TEGRA20_KBC); |
| 303 | if (node < 0) { |
| 304 | debug("%s: cannot locate keyboard node\n", __func__); |
| 305 | return node; |
| 306 | } |
| 307 | config.kbc = (struct kbc_tegra *)fdtdec_get_addr(gd->fdt_blob, |
| 308 | node, "reg"); |
| 309 | if ((fdt_addr_t)config.kbc == FDT_ADDR_T_NONE) { |
| 310 | debug("%s: No keyboard register found\n", __func__); |
| 311 | return -1; |
| 312 | } |
Simon Glass | 1b1d3e6 | 2012-09-27 15:18:41 +0000 | [diff] [blame] | 313 | input_set_delays(&config.input, KBC_REPEAT_DELAY_MS, |
| 314 | KBC_REPEAT_RATE_MS); |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 315 | |
| 316 | /* Decode the keyboard matrix information (16 rows, 8 columns) */ |
Simon Glass | 71dc6bc | 2012-09-27 15:18:42 +0000 | [diff] [blame] | 317 | if (key_matrix_init(&config.matrix, 16, 8, 1)) { |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 318 | debug("%s: Could not init key matrix\n", __func__); |
| 319 | return -1; |
| 320 | } |
| 321 | if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) { |
| 322 | debug("%s: Could not decode key matrix from fdt\n", __func__); |
| 323 | return -1; |
| 324 | } |
| 325 | if (config.matrix.fn_keycode) { |
| 326 | if (input_add_table(&config.input, KEY_FN, -1, |
| 327 | config.matrix.fn_keycode, |
| 328 | config.matrix.key_count)) |
| 329 | return -1; |
| 330 | } |
| 331 | #else |
| 332 | #error "Tegra keyboard driver requires FDT definitions" |
| 333 | #endif |
| 334 | |
| 335 | /* Set up pin mux and enable the clock */ |
| 336 | funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT); |
| 337 | clock_enable(PERIPH_ID_KBC); |
| 338 | config_kbc_gpio(config.kbc); |
| 339 | |
| 340 | tegra_kbc_open(); |
Allen Martin | 1ed0b51 | 2012-11-01 13:41:16 +0000 | [diff] [blame] | 341 | config.created = 1; |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 342 | debug("%s: Tegra keyboard ready\n", __func__); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | int drv_keyboard_init(void) |
| 348 | { |
| 349 | struct stdio_dev dev; |
Allen Martin | 1ed0b51 | 2012-11-01 13:41:16 +0000 | [diff] [blame] | 350 | char *stdinname = getenv("stdin"); |
| 351 | int error; |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 352 | |
Simon Glass | 1b1d3e6 | 2012-09-27 15:18:41 +0000 | [diff] [blame] | 353 | if (input_init(&config.input, 0)) { |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 354 | debug("%s: Cannot set up input\n", __func__); |
| 355 | return -1; |
| 356 | } |
| 357 | config.input.read_keys = tegra_kbc_check; |
| 358 | |
| 359 | memset(&dev, '\0', sizeof(dev)); |
| 360 | strcpy(dev.name, "tegra-kbc"); |
| 361 | dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 362 | dev.getc = kbd_getc; |
| 363 | dev.tstc = kbd_tstc; |
| 364 | dev.start = init_tegra_keyboard; |
| 365 | |
| 366 | /* Register the device. init_tegra_keyboard() will be called soon */ |
Allen Martin | 1ed0b51 | 2012-11-01 13:41:16 +0000 | [diff] [blame] | 367 | error = input_stdio_register(&dev); |
| 368 | if (error) |
| 369 | return error; |
| 370 | #ifdef CONFIG_CONSOLE_MUX |
| 371 | error = iomux_doenv(stdin, stdinname); |
| 372 | if (error) |
| 373 | return error; |
| 374 | #endif |
| 375 | return 0; |
Rakesh Iyer | 6642a68 | 2012-04-17 09:01:35 +0000 | [diff] [blame] | 376 | } |