1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Manage Keyboard Matrices
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2012 The Chromium OS Authors.
5*4882a593Smuzhiyun * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <dm.h>
12*4882a593Smuzhiyun #include <key_matrix.h>
13*4882a593Smuzhiyun #include <malloc.h>
14*4882a593Smuzhiyun #include <linux/input.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /**
17*4882a593Smuzhiyun * Determine if the current keypress configuration can cause key ghosting
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * We figure this out by seeing if we have two or more keys in the same
20*4882a593Smuzhiyun * column, as well as two or more keys in the same row.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * @param config Keyboard matrix config
23*4882a593Smuzhiyun * @param keys List of keys to check
24*4882a593Smuzhiyun * @param valid Number of valid keypresses to check
25*4882a593Smuzhiyun * @return 0 if no ghosting is possible, 1 if it is
26*4882a593Smuzhiyun */
has_ghosting(struct key_matrix * config,struct key_matrix_key * keys,int valid)27*4882a593Smuzhiyun static int has_ghosting(struct key_matrix *config, struct key_matrix_key *keys,
28*4882a593Smuzhiyun int valid)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun int key_in_same_col = 0, key_in_same_row = 0;
31*4882a593Smuzhiyun int i, j;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun if (!config->ghost_filter || valid < 3)
34*4882a593Smuzhiyun return 0;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun for (i = 0; i < valid; i++) {
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * Find 2 keys such that one key is in the same row
39*4882a593Smuzhiyun * and the other is in the same column as the i-th key.
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun for (j = i + 1; j < valid; j++) {
42*4882a593Smuzhiyun if (keys[j].col == keys[i].col)
43*4882a593Smuzhiyun key_in_same_col = 1;
44*4882a593Smuzhiyun if (keys[j].row == keys[i].row)
45*4882a593Smuzhiyun key_in_same_row = 1;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (key_in_same_col && key_in_same_row)
50*4882a593Smuzhiyun return 1;
51*4882a593Smuzhiyun else
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
key_matrix_decode(struct key_matrix * config,struct key_matrix_key keys[],int num_keys,int keycode[],int max_keycodes)55*4882a593Smuzhiyun int key_matrix_decode(struct key_matrix *config, struct key_matrix_key keys[],
56*4882a593Smuzhiyun int num_keys, int keycode[], int max_keycodes)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun const u8 *keymap;
59*4882a593Smuzhiyun int valid, upto;
60*4882a593Smuzhiyun int pos;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun debug("%s: num_keys = %d\n", __func__, num_keys);
63*4882a593Smuzhiyun keymap = config->plain_keycode;
64*4882a593Smuzhiyun for (valid = upto = 0; upto < num_keys; upto++) {
65*4882a593Smuzhiyun struct key_matrix_key *key = &keys[upto];
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun debug(" valid=%d, row=%d, col=%d\n", key->valid, key->row,
68*4882a593Smuzhiyun key->col);
69*4882a593Smuzhiyun if (!key->valid)
70*4882a593Smuzhiyun continue;
71*4882a593Smuzhiyun pos = key->row * config->num_cols + key->col;
72*4882a593Smuzhiyun if (config->fn_keycode && pos == config->fn_pos)
73*4882a593Smuzhiyun keymap = config->fn_keycode;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Convert the (row, col) values into a keycode */
76*4882a593Smuzhiyun if (valid < max_keycodes)
77*4882a593Smuzhiyun keycode[valid++] = keymap[pos];
78*4882a593Smuzhiyun debug(" keycode=%d\n", keymap[pos]);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* For a ghost key config, ignore the keypresses for this iteration. */
82*4882a593Smuzhiyun if (has_ghosting(config, keys, valid)) {
83*4882a593Smuzhiyun valid = 0;
84*4882a593Smuzhiyun debug(" ghosting detected!\n");
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun debug(" %d valid keycodes found\n", valid);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return valid;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun * Create a new keycode map from some provided data
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * This decodes a keycode map in the format used by the fdt, which is one
95*4882a593Smuzhiyun * word per entry, with the row, col and keycode encoded in that word.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * We create a (row x col) size byte array with each entry containing the
98*4882a593Smuzhiyun * keycode for that (row, col). We also search for map_keycode and return
99*4882a593Smuzhiyun * its position if found (this is used for finding the Fn key).
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * @param config Key matrix dimensions structure
102*4882a593Smuzhiyun * @param data Keycode data
103*4882a593Smuzhiyun * @param len Number of entries in keycode table
104*4882a593Smuzhiyun * @param map_keycode Key code to find in the map
105*4882a593Smuzhiyun * @param pos Returns position of map_keycode, if found, else -1
106*4882a593Smuzhiyun * @return map Pointer to allocated map
107*4882a593Smuzhiyun */
create_keymap(struct key_matrix * config,const u32 * data,int len,int map_keycode,int * pos)108*4882a593Smuzhiyun static uchar *create_keymap(struct key_matrix *config, const u32 *data, int len,
109*4882a593Smuzhiyun int map_keycode, int *pos)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun uchar *map;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (pos)
114*4882a593Smuzhiyun *pos = -1;
115*4882a593Smuzhiyun map = (uchar *)calloc(1, config->key_count);
116*4882a593Smuzhiyun if (!map) {
117*4882a593Smuzhiyun debug("%s: failed to malloc %d bytes\n", __func__,
118*4882a593Smuzhiyun config->key_count);
119*4882a593Smuzhiyun return NULL;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun for (; len >= sizeof(u32); data++, len -= 4) {
123*4882a593Smuzhiyun u32 tmp = fdt32_to_cpu(*data);
124*4882a593Smuzhiyun int key_code, row, col;
125*4882a593Smuzhiyun int entry;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun row = (tmp >> 24) & 0xff;
128*4882a593Smuzhiyun col = (tmp >> 16) & 0xff;
129*4882a593Smuzhiyun key_code = tmp & 0xffff;
130*4882a593Smuzhiyun entry = row * config->num_cols + col;
131*4882a593Smuzhiyun map[entry] = key_code;
132*4882a593Smuzhiyun debug(" map %d, %d: pos=%d, keycode=%d\n", row, col,
133*4882a593Smuzhiyun entry, key_code);
134*4882a593Smuzhiyun if (pos && map_keycode == key_code)
135*4882a593Smuzhiyun *pos = entry;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return map;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
key_matrix_decode_fdt(struct udevice * dev,struct key_matrix * config)141*4882a593Smuzhiyun int key_matrix_decode_fdt(struct udevice *dev, struct key_matrix *config)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun const u32 *prop;
144*4882a593Smuzhiyun int proplen;
145*4882a593Smuzhiyun uchar *plain_keycode;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun prop = dev_read_prop(dev, "linux,keymap", &proplen);
148*4882a593Smuzhiyun /* Basic keymap is required */
149*4882a593Smuzhiyun if (!prop) {
150*4882a593Smuzhiyun debug("%s: cannot find keycode-plain map\n", __func__);
151*4882a593Smuzhiyun return -1;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun plain_keycode = create_keymap(config, prop, proplen, KEY_FN,
155*4882a593Smuzhiyun &config->fn_pos);
156*4882a593Smuzhiyun config->plain_keycode = plain_keycode;
157*4882a593Smuzhiyun /* Conversion error -> fail */
158*4882a593Smuzhiyun if (!config->plain_keycode)
159*4882a593Smuzhiyun return -1;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun prop = dev_read_prop(dev, "linux,fn-keymap", &proplen);
162*4882a593Smuzhiyun /* fn keymap is optional */
163*4882a593Smuzhiyun if (!prop)
164*4882a593Smuzhiyun goto done;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun config->fn_keycode = create_keymap(config, prop, proplen, -1, NULL);
167*4882a593Smuzhiyun /* Conversion error -> fail */
168*4882a593Smuzhiyun if (!config->fn_keycode) {
169*4882a593Smuzhiyun free(plain_keycode);
170*4882a593Smuzhiyun return -1;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun done:
174*4882a593Smuzhiyun debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
175*4882a593Smuzhiyun config->plain_keycode, config->fn_keycode);
176*4882a593Smuzhiyun return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
key_matrix_init(struct key_matrix * config,int rows,int cols,int ghost_filter)179*4882a593Smuzhiyun int key_matrix_init(struct key_matrix *config, int rows, int cols,
180*4882a593Smuzhiyun int ghost_filter)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun memset(config, '\0', sizeof(*config));
183*4882a593Smuzhiyun config->num_rows = rows;
184*4882a593Smuzhiyun config->num_cols = cols;
185*4882a593Smuzhiyun config->key_count = rows * cols;
186*4882a593Smuzhiyun config->ghost_filter = ghost_filter;
187*4882a593Smuzhiyun assert(config->key_count > 0);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun return 0;
190*4882a593Smuzhiyun }
191