tegra-kbc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. int ret;
  315. ret = clk_prepare_enable(kbc->clk);
  316. if (ret)
  317. return ret;
  318. /* Reset the KBC controller to clear all previous status.*/
  319. reset_control_assert(kbc->rst);
  320. udelay(100);
  321. reset_control_deassert(kbc->rst);
  322. udelay(100);
  323. tegra_kbc_config_pins(kbc);
  324. tegra_kbc_setup_wakekeys(kbc, false);
  325. writel(kbc->repeat_cnt, kbc->mmio + KBC_RPT_DLY_0);
  326. /* Keyboard debounce count is maximum of 12 bits. */
  327. debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
  328. val = KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt);
  329. val |= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
  330. val |= KBC_CONTROL_FIFO_CNT_INT_EN; /* interrupt on FIFO threshold */
  331. val |= KBC_CONTROL_KBC_EN; /* enable */
  332. writel(val, kbc->mmio + KBC_CONTROL_0);
  333. /*
  334. * Compute the delay(ns) from interrupt mode to continuous polling
  335. * mode so the timer routine is scheduled appropriately.
  336. */
  337. val = readl(kbc->mmio + KBC_INIT_DLY_0);
  338. kbc->cp_dly_jiffies = usecs_to_jiffies((val & 0xfffff) * 32);
  339. kbc->num_pressed_keys = 0;
  340. /*
  341. * Atomically clear out any remaining entries in the key FIFO
  342. * and enable keyboard interrupts.
  343. */
  344. while (1) {
  345. val = readl(kbc->mmio + KBC_INT_0);
  346. val >>= 4;
  347. if (!val)
  348. break;
  349. val = readl(kbc->mmio + KBC_KP_ENT0_0);
  350. val = readl(kbc->mmio + KBC_KP_ENT1_0);
  351. }
  352. writel(0x7, kbc->mmio + KBC_INT_0);
  353. enable_irq(kbc->irq);
  354. return 0;
  355. }
  356. static void tegra_kbc_stop(struct tegra_kbc *kbc)
  357. {
  358. unsigned long flags;
  359. u32 val;
  360. spin_lock_irqsave(&kbc->lock, flags);
  361. val = readl(kbc->mmio + KBC_CONTROL_0);
  362. val &= ~1;
  363. writel(val, kbc->mmio + KBC_CONTROL_0);
  364. spin_unlock_irqrestore(&kbc->lock, flags);
  365. disable_irq(kbc->irq);
  366. del_timer_sync(&kbc->timer);
  367. clk_disable_unprepare(kbc->clk);
  368. }
  369. static int tegra_kbc_open(struct input_dev *dev)
  370. {
  371. struct tegra_kbc *kbc = input_get_drvdata(dev);
  372. return tegra_kbc_start(kbc);
  373. }
  374. static void tegra_kbc_close(struct input_dev *dev)
  375. {
  376. struct tegra_kbc *kbc = input_get_drvdata(dev);
  377. return tegra_kbc_stop(kbc);
  378. }
  379. static bool tegra_kbc_check_pin_cfg(const struct tegra_kbc *kbc,
  380. unsigned int *num_rows)
  381. {
  382. int i;
  383. *num_rows = 0;
  384. for (i = 0; i < KBC_MAX_GPIO; i++) {
  385. const struct tegra_kbc_pin_cfg *pin_cfg = &kbc->pin_cfg[i];
  386. switch (pin_cfg->type) {
  387. case PIN_CFG_ROW:
  388. if (pin_cfg->num >= kbc->hw_support->max_rows) {
  389. dev_err(kbc->dev,
  390. "pin_cfg[%d]: invalid row number %d\n",
  391. i, pin_cfg->num);
  392. return false;
  393. }
  394. (*num_rows)++;
  395. break;
  396. case PIN_CFG_COL:
  397. if (pin_cfg->num >= kbc->hw_support->max_columns) {
  398. dev_err(kbc->dev,
  399. "pin_cfg[%d]: invalid column number %d\n",
  400. i, pin_cfg->num);
  401. return false;
  402. }
  403. break;
  404. case PIN_CFG_IGNORE:
  405. break;
  406. default:
  407. dev_err(kbc->dev,
  408. "pin_cfg[%d]: invalid entry type %d\n",
  409. pin_cfg->type, pin_cfg->num);
  410. return false;
  411. }
  412. }
  413. return true;
  414. }
  415. static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
  416. {
  417. struct device_node *np = kbc->dev->of_node;
  418. u32 prop;
  419. int i;
  420. u32 num_rows = 0;
  421. u32 num_cols = 0;
  422. u32 cols_cfg[KBC_MAX_GPIO];
  423. u32 rows_cfg[KBC_MAX_GPIO];
  424. int proplen;
  425. int ret;
  426. if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
  427. kbc->debounce_cnt = prop;
  428. if (!of_property_read_u32(np, "nvidia,repeat-delay-ms", &prop))
  429. kbc->repeat_cnt = prop;
  430. if (of_find_property(np, "nvidia,needs-ghost-filter", NULL))
  431. kbc->use_ghost_filter = true;
  432. if (of_property_read_bool(np, "wakeup-source") ||
  433. of_property_read_bool(np, "nvidia,wakeup-source")) /* legacy */
  434. kbc->wakeup = true;
  435. if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
  436. dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
  437. return -ENOENT;
  438. }
  439. num_rows = proplen / sizeof(u32);
  440. if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
  441. dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
  442. return -ENOENT;
  443. }
  444. num_cols = proplen / sizeof(u32);
  445. if (num_rows > kbc->hw_support->max_rows) {
  446. dev_err(kbc->dev,
  447. "Number of rows is more than supported by hardware\n");
  448. return -EINVAL;
  449. }
  450. if (num_cols > kbc->hw_support->max_columns) {
  451. dev_err(kbc->dev,
  452. "Number of cols is more than supported by hardware\n");
  453. return -EINVAL;
  454. }
  455. if (!of_get_property(np, "linux,keymap", &proplen)) {
  456. dev_err(kbc->dev, "property linux,keymap not found\n");
  457. return -ENOENT;
  458. }
  459. if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
  460. dev_err(kbc->dev,
  461. "keypad rows/columns not properly specified\n");
  462. return -EINVAL;
  463. }
  464. /* Set all pins as non-configured */
  465. for (i = 0; i < kbc->num_rows_and_columns; i++)
  466. kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
  467. ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
  468. rows_cfg, num_rows);
  469. if (ret < 0) {
  470. dev_err(kbc->dev, "Rows configurations are not proper\n");
  471. return -EINVAL;
  472. }
  473. ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
  474. cols_cfg, num_cols);
  475. if (ret < 0) {
  476. dev_err(kbc->dev, "Cols configurations are not proper\n");
  477. return -EINVAL;
  478. }
  479. for (i = 0; i < num_rows; i++) {
  480. kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
  481. kbc->pin_cfg[rows_cfg[i]].num = i;
  482. }
  483. for (i = 0; i < num_cols; i++) {
  484. kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
  485. kbc->pin_cfg[cols_cfg[i]].num = i;
  486. }
  487. return 0;
  488. }
  489. static const struct tegra_kbc_hw_support tegra20_kbc_hw_support = {
  490. .max_rows = 16,
  491. .max_columns = 8,
  492. };
  493. static const struct tegra_kbc_hw_support tegra11_kbc_hw_support = {
  494. .max_rows = 11,
  495. .max_columns = 8,
  496. };
  497. static const struct of_device_id tegra_kbc_of_match[] = {
  498. { .compatible = "nvidia,tegra114-kbc", .data = &tegra11_kbc_hw_support},
  499. { .compatible = "nvidia,tegra30-kbc", .data = &tegra20_kbc_hw_support},
  500. { .compatible = "nvidia,tegra20-kbc", .data = &tegra20_kbc_hw_support},
  501. { },
  502. };
  503. MODULE_DEVICE_TABLE(of, tegra_kbc_of_match);
  504. static int tegra_kbc_probe(struct platform_device *pdev)
  505. {
  506. struct tegra_kbc *kbc;
  507. struct resource *res;
  508. int err;
  509. int num_rows = 0;
  510. unsigned int debounce_cnt;
  511. unsigned int scan_time_rows;
  512. unsigned int keymap_rows;
  513. const struct of_device_id *match;
  514. match = of_match_device(tegra_kbc_of_match, &pdev->dev);
  515. kbc = devm_kzalloc(&pdev->dev, sizeof(*kbc), GFP_KERNEL);
  516. if (!kbc) {
  517. dev_err(&pdev->dev, "failed to alloc memory for kbc\n");
  518. return -ENOMEM;
  519. }
  520. kbc->dev = &pdev->dev;
  521. kbc->hw_support = match->data;
  522. kbc->max_keys = kbc->hw_support->max_rows *
  523. kbc->hw_support->max_columns;
  524. kbc->num_rows_and_columns = kbc->hw_support->max_rows +
  525. kbc->hw_support->max_columns;
  526. keymap_rows = kbc->max_keys;
  527. spin_lock_init(&kbc->lock);
  528. err = tegra_kbc_parse_dt(kbc);
  529. if (err)
  530. return err;
  531. if (!tegra_kbc_check_pin_cfg(kbc, &num_rows))
  532. return -EINVAL;
  533. kbc->irq = platform_get_irq(pdev, 0);
  534. if (kbc->irq < 0) {
  535. dev_err(&pdev->dev, "failed to get keyboard IRQ\n");
  536. return -ENXIO;
  537. }
  538. kbc->idev = devm_input_allocate_device(&pdev->dev);
  539. if (!kbc->idev) {
  540. dev_err(&pdev->dev, "failed to allocate input device\n");
  541. return -ENOMEM;
  542. }
  543. setup_timer(&kbc->timer, tegra_kbc_keypress_timer, (unsigned long)kbc);
  544. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  545. kbc->mmio = devm_ioremap_resource(&pdev->dev, res);
  546. if (IS_ERR(kbc->mmio))
  547. return PTR_ERR(kbc->mmio);
  548. kbc->clk = devm_clk_get(&pdev->dev, NULL);
  549. if (IS_ERR(kbc->clk)) {
  550. dev_err(&pdev->dev, "failed to get keyboard clock\n");
  551. return PTR_ERR(kbc->clk);
  552. }
  553. kbc->rst = devm_reset_control_get(&pdev->dev, "kbc");
  554. if (IS_ERR(kbc->rst)) {
  555. dev_err(&pdev->dev, "failed to get keyboard reset\n");
  556. return PTR_ERR(kbc->rst);
  557. }
  558. /*
  559. * The time delay between two consecutive reads of the FIFO is
  560. * the sum of the repeat time and the time taken for scanning
  561. * the rows. There is an additional delay before the row scanning
  562. * starts. The repoll delay is computed in milliseconds.
  563. */
  564. debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
  565. scan_time_rows = (KBC_ROW_SCAN_TIME + debounce_cnt) * num_rows;
  566. kbc->repoll_dly = KBC_ROW_SCAN_DLY + scan_time_rows + kbc->repeat_cnt;
  567. kbc->repoll_dly = DIV_ROUND_UP(kbc->repoll_dly, KBC_CYCLE_MS);
  568. kbc->idev->name = pdev->name;
  569. kbc->idev->id.bustype = BUS_HOST;
  570. kbc->idev->dev.parent = &pdev->dev;
  571. kbc->idev->open = tegra_kbc_open;
  572. kbc->idev->close = tegra_kbc_close;
  573. if (kbc->keymap_data && kbc->use_fn_map)
  574. keymap_rows *= 2;
  575. err = matrix_keypad_build_keymap(kbc->keymap_data, NULL,
  576. keymap_rows,
  577. kbc->hw_support->max_columns,
  578. kbc->keycode, kbc->idev);
  579. if (err) {
  580. dev_err(&pdev->dev, "failed to setup keymap\n");
  581. return err;
  582. }
  583. __set_bit(EV_REP, kbc->idev->evbit);
  584. input_set_capability(kbc->idev, EV_MSC, MSC_SCAN);
  585. input_set_drvdata(kbc->idev, kbc);
  586. err = devm_request_irq(&pdev->dev, kbc->irq, tegra_kbc_isr,
  587. IRQF_TRIGGER_HIGH, pdev->name, kbc);
  588. if (err) {
  589. dev_err(&pdev->dev, "failed to request keyboard IRQ\n");
  590. return err;
  591. }
  592. disable_irq(kbc->irq);
  593. err = input_register_device(kbc->idev);
  594. if (err) {
  595. dev_err(&pdev->dev, "failed to register input device\n");
  596. return err;
  597. }
  598. platform_set_drvdata(pdev, kbc);
  599. device_init_wakeup(&pdev->dev, kbc->wakeup);
  600. return 0;
  601. }
  602. #ifdef CONFIG_PM_SLEEP
  603. static void tegra_kbc_set_keypress_interrupt(struct tegra_kbc *kbc, bool enable)
  604. {
  605. u32 val;
  606. val = readl(kbc->mmio + KBC_CONTROL_0);
  607. if (enable)
  608. val |= KBC_CONTROL_KEYPRESS_INT_EN;
  609. else
  610. val &= ~KBC_CONTROL_KEYPRESS_INT_EN;
  611. writel(val, kbc->mmio + KBC_CONTROL_0);
  612. }
  613. static int tegra_kbc_suspend(struct device *dev)
  614. {
  615. struct platform_device *pdev = to_platform_device(dev);
  616. struct tegra_kbc *kbc = platform_get_drvdata(pdev);
  617. mutex_lock(&kbc->idev->mutex);
  618. if (device_may_wakeup(&pdev->dev)) {
  619. disable_irq(kbc->irq);
  620. del_timer_sync(&kbc->timer);
  621. tegra_kbc_set_fifo_interrupt(kbc, false);
  622. /* Forcefully clear the interrupt status */
  623. writel(0x7, kbc->mmio + KBC_INT_0);
  624. /*
  625. * Store the previous resident time of continuous polling mode.
  626. * Force the keyboard into interrupt mode.
  627. */
  628. kbc->cp_to_wkup_dly = readl(kbc->mmio + KBC_TO_CNT_0);
  629. writel(0, kbc->mmio + KBC_TO_CNT_0);
  630. tegra_kbc_setup_wakekeys(kbc, true);
  631. msleep(30);
  632. kbc->keypress_caused_wake = false;
  633. /* Enable keypress interrupt before going into suspend. */
  634. tegra_kbc_set_keypress_interrupt(kbc, true);
  635. enable_irq(kbc->irq);
  636. enable_irq_wake(kbc->irq);
  637. } else {
  638. if (kbc->idev->users)
  639. tegra_kbc_stop(kbc);
  640. }
  641. mutex_unlock(&kbc->idev->mutex);
  642. return 0;
  643. }
  644. static int tegra_kbc_resume(struct device *dev)
  645. {
  646. struct platform_device *pdev = to_platform_device(dev);
  647. struct tegra_kbc *kbc = platform_get_drvdata(pdev);
  648. int err = 0;
  649. mutex_lock(&kbc->idev->mutex);
  650. if (device_may_wakeup(&pdev->dev)) {
  651. disable_irq_wake(kbc->irq);
  652. tegra_kbc_setup_wakekeys(kbc, false);
  653. /* We will use fifo interrupts for key detection. */
  654. tegra_kbc_set_keypress_interrupt(kbc, false);
  655. /* Restore the resident time of continuous polling mode. */
  656. writel(kbc->cp_to_wkup_dly, kbc->mmio + KBC_TO_CNT_0);
  657. tegra_kbc_set_fifo_interrupt(kbc, true);
  658. if (kbc->keypress_caused_wake && kbc->wakeup_key) {
  659. /*
  660. * We can't report events directly from the ISR
  661. * because timekeeping is stopped when processing
  662. * wakeup request and we get a nasty warning when
  663. * we try to call do_gettimeofday() in evdev
  664. * handler.
  665. */
  666. input_report_key(kbc->idev, kbc->wakeup_key, 1);
  667. input_sync(kbc->idev);
  668. input_report_key(kbc->idev, kbc->wakeup_key, 0);
  669. input_sync(kbc->idev);
  670. }
  671. } else {
  672. if (kbc->idev->users)
  673. err = tegra_kbc_start(kbc);
  674. }
  675. mutex_unlock(&kbc->idev->mutex);
  676. return err;
  677. }
  678. #endif
  679. static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops, tegra_kbc_suspend, tegra_kbc_resume);
  680. static struct platform_driver tegra_kbc_driver = {
  681. .probe = tegra_kbc_probe,
  682. .driver = {
  683. .name = "tegra-kbc",
  684. .pm = &tegra_kbc_pm_ops,
  685. .of_match_table = tegra_kbc_of_match,
  686. },
  687. };
  688. module_platform_driver(tegra_kbc_driver);
  689. MODULE_LICENSE("GPL");
  690. MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
  691. MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
  692. MODULE_ALIAS("platform:tegra-kbc");