1 /*
2  * Copyright (c) 2016 Firmwave
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ti_tmp112
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/sys/__assert.h>
16 #include <zephyr/logging/log.h>
17 #include "tmp112.h"
18 
19 LOG_MODULE_REGISTER(TMP112, CONFIG_SENSOR_LOG_LEVEL);
20 
tmp112_reg_read(const struct tmp112_config * cfg,uint8_t reg,uint16_t * val)21 static int tmp112_reg_read(const struct tmp112_config *cfg,
22 			   uint8_t reg, uint16_t *val)
23 {
24 	if (i2c_burst_read_dt(&cfg->bus, reg, (uint8_t *)val, sizeof(*val)) < 0) {
25 		return -EIO;
26 	}
27 
28 	*val = sys_be16_to_cpu(*val);
29 
30 	return 0;
31 }
32 
tmp112_reg_write(const struct tmp112_config * cfg,uint8_t reg,uint16_t val)33 static int tmp112_reg_write(const struct tmp112_config *cfg,
34 			    uint8_t reg, uint16_t val)
35 {
36 	uint8_t buf[3];
37 
38 	buf[0] = reg;
39 	sys_put_be16(val, &buf[1]);
40 
41 	return i2c_write_dt(&cfg->bus, buf, sizeof(buf));
42 }
43 
set_config_flags(struct tmp112_data * data,uint16_t mask,uint16_t value)44 static uint16_t set_config_flags(struct tmp112_data *data, uint16_t mask,
45 				 uint16_t value)
46 {
47 	return (data->config_reg & ~mask) | (value & mask);
48 }
49 
tmp112_update_config(const struct device * dev,uint16_t mask,uint16_t val)50 static int tmp112_update_config(const struct device *dev, uint16_t mask,
51 				uint16_t val)
52 {
53 	int rc;
54 	struct tmp112_data *data = dev->data;
55 	const uint16_t new_val = set_config_flags(data, mask, val);
56 
57 	rc = tmp112_reg_write(dev->config, TMP112_REG_CONFIG, new_val);
58 	if (rc == 0) {
59 		data->config_reg = new_val;
60 	}
61 
62 	return rc;
63 }
64 
tmp112_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)65 static int tmp112_attr_set(const struct device *dev,
66 			   enum sensor_channel chan,
67 			   enum sensor_attribute attr,
68 			   const struct sensor_value *val)
69 {
70 	uint16_t value;
71 	uint16_t cr;
72 
73 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
74 		return -ENOTSUP;
75 	}
76 
77 	switch (attr) {
78 #if CONFIG_TMP112_FULL_SCALE_RUNTIME
79 	case SENSOR_ATTR_FULL_SCALE:
80 		/* the sensor supports two ranges -55 to 128 and -55 to 150 */
81 		/* the value contains the upper limit */
82 		if (val->val1 == 128) {
83 			value = 0x0000;
84 		} else if (val->val1 == 150) {
85 			value = TMP112_CONFIG_EM;
86 		} else {
87 			return -ENOTSUP;
88 		}
89 
90 		if (tmp112_update_config(dev, TMP112_CONFIG_EM, value) < 0) {
91 			LOG_DBG("Failed to set attribute!");
92 			return -EIO;
93 		}
94 		break;
95 #endif
96 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
97 #if CONFIG_TMP112_SAMPLING_FREQUENCY_RUNTIME
98 		/* conversion rate in mHz */
99 		cr = val->val1 * 1000 + val->val2 / 1000;
100 
101 		/* the sensor supports 0.25Hz, 1Hz, 4Hz and 8Hz */
102 		/* conversion rate */
103 		switch (cr) {
104 		case 250:
105 			value = TMP112_CONV_RATE(TMP112_CONV_RATE_025);
106 			break;
107 
108 		case 1000:
109 			value = TMP112_CONV_RATE(TMP112_CONV_RATE_1000);
110 			break;
111 
112 		case 4000:
113 			value = TMP112_CONV_RATE(TMP112_CONV_RATE_4);
114 			break;
115 
116 		case 8000:
117 			value = TMP112_CONV_RATE(TMP112_CONV_RATE_8);
118 			break;
119 
120 		default:
121 			return -ENOTSUP;
122 		}
123 
124 		if (tmp112_update_config(dev, TMP112_CONV_RATE_MASK, value) < 0) {
125 			LOG_DBG("Failed to set attribute!");
126 			return -EIO;
127 		}
128 
129 		break;
130 #endif
131 
132 	default:
133 		return -ENOTSUP;
134 	}
135 
136 	return 0;
137 }
138 
tmp112_sample_fetch(const struct device * dev,enum sensor_channel chan)139 static int tmp112_sample_fetch(const struct device *dev,
140 			       enum sensor_channel chan)
141 {
142 	struct tmp112_data *drv_data = dev->data;
143 	const struct tmp112_config *cfg = dev->config;
144 	uint16_t val;
145 
146 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
147 
148 	if (tmp112_reg_read(cfg, TMP112_REG_TEMPERATURE, &val) < 0) {
149 		return -EIO;
150 	}
151 
152 	if (val & TMP112_DATA_EXTENDED) {
153 		drv_data->sample = arithmetic_shift_right((int16_t)val,
154 							  TMP112_DATA_EXTENDED_SHIFT);
155 	} else {
156 		drv_data->sample = arithmetic_shift_right((int16_t)val,
157 							  TMP112_DATA_NORMAL_SHIFT);
158 	}
159 
160 	return 0;
161 }
162 
tmp112_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)163 static int tmp112_channel_get(const struct device *dev,
164 			      enum sensor_channel chan,
165 			      struct sensor_value *val)
166 {
167 	struct tmp112_data *drv_data = dev->data;
168 	int32_t uval;
169 
170 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
171 		return -ENOTSUP;
172 	}
173 
174 	uval = (int32_t)drv_data->sample * TMP112_TEMP_SCALE;
175 	val->val1 = uval / 1000000;
176 	val->val2 = uval % 1000000;
177 
178 	return 0;
179 }
180 
181 static DEVICE_API(sensor, tmp112_driver_api) = {
182 	.attr_set = tmp112_attr_set,
183 	.sample_fetch = tmp112_sample_fetch,
184 	.channel_get = tmp112_channel_get,
185 };
186 
tmp112_init(const struct device * dev)187 int tmp112_init(const struct device *dev)
188 {
189 	const struct tmp112_config *cfg = dev->config;
190 	struct tmp112_data *data = dev->data;
191 
192 	if (!device_is_ready(cfg->bus.bus)) {
193 		LOG_ERR("I2C dev %s not ready", cfg->bus.bus->name);
194 		return -EINVAL;
195 	}
196 
197 	data->config_reg = TMP112_CONV_RATE(cfg->cr) | TMP112_CONV_RES_MASK
198 			   | (cfg->extended_mode ? TMP112_CONFIG_EM : 0);
199 
200 	return tmp112_update_config(dev, 0, 0);
201 }
202 
203 
204 #define TMP112_INST(inst)						    \
205 	static struct tmp112_data tmp112_data_##inst;			    \
206 	static const struct tmp112_config tmp112_config_##inst = {	    \
207 		.bus = I2C_DT_SPEC_INST_GET(inst),			    \
208 		.cr = DT_INST_ENUM_IDX(inst, conversion_rate),		    \
209 		.extended_mode = DT_INST_PROP(inst, extended_mode),	    \
210 	};								    \
211 									    \
212 	SENSOR_DEVICE_DT_INST_DEFINE(inst, tmp112_init, NULL, &tmp112_data_##inst, \
213 			      &tmp112_config_##inst, POST_KERNEL,	    \
214 			      CONFIG_SENSOR_INIT_PRIORITY, &tmp112_driver_api);
215 
216 DT_INST_FOREACH_STATUS_OKAY(TMP112_INST)
217