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 <init.h>
14 #include <drivers/sensor.h>
15 #include <sys/byteorder.h>
16 #include <kernel.h>
17 #include <logging/log.h>
18 
19 #include "bmg160.h"
20 
21 LOG_MODULE_REGISTER(BMG160, CONFIG_SENSOR_LOG_LEVEL);
22 
23 struct bmg160_device_data bmg160_data;
24 
bmg160_bus_config(const struct device * dev)25 static inline int bmg160_bus_config(const struct device *dev)
26 {
27 	const struct bmg160_device_config *dev_cfg = dev->config;
28 	struct bmg160_device_data *bmg160 = dev->data;
29 	uint32_t i2c_cfg;
30 
31 	i2c_cfg = I2C_MODE_MASTER | I2C_SPEED_SET(dev_cfg->i2c_speed);
32 
33 	return i2c_configure(bmg160->i2c, i2c_cfg);
34 }
35 
bmg160_read(const struct device * dev,uint8_t reg_addr,uint8_t * data,uint8_t len)36 int bmg160_read(const struct device *dev, uint8_t reg_addr, uint8_t *data,
37 		uint8_t len)
38 {
39 	const struct bmg160_device_config *dev_cfg = dev->config;
40 	struct bmg160_device_data *bmg160 = dev->data;
41 	int ret = 0;
42 
43 	bmg160_bus_config(dev);
44 
45 	k_sem_take(&bmg160->sem, K_FOREVER);
46 
47 	if (i2c_burst_read(bmg160->i2c, dev_cfg->i2c_addr,
48 			   reg_addr, data, len) < 0) {
49 		ret = -EIO;
50 	}
51 
52 	k_sem_give(&bmg160->sem);
53 
54 	return ret;
55 }
56 
bmg160_read_byte(const struct device * dev,uint8_t reg_addr,uint8_t * byte)57 int bmg160_read_byte(const struct device *dev, uint8_t reg_addr,
58 		     uint8_t *byte)
59 {
60 	return bmg160_read(dev, reg_addr, byte, 1);
61 }
62 
bmg160_write(const struct device * dev,uint8_t reg_addr,uint8_t * data,uint8_t len)63 static int bmg160_write(const struct device *dev, uint8_t reg_addr,
64 			uint8_t *data,
65 			uint8_t len)
66 {
67 	const struct bmg160_device_config *dev_cfg = dev->config;
68 	struct bmg160_device_data *bmg160 = dev->data;
69 	int ret = 0;
70 
71 	bmg160_bus_config(dev);
72 
73 	k_sem_take(&bmg160->sem, K_FOREVER);
74 
75 	if (i2c_burst_write(bmg160->i2c, dev_cfg->i2c_addr,
76 			    reg_addr, data, len) < 0) {
77 		ret = -EIO;
78 	}
79 
80 	k_sem_give(&bmg160->sem);
81 
82 	return ret;
83 }
84 
bmg160_write_byte(const struct device * dev,uint8_t reg_addr,uint8_t byte)85 int bmg160_write_byte(const struct device *dev, uint8_t reg_addr,
86 		      uint8_t byte)
87 {
88 	return bmg160_write(dev, reg_addr, &byte, 1);
89 }
90 
bmg160_update_byte(const struct device * dev,uint8_t reg_addr,uint8_t mask,uint8_t value)91 int bmg160_update_byte(const struct device *dev, uint8_t reg_addr,
92 		       uint8_t mask,
93 		       uint8_t value)
94 {
95 	const struct bmg160_device_config *dev_cfg = dev->config;
96 	struct bmg160_device_data *bmg160 = dev->data;
97 	int ret = 0;
98 
99 	bmg160_bus_config(dev);
100 
101 	k_sem_take(&bmg160->sem, K_FOREVER);
102 
103 	if (i2c_reg_update_byte(bmg160->i2c, dev_cfg->i2c_addr,
104 				reg_addr, mask, value) < 0) {
105 		ret = -EIO;
106 	}
107 
108 	k_sem_give(&bmg160->sem);
109 
110 	return ret;
111 }
112 
113 /* Allowed range values, in degrees/sec. */
114 static const int16_t bmg160_gyro_range_map[] = {2000, 1000, 500, 250, 125};
115 #define BMG160_GYRO_RANGE_MAP_SIZE	ARRAY_SIZE(bmg160_gyro_range_map)
116 
117 /* Allowed sampling frequencies, in Hz */
118 static const int16_t bmg160_sampling_freq_map[] = {2000, 1000, 400, 200, 100};
119 #define BMG160_SAMPLING_FREQ_MAP_SIZE	ARRAY_SIZE(bmg160_sampling_freq_map)
120 
bmg160_is_val_valid(int16_t val,const int16_t * val_map,uint16_t map_size)121 static int bmg160_is_val_valid(int16_t val, const int16_t *val_map,
122 			       uint16_t map_size)
123 {
124 	int i;
125 
126 	for (i = 0; i < map_size; i++) {
127 		if (val == val_map[i]) {
128 			return i;
129 		}
130 	}
131 
132 	return -1;
133 }
134 
bmg160_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)135 static int bmg160_attr_set(const struct device *dev, enum sensor_channel chan,
136 			   enum sensor_attribute attr,
137 			   const struct sensor_value *val)
138 {
139 	struct bmg160_device_data *bmg160 = dev->data;
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 		return bmg160_slope_config(dev, attr, val);
189 #endif
190 	default:
191 		return -ENOTSUP;
192 	}
193 }
194 
bmg160_sample_fetch(const struct device * dev,enum sensor_channel chan)195 static int bmg160_sample_fetch(const struct device *dev,
196 			       enum sensor_channel chan)
197 {
198 	struct bmg160_device_data *bmg160 = dev->data;
199 	union {
200 		uint8_t raw[7];
201 		struct {
202 			uint16_t x_axis;
203 			uint16_t y_axis;
204 			uint16_t z_axis;
205 			uint8_t temp;
206 		};
207 	} buf __aligned(2);
208 
209 	/* do a burst read, to fetch all axis data */
210 	if (bmg160_read(dev, BMG160_REG_RATE_X, buf.raw, sizeof(buf)) < 0) {
211 		return -EIO;
212 	}
213 
214 	bmg160->raw_gyro_xyz[0] = sys_le16_to_cpu(buf.x_axis);
215 	bmg160->raw_gyro_xyz[1] = sys_le16_to_cpu(buf.y_axis);
216 	bmg160->raw_gyro_xyz[2] = sys_le16_to_cpu(buf.z_axis);
217 	bmg160->raw_temp	= buf.temp;
218 
219 	return 0;
220 }
221 
bmg160_to_fixed_point(struct bmg160_device_data * bmg160,enum sensor_channel chan,int16_t raw,struct sensor_value * val)222 static void bmg160_to_fixed_point(struct bmg160_device_data *bmg160,
223 				  enum sensor_channel chan, int16_t raw,
224 				  struct sensor_value *val)
225 {
226 	if (chan == SENSOR_CHAN_DIE_TEMP) {
227 		val->val1 = 23 + (raw / 2);
228 		val->val2 = (raw % 2) * 500000;
229 	} else {
230 		int32_t converted_val = raw * bmg160->scale;
231 
232 		val->val1 = converted_val / 1000000;
233 		val->val2 = converted_val % 1000000;
234 	}
235 }
236 
bmg160_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)237 static int bmg160_channel_get(const struct device *dev,
238 			      enum sensor_channel chan,
239 			      struct sensor_value *val)
240 {
241 	struct bmg160_device_data *bmg160 = dev->data;
242 	int16_t raw_val;
243 	int i;
244 
245 	switch (chan) {
246 	case SENSOR_CHAN_GYRO_X:
247 	case SENSOR_CHAN_GYRO_Y:
248 	case SENSOR_CHAN_GYRO_Z:
249 		raw_val = bmg160->raw_gyro_xyz[chan - SENSOR_CHAN_GYRO_X];
250 		bmg160_to_fixed_point(bmg160, chan, raw_val, val);
251 		return 0;
252 
253 	case SENSOR_CHAN_GYRO_XYZ:
254 		/* return all channel values, in one read */
255 		for (i = 0; i < 3; i++, val++) {
256 			raw_val = bmg160->raw_gyro_xyz[i];
257 			bmg160_to_fixed_point(bmg160, chan, raw_val, val);
258 		}
259 
260 		return 0;
261 
262 	case SENSOR_CHAN_DIE_TEMP:
263 		bmg160_to_fixed_point(bmg160, chan, bmg160->raw_temp, val);
264 		return 0;
265 
266 	default:
267 		return -ENOTSUP;
268 	}
269 }
270 
271 static const struct sensor_driver_api bmg160_api = {
272 	.attr_set = bmg160_attr_set,
273 #ifdef CONFIG_BMG160_TRIGGER
274 	.trigger_set = bmg160_trigger_set,
275 #endif
276 	.sample_fetch = bmg160_sample_fetch,
277 	.channel_get = bmg160_channel_get,
278 };
279 
bmg160_init(const struct device * dev)280 int bmg160_init(const struct device *dev)
281 {
282 	const struct bmg160_device_config *cfg = dev->config;
283 	struct bmg160_device_data *bmg160 = dev->data;
284 	uint8_t chip_id = 0U;
285 	uint16_t range_dps;
286 
287 	bmg160->i2c = device_get_binding((char *)cfg->i2c_port);
288 	if (!bmg160->i2c) {
289 		LOG_DBG("I2C master controller not found!");
290 		return -EINVAL;
291 	}
292 
293 	k_sem_init(&bmg160->sem, 1, K_SEM_MAX_LIMIT);
294 
295 	if (bmg160_read_byte(dev, BMG160_REG_CHIPID, &chip_id) < 0) {
296 		LOG_DBG("Failed to read chip id.");
297 		return -EIO;
298 	}
299 
300 	if (chip_id != BMG160_CHIP_ID) {
301 		LOG_DBG("Unsupported chip detected (0x%x)!", chip_id);
302 		return -ENODEV;
303 	}
304 
305 	/* reset the chip */
306 	bmg160_write_byte(dev, BMG160_REG_BGW_SOFTRESET, BMG160_RESET);
307 
308 	k_busy_wait(1000); /* wait for the chip to come up */
309 
310 	if (bmg160_write_byte(dev, BMG160_REG_RANGE,
311 			      BMG160_DEFAULT_RANGE) < 0) {
312 		LOG_DBG("Failed to set range.");
313 		return -EIO;
314 	}
315 
316 	range_dps = bmg160_gyro_range_map[BMG160_DEFAULT_RANGE];
317 
318 	bmg160->scale = BMG160_RANGE_TO_SCALE(range_dps);
319 
320 	if (bmg160_write_byte(dev, BMG160_REG_BW, BMG160_DEFAULT_ODR) < 0) {
321 		LOG_DBG("Failed to set sampling frequency.");
322 		return -EIO;
323 	}
324 
325 	/* disable interrupts */
326 	if (bmg160_write_byte(dev, BMG160_REG_INT_EN0, 0) < 0) {
327 		LOG_DBG("Failed to disable all interrupts.");
328 		return -EIO;
329 	}
330 
331 #ifdef CONFIG_BMG160_TRIGGER
332 	bmg160_trigger_init(dev);
333 #endif
334 
335 	return 0;
336 }
337 
338 const struct bmg160_device_config bmg160_config = {
339 	.i2c_port = DT_INST_BUS_LABEL(0),
340 	.i2c_addr = DT_INST_REG_ADDR(0),
341 	.i2c_speed = BMG160_BUS_SPEED,
342 #ifdef CONFIG_BMG160_TRIGGER
343 	.int_pin = DT_INST_GPIO_PIN(0, int_gpios),
344 	.int_flags = DT_INST_GPIO_FLAGS(0, int_gpios),
345 	.gpio_port = DT_INST_GPIO_LABEL(0, int_gpios),
346 #endif
347 };
348 
349 DEVICE_DT_INST_DEFINE(0, bmg160_init, NULL,
350 		    &bmg160_data,
351 		    &bmg160_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
352 		    &bmg160_api);
353