Skip to content

Commit

Permalink
input: convert xpt2046 from kscan
Browse files Browse the repository at this point in the history
Convert the XPT2046 driver to the input subsystem, change the api,
remove the callback and enable logic.

Signed-off-by: Fabio Baltieri <[email protected]>
  • Loading branch information
fabiobaltieri committed Jun 27, 2023
1 parent 82e5eba commit 9065c2d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 61 deletions.
1 change: 1 addition & 0 deletions drivers/input/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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.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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
77 changes: 20 additions & 57 deletions drivers/kscan/kscan_xpt2046.c → drivers/input/input_xpt2046.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

#define DT_DRV_COMPAT xptek_xpt2046

#include <zephyr/kernel.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/kscan.h>
#include <zephyr/input/input.h>
#include <zephyr/kernel.h>

#include <zephyr/logging/log.h>
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;
Expand All @@ -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];
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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( \
Expand All @@ -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), \
Expand Down
1 change: 0 additions & 1 deletion drivers/kscan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion drivers/kscan/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9065c2d

Please sign in to comment.