tegra-kbc.c 21 KB

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