xref: /openbmc/u-boot/drivers/input/key_matrix.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
292c27c51SBernie Thompson /*
392c27c51SBernie Thompson  * Manage Keyboard Matrices
492c27c51SBernie Thompson  *
592c27c51SBernie Thompson  * Copyright (c) 2012 The Chromium OS Authors.
692c27c51SBernie Thompson  * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
792c27c51SBernie Thompson  */
892c27c51SBernie Thompson 
9c20dbf64SStephan Linz #include <common.h>
108327d41bSSimon Glass #include <dm.h>
1192c27c51SBernie Thompson #include <key_matrix.h>
1292c27c51SBernie Thompson #include <malloc.h>
1392c27c51SBernie Thompson #include <linux/input.h>
1492c27c51SBernie Thompson 
1592c27c51SBernie Thompson /**
1692c27c51SBernie Thompson  * Determine if the current keypress configuration can cause key ghosting
1792c27c51SBernie Thompson  *
1892c27c51SBernie Thompson  * We figure this out by seeing if we have two or more keys in the same
1992c27c51SBernie Thompson  * column, as well as two or more keys in the same row.
2092c27c51SBernie Thompson  *
2192c27c51SBernie Thompson  * @param config	Keyboard matrix config
2292c27c51SBernie Thompson  * @param keys		List of keys to check
2392c27c51SBernie Thompson  * @param valid		Number of valid keypresses to check
2492c27c51SBernie Thompson  * @return 0 if no ghosting is possible, 1 if it is
2592c27c51SBernie Thompson  */
has_ghosting(struct key_matrix * config,struct key_matrix_key * keys,int valid)2692c27c51SBernie Thompson static int has_ghosting(struct key_matrix *config, struct key_matrix_key *keys,
2792c27c51SBernie Thompson 			int valid)
2892c27c51SBernie Thompson {
2992c27c51SBernie Thompson 	int key_in_same_col = 0, key_in_same_row = 0;
3092c27c51SBernie Thompson 	int i, j;
3192c27c51SBernie Thompson 
3271dc6bcaSSimon Glass 	if (!config->ghost_filter || valid < 3)
3371dc6bcaSSimon Glass 		return 0;
3471dc6bcaSSimon Glass 
3592c27c51SBernie Thompson 	for (i = 0; i < valid; i++) {
3692c27c51SBernie Thompson 		/*
3792c27c51SBernie Thompson 		 * Find 2 keys such that one key is in the same row
3892c27c51SBernie Thompson 		 * and the other is in the same column as the i-th key.
3992c27c51SBernie Thompson 		 */
4092c27c51SBernie Thompson 		for (j = i + 1; j < valid; j++) {
4192c27c51SBernie Thompson 			if (keys[j].col == keys[i].col)
4292c27c51SBernie Thompson 				key_in_same_col = 1;
4392c27c51SBernie Thompson 			if (keys[j].row == keys[i].row)
4492c27c51SBernie Thompson 				key_in_same_row = 1;
4592c27c51SBernie Thompson 		}
4692c27c51SBernie Thompson 	}
4792c27c51SBernie Thompson 
4892c27c51SBernie Thompson 	if (key_in_same_col && key_in_same_row)
4992c27c51SBernie Thompson 		return 1;
5092c27c51SBernie Thompson 	else
5192c27c51SBernie Thompson 		return 0;
5292c27c51SBernie Thompson }
5392c27c51SBernie Thompson 
key_matrix_decode(struct key_matrix * config,struct key_matrix_key keys[],int num_keys,int keycode[],int max_keycodes)5492c27c51SBernie Thompson int key_matrix_decode(struct key_matrix *config, struct key_matrix_key keys[],
5592c27c51SBernie Thompson 		      int num_keys, int keycode[], int max_keycodes)
5692c27c51SBernie Thompson {
5792c27c51SBernie Thompson 	const u8 *keymap;
5892c27c51SBernie Thompson 	int valid, upto;
5992c27c51SBernie Thompson 	int pos;
6092c27c51SBernie Thompson 
6192c27c51SBernie Thompson 	debug("%s: num_keys = %d\n", __func__, num_keys);
6292c27c51SBernie Thompson 	keymap = config->plain_keycode;
6392c27c51SBernie Thompson 	for (valid = upto = 0; upto < num_keys; upto++) {
6492c27c51SBernie Thompson 		struct key_matrix_key *key = &keys[upto];
6592c27c51SBernie Thompson 
6692c27c51SBernie Thompson 		debug("  valid=%d, row=%d, col=%d\n", key->valid, key->row,
6792c27c51SBernie Thompson 		      key->col);
6892c27c51SBernie Thompson 		if (!key->valid)
6992c27c51SBernie Thompson 			continue;
7092c27c51SBernie Thompson 		pos = key->row * config->num_cols + key->col;
7192c27c51SBernie Thompson 		if (config->fn_keycode && pos == config->fn_pos)
7292c27c51SBernie Thompson 			keymap = config->fn_keycode;
7392c27c51SBernie Thompson 
7492c27c51SBernie Thompson 		/* Convert the (row, col) values into a keycode */
7592c27c51SBernie Thompson 		if (valid < max_keycodes)
7692c27c51SBernie Thompson 			keycode[valid++] = keymap[pos];
7792c27c51SBernie Thompson 		debug("    keycode=%d\n", keymap[pos]);
7892c27c51SBernie Thompson 	}
7992c27c51SBernie Thompson 
8092c27c51SBernie Thompson 	/* For a ghost key config, ignore the keypresses for this iteration. */
8171dc6bcaSSimon Glass 	if (has_ghosting(config, keys, valid)) {
8292c27c51SBernie Thompson 		valid = 0;
8392c27c51SBernie Thompson 		debug("    ghosting detected!\n");
8492c27c51SBernie Thompson 	}
8592c27c51SBernie Thompson 	debug("  %d valid keycodes found\n", valid);
8692c27c51SBernie Thompson 
8792c27c51SBernie Thompson 	return valid;
8892c27c51SBernie Thompson }
8992c27c51SBernie Thompson 
9092c27c51SBernie Thompson /**
9192c27c51SBernie Thompson  * Create a new keycode map from some provided data
9292c27c51SBernie Thompson  *
9392c27c51SBernie Thompson  * This decodes a keycode map in the format used by the fdt, which is one
9492c27c51SBernie Thompson  * word per entry, with the row, col and keycode encoded in that word.
9592c27c51SBernie Thompson  *
9692c27c51SBernie Thompson  * We create a (row x col) size byte array with each entry containing the
9792c27c51SBernie Thompson  * keycode for that (row, col). We also search for map_keycode and return
9892c27c51SBernie Thompson  * its position if found (this is used for finding the Fn key).
9992c27c51SBernie Thompson  *
10092c27c51SBernie Thompson  * @param config        Key matrix dimensions structure
10192c27c51SBernie Thompson  * @param data          Keycode data
10292c27c51SBernie Thompson  * @param len           Number of entries in keycode table
10392c27c51SBernie Thompson  * @param map_keycode   Key code to find in the map
10492c27c51SBernie Thompson  * @param pos           Returns position of map_keycode, if found, else -1
10592c27c51SBernie Thompson  * @return map  Pointer to allocated map
10692c27c51SBernie Thompson  */
create_keymap(struct key_matrix * config,const u32 * data,int len,int map_keycode,int * pos)1078327d41bSSimon Glass static uchar *create_keymap(struct key_matrix *config, const u32 *data, int len,
10892c27c51SBernie Thompson 			    int map_keycode, int *pos)
10992c27c51SBernie Thompson {
11092c27c51SBernie Thompson 	uchar *map;
11192c27c51SBernie Thompson 
11292c27c51SBernie Thompson 	if (pos)
11392c27c51SBernie Thompson 		*pos = -1;
11492c27c51SBernie Thompson 	map = (uchar *)calloc(1, config->key_count);
11592c27c51SBernie Thompson 	if (!map) {
11692c27c51SBernie Thompson 		debug("%s: failed to malloc %d bytes\n", __func__,
11792c27c51SBernie Thompson 			config->key_count);
11892c27c51SBernie Thompson 		return NULL;
11992c27c51SBernie Thompson 	}
12092c27c51SBernie Thompson 
12192c27c51SBernie Thompson 	for (; len >= sizeof(u32); data++, len -= 4) {
12292c27c51SBernie Thompson 		u32 tmp = fdt32_to_cpu(*data);
12392c27c51SBernie Thompson 		int key_code, row, col;
12492c27c51SBernie Thompson 		int entry;
12592c27c51SBernie Thompson 
12692c27c51SBernie Thompson 		row = (tmp >> 24) & 0xff;
12792c27c51SBernie Thompson 		col = (tmp >> 16) & 0xff;
12892c27c51SBernie Thompson 		key_code = tmp & 0xffff;
12992c27c51SBernie Thompson 		entry = row * config->num_cols + col;
13092c27c51SBernie Thompson 		map[entry] = key_code;
13114813f19SSimon Glass 		debug("   map %d, %d: pos=%d, keycode=%d\n", row, col,
13214813f19SSimon Glass 		      entry, key_code);
13392c27c51SBernie Thompson 		if (pos && map_keycode == key_code)
13492c27c51SBernie Thompson 			*pos = entry;
13592c27c51SBernie Thompson 	}
13692c27c51SBernie Thompson 
13792c27c51SBernie Thompson 	return map;
13892c27c51SBernie Thompson }
13992c27c51SBernie Thompson 
key_matrix_decode_fdt(struct udevice * dev,struct key_matrix * config)1408327d41bSSimon Glass int key_matrix_decode_fdt(struct udevice *dev, struct key_matrix *config)
14192c27c51SBernie Thompson {
1428327d41bSSimon Glass 	const u32 *prop;
143df637fa6SStephen Warren 	int proplen;
144df637fa6SStephen Warren 	uchar *plain_keycode;
14592c27c51SBernie Thompson 
1468327d41bSSimon Glass 	prop = dev_read_prop(dev, "linux,keymap", &proplen);
147df637fa6SStephen Warren 	/* Basic keymap is required */
148374e8370SStephen Warren 	if (!prop) {
149374e8370SStephen Warren 		debug("%s: cannot find keycode-plain map\n", __func__);
150df637fa6SStephen Warren 		return -1;
151374e8370SStephen Warren 	}
15292c27c51SBernie Thompson 
1538327d41bSSimon Glass 	plain_keycode = create_keymap(config, prop, proplen, KEY_FN,
1548327d41bSSimon Glass 				      &config->fn_pos);
155df637fa6SStephen Warren 	config->plain_keycode = plain_keycode;
156df637fa6SStephen Warren 	/* Conversion error -> fail */
157df637fa6SStephen Warren 	if (!config->plain_keycode)
158df637fa6SStephen Warren 		return -1;
15992c27c51SBernie Thompson 
1608327d41bSSimon Glass 	prop = dev_read_prop(dev, "linux,fn-keymap", &proplen);
161df637fa6SStephen Warren 	/* fn keymap is optional */
162df637fa6SStephen Warren 	if (!prop)
163df637fa6SStephen Warren 		goto done;
16492c27c51SBernie Thompson 
1658327d41bSSimon Glass 	config->fn_keycode = create_keymap(config, prop, proplen, -1, NULL);
166df637fa6SStephen Warren 	/* Conversion error -> fail */
167374e8370SStephen Warren 	if (!config->fn_keycode) {
168df637fa6SStephen Warren 		free(plain_keycode);
16992c27c51SBernie Thompson 		return -1;
17092c27c51SBernie Thompson 	}
17192c27c51SBernie Thompson 
172df637fa6SStephen Warren done:
173df637fa6SStephen Warren 	debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
174df637fa6SStephen Warren 	      config->plain_keycode, config->fn_keycode);
17592c27c51SBernie Thompson 	return 0;
17692c27c51SBernie Thompson }
17792c27c51SBernie Thompson 
key_matrix_init(struct key_matrix * config,int rows,int cols,int ghost_filter)17871dc6bcaSSimon Glass int key_matrix_init(struct key_matrix *config, int rows, int cols,
17971dc6bcaSSimon Glass 		    int ghost_filter)
18092c27c51SBernie Thompson {
18192c27c51SBernie Thompson 	memset(config, '\0', sizeof(*config));
18292c27c51SBernie Thompson 	config->num_rows = rows;
18392c27c51SBernie Thompson 	config->num_cols = cols;
18492c27c51SBernie Thompson 	config->key_count = rows * cols;
18571dc6bcaSSimon Glass 	config->ghost_filter = ghost_filter;
18692c27c51SBernie Thompson 	assert(config->key_count > 0);
18792c27c51SBernie Thompson 
18892c27c51SBernie Thompson 	return 0;
18992c27c51SBernie Thompson }
190