Skip to content

Commit

Permalink
drivers: input: CHSC6X Driver
Browse files Browse the repository at this point in the history
Added a driver for the CHSC6X chip used on the
Seeed Studio Round Display for Xiao

Signed-off-by: Nicolas Goualard <nicolas.goualard@sfr.fr>
  • Loading branch information
nicogou committed Apr 4, 2024
1 parent 80b3f15 commit cad2bfb
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/input/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions drivers/input/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions drivers/input/Kconfig.chsc6x
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2024 Nicolas Goualard <nicolas.goualard@sfr.fr>
# SPDX-License-Identifier: Apache-2.0

config INPUT_CHSC6X
bool "Use out of tree CHSC6X capacitive touch panel driver"
default y
depends on DT_HAS_SEEED_CHSC6X_ENABLED
select GPIO
select I2C
help
Enable out of tree driver for CHSC6X touch panel.
136 changes: 136 additions & 0 deletions drivers/input/input_chsc6x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2024 Nicolas Goualard <nicolas.goualard@sfr.fr>
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT seeed_chsc6x

#include <zephyr/sys/byteorder.h>
#include <zephyr/input/input.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>

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;
};

struct chsc6x_output {
uint8_t points;
uint16_t x;
uint16_t y;
} __packed;

LOG_MODULE_REGISTER(chsc6x, CONFIG_INPUT_LOG_LEVEL);

static int chsc6x_process(const struct device *dev)
{
uint16_t row, col;
bool is_pressed;

struct chsc6x_output output;
const struct chsc6x_config *cfg = dev->config;

int res = i2c_burst_read_dt(&cfg->i2c, 0, (uint8_t *)&output, sizeof(output));

if (res < 0) {
LOG_ERR("Could not read data %i", res);
return -ENODATA;
}

is_pressed = output.points & 0xff;
col = sys_be16_to_cpu(output.x) & 0x00ff;
row = sys_be16_to_cpu(output.y) & 0x00ff;

if (is_pressed) {
/* These events are generated for the LVGL touch implementation. */
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 {
/* This event is generated for the LVGL touch implementation. */
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 (!device_is_ready(cfg->i2c.bus)) {
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;

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 -EIO;
}

if (gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT) < 0) {
LOG_ERR("Could not configure interrupt GPIO pin");
return -EIO;
}

if (gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE) < 0) {
LOG_ERR("Could not configure interrupt GPIO interrupt.");
return -EIO;
}

gpio_init_callback(&data->int_gpio_cb, chsc6x_isr_handler, BIT(config->int_gpio.pin));

if (gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb) < 0) {
LOG_ERR("Could not set gpio callback");
return -EIO;
}

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)
13 changes: 13 additions & 0 deletions dts/bindings/input/seeed,chsc6x.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: CHSC6X touchscreen sensor

compatible: "seeed,chsc6x"

include: i2c-device.yaml

properties:
irq-gpios:
type: phandle-array
description: |
The irq signal defaults to active low as produced by the
sensor. The property value should ensure the flags properly
describe the signal that is presented to the driver.

0 comments on commit cad2bfb

Please sign in to comment.