drivers: input: cap12xx, support 3 to 8 channels
The Microchip CAP12xx series are available in 3, 6 or 8 channel versions. Co-authored-by: Benjamin Cabé <kartben@gmail.com> Signed-off-by: Lothar Felten <lothar.felten@gmail.com>
This commit is contained in:
parent
058f107089
commit
c552379f0e
4 changed files with 140 additions and 114 deletions
|
@ -27,6 +27,18 @@ Boards
|
||||||
Devicetree
|
Devicetree
|
||||||
**********
|
**********
|
||||||
|
|
||||||
|
* The :dtcompatible:`microchip,cap1203` driver has changed its compatible to
|
||||||
|
:dtcompatible:`microchip,cap12xx` and has been updated to support multiple
|
||||||
|
channels.
|
||||||
|
The number of available channels is derived from the length of the devicetree
|
||||||
|
array property ``input-codes``.
|
||||||
|
The :kconfig:option:`CONFIG_INPUT_CAP1203_POLL` has been removed:
|
||||||
|
If the devicetree property ``int-gpios`` is present, interrupt mode is used
|
||||||
|
otherwise, polling is used.
|
||||||
|
The :kconfig:option:`CONFIG_INPUT_CAP1203_PERIOD` has been replaced with
|
||||||
|
the devicetree property ``poll-interval-ms``.
|
||||||
|
In interrupt mode, the devicetree property ``repeat`` is supported.
|
||||||
|
|
||||||
STM32
|
STM32
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,11 @@
|
||||||
# Copyright (c) 2022 Keiya Nobuta
|
# Copyright (c) 2022 Keiya Nobuta
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
menuconfig INPUT_CAP12XX
|
config INPUT_CAP12XX
|
||||||
bool "cap12xx 3-cannel capacitive touch sensor driver"
|
bool "Microchip CAP12xx multi-channel capacitive touch sensor driver"
|
||||||
default y
|
default y
|
||||||
depends on DT_HAS_MICROCHIP_CAP12XX_ENABLED
|
depends on DT_HAS_MICROCHIP_CAP12XX_ENABLED
|
||||||
select I2C
|
select I2C
|
||||||
help
|
help
|
||||||
Enable driver for microchip cap12xx 3-cannel capacitive
|
Enable driver for Microchip CAP12xx multi-channel capacitive
|
||||||
touch sensor.
|
touch sensor.
|
||||||
|
|
||||||
if INPUT_CAP12XX
|
|
||||||
|
|
||||||
config INPUT_CAP12XX_POLL
|
|
||||||
bool "Polling"
|
|
||||||
help
|
|
||||||
Enable polling mode when interrupt GPIO is not specified.
|
|
||||||
|
|
||||||
config INPUT_CAP1XX3_PERIOD
|
|
||||||
int "Sample period"
|
|
||||||
depends on INPUT_CAP12XX_POLL
|
|
||||||
default 10
|
|
||||||
help
|
|
||||||
Sample period in milliseconds when in polling mode.
|
|
||||||
|
|
||||||
endif # INPUT_CAP12XX
|
|
||||||
|
|
|
@ -4,45 +4,45 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define DT_DRV_COMPAT microchip_cap1203
|
#define DT_DRV_COMPAT microchip_cap12xx
|
||||||
|
|
||||||
#include <zephyr/drivers/i2c.h>
|
#include <zephyr/drivers/i2c.h>
|
||||||
#include <zephyr/drivers/gpio.h>
|
#include <zephyr/drivers/gpio.h>
|
||||||
#include <zephyr/input/input.h>
|
#include <zephyr/input/input.h>
|
||||||
|
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
LOG_MODULE_REGISTER(cap1203, CONFIG_INPUT_LOG_LEVEL);
|
LOG_MODULE_REGISTER(cap12xx, CONFIG_INPUT_LOG_LEVEL);
|
||||||
|
|
||||||
#define REG_MAIN_CONTROL 0x0
|
#define REG_MAIN_CONTROL 0x00
|
||||||
#define CONTROL_INT 0x1
|
#define CONTROL_INT 0x01
|
||||||
|
|
||||||
#define REG_INPUT_STATUS 0x03
|
#define REG_INPUT_STATUS 0x03
|
||||||
|
|
||||||
#define REG_INTERRUPT_ENABLE 0x27
|
#define REG_INTERRUPT_ENABLE 0x27
|
||||||
#define INTERRUPT_ENABLE 0x7
|
#define INTERRUPT_ENABLE 0xFF
|
||||||
#define INTERRUPT_DISABLE 0x0
|
#define INTERRUPT_DISABLE 0x00
|
||||||
|
|
||||||
#define TOUCH_INPUT_COUNT 3
|
#define REG_REPEAT_ENABLE 0x28
|
||||||
|
#define REPEAT_ENABLE 0xFF
|
||||||
|
#define REPEAT_DISABLE 0x00
|
||||||
|
|
||||||
struct cap1203_config {
|
struct cap12xx_config {
|
||||||
struct i2c_dt_spec i2c;
|
struct i2c_dt_spec i2c;
|
||||||
struct gpio_dt_spec int_gpio;
|
const uint8_t input_channels;
|
||||||
const uint16_t *input_codes;
|
const uint16_t *input_codes;
|
||||||
|
struct gpio_dt_spec *int_gpio;
|
||||||
|
bool repeat;
|
||||||
|
const uint16_t poll_interval_ms;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct cap1203_data {
|
struct cap12xx_data {
|
||||||
const struct device *dev;
|
const struct device *dev;
|
||||||
struct k_work work;
|
struct k_work work;
|
||||||
/* Interrupt GPIO callback. */
|
|
||||||
struct gpio_callback int_gpio_cb;
|
|
||||||
uint8_t prev_input_state;
|
uint8_t prev_input_state;
|
||||||
#ifdef CONFIG_INPUT_CAP1203_POLL
|
struct gpio_callback int_gpio_cb;
|
||||||
/* Timer (polling mode). */
|
struct k_timer poll_timer;
|
||||||
struct k_timer timer;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int cap1203_clear_interrupt(const struct i2c_dt_spec *i2c)
|
static int cap12xx_clear_interrupt(const struct i2c_dt_spec *i2c)
|
||||||
{
|
{
|
||||||
uint8_t ctrl;
|
uint8_t ctrl;
|
||||||
int r;
|
int r;
|
||||||
|
@ -56,77 +56,79 @@ static int cap1203_clear_interrupt(const struct i2c_dt_spec *i2c)
|
||||||
return i2c_reg_write_byte_dt(i2c, REG_MAIN_CONTROL, ctrl);
|
return i2c_reg_write_byte_dt(i2c, REG_MAIN_CONTROL, ctrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cap1203_enable_interrupt(const struct i2c_dt_spec *i2c, bool enable)
|
static int cap12xx_enable_interrupt(const struct i2c_dt_spec *i2c, bool enable)
|
||||||
{
|
{
|
||||||
uint8_t intr = enable ? INTERRUPT_ENABLE : INTERRUPT_DISABLE;
|
uint8_t intr = enable ? INTERRUPT_ENABLE : INTERRUPT_DISABLE;
|
||||||
|
|
||||||
return i2c_reg_write_byte_dt(i2c, REG_INTERRUPT_ENABLE, intr);
|
return i2c_reg_write_byte_dt(i2c, REG_INTERRUPT_ENABLE, intr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cap1203_process(const struct device *dev)
|
static int cap12xx_process(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct cap1203_config *config = dev->config;
|
const struct cap12xx_config *config = dev->config;
|
||||||
struct cap1203_data *data = dev->data;
|
struct cap12xx_data *data = dev->data;
|
||||||
int r;
|
int r;
|
||||||
uint8_t input;
|
uint8_t input_state;
|
||||||
uint8_t single_input_state;
|
|
||||||
|
|
||||||
r = i2c_reg_read_byte_dt(&config->i2c, REG_INPUT_STATUS, &input);
|
|
||||||
if (r < 0) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < TOUCH_INPUT_COUNT; i++) {
|
|
||||||
single_input_state = input & BIT(i);
|
|
||||||
if (single_input_state != (data->prev_input_state & BIT(i))) {
|
|
||||||
input_report_key(dev, config->input_codes[i], single_input_state, true,
|
|
||||||
K_FOREVER);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data->prev_input_state = input;
|
|
||||||
|
|
||||||
LOG_DBG("event: input: %d\n", input);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Clear INT bit to clear SENSOR INPUT STATUS bits.
|
* Clear INT bit to clear SENSOR INPUT STATUS bits.
|
||||||
* Note that this is also required in polling mode.
|
* Note that this is also required in polling mode.
|
||||||
*/
|
*/
|
||||||
r = cap1203_clear_interrupt(&config->i2c);
|
r = cap12xx_clear_interrupt(&config->i2c);
|
||||||
|
|
||||||
|
if (r < 0) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
r = i2c_reg_read_byte_dt(&config->i2c, REG_INPUT_STATUS, &input_state);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config->int_gpio == NULL) {
|
||||||
|
if (data->prev_input_state == input_state) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < config->input_channels; i++) {
|
||||||
|
if (input_state & BIT(i)) {
|
||||||
|
input_report_key(dev, config->input_codes[i], input_state & BIT(i), true,
|
||||||
|
K_FOREVER);
|
||||||
|
} else if (data->prev_input_state & BIT(i)) {
|
||||||
|
input_report_key(dev, config->input_codes[i], input_state & BIT(i), true,
|
||||||
|
K_FOREVER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data->prev_input_state = input_state;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cap1203_work_handler(struct k_work *work)
|
static void cap12xx_work_handler(struct k_work *work)
|
||||||
{
|
{
|
||||||
struct cap1203_data *data = CONTAINER_OF(work, struct cap1203_data, work);
|
struct cap12xx_data *data = CONTAINER_OF(work, struct cap12xx_data, work);
|
||||||
|
|
||||||
cap1203_process(data->dev);
|
cap12xx_process(data->dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cap1203_isr_handler(const struct device *dev,
|
static void cap12xx_timer_handler(struct k_timer *poll_timer)
|
||||||
struct gpio_callback *cb, uint32_t pins)
|
|
||||||
{
|
{
|
||||||
struct cap1203_data *data = CONTAINER_OF(cb, struct cap1203_data, int_gpio_cb);
|
struct cap12xx_data *data = CONTAINER_OF(poll_timer, struct cap12xx_data, poll_timer);
|
||||||
|
|
||||||
k_work_submit(&data->work);
|
k_work_submit(&data->work);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_INPUT_CAP1203_POLL
|
static void cap12xx_isr_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
|
||||||
static void cap1203_timer_handler(struct k_timer *timer)
|
|
||||||
{
|
{
|
||||||
struct cap1203_data *data = CONTAINER_OF(timer, struct cap1203_data, timer);
|
struct cap12xx_data *data = CONTAINER_OF(cb, struct cap12xx_data, int_gpio_cb);
|
||||||
|
|
||||||
k_work_submit(&data->work);
|
k_work_submit(&data->work);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
static int cap1203_init(const struct device *dev)
|
static int cap12xx_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct cap1203_config *config = dev->config;
|
const struct cap12xx_config *config = dev->config;
|
||||||
struct cap1203_data *data = dev->data;
|
struct cap12xx_data *data = dev->data;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (!device_is_ready(config->i2c.bus)) {
|
if (!device_is_ready(config->i2c.bus)) {
|
||||||
|
@ -136,77 +138,93 @@ static int cap1203_init(const struct device *dev)
|
||||||
|
|
||||||
data->dev = dev;
|
data->dev = dev;
|
||||||
|
|
||||||
k_work_init(&data->work, cap1203_work_handler);
|
k_work_init(&data->work, cap12xx_work_handler);
|
||||||
|
|
||||||
if (config->int_gpio.port != NULL) {
|
if (config->int_gpio == NULL) {
|
||||||
if (!gpio_is_ready_dt(&config->int_gpio)) {
|
LOG_DBG("cap12xx driver in polling mode");
|
||||||
LOG_ERR("Interrupt GPIO controller device not ready");
|
k_timer_init(&data->poll_timer, cap12xx_timer_handler, NULL);
|
||||||
|
r = cap12xx_enable_interrupt(&config->i2c, true);
|
||||||
|
if (r < 0) {
|
||||||
|
LOG_ERR("Could not configure interrupt");
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
k_timer_start(&data->poll_timer, K_MSEC(config->poll_interval_ms),
|
||||||
|
K_MSEC(config->poll_interval_ms));
|
||||||
|
} else {
|
||||||
|
LOG_DBG("cap12xx driver in interrupt mode");
|
||||||
|
if (!gpio_is_ready_dt(config->int_gpio)) {
|
||||||
|
LOG_ERR("Interrupt GPIO controller device not ready (missing device tree "
|
||||||
|
"node?)");
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
|
r = gpio_pin_configure_dt(config->int_gpio, GPIO_INPUT);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not confighure interrupt GPIO pin");
|
LOG_ERR("Could not configure interrupt GPIO pin");
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = gpio_pin_interrupt_configure_dt(&config->int_gpio,
|
r = gpio_pin_interrupt_configure_dt(config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
GPIO_INT_EDGE_TO_ACTIVE);
|
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not configure interrupt GPIO interrupt");
|
LOG_ERR("Could not configure interrupt GPIO interrupt");
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_init_callback(&data->int_gpio_cb, cap1203_isr_handler,
|
gpio_init_callback(&data->int_gpio_cb, cap12xx_isr_handler,
|
||||||
BIT(config->int_gpio.pin));
|
BIT(config->int_gpio->pin));
|
||||||
|
|
||||||
r = gpio_add_callback(config->int_gpio.port, &data->int_gpio_cb);
|
r = gpio_add_callback_dt(config->int_gpio, &data->int_gpio_cb);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not set gpio callback");
|
LOG_ERR("Could not set gpio callback");
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = cap1203_clear_interrupt(&config->i2c);
|
r = cap12xx_clear_interrupt(&config->i2c);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not clear interrupt");
|
LOG_ERR("Could not clear interrupt");
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
r = cap12xx_enable_interrupt(&config->i2c, true);
|
||||||
r = cap1203_enable_interrupt(&config->i2c, true);
|
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not configure interrupt");
|
LOG_ERR("Could not configure interrupt");
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
if (config->repeat) {
|
||||||
#ifdef CONFIG_INPUT_CAP1203_POLL
|
r = i2c_reg_write_byte_dt(&config->i2c, REG_REPEAT_ENABLE, REPEAT_ENABLE);
|
||||||
else {
|
if (r < 0) {
|
||||||
k_timer_init(&data->timer, cap1203_timer_handler, NULL);
|
LOG_ERR("Could not disable repeated interrupts");
|
||||||
|
return r;
|
||||||
r = cap1203_enable_interrupt(&config->i2c, false);
|
}
|
||||||
if (r < 0) {
|
LOG_DBG("cap12xx enabled repeated interrupts");
|
||||||
LOG_ERR("Could not configure interrupt");
|
} else {
|
||||||
return r;
|
r = i2c_reg_write_byte_dt(&config->i2c, REG_REPEAT_ENABLE, REPEAT_DISABLE);
|
||||||
|
if (r < 0) {
|
||||||
|
LOG_ERR("Could not enable repeated interrupts");
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
LOG_DBG("cap12xx disabled repeated interrupts");
|
||||||
}
|
}
|
||||||
|
|
||||||
k_timer_start(&data->timer, K_MSEC(CONFIG_INPUT_CAP1203_PERIOD),
|
|
||||||
K_MSEC(CONFIG_INPUT_CAP1203_PERIOD));
|
|
||||||
}
|
}
|
||||||
#endif
|
LOG_DBG("%d channels configured", config->input_channels);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CAP1203_INIT(index) \
|
#define CAP12XX_INIT(index) \
|
||||||
static const uint16_t cap1203_input_codes_##inst[] = DT_INST_PROP(index, input_codes); \
|
IF_ENABLED(DT_INST_NODE_HAS_PROP(index, int_gpios), ( \
|
||||||
BUILD_ASSERT(DT_INST_PROP_LEN(index, input_codes) == TOUCH_INPUT_COUNT); \
|
static struct gpio_dt_spec cap12xx_int_gpio_##index = \
|
||||||
static const struct cap1203_config cap1203_config_##index = { \
|
GPIO_DT_SPEC_INST_GET(index, int_gpios);)) \
|
||||||
|
static const uint16_t cap12xx_input_codes_##index[] = DT_INST_PROP(index, input_codes); \
|
||||||
|
static const struct cap12xx_config cap12xx_config_##index = { \
|
||||||
.i2c = I2C_DT_SPEC_INST_GET(index), \
|
.i2c = I2C_DT_SPEC_INST_GET(index), \
|
||||||
.int_gpio = GPIO_DT_SPEC_INST_GET_OR(index, int_gpios, {0}), \
|
.input_channels = DT_INST_PROP_LEN(index, input_codes), \
|
||||||
.input_codes = cap1203_input_codes_##inst, \
|
.input_codes = cap12xx_input_codes_##index, \
|
||||||
}; \
|
IF_ENABLED(DT_INST_NODE_HAS_PROP(index, int_gpios), ( \
|
||||||
static struct cap1203_data cap1203_data_##index; \
|
.int_gpio = &cap12xx_int_gpio_##index,)) \
|
||||||
DEVICE_DT_INST_DEFINE(index, cap1203_init, NULL, &cap1203_data_##index, \
|
.repeat = DT_INST_PROP(index, repeat), \
|
||||||
&cap1203_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \
|
.poll_interval_ms = DT_INST_PROP_OR(index, poll_interval_ms, 10)}; \
|
||||||
|
static struct cap12xx_data cap12xx_data_##index; \
|
||||||
|
DEVICE_DT_INST_DEFINE(index, cap12xx_init, NULL, &cap12xx_data_##index, \
|
||||||
|
&cap12xx_config_##index, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, \
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
DT_INST_FOREACH_STATUS_OKAY(CAP1203_INIT)
|
DT_INST_FOREACH_STATUS_OKAY(CAP12XX_INIT)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Copyright (c) 2022 Keiya Nobuta
|
# Copyright (c) 2022 Keiya Nobuta
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
description: cap12xx 3-channel capacitive touch sensor
|
description: Microchip CAP12xx family of 3, 6 and 8-channel capacitive touch sensors.
|
||||||
|
|
||||||
compatible: "microchip,cap12xx"
|
compatible: "microchip,cap12xx"
|
||||||
|
|
||||||
|
@ -10,9 +10,21 @@ include: i2c-device.yaml
|
||||||
properties:
|
properties:
|
||||||
int-gpios:
|
int-gpios:
|
||||||
type: phandle-array
|
type: phandle-array
|
||||||
|
description: |
|
||||||
|
Interrupt GPIO when not using polling mode.
|
||||||
|
|
||||||
|
repeat:
|
||||||
|
type: boolean
|
||||||
|
description: |
|
||||||
|
Property to enable the interrupt repeat mode for prolonged touch events.
|
||||||
|
|
||||||
|
poll-interval-ms:
|
||||||
|
type: int
|
||||||
|
description: |
|
||||||
|
Polling interval in ms when not using interrupt mode.
|
||||||
|
|
||||||
input-codes:
|
input-codes:
|
||||||
type: array
|
type: array
|
||||||
required: true
|
required: true
|
||||||
description: |
|
description: |
|
||||||
array of input event key codes (INPUT_KEY_* or INPUT_BTN_*).
|
Array of input event key codes (INPUT_KEY_* or INPUT_BTN_*).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue