1 /* ST Microelectronics LPS2XDF pressure and temperature sensor
2  *
3  * Copyright (c) 2023 STMicroelectronics
4  * Copyright (c) 2023 PHYTEC Messtechnik GmbH
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Datasheet:
9  * https://www.st.com/resource/en/datasheet/lps22df.pdf
10  * https://www.st.com/resource/en/datasheet/lps28df.pdf
11  */
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 "lps2xdf.h"
22 
23 #if DT_HAS_COMPAT_STATUS_OKAY(st_lps22df)
24 #include "lps22df.h"
25 #endif
26 
27 #if DT_HAS_COMPAT_STATUS_OKAY(st_lps28dfw)
28 #include "lps28dfw.h"
29 #endif
30 
31 LOG_MODULE_REGISTER(LPS2XDF, CONFIG_SENSOR_LOG_LEVEL);
32 
33 static const uint16_t lps2xdf_map[] = {0, 1, 4, 10, 25, 50, 75, 100, 200};
34 
lps2xdf_odr_set(const struct device * dev,uint16_t freq)35 static int lps2xdf_odr_set(const struct device *dev, uint16_t freq)
36 {
37 	int odr;
38 	const struct lps2xdf_config *const cfg = dev->config;
39 	const struct lps2xdf_chip_api *chip_api = cfg->chip_api;
40 
41 	for (odr = 0; odr < ARRAY_SIZE(lps2xdf_map); odr++) {
42 		if (freq == lps2xdf_map[odr]) {
43 			break;
44 		}
45 	}
46 
47 	if (odr == ARRAY_SIZE(lps2xdf_map)) {
48 		LOG_DBG("bad frequency");
49 		return -EINVAL;
50 	}
51 
52 	if (chip_api->mode_set_odr_raw(dev, odr)) {
53 		LOG_DBG("failed to set sampling rate");
54 		return -EIO;
55 	}
56 
57 	return 0;
58 }
59 
lps2xdf_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)60 static int lps2xdf_attr_set(const struct device *dev, enum sensor_channel chan,
61 			    enum sensor_attribute attr, const struct sensor_value *val)
62 {
63 	if (chan != SENSOR_CHAN_ALL) {
64 		LOG_WRN("attr_set() not supported on this channel.");
65 		return -ENOTSUP;
66 	}
67 
68 	switch (attr) {
69 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
70 		return lps2xdf_odr_set(dev, val->val1);
71 	default:
72 		LOG_DBG("operation not supported.");
73 		return -ENOTSUP;
74 	}
75 
76 	return 0;
77 }
78 
lps2xdf_press_convert(const struct device * dev,struct sensor_value * val,int32_t raw_val)79 static inline void lps2xdf_press_convert(const struct device *dev,
80 					 struct sensor_value *val,
81 					 int32_t raw_val)
82 {
83 	const struct lps2xdf_config *const cfg = dev->config;
84 	int32_t press_tmp = raw_val >> 8; /* raw value is left aligned (24 msb) */
85 	int divider;
86 
87 	/* Pressure sensitivity is:
88 	 * - 4096 LSB/hPa for Full-Scale of 260 - 1260 hPa:
89 	 * - 2048 LSB/hPa for Full-Scale of 260 - 4060 hPa:
90 	 * Also convert hPa into kPa
91 	 */
92 	if (cfg->fs == 0) {
93 		divider = 40960;
94 	} else {
95 		divider = 20480;
96 	}
97 	val->val1 = press_tmp / divider;
98 
99 	/* For the decimal part use (3125 / 128) as a factor instead of
100 	 * (1000000 / 40960) to avoid int32 overflow
101 	 */
102 	val->val2 = (press_tmp % divider) * 3125 / 128;
103 }
104 
105 
lps2xdf_temp_convert(struct sensor_value * val,int16_t raw_val)106 static inline void lps2xdf_temp_convert(struct sensor_value *val, int16_t raw_val)
107 {
108 	/* Temperature sensitivity is 100 LSB/deg C */
109 	val->val1 = raw_val / 100;
110 	val->val2 = ((int32_t)raw_val % 100) * 10000;
111 }
112 
lps2xdf_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)113 static int lps2xdf_channel_get(const struct device *dev, enum sensor_channel chan,
114 			       struct sensor_value *val)
115 {
116 	struct lps2xdf_data *data = dev->data;
117 
118 	if (chan == SENSOR_CHAN_PRESS) {
119 		lps2xdf_press_convert(dev, val, data->sample_press);
120 	} else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
121 		lps2xdf_temp_convert(val, data->sample_temp);
122 	} else {
123 		return -ENOTSUP;
124 	}
125 
126 	return 0;
127 }
128 
lps2xdf_sample_fetch(const struct device * dev,enum sensor_channel chan)129 static int lps2xdf_sample_fetch(const struct device *dev, enum sensor_channel chan)
130 {
131 	const struct lps2xdf_config *const cfg = dev->config;
132 	const struct lps2xdf_chip_api *chip_api = cfg->chip_api;
133 
134 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
135 
136 	return chip_api->sample_fetch(dev, chan);
137 }
138 
139 static const struct sensor_driver_api lps2xdf_driver_api = {
140 	.attr_set = lps2xdf_attr_set,
141 	.sample_fetch = lps2xdf_sample_fetch,
142 	.channel_get = lps2xdf_channel_get,
143 #if CONFIG_LPS2XDF_TRIGGER
144 	.trigger_set = lps2xdf_trigger_set,
145 #endif
146 };
147 
148 #ifdef CONFIG_LPS2XDF_TRIGGER
149 #define LPS2XDF_CFG_IRQ(inst)                                                  \
150 	.trig_enabled = true,                                                  \
151 	.gpio_int = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios),                   \
152 	.drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed)
153 #else
154 #define LPS2XDF_CFG_IRQ(inst)
155 #endif /* CONFIG_LPS2XDF_TRIGGER */
156 
157 #define LPS2XDF_CONFIG_COMMON(inst, name)                                      \
158 	.odr = DT_INST_PROP(inst, odr),                                        \
159 	.lpf = DT_INST_PROP(inst, lpf),                                        \
160 	.avg = DT_INST_PROP(inst, avg),                                        \
161 	.chip_api = &name##_chip_api,                                          \
162 	IF_ENABLED(DT_INST_NODE_HAS_COMPAT(inst, st_lps28dfw),                 \
163 		   (.fs = DT_INST_PROP(inst, fs),))                            \
164 	IF_ENABLED(DT_INST_NODE_HAS_PROP(inst, drdy_gpios),                    \
165 		   (LPS2XDF_CFG_IRQ(inst)))
166 
167 #define LPS2XDF_SPI_OPERATION (SPI_WORD_SET(8) | SPI_OP_MODE_MASTER |          \
168 			       SPI_MODE_CPOL | SPI_MODE_CPHA)
169 
170 #define LPS2XDF_CONFIG_SPI(inst, name)                                         \
171 {                                                                              \
172 	STMEMSC_CTX_SPI(&lps2xdf_config_##name##_##inst.stmemsc_cfg),          \
173 	.stmemsc_cfg = {                                                       \
174 		.spi = SPI_DT_SPEC_INST_GET(inst, LPS2XDF_SPI_OPERATION, 0),   \
175 	},                                                                     \
176 	LPS2XDF_CONFIG_COMMON(inst, name)                                      \
177 }
178 
179 #define LPS2XDF_CONFIG_I2C(inst, name)                                         \
180 {                                                                              \
181 	STMEMSC_CTX_I2C(&lps2xdf_config_##name##_##inst.stmemsc_cfg),          \
182 	.stmemsc_cfg = {                                                       \
183 		.i2c = I2C_DT_SPEC_INST_GET(inst),                             \
184 	},                                                                     \
185 	LPS2XDF_CONFIG_COMMON(inst, name)                                      \
186 }
187 
188 #define LPS2XDF_CONFIG_I3C(inst, name)                                         \
189 {                                                                              \
190 	STMEMSC_CTX_I3C(&lps2xdf_config_##name##_##inst.stmemsc_cfg),          \
191 	.stmemsc_cfg = {                                                       \
192 		.i3c = &lps2xdf_data_##name##_##inst.i3c_dev,                  \
193 	},                                                                     \
194 	.i3c.bus = DEVICE_DT_GET(DT_INST_BUS(inst)),                           \
195 	.i3c.dev_id = I3C_DEVICE_ID_DT_INST(inst),                             \
196 	LPS2XDF_CONFIG_COMMON(inst, name)                                      \
197 }
198 
199 #define LPS2XDF_CONFIG_I3C_OR_I2C(inst, name)                                  \
200 	COND_CODE_0(DT_INST_PROP_BY_IDX(inst, reg, 1),                         \
201 		    (LPS2XDF_CONFIG_I2C(inst, name)),                          \
202 		    (LPS2XDF_CONFIG_I3C(inst, name)))
203 
204 #define LPS2XDF_DEFINE(inst, name)                                                           \
205 	static struct lps2xdf_data lps2xdf_data_##name##_##inst;                             \
206 	static const struct lps2xdf_config lps2xdf_config_##name##_##inst = COND_CODE_1(     \
207 		DT_INST_ON_BUS(inst, spi),                                                   \
208 		(LPS2XDF_CONFIG_SPI(inst, name)),                                            \
209 		(COND_CODE_1(DT_INST_ON_BUS(inst, i3c),                                      \
210 			     (LPS2XDF_CONFIG_I3C_OR_I2C(inst, name)),                        \
211 			     (LPS2XDF_CONFIG_I2C(inst, name)))));                            \
212 											     \
213 	SENSOR_DEVICE_DT_INST_DEFINE(inst, name##_init, NULL, &lps2xdf_data_##name##_##inst, \
214 				     &lps2xdf_config_##name##_##inst, POST_KERNEL,           \
215 				     CONFIG_SENSOR_INIT_PRIORITY, &lps2xdf_driver_api);
216 
217 #define DT_DRV_COMPAT st_lps22df
218 DT_INST_FOREACH_STATUS_OKAY_VARGS(LPS2XDF_DEFINE, DT_DRV_COMPAT)
219 #undef DT_DRV_COMPAT
220 
221 #define DT_DRV_COMPAT st_lps28dfw
222 DT_INST_FOREACH_STATUS_OKAY_VARGS(LPS2XDF_DEFINE, DT_DRV_COMPAT)
223 #undef DT_DRV_COMPAT
224