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