blob: 8dfa44de13068660d81afce30e57f23b5d47c303 [file] [log] [blame]
Bernie Thompson757342a2012-04-17 09:01:31 +00001/*
2 * Keyboard matrix helper functions
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Bernie Thompson757342a2012-04-17 09:01:31 +00005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Bernie Thompson757342a2012-04-17 09:01:31 +00007 */
8
9#ifndef _KEY_MATRIX_H
10#define _KEY_MATRIX_H
11
12#include <common.h>
13
14/* Information about a matrix keyboard */
15struct key_matrix {
16 /* Dimensions of the keyboard matrix, in rows and columns */
17 int num_rows;
18 int num_cols;
19 int key_count; /* number of keys in the matrix (= rows * cols) */
20
21 /*
22 * Information about keycode mappings. The plain_keycode array must
23 * exist but fn may be NULL in which case it is not decoded.
24 */
25 const u8 *plain_keycode; /* key code for each row / column */
26 const u8 *fn_keycode; /* ...when Fn held down */
27 int fn_pos; /* position of Fn key in key (or -1) */
Simon Glass7a951eb2012-09-27 15:18:42 +000028 int ghost_filter; /* non-zero to enable ghost filter */
Bernie Thompson757342a2012-04-17 09:01:31 +000029};
30
31/* Information about a particular key (row, column pair) in the matrix */
32struct key_matrix_key {
33 uint8_t row; /* row number (0 = first) */
34 uint8_t col; /* column number (0 = first) */
35 uint8_t valid; /* 1 if valid, 0 to ignore this */
36};
37
38/**
39 * Decode a set of pressed keys into key codes
40 *
41 * Given a list of keys that are pressed, this converts this list into
42 * a list of key codes. Each of the keys has a valid flag, which can be
43 * used to mark a particular key as invalid (so that it is ignored).
44 *
45 * The plain keymap is used, unless the Fn key is detected along the way,
46 * at which point we switch to the Fn key map.
47 *
48 * If key ghosting is detected, we simply ignore the keys and return 0.
49 *
50 * @param config Keyboard matrix config
51 * @param keys List of keys to process (each is row, col)
52 * @param num_keys Number of keys to process
53 * @param keycode Returns a list of key codes, decoded from input
54 * @param max_keycodes Size of key codes array (suggest 8)
55 *
56 */
57int key_matrix_decode(struct key_matrix *config, struct key_matrix_key *keys,
58 int num_keys, int keycode[], int max_keycodes);
59
60/**
61 * Read the keyboard configuration out of the fdt.
62 *
63 * Decode properties of named "linux,<type>keymap" where <type> is either
64 * empty, or "fn-". Then set up the plain key map (and the FN keymap if
65 * present).
66 *
67 * @param config Keyboard matrix config
68 * @param blob FDT blob
69 * @param node Node containing compatible data
70 * @return 0 if ok, -1 on error
71 */
Simon Glassbf45b2e2017-05-18 20:09:53 -060072int key_matrix_decode_fdt(struct udevice *dev, struct key_matrix *config);
Bernie Thompson757342a2012-04-17 09:01:31 +000073
74/**
75 * Set up a new key matrix.
76 *
77 * @param config Keyboard matrix config
78 * @param rows Number of rows in key matrix
79 * @param cols Number of columns in key matrix
Simon Glass7a951eb2012-09-27 15:18:42 +000080 * @param ghost_filter Non-zero to enable ghost filtering
Bernie Thompson757342a2012-04-17 09:01:31 +000081 * @return 0 if ok, -1 on error
82 */
Simon Glass7a951eb2012-09-27 15:18:42 +000083int key_matrix_init(struct key_matrix *config, int rows, int cols,
84 int ghost_filter);
Bernie Thompson757342a2012-04-17 09:01:31 +000085
86#endif