|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 STMicroelectronics |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT st_lsm6dsr |
| 8 | + |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/drivers/i2c.h> |
| 11 | +#include <zephyr/sys/__assert.h> |
| 12 | +#include <zephyr/sys/util.h> |
| 13 | +#include <zephyr/kernel.h> |
| 14 | +#include <zephyr/drivers/sensor.h> |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +#include "lsm6dsr.h" |
| 17 | + |
| 18 | +LOG_MODULE_DECLARE(LSM6DSR, CONFIG_SENSOR_LOG_LEVEL); |
| 19 | + |
| 20 | +static inline void setup_irq(const struct device *dev, bool enable) |
| 21 | +{ |
| 22 | + const struct lsm6dsr_config *config = dev->config; |
| 23 | + |
| 24 | + unsigned int flags = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE; |
| 25 | + |
| 26 | + gpio_pin_interrupt_configure_dt(&config->int_gpio, flags); |
| 27 | +} |
| 28 | + |
| 29 | +static inline void handle_irq(const struct device *dev) |
| 30 | +{ |
| 31 | + struct lsm6dsr_data *drv_data = dev->data; |
| 32 | + |
| 33 | + setup_irq(dev, false); |
| 34 | + |
| 35 | +#if defined(CONFIG_LSM6DSR_TRIGGER_OWN_THREAD) |
| 36 | + k_sem_give(&drv_data->gpio_sem); |
| 37 | +#elif defined(CONFIG_LSM6DSR_TRIGGER_GLOBAL_THREAD) |
| 38 | + k_work_submit(&drv_data->work); |
| 39 | +#endif |
| 40 | +} |
| 41 | + |
| 42 | +int lsm6dsr_trigger_set(const struct device *dev, const struct sensor_trigger *trig, |
| 43 | + sensor_trigger_handler_t handler) |
| 44 | +{ |
| 45 | + const struct lsm6dsr_config *config = dev->config; |
| 46 | + struct lsm6dsr_data *drv_data = dev->data; |
| 47 | + |
| 48 | + __ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY); |
| 49 | + |
| 50 | + /* If irq_gpio is not configured in DT just return error */ |
| 51 | + if (!config->int_gpio.port) { |
| 52 | + LOG_ERR("triggers not supported"); |
| 53 | + return -ENOTSUP; |
| 54 | + } |
| 55 | + |
| 56 | + setup_irq(dev, false); |
| 57 | + |
| 58 | + drv_data->data_ready_handler = handler; |
| 59 | + if (handler == NULL) { |
| 60 | + return 0; |
| 61 | + } |
| 62 | + |
| 63 | + drv_data->data_ready_trigger = trig; |
| 64 | + |
| 65 | + setup_irq(dev, true); |
| 66 | + if (gpio_pin_get_dt(&config->int_gpio) > 0) { |
| 67 | + handle_irq(dev); |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +static void lsm6dsr_gpio_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins) |
| 74 | +{ |
| 75 | + struct lsm6dsr_data *drv_data = CONTAINER_OF(cb, struct lsm6dsr_data, gpio_cb); |
| 76 | + |
| 77 | + ARG_UNUSED(pins); |
| 78 | + |
| 79 | + handle_irq(drv_data->dev); |
| 80 | +} |
| 81 | + |
| 82 | +static void lsm6dsr_thread_cb(const struct device *dev) |
| 83 | +{ |
| 84 | + struct lsm6dsr_data *drv_data = dev->data; |
| 85 | + |
| 86 | + if (drv_data->data_ready_handler != NULL) { |
| 87 | + drv_data->data_ready_handler(dev, drv_data->data_ready_trigger); |
| 88 | + } |
| 89 | + |
| 90 | + setup_irq(dev, true); |
| 91 | +} |
| 92 | + |
| 93 | +#ifdef CONFIG_LSM6DSR_TRIGGER_OWN_THREAD |
| 94 | +static void lsm6dsr_thread(void *p1, void *p2, void *p3) |
| 95 | +{ |
| 96 | + ARG_UNUSED(p2); |
| 97 | + ARG_UNUSED(p3); |
| 98 | + |
| 99 | + const struct device *dev = p1; |
| 100 | + struct lsm6dsr_data *drv_data = dev->data; |
| 101 | + |
| 102 | + while (1) { |
| 103 | + k_sem_take(&drv_data->gpio_sem, K_FOREVER); |
| 104 | + lsm6dsr_thread_cb(dev); |
| 105 | + } |
| 106 | +} |
| 107 | +#endif |
| 108 | + |
| 109 | +#ifdef CONFIG_LSM6DSR_TRIGGER_GLOBAL_THREAD |
| 110 | +static void lsm6dsr_work_cb(struct k_work *work) |
| 111 | +{ |
| 112 | + struct lsm6dsr_data *drv_data = CONTAINER_OF(work, struct lsm6dsr_data, work); |
| 113 | + |
| 114 | + lsm6dsr_thread_cb(drv_data->dev); |
| 115 | +} |
| 116 | +#endif |
| 117 | + |
| 118 | +int lsm6dsr_init_interrupt(const struct device *dev) |
| 119 | +{ |
| 120 | + const struct lsm6dsr_config *config = dev->config; |
| 121 | + struct lsm6dsr_data *drv_data = dev->data; |
| 122 | + |
| 123 | + if (!gpio_is_ready_dt(&config->int_gpio)) { |
| 124 | + LOG_ERR("GPIO device not ready"); |
| 125 | + return -ENODEV; |
| 126 | + } |
| 127 | + |
| 128 | + gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT); |
| 129 | + |
| 130 | + gpio_init_callback(&drv_data->gpio_cb, lsm6dsr_gpio_callback, BIT(config->int_gpio.pin)); |
| 131 | + |
| 132 | + if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) { |
| 133 | + LOG_ERR("Could not set gpio callback."); |
| 134 | + return -EIO; |
| 135 | + } |
| 136 | + |
| 137 | + /* enable data-ready interrupt */ |
| 138 | + if (drv_data->hw_tf->update_reg( |
| 139 | + dev, LSM6DSR_REG_INT1_CTRL, |
| 140 | + LSM6DSR_MASK_INT1_CTRL_DRDY_XL | LSM6DSR_MASK_INT1_CTRL_DRDY_G, |
| 141 | + BIT(LSM6DSR_SHIFT_INT1_CTRL_DRDY_XL) | BIT(LSM6DSR_SHIFT_INT1_CTRL_DRDY_G)) < |
| 142 | + 0) { |
| 143 | + LOG_ERR("Could not enable data-ready interrupt."); |
| 144 | + return -EIO; |
| 145 | + } |
| 146 | + |
| 147 | + drv_data->dev = dev; |
| 148 | + |
| 149 | +#if defined(CONFIG_LSM6DSR_TRIGGER_OWN_THREAD) |
| 150 | + k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT); |
| 151 | + |
| 152 | + k_thread_create(&drv_data->thread, drv_data->thread_stack, CONFIG_LSM6DSR_THREAD_STACK_SIZE, |
| 153 | + lsm6dsr_thread, (void *)dev, NULL, NULL, |
| 154 | + K_PRIO_COOP(CONFIG_LSM6DSR_THREAD_PRIORITY), 0, K_NO_WAIT); |
| 155 | +#elif defined(CONFIG_LSM6DSR_TRIGGER_GLOBAL_THREAD) |
| 156 | + drv_data->work.handler = lsm6dsr_work_cb; |
| 157 | +#endif |
| 158 | + |
| 159 | + setup_irq(dev, true); |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
0 commit comments