From 9065c2d156ff97dc0a8f31698ea7388625cf228d Mon Sep 17 00:00:00 2001 From: Fabio Baltieri Date: Thu, 22 Jun 2023 18:45:01 +0000 Subject: [PATCH] input: convert xpt2046 from kscan Convert the XPT2046 driver to the input subsystem, change the api, remove the callback and enable logic. Signed-off-by: Fabio Baltieri --- drivers/input/CMakeLists.txt | 1 + drivers/input/Kconfig | 1 + drivers/{kscan => input}/Kconfig.xpt2046 | 2 +- .../kscan_xpt2046.c => input/input_xpt2046.c} | 77 +++++-------------- drivers/kscan/CMakeLists.txt | 1 - drivers/kscan/Kconfig | 1 - .../{kscan => input}/xptek,xpt2046.yaml | 2 +- 7 files changed, 24 insertions(+), 61 deletions(-) rename drivers/{kscan => input}/Kconfig.xpt2046 (93%) rename drivers/{kscan/kscan_xpt2046.c => input/input_xpt2046.c} (84%) rename dts/bindings/{kscan => input}/xptek,xpt2046.yaml (96%) diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index d6e0f2e802..a5483ffc76 100644 --- a/drivers/input/CMakeLists.txt +++ b/drivers/input/CMakeLists.txt @@ -8,3 +8,4 @@ zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_KEYS input_gpio_keys.c) zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_QDEC input_gpio_qdec.c) zephyr_library_sources_ifdef(CONFIG_INPUT_NPCX_KBD input_npcx_kbd.c) zephyr_library_sources_ifdef(CONFIG_INPUT_SDL_TOUCH input_sdl_touch.c) +zephyr_library_sources_ifdef(CONFIG_INPUT_XPT2046 input_xpt2046.c) diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index ee9508ae4d..6e1d8abf5e 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -10,6 +10,7 @@ source "drivers/input/Kconfig.gpio_keys" source "drivers/input/Kconfig.gpio_qdec" source "drivers/input/Kconfig.npcx" source "drivers/input/Kconfig.sdl" +source "drivers/input/Kconfig.xpt2046" endmenu # Input Drivers diff --git a/drivers/kscan/Kconfig.xpt2046 b/drivers/input/Kconfig.xpt2046 similarity index 93% rename from drivers/kscan/Kconfig.xpt2046 rename to drivers/input/Kconfig.xpt2046 index 5d7d1247c4..21ce148dbb 100644 --- a/drivers/kscan/Kconfig.xpt2046 +++ b/drivers/input/Kconfig.xpt2046 @@ -1,7 +1,7 @@ # Copyright (c) 2023 Seppo Takalo # SPDX-License-Identifier: Apache-2.0 -config KSCAN_XPT2046 +config INPUT_XPT2046 bool "XPT2046 resistive touch panel driver" default y depends on DT_HAS_XPTEK_XPT2046_ENABLED diff --git a/drivers/kscan/kscan_xpt2046.c b/drivers/input/input_xpt2046.c similarity index 84% rename from drivers/kscan/kscan_xpt2046.c rename to drivers/input/input_xpt2046.c index 67b2c805e3..4c4f4c514b 100644 --- a/drivers/kscan/kscan_xpt2046.c +++ b/drivers/input/input_xpt2046.c @@ -6,12 +6,12 @@ #define DT_DRV_COMPAT xptek_xpt2046 -#include #include -#include +#include +#include #include -LOG_MODULE_REGISTER(xpt2046, CONFIG_KSCAN_LOG_LEVEL); +LOG_MODULE_REGISTER(xpt2046, CONFIG_INPUT_LOG_LEVEL); struct xpt2046_config { const struct spi_dt_spec bus; @@ -27,9 +27,7 @@ struct xpt2046_config { }; struct xpt2046_data { const struct device *dev; - kscan_callback_t callback; struct gpio_callback int_gpio_cb; - bool enabled; struct k_work work; struct k_work_delayable dwork; uint8_t rbuf[9]; @@ -109,14 +107,14 @@ static void xpt2046_release_handler(struct k_work *kw) struct xpt2046_data *data = CONTAINER_OF(dw, struct xpt2046_data, dwork); struct xpt2046_config *config = (struct xpt2046_config *)data->dev->config; - if (!data->pressed || !data->enabled) { + if (!data->pressed) { return; } /* Check if touch is still pressed */ if (gpio_pin_get_dt(&config->int_gpio) == 0) { data->pressed = false; - data->callback(data->dev, data->last_y, data->last_y, false); + input_report_key(data->dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER); } else { /* Re-check later */ k_work_reschedule(&data->dwork, K_MSEC(10)); @@ -168,58 +166,23 @@ static void xpt2046_work_handler(struct k_work *kw) /* Don't send any other than "pressed" events. * releasing seem to cause just random noise */ - if (data->enabled && pressed) { + if (pressed) { LOG_DBG("raw: x=%4u y=%4u ==> x=%4d y=%4d", meas.x, meas.y, x, y); + + input_report_abs(data->dev, INPUT_ABS_X, x, false, K_FOREVER); + input_report_abs(data->dev, INPUT_ABS_Y, y, false, K_FOREVER); + input_report_key(data->dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER); + data->last_x = x; data->last_y = y; data->pressed = pressed; - data->callback(data->dev, (uint32_t)y, (uint32_t)x, pressed); + /* Ensure that we send released event */ k_work_reschedule(&data->dwork, K_MSEC(100)); } gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb); } -static int xpt2046_configure(const struct device *dev, kscan_callback_t callback) -{ - struct xpt2046_data *data = dev->data; - - if (!callback) { - LOG_ERR("Callback is null"); - return -EINVAL; - } - LOG_DBG("%s: set callback", dev->name); - - data->callback = callback; - - return 0; -} - -static int xpt2046_enable_callback(const struct device *dev) -{ - struct xpt2046_data *data = dev->data; - const struct xpt2046_config *config = dev->config; - - LOG_DBG("%s: enable cb", dev->name); - data->enabled = true; - gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb); - - return 0; -} - -static int xpt2046_disable_callback(const struct device *dev) -{ - struct xpt2046_data *data = dev->data; - const struct xpt2046_config *config = dev->config; - - gpio_remove_callback(config->int_gpio.port, &data->int_gpio_cb); - data->enabled = false; - - LOG_DBG("%s: disable cb", dev->name); - - return 0; -} - static int xpt2046_init(const struct device *dev) { int r; @@ -254,17 +217,17 @@ static int xpt2046_init(const struct device *dev) gpio_init_callback(&data->int_gpio_cb, xpt2046_isr_handler, BIT(config->int_gpio.pin)); + r = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb); + if (r < 0) { + LOG_ERR("Could not set gpio callback"); + return r; + } + LOG_INF("Init '%s' device", dev->name); return 0; } -static const struct kscan_driver_api xpt2046_driver_api = { - .config = xpt2046_configure, - .enable_callback = xpt2046_enable_callback, - .disable_callback = xpt2046_disable_callback, -}; - #define XPT2046_INIT(index) \ static const struct xpt2046_config xpt2046_config_##index = { \ .bus = SPI_DT_SPEC_INST_GET( \ @@ -281,8 +244,8 @@ static const struct kscan_driver_api xpt2046_driver_api = { }; \ static struct xpt2046_data xpt2046_data_##index; \ DEVICE_DT_INST_DEFINE(index, xpt2046_init, NULL, &xpt2046_data_##index, \ - &xpt2046_config_##index, POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \ - &xpt2046_driver_api); \ + &xpt2046_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \ + NULL); \ BUILD_ASSERT(DT_INST_PROP(index, min_x) < DT_INST_PROP(index, max_x), \ "min_x must be less than max_x"); \ BUILD_ASSERT(DT_INST_PROP(index, min_y) < DT_INST_PROP(index, max_y), \ diff --git a/drivers/kscan/CMakeLists.txt b/drivers/kscan/CMakeLists.txt index c6c38222e2..b9b150873c 100644 --- a/drivers/kscan/CMakeLists.txt +++ b/drivers/kscan/CMakeLists.txt @@ -10,7 +10,6 @@ zephyr_library_sources_ifdef(CONFIG_KSCAN_XEC kscan_mchp_xec.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_HT16K33 kscan_ht16k33.c) 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_XPT2046 kscan_xpt2046.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_INPUT kscan_input.c) zephyr_library_sources_ifdef(CONFIG_USERSPACE kscan_handlers.c) diff --git a/drivers/kscan/Kconfig b/drivers/kscan/Kconfig index 30b05eb168..c5fcee278d 100644 --- a/drivers/kscan/Kconfig +++ b/drivers/kscan/Kconfig @@ -16,7 +16,6 @@ source "drivers/kscan/Kconfig.xec" source "drivers/kscan/Kconfig.ht16k33" source "drivers/kscan/Kconfig.cst816s" source "drivers/kscan/Kconfig.cap1203" -source "drivers/kscan/Kconfig.xpt2046" source "drivers/kscan/Kconfig.input" module = KSCAN diff --git a/dts/bindings/kscan/xptek,xpt2046.yaml b/dts/bindings/input/xptek,xpt2046.yaml similarity index 96% rename from dts/bindings/kscan/xptek,xpt2046.yaml rename to dts/bindings/input/xptek,xpt2046.yaml index 758137c2ee..e01a20bc3a 100644 --- a/dts/bindings/kscan/xptek,xpt2046.yaml +++ b/dts/bindings/input/xptek,xpt2046.yaml @@ -4,7 +4,7 @@ description: Driver for XPT2046 touch IC compatible: "xptek,xpt2046" -include: [kscan.yaml, spi-device.yaml] +include: spi-device.yaml properties: int-gpios: