tegra-kbc.c 21 KB

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