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(void * p1,void * p2,void * p3)33 static void adt7310_thread(void *p1, void *p2, void *p3)
34 {
35 ARG_UNUSED(p2);
36 ARG_UNUSED(p3);
37
38 struct adt7310_data *drv_data = p1;
39
40 while (true) {
41 k_sem_take(&drv_data->gpio_sem, K_FOREVER);
42 if (drv_data->th_handler != NULL) {
43 drv_data->th_handler(drv_data->dev, drv_data->th_trigger);
44 }
45 }
46 }
47
48 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
adt7310_work_cb(struct k_work * work)49 static void adt7310_work_cb(struct k_work *work)
50 {
51 struct adt7310_data *drv_data = CONTAINER_OF(work, struct adt7310_data, work);
52
53 if (drv_data->th_handler != NULL) {
54 drv_data->th_handler(drv_data->dev, drv_data->th_trigger);
55 }
56 }
57 #endif
58
adt7310_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)59 int adt7310_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
60 sensor_trigger_handler_t handler)
61 {
62 struct adt7310_data *drv_data = dev->data;
63 const struct adt7310_dev_config *cfg = dev->config;
64
65 if (!cfg->int_gpio.port) {
66 return -ENOTSUP;
67 }
68
69 if (trig->type != SENSOR_TRIG_THRESHOLD) {
70 LOG_ERR("Unsupported sensor trigger");
71 return -ENOTSUP;
72 }
73
74 /* disable the interrupt but ignore error if it's first */
75 /* time this function is called */
76 gpio_pin_interrupt_configure_dt(&cfg->int_gpio, GPIO_INT_DISABLE);
77
78 drv_data->th_handler = handler;
79
80 if (handler != NULL) {
81 int value, ret;
82 gpio_flags_t flags = GPIO_INT_EDGE_TO_ACTIVE;
83
84 drv_data->th_trigger = trig;
85
86 ret = gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
87 if (ret) {
88 drv_data->th_handler = NULL;
89 return ret;
90 }
91
92 value = gpio_pin_get_dt(&cfg->int_gpio);
93 if (value > 0) {
94 #if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
95 k_sem_give(&drv_data->gpio_sem);
96 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
97 k_work_submit(&drv_data->work);
98 #endif
99 }
100 }
101
102 return 0;
103 }
104
adt7310_init_interrupt(const struct device * dev)105 int adt7310_init_interrupt(const struct device *dev)
106 {
107 struct adt7310_data *drv_data = dev->data;
108 const struct adt7310_dev_config *cfg = dev->config;
109 int ret;
110
111 if (!gpio_is_ready_dt(&cfg->int_gpio)) {
112 LOG_ERR("%s: device %s is not ready", dev->name, cfg->int_gpio.port->name);
113 return -ENODEV;
114 }
115
116 gpio_init_callback(&drv_data->gpio_cb, adt7310_gpio_callback, BIT(cfg->int_gpio.pin));
117
118 ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT | GPIO_ACTIVE_LOW);
119 if (ret < 0) {
120 return ret;
121 }
122
123 ret = gpio_add_callback(cfg->int_gpio.port, &drv_data->gpio_cb);
124 if (ret < 0) {
125 return ret;
126 }
127
128 drv_data->dev = dev;
129 #if defined(CONFIG_ADT7310_TRIGGER_OWN_THREAD)
130 k_sem_init(&drv_data->gpio_sem, 0, 1);
131 k_thread_create(&drv_data->thread, drv_data->thread_stack,
132 CONFIG_ADT7310_THREAD_STACK_SIZE,
133 adt7310_thread, drv_data,
134 NULL, NULL, K_PRIO_COOP(CONFIG_ADT7310_THREAD_PRIORITY),
135 0, K_NO_WAIT);
136 k_thread_name_set(&drv_data->thread, dev->name);
137 #elif defined(CONFIG_ADT7310_TRIGGER_GLOBAL_THREAD)
138 drv_data->work.handler = adt7310_work_cb;
139 #endif
140
141 return 0;
142 }
143