tegra-kbc.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Keyboard class input driver for the NVIDIA Tegra SoC internal matrix
 * keyboard controller
 *
 * Copyright (c) 2009-2011, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

22
#include <linux/kernel.h>
23 24 25 26 27 28
#include <linux/module.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/interrupt.h>
29
#include <linux/of.h>
30 31
#include <linux/clk.h>
#include <linux/slab.h>
32
#include <linux/input/matrix_keypad.h>
33
#include <linux/clk/tegra.h>
34

35 36 37 38 39 40 41
#define KBC_MAX_GPIO	24
#define KBC_MAX_KPENT	8

#define KBC_MAX_ROW	16
#define KBC_MAX_COL	8
#define KBC_MAX_KEY	(KBC_MAX_ROW * KBC_MAX_COL)

42 43 44 45 46 47 48
#define KBC_MAX_DEBOUNCE_CNT	0x3ffu

/* KBC row scan time and delay for beginning the row scan. */
#define KBC_ROW_SCAN_TIME	16
#define KBC_ROW_SCAN_DLY	5

/* KBC uses a 32KHz clock so a cycle = 1/32Khz */
49
#define KBC_CYCLE_MS	32
50 51 52 53 54 55 56 57

/* KBC Registers */

/* KBC Control Register */
#define KBC_CONTROL_0	0x0
#define KBC_FIFO_TH_CNT_SHIFT(cnt)	(cnt << 14)
#define KBC_DEBOUNCE_CNT_SHIFT(cnt)	(cnt << 4)
#define KBC_CONTROL_FIFO_CNT_INT_EN	(1 << 3)
58
#define KBC_CONTROL_KEYPRESS_INT_EN	(1 << 1)
59 60 61 62 63
#define KBC_CONTROL_KBC_EN		(1 << 0)

/* KBC Interrupt Register */
#define KBC_INT_0	0x4
#define KBC_INT_FIFO_CNT_INT_STATUS	(1 << 2)
64
#define KBC_INT_KEYPRESS_INT_STATUS	(1 << 0)
65 66 67

#define KBC_ROW_CFG0_0	0x8
#define KBC_COL_CFG0_0	0x18
68
#define KBC_TO_CNT_0	0x24
69 70 71 72 73 74 75 76
#define KBC_INIT_DLY_0	0x28
#define KBC_RPT_DLY_0	0x2c
#define KBC_KP_ENT0_0	0x30
#define KBC_KP_ENT1_0	0x34
#define KBC_ROW0_MASK_0	0x38

#define KBC_ROW_SHIFT	3

77 78 79 80 81 82 83 84 85 86 87
enum tegra_pin_type {
	PIN_CFG_IGNORE,
	PIN_CFG_COL,
	PIN_CFG_ROW,
};

struct tegra_kbc_pin_cfg {
	enum tegra_pin_type type;
	unsigned char num;
};

88
struct tegra_kbc {
89 90 91 92 93 94
	struct device *dev;
	unsigned int debounce_cnt;
	unsigned int repeat_cnt;
	struct tegra_kbc_pin_cfg pin_cfg[KBC_MAX_GPIO];
	const struct matrix_keymap_data *keymap_data;
	bool wakeup;
95 96
	void __iomem *mmio;
	struct input_dev *idev;
97
	int irq;
98 99 100
	spinlock_t lock;
	unsigned int repoll_dly;
	unsigned long cp_dly_jiffies;
101
	unsigned int cp_to_wkup_dly;
102
	bool use_fn_map;
103
	bool use_ghost_filter;
104
	bool keypress_caused_wake;
105
	unsigned short keycode[KBC_MAX_KEY * 2];
106 107
	unsigned short current_keys[KBC_MAX_KPENT];
	unsigned int num_pressed_keys;
108
	u32 wakeup_key;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	struct timer_list timer;
	struct clk *clk;
};

static void tegra_kbc_report_released_keys(struct input_dev *input,
					   unsigned short old_keycodes[],
					   unsigned int old_num_keys,
					   unsigned short new_keycodes[],
					   unsigned int new_num_keys)
{
	unsigned int i, j;

	for (i = 0; i < old_num_keys; i++) {
		for (j = 0; j < new_num_keys; j++)
			if (old_keycodes[i] == new_keycodes[j])
				break;

		if (j == new_num_keys)
			input_report_key(input, old_keycodes[i], 0);
	}
}

static void tegra_kbc_report_pressed_keys(struct input_dev *input,
					  unsigned char scancodes[],
					  unsigned short keycodes[],
					  unsigned int num_pressed_keys)
{
	unsigned int i;

	for (i = 0; i < num_pressed_keys; i++) {
		input_event(input, EV_MSC, MSC_SCAN, scancodes[i]);
		input_report_key(input, keycodes[i], 1);
	}
}

static void tegra_kbc_report_keys(struct tegra_kbc *kbc)
{
	unsigned char scancodes[KBC_MAX_KPENT];
	unsigned short keycodes[KBC_MAX_KPENT];
	u32 val = 0;
	unsigned int i;
	unsigned int num_down = 0;
151
	bool fn_keypress = false;
152 153
	bool key_in_same_row = false;
	bool key_in_same_col = false;
154 155 156 157 158 159 160 161 162 163 164 165

	for (i = 0; i < KBC_MAX_KPENT; i++) {
		if ((i % 4) == 0)
			val = readl(kbc->mmio + KBC_KP_ENT0_0 + i);

		if (val & 0x80) {
			unsigned int col = val & 0x07;
			unsigned int row = (val >> 3) & 0x0f;
			unsigned char scancode =
				MATRIX_SCAN_CODE(row, col, KBC_ROW_SHIFT);

			scancodes[num_down] = scancode;
166 167 168 169 170 171
			keycodes[num_down] = kbc->keycode[scancode];
			/* If driver uses Fn map, do not report the Fn key. */
			if ((keycodes[num_down] == KEY_FN) && kbc->use_fn_map)
				fn_keypress = true;
			else
				num_down++;
172 173 174 175
		}

		val >>= 8;
	}
176

177 178 179 180 181 182
	/*
	 * Matrix keyboard designs are prone to keyboard ghosting.
	 * Ghosting occurs if there are 3 keys such that -
	 * any 2 of the 3 keys share a row, and any 2 of them share a column.
	 * If so ignore the key presses for this iteration.
	 */
183
	if (kbc->use_ghost_filter && num_down >= 3) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		for (i = 0; i < num_down; i++) {
			unsigned int j;
			u8 curr_col = scancodes[i] & 0x07;
			u8 curr_row = scancodes[i] >> KBC_ROW_SHIFT;

			/*
			 * Find 2 keys such that one key is in the same row
			 * and the other is in the same column as the i-th key.
			 */
			for (j = i + 1; j < num_down; j++) {
				u8 col = scancodes[j] & 0x07;
				u8 row = scancodes[j] >> KBC_ROW_SHIFT;

				if (col == curr_col)
					key_in_same_col = true;
				if (row == curr_row)
					key_in_same_row = true;
			}
		}
	}

205 206 207 208 209 210 211 212 213 214 215
	/*
	 * If the platform uses Fn keymaps, translate keys on a Fn keypress.
	 * Function keycodes are KBC_MAX_KEY apart from the plain keycodes.
	 */
	if (fn_keypress) {
		for (i = 0; i < num_down; i++) {
			scancodes[i] += KBC_MAX_KEY;
			keycodes[i] = kbc->keycode[scancodes[i]];
		}
	}

216 217 218 219
	/* Ignore the key presses for this iteration? */
	if (key_in_same_col && key_in_same_row)
		return;

220 221 222 223 224 225 226 227 228 229
	tegra_kbc_report_released_keys(kbc->idev,
				       kbc->current_keys, kbc->num_pressed_keys,
				       keycodes, num_down);
	tegra_kbc_report_pressed_keys(kbc->idev, scancodes, keycodes, num_down);
	input_sync(kbc->idev);

	memcpy(kbc->current_keys, keycodes, sizeof(kbc->current_keys));
	kbc->num_pressed_keys = num_down;
}

230 231 232 233 234 235 236 237 238 239 240 241
static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
{
	u32 val;

	val = readl(kbc->mmio + KBC_CONTROL_0);
	if (enable)
		val |= KBC_CONTROL_FIFO_CNT_INT_EN;
	else
		val &= ~KBC_CONTROL_FIFO_CNT_INT_EN;
	writel(val, kbc->mmio + KBC_CONTROL_0);
}

242 243 244 245 246 247 248
static void tegra_kbc_keypress_timer(unsigned long data)
{
	struct tegra_kbc *kbc = (struct tegra_kbc *)data;
	unsigned long flags;
	u32 val;
	unsigned int i;

249 250
	spin_lock_irqsave(&kbc->lock, flags);

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
	if (val) {
		unsigned long dly;

		tegra_kbc_report_keys(kbc);

		/*
		 * If more than one keys are pressed we need not wait
		 * for the repoll delay.
		 */
		dly = (val == 1) ? kbc->repoll_dly : 1;
		mod_timer(&kbc->timer, jiffies + msecs_to_jiffies(dly));
	} else {
		/* Release any pressed keys and exit the polling loop */
		for (i = 0; i < kbc->num_pressed_keys; i++)
			input_report_key(kbc->idev, kbc->current_keys[i], 0);
		input_sync(kbc->idev);

		kbc->num_pressed_keys = 0;

		/* All keys are released so enable the keypress interrupt */
272
		tegra_kbc_set_fifo_interrupt(kbc, true);
273
	}
274 275

	spin_unlock_irqrestore(&kbc->lock, flags);
276 277 278 279 280
}

static irqreturn_t tegra_kbc_isr(int irq, void *args)
{
	struct tegra_kbc *kbc = args;
281
	unsigned long flags;
282
	u32 val;
283

284
	spin_lock_irqsave(&kbc->lock, flags);
285 286 287 288 289 290 291 292 293 294

	/*
	 * Quickly bail out & reenable interrupts if the fifo threshold
	 * count interrupt wasn't the interrupt source
	 */
	val = readl(kbc->mmio + KBC_INT_0);
	writel(val, kbc->mmio + KBC_INT_0);

	if (val & KBC_INT_FIFO_CNT_INT_STATUS) {
		/*
295 296
		 * Until all keys are released, defer further processing to
		 * the polling loop in tegra_kbc_keypress_timer.
297
		 */
298
		tegra_kbc_set_fifo_interrupt(kbc, false);
299
		mod_timer(&kbc->timer, jiffies + kbc->cp_dly_jiffies);
300 301 302
	} else if (val & KBC_INT_KEYPRESS_INT_STATUS) {
		/* We can be here only through system resume path */
		kbc->keypress_caused_wake = true;
303 304
	}

305 306
	spin_unlock_irqrestore(&kbc->lock, flags);

307 308 309 310 311 312 313 314
	return IRQ_HANDLED;
}

static void tegra_kbc_setup_wakekeys(struct tegra_kbc *kbc, bool filter)
{
	int i;
	unsigned int rst_val;

315
	/* Either mask all keys or none. */
316
	rst_val = (filter && !kbc->wakeup) ? ~0 : 0;
317 318 319 320 321 322 323 324 325 326 327 328

	for (i = 0; i < KBC_MAX_ROW; i++)
		writel(rst_val, kbc->mmio + KBC_ROW0_MASK_0 + i * 4);
}

static void tegra_kbc_config_pins(struct tegra_kbc *kbc)
{
	int i;

	for (i = 0; i < KBC_MAX_GPIO; i++) {
		u32 r_shft = 5 * (i % 6);
		u32 c_shft = 4 * (i % 8);
R
Rakesh Iyer 已提交
329 330
		u32 r_mask = 0x1f << r_shft;
		u32 c_mask = 0x0f << c_shft;
331 332 333 334 335 336 337 338
		u32 r_offs = (i / 6) * 4 + KBC_ROW_CFG0_0;
		u32 c_offs = (i / 8) * 4 + KBC_COL_CFG0_0;
		u32 row_cfg = readl(kbc->mmio + r_offs);
		u32 col_cfg = readl(kbc->mmio + c_offs);

		row_cfg &= ~r_mask;
		col_cfg &= ~c_mask;

339
		switch (kbc->pin_cfg[i].type) {
340
		case PIN_CFG_ROW:
341
			row_cfg |= ((kbc->pin_cfg[i].num << 1) | 1) << r_shft;
342 343 344
			break;

		case PIN_CFG_COL:
345
			col_cfg |= ((kbc->pin_cfg[i].num << 1) | 1) << c_shft;
346 347 348 349 350
			break;

		case PIN_CFG_IGNORE:
			break;
		}
351 352 353 354 355 356 357 358 359 360 361

		writel(row_cfg, kbc->mmio + r_offs);
		writel(col_cfg, kbc->mmio + c_offs);
	}
}

static int tegra_kbc_start(struct tegra_kbc *kbc)
{
	unsigned int debounce_cnt;
	u32 val = 0;

362
	clk_prepare_enable(kbc->clk);
363 364 365 366 367 368 369 370 371 372

	/* Reset the KBC controller to clear all previous status.*/
	tegra_periph_reset_assert(kbc->clk);
	udelay(100);
	tegra_periph_reset_deassert(kbc->clk);
	udelay(100);

	tegra_kbc_config_pins(kbc);
	tegra_kbc_setup_wakekeys(kbc, false);

373
	writel(kbc->repeat_cnt, kbc->mmio + KBC_RPT_DLY_0);
374 375

	/* Keyboard debounce count is maximum of 12 bits. */
376
	debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	val = KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt);
	val |= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
	val |= KBC_CONTROL_FIFO_CNT_INT_EN;  /* interrupt on FIFO threshold */
	val |= KBC_CONTROL_KBC_EN;     /* enable */
	writel(val, kbc->mmio + KBC_CONTROL_0);

	/*
	 * Compute the delay(ns) from interrupt mode to continuous polling
	 * mode so the timer routine is scheduled appropriately.
	 */
	val = readl(kbc->mmio + KBC_INIT_DLY_0);
	kbc->cp_dly_jiffies = usecs_to_jiffies((val & 0xfffff) * 32);

	kbc->num_pressed_keys = 0;

	/*
	 * Atomically clear out any remaining entries in the key FIFO
	 * and enable keyboard interrupts.
	 */
	while (1) {
		val = readl(kbc->mmio + KBC_INT_0);
		val >>= 4;
		if (!val)
			break;

		val = readl(kbc->mmio + KBC_KP_ENT0_0);
		val = readl(kbc->mmio + KBC_KP_ENT1_0);
	}
	writel(0x7, kbc->mmio + KBC_INT_0);

	enable_irq(kbc->irq);

	return 0;
}

static void tegra_kbc_stop(struct tegra_kbc *kbc)
{
	unsigned long flags;
	u32 val;

	spin_lock_irqsave(&kbc->lock, flags);
	val = readl(kbc->mmio + KBC_CONTROL_0);
	val &= ~1;
	writel(val, kbc->mmio + KBC_CONTROL_0);
	spin_unlock_irqrestore(&kbc->lock, flags);

	disable_irq(kbc->irq);
	del_timer_sync(&kbc->timer);

426
	clk_disable_unprepare(kbc->clk);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
}

static int tegra_kbc_open(struct input_dev *dev)
{
	struct tegra_kbc *kbc = input_get_drvdata(dev);

	return tegra_kbc_start(kbc);
}

static void tegra_kbc_close(struct input_dev *dev)
{
	struct tegra_kbc *kbc = input_get_drvdata(dev);

	return tegra_kbc_stop(kbc);
}

443 444
static bool tegra_kbc_check_pin_cfg(const struct tegra_kbc *kbc,
					unsigned int *num_rows)
445 446 447 448 449 450
{
	int i;

	*num_rows = 0;

	for (i = 0; i < KBC_MAX_GPIO; i++) {
451
		const struct tegra_kbc_pin_cfg *pin_cfg = &kbc->pin_cfg[i];
452

453 454
		switch (pin_cfg->type) {
		case PIN_CFG_ROW:
455
			if (pin_cfg->num >= KBC_MAX_ROW) {
456
				dev_err(kbc->dev,
457 458 459 460 461
					"pin_cfg[%d]: invalid row number %d\n",
					i, pin_cfg->num);
				return false;
			}
			(*num_rows)++;
462 463 464
			break;

		case PIN_CFG_COL:
465
			if (pin_cfg->num >= KBC_MAX_COL) {
466
				dev_err(kbc->dev,
467 468 469 470
					"pin_cfg[%d]: invalid column number %d\n",
					i, pin_cfg->num);
				return false;
			}
471 472 473 474 475 476
			break;

		case PIN_CFG_IGNORE:
			break;

		default:
477
			dev_err(kbc->dev,
478 479 480
				"pin_cfg[%d]: invalid entry type %d\n",
				pin_cfg->type, pin_cfg->num);
			return false;
481 482 483 484 485 486
		}
	}

	return true;
}

487
static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
488
{
489
	struct device_node *np = kbc->dev->of_node;
490 491
	u32 prop;
	int i;
492 493 494 495 496 497 498
	u32 num_rows = 0;
	u32 num_cols = 0;
	u32 cols_cfg[KBC_MAX_GPIO];
	u32 rows_cfg[KBC_MAX_GPIO];
	int proplen;
	int ret;

499
	if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
500
		kbc->debounce_cnt = prop;
501

502
	if (!of_property_read_u32(np, "nvidia,repeat-delay-ms", &prop))
503
		kbc->repeat_cnt = prop;
504

505
	if (of_find_property(np, "nvidia,needs-ghost-filter", NULL))
506
		kbc->use_ghost_filter = true;
507

508
	if (of_find_property(np, "nvidia,wakeup-source", NULL))
509
		kbc->wakeup = true;
510

511
	if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
512 513
		dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
		return -ENOENT;
514 515 516 517
	}
	num_rows = proplen / sizeof(u32);

	if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
518 519
		dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
		return -ENOENT;
520 521 522 523
	}
	num_cols = proplen / sizeof(u32);

	if (!of_get_property(np, "linux,keymap", &proplen)) {
524 525
		dev_err(kbc->dev, "property linux,keymap not found\n");
		return -ENOENT;
526 527
	}

528
	if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
529
		dev_err(kbc->dev,
530
			"keypad rows/columns not porperly specified\n");
531
		return -EINVAL;
532 533 534 535
	}

	/* Set all pins as non-configured */
	for (i = 0; i < KBC_MAX_GPIO; i++)
536
		kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
537 538 539 540

	ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
				rows_cfg, num_rows);
	if (ret < 0) {
541 542
		dev_err(kbc->dev, "Rows configurations are not proper\n");
		return -EINVAL;
543 544 545 546 547
	}

	ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
				cols_cfg, num_cols);
	if (ret < 0) {
548 549
		dev_err(kbc->dev, "Cols configurations are not proper\n");
		return -EINVAL;
550 551 552
	}

	for (i = 0; i < num_rows; i++) {
553 554
		kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
		kbc->pin_cfg[rows_cfg[i]].num = i;
555 556 557
	}

	for (i = 0; i < num_cols; i++) {
558 559
		kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
		kbc->pin_cfg[cols_cfg[i]].num = i;
560 561
	}

562
	return 0;
563 564
}

B
Bill Pemberton 已提交
565
static int tegra_kbc_probe(struct platform_device *pdev)
566 567 568 569 570 571 572
{
	struct tegra_kbc *kbc;
	struct resource *res;
	int err;
	int num_rows = 0;
	unsigned int debounce_cnt;
	unsigned int scan_time_rows;
573
	unsigned int keymap_rows = KBC_MAX_KEY;
574

575 576 577 578 579 580 581 582
	kbc = devm_kzalloc(&pdev->dev, sizeof(*kbc), GFP_KERNEL);
	if (!kbc) {
		dev_err(&pdev->dev, "failed to alloc memory for kbc\n");
		return -ENOMEM;
	}

	kbc->dev = &pdev->dev;
	spin_lock_init(&kbc->lock);
583

584 585 586
	err = tegra_kbc_parse_dt(kbc);
	if (err)
		return err;
587

588
	if (!tegra_kbc_check_pin_cfg(kbc, &num_rows))
589 590
		return -EINVAL;

591 592 593
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "failed to get I/O memory\n");
594
		return -ENXIO;
595 596
	}

597 598
	kbc->irq = platform_get_irq(pdev, 0);
	if (kbc->irq < 0) {
599
		dev_err(&pdev->dev, "failed to get keyboard IRQ\n");
600
		return -ENXIO;
601 602
	}

603 604
	kbc->idev = devm_input_allocate_device(&pdev->dev);
	if (!kbc->idev) {
605 606
		dev_err(&pdev->dev, "failed to allocate input device\n");
		return -ENOMEM;
607 608 609 610
	}

	setup_timer(&kbc->timer, tegra_kbc_keypress_timer, (unsigned long)kbc);

611
	kbc->mmio = devm_request_and_ioremap(&pdev->dev, res);
612
	if (!kbc->mmio) {
613 614
		dev_err(&pdev->dev, "Cannot request memregion/iomap address\n");
		return -EBUSY;
615 616
	}

617
	kbc->clk = devm_clk_get(&pdev->dev, NULL);
618 619
	if (IS_ERR(kbc->clk)) {
		dev_err(&pdev->dev, "failed to get keyboard clock\n");
620
		return PTR_ERR(kbc->clk);
621 622 623 624 625 626 627 628
	}

	/*
	 * The time delay between two consecutive reads of the FIFO is
	 * the sum of the repeat time and the time taken for scanning
	 * the rows. There is an additional delay before the row scanning
	 * starts. The repoll delay is computed in milliseconds.
	 */
629
	debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
630
	scan_time_rows = (KBC_ROW_SCAN_TIME + debounce_cnt) * num_rows;
631
	kbc->repoll_dly = KBC_ROW_SCAN_DLY + scan_time_rows + kbc->repeat_cnt;
632
	kbc->repoll_dly = DIV_ROUND_UP(kbc->repoll_dly, KBC_CYCLE_MS);
633

634 635 636 637 638
	kbc->idev->name = pdev->name;
	kbc->idev->id.bustype = BUS_HOST;
	kbc->idev->dev.parent = &pdev->dev;
	kbc->idev->open = tegra_kbc_open;
	kbc->idev->close = tegra_kbc_close;
639

640
	if (kbc->keymap_data && kbc->use_fn_map)
641 642
		keymap_rows *= 2;

643
	err = matrix_keypad_build_keymap(kbc->keymap_data, NULL,
644
					 keymap_rows, KBC_MAX_COL,
645
					 kbc->keycode, kbc->idev);
646
	if (err) {
647
		dev_err(&pdev->dev, "failed to setup keymap\n");
648
		return err;
649 650
	}

651 652
	__set_bit(EV_REP, kbc->idev->evbit);
	input_set_capability(kbc->idev, EV_MSC, MSC_SCAN);
653

654
	input_set_drvdata(kbc->idev, kbc);
655

656
	err = devm_request_irq(&pdev->dev, kbc->irq, tegra_kbc_isr,
657
			  IRQF_NO_SUSPEND | IRQF_TRIGGER_HIGH, pdev->name, kbc);
658 659
	if (err) {
		dev_err(&pdev->dev, "failed to request keyboard IRQ\n");
660
		return err;
661 662 663 664 665 666 667
	}

	disable_irq(kbc->irq);

	err = input_register_device(kbc->idev);
	if (err) {
		dev_err(&pdev->dev, "failed to register input device\n");
668
		return err;
669 670 671
	}

	platform_set_drvdata(pdev, kbc);
672
	device_init_wakeup(&pdev->dev, kbc->wakeup);
673 674 675 676 677

	return 0;
}

#ifdef CONFIG_PM_SLEEP
678 679 680 681 682 683 684 685 686 687 688 689
static void tegra_kbc_set_keypress_interrupt(struct tegra_kbc *kbc, bool enable)
{
	u32 val;

	val = readl(kbc->mmio + KBC_CONTROL_0);
	if (enable)
		val |= KBC_CONTROL_KEYPRESS_INT_EN;
	else
		val &= ~KBC_CONTROL_KEYPRESS_INT_EN;
	writel(val, kbc->mmio + KBC_CONTROL_0);
}

690 691 692 693 694
static int tegra_kbc_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct tegra_kbc *kbc = platform_get_drvdata(pdev);

695
	mutex_lock(&kbc->idev->mutex);
696
	if (device_may_wakeup(&pdev->dev)) {
697 698 699 700
		disable_irq(kbc->irq);
		del_timer_sync(&kbc->timer);
		tegra_kbc_set_fifo_interrupt(kbc, false);

701 702
		/* Forcefully clear the interrupt status */
		writel(0x7, kbc->mmio + KBC_INT_0);
703 704 705 706 707 708 709 710
		/*
		 * Store the previous resident time of continuous polling mode.
		 * Force the keyboard into interrupt mode.
		 */
		kbc->cp_to_wkup_dly = readl(kbc->mmio + KBC_TO_CNT_0);
		writel(0, kbc->mmio + KBC_TO_CNT_0);

		tegra_kbc_setup_wakekeys(kbc, true);
711
		msleep(30);
712

713
		kbc->keypress_caused_wake = false;
714 715
		/* Enable keypress interrupt before going into suspend. */
		tegra_kbc_set_keypress_interrupt(kbc, true);
716
		enable_irq(kbc->irq);
717
		enable_irq_wake(kbc->irq);
718 719 720 721
	} else {
		if (kbc->idev->users)
			tegra_kbc_stop(kbc);
	}
722
	mutex_unlock(&kbc->idev->mutex);
723 724 725 726 727 728 729 730 731 732

	return 0;
}

static int tegra_kbc_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct tegra_kbc *kbc = platform_get_drvdata(pdev);
	int err = 0;

733
	mutex_lock(&kbc->idev->mutex);
734 735 736
	if (device_may_wakeup(&pdev->dev)) {
		disable_irq_wake(kbc->irq);
		tegra_kbc_setup_wakekeys(kbc, false);
737 738
		/* We will use fifo interrupts for key detection. */
		tegra_kbc_set_keypress_interrupt(kbc, false);
739 740 741 742 743 744

		/* Restore the resident time of continuous polling mode. */
		writel(kbc->cp_to_wkup_dly, kbc->mmio + KBC_TO_CNT_0);

		tegra_kbc_set_fifo_interrupt(kbc, true);

745 746 747 748 749 750 751 752 753 754 755 756 757
		if (kbc->keypress_caused_wake && kbc->wakeup_key) {
			/*
			 * We can't report events directly from the ISR
			 * because timekeeping is stopped when processing
			 * wakeup request and we get a nasty warning when
			 * we try to call do_gettimeofday() in evdev
			 * handler.
			 */
			input_report_key(kbc->idev, kbc->wakeup_key, 1);
			input_sync(kbc->idev);
			input_report_key(kbc->idev, kbc->wakeup_key, 0);
			input_sync(kbc->idev);
		}
758 759 760 761
	} else {
		if (kbc->idev->users)
			err = tegra_kbc_start(kbc);
	}
762
	mutex_unlock(&kbc->idev->mutex);
763 764 765 766 767 768 769

	return err;
}
#endif

static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops, tegra_kbc_suspend, tegra_kbc_resume);

770 771 772 773 774 775
static const struct of_device_id tegra_kbc_of_match[] = {
	{ .compatible = "nvidia,tegra20-kbc", },
	{ },
};
MODULE_DEVICE_TABLE(of, tegra_kbc_of_match);

776 777 778 779 780 781
static struct platform_driver tegra_kbc_driver = {
	.probe		= tegra_kbc_probe,
	.driver	= {
		.name	= "tegra-kbc",
		.owner  = THIS_MODULE,
		.pm	= &tegra_kbc_pm_ops,
782
		.of_match_table = tegra_kbc_of_match,
783 784
	},
};
785
module_platform_driver(tegra_kbc_driver);
786 787 788 789 790

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
MODULE_ALIAS("platform:tegra-kbc");