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 /* val = raw_val / 40960 */
73 val->val1 = raw_val / 40960;
74 val->val2 = ((int32_t)raw_val * 1000000 / 40960) % 1000000;
75 }
76
lps25hb_temp_convert(struct sensor_value * val,int16_t raw_val)77 static inline void lps25hb_temp_convert(struct sensor_value *val,
78 int16_t raw_val)
79 {
80 int32_t uval;
81
82 /* val = raw_val / 480 + 42.5 */
83 uval = (int32_t)raw_val * 1000000 / 480 + 42500000;
84 val->val1 = (raw_val * 10 / 480 + 425) / 10;
85 val->val2 = uval % 1000000;
86 }
87
lps25hb_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)88 static int lps25hb_channel_get(const struct device *dev,
89 enum sensor_channel chan,
90 struct sensor_value *val)
91 {
92 struct lps25hb_data *data = dev->data;
93
94 if (chan == SENSOR_CHAN_PRESS) {
95 lps25hb_press_convert(val, data->sample_press);
96 } else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
97 lps25hb_temp_convert(val, data->sample_temp);
98 } else {
99 return -ENOTSUP;
100 }
101
102 return 0;
103 }
104
105 static const struct sensor_driver_api lps25hb_api_funcs = {
106 .sample_fetch = lps25hb_sample_fetch,
107 .channel_get = lps25hb_channel_get,
108 };
109
lps25hb_init_chip(const struct device * dev)110 static int lps25hb_init_chip(const struct device *dev)
111 {
112 const struct lps25hb_config *config = dev->config;
113 uint8_t chip_id;
114
115 lps25hb_power_ctrl(dev, 0);
116 k_busy_wait(USEC_PER_MSEC * 50U);
117
118 if (lps25hb_power_ctrl(dev, 1) < 0) {
119 LOG_DBG("failed to power on device");
120 return -EIO;
121 }
122
123 k_busy_wait(USEC_PER_MSEC * 20U);
124
125 if (i2c_reg_read_byte_dt(&config->i2c, LPS25HB_REG_WHO_AM_I,
126 &chip_id) < 0) {
127 LOG_DBG("failed reading chip id");
128 goto err_poweroff;
129 }
130 if (chip_id != LPS25HB_VAL_WHO_AM_I) {
131 LOG_DBG("invalid chip id 0x%x", chip_id);
132 goto err_poweroff;
133 }
134
135 LOG_DBG("chip id 0x%x", chip_id);
136
137 if (lps25hb_set_odr_raw(dev, LPS25HB_DEFAULT_SAMPLING_RATE)
138 < 0) {
139 LOG_DBG("failed to set sampling rate");
140 goto err_poweroff;
141 }
142
143 if (i2c_reg_update_byte_dt(&config->i2c, LPS25HB_REG_CTRL_REG1,
144 LPS25HB_MASK_CTRL_REG1_BDU,
145 (1 << LPS25HB_SHIFT_CTRL_REG1_BDU)) < 0) {
146 LOG_DBG("failed to set BDU");
147 goto err_poweroff;
148 }
149
150 return 0;
151
152 err_poweroff:
153 lps25hb_power_ctrl(dev, 0);
154 return -EIO;
155 }
156
lps25hb_init(const struct device * dev)157 static int lps25hb_init(const struct device *dev)
158 {
159 const struct lps25hb_config * const config = dev->config;
160
161 if (!device_is_ready(config->i2c.bus)) {
162 LOG_ERR("I2C bus device not ready");
163 return -ENODEV;
164 }
165
166 if (lps25hb_init_chip(dev) < 0) {
167 LOG_DBG("failed to initialize chip");
168 return -EIO;
169 }
170
171 return 0;
172 }
173
174 #define LPS25HB_DEFINE(inst) \
175 static struct lps25hb_data lps25hb_data_##inst; \
176 \
177 static const struct lps25hb_config lps25hb_config_##inst = { \
178 .i2c = I2C_DT_SPEC_INST_GET(inst), \
179 }; \
180 \
181 SENSOR_DEVICE_DT_INST_DEFINE(inst, lps25hb_init, NULL, \
182 &lps25hb_data_##inst, &lps25hb_config_##inst, POST_KERNEL, \
183 CONFIG_SENSOR_INIT_PRIORITY, &lps25hb_api_funcs); \
184
185 DT_INST_FOREACH_STATUS_OKAY(LPS25HB_DEFINE)
186