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(struct mcp9808_data * data)132 static void mcp9808_thread_main(struct mcp9808_data *data)
133 {
134 	while (true) {
135 		k_sem_take(&data->sem, K_FOREVER);
136 		process_int(data->dev);
137 	}
138 }
139 
140 static K_KERNEL_STACK_DEFINE(mcp9808_thread_stack, CONFIG_MCP9808_THREAD_STACK_SIZE);
141 static struct k_thread mcp9808_thread;
142 #else /* CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD */
143 
mcp9808_gpio_thread_cb(struct k_work * work)144 static void mcp9808_gpio_thread_cb(struct k_work *work)
145 {
146 	struct mcp9808_data *data =
147 		CONTAINER_OF(work, struct mcp9808_data, work);
148 
149 	process_int(data->dev);
150 }
151 
152 #endif /* CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD */
153 
mcp9808_setup_interrupt(const struct device * dev)154 int mcp9808_setup_interrupt(const struct device *dev)
155 {
156 	struct mcp9808_data *data = dev->data;
157 	const struct mcp9808_config *cfg = dev->config;
158 	int rc = mcp9808_reg_write_16bit(dev, MCP9808_REG_CRITICAL,
159 				   MCP9808_TEMP_ABS_MASK);
160 	if (rc == 0) {
161 		rc = mcp9808_reg_write_16bit(dev, MCP9808_REG_CONFIG,
162 				       MCP9808_CFG_ALERT_ENA);
163 	}
164 
165 	data->dev = dev;
166 
167 #ifdef CONFIG_MCP9808_TRIGGER_OWN_THREAD
168 	k_sem_init(&data->sem, 0, K_SEM_MAX_LIMIT);
169 
170 	k_thread_create(&mcp9808_thread, mcp9808_thread_stack,
171 			CONFIG_MCP9808_THREAD_STACK_SIZE,
172 			(k_thread_entry_t)mcp9808_thread_main, data, NULL, NULL,
173 			K_PRIO_COOP(CONFIG_MCP9808_THREAD_PRIORITY),
174 			0, K_NO_WAIT);
175 #else /* CONFIG_MCP9808_TRIGGER_GLOBAL_THREAD */
176 	data->work.handler = mcp9808_gpio_thread_cb;
177 #endif /* trigger type */
178 
179 	if (!gpio_is_ready_dt(&cfg->int_gpio)) {
180 		LOG_ERR("GPIO device not ready");
181 		return -ENODEV;
182 	}
183 
184 	if (rc == 0) {
185 		rc = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
186 	}
187 
188 	if (rc == 0) {
189 		gpio_init_callback(&data->alert_cb, alert_cb, BIT(cfg->int_gpio.pin));
190 
191 		rc = gpio_add_callback(cfg->int_gpio.port, &data->alert_cb);
192 	}
193 
194 	return rc;
195 }
196