kscan: input: add input to kscan adapter
Add a driver that listens for input events and reports them on a kscan API. This allows porting kscan drivers to the input APIs while maintaining compatibility with the existing kscan based applications. Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
This commit is contained in:
parent
286f10323c
commit
a9735abf84
|
@ -12,5 +12,6 @@ zephyr_library_sources_ifdef(CONFIG_KSCAN_CST816S kscan_cst816s.c)
|
|||
zephyr_library_sources_ifdef(CONFIG_KSCAN_CAP1203 kscan_cap1203.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_KSCAN_NPCX kscan_npcx.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_KSCAN_XPT2046 kscan_xpt2046.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_KSCAN_INPUT kscan_input.c)
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_USERSPACE kscan_handlers.c)
|
||||
|
|
|
@ -20,6 +20,7 @@ source "drivers/kscan/Kconfig.cst816s"
|
|||
source "drivers/kscan/Kconfig.cap1203"
|
||||
source "drivers/kscan/Kconfig.npcx"
|
||||
source "drivers/kscan/Kconfig.xpt2046"
|
||||
source "drivers/kscan/Kconfig.input"
|
||||
|
||||
module = KSCAN
|
||||
module-str = kscan
|
||||
|
|
8
drivers/kscan/Kconfig.input
Normal file
8
drivers/kscan/Kconfig.input
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Copyright 2023 Google LLC
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config KSCAN_INPUT
|
||||
bool "Input subsystem to kscan adapter driver"
|
||||
default y
|
||||
depends on INPUT
|
||||
depends on DT_HAS_ZEPHYR_KSCAN_INPUT_ENABLED
|
120
drivers/kscan/kscan_input.c
Normal file
120
drivers/kscan/kscan_input.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* Copyright 2023 Google LLC
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#define DT_DRV_COMPAT zephyr_kscan_input
|
||||
|
||||
#include <zephyr/drivers/kscan.h>
|
||||
#include <zephyr/input/input.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(kscan_input, CONFIG_KSCAN_LOG_LEVEL);
|
||||
|
||||
struct kscan_input_config {
|
||||
const struct device *input_dev;
|
||||
};
|
||||
|
||||
struct kscan_input_data {
|
||||
bool enabled;
|
||||
kscan_callback_t callback;
|
||||
int row;
|
||||
int col;
|
||||
bool pressed;
|
||||
};
|
||||
|
||||
static void kscan_input_cb(const struct device *dev, struct input_event *evt)
|
||||
{
|
||||
struct kscan_input_data *data = dev->data;
|
||||
|
||||
switch (evt->code) {
|
||||
case INPUT_ABS_X:
|
||||
data->col = evt->value;
|
||||
break;
|
||||
case INPUT_ABS_Y:
|
||||
data->row = evt->value;
|
||||
break;
|
||||
case INPUT_BTN_TOUCH:
|
||||
data->pressed = evt->value;
|
||||
break;
|
||||
}
|
||||
|
||||
if (evt->sync) {
|
||||
LOG_DBG("input event: %3d %3d %d",
|
||||
data->row, data->col, data->pressed);
|
||||
if (data->callback) {
|
||||
data->callback(dev, data->row, data->col, data->pressed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int kscan_input_configure(const struct device *dev,
|
||||
kscan_callback_t callback)
|
||||
{
|
||||
struct kscan_input_data *data = dev->data;
|
||||
|
||||
if (!callback) {
|
||||
LOG_ERR("Invalid callback (NULL)");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
data->callback = callback;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kscan_input_enable_callback(const struct device *dev)
|
||||
{
|
||||
struct kscan_input_data *data = dev->data;
|
||||
|
||||
data->enabled = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kscan_input_disable_callback(const struct device *dev)
|
||||
{
|
||||
struct kscan_input_data *data = dev->data;
|
||||
|
||||
data->enabled = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int kscan_input_init(const struct device *dev)
|
||||
{
|
||||
const struct kscan_input_config *cfg = dev->config;
|
||||
|
||||
if (!device_is_ready(cfg->input_dev)) {
|
||||
LOG_ERR("Input device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct kscan_driver_api kscan_input_driver_api = {
|
||||
.config = kscan_input_configure,
|
||||
.enable_callback = kscan_input_enable_callback,
|
||||
.disable_callback = kscan_input_disable_callback,
|
||||
};
|
||||
|
||||
#define KSCAN_INPUT_INIT(index) \
|
||||
static void kscan_input_cb_##index(struct input_event *evt) \
|
||||
{ \
|
||||
kscan_input_cb(DEVICE_DT_GET(DT_INST(index, DT_DRV_COMPAT)), \
|
||||
evt); \
|
||||
} \
|
||||
INPUT_LISTENER_CB_DEFINE(DEVICE_DT_GET(DT_INST_PARENT(index)), \
|
||||
kscan_input_cb_##index); \
|
||||
static const struct kscan_input_config kscan_input_config_##index = { \
|
||||
.input_dev = DEVICE_DT_GET(DT_INST_PARENT(index)), \
|
||||
}; \
|
||||
static struct kscan_input_data kscan_input_data_##index; \
|
||||
DEVICE_DT_INST_DEFINE(index, kscan_input_init, NULL, \
|
||||
&kscan_input_data_##index, \
|
||||
&kscan_input_config_##index, \
|
||||
POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \
|
||||
&kscan_input_driver_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(KSCAN_INPUT_INIT)
|
23
dts/bindings/kscan/zephyr,kscan-input.yaml
Normal file
23
dts/bindings/kscan/zephyr,kscan-input.yaml
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Copyright 2023 Google LLC
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
description: |
|
||||
Input to kscan adapter.
|
||||
|
||||
Allows using an input device with the kscan API. Define as a child node of
|
||||
the input device, for example
|
||||
|
||||
chosen {
|
||||
zephyr,keyboard-scan = &kscan_input;
|
||||
};
|
||||
|
||||
ft5336@38 {
|
||||
...
|
||||
kscan_input: kscan-input {
|
||||
compatible = "zephyr,kscan-input";
|
||||
};
|
||||
};
|
||||
|
||||
compatible: "zephyr,kscan-input"
|
||||
|
||||
include: kscan.yaml
|
Loading…
Reference in a new issue