From 723421b23104d42a787438bfb6c1eb8553360298 Mon Sep 17 00:00:00 2001 From: Fabian Blatz Date: Tue, 18 Jul 2023 08:39:10 +0200 Subject: [PATCH] input: convert cst816s from kscan Convert the cst816s capacitive touch screen driver to the input subsystem. Signed-off-by: Fabian Blatz --- boards/arm/pinetime_devkit0/Kconfig.defconfig | 3 + .../arm/pinetime_devkit0/pinetime_devkit0.dts | 8 +- drivers/input/CMakeLists.txt | 1 + drivers/input/Kconfig | 1 + drivers/{kscan => input}/Kconfig.cst816s | 12 +- .../kscan_cst816s.c => input/input_cst816s.c} | 166 ++++++------------ drivers/kscan/CMakeLists.txt | 1 - drivers/kscan/Kconfig | 1 - tests/drivers/build_all/input/app.overlay | 7 + 9 files changed, 82 insertions(+), 118 deletions(-) rename drivers/{kscan => input}/Kconfig.cst816s (74%) rename drivers/{kscan/kscan_cst816s.c => input/input_cst816s.c} (62%) diff --git a/boards/arm/pinetime_devkit0/Kconfig.defconfig b/boards/arm/pinetime_devkit0/Kconfig.defconfig index 10855a7893..0edb704b90 100644 --- a/boards/arm/pinetime_devkit0/Kconfig.defconfig +++ b/boards/arm/pinetime_devkit0/Kconfig.defconfig @@ -11,6 +11,9 @@ config BOARD config BT_CTLR default BT +config INPUT + default y if KSCAN + if FLASH config SPI_NOR_FLASH_LAYOUT_PAGE_SIZE diff --git a/boards/arm/pinetime_devkit0/pinetime_devkit0.dts b/boards/arm/pinetime_devkit0/pinetime_devkit0.dts index 55b16ecc79..e2817fbf0c 100644 --- a/boards/arm/pinetime_devkit0/pinetime_devkit0.dts +++ b/boards/arm/pinetime_devkit0/pinetime_devkit0.dts @@ -25,7 +25,7 @@ zephyr,flash = &flash0; zephyr,code-partition = &slot0_partition; zephyr,display = &st7789v; - zephyr,keyboard-scan = &cst816s; + zephyr,keyboard-scan = &kscan_input; }; aliases { @@ -34,7 +34,7 @@ led2 = &blled2; /* backlight high */ led3 = &statusled; /* status led, may be not populated */ sw0 = &key_in; /* key in */ - kscan0 = &cst816s; + kscan0 = &kscan_input; watchdog0 = &wdt0; }; @@ -128,6 +128,10 @@ reg = <0x15>; irq-gpios = <&gpio0 28 GPIO_ACTIVE_LOW>; rst-gpios = <&gpio0 10 GPIO_ACTIVE_LOW>; + + kscan_input: kscan-input { + compatible = "zephyr,kscan-input"; + }; }; }; diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index 3872f182b3..0f47dd4933 100644 --- a/drivers/input/CMakeLists.txt +++ b/drivers/input/CMakeLists.txt @@ -3,6 +3,7 @@ zephyr_library() zephyr_library_property(ALLOW_EMPTY TRUE) +zephyr_library_sources_ifdef(CONFIG_INPUT_CST816S input_cst816s.c) zephyr_library_sources_ifdef(CONFIG_INPUT_FT5336 input_ft5336.c) zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_KEYS input_gpio_keys.c) zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_QDEC input_gpio_qdec.c) diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index 226c9c9d6c..2be3cfcf63 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -5,6 +5,7 @@ if INPUT menu "Input drivers" +source "drivers/input/Kconfig.cst816s" source "drivers/input/Kconfig.ft5336" source "drivers/input/Kconfig.gpio_keys" source "drivers/input/Kconfig.gpio_qdec" diff --git a/drivers/kscan/Kconfig.cst816s b/drivers/input/Kconfig.cst816s similarity index 74% rename from drivers/kscan/Kconfig.cst816s rename to drivers/input/Kconfig.cst816s index 2524a0841c..c7cbc59e24 100644 --- a/drivers/kscan/Kconfig.cst816s +++ b/drivers/input/Kconfig.cst816s @@ -1,7 +1,7 @@ # Copyright (c) 2020 Qingsong Gou # SPDX-License-Identifier: Apache-2.0 -menuconfig KSCAN_CST816S +menuconfig INPUT_CST816S bool "CST816S capacitive touch panel driver" default y depends on DT_HAS_HYNITRON_CST816S_ENABLED @@ -9,20 +9,20 @@ menuconfig KSCAN_CST816S help Enable driver for hynitron cst816s touch panel. -if KSCAN_CST816S +if INPUT_CST816S -config KSCAN_CST816S_PERIOD +config INPUT_CST816S_PERIOD int "Sample period" - depends on !KSCAN_CST816S_INTERRUPT + depends on !INPUT_CST816S_INTERRUPT default 20 help Sample period in milliseconds when in polling mode. -config KSCAN_CST816S_INTERRUPT +config INPUT_CST816S_INTERRUPT bool "Interrupt support" default y depends on GPIO help Enable interrupt support (requires GPIO). -endif # KSCAN_CST816S +endif # INPUT_CST816S diff --git a/drivers/kscan/kscan_cst816s.c b/drivers/input/input_cst816s.c similarity index 62% rename from drivers/kscan/kscan_cst816s.c rename to drivers/input/input_cst816s.c index ee370f881d..5d22af3c51 100644 --- a/drivers/kscan/kscan_cst816s.c +++ b/drivers/input/input_cst816s.c @@ -7,14 +7,14 @@ #define DT_DRV_COMPAT hynitron_cst816s #include -#include +#include #include #include #include -LOG_MODULE_REGISTER(cst816s, CONFIG_KSCAN_LOG_LEVEL); +LOG_MODULE_REGISTER(cst816s, CONFIG_INPUT_LOG_LEVEL); -#define CST816S_CHIP_ID 0xB4 +#define CST816S_CHIP_ID 0xB4 #define CST816S_REG_DATA 0x00 #define CST816S_REG_GESTURE_ID 0x01 @@ -51,39 +51,38 @@ LOG_MODULE_REGISTER(cst816s, CONFIG_KSCAN_LOG_LEVEL); #define CST816S_REG_IOCTL 0xFD #define CST816S_REG_DIS_AUTO_SLEEP 0xFE -#define CST816S_MOTION_EN_CON_LR BIT(2) -#define CST816S_MOTION_EN_CON_UR BIT(1) -#define CST816S_MOTION_EN_DCLICK BIT(0) +#define CST816S_MOTION_EN_CON_LR BIT(2) +#define CST816S_MOTION_EN_CON_UR BIT(1) +#define CST816S_MOTION_EN_DCLICK BIT(0) -#define CST816S_IRQ_EN_TEST BIT(7) -#define CST816S_IRQ_EN_TOUCH BIT(6) -#define CST816S_IRQ_EN_CHANGE BIT(5) -#define CST816S_IRQ_EN_MOTION BIT(4) -#define CST816S_IRQ_ONCE_WLP BIT(0) +#define CST816S_IRQ_EN_TEST BIT(7) +#define CST816S_IRQ_EN_TOUCH BIT(6) +#define CST816S_IRQ_EN_CHANGE BIT(5) +#define CST816S_IRQ_EN_MOTION BIT(4) +#define CST816S_IRQ_ONCE_WLP BIT(0) -#define CST816S_IOCTL_SOFT_RTS BIT(2) -#define CST816S_IOCTL_IIC_OD BIT(1) -#define CST816S_IOCTL_EN_1V8 BIT(0) +#define CST816S_IOCTL_SOFT_RTS BIT(2) +#define CST816S_IOCTL_IIC_OD BIT(1) +#define CST816S_IOCTL_EN_1V8 BIT(0) -#define CST816S_POWER_MODE_SLEEP (0x03) -#define CST816S_POWER_MODE_EXPERIMENTAL (0x05) +#define CST816S_POWER_MODE_SLEEP (0x03) +#define CST816S_POWER_MODE_EXPERIMENTAL (0x05) -#define CST816S_EVENT_BITS_POS (0x06) +#define CST816S_EVENT_BITS_POS (0x06) -#define CST816S_RESET_DELAY (5) /* in ms */ -#define CST816S_WAIT_DELAY (50) /* in ms */ - -#define EVENT_PRESS_DOWN 0x00U -#define EVENT_LIFT_UP 0x01U -#define EVENT_CONTACT 0x02U -#define EVENT_NONE 0x03U +#define CST816S_RESET_DELAY (5) /* in ms */ +#define CST816S_WAIT_DELAY (50) /* in ms */ +#define EVENT_PRESS_DOWN 0x00U +#define EVENT_LIFT_UP 0x01U +#define EVENT_CONTACT 0x02U +#define EVENT_NONE 0x03U /** cst816s configuration (DT). */ struct cst816s_config { struct i2c_dt_spec i2c; const struct gpio_dt_spec rst_gpio; -#ifdef CONFIG_KSCAN_CST816S_INTERRUPT +#ifdef CONFIG_INPUT_CST816S_INTERRUPT const struct gpio_dt_spec int_gpio; #endif }; @@ -92,12 +91,10 @@ struct cst816s_config { struct cst816s_data { /** Device pointer. */ const struct device *dev; - /** KSCAN Callback. */ - kscan_callback_t callback; /** Work queue (for deferred read). */ struct k_work work; -#ifdef CONFIG_KSCAN_CST816S_INTERRUPT +#ifdef CONFIG_INPUT_CST816S_INTERRUPT /** Interrupt GPIO callback. */ struct gpio_callback int_gpio_cb; #else @@ -109,7 +106,6 @@ struct cst816s_data { static int cst816s_process(const struct device *dev) { const struct cst816s_config *cfg = dev->config; - struct cst816s_data *data = dev->data; int r; uint8_t event; @@ -137,9 +133,14 @@ static int cst816s_process(const struct device *dev) LOG_DBG("event: %d, row: %d, col: %d", event, row, col); - if (data->callback) { - data->callback(dev, row, col, pressed); + if (pressed) { + input_report_abs(dev, INPUT_ABS_X, col, false, K_FOREVER); + input_report_abs(dev, INPUT_ABS_Y, row, false, K_FOREVER); + input_report_key(dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER); + } else { + input_report_key(dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER); } + return r; } @@ -150,9 +151,8 @@ static void cst816s_work_handler(struct k_work *work) cst816s_process(data->dev); } -#ifdef CONFIG_KSCAN_CST816S_INTERRUPT -static void cst816s_isr_handler(const struct device *dev, - struct gpio_callback *cb, uint32_t pins) +#ifdef CONFIG_INPUT_CST816S_INTERRUPT +static void cst816s_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins) { struct cst816s_data *data = CONTAINER_OF(cb, struct cst816s_data, int_gpio_cb); @@ -167,60 +167,13 @@ static void cst816s_timer_handler(struct k_timer *timer) } #endif -static int cst816s_configure(const struct device *dev, - kscan_callback_t callback) -{ - struct cst816s_data *data = dev->data; - - if (!callback) { - LOG_ERR("Invalid callback (NULL)"); - return -EINVAL; - } - - data->callback = callback; - - return 0; -} - -static int cst816s_enable_callback(const struct device *dev) -{ - struct cst816s_data *data = dev->data; - -#ifdef CONFIG_KSCAN_CST816S_INTERRUPT - const struct cst816s_config *config = dev->config; - - gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb); -#else - k_timer_start(&data->timer, K_MSEC(CONFIG_KSCAN_CST816S_PERIOD), - K_MSEC(CONFIG_KSCAN_CST816S_PERIOD)); -#endif - - return 0; -} - -static int cst816s_disable_callback(const struct device *dev) -{ - struct cst816s_data *data = dev->data; - -#ifdef CONFIG_KSCAN_CST816S_INTERRUPT - const struct cst816s_config *config = dev->config; - - gpio_remove_callback(config->int_gpio.port, &data->int_gpio_cb); -#else - k_timer_stop(&data->timer); -#endif - - return 0; -} - static void cst816s_chip_reset(const struct device *dev) { const struct cst816s_config *config = dev->config; int ret; if (device_is_ready(config->rst_gpio.port)) { - ret = gpio_pin_configure_dt(&config->rst_gpio, - GPIO_OUTPUT_INACTIVE); + ret = gpio_pin_configure_dt(&config->rst_gpio, GPIO_OUTPUT_INACTIVE); if (ret < 0) { LOG_ERR("Could not configure reset GPIO pin"); return; @@ -255,8 +208,7 @@ static int cst816s_chip_init(const struct device *dev) return -ENODEV; } - ret = i2c_reg_update_byte_dt(&cfg->i2c, - CST816S_REG_IRQ_CTL, + ret = i2c_reg_update_byte_dt(&cfg->i2c, CST816S_REG_IRQ_CTL, CST816S_IRQ_EN_TOUCH | CST816S_IRQ_EN_CHANGE, CST816S_IRQ_EN_TOUCH | CST816S_IRQ_EN_CHANGE); if (ret < 0) { @@ -273,7 +225,7 @@ static int cst816s_init(const struct device *dev) data->dev = dev; k_work_init(&data->work, cst816s_work_handler); -#ifdef CONFIG_KSCAN_CST816S_INTERRUPT +#ifdef CONFIG_INPUT_CST816S_INTERRUPT const struct cst816s_config *config = dev->config; int ret; @@ -288,40 +240,38 @@ static int cst816s_init(const struct device *dev) return ret; } - ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, - GPIO_INT_EDGE_TO_ACTIVE); + ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE); if (ret < 0) { LOG_ERR("Could not configure interrupt GPIO interrupt."); return ret; } - gpio_init_callback(&data->int_gpio_cb, cst816s_isr_handler, - BIT(config->int_gpio.pin)); + gpio_init_callback(&data->int_gpio_cb, cst816s_isr_handler, BIT(config->int_gpio.pin)); + + ret = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb); + if (ret < 0) { + LOG_ERR("Could not set gpio callback"); + return ret; + } #else k_timer_init(&data->timer, cst816s_timer_handler, NULL); + k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_CST816S_PERIOD), + K_MSEC(CONFIG_INPUT_CST816S_PERIOD)); #endif return cst816s_chip_init(dev); } -static const struct kscan_driver_api cst816s_driver_api = { - .config = cst816s_configure, - .enable_callback = cst816s_enable_callback, - .disable_callback = cst816s_disable_callback, -}; - -#define CST816S_DEFINE(index) \ - static const struct cst816s_config cst816s_config_##index = { \ - .i2c = I2C_DT_SPEC_INST_GET(index), \ - COND_CODE_1(CONFIG_KSCAN_CST816S_INTERRUPT, \ - (.int_gpio = GPIO_DT_SPEC_INST_GET(index, irq_gpios),),\ - ()) \ - .rst_gpio = GPIO_DT_SPEC_INST_GET_OR(index, rst_gpios, {}), \ - }; \ - static struct cst816s_data cst816s_data_##index; \ - DEVICE_DT_INST_DEFINE(index, cst816s_init, NULL, \ - &cst816s_data_##index, &cst816s_config_##index, \ - POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \ - &cst816s_driver_api); +#define CST816S_DEFINE(index) \ + static const struct cst816s_config cst816s_config_##index = { \ + .i2c = I2C_DT_SPEC_INST_GET(index), \ + COND_CODE_1(CONFIG_INPUT_CST816S_INTERRUPT, \ + (.int_gpio = GPIO_DT_SPEC_INST_GET(index, irq_gpios),), ()) \ + .rst_gpio = GPIO_DT_SPEC_INST_GET_OR(index, rst_gpios, {}), \ + }; \ + static struct cst816s_data cst816s_data_##index; \ + DEVICE_DT_INST_DEFINE(index, cst816s_init, NULL, &cst816s_data_##index, \ + &cst816s_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \ + NULL); DT_INST_FOREACH_STATUS_OKAY(CST816S_DEFINE) diff --git a/drivers/kscan/CMakeLists.txt b/drivers/kscan/CMakeLists.txt index 2f27d65ff2..abec7d712c 100644 --- a/drivers/kscan/CMakeLists.txt +++ b/drivers/kscan/CMakeLists.txt @@ -7,7 +7,6 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_KSCAN_ITE_IT8XXX2 kscan_ite_it8xxx2.c) 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_INPUT kscan_input.c) diff --git a/drivers/kscan/Kconfig b/drivers/kscan/Kconfig index 4c08a2d888..d82067901c 100644 --- a/drivers/kscan/Kconfig +++ b/drivers/kscan/Kconfig @@ -13,7 +13,6 @@ if KSCAN source "drivers/kscan/Kconfig.it8xxx2" source "drivers/kscan/Kconfig.xec" source "drivers/kscan/Kconfig.ht16k33" -source "drivers/kscan/Kconfig.cst816s" source "drivers/kscan/Kconfig.cap1203" source "drivers/kscan/Kconfig.input" diff --git a/tests/drivers/build_all/input/app.overlay b/tests/drivers/build_all/input/app.overlay index 686da3a418..b5be8830bd 100644 --- a/tests/drivers/build_all/input/app.overlay +++ b/tests/drivers/build_all/input/app.overlay @@ -64,6 +64,13 @@ irq-gpios = <&gpio0 0 0>; reset-gpios = <&gpio0 0 0>; }; + + cst816s: cst816s@2 { + compatible = "hynitron,cst816s"; + reg = <0x2>; + irq-gpios = <&gpio0 0 0>; + rst-gpios = <&gpio0 0 0>; + }; }; spi@2 {