1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */ 292c27c51SBernie Thompson /* 392c27c51SBernie Thompson * Keyboard matrix helper functions 492c27c51SBernie Thompson * 592c27c51SBernie Thompson * Copyright (c) 2012 The Chromium OS Authors. 692c27c51SBernie Thompson */ 792c27c51SBernie Thompson 892c27c51SBernie Thompson #ifndef _KEY_MATRIX_H 992c27c51SBernie Thompson #define _KEY_MATRIX_H 1092c27c51SBernie Thompson 1192c27c51SBernie Thompson #include <common.h> 1292c27c51SBernie Thompson 1392c27c51SBernie Thompson /* Information about a matrix keyboard */ 1492c27c51SBernie Thompson struct key_matrix { 1592c27c51SBernie Thompson /* Dimensions of the keyboard matrix, in rows and columns */ 1692c27c51SBernie Thompson int num_rows; 1792c27c51SBernie Thompson int num_cols; 1892c27c51SBernie Thompson int key_count; /* number of keys in the matrix (= rows * cols) */ 1992c27c51SBernie Thompson 2092c27c51SBernie Thompson /* 2192c27c51SBernie Thompson * Information about keycode mappings. The plain_keycode array must 2292c27c51SBernie Thompson * exist but fn may be NULL in which case it is not decoded. 2392c27c51SBernie Thompson */ 2492c27c51SBernie Thompson const u8 *plain_keycode; /* key code for each row / column */ 2592c27c51SBernie Thompson const u8 *fn_keycode; /* ...when Fn held down */ 2692c27c51SBernie Thompson int fn_pos; /* position of Fn key in key (or -1) */ 2771dc6bcaSSimon Glass int ghost_filter; /* non-zero to enable ghost filter */ 2892c27c51SBernie Thompson }; 2992c27c51SBernie Thompson 3092c27c51SBernie Thompson /* Information about a particular key (row, column pair) in the matrix */ 3192c27c51SBernie Thompson struct key_matrix_key { 3292c27c51SBernie Thompson uint8_t row; /* row number (0 = first) */ 3392c27c51SBernie Thompson uint8_t col; /* column number (0 = first) */ 3492c27c51SBernie Thompson uint8_t valid; /* 1 if valid, 0 to ignore this */ 3592c27c51SBernie Thompson }; 3692c27c51SBernie Thompson 3792c27c51SBernie Thompson /** 3892c27c51SBernie Thompson * Decode a set of pressed keys into key codes 3992c27c51SBernie Thompson * 4092c27c51SBernie Thompson * Given a list of keys that are pressed, this converts this list into 4192c27c51SBernie Thompson * a list of key codes. Each of the keys has a valid flag, which can be 4292c27c51SBernie Thompson * used to mark a particular key as invalid (so that it is ignored). 4392c27c51SBernie Thompson * 4492c27c51SBernie Thompson * The plain keymap is used, unless the Fn key is detected along the way, 4592c27c51SBernie Thompson * at which point we switch to the Fn key map. 4692c27c51SBernie Thompson * 4792c27c51SBernie Thompson * If key ghosting is detected, we simply ignore the keys and return 0. 4892c27c51SBernie Thompson * 4992c27c51SBernie Thompson * @param config Keyboard matrix config 5092c27c51SBernie Thompson * @param keys List of keys to process (each is row, col) 5192c27c51SBernie Thompson * @param num_keys Number of keys to process 5292c27c51SBernie Thompson * @param keycode Returns a list of key codes, decoded from input 5392c27c51SBernie Thompson * @param max_keycodes Size of key codes array (suggest 8) 5492c27c51SBernie Thompson * 5592c27c51SBernie Thompson */ 5692c27c51SBernie Thompson int key_matrix_decode(struct key_matrix *config, struct key_matrix_key *keys, 5792c27c51SBernie Thompson int num_keys, int keycode[], int max_keycodes); 5892c27c51SBernie Thompson 5992c27c51SBernie Thompson /** 6092c27c51SBernie Thompson * Read the keyboard configuration out of the fdt. 6192c27c51SBernie Thompson * 6292c27c51SBernie Thompson * Decode properties of named "linux,<type>keymap" where <type> is either 6392c27c51SBernie Thompson * empty, or "fn-". Then set up the plain key map (and the FN keymap if 6492c27c51SBernie Thompson * present). 6592c27c51SBernie Thompson * 6692c27c51SBernie Thompson * @param config Keyboard matrix config 6792c27c51SBernie Thompson * @param blob FDT blob 6892c27c51SBernie Thompson * @param node Node containing compatible data 6992c27c51SBernie Thompson * @return 0 if ok, -1 on error 7092c27c51SBernie Thompson */ 718327d41bSSimon Glass int key_matrix_decode_fdt(struct udevice *dev, struct key_matrix *config); 7292c27c51SBernie Thompson 7392c27c51SBernie Thompson /** 7492c27c51SBernie Thompson * Set up a new key matrix. 7592c27c51SBernie Thompson * 7692c27c51SBernie Thompson * @param config Keyboard matrix config 7792c27c51SBernie Thompson * @param rows Number of rows in key matrix 7892c27c51SBernie Thompson * @param cols Number of columns in key matrix 7971dc6bcaSSimon Glass * @param ghost_filter Non-zero to enable ghost filtering 8092c27c51SBernie Thompson * @return 0 if ok, -1 on error 8192c27c51SBernie Thompson */ 8271dc6bcaSSimon Glass int key_matrix_init(struct key_matrix *config, int rows, int cols, 8371dc6bcaSSimon Glass int ghost_filter); 8492c27c51SBernie Thompson 8592c27c51SBernie Thompson #endif 86