1 /* Bosch BMG160 gyro driver
2  *
3  * Copyright (c) 2016 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * http://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMG160-DS000-09.pdf
9  */
10 
11 #define DT_DRV_COMPAT bosch_bmg160
12 
13 #include <zephyr/init.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/sys/byteorder.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/logging/log.h>
18 
19 #include "bmg160.h"
20 
21 LOG_MODULE_REGISTER(BMG160, CONFIG_SENSOR_LOG_LEVEL);
22 
bmg160_bus_config(const struct device * dev)23 static inline int bmg160_bus_config(const struct device *dev)
24 {
25 	const struct bmg160_device_config *dev_cfg = dev->config;
26 	uint32_t i2c_cfg;
27 
28 	i2c_cfg = I2C_MODE_CONTROLLER | I2C_SPEED_SET(BMG160_BUS_SPEED);
29 
30 	return i2c_configure(dev_cfg->i2c.bus, i2c_cfg);
31 }
32 
bmg160_read(const struct device * dev,uint8_t reg_addr,uint8_t * data,uint8_t len)33 int bmg160_read(const struct device *dev, uint8_t reg_addr, uint8_t *data,
34 		uint8_t len)
35 {
36 	const struct bmg160_device_config *dev_cfg = dev->config;
37 	struct bmg160_device_data *bmg160 = dev->data;
38 	int ret = 0;
39 
40 	bmg160_bus_config(dev);
41 
42 	k_sem_take(&bmg160->sem, K_FOREVER);
43 
44 	if (i2c_burst_read_dt(&dev_cfg->i2c,
45 			      reg_addr, data, len) < 0) {
46 		ret = -EIO;
47 	}
48 
49 	k_sem_give(&bmg160->sem);
50 
51 	return ret;
52 }
53 
bmg160_read_byte(const struct device * dev,uint8_t reg_addr,uint8_t * byte)54 int bmg160_read_byte(const struct device *dev, uint8_t reg_addr,
55 		     uint8_t *byte)
56 {
57 	return bmg160_read(dev, reg_addr, byte, 1);
58 }
59 
bmg160_write(const struct device * dev,uint8_t reg_addr,uint8_t * data,uint8_t len)60 static int bmg160_write(const struct device *dev, uint8_t reg_addr,
61 			uint8_t *data,
62 			uint8_t len)
63 {
64 	const struct bmg160_device_config *dev_cfg = dev->config;
65 	struct bmg160_device_data *bmg160 = dev->data;
66 	int ret = 0;
67 
68 	bmg160_bus_config(dev);
69 
70 	k_sem_take(&bmg160->sem, K_FOREVER);
71 
72 	if (i2c_burst_write_dt(&dev_cfg->i2c,
73 			       reg_addr, data, len) < 0) {
74 		ret = -EIO;
75 	}
76 
77 	k_sem_give(&bmg160->sem);
78 
79 	return ret;
80 }
81 
bmg160_write_byte(const struct device * dev,uint8_t reg_addr,uint8_t byte)82 int bmg160_write_byte(const struct device *dev, uint8_t reg_addr,
83 		      uint8_t byte)
84 {
85 	return bmg160_write(dev, reg_addr, &byte, 1);
86 }
87 
bmg160_update_byte(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t value)88 int bmg160_update_byte(const struct device *dev, uint8_t reg_addr,
89 		       uint8_t mask,
90 		       uint8_t value)
91 {
92 	const struct bmg160_device_config *dev_cfg = dev->config;
93 	struct bmg160_device_data *bmg160 = dev->data;
94 	int ret = 0;
95 
96 	bmg160_bus_config(dev);
97 
98 	k_sem_take(&bmg160->sem, K_FOREVER);
99 
100 	if (i2c_reg_update_byte_dt(&dev_cfg->i2c,
101 				   reg_addr, mask, value) < 0) {
102 		ret = -EIO;
103 	}
104 
105 	k_sem_give(&bmg160->sem);
106 
107 	return ret;
108 }
109 
110 /* Allowed range values, in degrees/sec. */
111 static const int16_t bmg160_gyro_range_map[] = {2000, 1000, 500, 250, 125};
112 #define BMG160_GYRO_RANGE_MAP_SIZE	ARRAY_SIZE(bmg160_gyro_range_map)
113 
114 /* Allowed sampling frequencies, in Hz */
115 static const int16_t bmg160_sampling_freq_map[] = {2000, 1000, 400, 200, 100};
116 #define BMG160_SAMPLING_FREQ_MAP_SIZE	ARRAY_SIZE(bmg160_sampling_freq_map)
117 
bmg160_is_val_valid(int16_t val,const int16_t * val_map,uint16_t map_size)118 static int bmg160_is_val_valid(int16_t val, const int16_t *val_map,
119 			       uint16_t map_size)
120 {
121 	int i;
122 
123 	for (i = 0; i < map_size; i++) {
124 		if (val == val_map[i]) {
125 			return i;
126 		}
127 	}
128 
129 	return -1;
130 }
131 
bmg160_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)132 static int bmg160_attr_set(const struct device *dev, enum sensor_channel chan,
133 			   enum sensor_attribute attr,
134 			   const struct sensor_value *val)
135 {
136 	struct bmg160_device_data *bmg160 = dev->data;
137 #ifdef CONFIG_BMG160_TRIGGER
138 	const struct bmg160_device_config *config = dev->config;
139 #endif
140 	int idx;
141 	uint16_t range_dps;
142 
143 	if (chan != SENSOR_CHAN_GYRO_XYZ) {
144 		return -ENOTSUP;
145 	}
146 
147 	switch (attr) {
148 	case SENSOR_ATTR_FULL_SCALE:
149 		range_dps = sensor_rad_to_degrees(val);
150 
151 		idx = bmg160_is_val_valid(range_dps,
152 					  bmg160_gyro_range_map,
153 					  BMG160_GYRO_RANGE_MAP_SIZE);
154 		if (idx < 0) {
155 			return -ENOTSUP;
156 		}
157 
158 		if (bmg160_write_byte(dev, BMG160_REG_RANGE, idx) < 0) {
159 			return -EIO;
160 		}
161 
162 		bmg160->scale = BMG160_RANGE_TO_SCALE(range_dps);
163 
164 		return 0;
165 
166 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
167 		idx = bmg160_is_val_valid(val->val1,
168 					  bmg160_sampling_freq_map,
169 					  BMG160_SAMPLING_FREQ_MAP_SIZE);
170 		if (idx < 0) {
171 			return -ENOTSUP;
172 		}
173 
174 		/*
175 		 * The sampling frequencies values start at 1, i.e. a
176 		 * sampling frequency of 2000Hz translates to BW value
177 		 * of 1. Hence the 1 added to the index received.
178 		 */
179 		if (bmg160_write_byte(dev, BMG160_REG_BW, idx + 1) < 0) {
180 			return -EIO;
181 		}
182 
183 		return 0;
184 
185 #ifdef CONFIG_BMG160_TRIGGER
186 	case SENSOR_ATTR_SLOPE_TH:
187 	case SENSOR_ATTR_SLOPE_DUR:
188 		if (!config->int_gpio.port) {
189 			return -ENOTSUP;
190 		}
191 
192 		return bmg160_slope_config(dev, attr, val);
193 #endif
194 	default:
195 		return -ENOTSUP;
196 	}
197 }
198 
bmg160_sample_fetch(const struct device * dev,enum sensor_channel chan)199 static int bmg160_sample_fetch(const struct device *dev,
200 			       enum sensor_channel chan)
201 {
202 	struct bmg160_device_data *bmg160 = dev->data;
203 	union {
204 		uint8_t raw[7];
205 		struct {
206 			uint16_t x_axis;
207 			uint16_t y_axis;
208 			uint16_t z_axis;
209 			uint8_t temp;
210 		};
211 	} buf __aligned(2);
212 
213 	/* do a burst read, to fetch all axis data */
214 	if (bmg160_read(dev, BMG160_REG_RATE_X, buf.raw, sizeof(buf)) < 0) {
215 		return -EIO;
216 	}
217 
218 	bmg160->raw_gyro_xyz[0] = sys_le16_to_cpu(buf.x_axis);
219 	bmg160->raw_gyro_xyz[1] = sys_le16_to_cpu(buf.y_axis);
220 	bmg160->raw_gyro_xyz[2] = sys_le16_to_cpu(buf.z_axis);
221 	bmg160->raw_temp	= buf.temp;
222 
223 	return 0;
224 }
225 
bmg160_to_fixed_point(struct bmg160_device_data * bmg160,enum sensor_channel chan,int16_t raw,struct sensor_value * val)226 static void bmg160_to_fixed_point(struct bmg160_device_data *bmg160,
227 				  enum sensor_channel chan, int16_t raw,
228 				  struct sensor_value *val)
229 {
230 	if (chan == SENSOR_CHAN_DIE_TEMP) {
231 		val->val1 = 23 + (raw / 2);
232 		val->val2 = (raw % 2) * 500000;
233 	} else {
234 		int32_t converted_val = raw * bmg160->scale;
235 
236 		val->val1 = converted_val / 1000000;
237 		val->val2 = converted_val % 1000000;
238 	}
239 }
240 
bmg160_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)241 static int bmg160_channel_get(const struct device *dev,
242 			      enum sensor_channel chan,
243 			      struct sensor_value *val)
244 {
245 	struct bmg160_device_data *bmg160 = dev->data;
246 	int16_t raw_val;
247 	int i;
248 
249 	switch (chan) {
250 	case SENSOR_CHAN_GYRO_X:
251 	case SENSOR_CHAN_GYRO_Y:
252 	case SENSOR_CHAN_GYRO_Z:
253 		raw_val = bmg160->raw_gyro_xyz[chan - SENSOR_CHAN_GYRO_X];
254 		bmg160_to_fixed_point(bmg160, chan, raw_val, val);
255 		return 0;
256 
257 	case SENSOR_CHAN_GYRO_XYZ:
258 		/* return all channel values, in one read */
259 		for (i = 0; i < 3; i++, val++) {
260 			raw_val = bmg160->raw_gyro_xyz[i];
261 			bmg160_to_fixed_point(bmg160, chan, raw_val, val);
262 		}
263 
264 		return 0;
265 
266 	case SENSOR_CHAN_DIE_TEMP:
267 		bmg160_to_fixed_point(bmg160, chan, bmg160->raw_temp, val);
268 		return 0;
269 
270 	default:
271 		return -ENOTSUP;
272 	}
273 }
274 
275 static const struct sensor_driver_api bmg160_api = {
276 	.attr_set = bmg160_attr_set,
277 #ifdef CONFIG_BMG160_TRIGGER
278 	.trigger_set = bmg160_trigger_set,
279 #endif
280 	.sample_fetch = bmg160_sample_fetch,
281 	.channel_get = bmg160_channel_get,
282 };
283 
bmg160_init(const struct device * dev)284 int bmg160_init(const struct device *dev)
285 {
286 	const struct bmg160_device_config *cfg = dev->config;
287 	struct bmg160_device_data *bmg160 = dev->data;
288 	uint8_t chip_id = 0U;
289 	uint16_t range_dps;
290 
291 	if (!device_is_ready(cfg->i2c.bus)) {
292 		LOG_ERR("I2C bus device not ready");
293 		return -ENODEV;
294 	}
295 
296 	k_sem_init(&bmg160->sem, 1, K_SEM_MAX_LIMIT);
297 
298 	if (bmg160_read_byte(dev, BMG160_REG_CHIPID, &chip_id) < 0) {
299 		LOG_DBG("Failed to read chip id.");
300 		return -EIO;
301 	}
302 
303 	if (chip_id != BMG160_CHIP_ID) {
304 		LOG_DBG("Unsupported chip detected (0x%x)!", chip_id);
305 		return -ENODEV;
306 	}
307 
308 	/* reset the chip */
309 	bmg160_write_byte(dev, BMG160_REG_BGW_SOFTRESET, BMG160_RESET);
310 
311 	k_busy_wait(1000); /* wait for the chip to come up */
312 
313 	if (bmg160_write_byte(dev, BMG160_REG_RANGE,
314 			      BMG160_DEFAULT_RANGE) < 0) {
315 		LOG_DBG("Failed to set range.");
316 		return -EIO;
317 	}
318 
319 	range_dps = bmg160_gyro_range_map[BMG160_DEFAULT_RANGE];
320 
321 	bmg160->scale = BMG160_RANGE_TO_SCALE(range_dps);
322 
323 	if (bmg160_write_byte(dev, BMG160_REG_BW, BMG160_DEFAULT_ODR) < 0) {
324 		LOG_DBG("Failed to set sampling frequency.");
325 		return -EIO;
326 	}
327 
328 	/* disable interrupts */
329 	if (bmg160_write_byte(dev, BMG160_REG_INT_EN0, 0) < 0) {
330 		LOG_DBG("Failed to disable all interrupts.");
331 		return -EIO;
332 	}
333 
334 #ifdef CONFIG_BMG160_TRIGGER
335 	if (cfg->int_gpio.port) {
336 		bmg160_trigger_init(dev);
337 	}
338 #endif
339 
340 	return 0;
341 }
342 
343 #define BMG160_DEFINE(inst)									\
344 	static struct bmg160_device_data bmg160_data_##inst;					\
345 												\
346 	static const struct bmg160_device_config bmg160_config_##inst = {			\
347 		.i2c = I2C_DT_SPEC_INST_GET(inst),						\
348 		IF_ENABLED(CONFIG_BMG160_TRIGGER,						\
349 			   (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),))	\
350 												\
351 	};											\
352 												\
353 	SENSOR_DEVICE_DT_INST_DEFINE(inst, bmg160_init, NULL,					\
354 			      &bmg160_data_##inst, &bmg160_config_##inst,			\
355 			      POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &bmg160_api);		\
356 
357 DT_INST_FOREACH_STATUS_OKAY(BMG160_DEFINE)
358