1 /*
2  * Copyright 2023 Daniel DeGrasse <daniel@degrasse.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(tcn75a, CONFIG_SENSOR_LOG_LEVEL);
9 
10 #include "tcn75a.h"
11 
tcn75a_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)12 int tcn75a_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
13 		       sensor_trigger_handler_t handler)
14 {
15 	const struct tcn75a_config *config = dev->config;
16 	struct tcn75a_data *data = dev->data;
17 	int ret;
18 
19 	if (trig->type != SENSOR_TRIG_THRESHOLD) {
20 		return -ENOTSUP;
21 	}
22 
23 	if ((trig->chan != SENSOR_CHAN_ALL) && (trig->chan != SENSOR_CHAN_AMBIENT_TEMP)) {
24 		return -ENOTSUP;
25 	}
26 
27 	data->sensor_cb = handler;
28 	data->sensor_trig = trig;
29 
30 	/* TCN75A starts in comparator mode by default, switch it to
31 	 * use interrupt mode.
32 	 */
33 	ret = i2c_reg_update_byte_dt(&config->i2c_spec, TCN75A_CONFIG_REG, TCN75A_CONFIG_INT_EN,
34 				     TCN75A_CONFIG_INT_EN);
35 	if (ret < 0) {
36 		return ret;
37 	}
38 
39 	ret = gpio_pin_interrupt_configure_dt(&config->alert_gpios, GPIO_INT_EDGE_TO_ACTIVE);
40 	if (ret < 0) {
41 		return ret;
42 	}
43 
44 	return ret;
45 }
46 
tcn75a_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)47 int tcn75a_attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr,
48 		    const struct sensor_value *val)
49 {
50 	const struct tcn75a_config *config = dev->config;
51 	uint8_t tx_buf[3];
52 
53 	if ((chan != SENSOR_CHAN_AMBIENT_TEMP) && (chan != SENSOR_CHAN_ALL)) {
54 		return -ENOTSUP;
55 	}
56 
57 	switch (attr) {
58 	case SENSOR_ATTR_LOWER_THRESH:
59 		tx_buf[0] = TCN75A_THYST_REG;
60 		break;
61 	case SENSOR_ATTR_UPPER_THRESH:
62 		tx_buf[0] = TCN75A_TSET_REG;
63 		break;
64 	default:
65 		return -ENOTSUP;
66 	}
67 
68 	/* Convert sensor val to fixed point */
69 	tx_buf[1] = (uint8_t)val->val1;
70 	tx_buf[2] = TCN75A_SENSOR_TO_FIXED_PT(val->val2);
71 
72 	LOG_DBG("Writing 0x%02X to limit reg %s", *(uint16_t *)(tx_buf + 1),
73 		tx_buf[0] == TCN75A_THYST_REG ? "THYST" : "TSET");
74 
75 	return i2c_write_dt(&config->i2c_spec, tx_buf, 3);
76 }
77 
tcn75a_attr_get(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,struct sensor_value * val)78 int tcn75a_attr_get(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr,
79 		    struct sensor_value *val)
80 {
81 	const struct tcn75a_config *config = dev->config;
82 	uint8_t config_reg;
83 	uint8_t rx_buf[2];
84 	uint16_t limit, temp_lsb;
85 	int ret;
86 
87 	if ((chan != SENSOR_CHAN_AMBIENT_TEMP) && (chan != SENSOR_CHAN_ALL)) {
88 		return -ENOTSUP;
89 	}
90 
91 	switch (attr) {
92 	case SENSOR_ATTR_LOWER_THRESH:
93 		config_reg = TCN75A_THYST_REG;
94 		break;
95 	case SENSOR_ATTR_UPPER_THRESH:
96 		config_reg = TCN75A_TSET_REG;
97 		break;
98 	default:
99 		return -ENOTSUP;
100 	}
101 
102 	ret = i2c_write_read_dt(&config->i2c_spec, &config_reg, 1, rx_buf, 2);
103 	if (ret < 0) {
104 		return ret;
105 	}
106 
107 	limit = sys_get_be16(rx_buf);
108 
109 	LOG_DBG("Read 0x%02X from %s", limit, config_reg == TCN75A_THYST_REG ? "THYST" : "TSET");
110 
111 	/* Convert fixed point to sensor value  */
112 	val->val1 = limit >> TCN75A_TEMP_MSB_POS;
113 	temp_lsb = (limit & TCN75A_TEMP_LSB_MASK);
114 	val->val2 = TCN75A_FIXED_PT_TO_SENSOR(temp_lsb);
115 
116 	return ret;
117 }
118 
tcn75a_handle_int(const struct device * dev)119 static void tcn75a_handle_int(const struct device *dev)
120 {
121 	struct tcn75a_data *data = dev->data;
122 	/* Note that once the temperature rises
123 	 * above T_SET, the sensor will not trigger another interrupt until
124 	 * it falls below T_HYST (or vice versa for falling below T_HYST).
125 	 *
126 	 * Reading from any register will de-assert the interrupt.
127 	 */
128 
129 	if (data->sensor_cb) {
130 		data->sensor_cb(dev, data->sensor_trig);
131 	}
132 }
133 
tcn75a_gpio_callback(const struct device * dev,struct gpio_callback * cb,uint32_t pin_mask)134 static void tcn75a_gpio_callback(const struct device *dev, struct gpio_callback *cb,
135 				 uint32_t pin_mask)
136 {
137 	struct tcn75a_data *data = CONTAINER_OF(cb, struct tcn75a_data, gpio_cb);
138 	const struct tcn75a_config *config = data->dev->config;
139 
140 	if ((pin_mask & BIT(config->alert_gpios.pin)) == 0U) {
141 		return;
142 	}
143 
144 #if defined(CONFIG_TCN75A_TRIGGER_OWN_THREAD)
145 	k_sem_give(&data->trig_sem);
146 #elif defined(CONFIG_TCN75A_TRIGGER_GLOBAL_THREAD)
147 	k_work_submit(&data->work);
148 #endif
149 }
150 
151 #ifdef CONFIG_TCN75A_TRIGGER_OWN_THREAD
tcn75a_thread_main(void * p1,void * p2,void * p3)152 static void tcn75a_thread_main(void *p1, void *p2, void *p3)
153 {
154 	ARG_UNUSED(p2);
155 	ARG_UNUSED(p3);
156 
157 	struct tcn75a_data *data = p1;
158 
159 	while (true) {
160 		k_sem_take(&data->trig_sem, K_FOREVER);
161 		tcn75a_handle_int(data->dev);
162 	}
163 }
164 #endif
165 
166 #ifdef CONFIG_TCN75A_TRIGGER_GLOBAL_THREAD
tcn75a_work_handler(struct k_work * work)167 static void tcn75a_work_handler(struct k_work *work)
168 {
169 	struct tcn75a_data *data = CONTAINER_OF(work, struct tcn75a_data, work);
170 
171 	tcn75a_handle_int(data->dev);
172 }
173 #endif
174 
tcn75a_trigger_init(const struct device * dev)175 int tcn75a_trigger_init(const struct device *dev)
176 {
177 	const struct tcn75a_config *config = dev->config;
178 	struct tcn75a_data *data = dev->data;
179 	int ret;
180 
181 	/* Save config pointer */
182 	data->dev = dev;
183 
184 	if (!gpio_is_ready_dt(&config->alert_gpios)) {
185 		LOG_ERR("alert GPIO device is not ready");
186 		return -ENODEV;
187 	}
188 
189 	ret = gpio_pin_configure_dt(&config->alert_gpios, GPIO_INPUT);
190 	if (ret < 0) {
191 		return ret;
192 	}
193 
194 	gpio_init_callback(&data->gpio_cb, tcn75a_gpio_callback, BIT(config->alert_gpios.pin));
195 
196 	ret = gpio_add_callback(config->alert_gpios.port, &data->gpio_cb);
197 
198 #if defined(CONFIG_TCN75A_TRIGGER_OWN_THREAD)
199 	k_sem_init(&data->trig_sem, 0, K_SEM_MAX_LIMIT);
200 	k_thread_create(&data->thread, data->thread_stack, CONFIG_TCN75A_THREAD_STACK_SIZE,
201 			tcn75a_thread_main, data, NULL, NULL,
202 			K_PRIO_COOP(CONFIG_TCN75A_THREAD_PRIORITY), 0, K_NO_WAIT);
203 #elif defined(CONFIG_TCN75A_TRIGGER_GLOBAL_THREAD)
204 	data->work.handler = tcn75a_work_handler;
205 #endif
206 
207 	return ret;
208 }
209