16642a681SRakesh Iyer /* 26642a681SRakesh Iyer * (C) Copyright 2011 36642a681SRakesh Iyer * NVIDIA Corporation <www.nvidia.com> 46642a681SRakesh Iyer * 51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 66642a681SRakesh Iyer */ 76642a681SRakesh Iyer 86642a681SRakesh Iyer #include <common.h> 9f77f5e9bSSimon Glass #include <dm.h> 106642a681SRakesh Iyer #include <fdtdec.h> 116642a681SRakesh Iyer #include <input.h> 12f77f5e9bSSimon Glass #include <keyboard.h> 136642a681SRakesh Iyer #include <key_matrix.h> 146642a681SRakesh Iyer #include <stdio_dev.h> 156642a681SRakesh Iyer #include <tegra-kbc.h> 166642a681SRakesh Iyer #include <asm/io.h> 176642a681SRakesh Iyer #include <asm/arch/clock.h> 186642a681SRakesh Iyer #include <asm/arch/funcmux.h> 19150c2493STom Warren #include <asm/arch-tegra/timer.h> 206642a681SRakesh Iyer #include <linux/input.h> 216642a681SRakesh Iyer 226642a681SRakesh Iyer DECLARE_GLOBAL_DATA_PTR; 236642a681SRakesh Iyer 246642a681SRakesh Iyer enum { 256642a681SRakesh Iyer KBC_MAX_GPIO = 24, 266642a681SRakesh Iyer KBC_MAX_KPENT = 8, /* size of keypress entry queue */ 276642a681SRakesh Iyer }; 286642a681SRakesh Iyer 296642a681SRakesh Iyer #define KBC_FIFO_TH_CNT_SHIFT 14 306642a681SRakesh Iyer #define KBC_DEBOUNCE_CNT_SHIFT 4 316642a681SRakesh Iyer #define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3) 326642a681SRakesh Iyer #define KBC_CONTROL_KBC_EN (1 << 0) 336642a681SRakesh Iyer #define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2) 346642a681SRakesh Iyer #define KBC_KPENT_VALID (1 << 7) 356642a681SRakesh Iyer #define KBC_ST_STATUS (1 << 3) 366642a681SRakesh Iyer 376642a681SRakesh Iyer enum { 386642a681SRakesh Iyer KBC_DEBOUNCE_COUNT = 2, 396642a681SRakesh Iyer KBC_REPEAT_RATE_MS = 30, 406642a681SRakesh Iyer KBC_REPEAT_DELAY_MS = 240, 416642a681SRakesh Iyer KBC_CLOCK_KHZ = 32, /* Keyboard uses a 32KHz clock */ 426642a681SRakesh Iyer }; 436642a681SRakesh Iyer 446642a681SRakesh Iyer /* keyboard controller config and state */ 45f77f5e9bSSimon Glass struct tegra_kbd_priv { 46f77f5e9bSSimon Glass struct input_config *input; /* The input layer */ 476642a681SRakesh Iyer struct key_matrix matrix; /* The key matrix layer */ 486642a681SRakesh Iyer 496642a681SRakesh Iyer struct kbc_tegra *kbc; /* tegra keyboard controller */ 506642a681SRakesh Iyer unsigned char inited; /* 1 if keyboard has been inited */ 516642a681SRakesh Iyer unsigned char first_scan; /* 1 if this is our first key scan */ 526642a681SRakesh Iyer 536642a681SRakesh Iyer /* 546642a681SRakesh Iyer * After init we must wait a short time before polling the keyboard. 556642a681SRakesh Iyer * This gives the tegra keyboard controller time to react after reset 566642a681SRakesh Iyer * and lets us grab keys pressed during reset. 576642a681SRakesh Iyer */ 586642a681SRakesh Iyer unsigned int init_dly_ms; /* Delay before we can read keyboard */ 596642a681SRakesh Iyer unsigned int start_time_ms; /* Time that we inited (in ms) */ 606642a681SRakesh Iyer unsigned int last_poll_ms; /* Time we should last polled */ 616642a681SRakesh Iyer unsigned int next_repeat_ms; /* Next time we repeat a key */ 62f77f5e9bSSimon Glass }; 636642a681SRakesh Iyer 646642a681SRakesh Iyer /** 656642a681SRakesh Iyer * reads the keyboard fifo for current keypresses 666642a681SRakesh Iyer * 67f77f5e9bSSimon Glass * @param priv Keyboard private data 686642a681SRakesh Iyer * @param fifo Place to put fifo results 696642a681SRakesh Iyer * @param max_keycodes Maximum number of key codes to put in the fifo 706642a681SRakesh Iyer * @return number of items put into fifo 716642a681SRakesh Iyer */ 72f77f5e9bSSimon Glass static int tegra_kbc_find_keys(struct tegra_kbd_priv *priv, int *fifo, 736642a681SRakesh Iyer int max_keycodes) 746642a681SRakesh Iyer { 756642a681SRakesh Iyer struct key_matrix_key keys[KBC_MAX_KPENT], *key; 766642a681SRakesh Iyer u32 kp_ent = 0; 776642a681SRakesh Iyer int i; 786642a681SRakesh Iyer 796642a681SRakesh Iyer for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) { 806642a681SRakesh Iyer /* Get next word */ 816642a681SRakesh Iyer if (!(i & 3)) 82f77f5e9bSSimon Glass kp_ent = readl(&priv->kbc->kp_ent[i / 4]); 836642a681SRakesh Iyer 846642a681SRakesh Iyer key->valid = (kp_ent & KBC_KPENT_VALID) != 0; 856642a681SRakesh Iyer key->row = (kp_ent >> 3) & 0xf; 866642a681SRakesh Iyer key->col = kp_ent & 0x7; 876642a681SRakesh Iyer 886642a681SRakesh Iyer /* Shift to get next entry */ 896642a681SRakesh Iyer kp_ent >>= 8; 906642a681SRakesh Iyer } 91f77f5e9bSSimon Glass return key_matrix_decode(&priv->matrix, keys, KBC_MAX_KPENT, fifo, 926642a681SRakesh Iyer max_keycodes); 936642a681SRakesh Iyer } 946642a681SRakesh Iyer 956642a681SRakesh Iyer /** 966642a681SRakesh Iyer * Process all the keypress sequences in fifo and send key codes 976642a681SRakesh Iyer * 986642a681SRakesh Iyer * The fifo contains zero or more keypress sets. Each set 996642a681SRakesh Iyer * consists of from 1-8 keycodes, representing the keycodes which 1006642a681SRakesh Iyer * were simultaneously pressed during that scan. 1016642a681SRakesh Iyer * 1026642a681SRakesh Iyer * This function works through each set and generates ASCII characters 1036642a681SRakesh Iyer * for each. Not that one set may produce more than one ASCII characters - 1046642a681SRakesh Iyer * for example holding down 'd' and 'f' at the same time will generate 1056642a681SRakesh Iyer * two ASCII characters. 1066642a681SRakesh Iyer * 1076642a681SRakesh Iyer * Note: if fifo_cnt is 0, we will tell the input layer that no keys are 1086642a681SRakesh Iyer * pressed. 1096642a681SRakesh Iyer * 110f77f5e9bSSimon Glass * @param priv Keyboard private data 1116642a681SRakesh Iyer * @param fifo_cnt Number of entries in the keyboard fifo 1126642a681SRakesh Iyer */ 113f77f5e9bSSimon Glass static void process_fifo(struct tegra_kbd_priv *priv, int fifo_cnt) 1146642a681SRakesh Iyer { 1156642a681SRakesh Iyer int fifo[KBC_MAX_KPENT]; 1166642a681SRakesh Iyer int cnt = 0; 1176642a681SRakesh Iyer 1186642a681SRakesh Iyer /* Always call input_send_keycodes() at least once */ 1196642a681SRakesh Iyer do { 1206642a681SRakesh Iyer if (fifo_cnt) 121f77f5e9bSSimon Glass cnt = tegra_kbc_find_keys(priv, fifo, KBC_MAX_KPENT); 1226642a681SRakesh Iyer 123f77f5e9bSSimon Glass input_send_keycodes(priv->input, fifo, cnt); 1246642a681SRakesh Iyer } while (--fifo_cnt > 0); 1256642a681SRakesh Iyer } 1266642a681SRakesh Iyer 1276642a681SRakesh Iyer /** 1286642a681SRakesh Iyer * Check the keyboard controller and emit ASCII characters for any keys that 1296642a681SRakesh Iyer * are pressed. 1306642a681SRakesh Iyer * 131f77f5e9bSSimon Glass * @param priv Keyboard private data 1326642a681SRakesh Iyer */ 133f77f5e9bSSimon Glass static void check_for_keys(struct tegra_kbd_priv *priv) 1346642a681SRakesh Iyer { 1356642a681SRakesh Iyer int fifo_cnt; 1366642a681SRakesh Iyer 137f77f5e9bSSimon Glass if (!priv->first_scan && 138f77f5e9bSSimon Glass get_timer(priv->last_poll_ms) < KBC_REPEAT_RATE_MS) 1396642a681SRakesh Iyer return; 140f77f5e9bSSimon Glass priv->last_poll_ms = get_timer(0); 141f77f5e9bSSimon Glass priv->first_scan = 0; 1426642a681SRakesh Iyer 1436642a681SRakesh Iyer /* 1446642a681SRakesh Iyer * Once we get here we know the keyboard has been scanned. So if there 1456642a681SRakesh Iyer * scan waiting for us, we know that nothing is held down. 1466642a681SRakesh Iyer */ 147f77f5e9bSSimon Glass fifo_cnt = (readl(&priv->kbc->interrupt) >> 4) & 0xf; 148f77f5e9bSSimon Glass process_fifo(priv, fifo_cnt); 1496642a681SRakesh Iyer } 1506642a681SRakesh Iyer 1516642a681SRakesh Iyer /** 1526642a681SRakesh Iyer * In order to detect keys pressed on boot, wait for the hardware to 1536642a681SRakesh Iyer * complete scanning the keys. This includes time to transition from 1546642a681SRakesh Iyer * Wkup mode to Continous polling mode and the repoll time. We can 1556642a681SRakesh Iyer * deduct the time that's already elapsed. 1566642a681SRakesh Iyer * 157f77f5e9bSSimon Glass * @param priv Keyboard private data 1586642a681SRakesh Iyer */ 159f77f5e9bSSimon Glass static void kbd_wait_for_fifo_init(struct tegra_kbd_priv *priv) 1606642a681SRakesh Iyer { 161f77f5e9bSSimon Glass if (!priv->inited) { 1626642a681SRakesh Iyer unsigned long elapsed_time; 1636642a681SRakesh Iyer long delay_ms; 1646642a681SRakesh Iyer 165f77f5e9bSSimon Glass elapsed_time = get_timer(priv->start_time_ms); 166f77f5e9bSSimon Glass delay_ms = priv->init_dly_ms - elapsed_time; 1676642a681SRakesh Iyer if (delay_ms > 0) { 1686642a681SRakesh Iyer udelay(delay_ms * 1000); 1696642a681SRakesh Iyer debug("%s: delay %ldms\n", __func__, delay_ms); 1706642a681SRakesh Iyer } 1716642a681SRakesh Iyer 172f77f5e9bSSimon Glass priv->inited = 1; 1736642a681SRakesh Iyer } 1746642a681SRakesh Iyer } 1756642a681SRakesh Iyer 1766642a681SRakesh Iyer /** 1776642a681SRakesh Iyer * Check the tegra keyboard, and send any keys that are pressed. 1786642a681SRakesh Iyer * 1796642a681SRakesh Iyer * This is called by input_tstc() and input_getc() when they need more 1806642a681SRakesh Iyer * characters 1816642a681SRakesh Iyer * 1826642a681SRakesh Iyer * @param input Input configuration 1836642a681SRakesh Iyer * @return 1, to indicate that we have something to look at 1846642a681SRakesh Iyer */ 18519d7bf3dSJeroen Hofstee static int tegra_kbc_check(struct input_config *input) 1866642a681SRakesh Iyer { 187f77f5e9bSSimon Glass struct tegra_kbd_priv *priv = dev_get_priv(input->dev); 188f77f5e9bSSimon Glass 189f77f5e9bSSimon Glass kbd_wait_for_fifo_init(priv); 190f77f5e9bSSimon Glass check_for_keys(priv); 1916642a681SRakesh Iyer 1926642a681SRakesh Iyer return 1; 1936642a681SRakesh Iyer } 1946642a681SRakesh Iyer 1956642a681SRakesh Iyer /* configures keyboard GPIO registers to use the rows and columns */ 196f77f5e9bSSimon Glass static void config_kbc_gpio(struct tegra_kbd_priv *priv, struct kbc_tegra *kbc) 1976642a681SRakesh Iyer { 1986642a681SRakesh Iyer int i; 1996642a681SRakesh Iyer 2006642a681SRakesh Iyer for (i = 0; i < KBC_MAX_GPIO; i++) { 2016642a681SRakesh Iyer u32 row_cfg, col_cfg; 2026642a681SRakesh Iyer u32 r_shift = 5 * (i % 6); 2036642a681SRakesh Iyer u32 c_shift = 4 * (i % 8); 2046642a681SRakesh Iyer u32 r_mask = 0x1f << r_shift; 2056642a681SRakesh Iyer u32 c_mask = 0xf << c_shift; 2066642a681SRakesh Iyer u32 r_offs = i / 6; 2076642a681SRakesh Iyer u32 c_offs = i / 8; 2086642a681SRakesh Iyer 2096642a681SRakesh Iyer row_cfg = readl(&kbc->row_cfg[r_offs]); 2106642a681SRakesh Iyer col_cfg = readl(&kbc->col_cfg[c_offs]); 2116642a681SRakesh Iyer 2126642a681SRakesh Iyer row_cfg &= ~r_mask; 2136642a681SRakesh Iyer col_cfg &= ~c_mask; 2146642a681SRakesh Iyer 215f77f5e9bSSimon Glass if (i < priv->matrix.num_rows) { 2166642a681SRakesh Iyer row_cfg |= ((i << 1) | 1) << r_shift; 2176642a681SRakesh Iyer } else { 218f77f5e9bSSimon Glass col_cfg |= (((i - priv->matrix.num_rows) << 1) | 1) 2196642a681SRakesh Iyer << c_shift; 2206642a681SRakesh Iyer } 2216642a681SRakesh Iyer 2226642a681SRakesh Iyer writel(row_cfg, &kbc->row_cfg[r_offs]); 2236642a681SRakesh Iyer writel(col_cfg, &kbc->col_cfg[c_offs]); 2246642a681SRakesh Iyer } 2256642a681SRakesh Iyer } 2266642a681SRakesh Iyer 2276642a681SRakesh Iyer /** 2286642a681SRakesh Iyer * Start up the keyboard device 2296642a681SRakesh Iyer */ 230f77f5e9bSSimon Glass static void tegra_kbc_open(struct tegra_kbd_priv *priv) 2316642a681SRakesh Iyer { 232f77f5e9bSSimon Glass struct kbc_tegra *kbc = priv->kbc; 2336642a681SRakesh Iyer unsigned int scan_period; 2346642a681SRakesh Iyer u32 val; 2356642a681SRakesh Iyer 2366642a681SRakesh Iyer /* 2376642a681SRakesh Iyer * We will scan at twice the keyboard repeat rate, so that there is 2386642a681SRakesh Iyer * always a scan ready when we check it in check_for_keys(). 2396642a681SRakesh Iyer */ 2406642a681SRakesh Iyer scan_period = KBC_REPEAT_RATE_MS / 2; 2416642a681SRakesh Iyer writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly); 2426642a681SRakesh Iyer writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly); 2436642a681SRakesh Iyer /* 2446642a681SRakesh Iyer * Before reading from the keyboard we must wait for the init_dly 2456642a681SRakesh Iyer * plus the rpt_delay, plus 2ms for the row scan time. 2466642a681SRakesh Iyer */ 247f77f5e9bSSimon Glass priv->init_dly_ms = scan_period * 2 + 2; 2486642a681SRakesh Iyer 2496642a681SRakesh Iyer val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT; 2506642a681SRakesh Iyer val |= 1 << KBC_FIFO_TH_CNT_SHIFT; /* fifo interrupt threshold */ 2516642a681SRakesh Iyer val |= KBC_CONTROL_KBC_EN; /* enable */ 2526642a681SRakesh Iyer writel(val, &kbc->control); 2536642a681SRakesh Iyer 254f77f5e9bSSimon Glass priv->start_time_ms = get_timer(0); 255f77f5e9bSSimon Glass priv->last_poll_ms = get_timer(0); 256f77f5e9bSSimon Glass priv->next_repeat_ms = priv->last_poll_ms; 257f77f5e9bSSimon Glass priv->first_scan = 1; 258f77f5e9bSSimon Glass } 259f77f5e9bSSimon Glass 260f77f5e9bSSimon Glass static int tegra_kbd_start(struct udevice *dev) 261f77f5e9bSSimon Glass { 262f77f5e9bSSimon Glass struct tegra_kbd_priv *priv = dev_get_priv(dev); 263f77f5e9bSSimon Glass 264f77f5e9bSSimon Glass /* Set up pin mux and enable the clock */ 265f77f5e9bSSimon Glass funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT); 266f77f5e9bSSimon Glass clock_enable(PERIPH_ID_KBC); 267f77f5e9bSSimon Glass config_kbc_gpio(priv, priv->kbc); 268f77f5e9bSSimon Glass 269f77f5e9bSSimon Glass tegra_kbc_open(priv); 270f77f5e9bSSimon Glass debug("%s: Tegra keyboard ready\n", __func__); 271f77f5e9bSSimon Glass 272f77f5e9bSSimon Glass return 0; 2736642a681SRakesh Iyer } 2746642a681SRakesh Iyer 2756642a681SRakesh Iyer /** 2766642a681SRakesh Iyer * Set up the tegra keyboard. This is called by the stdio device handler 2776642a681SRakesh Iyer * 2786642a681SRakesh Iyer * We want to do this init when the keyboard is actually used rather than 2796642a681SRakesh Iyer * at start-up, since keyboard input may not currently be selected. 2806642a681SRakesh Iyer * 2816642a681SRakesh Iyer * Once the keyboard starts there will be a period during which we must 2826642a681SRakesh Iyer * wait for the keyboard to init. We do this only when a key is first 2836642a681SRakesh Iyer * read - see kbd_wait_for_fifo_init(). 2846642a681SRakesh Iyer * 2856642a681SRakesh Iyer * @return 0 if ok, -ve on error 2866642a681SRakesh Iyer */ 287f77f5e9bSSimon Glass static int tegra_kbd_probe(struct udevice *dev) 2886642a681SRakesh Iyer { 289f77f5e9bSSimon Glass struct tegra_kbd_priv *priv = dev_get_priv(dev); 290f77f5e9bSSimon Glass struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); 291f77f5e9bSSimon Glass struct stdio_dev *sdev = &uc_priv->sdev; 292f77f5e9bSSimon Glass struct input_config *input = &uc_priv->input; 293f77f5e9bSSimon Glass int node = dev->of_offset; 294f77f5e9bSSimon Glass int ret; 2951ed0b51bSAllen Martin 296f77f5e9bSSimon Glass priv->kbc = (struct kbc_tegra *)dev_get_addr(dev); 297f77f5e9bSSimon Glass if ((fdt_addr_t)priv->kbc == FDT_ADDR_T_NONE) { 2986642a681SRakesh Iyer debug("%s: No keyboard register found\n", __func__); 299f77f5e9bSSimon Glass return -EINVAL; 3006642a681SRakesh Iyer } 301f77f5e9bSSimon Glass input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS); 3026642a681SRakesh Iyer 3036642a681SRakesh Iyer /* Decode the keyboard matrix information (16 rows, 8 columns) */ 304f77f5e9bSSimon Glass ret = key_matrix_init(&priv->matrix, 16, 8, 1); 305f77f5e9bSSimon Glass if (ret) { 306f77f5e9bSSimon Glass debug("%s: Could not init key matrix: %d\n", __func__, ret); 307f77f5e9bSSimon Glass return ret; 3086642a681SRakesh Iyer } 309f77f5e9bSSimon Glass ret = key_matrix_decode_fdt(&priv->matrix, gd->fdt_blob, node); 310f77f5e9bSSimon Glass if (ret) { 311f77f5e9bSSimon Glass debug("%s: Could not decode key matrix from fdt: %d\n", 312f77f5e9bSSimon Glass __func__, ret); 313f77f5e9bSSimon Glass return ret; 3146642a681SRakesh Iyer } 315*73248479SSimon Glass input_add_tables(input, false); 316f77f5e9bSSimon Glass if (priv->matrix.fn_keycode) { 317f77f5e9bSSimon Glass ret = input_add_table(input, KEY_FN, -1, 318f77f5e9bSSimon Glass priv->matrix.fn_keycode, 319f77f5e9bSSimon Glass priv->matrix.key_count); 320f77f5e9bSSimon Glass if (ret) { 321f77f5e9bSSimon Glass debug("%s: input_add_table() failed\n", __func__); 322f77f5e9bSSimon Glass return ret; 3236642a681SRakesh Iyer } 3246642a681SRakesh Iyer } 3256642a681SRakesh Iyer 3266642a681SRakesh Iyer /* Register the device. init_tegra_keyboard() will be called soon */ 327f77f5e9bSSimon Glass priv->input = input; 328f77f5e9bSSimon Glass input->dev = dev; 329f77f5e9bSSimon Glass input->read_keys = tegra_kbc_check; 330f77f5e9bSSimon Glass strcpy(sdev->name, "tegra-kbc"); 331f77f5e9bSSimon Glass ret = input_stdio_register(sdev); 332f77f5e9bSSimon Glass if (ret) { 333f77f5e9bSSimon Glass debug("%s: input_stdio_register() failed\n", __func__); 334f77f5e9bSSimon Glass return ret; 335f77f5e9bSSimon Glass } 336f77f5e9bSSimon Glass 3371ed0b51bSAllen Martin return 0; 3386642a681SRakesh Iyer } 339f77f5e9bSSimon Glass 340f77f5e9bSSimon Glass static const struct keyboard_ops tegra_kbd_ops = { 341f77f5e9bSSimon Glass .start = tegra_kbd_start, 342f77f5e9bSSimon Glass }; 343f77f5e9bSSimon Glass 344f77f5e9bSSimon Glass static const struct udevice_id tegra_kbd_ids[] = { 345f77f5e9bSSimon Glass { .compatible = "nvidia,tegra20-kbc" }, 346f77f5e9bSSimon Glass { } 347f77f5e9bSSimon Glass }; 348f77f5e9bSSimon Glass 349f77f5e9bSSimon Glass U_BOOT_DRIVER(tegra_kbd) = { 350f77f5e9bSSimon Glass .name = "tegra_kbd", 351f77f5e9bSSimon Glass .id = UCLASS_KEYBOARD, 352f77f5e9bSSimon Glass .of_match = tegra_kbd_ids, 353f77f5e9bSSimon Glass .probe = tegra_kbd_probe, 354f77f5e9bSSimon Glass .ops = &tegra_kbd_ops, 355f77f5e9bSSimon Glass .priv_auto_alloc_size = sizeof(struct tegra_kbd_priv), 356f77f5e9bSSimon Glass }; 357