tegra-kbc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011
  4. * NVIDIA Corporation <www.nvidia.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <input.h>
  10. #include <keyboard.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>
  17. #include <asm/arch-tegra/timer.h>
  18. #include <linux/input.h>
  19. enum {
  20. KBC_MAX_GPIO = 24,
  21. KBC_MAX_KPENT = 8, /* size of keypress entry queue */
  22. };
  23. #define KBC_FIFO_TH_CNT_SHIFT 14
  24. #define KBC_DEBOUNCE_CNT_SHIFT 4
  25. #define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3)
  26. #define KBC_CONTROL_KBC_EN (1 << 0)
  27. #define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
  28. #define KBC_KPENT_VALID (1 << 7)
  29. #define KBC_ST_STATUS (1 << 3)
  30. enum {
  31. KBC_DEBOUNCE_COUNT = 2,
  32. KBC_REPEAT_RATE_MS = 30,
  33. KBC_REPEAT_DELAY_MS = 240,
  34. KBC_CLOCK_KHZ = 32, /* Keyboard uses a 32KHz clock */
  35. };
  36. /* keyboard controller config and state */
  37. struct tegra_kbd_priv {
  38. struct input_config *input; /* The input layer */
  39. struct key_matrix matrix; /* The key matrix layer */
  40. struct kbc_tegra *kbc; /* tegra keyboard controller */
  41. unsigned char inited; /* 1 if keyboard has been inited */
  42. unsigned char first_scan; /* 1 if this is our first key scan */
  43. /*
  44. * After init we must wait a short time before polling the keyboard.
  45. * This gives the tegra keyboard controller time to react after reset
  46. * and lets us grab keys pressed during reset.
  47. */
  48. unsigned int init_dly_ms; /* Delay before we can read keyboard */
  49. unsigned int start_time_ms; /* Time that we inited (in ms) */
  50. unsigned int last_poll_ms; /* Time we should last polled */
  51. unsigned int next_repeat_ms; /* Next time we repeat a key */
  52. };
  53. /**
  54. * reads the keyboard fifo for current keypresses
  55. *
  56. * @param priv Keyboard private data
  57. * @param fifo Place to put fifo results
  58. * @param max_keycodes Maximum number of key codes to put in the fifo
  59. * @return number of items put into fifo
  60. */
  61. static int tegra_kbc_find_keys(struct tegra_kbd_priv *priv, int *fifo,
  62. int max_keycodes)
  63. {
  64. struct key_matrix_key keys[KBC_MAX_KPENT], *key;
  65. u32 kp_ent = 0;
  66. int i;
  67. for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) {
  68. /* Get next word */
  69. if (!(i & 3))
  70. kp_ent = readl(&priv->kbc->kp_ent[i / 4]);
  71. key->valid = (kp_ent & KBC_KPENT_VALID) != 0;
  72. key->row = (kp_ent >> 3) & 0xf;
  73. key->col = kp_ent & 0x7;
  74. /* Shift to get next entry */
  75. kp_ent >>= 8;
  76. }
  77. return key_matrix_decode(&priv->matrix, keys, KBC_MAX_KPENT, fifo,
  78. max_keycodes);
  79. }
  80. /**
  81. * Process all the keypress sequences in fifo and send key codes
  82. *
  83. * The fifo contains zero or more keypress sets. Each set
  84. * consists of from 1-8 keycodes, representing the keycodes which
  85. * were simultaneously pressed during that scan.
  86. *
  87. * This function works through each set and generates ASCII characters
  88. * for each. Not that one set may produce more than one ASCII characters -
  89. * for example holding down 'd' and 'f' at the same time will generate
  90. * two ASCII characters.
  91. *
  92. * Note: if fifo_cnt is 0, we will tell the input layer that no keys are
  93. * pressed.
  94. *
  95. * @param priv Keyboard private data
  96. * @param fifo_cnt Number of entries in the keyboard fifo
  97. */
  98. static void process_fifo(struct tegra_kbd_priv *priv, int fifo_cnt)
  99. {
  100. int fifo[KBC_MAX_KPENT];
  101. int cnt = 0;
  102. /* Always call input_send_keycodes() at least once */
  103. do {
  104. if (fifo_cnt)
  105. cnt = tegra_kbc_find_keys(priv, fifo, KBC_MAX_KPENT);
  106. input_send_keycodes(priv->input, fifo, cnt);
  107. } while (--fifo_cnt > 0);
  108. }
  109. /**
  110. * Check the keyboard controller and emit ASCII characters for any keys that
  111. * are pressed.
  112. *
  113. * @param priv Keyboard private data
  114. */
  115. static void check_for_keys(struct tegra_kbd_priv *priv)
  116. {
  117. int fifo_cnt;
  118. if (!priv->first_scan &&
  119. get_timer(priv->last_poll_ms) < KBC_REPEAT_RATE_MS)
  120. return;
  121. priv->last_poll_ms = get_timer(0);
  122. priv->first_scan = 0;
  123. /*
  124. * Once we get here we know the keyboard has been scanned. So if there
  125. * scan waiting for us, we know that nothing is held down.
  126. */
  127. fifo_cnt = (readl(&priv->kbc->interrupt) >> 4) & 0xf;
  128. process_fifo(priv, fifo_cnt);
  129. }
  130. /**
  131. * In order to detect keys pressed on boot, wait for the hardware to
  132. * complete scanning the keys. This includes time to transition from
  133. * Wkup mode to Continous polling mode and the repoll time. We can
  134. * deduct the time that's already elapsed.
  135. *
  136. * @param priv Keyboard private data
  137. */
  138. static void kbd_wait_for_fifo_init(struct tegra_kbd_priv *priv)
  139. {
  140. if (!priv->inited) {
  141. unsigned long elapsed_time;
  142. long delay_ms;
  143. elapsed_time = get_timer(priv->start_time_ms);
  144. delay_ms = priv->init_dly_ms - elapsed_time;
  145. if (delay_ms > 0) {
  146. udelay(delay_ms * 1000);
  147. debug("%s: delay %ldms\n", __func__, delay_ms);
  148. }
  149. priv->inited = 1;
  150. }
  151. }
  152. /**
  153. * Check the tegra keyboard, and send any keys that are pressed.
  154. *
  155. * This is called by input_tstc() and input_getc() when they need more
  156. * characters
  157. *
  158. * @param input Input configuration
  159. * @return 1, to indicate that we have something to look at
  160. */
  161. static int tegra_kbc_check(struct input_config *input)
  162. {
  163. struct tegra_kbd_priv *priv = dev_get_priv(input->dev);
  164. kbd_wait_for_fifo_init(priv);
  165. check_for_keys(priv);
  166. return 1;
  167. }
  168. /* configures keyboard GPIO registers to use the rows and columns */
  169. static void config_kbc_gpio(struct tegra_kbd_priv *priv, struct kbc_tegra *kbc)
  170. {
  171. int i;
  172. for (i = 0; i < KBC_MAX_GPIO; i++) {
  173. u32 row_cfg, col_cfg;
  174. u32 r_shift = 5 * (i % 6);
  175. u32 c_shift = 4 * (i % 8);
  176. u32 r_mask = 0x1f << r_shift;
  177. u32 c_mask = 0xf << c_shift;
  178. u32 r_offs = i / 6;
  179. u32 c_offs = i / 8;
  180. row_cfg = readl(&kbc->row_cfg[r_offs]);
  181. col_cfg = readl(&kbc->col_cfg[c_offs]);
  182. row_cfg &= ~r_mask;
  183. col_cfg &= ~c_mask;
  184. if (i < priv->matrix.num_rows) {
  185. row_cfg |= ((i << 1) | 1) << r_shift;
  186. } else {
  187. col_cfg |= (((i - priv->matrix.num_rows) << 1) | 1)
  188. << c_shift;
  189. }
  190. writel(row_cfg, &kbc->row_cfg[r_offs]);
  191. writel(col_cfg, &kbc->col_cfg[c_offs]);
  192. }
  193. }
  194. /**
  195. * Start up the keyboard device
  196. */
  197. static void tegra_kbc_open(struct tegra_kbd_priv *priv)
  198. {
  199. struct kbc_tegra *kbc = priv->kbc;
  200. unsigned int scan_period;
  201. u32 val;
  202. /*
  203. * We will scan at twice the keyboard repeat rate, so that there is
  204. * always a scan ready when we check it in check_for_keys().
  205. */
  206. scan_period = KBC_REPEAT_RATE_MS / 2;
  207. writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly);
  208. writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly);
  209. /*
  210. * Before reading from the keyboard we must wait for the init_dly
  211. * plus the rpt_delay, plus 2ms for the row scan time.
  212. */
  213. priv->init_dly_ms = scan_period * 2 + 2;
  214. val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT;
  215. val |= 1 << KBC_FIFO_TH_CNT_SHIFT; /* fifo interrupt threshold */
  216. val |= KBC_CONTROL_KBC_EN; /* enable */
  217. writel(val, &kbc->control);
  218. priv->start_time_ms = get_timer(0);
  219. priv->last_poll_ms = get_timer(0);
  220. priv->next_repeat_ms = priv->last_poll_ms;
  221. priv->first_scan = 1;
  222. }
  223. static int tegra_kbd_start(struct udevice *dev)
  224. {
  225. struct tegra_kbd_priv *priv = dev_get_priv(dev);
  226. /* Set up pin mux and enable the clock */
  227. funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT);
  228. clock_enable(PERIPH_ID_KBC);
  229. config_kbc_gpio(priv, priv->kbc);
  230. tegra_kbc_open(priv);
  231. debug("%s: Tegra keyboard ready\n", __func__);
  232. return 0;
  233. }
  234. /**
  235. * Set up the tegra keyboard. This is called by the stdio device handler
  236. *
  237. * We want to do this init when the keyboard is actually used rather than
  238. * at start-up, since keyboard input may not currently be selected.
  239. *
  240. * Once the keyboard starts there will be a period during which we must
  241. * wait for the keyboard to init. We do this only when a key is first
  242. * read - see kbd_wait_for_fifo_init().
  243. *
  244. * @return 0 if ok, -ve on error
  245. */
  246. static int tegra_kbd_probe(struct udevice *dev)
  247. {
  248. struct tegra_kbd_priv *priv = dev_get_priv(dev);
  249. struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
  250. struct stdio_dev *sdev = &uc_priv->sdev;
  251. struct input_config *input = &uc_priv->input;
  252. int ret;
  253. priv->kbc = (struct kbc_tegra *)devfdt_get_addr(dev);
  254. if ((fdt_addr_t)priv->kbc == FDT_ADDR_T_NONE) {
  255. debug("%s: No keyboard register found\n", __func__);
  256. return -EINVAL;
  257. }
  258. input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
  259. /* Decode the keyboard matrix information (16 rows, 8 columns) */
  260. ret = key_matrix_init(&priv->matrix, 16, 8, 1);
  261. if (ret) {
  262. debug("%s: Could not init key matrix: %d\n", __func__, ret);
  263. return ret;
  264. }
  265. ret = key_matrix_decode_fdt(dev, &priv->matrix);
  266. if (ret) {
  267. debug("%s: Could not decode key matrix from fdt: %d\n",
  268. __func__, ret);
  269. return ret;
  270. }
  271. input_add_tables(input, false);
  272. if (priv->matrix.fn_keycode) {
  273. ret = input_add_table(input, KEY_FN, -1,
  274. priv->matrix.fn_keycode,
  275. priv->matrix.key_count);
  276. if (ret) {
  277. debug("%s: input_add_table() failed\n", __func__);
  278. return ret;
  279. }
  280. }
  281. /* Register the device. init_tegra_keyboard() will be called soon */
  282. priv->input = input;
  283. input->dev = dev;
  284. input->read_keys = tegra_kbc_check;
  285. strcpy(sdev->name, "tegra-kbc");
  286. ret = input_stdio_register(sdev);
  287. if (ret) {
  288. debug("%s: input_stdio_register() failed\n", __func__);
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. static const struct keyboard_ops tegra_kbd_ops = {
  294. .start = tegra_kbd_start,
  295. };
  296. static const struct udevice_id tegra_kbd_ids[] = {
  297. { .compatible = "nvidia,tegra20-kbc" },
  298. { }
  299. };
  300. U_BOOT_DRIVER(tegra_kbd) = {
  301. .name = "tegra_kbd",
  302. .id = UCLASS_KEYBOARD,
  303. .of_match = tegra_kbd_ids,
  304. .probe = tegra_kbd_probe,
  305. .ops = &tegra_kbd_ops,
  306. .priv_auto_alloc_size = sizeof(struct tegra_kbd_priv),
  307. };