summaryrefslogtreecommitdiff
path: root/chip/lm4/keyboard_scan_stub.c
blob: 01d1aca8e2a0c13904b0f1c146da3fb7b0a69b00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Functions needed by keyboard scanner module for Chrome EC */

#include "board.h"
#include "keyboard_scan.h"
#include "keyboard_scan_stub.h"
#include "registers.h"


/* Notes:
 *
 * Link proto0 board:
 *
 *   Columns (outputs):
 *      KSO0 - KSO7  = PP0:7
 *      KSO8 - KSO12 = PQ0:4
 *
 *   Rows (inputs):
 *      KSI0 - KSI7  = PN0:7
 *
 *   Other:
 *      PWR_BTN#     = PK7 (handled by gpio module)
 */


static int enable_scanning = 1;  /* Must init to 1 for scanning at boot */


void lm4_set_scanning_enabled(int enabled)
{
	enable_scanning = enabled;
}


int lm4_get_scanning_enabled(void)
{
	return enable_scanning;
}


void lm4_select_column(int col)
{
	if (col == COLUMN_TRI_STATE_ALL || !enable_scanning) {
		/* Tri-state all outputs */
		LM4_GPIO_DATA(LM4_GPIO_P, 0xff) = 0xff;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0x1f) = 0x1f;
	} else if (col == COLUMN_ASSERT_ALL) {
		/* Assert all outputs */
		LM4_GPIO_DATA(LM4_GPIO_P, 0xff) = 0;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0x1f) = 0;
	} else {
		/* Assert a single output */
		LM4_GPIO_DATA(LM4_GPIO_P, 0xff) = 0xff;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0x1f) = 0x1f;
		if (col < 8)
			LM4_GPIO_DATA(LM4_GPIO_P, 1 << col) = 0;
		else
			LM4_GPIO_DATA(LM4_GPIO_Q, 1 << (col - 8)) = 0;
	}
}


uint32_t lm4_clear_matrix_interrupt_status(void)
{
	uint32_t ris = LM4_GPIO_RIS(KB_SCAN_ROW_GPIO);
	LM4_GPIO_ICR(KB_SCAN_ROW_GPIO) = ris;

	return ris;
}


void lm4_enable_matrix_interrupt(void)
{
	LM4_GPIO_IM(KB_SCAN_ROW_GPIO) = 0xff;   /* 1: enable interrupt */
}


void lm4_disable_matrix_interrupt(void)
{
	LM4_GPIO_IM(KB_SCAN_ROW_GPIO) = 0;      /* 0: disable interrupt */
}


int lm4_read_raw_row_state(void)
{
	return LM4_GPIO_DATA(KB_SCAN_ROW_GPIO, 0xff);
}


void lm4_configure_keyboard_gpio(void)
{
	/* Set column outputs as open-drain; we either pull them low or let
	 * them float high. */
	LM4_GPIO_AFSEL(LM4_GPIO_P) = 0;  /* KSO[7:0] */
	LM4_GPIO_AFSEL(LM4_GPIO_Q) &= ~0x1f;  /* KSO[12:8] */
	LM4_GPIO_DEN(LM4_GPIO_P) = 0xff;
	LM4_GPIO_DEN(LM4_GPIO_Q) |= 0x1f;
	LM4_GPIO_DIR(LM4_GPIO_P) = 0xff;
	LM4_GPIO_DIR(LM4_GPIO_Q) |= 0x1f;
	LM4_GPIO_ODR(LM4_GPIO_P) = 0xff;
	LM4_GPIO_ODR(LM4_GPIO_Q) |= 0x1f;

	/* Set row inputs with pull-up */
	LM4_GPIO_AFSEL(KB_SCAN_ROW_GPIO) &= 0xff;
	LM4_GPIO_DEN(KB_SCAN_ROW_GPIO) |= 0xff;
	LM4_GPIO_DIR(KB_SCAN_ROW_GPIO) = 0;
	LM4_GPIO_PUR(KB_SCAN_ROW_GPIO) = 0xff;
	/* Edge-sensitive on both edges.  Don't enable interrupts yet. */
	LM4_GPIO_IS(KB_SCAN_ROW_GPIO) = 0;
	LM4_GPIO_IBE(KB_SCAN_ROW_GPIO) = 0xff;
}