tegra-kbc.c 20 KB

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