2020-07-17 19:43:19 -07:00
|
|
|
/*
|
2020-09-09 20:57:44 -07:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-07-17 19:43:19 -07:00
|
|
|
*
|
2020-08-11 11:49:43 -07:00
|
|
|
* SPDX-License-Identifier: MIT
|
2020-07-17 19:43:19 -07:00
|
|
|
*/
|
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#define DT_DRV_COMPAT alps_ec11
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2022-10-07 21:46:08 -07:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/drivers/sensor.h>
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#include "ec11.h"
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
extern struct ec11_data ec11_driver;
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2022-10-07 21:46:08 -07:00
|
|
|
#include <zephyr/logging/log.h>
|
2020-07-20 17:54:58 -07:00
|
|
|
LOG_MODULE_DECLARE(EC11, CONFIG_SENSOR_LOG_LEVEL);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-12-10 11:31:51 -08:00
|
|
|
static inline void setup_int(const struct device *dev, bool enable) {
|
2020-11-28 12:03:37 -08:00
|
|
|
const struct ec11_config *cfg = dev->config;
|
2020-09-13 19:53:24 -07:00
|
|
|
|
|
|
|
LOG_DBG("enabled %s", (enable ? "true" : "false"));
|
|
|
|
|
2023-01-17 17:40:44 -08:00
|
|
|
if (gpio_pin_interrupt_configure_dt(&cfg->a, enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) {
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_WRN("Unable to set A pin GPIO interrupt");
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:40:44 -08:00
|
|
|
if (gpio_pin_interrupt_configure_dt(&cfg->b, enable ? GPIO_INT_EDGE_BOTH : GPIO_INT_DISABLE)) {
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_WRN("Unable to set A pin GPIO interrupt");
|
|
|
|
}
|
2020-07-17 19:43:19 -07:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:31:51 -08:00
|
|
|
static void ec11_a_gpio_callback(const struct device *dev, struct gpio_callback *cb,
|
|
|
|
uint32_t pins) {
|
2020-09-13 19:53:24 -07:00
|
|
|
struct ec11_data *drv_data = CONTAINER_OF(cb, struct ec11_data, a_gpio_cb);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_DBG("");
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
setup_int(drv_data->dev, false);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
2020-09-13 19:53:24 -07:00
|
|
|
k_sem_give(&drv_data->gpio_sem);
|
2020-07-20 17:54:58 -07:00
|
|
|
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
|
2020-09-13 19:53:24 -07:00
|
|
|
k_work_submit(&drv_data->work);
|
2020-07-17 19:43:19 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-10 11:31:51 -08:00
|
|
|
static void ec11_b_gpio_callback(const struct device *dev, struct gpio_callback *cb,
|
|
|
|
uint32_t pins) {
|
2020-09-13 19:53:24 -07:00
|
|
|
struct ec11_data *drv_data = CONTAINER_OF(cb, struct ec11_data, b_gpio_cb);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_DBG("");
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
setup_int(drv_data->dev, false);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
2020-09-13 19:53:24 -07:00
|
|
|
k_sem_give(&drv_data->gpio_sem);
|
2020-07-20 17:54:58 -07:00
|
|
|
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
|
2020-09-13 19:53:24 -07:00
|
|
|
k_work_submit(&drv_data->work);
|
2020-07-17 19:43:19 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:58:03 -08:00
|
|
|
static void ec11_thread_cb(const struct device *dev) {
|
2020-11-28 12:03:12 -08:00
|
|
|
struct ec11_data *drv_data = dev->data;
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
drv_data->handler(dev, drv_data->trigger);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
setup_int(dev, true);
|
2020-07-17 19:43:19 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#ifdef CONFIG_EC11_TRIGGER_OWN_THREAD
|
2020-09-13 19:53:24 -07:00
|
|
|
static void ec11_thread(int dev_ptr, int unused) {
|
2020-12-10 11:31:51 -08:00
|
|
|
const struct device *dev = INT_TO_POINTER(dev_ptr);
|
2020-11-28 12:03:12 -08:00
|
|
|
struct ec11_data *drv_data = dev->data;
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
ARG_UNUSED(unused);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
while (1) {
|
|
|
|
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
|
|
|
|
ec11_thread_cb(dev);
|
|
|
|
}
|
2020-07-17 19:43:19 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#ifdef CONFIG_EC11_TRIGGER_GLOBAL_THREAD
|
2020-09-13 19:53:24 -07:00
|
|
|
static void ec11_work_cb(struct k_work *work) {
|
|
|
|
struct ec11_data *drv_data = CONTAINER_OF(work, struct ec11_data, work);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_DBG("");
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
ec11_thread_cb(drv_data->dev);
|
2020-07-17 19:43:19 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-10 11:31:51 -08:00
|
|
|
int ec11_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
|
2020-09-13 19:53:24 -07:00
|
|
|
sensor_trigger_handler_t handler) {
|
2020-11-28 12:03:12 -08:00
|
|
|
struct ec11_data *drv_data = dev->data;
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
setup_int(dev, false);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
k_msleep(5);
|
2020-07-20 17:54:58 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
drv_data->trigger = trig;
|
|
|
|
drv_data->handler = handler;
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
setup_int(dev, true);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
return 0;
|
2020-07-17 19:43:19 -07:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:31:51 -08:00
|
|
|
int ec11_init_interrupt(const struct device *dev) {
|
2020-11-28 12:03:12 -08:00
|
|
|
struct ec11_data *drv_data = dev->data;
|
2020-11-28 12:03:37 -08:00
|
|
|
const struct ec11_config *drv_cfg = dev->config;
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
drv_data->dev = dev;
|
|
|
|
/* setup gpio interrupt */
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2023-01-17 17:40:44 -08:00
|
|
|
gpio_init_callback(&drv_data->a_gpio_cb, ec11_a_gpio_callback, BIT(drv_cfg->a.pin));
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2023-01-17 17:40:44 -08:00
|
|
|
if (gpio_add_callback(drv_cfg->a.port, &drv_data->a_gpio_cb) < 0) {
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_DBG("Failed to set A callback!");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2023-01-17 17:40:44 -08:00
|
|
|
gpio_init_callback(&drv_data->b_gpio_cb, ec11_b_gpio_callback, BIT(drv_cfg->b.pin));
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2023-01-17 17:40:44 -08:00
|
|
|
if (gpio_add_callback(drv_cfg->b.port, &drv_data->b_gpio_cb) < 0) {
|
2020-09-13 19:53:24 -07:00
|
|
|
LOG_DBG("Failed to set B callback!");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-07-20 17:54:58 -07:00
|
|
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
2020-09-13 19:53:24 -07:00
|
|
|
k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
|
2020-07-17 19:43:19 -07:00
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
k_thread_create(&drv_data->thread, drv_data->thread_stack, CONFIG_EC11_THREAD_STACK_SIZE,
|
|
|
|
(k_thread_entry_t)ec11_thread, dev, 0, NULL,
|
|
|
|
K_PRIO_COOP(CONFIG_EC11_THREAD_PRIORITY), 0, K_NO_WAIT);
|
2020-07-20 17:54:58 -07:00
|
|
|
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
|
2020-09-13 19:53:24 -07:00
|
|
|
k_work_init(&drv_data->work, ec11_work_cb);
|
2020-07-17 19:43:19 -07:00
|
|
|
#endif
|
|
|
|
|
2020-09-13 19:53:24 -07:00
|
|
|
return 0;
|
2020-07-17 19:43:19 -07:00
|
|
|
}
|