From adcb2f580cedd21273cb71d1dbd2be2683e78cd5 Mon Sep 17 00:00:00 2001 From: Fabio Baltieri Date: Fri, 17 Nov 2023 13:12:24 +0000 Subject: [PATCH] input: gpio_kbd_matrix: define a type for the row data Add a typedef for the row type rather than using uint8_t directly, this allow supporting bigger matrix as an option by using a different type. Signed-off-by: Fabio Baltieri --- drivers/input/input_gpio_kbd_matrix.c | 2 +- drivers/input/input_ite_it8xxx2_kbd.c | 2 +- drivers/input/input_kbd_matrix.c | 22 ++++++++++----------- drivers/input/input_npcx_kbd.c | 2 +- include/zephyr/input/input_kbd_matrix.h | 26 +++++++++++++++---------- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/drivers/input/input_gpio_kbd_matrix.c b/drivers/input/input_gpio_kbd_matrix.c index 9d532bfd0a..f81a01837f 100644 --- a/drivers/input/input_gpio_kbd_matrix.c +++ b/drivers/input/input_gpio_kbd_matrix.c @@ -64,7 +64,7 @@ static void gpio_kbd_matrix_drive_column(const struct device *dev, int col) data->last_col_state = state; } -static int gpio_kbd_matrix_read_row(const struct device *dev) +static kbd_row_t gpio_kbd_matrix_read_row(const struct device *dev) { const struct gpio_kbd_matrix_config *cfg = dev->config; const struct input_kbd_matrix_common_config *common = &cfg->common; diff --git a/drivers/input/input_ite_it8xxx2_kbd.c b/drivers/input/input_ite_it8xxx2_kbd.c index 8d688d42db..5e62e6ee32 100644 --- a/drivers/input/input_ite_it8xxx2_kbd.c +++ b/drivers/input/input_ite_it8xxx2_kbd.c @@ -79,7 +79,7 @@ static void it8xxx2_kbd_drive_column(const struct device *dev, int col) } } -static int it8xxx2_kbd_read_row(const struct device *dev) +static kbd_row_t it8xxx2_kbd_read_row(const struct device *dev) { const struct it8xxx2_kbd_config *const config = dev->config; struct kscan_it8xxx2_regs *const inst = config->base; diff --git a/drivers/input/input_kbd_matrix.c b/drivers/input/input_kbd_matrix.c index 6005a76b3b..07e3c4c054 100644 --- a/drivers/input/input_kbd_matrix.c +++ b/drivers/input/input_kbd_matrix.c @@ -16,8 +16,6 @@ LOG_MODULE_REGISTER(input_kbd_matrix, CONFIG_INPUT_LOG_LEVEL); -#define INPUT_KBD_MATRIX_ROW_MASK UINT8_MAX - void input_kbd_matrix_poll_start(const struct device *dev) { struct input_kbd_matrix_common_data *data = dev->data; @@ -28,7 +26,7 @@ void input_kbd_matrix_poll_start(const struct device *dev) static bool input_kbd_matrix_ghosting(const struct device *dev) { const struct input_kbd_matrix_common_config *cfg = dev->config; - const uint8_t *state = cfg->matrix_new_state; + const kbd_row_t *state = cfg->matrix_new_state; /* * Matrix keyboard designs are suceptible to ghosting. @@ -59,7 +57,7 @@ static bool input_kbd_matrix_ghosting(const struct device *dev) * using z&(z-1) which is non-zero only if z has more * than one bit set. */ - uint8_t common_row_bits = state[c] & state[c_next]; + kbd_row_t common_row_bits = state[c] & state[c_next]; if (common_row_bits & (common_row_bits - 1)) { return true; @@ -74,8 +72,8 @@ static bool input_kbd_matrix_scan(const struct device *dev) { const struct input_kbd_matrix_common_config *cfg = dev->config; const struct input_kbd_matrix_api *api = cfg->api; - int row; - uint8_t key_event = 0U; + kbd_row_t row; + kbd_row_t key_event = 0U; for (int col = 0; col < cfg->col_size; col++) { api->drive_column(dev, col); @@ -83,7 +81,7 @@ static bool input_kbd_matrix_scan(const struct device *dev) /* Allow the matrix to stabilize before reading it */ k_busy_wait(cfg->settle_time_us); - row = api->read_row(dev) & INPUT_KBD_MATRIX_ROW_MASK; + row = api->read_row(dev); cfg->matrix_new_state[col] = row; key_event |= row; } @@ -97,10 +95,10 @@ static void input_kbd_matrix_update_state(const struct device *dev) { const struct input_kbd_matrix_common_config *cfg = dev->config; struct input_kbd_matrix_common_data *data = dev->data; - uint8_t *matrix_new_state = cfg->matrix_new_state; + kbd_row_t *matrix_new_state = cfg->matrix_new_state; uint32_t cycles_now; - uint8_t row_changed; - uint8_t deb_col; + kbd_row_t row_changed; + kbd_row_t deb_col; cycles_now = k_cycle_get_32(); @@ -143,8 +141,8 @@ static void input_kbd_matrix_update_state(const struct device *dev) /* Debouncing for each row key occurs here */ for (int r = 0; r < cfg->row_size; r++) { - uint8_t mask = BIT(r); - uint8_t row_bit = matrix_new_state[c] & mask; + kbd_row_t mask = BIT(r); + kbd_row_t row_bit = matrix_new_state[c] & mask; /* Continue if we already debounce a key */ if (!(deb_col & mask)) { diff --git a/drivers/input/input_npcx_kbd.c b/drivers/input/input_npcx_kbd.c index 64cde93bda..25f28e3e60 100644 --- a/drivers/input/input_npcx_kbd.c +++ b/drivers/input/input_npcx_kbd.c @@ -97,7 +97,7 @@ static void npcx_kbd_drive_column(const struct device *dev, int col) inst->KBSOUT1 = ((mask >> 16) & 0x03); } -static int npcx_kbd_read_row(const struct device *dev) +static kbd_row_t npcx_kbd_read_row(const struct device *dev) { const struct npcx_kbd_config *config = dev->config; const struct input_kbd_matrix_common_config *common = &config->common; diff --git a/include/zephyr/input/input_kbd_matrix.h b/include/zephyr/input/input_kbd_matrix.h index 0566f14d2c..98bd791d49 100644 --- a/include/zephyr/input/input_kbd_matrix.h +++ b/include/zephyr/input/input_kbd_matrix.h @@ -29,6 +29,12 @@ /** Number of tracked scan cycles */ #define INPUT_KBD_MATRIX_SCAN_OCURRENCES 30U +/** Row entry data type */ +typedef uint8_t kbd_row_t; + +/** Maximum number of rows */ +#define INPUT_KBD_MATRIX_ROW_BITS NUM_BITS(kbd_row_t) + /** * @brief Keyboard matrix internal APIs. */ @@ -49,7 +55,7 @@ struct input_kbd_matrix_api { * * @param dev Pointer to the keyboard matrix device. */ - int (*read_row)(const struct device *dev); + kbd_row_t (*read_row)(const struct device *dev); /** * @brief Request to put the matrix in detection mode. * @@ -80,10 +86,10 @@ struct input_kbd_matrix_common_config { bool ghostkey_check; /* extra data pointers */ - uint8_t *matrix_stable_state; - uint8_t *matrix_unstable_state; - uint8_t *matrix_previous_state; - uint8_t *matrix_new_state; + kbd_row_t *matrix_stable_state; + kbd_row_t *matrix_unstable_state; + kbd_row_t *matrix_previous_state; + kbd_row_t *matrix_new_state; uint8_t *scan_cycle_idx; }; @@ -96,12 +102,12 @@ struct input_kbd_matrix_common_config { * specify row and col count. */ #define INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(node_id, _row_size, _col_size) \ - BUILD_ASSERT(IN_RANGE(_row_size, 1, 8), "invalid row-size"); \ + BUILD_ASSERT(IN_RANGE(_row_size, 1, INPUT_KBD_MATRIX_ROW_BITS), "invalid row-size"); \ BUILD_ASSERT(IN_RANGE(_col_size, 1, UINT8_MAX), "invalid col-size"); \ - static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state)[_col_size]; \ - static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state)[_col_size]; \ - static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state)[_col_size]; \ - static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state)[_col_size]; \ + static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state)[_col_size]; \ + static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state)[_col_size]; \ + static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state)[_col_size]; \ + static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state)[_col_size]; \ static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx)[_row_size * _col_size]; /**