1 /*
2  * Copyright (c) 2019 Peter Bigot Consulting, LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/sys/byteorder.h>
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/logging/log.h>
13 #include "mcp9808.h"
14 
15 LOG_MODULE_DECLARE(MCP9808, CONFIG_SENSOR_LOG_LEVEL);
16 
mcp9808_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)17 int mcp9808_attr_set(const struct device *dev, enum sensor_channel chan,
18 		     enum sensor_attribute attr,
19 		     const struct sensor_value *val)
20 {
21 	const struct mcp9808_config *cfg = dev->config;
22 	uint8_t reg_addr;
23 	int temp;
24 
25 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP);
26 
27 	if (!cfg->int_gpio.port) {
28 		return -ENOTSUP;
29 	}
30 
31 	switch (attr) {
32 	case SENSOR_ATTR_LOWER_THRESH:
33 		reg_addr = MCP9808_REG_LOWER_LIMIT;
34 		break;
35 	case SENSOR_ATTR_UPPER_THRESH:
36 		reg_addr = MCP9808_REG_UPPER_LIMIT;
37 		break;
38 	default:
39 		return -EINVAL;
40 	}
41 
42 	/* Convert temperature to a signed scaled value, then write
43 	 * the 12-bit 2s complement-plus-sign-bit register value.
44 	 */
45 	temp = val->val1 * MCP9808_TEMP_SCALE_CEL;
46 	temp += (MCP9808_TEMP_SCALE_CEL * val->val2) / 1000000;
47 
48 	return mcp9808_reg_write_16bit(dev, reg_addr,
49 				 mcp9808_temp_reg_from_signed(temp));
50 }
51 
setup_int(const struct device * dev,bool enable)52 static inline void setup_int(const struct device *dev,
53 			     bool enable)
54 {
55 	const struct mcp9808_config *cfg = dev->config;
56 	unsigned int flags = enable
57 		? GPIO_INT_EDGE_TO_ACTIVE
58 		: GPIO_INT_DISABLE;
59 
60 	gpio_pin_interrupt_configure_dt(&cfg->int_gpio, flags);
61 }
62 
handle_int(const struct device * dev)63 static void handle_int(const struct device *dev)
64 {
65 	struct mcp9808_data *data = dev->data;
66 
67 	setup_int(dev, false);
68 
69 #if defined(CONFIG_MCP9808_TRIGGER_OWN_THREAD)
70 	k_sem_give(&data->sem);
71 #elif defined(CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD)
72 	k_work_submit(&data->work);
73 #endif
74 }
75 
process_int(const struct device * dev)76 static void process_int(const struct device *dev)
77 {
78 	struct mcp9808_data *data = dev->data;
79 
80 	if (data->trigger_handler) {
81 		data->trigger_handler(dev, data->trig);
82 	}
83 
84 	if (data->trigger_handler) {
85 		setup_int(dev, true);
86 	}
87 }
88 
mcp9808_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)89 int mcp9808_trigger_set(const struct device *dev,
90 			const struct sensor_trigger *trig,
91 			sensor_trigger_handler_t handler)
92 {
93 	struct mcp9808_data *data = dev->data;
94 	const struct mcp9808_config *cfg = dev->config;
95 	int rv = 0;
96 
97 	if (!cfg->int_gpio.port) {
98 		return -ENOTSUP;
99 	}
100 
101 	setup_int(dev, false);
102 
103 	data->trig = trig;
104 	data->trigger_handler = handler;
105 
106 	if (handler != NULL) {
107 		setup_int(dev, true);
108 
109 		rv = gpio_pin_get_dt(&cfg->int_gpio);
110 		if (rv > 0) {
111 			handle_int(dev);
112 			rv = 0;
113 		}
114 	}
115 
116 	return rv;
117 }
118 
alert_cb(const struct device * dev,struct gpio_callback * cb,uint32_t pins)119 static void alert_cb(const struct device *dev, struct gpio_callback *cb,
120 		     uint32_t pins)
121 {
122 	struct mcp9808_data *data =
123 		CONTAINER_OF(cb, struct mcp9808_data, alert_cb);
124 
125 	ARG_UNUSED(pins);
126 
127 	handle_int(data->dev);
128 }
129 
130 #ifdef CONFIG_MCP9808_TRIGGER_OWN_THREAD
131 
mcp9808_thread_main(void * p1,void * p2,void * p3)132 static void mcp9808_thread_main(void *p1, void *p2, void *p3)
133 {
134 	ARG_UNUSED(p2);
135 	ARG_UNUSED(p3);
136 
137 	struct mcp9808_data *data = p1;
138 
139 	while (true) {
140 		k_sem_take(&data->sem, K_FOREVER);
141 		process_int(data->dev);
142 	}
143 }
144 
145 static K_KERNEL_STACK_DEFINE(mcp9808_thread_stack, CONFIG_MCP9808_THREAD_STACK_SIZE);
146 static struct k_thread mcp9808_thread;
147 #else /* CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD */
148 
mcp9808_gpio_thread_cb(struct k_work * work)149 static void mcp9808_gpio_thread_cb(struct k_work *work)
150 {
151 	struct mcp9808_data *data =
152 		CONTAINER_OF(work, struct mcp9808_data, work);
153 
154 	process_int(data->dev);
155 }
156 
157 #endif /* CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD */
158 
mcp9808_setup_interrupt(const struct device * dev)159 int mcp9808_setup_interrupt(const struct device *dev)
160 {
161 	struct mcp9808_data *data = dev->data;
162 	const struct mcp9808_config *cfg = dev->config;
163 	int rc = mcp9808_reg_write_16bit(dev, MCP9808_REG_CRITICAL,
164 				   MCP9808_TEMP_ABS_MASK);
165 	if (rc == 0) {
166 		rc = mcp9808_reg_write_16bit(dev, MCP9808_REG_CONFIG,
167 				       MCP9808_CFG_ALERT_ENA);
168 	}
169 
170 	data->dev = dev;
171 
172 #ifdef CONFIG_MCP9808_TRIGGER_OWN_THREAD
173 	k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT);
174 
175 	k_thread_create(&mcp9808_thread, mcp9808_thread_stack,
176 			CONFIG_MCP9808_THREAD_STACK_SIZE,
177 			mcp9808_thread_main, data, NULL, NULL,
178 			K_PRIO_COOP(CONFIG_MCP9808_THREAD_PRIORITY),
179 			0, K_NO_WAIT);
180 #else /* CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD */
181 	data->work.handler = mcp9808_gpio_thread_cb;
182 #endif /* trigger type */
183 
184 	if (!gpio_is_ready_dt(&cfg->int_gpio)) {
185 		LOG_ERR("GPIO device not ready");
186 		return -ENODEV;
187 	}
188 
189 	if (rc == 0) {
190 		rc = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
191 	}
192 
193 	if (rc == 0) {
194 		gpio_init_callback(&data->alert_cb, alert_cb, BIT(cfg->int_gpio.pin));
195 
196 		rc = gpio_add_callback(cfg->int_gpio.port, &data->alert_cb);
197 	}
198 
199 	return rc;
200 }
201