From 1d56b8e2aaba9fb0783b576ba2ca08e1b1c68a18 Mon Sep 17 00:00:00 2001 From: Fabian Blatz Date: Sun, 16 Jul 2023 19:29:40 +0200 Subject: [PATCH] input: convert cap1203 from kscan Convert the CAP1203 driver to the input subsystem, add to build_all tests. Signed-off-by: Fabian Blatz --- drivers/input/CMakeLists.txt | 1 + drivers/input/Kconfig | 1 + drivers/{kscan => input}/Kconfig.cap1203 | 12 +- .../kscan_cap1203.c => input/input_cap1203.c} | 115 ++++++------------ drivers/kscan/CMakeLists.txt | 1 - drivers/kscan/Kconfig | 1 - dts/bindings/kscan/microchip,cap1203.yaml | 2 +- tests/drivers/build_all/input/app.overlay | 6 + 8 files changed, 51 insertions(+), 88 deletions(-) rename drivers/{kscan => input}/Kconfig.cap1203 (75%) rename drivers/{kscan/kscan_cap1203.c => input/input_cap1203.c} (71%) diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index 0f47dd4933..51b2515591 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_CAP1203 input_cap1203.c) 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) diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index 2be3cfcf63..3aa5271bf4 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -5,6 +5,7 @@ if INPUT menu "Input drivers" +source "drivers/input/Kconfig.cap1203" source "drivers/input/Kconfig.cst816s" source "drivers/input/Kconfig.ft5336" source "drivers/input/Kconfig.gpio_keys" diff --git a/drivers/kscan/Kconfig.cap1203 b/drivers/input/Kconfig.cap1203 similarity index 75% rename from drivers/kscan/Kconfig.cap1203 rename to drivers/input/Kconfig.cap1203 index 2d184050ea..28a87b6c70 100644 --- a/drivers/kscan/Kconfig.cap1203 +++ b/drivers/input/Kconfig.cap1203 @@ -1,7 +1,7 @@ # Copyright (c) 2022 Keiya Nobuta # SPDX-License-Identifier: Apache-2.0 -menuconfig KSCAN_CAP1203 +menuconfig INPUT_CAP1203 bool "CAP1203 3-cannel capacitive touch sensor driver" default y depends on DT_HAS_MICROCHIP_CAP1203_ENABLED @@ -10,18 +10,18 @@ menuconfig KSCAN_CAP1203 Enable driver for microchip CAP1203 3-cannel capacitive touch sensor. -if KSCAN_CAP1203 +if INPUT_CAP1203 -config KSCAN_CAP1203_POLL +config INPUT_CAP1203_POLL bool "Polling" help Enable polling mode when interrupt GPIO is not specified. -config KSCAN_CAP1203_PERIOD +config INPUT_CAP1203_PERIOD int "Sample period" - depends on KSCAN_CAP1203_POLL + depends on INPUT_CAP1203_POLL default 10 help Sample period in milliseconds when in polling mode. -endif # KSCAN_CAP1203 +endif # INPUT_CAP1203 diff --git a/drivers/kscan/kscan_cap1203.c b/drivers/input/input_cap1203.c similarity index 71% rename from drivers/kscan/kscan_cap1203.c rename to drivers/input/input_cap1203.c index 798193769c..9524b12f56 100644 --- a/drivers/kscan/kscan_cap1203.c +++ b/drivers/input/input_cap1203.c @@ -6,12 +6,12 @@ #define DT_DRV_COMPAT microchip_cap1203 -#include #include #include +#include #include -LOG_MODULE_REGISTER(cap1203, CONFIG_KSCAN_LOG_LEVEL); +LOG_MODULE_REGISTER(cap1203, CONFIG_INPUT_LOG_LEVEL); #define REG_MAIN_CONTROL 0x0 #define CONTROL_INT 0x1 @@ -28,12 +28,11 @@ struct cap1203_config { }; struct cap1203_data { - struct device *dev; - kscan_callback_t callback; + const struct device *dev; struct k_work work; /* Interrupt GPIO callback. */ struct gpio_callback int_gpio_cb; -#ifdef CONFIG_KSCAN_CAP1203_POLL +#ifdef CONFIG_INPUT_CAP1203_POLL /* Timer (polling mode). */ struct k_timer timer; #endif @@ -96,7 +95,13 @@ static int cap1203_process(const struct device *dev) return r; } - data->callback(dev, 0, col, pressed); + if (pressed) { + input_report_abs(dev, INPUT_ABS_X, col, false, K_FOREVER); + input_report_abs(dev, INPUT_ABS_Y, 0, 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 0; } @@ -116,7 +121,7 @@ static void cap1203_isr_handler(const struct device *dev, k_work_submit(&data->work); } -#ifdef CONFIG_KSCAN_CAP1203_POLL +#ifdef CONFIG_INPUT_CAP1203_POLL static void cap1203_timer_handler(struct k_timer *timer) { struct cap1203_data *data = CONTAINER_OF(timer, struct cap1203_data, timer); @@ -125,69 +130,6 @@ static void cap1203_timer_handler(struct k_timer *timer) } #endif -static int cap1203_configure(const struct device *dev, - kscan_callback_t callback) -{ - struct cap1203_data *data = dev->data; - const struct cap1203_config *config = dev->config; - - data->callback = callback; - - if (config->int_gpio.port != NULL) { - int r; - - /* Clear pending interrupt */ - r = cap1203_clear_interrupt(&config->i2c); - if (r < 0) { - LOG_ERR("Could not clear interrupt"); - return r; - } - - r = cap1203_enable_interrupt(&config->i2c, true); - if (r < 0) { - LOG_ERR("Could not configure interrupt"); - return r; - } - } - - return 0; -} - -static int cap1203_enable_callback(const struct device *dev) -{ - struct cap1203_data *data = dev->data; - - const struct cap1203_config *config = dev->config; - - if (config->int_gpio.port != NULL) { - gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb); - } -#ifdef CONFIG_KSCAN_CAP1203_POLL - else { - k_timer_start(&data->timer, K_MSEC(CONFIG_KSCAN_CAP1203_PERIOD), - K_MSEC(CONFIG_KSCAN_CAP1203_PERIOD)); - } -#endif - return 0; -} - -static int cap1203_disable_callback(const struct device *dev) -{ - struct cap1203_data *data = dev->data; - - const struct cap1203_config *config = dev->config; - - if (config->int_gpio.port != NULL) { - gpio_remove_callback(config->int_gpio.port, &data->int_gpio_cb); - } -#ifdef CONFIG_KSCAN_CAP1203_POLL - else { - k_timer_stop(&data->timer); - } -#endif - return 0; -} - static int cap1203_init(const struct device *dev) { const struct cap1203_config *config = dev->config; @@ -224,8 +166,26 @@ static int cap1203_init(const struct device *dev) gpio_init_callback(&data->int_gpio_cb, cap1203_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; + } + + r = cap1203_clear_interrupt(&config->i2c); + if (r < 0) { + LOG_ERR("Could not clear interrupt"); + return r; + } + + r = cap1203_enable_interrupt(&config->i2c, true); + if (r < 0) { + LOG_ERR("Could not configure interrupt"); + return r; + } } -#ifdef CONFIG_KSCAN_CAP1203_POLL +#ifdef CONFIG_INPUT_CAP1203_POLL else { k_timer_init(&data->timer, cap1203_timer_handler, NULL); @@ -234,18 +194,15 @@ static int cap1203_init(const struct device *dev) LOG_ERR("Could not configure interrupt"); return r; } + + k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_CAP1203_PERIOD), + K_MSEC(CONFIG_INPUT_CAP1203_PERIOD)); } #endif return 0; } -static const struct kscan_driver_api cap1203_driver_api = { - .config = cap1203_configure, - .enable_callback = cap1203_enable_callback, - .disable_callback = cap1203_disable_callback, -}; - #define CAP1203_INIT(index) \ static const struct cap1203_config cap1203_config_##index = { \ .i2c = I2C_DT_SPEC_INST_GET(index), \ @@ -254,7 +211,7 @@ static const struct kscan_driver_api cap1203_driver_api = { static struct cap1203_data cap1203_data_##index; \ DEVICE_DT_INST_DEFINE(index, cap1203_init, NULL, \ &cap1203_data_##index, &cap1203_config_##index, \ - POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \ - &cap1203_driver_api); + POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \ + NULL); DT_INST_FOREACH_STATUS_OKAY(CAP1203_INIT) diff --git a/drivers/kscan/CMakeLists.txt b/drivers/kscan/CMakeLists.txt index abec7d712c..d650f17f91 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_CAP1203 kscan_cap1203.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 d82067901c..02ef0987cc 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.cap1203" source "drivers/kscan/Kconfig.input" module = KSCAN diff --git a/dts/bindings/kscan/microchip,cap1203.yaml b/dts/bindings/kscan/microchip,cap1203.yaml index cfa8b0158a..6a228041e9 100644 --- a/dts/bindings/kscan/microchip,cap1203.yaml +++ b/dts/bindings/kscan/microchip,cap1203.yaml @@ -5,7 +5,7 @@ description: CAP1203 3-channel capacitive touch sensor compatible: "microchip,cap1203" -include: [kscan.yaml, i2c-device.yaml] +include: i2c-device.yaml properties: int-gpios: diff --git a/tests/drivers/build_all/input/app.overlay b/tests/drivers/build_all/input/app.overlay index b5be8830bd..c1d8dd273c 100644 --- a/tests/drivers/build_all/input/app.overlay +++ b/tests/drivers/build_all/input/app.overlay @@ -71,6 +71,12 @@ irq-gpios = <&gpio0 0 0>; rst-gpios = <&gpio0 0 0>; }; + + cap1203@3 { + compatible = "microchip,cap1203"; + reg = <0x3>; + int-gpios = <&gpio0 0 0>; + }; }; spi@2 {