tegra-kbc.c 9.4 KB

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