1 /*
2  * Copyright (c) 2024 Arrow Electronics.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ti_tmp1075
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/util.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/sys/__assert.h>
17 #include <zephyr/logging/log.h>
18 
19 #include "tmp1075.h"
20 
21 LOG_MODULE_REGISTER(TMP1075, CONFIG_SENSOR_LOG_LEVEL);
22 
23 #define I2C_REG_ADDR_SIZE   1
24 #define I2C_REG_SENSOR_SIZE sizeof(uint16_t)
25 #define I2C_BUFFER_SIZE     I2C_REG_ADDR_SIZE + I2C_REG_SENSOR_SIZE
26 
27 #define I2C_REG_ADDR_OFFSET   0
28 #define I2C_WRITE_DATA_OFFSET 1
29 
tmp1075_reg_read(const struct tmp1075_config * cfg,uint8_t reg,uint16_t * val)30 static int tmp1075_reg_read(const struct tmp1075_config *cfg, uint8_t reg, uint16_t *val)
31 {
32 	if (i2c_burst_read_dt(&cfg->bus, reg, (uint8_t *)val, sizeof(*val)) < 0) {
33 		return -EIO;
34 	}
35 	*val = sys_be16_to_cpu(*val);
36 	return 0;
37 }
38 
tmp1075_reg_write(const struct tmp1075_config * cfg,uint8_t reg,uint16_t val)39 static int tmp1075_reg_write(const struct tmp1075_config *cfg, uint8_t reg, uint16_t val)
40 {
41 	uint8_t buf[I2C_REG_ADDR_SIZE + I2C_REG_SENSOR_SIZE];
42 
43 	buf[I2C_REG_ADDR_OFFSET] = reg;
44 	sys_put_be16(val, &buf[I2C_WRITE_DATA_OFFSET]);
45 
46 	return i2c_write_dt(&cfg->bus, buf, sizeof(buf));
47 }
48 
49 #if CONFIG_TMP1075_ALERT_INTERRUPTS
set_threshold_attribute(const struct device * dev,uint8_t reg,int16_t value,const char * error_msg)50 static int set_threshold_attribute(const struct device *dev, uint8_t reg, int16_t value,
51 				   const char *error_msg)
52 {
53 	if (tmp1075_reg_write(dev->config, reg, value) < 0) {
54 		LOG_ERR("Failed to set %s attribute!", error_msg);
55 		return -EIO;
56 	}
57 	return 0;
58 }
59 #endif
60 
tmp1075_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)61 static int tmp1075_attr_set(const struct device *dev, enum sensor_channel chan,
62 			    enum sensor_attribute attr, const struct sensor_value *val)
63 {
64 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
65 		return -ENOTSUP;
66 	}
67 
68 	switch (attr) {
69 #if CONFIG_TMP1075_ALERT_INTERRUPTS
70 		int integer, frac;
71 
72 	case SENSOR_ATTR_LOWER_THRESH:
73 		/* Extract integer and fractional parts from TMP1075 12-bit register value */
74 		integer = (val->val1 << TMP1075_DATA_INTE_SHIFT) & TMP1075_DATA_INTE_MASK;
75 		frac = ((val->val2 / TMP1075_TEMP_SCALE) << TMP1075_DATA_FRAC_SHIFT) &
76 		       TMP1075_DATA_FRAC_MASK;
77 		return set_threshold_attribute(dev, TMP1075_REG_TLOW, integer | frac,
78 					       "SENSOR_ATTR_LOWER_THRESH");
79 
80 	case SENSOR_ATTR_UPPER_THRESH:
81 		/* Extract integer and fractional parts from TMP1075 12-bit register value */
82 		integer = (val->val1 << TMP1075_DATA_INTE_SHIFT) & TMP1075_DATA_INTE_MASK;
83 		frac = ((val->val2 / TMP1075_TEMP_SCALE) << TMP1075_DATA_FRAC_SHIFT) &
84 		       TMP1075_DATA_FRAC_MASK;
85 		return set_threshold_attribute(dev, TMP1075_REG_THIGH, integer | frac,
86 					       "SENSOR_ATTR_UPPER_THRESH");
87 #endif
88 
89 	default:
90 		return -ENOTSUP;
91 	}
92 }
93 
94 #if CONFIG_TMP1075_ALERT_INTERRUPTS
get_threshold_attribute(const struct device * dev,uint8_t reg,struct sensor_value * val,const char * error_msg)95 static int get_threshold_attribute(const struct device *dev, uint8_t reg, struct sensor_value *val,
96 				   const char *error_msg)
97 {
98 	uint16_t value;
99 
100 	if (tmp1075_reg_read(dev->config, reg, &value) < 0) {
101 		LOG_ERR("Failed to get %s attribute!", error_msg);
102 		return -EIO;
103 	}
104 	val->val1 = (value & TMP1075_DATA_INTE_MASK) >> TMP1075_DATA_INTE_SHIFT;
105 	val->val2 =
106 		((value & TMP1075_DATA_FRAC_MASK) >> TMP1075_DATA_FRAC_SHIFT) * TMP1075_TEMP_SCALE;
107 	return 0;
108 }
109 #endif
110 
tmp1075_attr_get(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,struct sensor_value * val)111 static int tmp1075_attr_get(const struct device *dev, enum sensor_channel chan,
112 			    enum sensor_attribute attr, struct sensor_value *val)
113 {
114 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
115 		return -ENOTSUP;
116 	}
117 
118 	switch (attr) {
119 #if CONFIG_TMP1075_ALERT_INTERRUPTS
120 	case SENSOR_ATTR_LOWER_THRESH:
121 		return get_threshold_attribute(dev, TMP1075_REG_TLOW, val,
122 					       "SENSOR_ATTR_LOWER_THRESH");
123 
124 	case SENSOR_ATTR_UPPER_THRESH:
125 		return get_threshold_attribute(dev, TMP1075_REG_THIGH, val,
126 					       "SENSOR_ATTR_UPPER_THRESH");
127 #endif
128 
129 	default:
130 		return -ENOTSUP;
131 	}
132 }
133 
tmp1075_sample_fetch(const struct device * dev,enum sensor_channel chan)134 static int tmp1075_sample_fetch(const struct device *dev, enum sensor_channel chan)
135 {
136 	struct tmp1075_data *drv_data = dev->data;
137 	const struct tmp1075_config *cfg = dev->config;
138 	uint16_t val;
139 
140 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
141 
142 	if (tmp1075_reg_read(cfg, TMP1075_REG_TEMPERATURE, &val) < 0) {
143 		return -EIO;
144 	}
145 	drv_data->sample = arithmetic_shift_right((int16_t)val, TMP1075_DATA_NORMAL_SHIFT);
146 	return 0;
147 }
148 
tmp1075_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)149 static int tmp1075_channel_get(const struct device *dev, enum sensor_channel chan,
150 			       struct sensor_value *val)
151 {
152 	struct tmp1075_data *drv_data = dev->data;
153 	int32_t uval;
154 
155 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
156 		return -ENOTSUP;
157 	}
158 
159 	uval = (int32_t)drv_data->sample * TMP1075_TEMP_SCALE;
160 	val->val1 = uval / uCELSIUS_IN_CELSIUS;
161 	val->val2 = uval % uCELSIUS_IN_CELSIUS;
162 
163 	return 0;
164 }
165 
166 static DEVICE_API(sensor, tmp1075_driver_api) = {
167 	.attr_set = tmp1075_attr_set,
168 	.attr_get = tmp1075_attr_get,
169 	.sample_fetch = tmp1075_sample_fetch,
170 	.channel_get = tmp1075_channel_get,
171 #ifdef CONFIG_TMP1075_ALERT_INTERRUPTS
172 	.trigger_set = tmp1075_trigger_set,
173 #endif
174 };
175 
176 #ifdef CONFIG_TMP1075_ALERT_INTERRUPTS
setup_interrupts(const struct device * dev)177 static int setup_interrupts(const struct device *dev)
178 {
179 	struct tmp1075_data *drv_data = dev->data;
180 	const struct tmp1075_config *config = dev->config;
181 	const struct gpio_dt_spec *alert_gpio = &config->alert_gpio;
182 	int result;
183 
184 	if (!gpio_is_ready_dt(alert_gpio)) {
185 		LOG_ERR("gpio controller %s not ready", alert_gpio->port->name);
186 		return -ENODEV;
187 	}
188 
189 	result = gpio_pin_configure_dt(alert_gpio, GPIO_INPUT);
190 
191 	if (result < 0) {
192 		return result;
193 	}
194 
195 	gpio_init_callback(&drv_data->temp_alert_gpio_cb, tmp1075_trigger_handle_alert,
196 			   BIT(alert_gpio->pin));
197 
198 	result = gpio_add_callback(alert_gpio->port, &drv_data->temp_alert_gpio_cb);
199 
200 	if (result < 0) {
201 		return result;
202 	}
203 
204 	result = gpio_pin_interrupt_configure_dt(alert_gpio, GPIO_INT_EDGE_BOTH);
205 
206 	if (result < 0) {
207 		return result;
208 	}
209 
210 	return 0;
211 }
212 #endif
213 
tmp1075_init(const struct device * dev)214 static int tmp1075_init(const struct device *dev)
215 {
216 	const struct tmp1075_config *cfg = dev->config;
217 	struct tmp1075_data *data = dev->data;
218 
219 	if (!i2c_is_ready_dt(&cfg->bus)) {
220 		LOG_ERR("I2C dev %s not ready", cfg->bus.bus->name);
221 		return -EINVAL;
222 	}
223 #ifdef CONFIG_TMP1075_ALERT_INTERRUPTS
224 	int result = setup_interrupts(dev);
225 
226 	if (result < 0) {
227 		LOG_ERR("Couldn't setup interrupts");
228 		return -EIO;
229 	}
230 #endif
231 	data->tmp1075_dev = dev;
232 
233 	uint16_t config_reg = 0;
234 
235 	TMP1075_SET_ONE_SHOT_CONVERSION(config_reg, cfg->one_shot);
236 	TMP1075_SET_CONVERSION_RATE(config_reg, cfg->cr);
237 	TMP1075_SET_CONSECUTIVE_FAULT_MEASUREMENTS(config_reg, cfg->cf);
238 	TMP1075_SET_ALERT_PIN_POLARITY(config_reg, cfg->alert_pol);
239 	TMP1075_SET_ALERT_PIN_FUNCTION(config_reg, cfg->interrupt_mode);
240 	TMP1075_SET_SHUTDOWN_MODE(config_reg, cfg->shutdown_mode);
241 
242 	int rc = tmp1075_reg_write(dev->config, TMP1075_REG_CONFIG, config_reg);
243 
244 	if (rc == 0) {
245 		data->config_reg = config_reg;
246 	}
247 	return rc;
248 }
249 
250 #define TMP1075_INST(inst)                                                                         \
251 	static struct tmp1075_data tmp1075_data_##inst;                                            \
252 	static const struct tmp1075_config tmp1075_config_##inst = {                               \
253 		.cr = DT_INST_ENUM_IDX(inst, conversion_rate),                                     \
254 		.cf = DT_INST_ENUM_IDX(inst, consecutive_fault_measurements),                      \
255 		.alert_pol = DT_INST_PROP(inst, alert_pin_active_high),                            \
256 		.interrupt_mode = DT_INST_PROP(inst, interrupt_mode),                              \
257 		.shutdown_mode = DT_INST_PROP(inst, shutdown_mode),                                \
258 		.bus = I2C_DT_SPEC_INST_GET(inst),                                                 \
259 		.alert_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, alert_gpios, {0}),                    \
260 	};                                                                                         \
261 	SENSOR_DEVICE_DT_INST_DEFINE(inst, tmp1075_init, NULL, &tmp1075_data_##inst,               \
262                                                                                                    \
263 				     &tmp1075_config_##inst, POST_KERNEL,                          \
264 				     CONFIG_SENSOR_INIT_PRIORITY, &tmp1075_driver_api);
265 
266 DT_INST_FOREACH_STATUS_OKAY(TMP1075_INST)
267