tegra-kbc.c 20 KB

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