1 /* lps25hb.c - Driver for LPS25HB pressure and temperature sensor */
2
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define DT_DRV_COMPAT st_lps25hb_press
10
11 #include <zephyr/drivers/sensor.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/device.h>
14 #include <zephyr/init.h>
15 #include <zephyr/sys/byteorder.h>
16 #include <zephyr/sys/__assert.h>
17 #include <zephyr/logging/log.h>
18
19 #include "lps25hb.h"
20
21 LOG_MODULE_REGISTER(LPS25HB, CONFIG_SENSOR_LOG_LEVEL);
22
lps25hb_power_ctrl(const struct device * dev,uint8_t value)23 static inline int lps25hb_power_ctrl(const struct device *dev, uint8_t value)
24 {
25 const struct lps25hb_config *config = dev->config;
26
27 return i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
28 LPS25HB_MASK_CTRL_REG1_PD,
29 value << LPS25HB_SHIFT_CTRL_REG1_PD);
30 }
31
lps25hb_set_odr_raw(const struct device * dev,uint8_t odr)32 static inline int lps25hb_set_odr_raw(const struct device *dev, uint8_t odr)
33 {
34 const struct lps25hb_config *config = dev->config;
35
36 return i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
37 LPS25HB_MASK_CTRL_REG1_ODR,
38 odr << LPS25HB_SHIFT_CTRL_REG1_ODR);
39 }
40
lps25hb_sample_fetch(const struct device * dev,enum sensor_channel chan)41 static int lps25hb_sample_fetch(const struct device *dev,
42 enum sensor_channel chan)
43 {
44 struct lps25hb_data *data = dev->data;
45 const struct lps25hb_config *config = dev->config;
46 uint8_t out[5];
47 int offset;
48
49 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
50
51 for (offset = 0; offset < sizeof(out); ++offset) {
52 if (i2c_reg_read_byte_dt(&config->i2c,
53 LPS25HB_REG_PRESS_OUT_XL + offset,
54 out + offset) < 0) {
55 LOG_DBG("failed to read sample");
56 return -EIO;
57 }
58 }
59
60 data->sample_press = (int32_t)((uint32_t)(out[0]) |
61 ((uint32_t)(out[1]) << 8) |
62 ((uint32_t)(out[2]) << 16));
63 data->sample_temp = (int16_t)((uint16_t)(out[3]) |
64 ((uint16_t)(out[4]) << 8));
65
66 return 0;
67 }
68
lps25hb_press_convert(struct sensor_value * val,int32_t raw_val)69 static inline void lps25hb_press_convert(struct sensor_value *val,
70 int32_t raw_val)
71 {
72 /* Pressure sensitivity is 4096 LSB/hPa */
73 /* Also convert hPa into kPa */
74 val->val1 = raw_val / 40960;
75
76 /* For the decimal part use (3125 / 128) as a factor instead of
77 * (1000000 / 40960) to avoid int32 overflow
78 */
79 val->val2 = (raw_val % 40960) * 3125 / 128;
80 }
81
lps25hb_temp_convert(struct sensor_value * val,int16_t raw_val)82 static inline void lps25hb_temp_convert(struct sensor_value *val,
83 int16_t raw_val)
84 {
85 int32_t uval;
86
87 /* val = raw_val / 480 + 42.5 */
88 uval = (int32_t)raw_val * 1000000 / 480 + 42500000;
89 val->val1 = (raw_val * 10 / 480 + 425) / 10;
90 val->val2 = uval % 1000000;
91 }
92
lps25hb_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)93 static int lps25hb_channel_get(const struct device *dev,
94 enum sensor_channel chan,
95 struct sensor_value *val)
96 {
97 struct lps25hb_data *data = dev->data;
98
99 if (chan == SENSOR_CHAN_PRESS) {
100 lps25hb_press_convert(val, data->sample_press);
101 } else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
102 lps25hb_temp_convert(val, data->sample_temp);
103 } else {
104 return -ENOTSUP;
105 }
106
107 return 0;
108 }
109
110 static DEVICE_API(sensor, lps25hb_api_funcs) = {
111 .sample_fetch = lps25hb_sample_fetch,
112 .channel_get = lps25hb_channel_get,
113 };
114
lps25hb_init_chip(const struct device * dev)115 static int lps25hb_init_chip(const struct device *dev)
116 {
117 const struct lps25hb_config *config = dev->config;
118 uint8_t chip_id;
119
120 lps25hb_power_ctrl(dev, 0);
121 k_busy_wait(USEC_PER_MSEC * 50U);
122
123 if (lps25hb_power_ctrl(dev, 1) < 0) {
124 LOG_DBG("failed to power on device");
125 return -EIO;
126 }
127
128 k_busy_wait(USEC_PER_MSEC * 20U);
129
130 if (i2c_reg_read_byte_dt(&config->i2c, LPS25HB_REG_WHO_AM_I,
131 &chip_id) < 0) {
132 LOG_DBG("failed reading chip id");
133 goto err_poweroff;
134 }
135 if (chip_id != LPS25HB_VAL_WHO_AM_I) {
136 LOG_DBG("invalid chip id 0x%x", chip_id);
137 goto err_poweroff;
138 }
139
140 LOG_DBG("chip id 0x%x", chip_id);
141
142 if (lps25hb_set_odr_raw(dev, LPS25HB_DEFAULT_SAMPLING_RATE)
143 < 0) {
144 LOG_DBG("failed to set sampling rate");
145 goto err_poweroff;
146 }
147
148 if (i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
149 LPS25HB_MASK_CTRL_REG1_BDU,
150 (1 << LPS25HB_SHIFT_CTRL_REG1_BDU)) < 0) {
151 LOG_DBG("failed to set BDU");
152 goto err_poweroff;
153 }
154
155 return 0;
156
157 err_poweroff:
158 lps25hb_power_ctrl(dev, 0);
159 return -EIO;
160 }
161
lps25hb_init(const struct device * dev)162 static int lps25hb_init(const struct device *dev)
163 {
164 const struct lps25hb_config * const config = dev->config;
165
166 if (!device_is_ready(config->i2c.bus)) {
167 LOG_ERR("I2C bus device not ready");
168 return -ENODEV;
169 }
170
171 if (lps25hb_init_chip(dev) < 0) {
172 LOG_DBG("failed to initialize chip");
173 return -EIO;
174 }
175
176 return 0;
177 }
178
179 #define LPS25HB_DEFINE(inst) \
180 static struct lps25hb_data lps25hb_data_##inst; \
181 \
182 static const struct lps25hb_config lps25hb_config_##inst = { \
183 .i2c = I2C_DT_SPEC_INST_GET(inst), \
184 }; \
185 \
186 SENSOR_DEVICE_DT_INST_DEFINE(inst, lps25hb_init, NULL, \
187 &lps25hb_data_##inst, &lps25hb_config_##inst, POST_KERNEL, \
188 CONFIG_SENSOR_INIT_PRIORITY, &lps25hb_api_funcs); \
189
190 DT_INST_FOREACH_STATUS_OKAY(LPS25HB_DEFINE)
191