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 <drivers/sensor.h>
14 #include <kernel.h>
15 #include <device.h>
16 #include <init.h>
17 #include <sys/byteorder.h>
18 #include <sys/__assert.h>
19 #include <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 	data->bus = device_get_binding(config->master_dev_name);
179 	if (!data->bus) {
180 		LOG_DBG("bus master not found: %s", config->master_dev_name);
181 		return -EINVAL;
182 	}
183 
184 	config->bus_init(dev);
185 
186 	if (stts751_init_chip(dev) < 0) {
187 		LOG_DBG("Failed to initialize chip");
188 		return -EIO;
189 	}
190 
191 #ifdef CONFIG_STTS751_TRIGGER
192 	if (stts751_init_interrupt(dev) < 0) {
193 		LOG_ERR("Failed to initialize interrupt.");
194 		return -EIO;
195 	}
196 #endif
197 
198 	return 0;
199 }
200 
201 static struct stts751_data stts751_data;
202 
203 static const struct stts751_config stts751_config = {
204 	.master_dev_name = DT_INST_BUS_LABEL(0),
205 #ifdef CONFIG_STTS751_TRIGGER
206 	.event_port	= DT_INST_GPIO_LABEL(0, drdy_gpios),
207 	.event_pin	= DT_INST_GPIO_PIN(0, drdy_gpios),
208 	.int_flags	= DT_INST_GPIO_FLAGS(0, drdy_gpios),
209 #endif
210 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
211 	.bus_init = stts751_i2c_init,
212 	.i2c_slv_addr = DT_INST_REG_ADDR(0),
213 #else
214 #error "BUS MACRO NOT DEFINED IN DTS"
215 #endif
216 };
217 
218 DEVICE_DT_INST_DEFINE(0, stts751_init, NULL,
219 		    &stts751_data, &stts751_config, POST_KERNEL,
220 		    CONFIG_SENSOR_INIT_PRIORITY, &stts751_api_funcs);
221