From d9fd292f4b9af24cb4c1f16ea73c3a857d6af42f Mon Sep 17 00:00:00 2001 From: Nicolas Goualard Date: Thu, 4 Apr 2024 00:30:00 +0200 Subject: [PATCH] drivers: input: CHSC6X Driver Added a driver for the CHSC6X chip used on the Seeed Studio Round Display for Xiao Signed-off-by: Nicolas Goualard --- drivers/input/CMakeLists.txt | 1 + drivers/input/Kconfig | 1 + drivers/input/Kconfig.chsc6x | 11 ++ drivers/input/input_chsc6x.c | 139 ++++++++++++++++++++++ dts/bindings/input/chipsemi,chsc6x.yaml | 13 ++ dts/bindings/vendor-prefixes.txt | 1 + tests/drivers/build_all/input/app.overlay | 6 + 7 files changed, 172 insertions(+) create mode 100644 drivers/input/Kconfig.chsc6x create mode 100644 drivers/input/input_chsc6x.c create mode 100644 dts/bindings/input/chipsemi,chsc6x.yaml diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index 3a5c901c44..4131cca963 100644 --- a/drivers/input/CMakeLists.txt +++ b/drivers/input/CMakeLists.txt @@ -9,6 +9,7 @@ zephyr_library_sources_ifdef(CONFIG_INPUT_ANALOG_AXIS input_analog_axis.c) zephyr_library_sources_ifdef(CONFIG_INPUT_ANALOG_AXIS_SETTINGS input_analog_axis_settings.c) zephyr_library_sources_ifdef(CONFIG_INPUT_CAP1203 input_cap1203.c) zephyr_library_sources_ifdef(CONFIG_INPUT_CF1133 input_cf1133.c) +zephyr_library_sources_ifdef(CONFIG_INPUT_CHSC6X input_chsc6x.c) zephyr_library_sources_ifdef(CONFIG_INPUT_CST816S input_cst816s.c) zephyr_library_sources_ifdef(CONFIG_INPUT_ESP32_TOUCH_SENSOR input_esp32_touch_sensor.c) zephyr_library_sources_ifdef(CONFIG_INPUT_FT5336 input_ft5336.c) diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index 72c6625849..2312767cea 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -10,6 +10,7 @@ source "drivers/input/Kconfig.adc_keys" source "drivers/input/Kconfig.analog_axis" source "drivers/input/Kconfig.cap1203" source "drivers/input/Kconfig.cf1133" +source "drivers/input/Kconfig.chsc6x" source "drivers/input/Kconfig.cst816s" source "drivers/input/Kconfig.esp32" source "drivers/input/Kconfig.evdev" diff --git a/drivers/input/Kconfig.chsc6x b/drivers/input/Kconfig.chsc6x new file mode 100644 index 0000000000..18ec25a503 --- /dev/null +++ b/drivers/input/Kconfig.chsc6x @@ -0,0 +1,11 @@ +# Copyright (c) 2024 Nicolas Goualard +# SPDX-License-Identifier: Apache-2.0 + +config INPUT_CHSC6X + bool "Use CHSC6X capacitive touch panel driver" + default y + depends on DT_HAS_CHIPSEMI_CHSC6X_ENABLED + select GPIO + select I2C + help + Enable out of tree driver for CHSC6X touch panel. diff --git a/drivers/input/input_chsc6x.c b/drivers/input/input_chsc6x.c new file mode 100644 index 0000000000..2ef6880725 --- /dev/null +++ b/drivers/input/input_chsc6x.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2024 Nicolas Goualard + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT chipsemi_chsc6x + +#include +#include +#include +#include +#include + +struct chsc6x_config { + struct i2c_dt_spec i2c; + const struct gpio_dt_spec int_gpio; +}; + +struct chsc6x_data { + const struct device *dev; + struct k_work work; + struct gpio_callback int_gpio_cb; +}; + +#define CHSC6X_READ_ADDR 0 +#define CHSC6X_READ_LENGTH 5 +#define CHSC6X_OUTPUT_POINTS_PRESSED 0 +#define CHSC6X_OUTPUT_COL 2 +#define CHSC6X_OUTPUT_ROW 4 + +LOG_MODULE_REGISTER(chsc6x, CONFIG_INPUT_LOG_LEVEL); + +static int chsc6x_process(const struct device *dev) +{ + uint8_t output[CHSC6X_READ_LENGTH]; + uint8_t row, col; + bool is_pressed; + int ret; + + const struct chsc6x_config *cfg = dev->config; + + ret = i2c_burst_read_dt(&cfg->i2c, CHSC6X_READ_ADDR, output, CHSC6X_READ_LENGTH); + if (ret < 0) { + LOG_ERR("Could not read data: %i", ret); + return -ENODATA; + } + + is_pressed = output[CHSC6X_OUTPUT_POINTS_PRESSED]; + col = output[CHSC6X_OUTPUT_COL]; + row = output[CHSC6X_OUTPUT_ROW]; + + if (is_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 0; +} + +static void chsc6x_work_handler(struct k_work *work) +{ + struct chsc6x_data *data = CONTAINER_OF(work, struct chsc6x_data, work); + + chsc6x_process(data->dev); +} + +static void chsc6x_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t mask) +{ + struct chsc6x_data *data = CONTAINER_OF(cb, struct chsc6x_data, int_gpio_cb); + + k_work_submit(&data->work); +} + +static int chsc6x_chip_init(const struct device *dev) +{ + const struct chsc6x_config *cfg = dev->config; + + if (!i2c_is_ready_dt(&cfg->i2c)) { + LOG_ERR("I2C bus %s not ready", cfg->i2c.bus->name); + return -ENODEV; + } + + return 0; +} + +static int chsc6x_init(const struct device *dev) +{ + struct chsc6x_data *data = dev->data; + int ret; + + data->dev = dev; + + k_work_init(&data->work, chsc6x_work_handler); + + const struct chsc6x_config *config = dev->config; + + if (!gpio_is_ready_dt(&config->int_gpio)) { + LOG_ERR("GPIO port %s not ready", config->int_gpio.port->name); + return -ENODEV; + } + + ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT); + if (ret < 0) { + LOG_ERR("Could not configure interrupt GPIO pin: %d", ret); + return ret; + } + + 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: %d", ret); + return ret; + } + + gpio_init_callback(&data->int_gpio_cb, chsc6x_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: %d", ret); + return ret; + } + + return chsc6x_chip_init(dev); +}; + +#define CHSC6X_DEFINE(index) \ + static const struct chsc6x_config chsc6x_config_##index = { \ + .i2c = I2C_DT_SPEC_INST_GET(index), \ + .int_gpio = GPIO_DT_SPEC_INST_GET(index, irq_gpios), \ + }; \ + static struct chsc6x_data chsc6x_data_##index; \ + DEVICE_DT_INST_DEFINE(index, chsc6x_init, NULL, &chsc6x_data_##index, \ + &chsc6x_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \ + NULL); + +DT_INST_FOREACH_STATUS_OKAY(CHSC6X_DEFINE) diff --git a/dts/bindings/input/chipsemi,chsc6x.yaml b/dts/bindings/input/chipsemi,chsc6x.yaml new file mode 100644 index 0000000000..9f491bd3be --- /dev/null +++ b/dts/bindings/input/chipsemi,chsc6x.yaml @@ -0,0 +1,13 @@ +description: CHSC6X touchscreen sensor + +compatible: "chipsemi,chsc6x" + +include: i2c-device.yaml + +properties: + irq-gpios: + type: phandle-array + required: true + description: | + Interrupt GPIO. Used by the controller to signal touch data is + available. Active low. diff --git a/dts/bindings/vendor-prefixes.txt b/dts/bindings/vendor-prefixes.txt index fe3d98c90f..63e7186781 100644 --- a/dts/bindings/vendor-prefixes.txt +++ b/dts/bindings/vendor-prefixes.txt @@ -127,6 +127,7 @@ checkpoint Check Point Software Technologies Ltd. chefree Chefree Technology Corp. chipidea Chipidea, Inc chipone ChipOne +chipsemi Chipsemi Corp. chipspark ChipSPARK chrontel Chrontel, Inc. chrp Common Hardware Reference Platform diff --git a/tests/drivers/build_all/input/app.overlay b/tests/drivers/build_all/input/app.overlay index a5c3c0aff5..93e7bf1c14 100644 --- a/tests/drivers/build_all/input/app.overlay +++ b/tests/drivers/build_all/input/app.overlay @@ -218,6 +218,12 @@ int-gpios = <&test_gpio 0 0>; }; + chsc6x@7 { + compatible = "chipsemi,chsc6x"; + reg = <0x7>; + irq-gpios = <&test_gpio 0 0>; + }; + pinnacle@2a { compatible = "cirque,pinnacle"; reg = <0x2a>;