tegra-kbc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Keyboard class input driver for the NVIDIA Tegra SoC internal matrix
  3. * keyboard controller
  4. *
  5. * Copyright (c) 2009-2011, NVIDIA Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/input.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/of.h>
  29. #include <linux/clk.h>
  30. #include <linux/slab.h>
  31. #include <linux/input/matrix_keypad.h>
  32. #include <linux/clk/tegra.h>
  33. #define KBC_MAX_GPIO 24
  34. #define KBC_MAX_KPENT 8
  35. #define KBC_MAX_ROW 16
  36. #define KBC_MAX_COL 8
  37. #define KBC_MAX_KEY (KBC_MAX_ROW * KBC_MAX_COL)
  38. #define KBC_MAX_DEBOUNCE_CNT 0x3ffu
  39. /* KBC row scan time and delay for beginning the row scan. */
  40. #define KBC_ROW_SCAN_TIME 16
  41. #define KBC_ROW_SCAN_DLY 5
  42. /* KBC uses a 32KHz clock so a cycle = 1/32Khz */
  43. #define KBC_CYCLE_MS 32
  44. /* KBC Registers */
  45. /* KBC Control Register */
  46. #define KBC_CONTROL_0 0x0
  47. #define KBC_FIFO_TH_CNT_SHIFT(cnt) (cnt << 14)
  48. #define KBC_DEBOUNCE_CNT_SHIFT(cnt) (cnt << 4)
  49. #define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3)
  50. #define KBC_CONTROL_KEYPRESS_INT_EN (1 << 1)
  51. #define KBC_CONTROL_KBC_EN (1 << 0)
  52. /* KBC Interrupt Register */
  53. #define KBC_INT_0 0x4
  54. #define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
  55. #define KBC_INT_KEYPRESS_INT_STATUS (1 << 0)
  56. #define KBC_ROW_CFG0_0 0x8
  57. #define KBC_COL_CFG0_0 0x18
  58. #define KBC_TO_CNT_0 0x24
  59. #define KBC_INIT_DLY_0 0x28
  60. #define KBC_RPT_DLY_0 0x2c
  61. #define KBC_KP_ENT0_0 0x30
  62. #define KBC_KP_ENT1_0 0x34
  63. #define KBC_ROW0_MASK_0 0x38
  64. #define KBC_ROW_SHIFT 3
  65. enum tegra_pin_type {
  66. PIN_CFG_IGNORE,
  67. PIN_CFG_COL,
  68. PIN_CFG_ROW,
  69. };
  70. struct tegra_kbc_pin_cfg {
  71. enum tegra_pin_type type;
  72. unsigned char num;
  73. };
  74. struct tegra_kbc {
  75. struct device *dev;
  76. unsigned int debounce_cnt;
  77. unsigned int repeat_cnt;
  78. struct tegra_kbc_pin_cfg pin_cfg[KBC_MAX_GPIO];
  79. const struct matrix_keymap_data *keymap_data;
  80. bool wakeup;
  81. void __iomem *mmio;
  82. struct input_dev *idev;
  83. int irq;
  84. spinlock_t lock;
  85. unsigned int repoll_dly;
  86. unsigned long cp_dly_jiffies;
  87. unsigned int cp_to_wkup_dly;
  88. bool use_fn_map;
  89. bool use_ghost_filter;
  90. bool keypress_caused_wake;
  91. unsigned short keycode[KBC_MAX_KEY * 2];
  92. unsigned short current_keys[KBC_MAX_KPENT];
  93. unsigned int num_pressed_keys;
  94. u32 wakeup_key;
  95. struct timer_list timer;
  96. struct clk *clk;
  97. };
  98. static void tegra_kbc_report_released_keys(struct input_dev *input,
  99. unsigned short old_keycodes[],
  100. unsigned int old_num_keys,
  101. unsigned short new_keycodes[],
  102. unsigned int new_num_keys)
  103. {
  104. unsigned int i, j;
  105. for (i = 0; i < old_num_keys; i++) {
  106. for (j = 0; j < new_num_keys; j++)
  107. if (old_keycodes[i] == new_keycodes[j])
  108. break;
  109. if (j == new_num_keys)
  110. input_report_key(input, old_keycodes[i], 0);
  111. }
  112. }
  113. static void tegra_kbc_report_pressed_keys(struct input_dev *input,
  114. unsigned char scancodes[],
  115. unsigned short keycodes[],
  116. unsigned int num_pressed_keys)
  117. {
  118. unsigned int i;
  119. for (i = 0; i < num_pressed_keys; i++) {
  120. input_event(input, EV_MSC, MSC_SCAN, scancodes[i]);
  121. input_report_key(input, keycodes[i], 1);
  122. }
  123. }
  124. static void tegra_kbc_report_keys(struct tegra_kbc *kbc)
  125. {
  126. unsigned char scancodes[KBC_MAX_KPENT];
  127. unsigned short keycodes[KBC_MAX_KPENT];
  128. u32 val = 0;
  129. unsigned int i;
  130. unsigned int num_down = 0;
  131. bool fn_keypress = false;
  132. bool key_in_same_row = false;
  133. bool key_in_same_col = false;
  134. for (i = 0; i < KBC_MAX_KPENT; i++) {
  135. if ((i % 4) == 0)
  136. val = readl(kbc->mmio + KBC_KP_ENT0_0 + i);
  137. if (val & 0x80) {
  138. unsigned int col = val & 0x07;
  139. unsigned int row = (val >> 3) & 0x0f;
  140. unsigned char scancode =
  141. MATRIX_SCAN_CODE(row, col, KBC_ROW_SHIFT);
  142. scancodes[num_down] = scancode;
  143. keycodes[num_down] = kbc->keycode[scancode];
  144. /* If driver uses Fn map, do not report the Fn key. */
  145. if ((keycodes[num_down] == KEY_FN) && kbc->use_fn_map)
  146. fn_keypress = true;
  147. else
  148. num_down++;
  149. }
  150. val >>= 8;
  151. }
  152. /*
  153. * Matrix keyboard designs are prone to keyboard ghosting.
  154. * Ghosting occurs if there are 3 keys such that -
  155. * any 2 of the 3 keys share a row, and any 2 of them share a column.
  156. * If so ignore the key presses for this iteration.
  157. */
  158. if (kbc->use_ghost_filter && num_down >= 3) {
  159. for (i = 0; i < num_down; i++) {
  160. unsigned int j;
  161. u8 curr_col = scancodes[i] & 0x07;
  162. u8 curr_row = scancodes[i] >> KBC_ROW_SHIFT;
  163. /*
  164. * Find 2 keys such that one key is in the same row
  165. * and the other is in the same column as the i-th key.
  166. */
  167. for (j = i + 1; j < num_down; j++) {
  168. u8 col = scancodes[j] & 0x07;
  169. u8 row = scancodes[j] >> KBC_ROW_SHIFT;
  170. if (col == curr_col)
  171. key_in_same_col = true;
  172. if (row == curr_row)
  173. key_in_same_row = true;
  174. }
  175. }
  176. }
  177. /*
  178. * If the platform uses Fn keymaps, translate keys on a Fn keypress.
  179. * Function keycodes are KBC_MAX_KEY apart from the plain keycodes.
  180. */
  181. if (fn_keypress) {
  182. for (i = 0; i < num_down; i++) {
  183. scancodes[i] += KBC_MAX_KEY;
  184. keycodes[i] = kbc->keycode[scancodes[i]];
  185. }
  186. }
  187. /* Ignore the key presses for this iteration? */
  188. if (key_in_same_col && key_in_same_row)
  189. return;
  190. tegra_kbc_report_released_keys(kbc->idev,
  191. kbc->current_keys, kbc->num_pressed_keys,
  192. keycodes, num_down);
  193. tegra_kbc_report_pressed_keys(kbc->idev, scancodes, keycodes, num_down);
  194. input_sync(kbc->idev);
  195. memcpy(kbc->current_keys, keycodes, sizeof(kbc->current_keys));
  196. kbc->num_pressed_keys = num_down;
  197. }
  198. static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
  199. {
  200. u32 val;
  201. val = readl(kbc->mmio + KBC_CONTROL_0);
  202. if (enable)
  203. val |= KBC_CONTROL_FIFO_CNT_INT_EN;
  204. else
  205. val &= ~KBC_CONTROL_FIFO_CNT_INT_EN;
  206. writel(val, kbc->mmio + KBC_CONTROL_0);
  207. }
  208. static void tegra_kbc_keypress_timer(unsigned long data)
  209. {
  210. struct tegra_kbc *kbc = (struct tegra_kbc *)data;
  211. unsigned long flags;
  212. u32 val;
  213. unsigned int i;
  214. spin_lock_irqsave(&kbc->lock, flags);
  215. val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
  216. if (val) {
  217. unsigned long dly;
  218. tegra_kbc_report_keys(kbc);
  219. /*
  220. * If more than one keys are pressed we need not wait
  221. * for the repoll delay.
  222. */
  223. dly = (val == 1) ? kbc->repoll_dly : 1;
  224. mod_timer(&kbc->timer, jiffies + msecs_to_jiffies(dly));
  225. } else {
  226. /* Release any pressed keys and exit the polling loop */
  227. for (i = 0; i < kbc->num_pressed_keys; i++)
  228. input_report_key(kbc->idev, kbc->current_keys[i], 0);
  229. input_sync(kbc->idev);
  230. kbc->num_pressed_keys = 0;
  231. /* All keys are released so enable the keypress interrupt */
  232. tegra_kbc_set_fifo_interrupt(kbc, true);
  233. }
  234. spin_unlock_irqrestore(&kbc->lock, flags);
  235. }
  236. static irqreturn_t tegra_kbc_isr(int irq, void *args)
  237. {
  238. struct tegra_kbc *kbc = args;
  239. unsigned long flags;
  240. u32 val;
  241. spin_lock_irqsave(&kbc->lock, flags);
  242. /*
  243. * Quickly bail out & reenable interrupts if the fifo threshold
  244. * count interrupt wasn't the interrupt source
  245. */
  246. val = readl(kbc->mmio + KBC_INT_0);
  247. writel(val, kbc->mmio + KBC_INT_0);
  248. if (val & KBC_INT_FIFO_CNT_INT_STATUS) {
  249. /*
  250. * Until all keys are released, defer further processing to
  251. * the polling loop in tegra_kbc_keypress_timer.
  252. */
  253. tegra_kbc_set_fifo_interrupt(kbc, false);
  254. mod_timer(&kbc->timer, jiffies + kbc->cp_dly_jiffies);
  255. } else if (val & KBC_INT_KEYPRESS_INT_STATUS) {
  256. /* We can be here only through system resume path */
  257. kbc->keypress_caused_wake = true;
  258. }
  259. spin_unlock_irqrestore(&kbc->lock, flags);
  260. return IRQ_HANDLED;
  261. }
  262. static void tegra_kbc_setup_wakekeys(struct tegra_kbc *kbc, bool filter)
  263. {
  264. int i;
  265. unsigned int rst_val;
  266. /* Either mask all keys or none. */
  267. rst_val = (filter && !kbc->wakeup) ? ~0 : 0;
  268. for (i = 0; i < KBC_MAX_ROW; i++)
  269. writel(rst_val, kbc->mmio + KBC_ROW0_MASK_0 + i * 4);
  270. }
  271. static void tegra_kbc_config_pins(struct tegra_kbc *kbc)
  272. {
  273. int i;
  274. for (i = 0; i < KBC_MAX_GPIO; i++) {
  275. u32 r_shft = 5 * (i % 6);
  276. u32 c_shft = 4 * (i % 8);
  277. u32 r_mask = 0x1f << r_shft;
  278. u32 c_mask = 0x0f << c_shft;
  279. u32 r_offs = (i / 6) * 4 + KBC_ROW_CFG0_0;
  280. u32 c_offs = (i / 8) * 4 + KBC_COL_CFG0_0;
  281. u32 row_cfg = readl(kbc->mmio + r_offs);
  282. u32 col_cfg = readl(kbc->mmio + c_offs);
  283. row_cfg &= ~r_mask;
  284. col_cfg &= ~c_mask;
  285. switch (kbc->pin_cfg[i].type) {
  286. case PIN_CFG_ROW:
  287. row_cfg |= ((kbc->pin_cfg[i].num << 1) | 1) << r_shft;
  288. break;
  289. case PIN_CFG_COL:
  290. col_cfg |= ((kbc->pin_cfg[i].num << 1) | 1) << c_shft;
  291. break;
  292. case PIN_CFG_IGNORE:
  293. break;
  294. }
  295. writel(row_cfg, kbc->mmio + r_offs);
  296. writel(col_cfg, kbc->mmio + c_offs);
  297. }
  298. }
  299. static int tegra_kbc_start(struct tegra_kbc *kbc)
  300. {
  301. unsigned int debounce_cnt;
  302. u32 val = 0;
  303. clk_prepare_enable(kbc->clk);
  304. /* Reset the KBC controller to clear all previous status.*/
  305. tegra_periph_reset_assert(kbc->clk);
  306. udelay(100);
  307. tegra_periph_reset_deassert(kbc->clk);
  308. udelay(100);
  309. tegra_kbc_config_pins(kbc);
  310. tegra_kbc_setup_wakekeys(kbc, false);
  311. writel(kbc->repeat_cnt, kbc->mmio + KBC_RPT_DLY_0);
  312. /* Keyboard debounce count is maximum of 12 bits. */
  313. debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
  314. val = KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt);
  315. val |= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
  316. val |= KBC_CONTROL_FIFO_CNT_INT_EN; /* interrupt on FIFO threshold */
  317. val |= KBC_CONTROL_KBC_EN; /* enable */
  318. writel(val, kbc->mmio + KBC_CONTROL_0);
  319. /*
  320. * Compute the delay(ns) from interrupt mode to continuous polling
  321. * mode so the timer routine is scheduled appropriately.
  322. */
  323. val = readl(kbc->mmio + KBC_INIT_DLY_0);
  324. kbc->cp_dly_jiffies = usecs_to_jiffies((val & 0xfffff) * 32);
  325. kbc->num_pressed_keys = 0;
  326. /*
  327. * Atomically clear out any remaining entries in the key FIFO
  328. * and enable keyboard interrupts.
  329. */
  330. while (1) {
  331. val = readl(kbc->mmio + KBC_INT_0);
  332. val >>= 4;
  333. if (!val)
  334. break;
  335. val = readl(kbc->mmio + KBC_KP_ENT0_0);
  336. val = readl(kbc->mmio + KBC_KP_ENT1_0);
  337. }
  338. writel(0x7, kbc->mmio + KBC_INT_0);
  339. enable_irq(kbc->irq);
  340. return 0;
  341. }
  342. static void tegra_kbc_stop(struct tegra_kbc *kbc)
  343. {
  344. unsigned long flags;
  345. u32 val;
  346. spin_lock_irqsave(&kbc->lock, flags);
  347. val = readl(kbc->mmio + KBC_CONTROL_0);
  348. val &= ~1;
  349. writel(val, kbc->mmio + KBC_CONTROL_0);
  350. spin_unlock_irqrestore(&kbc->lock, flags);
  351. disable_irq(kbc->irq);
  352. del_timer_sync(&kbc->timer);
  353. clk_disable_unprepare(kbc->clk);
  354. }
  355. static int tegra_kbc_open(struct input_dev *dev)
  356. {
  357. struct tegra_kbc *kbc = input_get_drvdata(dev);
  358. return tegra_kbc_start(kbc);
  359. }
  360. static void tegra_kbc_close(struct input_dev *dev)
  361. {
  362. struct tegra_kbc *kbc = input_get_drvdata(dev);
  363. return tegra_kbc_stop(kbc);
  364. }
  365. static bool tegra_kbc_check_pin_cfg(const struct tegra_kbc *kbc,
  366. unsigned int *num_rows)
  367. {
  368. int i;
  369. *num_rows = 0;
  370. for (i = 0; i < KBC_MAX_GPIO; i++) {
  371. const struct tegra_kbc_pin_cfg *pin_cfg = &kbc->pin_cfg[i];
  372. switch (pin_cfg->type) {
  373. case PIN_CFG_ROW:
  374. if (pin_cfg->num >= KBC_MAX_ROW) {
  375. dev_err(kbc->dev,
  376. "pin_cfg[%d]: invalid row number %d\n",
  377. i, pin_cfg->num);
  378. return false;
  379. }
  380. (*num_rows)++;
  381. break;
  382. case PIN_CFG_COL:
  383. if (pin_cfg->num >= KBC_MAX_COL) {
  384. dev_err(kbc->dev,
  385. "pin_cfg[%d]: invalid column number %d\n",
  386. i, pin_cfg->num);
  387. return false;
  388. }
  389. break;
  390. case PIN_CFG_IGNORE:
  391. break;
  392. default:
  393. dev_err(kbc->dev,
  394. "pin_cfg[%d]: invalid entry type %d\n",
  395. pin_cfg->type, pin_cfg->num);
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
  402. {
  403. struct device_node *np = kbc->dev->of_node;
  404. u32 prop;
  405. int i;
  406. u32 num_rows = 0;
  407. u32 num_cols = 0;
  408. u32 cols_cfg[KBC_MAX_GPIO];
  409. u32 rows_cfg[KBC_MAX_GPIO];
  410. int proplen;
  411. int ret;
  412. if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
  413. kbc->debounce_cnt = prop;
  414. if (!of_property_read_u32(np, "nvidia,repeat-delay-ms", &prop))
  415. kbc->repeat_cnt = prop;
  416. if (of_find_property(np, "nvidia,needs-ghost-filter", NULL))
  417. kbc->use_ghost_filter = true;
  418. if (of_find_property(np, "nvidia,wakeup-source", NULL))
  419. kbc->wakeup = true;
  420. if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
  421. dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
  422. return -ENOENT;
  423. }
  424. num_rows = proplen / sizeof(u32);
  425. if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
  426. dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
  427. return -ENOENT;
  428. }
  429. num_cols = proplen / sizeof(u32);
  430. if (!of_get_property(np, "linux,keymap", &proplen)) {
  431. dev_err(kbc->dev, "property linux,keymap not found\n");
  432. return -ENOENT;
  433. }
  434. if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
  435. dev_err(kbc->dev,
  436. "keypad rows/columns not porperly specified\n");
  437. return -EINVAL;
  438. }
  439. /* Set all pins as non-configured */
  440. for (i = 0; i < KBC_MAX_GPIO; i++)
  441. kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
  442. ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
  443. rows_cfg, num_rows);
  444. if (ret < 0) {
  445. dev_err(kbc->dev, "Rows configurations are not proper\n");
  446. return -EINVAL;
  447. }
  448. ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
  449. cols_cfg, num_cols);
  450. if (ret < 0) {
  451. dev_err(kbc->dev, "Cols configurations are not proper\n");
  452. return -EINVAL;
  453. }
  454. for (i = 0; i < num_rows; i++) {
  455. kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
  456. kbc->pin_cfg[rows_cfg[i]].num = i;
  457. }
  458. for (i = 0; i < num_cols; i++) {
  459. kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
  460. kbc->pin_cfg[cols_cfg[i]].num = i;
  461. }
  462. return 0;
  463. }
  464. static int tegra_kbc_probe(struct platform_device *pdev)
  465. {
  466. struct tegra_kbc *kbc;
  467. struct resource *res;
  468. int err;
  469. int num_rows = 0;
  470. unsigned int debounce_cnt;
  471. unsigned int scan_time_rows;
  472. unsigned int keymap_rows = KBC_MAX_KEY;
  473. kbc = devm_kzalloc(&pdev->dev, sizeof(*kbc), GFP_KERNEL);
  474. if (!kbc) {
  475. dev_err(&pdev->dev, "failed to alloc memory for kbc\n");
  476. return -ENOMEM;
  477. }
  478. kbc->dev = &pdev->dev;
  479. spin_lock_init(&kbc->lock);
  480. err = tegra_kbc_parse_dt(kbc);
  481. if (err)
  482. return err;
  483. if (!tegra_kbc_check_pin_cfg(kbc, &num_rows))
  484. return -EINVAL;
  485. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  486. if (!res) {
  487. dev_err(&pdev->dev, "failed to get I/O memory\n");
  488. return -ENXIO;
  489. }
  490. kbc->irq = platform_get_irq(pdev, 0);
  491. if (kbc->irq < 0) {
  492. dev_err(&pdev->dev, "failed to get keyboard IRQ\n");
  493. return -ENXIO;
  494. }
  495. kbc->idev = devm_input_allocate_device(&pdev->dev);
  496. if (!kbc->idev) {
  497. dev_err(&pdev->dev, "failed to allocate input device\n");
  498. return -ENOMEM;
  499. }
  500. setup_timer(&kbc->timer, tegra_kbc_keypress_timer, (unsigned long)kbc);
  501. kbc->mmio = devm_request_and_ioremap(&pdev->dev, res);
  502. if (!kbc->mmio) {
  503. dev_err(&pdev->dev, "Cannot request memregion/iomap address\n");
  504. return -EBUSY;
  505. }
  506. kbc->clk = devm_clk_get(&pdev->dev, NULL);
  507. if (IS_ERR(kbc->clk)) {
  508. dev_err(&pdev->dev, "failed to get keyboard clock\n");
  509. return PTR_ERR(kbc->clk);
  510. }
  511. /*
  512. * The time delay between two consecutive reads of the FIFO is
  513. * the sum of the repeat time and the time taken for scanning
  514. * the rows. There is an additional delay before the row scanning
  515. * starts. The repoll delay is computed in milliseconds.
  516. */
  517. debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
  518. scan_time_rows = (KBC_ROW_SCAN_TIME + debounce_cnt) * num_rows;
  519. kbc->repoll_dly = KBC_ROW_SCAN_DLY + scan_time_rows + kbc->repeat_cnt;
  520. kbc->repoll_dly = DIV_ROUND_UP(kbc->repoll_dly, KBC_CYCLE_MS);
  521. kbc->idev->name = pdev->name;
  522. kbc->idev->id.bustype = BUS_HOST;
  523. kbc->idev->dev.parent = &pdev->dev;
  524. kbc->idev->open = tegra_kbc_open;
  525. kbc->idev->close = tegra_kbc_close;
  526. if (kbc->keymap_data && kbc->use_fn_map)
  527. keymap_rows *= 2;
  528. err = matrix_keypad_build_keymap(kbc->keymap_data, NULL,
  529. keymap_rows, KBC_MAX_COL,
  530. kbc->keycode, kbc->idev);
  531. if (err) {
  532. dev_err(&pdev->dev, "failed to setup keymap\n");
  533. return err;
  534. }
  535. __set_bit(EV_REP, kbc->idev->evbit);
  536. input_set_capability(kbc->idev, EV_MSC, MSC_SCAN);
  537. input_set_drvdata(kbc->idev, kbc);
  538. err = devm_request_irq(&pdev->dev, kbc->irq, tegra_kbc_isr,
  539. IRQF_NO_SUSPEND | IRQF_TRIGGER_HIGH, pdev->name, kbc);
  540. if (err) {
  541. dev_err(&pdev->dev, "failed to request keyboard IRQ\n");
  542. return err;
  543. }
  544. disable_irq(kbc->irq);
  545. err = input_register_device(kbc->idev);
  546. if (err) {
  547. dev_err(&pdev->dev, "failed to register input device\n");
  548. return err;
  549. }
  550. platform_set_drvdata(pdev, kbc);
  551. device_init_wakeup(&pdev->dev, kbc->wakeup);
  552. return 0;
  553. }
  554. #ifdef CONFIG_PM_SLEEP
  555. static void tegra_kbc_set_keypress_interrupt(struct tegra_kbc *kbc, bool enable)
  556. {
  557. u32 val;
  558. val = readl(kbc->mmio + KBC_CONTROL_0);
  559. if (enable)
  560. val |= KBC_CONTROL_KEYPRESS_INT_EN;
  561. else
  562. val &= ~KBC_CONTROL_KEYPRESS_INT_EN;
  563. writel(val, kbc->mmio + KBC_CONTROL_0);
  564. }
  565. static int tegra_kbc_suspend(struct device *dev)
  566. {
  567. struct platform_device *pdev = to_platform_device(dev);
  568. struct tegra_kbc *kbc = platform_get_drvdata(pdev);
  569. mutex_lock(&kbc->idev->mutex);
  570. if (device_may_wakeup(&pdev->dev)) {
  571. disable_irq(kbc->irq);
  572. del_timer_sync(&kbc->timer);
  573. tegra_kbc_set_fifo_interrupt(kbc, false);
  574. /* Forcefully clear the interrupt status */
  575. writel(0x7, kbc->mmio + KBC_INT_0);
  576. /*
  577. * Store the previous resident time of continuous polling mode.
  578. * Force the keyboard into interrupt mode.
  579. */
  580. kbc->cp_to_wkup_dly = readl(kbc->mmio + KBC_TO_CNT_0);
  581. writel(0, kbc->mmio + KBC_TO_CNT_0);
  582. tegra_kbc_setup_wakekeys(kbc, true);
  583. msleep(30);
  584. kbc->keypress_caused_wake = false;
  585. /* Enable keypress interrupt before going into suspend. */
  586. tegra_kbc_set_keypress_interrupt(kbc, true);
  587. enable_irq(kbc->irq);
  588. enable_irq_wake(kbc->irq);
  589. } else {
  590. if (kbc->idev->users)
  591. tegra_kbc_stop(kbc);
  592. }
  593. mutex_unlock(&kbc->idev->mutex);
  594. return 0;
  595. }
  596. static int tegra_kbc_resume(struct device *dev)
  597. {
  598. struct platform_device *pdev = to_platform_device(dev);
  599. struct tegra_kbc *kbc = platform_get_drvdata(pdev);
  600. int err = 0;
  601. mutex_lock(&kbc->idev->mutex);
  602. if (device_may_wakeup(&pdev->dev)) {
  603. disable_irq_wake(kbc->irq);
  604. tegra_kbc_setup_wakekeys(kbc, false);
  605. /* We will use fifo interrupts for key detection. */
  606. tegra_kbc_set_keypress_interrupt(kbc, false);
  607. /* Restore the resident time of continuous polling mode. */
  608. writel(kbc->cp_to_wkup_dly, kbc->mmio + KBC_TO_CNT_0);
  609. tegra_kbc_set_fifo_interrupt(kbc, true);
  610. if (kbc->keypress_caused_wake && kbc->wakeup_key) {
  611. /*
  612. * We can't report events directly from the ISR
  613. * because timekeeping is stopped when processing
  614. * wakeup request and we get a nasty warning when
  615. * we try to call do_gettimeofday() in evdev
  616. * handler.
  617. */
  618. input_report_key(kbc->idev, kbc->wakeup_key, 1);
  619. input_sync(kbc->idev);
  620. input_report_key(kbc->idev, kbc->wakeup_key, 0);
  621. input_sync(kbc->idev);
  622. }
  623. } else {
  624. if (kbc->idev->users)
  625. err = tegra_kbc_start(kbc);
  626. }
  627. mutex_unlock(&kbc->idev->mutex);
  628. return err;
  629. }
  630. #endif
  631. static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops, tegra_kbc_suspend, tegra_kbc_resume);
  632. static const struct of_device_id tegra_kbc_of_match[] = {
  633. { .compatible = "nvidia,tegra20-kbc", },
  634. { },
  635. };
  636. MODULE_DEVICE_TABLE(of, tegra_kbc_of_match);
  637. static struct platform_driver tegra_kbc_driver = {
  638. .probe = tegra_kbc_probe,
  639. .driver = {
  640. .name = "tegra-kbc",
  641. .owner = THIS_MODULE,
  642. .pm = &tegra_kbc_pm_ops,
  643. .of_match_table = tegra_kbc_of_match,
  644. },
  645. };
  646. module_platform_driver(tegra_kbc_driver);
  647. MODULE_LICENSE("GPL");
  648. MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
  649. MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
  650. MODULE_ALIAS("platform:tegra-kbc");