tegra-kbc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/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. /*
  60. * After init we must wait a short time before polling the keyboard.
  61. * This gives the tegra keyboard controller time to react after reset
  62. * and lets us grab keys pressed during reset.
  63. */
  64. unsigned int init_dly_ms; /* Delay before we can read keyboard */
  65. unsigned int start_time_ms; /* Time that we inited (in ms) */
  66. unsigned int last_poll_ms; /* Time we should last polled */
  67. unsigned int next_repeat_ms; /* Next time we repeat a key */
  68. } config;
  69. /**
  70. * reads the keyboard fifo for current keypresses
  71. *
  72. * @param config Keyboard config
  73. * @param fifo Place to put fifo results
  74. * @param max_keycodes Maximum number of key codes to put in the fifo
  75. * @return number of items put into fifo
  76. */
  77. static int tegra_kbc_find_keys(struct keyb *config, int *fifo,
  78. int max_keycodes)
  79. {
  80. struct key_matrix_key keys[KBC_MAX_KPENT], *key;
  81. u32 kp_ent = 0;
  82. int i;
  83. for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) {
  84. /* Get next word */
  85. if (!(i & 3))
  86. kp_ent = readl(&config->kbc->kp_ent[i / 4]);
  87. key->valid = (kp_ent & KBC_KPENT_VALID) != 0;
  88. key->row = (kp_ent >> 3) & 0xf;
  89. key->col = kp_ent & 0x7;
  90. /* Shift to get next entry */
  91. kp_ent >>= 8;
  92. }
  93. return key_matrix_decode(&config->matrix, keys, KBC_MAX_KPENT, fifo,
  94. max_keycodes);
  95. }
  96. /**
  97. * Process all the keypress sequences in fifo and send key codes
  98. *
  99. * The fifo contains zero or more keypress sets. Each set
  100. * consists of from 1-8 keycodes, representing the keycodes which
  101. * were simultaneously pressed during that scan.
  102. *
  103. * This function works through each set and generates ASCII characters
  104. * for each. Not that one set may produce more than one ASCII characters -
  105. * for example holding down 'd' and 'f' at the same time will generate
  106. * two ASCII characters.
  107. *
  108. * Note: if fifo_cnt is 0, we will tell the input layer that no keys are
  109. * pressed.
  110. *
  111. * @param config Keyboard config
  112. * @param fifo_cnt Number of entries in the keyboard fifo
  113. */
  114. static void process_fifo(struct keyb *config, int fifo_cnt)
  115. {
  116. int fifo[KBC_MAX_KPENT];
  117. int cnt = 0;
  118. /* Always call input_send_keycodes() at least once */
  119. do {
  120. if (fifo_cnt)
  121. cnt = tegra_kbc_find_keys(config, fifo, KBC_MAX_KPENT);
  122. input_send_keycodes(&config->input, fifo, cnt);
  123. } while (--fifo_cnt > 0);
  124. }
  125. /**
  126. * Check the keyboard controller and emit ASCII characters for any keys that
  127. * are pressed.
  128. *
  129. * @param config Keyboard config
  130. */
  131. static void check_for_keys(struct keyb *config)
  132. {
  133. int fifo_cnt;
  134. if (!config->first_scan &&
  135. get_timer(config->last_poll_ms) < KBC_REPEAT_RATE_MS)
  136. return;
  137. config->last_poll_ms = get_timer(0);
  138. config->first_scan = 0;
  139. /*
  140. * Once we get here we know the keyboard has been scanned. So if there
  141. * scan waiting for us, we know that nothing is held down.
  142. */
  143. fifo_cnt = (readl(&config->kbc->interrupt) >> 4) & 0xf;
  144. process_fifo(config, fifo_cnt);
  145. }
  146. /**
  147. * In order to detect keys pressed on boot, wait for the hardware to
  148. * complete scanning the keys. This includes time to transition from
  149. * Wkup mode to Continous polling mode and the repoll time. We can
  150. * deduct the time that's already elapsed.
  151. *
  152. * @param config Keyboard config
  153. */
  154. static void kbd_wait_for_fifo_init(struct keyb *config)
  155. {
  156. if (!config->inited) {
  157. unsigned long elapsed_time;
  158. long delay_ms;
  159. elapsed_time = get_timer(config->start_time_ms);
  160. delay_ms = config->init_dly_ms - elapsed_time;
  161. if (delay_ms > 0) {
  162. udelay(delay_ms * 1000);
  163. debug("%s: delay %ldms\n", __func__, delay_ms);
  164. }
  165. config->inited = 1;
  166. }
  167. }
  168. /**
  169. * Check the tegra keyboard, and send any keys that are pressed.
  170. *
  171. * This is called by input_tstc() and input_getc() when they need more
  172. * characters
  173. *
  174. * @param input Input configuration
  175. * @return 1, to indicate that we have something to look at
  176. */
  177. int tegra_kbc_check(struct input_config *input)
  178. {
  179. kbd_wait_for_fifo_init(&config);
  180. check_for_keys(&config);
  181. return 1;
  182. }
  183. /**
  184. * Test if keys are available to be read
  185. *
  186. * @return 0 if no keys available, 1 if keys are available
  187. */
  188. static int kbd_tstc(void)
  189. {
  190. /* Just get input to do this for us */
  191. return input_tstc(&config.input);
  192. }
  193. /**
  194. * Read a key
  195. *
  196. * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key...
  197. *
  198. * @return ASCII key code, or 0 if no key, or -1 if error
  199. */
  200. static int kbd_getc(void)
  201. {
  202. /* Just get input to do this for us */
  203. return input_getc(&config.input);
  204. }
  205. /* configures keyboard GPIO registers to use the rows and columns */
  206. static void config_kbc_gpio(struct kbc_tegra *kbc)
  207. {
  208. int i;
  209. for (i = 0; i < KBC_MAX_GPIO; i++) {
  210. u32 row_cfg, col_cfg;
  211. u32 r_shift = 5 * (i % 6);
  212. u32 c_shift = 4 * (i % 8);
  213. u32 r_mask = 0x1f << r_shift;
  214. u32 c_mask = 0xf << c_shift;
  215. u32 r_offs = i / 6;
  216. u32 c_offs = i / 8;
  217. row_cfg = readl(&kbc->row_cfg[r_offs]);
  218. col_cfg = readl(&kbc->col_cfg[c_offs]);
  219. row_cfg &= ~r_mask;
  220. col_cfg &= ~c_mask;
  221. if (i < config.matrix.num_rows) {
  222. row_cfg |= ((i << 1) | 1) << r_shift;
  223. } else {
  224. col_cfg |= (((i - config.matrix.num_rows) << 1) | 1)
  225. << c_shift;
  226. }
  227. writel(row_cfg, &kbc->row_cfg[r_offs]);
  228. writel(col_cfg, &kbc->col_cfg[c_offs]);
  229. }
  230. }
  231. /**
  232. * Start up the keyboard device
  233. */
  234. static void tegra_kbc_open(void)
  235. {
  236. struct kbc_tegra *kbc = config.kbc;
  237. unsigned int scan_period;
  238. u32 val;
  239. /*
  240. * We will scan at twice the keyboard repeat rate, so that there is
  241. * always a scan ready when we check it in check_for_keys().
  242. */
  243. scan_period = KBC_REPEAT_RATE_MS / 2;
  244. writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly);
  245. writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly);
  246. /*
  247. * Before reading from the keyboard we must wait for the init_dly
  248. * plus the rpt_delay, plus 2ms for the row scan time.
  249. */
  250. config.init_dly_ms = scan_period * 2 + 2;
  251. val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT;
  252. val |= 1 << KBC_FIFO_TH_CNT_SHIFT; /* fifo interrupt threshold */
  253. val |= KBC_CONTROL_KBC_EN; /* enable */
  254. writel(val, &kbc->control);
  255. config.start_time_ms = get_timer(0);
  256. config.last_poll_ms = config.next_repeat_ms = get_timer(0);
  257. config.first_scan = 1;
  258. }
  259. /**
  260. * Set up the tegra keyboard. This is called by the stdio device handler
  261. *
  262. * We want to do this init when the keyboard is actually used rather than
  263. * at start-up, since keyboard input may not currently be selected.
  264. *
  265. * Once the keyboard starts there will be a period during which we must
  266. * wait for the keyboard to init. We do this only when a key is first
  267. * read - see kbd_wait_for_fifo_init().
  268. *
  269. * @return 0 if ok, -ve on error
  270. */
  271. static int init_tegra_keyboard(void)
  272. {
  273. #ifdef CONFIG_OF_CONTROL
  274. int node;
  275. node = fdtdec_next_compatible(gd->fdt_blob, 0,
  276. COMPAT_NVIDIA_TEGRA20_KBC);
  277. if (node < 0) {
  278. debug("%s: cannot locate keyboard node\n", __func__);
  279. return node;
  280. }
  281. config.kbc = (struct kbc_tegra *)fdtdec_get_addr(gd->fdt_blob,
  282. node, "reg");
  283. if ((fdt_addr_t)config.kbc == FDT_ADDR_T_NONE) {
  284. debug("%s: No keyboard register found\n", __func__);
  285. return -1;
  286. }
  287. /* Decode the keyboard matrix information (16 rows, 8 columns) */
  288. if (key_matrix_init(&config.matrix, 16, 8)) {
  289. debug("%s: Could not init key matrix\n", __func__);
  290. return -1;
  291. }
  292. if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
  293. debug("%s: Could not decode key matrix from fdt\n", __func__);
  294. return -1;
  295. }
  296. if (config.matrix.fn_keycode) {
  297. if (input_add_table(&config.input, KEY_FN, -1,
  298. config.matrix.fn_keycode,
  299. config.matrix.key_count))
  300. return -1;
  301. }
  302. #else
  303. #error "Tegra keyboard driver requires FDT definitions"
  304. #endif
  305. /* Set up pin mux and enable the clock */
  306. funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT);
  307. clock_enable(PERIPH_ID_KBC);
  308. config_kbc_gpio(config.kbc);
  309. tegra_kbc_open();
  310. debug("%s: Tegra keyboard ready\n", __func__);
  311. return 0;
  312. }
  313. int drv_keyboard_init(void)
  314. {
  315. struct stdio_dev dev;
  316. if (input_init(&config.input, 0, KBC_REPEAT_DELAY_MS,
  317. KBC_REPEAT_RATE_MS)) {
  318. debug("%s: Cannot set up input\n", __func__);
  319. return -1;
  320. }
  321. config.input.read_keys = tegra_kbc_check;
  322. memset(&dev, '\0', sizeof(dev));
  323. strcpy(dev.name, "tegra-kbc");
  324. dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  325. dev.getc = kbd_getc;
  326. dev.tstc = kbd_tstc;
  327. dev.start = init_tegra_keyboard;
  328. /* Register the device. init_tegra_keyboard() will be called soon */
  329. return input_stdio_register(&dev);
  330. }