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 <drivers/sensor.h>
12 #include <kernel.h>
13 #include <device.h>
14 #include <init.h>
15 #include <sys/byteorder.h>
16 #include <sys/__assert.h>
17 #include <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 	struct lps22hb_data *data = dev->data;
26 	const struct lps22hb_config *config = dev->config;
27 
28 	return i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
29 				   LPS22HB_REG_CTRL_REG1,
30 				   LPS22HB_MASK_CTRL_REG1_ODR,
31 				   odr << LPS22HB_SHIFT_CTRL_REG1_ODR);
32 }
33 
lps22hb_sample_fetch(const struct device * dev,enum sensor_channel chan)34 static int lps22hb_sample_fetch(const struct device *dev,
35 				enum sensor_channel chan)
36 {
37 	struct lps22hb_data *data = dev->data;
38 	const struct lps22hb_config *config = dev->config;
39 	uint8_t out[5];
40 
41 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
42 
43 	if (i2c_burst_read(data->i2c_master, config->i2c_slave_addr,
44 			   LPS22HB_REG_PRESS_OUT_XL, out, 5) < 0) {
45 		LOG_DBG("Failed to read sample");
46 		return -EIO;
47 	}
48 
49 	data->sample_press = (int32_t)((uint32_t)(out[0]) |
50 				     ((uint32_t)(out[1]) << 8) |
51 				     ((uint32_t)(out[2]) << 16));
52 	data->sample_temp = (int16_t)((uint16_t)(out[3]) |
53 				    ((uint16_t)(out[4]) << 8));
54 
55 	return 0;
56 }
57 
lps22hb_press_convert(struct sensor_value * val,int32_t raw_val)58 static inline void lps22hb_press_convert(struct sensor_value *val,
59 					 int32_t raw_val)
60 {
61 	/* Pressure sensitivity is 4096 LSB/hPa */
62 	/* Convert raw_val to val in kPa */
63 	val->val1 = (raw_val >> 12) / 10;
64 	val->val2 = (raw_val >> 12) % 10 * 100000 +
65 		(((int32_t)((raw_val) & 0x0FFF) * 100000L) >> 12);
66 }
67 
lps22hb_temp_convert(struct sensor_value * val,int16_t raw_val)68 static inline void lps22hb_temp_convert(struct sensor_value *val,
69 					int16_t raw_val)
70 {
71 	/* Temperature sensitivity is 100 LSB/deg C */
72 	val->val1 = raw_val / 100;
73 	val->val2 = ((int32_t)raw_val % 100) * 10000;
74 }
75 
lps22hb_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)76 static int lps22hb_channel_get(const struct device *dev,
77 			       enum sensor_channel chan,
78 			       struct sensor_value *val)
79 {
80 	struct lps22hb_data *data = dev->data;
81 
82 	if (chan == SENSOR_CHAN_PRESS) {
83 		lps22hb_press_convert(val, data->sample_press);
84 	} else if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
85 		lps22hb_temp_convert(val, data->sample_temp);
86 	} else {
87 		return -ENOTSUP;
88 	}
89 
90 	return 0;
91 }
92 
93 static const struct sensor_driver_api lps22hb_api_funcs = {
94 	.sample_fetch = lps22hb_sample_fetch,
95 	.channel_get = lps22hb_channel_get,
96 };
97 
lps22hb_init_chip(const struct device * dev)98 static int lps22hb_init_chip(const struct device *dev)
99 {
100 	struct lps22hb_data *data = dev->data;
101 	const struct lps22hb_config *config = dev->config;
102 	uint8_t chip_id;
103 
104 	if (i2c_reg_read_byte(data->i2c_master, config->i2c_slave_addr,
105 			      LPS22HB_REG_WHO_AM_I, &chip_id) < 0) {
106 		LOG_DBG("Failed reading chip id");
107 		goto err_poweroff;
108 	}
109 
110 	if (chip_id != LPS22HB_VAL_WHO_AM_I) {
111 		LOG_DBG("Invalid chip id 0x%x", chip_id);
112 		goto err_poweroff;
113 	}
114 
115 	if (lps22hb_set_odr_raw(dev, LPS22HB_DEFAULT_SAMPLING_RATE) < 0) {
116 		LOG_DBG("Failed to set sampling rate");
117 		goto err_poweroff;
118 	}
119 
120 	if (i2c_reg_update_byte(data->i2c_master, config->i2c_slave_addr,
121 				LPS22HB_REG_CTRL_REG1,
122 				LPS22HB_MASK_CTRL_REG1_BDU,
123 				(1 << LPS22HB_SHIFT_CTRL_REG1_BDU)) < 0) {
124 		LOG_DBG("Failed to set BDU");
125 		goto err_poweroff;
126 	}
127 
128 	return 0;
129 
130 err_poweroff:
131 	return -EIO;
132 }
133 
lps22hb_init(const struct device * dev)134 static int lps22hb_init(const struct device *dev)
135 {
136 	const struct lps22hb_config * const config = dev->config;
137 	struct lps22hb_data *data = dev->data;
138 
139 	data->i2c_master = device_get_binding(config->i2c_master_dev_name);
140 
141 	if (!data->i2c_master) {
142 		LOG_DBG("I2c master not found: %s",
143 			    config->i2c_master_dev_name);
144 		return -EINVAL;
145 	}
146 
147 	if (lps22hb_init_chip(dev) < 0) {
148 		LOG_DBG("Failed to initialize chip");
149 		return -EIO;
150 	}
151 
152 	return 0;
153 }
154 
155 static const struct lps22hb_config lps22hb_config = {
156 	.i2c_master_dev_name = DT_INST_BUS_LABEL(0),
157 	.i2c_slave_addr = DT_INST_REG_ADDR(0),
158 };
159 
160 static struct lps22hb_data lps22hb_data;
161 
162 DEVICE_DT_INST_DEFINE(0, lps22hb_init, NULL,
163 		    &lps22hb_data, &lps22hb_config, POST_KERNEL,
164 		    CONFIG_SENSOR_INIT_PRIORITY, &lps22hb_api_funcs);
165