1 /*
2 * Copyright (c) 2023 Andriy Gelman <andriy.gelman@gmail.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/drivers/spi.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/types.h>
14
15 #include "adt7310.h"
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_DECLARE(ADT7310, CONFIG_SENSOR_LOG_LEVEL);
19
adt7310_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)20 static void adt7310_gpio_callback(const struct device *dev, struct gpio_callback *cb,
21 uint32_t pins)
22 {
23 struct adt7310_data *drv_data = CONTAINER_OF(cb, struct adt7310_data, gpio_cb);
24
25 #if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
26 k_sem_give(&drv_data->gpio_sem);
27 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
28 k_work_submit(&drv_data->work);
29 #endif
30 }
31
32 #if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
adt7310_thread(struct adt7310_data * drv_data)33 static void adt7310_thread(struct adt7310_data *drv_data)
34 {
35 while (true) {
36 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
37 if (drv_data->th_handler != NULL) {
38 drv_data->th_handler(drv_data->dev, drv_data->th_trigger);
39 }
40 }
41 }
42
43 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
adt7310_work_cb(struct k_work * work)44 static void adt7310_work_cb(struct k_work *work)
45 {
46 struct adt7310_data *drv_data = CONTAINER_OF(work, struct adt7310_data, work);
47
48 if (drv_data->th_handler != NULL) {
49 drv_data->th_handler(drv_data->dev, drv_data->th_trigger);
50 }
51 }
52 #endif
53
adt7310_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)54 int adt7310_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
55 sensor_trigger_handler_t handler)
56 {
57 struct adt7310_data *drv_data = dev->data;
58 const struct adt7310_dev_config *cfg = dev->config;
59
60 if (!cfg->int_gpio.port) {
61 return -ENOTSUP;
62 }
63
64 if (trig->type != SENSOR_TRIG_THRESHOLD) {
65 LOG_ERR("Unsupported sensor trigger");
66 return -ENOTSUP;
67 }
68
69 /* disable the interrupt but ignore error if it's first */
70 /* time this function is called */
71 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
72
73 drv_data->th_handler = handler;
74
75 if (handler != NULL) {
76 int value, ret;
77 gpio_flags_t flags = GPIO_INT_EDGE_TO_ACTIVE;
78
79 drv_data->th_trigger = trig;
80
81 ret = gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
82 if (ret) {
83 drv_data->th_handler = NULL;
84 return ret;
85 }
86
87 value = gpio_pin_get_dt(&cfg->int_gpio);
88 if (value > 0) {
89 #if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
90 k_sem_give(&drv_data->gpio_sem);
91 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
92 k_work_submit(&drv_data->work);
93 #endif
94 }
95 }
96
97 return 0;
98 }
99
adt7310_init_interrupt(const struct device * dev)100 int adt7310_init_interrupt(const struct device *dev)
101 {
102 struct adt7310_data *drv_data = dev->data;
103 const struct adt7310_dev_config *cfg = dev->config;
104 int ret;
105
106 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
107 LOG_ERR("%s: device %s is not ready", dev->name, cfg->int_gpio.port->name);
108 return -ENODEV;
109 }
110
111 gpio_init_callback(&drv_data->gpio_cb, adt7310_gpio_callback, BIT(cfg->int_gpio.pin));
112
113 ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT | GPIO_ACTIVE_LOW);
114 if (ret < 0) {
115 return ret;
116 }
117
118 ret = gpio_add_callback(cfg->int_gpio.port, &drv_data->gpio_cb);
119 if (ret < 0) {
120 return ret;
121 }
122
123 drv_data->dev = dev;
124 #if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
125 k_sem_init(&drv_data->gpio_sem, 0, 1);
126 k_thread_create(&drv_data->thread, drv_data->thread_stack,
127 CONFIG_ADT7310_THREAD_STACK_SIZE,
128 (k_thread_entry_t)adt7310_thread, drv_data,
129 NULL, NULL, K_PRIO_COOP(CONFIG_ADT7310_THREAD_PRIORITY),
130 0, K_NO_WAIT);
131 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
132 drv_data->work.handler = adt7310_work_cb;
133 #endif
134
135 return 0;
136 }
137