You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
4.3 KiB
194 lines
4.3 KiB
4 years ago
|
/*
|
||
|
* Copyright (c) 2016 Intel Corporation
|
||
|
*
|
||
|
* SPDX-License-Identifier: Apache-2.0
|
||
|
*/
|
||
|
|
||
4 years ago
|
#define DT_DRV_COMPAT alps_ec11
|
||
4 years ago
|
|
||
|
#include <device.h>
|
||
|
#include <drivers/gpio.h>
|
||
|
#include <sys/util.h>
|
||
|
#include <kernel.h>
|
||
|
#include <drivers/sensor.h>
|
||
|
|
||
4 years ago
|
#include "ec11.h"
|
||
4 years ago
|
|
||
4 years ago
|
extern struct ec11_data ec11_driver;
|
||
4 years ago
|
|
||
|
#include <logging/log.h>
|
||
4 years ago
|
LOG_MODULE_DECLARE(EC11, CONFIG_SENSOR_LOG_LEVEL);
|
||
4 years ago
|
|
||
|
static inline void setup_int(struct device *dev,
|
||
|
bool enable)
|
||
|
{
|
||
4 years ago
|
struct ec11_data *data = dev->driver_data;
|
||
|
const struct ec11_config *cfg = dev->config_info;
|
||
4 years ago
|
|
||
|
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");
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
static void ec11_a_gpio_callback(struct device *dev,
|
||
4 years ago
|
struct gpio_callback *cb, u32_t pins)
|
||
|
{
|
||
4 years ago
|
struct ec11_data *drv_data =
|
||
|
CONTAINER_OF(cb, struct ec11_data, a_gpio_cb);
|
||
4 years ago
|
|
||
|
LOG_DBG("");
|
||
|
|
||
|
setup_int(drv_data->dev, false);
|
||
|
|
||
4 years ago
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
||
4 years ago
|
k_sem_give(&drv_data->gpio_sem);
|
||
4 years ago
|
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
|
||
4 years ago
|
k_work_submit(&drv_data->work);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
4 years ago
|
static void ec11_b_gpio_callback(struct device *dev,
|
||
4 years ago
|
struct gpio_callback *cb, u32_t pins)
|
||
|
{
|
||
4 years ago
|
struct ec11_data *drv_data =
|
||
|
CONTAINER_OF(cb, struct ec11_data, b_gpio_cb);
|
||
4 years ago
|
|
||
|
LOG_DBG("");
|
||
|
|
||
|
setup_int(drv_data->dev, false);
|
||
|
|
||
4 years ago
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
||
4 years ago
|
k_sem_give(&drv_data->gpio_sem);
|
||
4 years ago
|
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
|
||
4 years ago
|
k_work_submit(&drv_data->work);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
4 years ago
|
static void ec11_thread_cb(void *arg)
|
||
4 years ago
|
{
|
||
|
struct device *dev = arg;
|
||
4 years ago
|
struct ec11_data *drv_data = dev->driver_data;
|
||
4 years ago
|
|
||
4 years ago
|
drv_data->handler(dev, &drv_data->trigger);
|
||
4 years ago
|
|
||
|
setup_int(dev, true);
|
||
|
}
|
||
|
|
||
4 years ago
|
#ifdef CONFIG_EC11_TRIGGER_OWN_THREAD
|
||
|
static void ec11_thread(int dev_ptr, int unused)
|
||
4 years ago
|
{
|
||
|
struct device *dev = INT_TO_POINTER(dev_ptr);
|
||
4 years ago
|
struct ec11_data *drv_data = dev->driver_data;
|
||
4 years ago
|
|
||
|
ARG_UNUSED(unused);
|
||
|
|
||
|
while (1) {
|
||
|
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
|
||
4 years ago
|
ec11_thread_cb(dev);
|
||
4 years ago
|
}
|
||
|
}
|
||
|
#endif
|
||
|
|
||
4 years ago
|
#ifdef CONFIG_EC11_TRIGGER_GLOBAL_THREAD
|
||
|
static void ec11_work_cb(struct k_work *work)
|
||
4 years ago
|
{
|
||
4 years ago
|
struct ec11_data *drv_data =
|
||
|
CONTAINER_OF(work, struct ec11_data, work);
|
||
4 years ago
|
|
||
|
LOG_DBG("");
|
||
|
|
||
4 years ago
|
ec11_thread_cb(drv_data->dev);
|
||
4 years ago
|
}
|
||
|
#endif
|
||
|
|
||
4 years ago
|
int ec11_trigger_set(struct device *dev,
|
||
4 years ago
|
const struct sensor_trigger *trig,
|
||
|
sensor_trigger_handler_t handler)
|
||
|
{
|
||
4 years ago
|
struct ec11_data *drv_data = dev->driver_data;
|
||
4 years ago
|
|
||
|
setup_int(dev, false);
|
||
|
|
||
4 years ago
|
k_msleep(5);
|
||
|
|
||
4 years ago
|
drv_data->trigger = *trig;
|
||
|
drv_data->handler = handler;
|
||
|
|
||
|
setup_int(dev, true);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
4 years ago
|
int ec11_init_interrupt(struct device *dev)
|
||
4 years ago
|
{
|
||
4 years ago
|
struct ec11_data *drv_data = dev->driver_data;
|
||
|
const struct ec11_config *drv_cfg = dev->config_info;
|
||
4 years ago
|
|
||
4 years ago
|
drv_data->dev = dev;
|
||
4 years ago
|
/* setup gpio interrupt */
|
||
|
|
||
|
LOG_DBG("A: %s %d B: %s %d", drv_cfg->a_label, drv_cfg->a_pin, drv_cfg->b_label, drv_cfg->b_pin);
|
||
|
|
||
4 years ago
|
if (gpio_pin_configure(drv_data->a, drv_cfg->a_pin,
|
||
4 years ago
|
drv_cfg->a_flags
|
||
4 years ago
|
| GPIO_INPUT)) {
|
||
|
LOG_DBG("Failed to configure B pin");
|
||
4 years ago
|
return -EIO;
|
||
|
}
|
||
|
|
||
|
gpio_init_callback(&drv_data->a_gpio_cb,
|
||
4 years ago
|
ec11_a_gpio_callback,
|
||
4 years ago
|
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;
|
||
|
}
|
||
|
|
||
4 years ago
|
if (gpio_pin_configure(drv_data->b, drv_cfg->b_pin,
|
||
4 years ago
|
drv_cfg->b_flags
|
||
4 years ago
|
| GPIO_INPUT)) {
|
||
|
LOG_DBG("Failed to configure B pin");
|
||
4 years ago
|
return -EIO;
|
||
|
}
|
||
|
|
||
|
gpio_init_callback(&drv_data->b_gpio_cb,
|
||
4 years ago
|
ec11_b_gpio_callback,
|
||
4 years ago
|
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;
|
||
|
}
|
||
|
|
||
|
LOG_DBG("A Pin? %d, B Pin? %d", gpio_pin_get(drv_data->a, drv_cfg->a_pin), gpio_pin_get(drv_data->b, drv_cfg->b_pin));
|
||
|
|
||
4 years ago
|
#if defined(CONFIG_EC11_TRIGGER_OWN_THREAD)
|
||
4 years ago
|
k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
|
||
|
|
||
|
k_thread_create(&drv_data->thread, drv_data->thread_stack,
|
||
4 years ago
|
CONFIG_EC11_THREAD_STACK_SIZE,
|
||
|
(k_thread_entry_t)ec11_thread, dev,
|
||
|
0, NULL, K_PRIO_COOP(CONFIG_EC11_THREAD_PRIORITY),
|
||
4 years ago
|
0, K_NO_WAIT);
|
||
4 years ago
|
#elif defined(CONFIG_EC11_TRIGGER_GLOBAL_THREAD)
|
||
|
k_work_init(&drv_data->work, ec11_work_cb);
|
||
4 years ago
|
#endif
|
||
|
|
||
|
return 0;
|
||
|
}
|