1 /* ST Microelectronics STTS751 temperature sensor
2  *
3  * Copyright (c) 2019 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/stts751.pdf
9  */
10 
11 #define DT_DRV_COMPAT st_stts751
12 
13 #include <zephyr/drivers/sensor.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/device.h>
16 #include <zephyr/init.h>
17 #include <zephyr/sys/byteorder.h>
18 #include <zephyr/sys/__assert.h>
19 #include <zephyr/logging/log.h>
20 
21 #include "stts751.h"
22 
23 LOG_MODULE_REGISTER(STTS751, CONFIG_SENSOR_LOG_LEVEL);
24 
stts751_set_odr_raw(const struct device * dev,uint8_t odr)25 static inline int stts751_set_odr_raw(const struct device *dev, uint8_t odr)
26 {
27 	struct stts751_data *data = dev->data;
28 
29 	return stts751_temp_data_rate_set(data->ctx, odr);
30 }
31 
stts751_sample_fetch(const struct device * dev,enum sensor_channel chan)32 static int stts751_sample_fetch(const struct device *dev,
33 				enum sensor_channel chan)
34 {
35 	struct stts751_data *data = dev->data;
36 	int16_t raw_temp;
37 
38 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
39 
40 	if (stts751_temperature_raw_get(data->ctx, &raw_temp) < 0) {
41 		LOG_DBG("Failed to read sample");
42 		return -EIO;
43 	}
44 
45 	data->sample_temp = raw_temp;
46 
47 	return 0;
48 }
49 
stts751_temp_convert(struct sensor_value * val,int16_t raw_val)50 static inline void stts751_temp_convert(struct sensor_value *val,
51 					int16_t raw_val)
52 {
53 	val->val1 = raw_val / 256;
54 	val->val2 = ((int32_t)raw_val % 256) * 10000;
55 }
56 
stts751_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)57 static int stts751_channel_get(const struct device *dev,
58 			       enum sensor_channel chan,
59 			       struct sensor_value *val)
60 {
61 	struct stts751_data *data = dev->data;
62 
63 	if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
64 		stts751_temp_convert(val, data->sample_temp);
65 	} else {
66 		return -ENOTSUP;
67 	}
68 
69 	return 0;
70 }
71 
72 static const struct {
73 	int32_t rate;
74 	int32_t rate_dec;
75 } stts751_map[] = {
76 			{0, 62500},
77 			{0, 125000},
78 			{0, 250000},
79 			{0, 500000},
80 			{1, 0},
81 			{2, 0},
82 			{4, 0},
83 			{8, 0},
84 			{16, 0},
85 			{32, 0},
86 		};
87 
stts751_odr_set(const struct device * dev,const struct sensor_value * val)88 static int stts751_odr_set(const struct device *dev,
89 			   const struct sensor_value *val)
90 {
91 	int odr;
92 
93 	for (odr = 0; odr < ARRAY_SIZE(stts751_map); odr++) {
94 		if (val->val1 == stts751_map[odr].rate &&
95 		    val->val2 == stts751_map[odr].rate_dec) {
96 			break;
97 		}
98 	}
99 
100 	if (odr == ARRAY_SIZE(stts751_map)) {
101 		LOG_DBG("bad frequency");
102 		return -EINVAL;
103 	}
104 
105 	if (stts751_set_odr_raw(dev, odr) < 0) {
106 		LOG_DBG("failed to set sampling rate");
107 		return -EIO;
108 	}
109 
110 	return 0;
111 }
112 
stts751_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)113 static int stts751_attr_set(const struct device *dev,
114 			    enum sensor_channel chan,
115 			    enum sensor_attribute attr,
116 			    const struct sensor_value *val)
117 {
118 	if (chan != SENSOR_CHAN_ALL) {
119 		LOG_WRN("attr_set() not supported on this channel.");
120 		return -ENOTSUP;
121 	}
122 
123 	switch (attr) {
124 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
125 		return stts751_odr_set(dev, val);
126 	default:
127 		LOG_DBG("operation not supported.");
128 		return -ENOTSUP;
129 	}
130 
131 	return 0;
132 }
133 
134 static const struct sensor_driver_api stts751_api_funcs = {
135 	.attr_set = stts751_attr_set,
136 	.sample_fetch = stts751_sample_fetch,
137 	.channel_get = stts751_channel_get,
138 #if CONFIG_STTS751_TRIGGER
139 	.trigger_set = stts751_trigger_set,
140 #endif
141 };
142 
stts751_init_chip(const struct device * dev)143 static int stts751_init_chip(const struct device *dev)
144 {
145 	struct stts751_data *data = dev->data;
146 	stts751_id_t chip_id;
147 
148 	if (stts751_device_id_get(data->ctx, &chip_id) < 0) {
149 		LOG_DBG("Failed reading chip id");
150 		return -EIO;
151 	}
152 
153 	if (chip_id.manufacturer_id != STTS751_ID_MAN) {
154 		LOG_DBG("Invalid chip id 0x%x", chip_id.manufacturer_id);
155 		return -EIO;
156 	}
157 
158 	if (stts751_set_odr_raw(dev, CONFIG_STTS751_SAMPLING_RATE) < 0) {
159 		LOG_DBG("Failed to set sampling rate");
160 		return -EIO;
161 	}
162 
163 	if (stts751_resolution_set(data->ctx, STTS751_11bit) < 0) {
164 		LOG_DBG("Failed to set resolution");
165 		return -EIO;
166 	}
167 
168 	return 0;
169 }
170 
stts751_init(const struct device * dev)171 static int stts751_init(const struct device *dev)
172 {
173 	const struct stts751_config * const config = dev->config;
174 	struct stts751_data *data = dev->data;
175 
176 	data->dev = dev;
177 
178 	if (!device_is_ready(config->i2c.bus)) {
179 		LOG_ERR("Bus device is not ready");
180 		return -ENODEV;
181 	}
182 
183 	config->bus_init(dev);
184 
185 	if (stts751_init_chip(dev) < 0) {
186 		LOG_DBG("Failed to initialize chip");
187 		return -EIO;
188 	}
189 
190 #ifdef CONFIG_STTS751_TRIGGER
191 	if (config->int_gpio.port) {
192 		if (stts751_init_interrupt(dev) < 0) {
193 			LOG_ERR("Failed to initialize interrupt.");
194 			return -EIO;
195 		}
196 	}
197 #endif
198 
199 	return 0;
200 }
201 
202 #define STTS751_DEFINE(inst)									\
203 	static struct stts751_data stts751_data_##inst;						\
204 												\
205 	static const struct stts751_config stts751_config_##inst = {				\
206 		COND_CODE_1(DT_INST_ON_BUS(inst, i2c),						\
207 			    (.i2c = I2C_DT_SPEC_INST_GET(inst),					\
208 			     .bus_init = stts751_i2c_init,),					\
209 			    ())									\
210 		IF_ENABLED(CONFIG_STTS751_TRIGGER,						\
211 			   (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, drdy_gpios, { 0 }),))	\
212 	};											\
213 												\
214 	SENSOR_DEVICE_DT_INST_DEFINE(inst, stts751_init, NULL,					\
215 			      &stts751_data_##inst, &stts751_config_##inst, POST_KERNEL,	\
216 			      CONFIG_SENSOR_INIT_PRIORITY, &stts751_api_funcs);			\
217 
218 DT_INST_FOREACH_STATUS_OKAY(STTS751_DEFINE)
219