1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT honeywell_hmc5883l
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 "hmc5883l.h"
17 
18 LOG_MODULE_DECLARE(HMC5883L, CONFIG_SENSOR_LOG_LEVEL);
19 
hmc5883l_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)20 int hmc5883l_trigger_set(const struct device *dev,
21 			 const struct sensor_trigger *trig,
22 			 sensor_trigger_handler_t handler)
23 {
24 	struct hmc5883l_data *drv_data = dev->data;
25 	const struct hmc5883l_config *config = dev->config;
26 
27 	if (!config->int_gpio.port) {
28 		return -ENOTSUP;
29 	}
30 
31 	__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
32 
33 	gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
34 
35 	drv_data->data_ready_handler = handler;
36 	if (handler == NULL) {
37 		return 0;
38 	}
39 
40 	drv_data->data_ready_trigger = trig;
41 
42 	gpio_pin_interrupt_configure_dt(&config->int_gpio,
43 					GPIO_INT_EDGE_TO_ACTIVE);
44 
45 	return 0;
46 }
47 
hmc5883l_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pins)48 static void hmc5883l_gpio_callback(const struct device *dev,
49 				   struct gpio_callback *cb, uint32_t pins)
50 {
51 	struct hmc5883l_data *drv_data =
52 		CONTAINER_OF(cb, struct hmc5883l_data, gpio_cb);
53 	const struct hmc5883l_config *config = drv_data->dev->config;
54 
55 	ARG_UNUSED(pins);
56 
57 	gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
58 
59 #if defined(CONFIG_HMC5883L_TRIGGER_OWN_THREAD)
60 	k_sem_give(&drv_data->gpio_sem);
61 #elif defined(CONFIG_HMC5883L_TRIGGER_GLOBAL_THREAD)
62 	k_work_submit(&drv_data->work);
63 #endif
64 }
65 
hmc5883l_thread_cb(const struct device * dev)66 static void hmc5883l_thread_cb(const struct device *dev)
67 {
68 	struct hmc5883l_data *drv_data = dev->data;
69 	const struct hmc5883l_config *config = dev->config;
70 
71 	if (drv_data->data_ready_handler != NULL) {
72 		drv_data->data_ready_handler(dev,
73 					     drv_data->data_ready_trigger);
74 	}
75 
76 	gpio_pin_interrupt_configure_dt(&config->int_gpio,
77 					GPIO_INT_EDGE_TO_ACTIVE);
78 }
79 
80 #ifdef CONFIG_HMC5883L_TRIGGER_OWN_THREAD
hmc5883l_thread(void * p1,void * p2,void * p3)81 static void hmc5883l_thread(void *p1, void *p2, void *p3)
82 {
83 	ARG_UNUSED(p2);
84 	ARG_UNUSED(p3);
85 
86 	struct hmc5883l_data *drv_data = p1;
87 
88 	while (1) {
89 		k_sem_take(&drv_data->gpio_sem, K_FOREVER);
90 		hmc5883l_thread_cb(drv_data->dev);
91 	}
92 }
93 #endif
94 
95 #ifdef CONFIG_HMC5883L_TRIGGER_GLOBAL_THREAD
hmc5883l_work_cb(struct k_work * work)96 static void hmc5883l_work_cb(struct k_work *work)
97 {
98 	struct hmc5883l_data *drv_data =
99 		CONTAINER_OF(work, struct hmc5883l_data, work);
100 
101 	hmc5883l_thread_cb(drv_data->dev);
102 }
103 #endif
104 
hmc5883l_init_interrupt(const struct device * dev)105 int hmc5883l_init_interrupt(const struct device *dev)
106 {
107 	struct hmc5883l_data *drv_data = dev->data;
108 	const struct hmc5883l_config *config = dev->config;
109 
110 	if (!gpio_is_ready_dt(&config->int_gpio)) {
111 		LOG_ERR("GPIO device not ready");
112 		return -ENODEV;
113 	}
114 
115 	gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
116 
117 	gpio_init_callback(&drv_data->gpio_cb, hmc5883l_gpio_callback,
118 			   BIT(config->int_gpio.pin));
119 
120 	if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
121 		LOG_ERR("Failed to set gpio callback.");
122 		return -EIO;
123 	}
124 
125 	drv_data->dev = dev;
126 
127 #if defined(CONFIG_HMC5883L_TRIGGER_OWN_THREAD)
128 	k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
129 
130 	k_thread_create(&drv_data->thread, drv_data->thread_stack,
131 			CONFIG_HMC5883L_THREAD_STACK_SIZE,
132 			hmc5883l_thread,
133 			drv_data, NULL, NULL,
134 			K_PRIO_COOP(CONFIG_HMC5883L_THREAD_PRIORITY),
135 			0, K_NO_WAIT);
136 #elif defined(CONFIG_HMC5883L_TRIGGER_GLOBAL_THREAD)
137 	drv_data->work.handler = hmc5883l_work_cb;
138 #endif
139 
140 	gpio_pin_interrupt_configure_dt(&config->int_gpio,
141 					GPIO_INT_EDGE_TO_ACTIVE);
142 
143 	return 0;
144 }
145