xref: /rk3399_rockchip-uboot/drivers/input/tegra-kbc.c (revision 1ed0b51b7d58bfcf1901005fb98591bbdb9e30e3)
16642a681SRakesh Iyer /*
26642a681SRakesh Iyer  *  (C) Copyright 2011
36642a681SRakesh Iyer  *  NVIDIA Corporation <www.nvidia.com>
46642a681SRakesh Iyer  *
56642a681SRakesh Iyer  * See file CREDITS for list of people who contributed to this
66642a681SRakesh Iyer  * project.
76642a681SRakesh Iyer  *
86642a681SRakesh Iyer  * This program is free software; you can redistribute it and/or
96642a681SRakesh Iyer  * modify it under the terms of the GNU General Public License as
106642a681SRakesh Iyer  * published by the Free Software Foundation; either version 2 of
116642a681SRakesh Iyer  * the License, or (at your option) any later version.
126642a681SRakesh Iyer  *
136642a681SRakesh Iyer  * This program is distributed in the hope that it will be useful,
146642a681SRakesh Iyer  * but WITHOUT ANY WARRANTY; without even the implied warranty of
156642a681SRakesh Iyer  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
166642a681SRakesh Iyer  * GNU General Public License for more details.
176642a681SRakesh Iyer  *
186642a681SRakesh Iyer  * You should have received a copy of the GNU General Public License
196642a681SRakesh Iyer  * along with this program; if not, write to the Free Software
206642a681SRakesh Iyer  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
216642a681SRakesh Iyer  * MA 02111-1307 USA
226642a681SRakesh Iyer  */
236642a681SRakesh Iyer 
246642a681SRakesh Iyer #include <common.h>
256642a681SRakesh Iyer #include <fdtdec.h>
266642a681SRakesh Iyer #include <input.h>
276642a681SRakesh Iyer #include <key_matrix.h>
286642a681SRakesh Iyer #include <stdio_dev.h>
296642a681SRakesh Iyer #include <tegra-kbc.h>
306642a681SRakesh Iyer #include <asm/io.h>
316642a681SRakesh Iyer #include <asm/arch/clock.h>
326642a681SRakesh Iyer #include <asm/arch/funcmux.h>
33150c2493STom Warren #include <asm/arch-tegra/timer.h>
346642a681SRakesh Iyer #include <linux/input.h>
356642a681SRakesh Iyer 
366642a681SRakesh Iyer DECLARE_GLOBAL_DATA_PTR;
376642a681SRakesh Iyer 
386642a681SRakesh Iyer enum {
396642a681SRakesh Iyer 	KBC_MAX_GPIO		= 24,
406642a681SRakesh Iyer 	KBC_MAX_KPENT		= 8,	/* size of keypress entry queue */
416642a681SRakesh Iyer };
426642a681SRakesh Iyer 
436642a681SRakesh Iyer #define KBC_FIFO_TH_CNT_SHIFT		14
446642a681SRakesh Iyer #define KBC_DEBOUNCE_CNT_SHIFT		4
456642a681SRakesh Iyer #define KBC_CONTROL_FIFO_CNT_INT_EN	(1 << 3)
466642a681SRakesh Iyer #define KBC_CONTROL_KBC_EN		(1 << 0)
476642a681SRakesh Iyer #define KBC_INT_FIFO_CNT_INT_STATUS	(1 << 2)
486642a681SRakesh Iyer #define KBC_KPENT_VALID			(1 << 7)
496642a681SRakesh Iyer #define KBC_ST_STATUS			(1 << 3)
506642a681SRakesh Iyer 
516642a681SRakesh Iyer enum {
526642a681SRakesh Iyer 	KBC_DEBOUNCE_COUNT	= 2,
536642a681SRakesh Iyer 	KBC_REPEAT_RATE_MS	= 30,
546642a681SRakesh Iyer 	KBC_REPEAT_DELAY_MS	= 240,
556642a681SRakesh Iyer 	KBC_CLOCK_KHZ		= 32,	/* Keyboard uses a 32KHz clock */
566642a681SRakesh Iyer };
576642a681SRakesh Iyer 
586642a681SRakesh Iyer /* keyboard controller config and state */
596642a681SRakesh Iyer static struct keyb {
606642a681SRakesh Iyer 	struct input_config input;	/* The input layer */
616642a681SRakesh Iyer 	struct key_matrix matrix;	/* The key matrix layer */
626642a681SRakesh Iyer 
636642a681SRakesh Iyer 	struct kbc_tegra *kbc;		/* tegra keyboard controller */
646642a681SRakesh Iyer 	unsigned char inited;		/* 1 if keyboard has been inited */
656642a681SRakesh Iyer 	unsigned char first_scan;	/* 1 if this is our first key scan */
66*1ed0b51bSAllen Martin 	unsigned char created;		/* 1 if driver has been created */
676642a681SRakesh Iyer 
686642a681SRakesh Iyer 	/*
696642a681SRakesh Iyer 	 * After init we must wait a short time before polling the keyboard.
706642a681SRakesh Iyer 	 * This gives the tegra keyboard controller time to react after reset
716642a681SRakesh Iyer 	 * and lets us grab keys pressed during reset.
726642a681SRakesh Iyer 	 */
736642a681SRakesh Iyer 	unsigned int init_dly_ms;	/* Delay before we can read keyboard */
746642a681SRakesh Iyer 	unsigned int start_time_ms;	/* Time that we inited (in ms) */
756642a681SRakesh Iyer 	unsigned int last_poll_ms;	/* Time we should last polled */
766642a681SRakesh Iyer 	unsigned int next_repeat_ms;	/* Next time we repeat a key */
776642a681SRakesh Iyer } config;
786642a681SRakesh Iyer 
796642a681SRakesh Iyer /**
806642a681SRakesh Iyer  * reads the keyboard fifo for current keypresses
816642a681SRakesh Iyer  *
826642a681SRakesh Iyer  * @param config	Keyboard config
836642a681SRakesh Iyer  * @param fifo		Place to put fifo results
846642a681SRakesh Iyer  * @param max_keycodes	Maximum number of key codes to put in the fifo
856642a681SRakesh Iyer  * @return number of items put into fifo
866642a681SRakesh Iyer  */
876642a681SRakesh Iyer static int tegra_kbc_find_keys(struct keyb *config, int *fifo,
886642a681SRakesh Iyer 			       int max_keycodes)
896642a681SRakesh Iyer {
906642a681SRakesh Iyer 	struct key_matrix_key keys[KBC_MAX_KPENT], *key;
916642a681SRakesh Iyer 	u32 kp_ent = 0;
926642a681SRakesh Iyer 	int i;
936642a681SRakesh Iyer 
946642a681SRakesh Iyer 	for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) {
956642a681SRakesh Iyer 		/* Get next word */
966642a681SRakesh Iyer 		if (!(i & 3))
976642a681SRakesh Iyer 			kp_ent = readl(&config->kbc->kp_ent[i / 4]);
986642a681SRakesh Iyer 
996642a681SRakesh Iyer 		key->valid = (kp_ent & KBC_KPENT_VALID) != 0;
1006642a681SRakesh Iyer 		key->row = (kp_ent >> 3) & 0xf;
1016642a681SRakesh Iyer 		key->col = kp_ent & 0x7;
1026642a681SRakesh Iyer 
1036642a681SRakesh Iyer 		/* Shift to get next entry */
1046642a681SRakesh Iyer 		kp_ent >>= 8;
1056642a681SRakesh Iyer 	}
1066642a681SRakesh Iyer 	return key_matrix_decode(&config->matrix, keys, KBC_MAX_KPENT, fifo,
1076642a681SRakesh Iyer 				 max_keycodes);
1086642a681SRakesh Iyer }
1096642a681SRakesh Iyer 
1106642a681SRakesh Iyer /**
1116642a681SRakesh Iyer  * Process all the keypress sequences in fifo and send key codes
1126642a681SRakesh Iyer  *
1136642a681SRakesh Iyer  * The fifo contains zero or more keypress sets. Each set
1146642a681SRakesh Iyer  * consists of from 1-8 keycodes, representing the keycodes which
1156642a681SRakesh Iyer  * were simultaneously pressed during that scan.
1166642a681SRakesh Iyer  *
1176642a681SRakesh Iyer  * This function works through each set and generates ASCII characters
1186642a681SRakesh Iyer  * for each. Not that one set may produce more than one ASCII characters -
1196642a681SRakesh Iyer  * for example holding down 'd' and 'f' at the same time will generate
1206642a681SRakesh Iyer  * two ASCII characters.
1216642a681SRakesh Iyer  *
1226642a681SRakesh Iyer  * Note: if fifo_cnt is 0, we will tell the input layer that no keys are
1236642a681SRakesh Iyer  * pressed.
1246642a681SRakesh Iyer  *
1256642a681SRakesh Iyer  * @param config	Keyboard config
1266642a681SRakesh Iyer  * @param fifo_cnt	Number of entries in the keyboard fifo
1276642a681SRakesh Iyer  */
1286642a681SRakesh Iyer static void process_fifo(struct keyb *config, int fifo_cnt)
1296642a681SRakesh Iyer {
1306642a681SRakesh Iyer 	int fifo[KBC_MAX_KPENT];
1316642a681SRakesh Iyer 	int cnt = 0;
1326642a681SRakesh Iyer 
1336642a681SRakesh Iyer 	/* Always call input_send_keycodes() at least once */
1346642a681SRakesh Iyer 	do {
1356642a681SRakesh Iyer 		if (fifo_cnt)
1366642a681SRakesh Iyer 			cnt = tegra_kbc_find_keys(config, fifo, KBC_MAX_KPENT);
1376642a681SRakesh Iyer 
1386642a681SRakesh Iyer 		input_send_keycodes(&config->input, fifo, cnt);
1396642a681SRakesh Iyer 	} while (--fifo_cnt > 0);
1406642a681SRakesh Iyer }
1416642a681SRakesh Iyer 
1426642a681SRakesh Iyer /**
1436642a681SRakesh Iyer  * Check the keyboard controller and emit ASCII characters for any keys that
1446642a681SRakesh Iyer  * are pressed.
1456642a681SRakesh Iyer  *
1466642a681SRakesh Iyer  * @param config	Keyboard config
1476642a681SRakesh Iyer  */
1486642a681SRakesh Iyer static void check_for_keys(struct keyb *config)
1496642a681SRakesh Iyer {
1506642a681SRakesh Iyer 	int fifo_cnt;
1516642a681SRakesh Iyer 
1526642a681SRakesh Iyer 	if (!config->first_scan &&
1536642a681SRakesh Iyer 			get_timer(config->last_poll_ms) < KBC_REPEAT_RATE_MS)
1546642a681SRakesh Iyer 		return;
1556642a681SRakesh Iyer 	config->last_poll_ms = get_timer(0);
1566642a681SRakesh Iyer 	config->first_scan = 0;
1576642a681SRakesh Iyer 
1586642a681SRakesh Iyer 	/*
1596642a681SRakesh Iyer 	 * Once we get here we know the keyboard has been scanned. So if there
1606642a681SRakesh Iyer 	 * scan waiting for us, we know that nothing is held down.
1616642a681SRakesh Iyer 	 */
1626642a681SRakesh Iyer 	fifo_cnt = (readl(&config->kbc->interrupt) >> 4) & 0xf;
1636642a681SRakesh Iyer 	process_fifo(config, fifo_cnt);
1646642a681SRakesh Iyer }
1656642a681SRakesh Iyer 
1666642a681SRakesh Iyer /**
1676642a681SRakesh Iyer  * In order to detect keys pressed on boot, wait for the hardware to
1686642a681SRakesh Iyer  * complete scanning the keys. This includes time to transition from
1696642a681SRakesh Iyer  * Wkup mode to Continous polling mode and the repoll time. We can
1706642a681SRakesh Iyer  * deduct the time that's already elapsed.
1716642a681SRakesh Iyer  *
1726642a681SRakesh Iyer  * @param config	Keyboard config
1736642a681SRakesh Iyer  */
1746642a681SRakesh Iyer static void kbd_wait_for_fifo_init(struct keyb *config)
1756642a681SRakesh Iyer {
1766642a681SRakesh Iyer 	if (!config->inited) {
1776642a681SRakesh Iyer 		unsigned long elapsed_time;
1786642a681SRakesh Iyer 		long delay_ms;
1796642a681SRakesh Iyer 
1806642a681SRakesh Iyer 		elapsed_time = get_timer(config->start_time_ms);
1816642a681SRakesh Iyer 		delay_ms = config->init_dly_ms - elapsed_time;
1826642a681SRakesh Iyer 		if (delay_ms > 0) {
1836642a681SRakesh Iyer 			udelay(delay_ms * 1000);
1846642a681SRakesh Iyer 			debug("%s: delay %ldms\n", __func__, delay_ms);
1856642a681SRakesh Iyer 		}
1866642a681SRakesh Iyer 
1876642a681SRakesh Iyer 		config->inited = 1;
1886642a681SRakesh Iyer 	}
1896642a681SRakesh Iyer }
1906642a681SRakesh Iyer 
1916642a681SRakesh Iyer /**
1926642a681SRakesh Iyer  * Check the tegra keyboard, and send any keys that are pressed.
1936642a681SRakesh Iyer  *
1946642a681SRakesh Iyer  * This is called by input_tstc() and input_getc() when they need more
1956642a681SRakesh Iyer  * characters
1966642a681SRakesh Iyer  *
1976642a681SRakesh Iyer  * @param input		Input configuration
1986642a681SRakesh Iyer  * @return 1, to indicate that we have something to look at
1996642a681SRakesh Iyer  */
2006642a681SRakesh Iyer int tegra_kbc_check(struct input_config *input)
2016642a681SRakesh Iyer {
2026642a681SRakesh Iyer 	kbd_wait_for_fifo_init(&config);
2036642a681SRakesh Iyer 	check_for_keys(&config);
2046642a681SRakesh Iyer 
2056642a681SRakesh Iyer 	return 1;
2066642a681SRakesh Iyer }
2076642a681SRakesh Iyer 
2086642a681SRakesh Iyer /**
2096642a681SRakesh Iyer  * Test if keys are available to be read
2106642a681SRakesh Iyer  *
2116642a681SRakesh Iyer  * @return 0 if no keys available, 1 if keys are available
2126642a681SRakesh Iyer  */
2136642a681SRakesh Iyer static int kbd_tstc(void)
2146642a681SRakesh Iyer {
2156642a681SRakesh Iyer 	/* Just get input to do this for us */
2166642a681SRakesh Iyer 	return input_tstc(&config.input);
2176642a681SRakesh Iyer }
2186642a681SRakesh Iyer 
2196642a681SRakesh Iyer /**
2206642a681SRakesh Iyer  * Read a key
2216642a681SRakesh Iyer  *
2226642a681SRakesh Iyer  * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key...
2236642a681SRakesh Iyer  *
2246642a681SRakesh Iyer  * @return ASCII key code, or 0 if no key, or -1 if error
2256642a681SRakesh Iyer  */
2266642a681SRakesh Iyer static int kbd_getc(void)
2276642a681SRakesh Iyer {
2286642a681SRakesh Iyer 	/* Just get input to do this for us */
2296642a681SRakesh Iyer 	return input_getc(&config.input);
2306642a681SRakesh Iyer }
2316642a681SRakesh Iyer 
2326642a681SRakesh Iyer /* configures keyboard GPIO registers to use the rows and columns */
2336642a681SRakesh Iyer static void config_kbc_gpio(struct kbc_tegra *kbc)
2346642a681SRakesh Iyer {
2356642a681SRakesh Iyer 	int i;
2366642a681SRakesh Iyer 
2376642a681SRakesh Iyer 	for (i = 0; i < KBC_MAX_GPIO; i++) {
2386642a681SRakesh Iyer 		u32 row_cfg, col_cfg;
2396642a681SRakesh Iyer 		u32 r_shift = 5 * (i % 6);
2406642a681SRakesh Iyer 		u32 c_shift = 4 * (i % 8);
2416642a681SRakesh Iyer 		u32 r_mask = 0x1f << r_shift;
2426642a681SRakesh Iyer 		u32 c_mask = 0xf << c_shift;
2436642a681SRakesh Iyer 		u32 r_offs = i / 6;
2446642a681SRakesh Iyer 		u32 c_offs = i / 8;
2456642a681SRakesh Iyer 
2466642a681SRakesh Iyer 		row_cfg = readl(&kbc->row_cfg[r_offs]);
2476642a681SRakesh Iyer 		col_cfg = readl(&kbc->col_cfg[c_offs]);
2486642a681SRakesh Iyer 
2496642a681SRakesh Iyer 		row_cfg &= ~r_mask;
2506642a681SRakesh Iyer 		col_cfg &= ~c_mask;
2516642a681SRakesh Iyer 
2526642a681SRakesh Iyer 		if (i < config.matrix.num_rows) {
2536642a681SRakesh Iyer 			row_cfg |= ((i << 1) | 1) << r_shift;
2546642a681SRakesh Iyer 		} else {
2556642a681SRakesh Iyer 			col_cfg |= (((i - config.matrix.num_rows) << 1) | 1)
2566642a681SRakesh Iyer 					<< c_shift;
2576642a681SRakesh Iyer 		}
2586642a681SRakesh Iyer 
2596642a681SRakesh Iyer 		writel(row_cfg, &kbc->row_cfg[r_offs]);
2606642a681SRakesh Iyer 		writel(col_cfg, &kbc->col_cfg[c_offs]);
2616642a681SRakesh Iyer 	}
2626642a681SRakesh Iyer }
2636642a681SRakesh Iyer 
2646642a681SRakesh Iyer /**
2656642a681SRakesh Iyer  * Start up the keyboard device
2666642a681SRakesh Iyer  */
2676642a681SRakesh Iyer static void tegra_kbc_open(void)
2686642a681SRakesh Iyer {
2696642a681SRakesh Iyer 	struct kbc_tegra *kbc = config.kbc;
2706642a681SRakesh Iyer 	unsigned int scan_period;
2716642a681SRakesh Iyer 	u32 val;
2726642a681SRakesh Iyer 
2736642a681SRakesh Iyer 	/*
2746642a681SRakesh Iyer 	 * We will scan at twice the keyboard repeat rate, so that there is
2756642a681SRakesh Iyer 	 * always a scan ready when we check it in check_for_keys().
2766642a681SRakesh Iyer 	 */
2776642a681SRakesh Iyer 	scan_period = KBC_REPEAT_RATE_MS / 2;
2786642a681SRakesh Iyer 	writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly);
2796642a681SRakesh Iyer 	writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly);
2806642a681SRakesh Iyer 	/*
2816642a681SRakesh Iyer 	 * Before reading from the keyboard we must wait for the init_dly
2826642a681SRakesh Iyer 	 * plus the rpt_delay, plus 2ms for the row scan time.
2836642a681SRakesh Iyer 	 */
2846642a681SRakesh Iyer 	config.init_dly_ms = scan_period * 2 + 2;
2856642a681SRakesh Iyer 
2866642a681SRakesh Iyer 	val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT;
2876642a681SRakesh Iyer 	val |= 1 << KBC_FIFO_TH_CNT_SHIFT;	/* fifo interrupt threshold */
2886642a681SRakesh Iyer 	val |= KBC_CONTROL_KBC_EN;		/* enable */
2896642a681SRakesh Iyer 	writel(val, &kbc->control);
2906642a681SRakesh Iyer 
2916642a681SRakesh Iyer 	config.start_time_ms = get_timer(0);
2926642a681SRakesh Iyer 	config.last_poll_ms = config.next_repeat_ms = get_timer(0);
2936642a681SRakesh Iyer 	config.first_scan = 1;
2946642a681SRakesh Iyer }
2956642a681SRakesh Iyer 
2966642a681SRakesh Iyer /**
2976642a681SRakesh Iyer  * Set up the tegra keyboard. This is called by the stdio device handler
2986642a681SRakesh Iyer  *
2996642a681SRakesh Iyer  * We want to do this init when the keyboard is actually used rather than
3006642a681SRakesh Iyer  * at start-up, since keyboard input may not currently be selected.
3016642a681SRakesh Iyer  *
3026642a681SRakesh Iyer  * Once the keyboard starts there will be a period during which we must
3036642a681SRakesh Iyer  * wait for the keyboard to init. We do this only when a key is first
3046642a681SRakesh Iyer  * read - see kbd_wait_for_fifo_init().
3056642a681SRakesh Iyer  *
3066642a681SRakesh Iyer  * @return 0 if ok, -ve on error
3076642a681SRakesh Iyer  */
3086642a681SRakesh Iyer static int init_tegra_keyboard(void)
3096642a681SRakesh Iyer {
310*1ed0b51bSAllen Martin 	/* check if already created */
311*1ed0b51bSAllen Martin 	if (config.created)
312*1ed0b51bSAllen Martin 		return 0;
313*1ed0b51bSAllen Martin 
3146642a681SRakesh Iyer #ifdef CONFIG_OF_CONTROL
3156642a681SRakesh Iyer 	int	node;
3166642a681SRakesh Iyer 
3176642a681SRakesh Iyer 	node = fdtdec_next_compatible(gd->fdt_blob, 0,
3186642a681SRakesh Iyer 					  COMPAT_NVIDIA_TEGRA20_KBC);
3196642a681SRakesh Iyer 	if (node < 0) {
3206642a681SRakesh Iyer 		debug("%s: cannot locate keyboard node\n", __func__);
3216642a681SRakesh Iyer 		return node;
3226642a681SRakesh Iyer 	}
3236642a681SRakesh Iyer 	config.kbc = (struct kbc_tegra *)fdtdec_get_addr(gd->fdt_blob,
3246642a681SRakesh Iyer 		       node, "reg");
3256642a681SRakesh Iyer 	if ((fdt_addr_t)config.kbc == FDT_ADDR_T_NONE) {
3266642a681SRakesh Iyer 		debug("%s: No keyboard register found\n", __func__);
3276642a681SRakesh Iyer 		return -1;
3286642a681SRakesh Iyer 	}
3291b1d3e64SSimon Glass 	input_set_delays(&config.input, KBC_REPEAT_DELAY_MS,
3301b1d3e64SSimon Glass 			KBC_REPEAT_RATE_MS);
3316642a681SRakesh Iyer 
3326642a681SRakesh Iyer 	/* Decode the keyboard matrix information (16 rows, 8 columns) */
33371dc6bcaSSimon Glass 	if (key_matrix_init(&config.matrix, 16, 8, 1)) {
3346642a681SRakesh Iyer 		debug("%s: Could not init key matrix\n", __func__);
3356642a681SRakesh Iyer 		return -1;
3366642a681SRakesh Iyer 	}
3376642a681SRakesh Iyer 	if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
3386642a681SRakesh Iyer 		debug("%s: Could not decode key matrix from fdt\n", __func__);
3396642a681SRakesh Iyer 		return -1;
3406642a681SRakesh Iyer 	}
3416642a681SRakesh Iyer 	if (config.matrix.fn_keycode) {
3426642a681SRakesh Iyer 		if (input_add_table(&config.input, KEY_FN, -1,
3436642a681SRakesh Iyer 				    config.matrix.fn_keycode,
3446642a681SRakesh Iyer 				    config.matrix.key_count))
3456642a681SRakesh Iyer 			return -1;
3466642a681SRakesh Iyer 	}
3476642a681SRakesh Iyer #else
3486642a681SRakesh Iyer #error "Tegra keyboard driver requires FDT definitions"
3496642a681SRakesh Iyer #endif
3506642a681SRakesh Iyer 
3516642a681SRakesh Iyer 	/* Set up pin mux and enable the clock */
3526642a681SRakesh Iyer 	funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT);
3536642a681SRakesh Iyer 	clock_enable(PERIPH_ID_KBC);
3546642a681SRakesh Iyer 	config_kbc_gpio(config.kbc);
3556642a681SRakesh Iyer 
3566642a681SRakesh Iyer 	tegra_kbc_open();
357*1ed0b51bSAllen Martin 	config.created = 1;
3586642a681SRakesh Iyer 	debug("%s: Tegra keyboard ready\n", __func__);
3596642a681SRakesh Iyer 
3606642a681SRakesh Iyer 	return 0;
3616642a681SRakesh Iyer }
3626642a681SRakesh Iyer 
3636642a681SRakesh Iyer int drv_keyboard_init(void)
3646642a681SRakesh Iyer {
3656642a681SRakesh Iyer 	struct stdio_dev dev;
366*1ed0b51bSAllen Martin 	char *stdinname = getenv("stdin");
367*1ed0b51bSAllen Martin 	int error;
3686642a681SRakesh Iyer 
3691b1d3e64SSimon Glass 	if (input_init(&config.input, 0)) {
3706642a681SRakesh Iyer 		debug("%s: Cannot set up input\n", __func__);
3716642a681SRakesh Iyer 		return -1;
3726642a681SRakesh Iyer 	}
3736642a681SRakesh Iyer 	config.input.read_keys = tegra_kbc_check;
3746642a681SRakesh Iyer 
3756642a681SRakesh Iyer 	memset(&dev, '\0', sizeof(dev));
3766642a681SRakesh Iyer 	strcpy(dev.name, "tegra-kbc");
3776642a681SRakesh Iyer 	dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
3786642a681SRakesh Iyer 	dev.getc = kbd_getc;
3796642a681SRakesh Iyer 	dev.tstc = kbd_tstc;
3806642a681SRakesh Iyer 	dev.start = init_tegra_keyboard;
3816642a681SRakesh Iyer 
3826642a681SRakesh Iyer 	/* Register the device. init_tegra_keyboard() will be called soon */
383*1ed0b51bSAllen Martin 	error = input_stdio_register(&dev);
384*1ed0b51bSAllen Martin 	if (error)
385*1ed0b51bSAllen Martin 		return error;
386*1ed0b51bSAllen Martin #ifdef CONFIG_CONSOLE_MUX
387*1ed0b51bSAllen Martin 	error = iomux_doenv(stdin, stdinname);
388*1ed0b51bSAllen Martin 	if (error)
389*1ed0b51bSAllen Martin 		return error;
390*1ed0b51bSAllen Martin #endif
391*1ed0b51bSAllen Martin 	return 0;
3926642a681SRakesh Iyer }
393