zmk_mf68/app/drivers/zephyr/ec11_trigger.c

177 lines
3.8 KiB
C
Raw Normal View History

2020-07-18 12:43:19 +10:00
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT alps_ec11
2020-07-18 12:43:19 +10:00
#include <device.h>
#include <drivers/gpio.h>
#include <sys/util.h>
#include <kernel.h>
#include <drivers/sensor.h>
#include "ec11.h"
2020-07-18 12:43:19 +10:00
extern struct ec11_data ec11_driver;
2020-07-18 12:43:19 +10:00
#include <logging/log.h>
LOG_MODULE_DECLARE(EC11, CONFIG_SENSOR_LOG_LEVEL);
2020-07-18 12:43:19 +10:00
static inline void setup_int(struct device *dev,
bool enable)
{
struct ec11_data *data = dev->driver_data;
const struct ec11_config *cfg = dev->config_info;
2020-07-18 12:43:19 +10:00
LOG_DBG("enabled %s", (enable ? "true" : "false"));
if (gpio_pin_interrupt_configure(data->a,
cfg->a_pin,
enable
? GPIO_INT_EDGE_BOTH
: GPIO_INT_DISABLE)) {
LOG_WRN("Unable to set A pin GPIO interrupt");
}
if (gpio_pin_interrupt_configure(data->b,
cfg->b_pin,
enable
? GPIO_INT_EDGE_BOTH
: GPIO_INT_DISABLE)) {
LOG_WRN("Unable to set A pin GPIO interrupt");
}
}
static void ec11_a_gpio_callback(struct device *dev,
2020-07-18 12:43:19 +10:00
struct gpio_callback *cb, u32_t pins)
{
struct ec11_data *drv_data =
CONTAINER_OF(cb, struct ec11_data, a_gpio_cb);
2020-07-18 12:43:19 +10:00
LOG_DBG("");
setup_int(drv_data->dev, false);
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
2020-07-18 12:43:19 +10:00
k_sem_give(&drv_data->gpio_sem);
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
2020-07-18 12:43:19 +10:00
k_work_submit(&drv_data->work);
#endif
}
static void ec11_b_gpio_callback(struct device *dev,
2020-07-18 12:43:19 +10:00
struct gpio_callback *cb, u32_t pins)
{
struct ec11_data *drv_data =
CONTAINER_OF(cb, struct ec11_data, b_gpio_cb);
2020-07-18 12:43:19 +10:00
LOG_DBG("");
setup_int(drv_data->dev, false);
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
2020-07-18 12:43:19 +10:00
k_sem_give(&drv_data->gpio_sem);
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
2020-07-18 12:43:19 +10:00
k_work_submit(&drv_data->work);
#endif
}
static void ec11_thread_cb(void *arg)
2020-07-18 12:43:19 +10:00
{
struct device *dev = arg;
struct ec11_data *drv_data = dev->driver_data;
2020-07-18 12:43:19 +10:00
2020-07-22 16:13:52 +10:00
drv_data->handler(dev, drv_data->trigger);
2020-07-18 12:43:19 +10:00
setup_int(dev, true);
}
#ifdef CONFIG_EC11_TRIGGER_OWN_THREAD
static void ec11_thread(int dev_ptr, int unused)
2020-07-18 12:43:19 +10:00
{
struct device *dev = INT_TO_POINTER(dev_ptr);
struct ec11_data *drv_data = dev->driver_data;
2020-07-18 12:43:19 +10:00
ARG_UNUSED(unused);
while (1) {
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
ec11_thread_cb(dev);
2020-07-18 12:43:19 +10:00
}
}
#endif
#ifdef CONFIG_EC11_TRIGGER_GLOBAL_THREAD
static void ec11_work_cb(struct k_work *work)
2020-07-18 12:43:19 +10:00
{
struct ec11_data *drv_data =
CONTAINER_OF(work, struct ec11_data, work);
2020-07-18 12:43:19 +10:00
LOG_DBG("");
ec11_thread_cb(drv_data->dev);
2020-07-18 12:43:19 +10:00
}
#endif
int ec11_trigger_set(struct device *dev,
2020-07-18 12:43:19 +10:00
const struct sensor_trigger *trig,
sensor_trigger_handler_t handler)
{
struct ec11_data *drv_data = dev->driver_data;
2020-07-18 12:43:19 +10:00
setup_int(dev, false);
k_msleep(5);
2020-07-22 16:13:52 +10:00
drv_data->trigger = trig;
2020-07-18 12:43:19 +10:00
drv_data->handler = handler;
setup_int(dev, true);
return 0;
}
int ec11_init_interrupt(struct device *dev)
2020-07-18 12:43:19 +10:00
{
struct ec11_data *drv_data = dev->driver_data;
const struct ec11_config *drv_cfg = dev->config_info;
2020-07-18 12:43:19 +10:00
2020-07-21 00:27:44 +10:00
drv_data->dev = dev;
2020-07-18 12:43:19 +10:00
/* setup gpio interrupt */
gpio_init_callback(&drv_data->a_gpio_cb,
ec11_a_gpio_callback,
2020-07-18 12:43:19 +10:00
BIT(drv_cfg->a_pin));
if (gpio_add_callback(drv_data->a, &drv_data->a_gpio_cb) < 0) {
LOG_DBG("Failed to set A callback!");
return -EIO;
}
gpio_init_callback(&drv_data->b_gpio_cb,
ec11_b_gpio_callback,
2020-07-18 12:43:19 +10:00
BIT(drv_cfg->b_pin));
if (gpio_add_callback(drv_data->b, &drv_data->b_gpio_cb) < 0) {
LOG_DBG("Failed to set B callback!");
return -EIO;
}
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
2020-07-18 12:43:19 +10:00
k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
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),
2020-07-18 12:43:19 +10:00
0, K_NO_WAIT);
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
k_work_init(&drv_data->work, ec11_work_cb);
2020-07-18 12:43:19 +10:00
#endif
return 0;
}