tegra-kbc.c 10 KB

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