Skip to content

Commit 13a2f42

Browse files
fabiobaltierinashif
authored andcommitted
input: kbd_matrix: implement stable poll period support
Implement a new stable-poll-period-ms property to specify a new (slower) polling rate for when the matrix is stable. The keyboard thread can eat up a surprisingly high amount of cpu cycles in busy waiting if the specific hardware implementation happen to have a particularly slow settle time, but high frequency polling is really only needed when debouncing. The new property allow slowing down the polling rate when the matrix is stable (either key pressed but none to be debounced or idle in the case of the gpio implementation with no interrupts), this allows reducing the overall cpu time taken by the keyboard scanning thread when keys are persistently pressed. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 410c8a5 commit 13a2f42

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

drivers/input/input_gpio_kbd_matrix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static __maybe_unused void gpio_kbd_matrix_idle_poll_handler(const struct device
121121

122122
if (gpio_kbd_matrix_read_row(dev) == 0) {
123123
k_work_reschedule(cfg->idle_poll_dwork,
124-
K_USEC(common->poll_period_us));
124+
K_USEC(common->stable_poll_period_us));
125125
return;
126126
}
127127

@@ -137,7 +137,7 @@ static void gpio_kbd_matrix_set_detect_mode(const struct device *dev, bool enabl
137137
if (cfg->idle_poll_dwork != NULL) {
138138
if (enabled) {
139139
k_work_reschedule(cfg->idle_poll_dwork,
140-
K_USEC(common->poll_period_us));
140+
K_USEC(common->stable_poll_period_us));
141141
}
142142
return;
143143
}

drivers/input/input_kbd_matrix.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,27 @@ static k_timepoint_t input_kbd_matrix_poll_timeout(const struct device *dev)
264264
return sys_timepoint_calc(K_MSEC(cfg->poll_timeout_ms));
265265
}
266266

267+
static bool input_kbd_matrix_is_unstable(const struct device *dev)
268+
{
269+
const struct input_kbd_matrix_common_config *cfg = dev->config;
270+
271+
for (uint8_t c = 0; c < cfg->col_size; c++) {
272+
if (cfg->matrix_unstable_state[c] != 0) {
273+
return true;
274+
}
275+
}
276+
277+
return false;
278+
}
279+
267280
static void input_kbd_matrix_poll(const struct device *dev)
268281
{
269282
const struct input_kbd_matrix_common_config *cfg = dev->config;
270283
k_timepoint_t poll_time_end;
271284
uint32_t current_cycles;
272285
uint32_t cycles_diff;
273286
uint32_t wait_period_us;
287+
uint32_t poll_period_us;
274288

275289
poll_time_end = input_kbd_matrix_poll_timeout(dev);
276290

@@ -289,10 +303,14 @@ static void input_kbd_matrix_poll(const struct device *dev)
289303
*/
290304
current_cycles = k_cycle_get_32();
291305
cycles_diff = current_cycles - start_period_cycles;
292-
wait_period_us = cfg->poll_period_us - k_cyc_to_us_floor32(cycles_diff);
293306

294-
wait_period_us = CLAMP(wait_period_us,
295-
USEC_PER_MSEC, cfg->poll_period_us);
307+
if (input_kbd_matrix_is_unstable(dev)) {
308+
poll_period_us = cfg->poll_period_us;
309+
} else {
310+
poll_period_us = cfg->stable_poll_period_us;
311+
}
312+
wait_period_us = CLAMP(poll_period_us - k_cyc_to_us_floor32(cycles_diff),
313+
USEC_PER_MSEC, poll_period_us);
296314

297315
LOG_DBG("wait_period_us: %d", wait_period_us);
298316

dts/bindings/input/kbd-matrix-common.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ properties:
2323
Defines the poll period in msecs between between matrix scans, set to 0
2424
to never exit poll mode. Defaults to 5ms if unspecified.
2525
26+
stable-poll-period-ms:
27+
type: int
28+
description: |
29+
Defines the poll period in msecs between matrix scans when the matrix is
30+
stable, defaults to poll-period-ms value if unspecified.
31+
2632
poll-timeout-ms:
2733
type: int
2834
default: 100

include/zephyr/input/input_kbd_matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct input_kbd_matrix_common_config {
113113
uint8_t row_size;
114114
uint8_t col_size;
115115
uint32_t poll_period_us;
116+
uint32_t stable_poll_period_us;
116117
uint32_t poll_timeout_ms;
117118
uint32_t debounce_down_us;
118119
uint32_t debounce_up_us;
@@ -192,6 +193,9 @@ struct input_kbd_matrix_common_config {
192193
.row_size = _row_size, \
193194
.col_size = _col_size, \
194195
.poll_period_us = DT_PROP(node_id, poll_period_ms) * USEC_PER_MSEC, \
196+
.stable_poll_period_us = DT_PROP_OR(node_id, stable_poll_period_ms, \
197+
DT_PROP(node_id, poll_period_ms)) * \
198+
USEC_PER_MSEC, \
195199
.poll_timeout_ms = DT_PROP(node_id, poll_timeout_ms), \
196200
.debounce_down_us = DT_PROP(node_id, debounce_down_ms) * USEC_PER_MSEC, \
197201
.debounce_up_us = DT_PROP(node_id, debounce_up_ms) * USEC_PER_MSEC, \

0 commit comments

Comments
 (0)