tegra-kbc.c 20 KB

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