1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT bosch_bma280
8 
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/init.h>
11 #include <zephyr/drivers/sensor.h>
12 #include <zephyr/sys/__assert.h>
13 #include <zephyr/logging/log.h>
14 
15 #include "bma280.h"
16 
17 LOG_MODULE_REGISTER(BMA280, CONFIG_SENSOR_LOG_LEVEL);
18 
bma280_sample_fetch(const struct device * dev,enum sensor_channel chan)19 static int bma280_sample_fetch(const struct device *dev,
20 			       enum sensor_channel chan)
21 {
22 	struct bma280_data *drv_data = dev->data;
23 	const struct bma280_config *config = dev->config;
24 	uint8_t buf[6];
25 	uint8_t lsb;
26 
27 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
28 
29 	/*
30 	 * since all accel data register addresses are consecutive,
31 	 * a burst read can be used to read all the samples
32 	 */
33 	if (i2c_burst_read_dt(&config->i2c,
34 			      BMA280_REG_ACCEL_X_LSB, buf, 6) < 0) {
35 		LOG_DBG("Could not read accel axis data");
36 		return -EIO;
37 	}
38 
39 	lsb = (buf[0] & BMA280_ACCEL_LSB_MASK) >> BMA280_ACCEL_LSB_SHIFT;
40 	drv_data->x_sample = (((int8_t)buf[1]) << BMA280_ACCEL_LSB_BITS) | lsb;
41 
42 	lsb = (buf[2] & BMA280_ACCEL_LSB_MASK) >> BMA280_ACCEL_LSB_SHIFT;
43 	drv_data->y_sample = (((int8_t)buf[3]) << BMA280_ACCEL_LSB_BITS) | lsb;
44 
45 	lsb = (buf[4] & BMA280_ACCEL_LSB_MASK) >> BMA280_ACCEL_LSB_SHIFT;
46 	drv_data->z_sample = (((int8_t)buf[5]) << BMA280_ACCEL_LSB_BITS) | lsb;
47 
48 	if (i2c_reg_read_byte_dt(&config->i2c,
49 				 BMA280_REG_TEMP,
50 				 (uint8_t *)&drv_data->temp_sample) < 0) {
51 		LOG_DBG("Could not read temperature data");
52 		return -EIO;
53 	}
54 
55 	return 0;
56 }
57 
bma280_channel_accel_convert(struct sensor_value * val,int64_t raw_val)58 static void bma280_channel_accel_convert(struct sensor_value *val,
59 					int64_t raw_val)
60 {
61 	/*
62 	 * accel_val = (sample * BMA280_PMU_FULL_RAGE) /
63 	 *             (2^data_width * 10^6)
64 	 */
65 	raw_val = (raw_val * BMA280_PMU_FULL_RANGE) /
66 		  (1 << (8 + BMA280_ACCEL_LSB_BITS));
67 	val->val1 = raw_val / 1000000;
68 	val->val2 = raw_val % 1000000;
69 
70 	/* normalize val to make sure val->val2 is positive */
71 	if (val->val2 < 0) {
72 		val->val1 -= 1;
73 		val->val2 += 1000000;
74 	}
75 }
76 
bma280_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)77 static int bma280_channel_get(const struct device *dev,
78 			      enum sensor_channel chan,
79 			      struct sensor_value *val)
80 {
81 	struct bma280_data *drv_data = dev->data;
82 
83 	/*
84 	 * See datasheet "Sensor data" section for
85 	 * more details on processing sample data.
86 	 */
87 	if (chan == SENSOR_CHAN_ACCEL_X) {
88 		bma280_channel_accel_convert(val, drv_data->x_sample);
89 	} else if (chan == SENSOR_CHAN_ACCEL_Y) {
90 		bma280_channel_accel_convert(val, drv_data->y_sample);
91 	} else if (chan == SENSOR_CHAN_ACCEL_Z) {
92 		bma280_channel_accel_convert(val, drv_data->z_sample);
93 	} else if (chan == SENSOR_CHAN_ACCEL_XYZ) {
94 		bma280_channel_accel_convert(val, drv_data->x_sample);
95 		bma280_channel_accel_convert(val + 1, drv_data->y_sample);
96 		bma280_channel_accel_convert(val + 2, drv_data->z_sample);
97 	} else if (chan == SENSOR_CHAN_DIE_TEMP) {
98 		/* temperature_val = 23 + sample / 2 */
99 		val->val1 = (drv_data->temp_sample >> 1) + 23;
100 		val->val2 = 500000 * (drv_data->temp_sample & 1);
101 		return 0;
102 	} else {
103 		return -ENOTSUP;
104 	}
105 
106 	return 0;
107 }
108 
109 static DEVICE_API(sensor, bma280_driver_api) = {
110 #if CONFIG_BMA280_TRIGGER
111 	.attr_set = bma280_attr_set,
112 	.trigger_set = bma280_trigger_set,
113 #endif
114 	.sample_fetch = bma280_sample_fetch,
115 	.channel_get = bma280_channel_get,
116 };
117 
bma280_init(const struct device * dev)118 int bma280_init(const struct device *dev)
119 {
120 	const struct bma280_config *config = dev->config;
121 	uint8_t id = 0U;
122 
123 	if (!device_is_ready(config->i2c.bus)) {
124 		LOG_ERR("I2C bus device not ready");
125 		return -ENODEV;
126 	}
127 
128 	/* read device ID */
129 	if (i2c_reg_read_byte_dt(&config->i2c,
130 				 BMA280_REG_CHIP_ID, &id) < 0) {
131 		LOG_DBG("Could not read chip id");
132 		return -EIO;
133 	}
134 
135 	if (id != BMA280_CHIP_ID) {
136 		LOG_DBG("Unexpected chip id (%x)", id);
137 		return -EIO;
138 	}
139 
140 	if (i2c_reg_write_byte_dt(&config->i2c,
141 				  BMA280_REG_PMU_BW, BMA280_PMU_BW) < 0) {
142 		LOG_DBG("Could not set data filter bandwidth");
143 		return -EIO;
144 	}
145 
146 	/* set g-range */
147 	if (i2c_reg_write_byte_dt(&config->i2c,
148 				  BMA280_REG_PMU_RANGE, BMA280_PMU_RANGE) < 0) {
149 		LOG_DBG("Could not set data g-range");
150 		return -EIO;
151 	}
152 
153 #ifdef CONFIG_BMA280_TRIGGER
154 	if (config->int1_gpio.port) {
155 		if (bma280_init_interrupt(dev) < 0) {
156 			LOG_DBG("Could not initialize interrupts");
157 			return -EIO;
158 		}
159 	}
160 #endif
161 
162 	return 0;
163 }
164 
165 #define BMA280_DEFINE(inst)									\
166 	static struct bma280_data bma280_data_##inst;						\
167 												\
168 	static const struct bma280_config bma280_config##inst = {				\
169 		.i2c = I2C_DT_SPEC_INST_GET(inst),						\
170 		IF_ENABLED(CONFIG_BMA280_TRIGGER,						\
171 			   (.int1_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, { 0 }),))	\
172 	};											\
173 												\
174 	SENSOR_DEVICE_DT_INST_DEFINE(inst, bma280_init, NULL, &bma280_data_##inst,		\
175 			      &bma280_config##inst, POST_KERNEL,				\
176 			      CONFIG_SENSOR_INIT_PRIORITY, &bma280_driver_api);			\
177 
178 DT_INST_FOREACH_STATUS_OKAY(BMA280_DEFINE)
179