1 /* lps22hb.c - Driver for LPS22HB pressure and temperature sensor */
2
3 /*
4 * Copyright (c) 2017 Linaro Limited
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define DT_DRV_COMPAT st_lps22hb_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 "lps22hb.h"
20
21 LOG_MODULE_REGISTER(LPS22HB, CONFIG_SENSOR_LOG_LEVEL);
22
lps22hb_set_odr_raw(const struct device * dev,uint8_t odr)23 static inline int lps22hb_set_odr_raw(const struct device *dev, uint8_t odr)
24 {
25 const struct lps22hb_config *config = dev->config;
26
27 return i2c_reg_update_byte_dt(&config->i2c, LPS22HB_REG_CTRL_REG1,
28 LPS22HB_MASK_CTRL_REG1_ODR,
29 odr << LPS22HB_SHIFT_CTRL_REG1_ODR);
30 }
31
lps22hb_sample_fetch(const struct device * dev,enum sensor_channel chan)32 static int lps22hb_sample_fetch(const struct device *dev,
33 enum sensor_channel chan)
34 {
35 struct lps22hb_data *data = dev->data;
36 const struct lps22hb_config *config = dev->config;
37 uint8_t out[5];
38
39 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
40
41 if (i2c_burst_read_dt(&config->i2c, LPS22HB_REG_PRESS_OUT_XL,
42 out, 5) < 0) {
43 LOG_DBG("Failed to read sample");
44 return -EIO;
45 }
46
47 data->sample_press = (int32_t)((uint32_t)(out[0]) |
48 ((uint32_t)(out[1]) << 8) |
49 ((uint32_t)(out[2]) << 16));
50 data->sample_temp = (int16_t)((uint16_t)(out[3]) |
51 ((uint16_t)(out[4]) << 8));
52
53 return 0;
54 }
55
lps22hb_press_convert(struct sensor_value * val,int32_t raw_val)56 static inline void lps22hb_press_convert(struct sensor_value *val,
57 int32_t raw_val)
58 {
59 /* Pressure sensitivity is 4096 LSB/hPa */
60 /* Convert raw_val to val in kPa */
61 val->val1 = (raw_val >> 12) / 10;
62 val->val2 = (raw_val >> 12) % 10 * 100000 +
63 (((int32_t)((raw_val) & 0x0FFF) * 100000L) >> 12);
64 }
65
lps22hb_temp_convert(struct sensor_value * val,int16_t raw_val)66 static inline void lps22hb_temp_convert(struct sensor_value *val,
67 int16_t raw_val)
68 {
69 /* Temperature sensitivity is 100 LSB/deg C */
70 val->val1 = raw_val / 100;
71 val->val2 = ((int32_t)raw_val % 100) * 10000;
72 }
73
lps22hb_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)74 static int lps22hb_channel_get(const struct device *dev,
75 enum sensor_channel chan,
76 struct sensor_value *val)
77 {
78 struct lps22hb_data *data = dev->data;
79
80 if (chan == SENSOR_CHAN_PRESS) {
81 lps22hb_press_convert(val, data->sample_press);
82 } else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
83 lps22hb_temp_convert(val, data->sample_temp);
84 } else {
85 return -ENOTSUP;
86 }
87
88 return 0;
89 }
90
91 static const struct sensor_driver_api lps22hb_api_funcs = {
92 .sample_fetch = lps22hb_sample_fetch,
93 .channel_get = lps22hb_channel_get,
94 };
95
lps22hb_init_chip(const struct device * dev)96 static int lps22hb_init_chip(const struct device *dev)
97 {
98 const struct lps22hb_config *config = dev->config;
99 uint8_t chip_id;
100
101 if (i2c_reg_read_byte_dt(&config->i2c, LPS22HB_REG_WHO_AM_I,
102 &chip_id) < 0) {
103 LOG_DBG("Failed reading chip id");
104 goto err_poweroff;
105 }
106
107 if (chip_id != LPS22HB_VAL_WHO_AM_I) {
108 LOG_DBG("Invalid chip id 0x%x", chip_id);
109 goto err_poweroff;
110 }
111
112 if (lps22hb_set_odr_raw(dev, LPS22HB_DEFAULT_SAMPLING_RATE) < 0) {
113 LOG_DBG("Failed to set sampling rate");
114 goto err_poweroff;
115 }
116
117 if (i2c_reg_update_byte_dt(&config->i2c, LPS22HB_REG_CTRL_REG1,
118 LPS22HB_MASK_CTRL_REG1_BDU,
119 (1 << LPS22HB_SHIFT_CTRL_REG1_BDU)) < 0) {
120 LOG_DBG("Failed to set BDU");
121 goto err_poweroff;
122 }
123
124 return 0;
125
126 err_poweroff:
127 return -EIO;
128 }
129
lps22hb_init(const struct device * dev)130 static int lps22hb_init(const struct device *dev)
131 {
132 const struct lps22hb_config * const config = dev->config;
133
134 if (!device_is_ready(config->i2c.bus)) {
135 LOG_ERR("I2C bus device not ready");
136 return -ENODEV;
137 }
138
139 if (lps22hb_init_chip(dev) < 0) {
140 LOG_DBG("Failed to initialize chip");
141 return -EIO;
142 }
143
144 return 0;
145 }
146
147 #define LPS22HB_DEFINE(inst) \
148 static struct lps22hb_data lps22hb_data_##inst; \
149 \
150 static const struct lps22hb_config lps22hb_config_##inst = { \
151 .i2c = I2C_DT_SPEC_INST_GET(inst), \
152 }; \
153 \
154 SENSOR_DEVICE_DT_INST_DEFINE(inst, lps22hb_init, NULL, \
155 &lps22hb_data_##inst, &lps22hb_config_##inst, POST_KERNEL, \
156 CONFIG_SENSOR_INIT_PRIORITY, &lps22hb_api_funcs); \
157
158 DT_INST_FOREACH_STATUS_OKAY(LPS22HB_DEFINE)
159