1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_MPU6050_MPU6050_H_
8 #define ZEPHYR_DRIVERS_SENSOR_MPU6050_MPU6050_H_
9 
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/sys/util.h>
15 #include <zephyr/types.h>
16 
17 #define MPU6050_REG_CHIP_ID		0x75
18 #define MPU6050_CHIP_ID			0x68
19 #define MPU6500_CHIP_ID			0x70
20 #define MPU9250_CHIP_ID			0x71
21 #define MPU6880_CHIP_ID			0x19
22 
23 #define MPU6050_REG_SMPLRT_DIV		0x19
24 
25 #define MPU6050_REG_GYRO_CFG		0x1B
26 #define MPU6050_GYRO_FS_SHIFT		3
27 
28 #define MPU6050_REG_ACCEL_CFG		0x1C
29 #define MPU6050_ACCEL_FS_SHIFT		3
30 
31 #define MPU6050_REG_INT_EN		0x38
32 #define MPU6050_DRDY_EN			BIT(0)
33 
34 #define MPU6050_REG_DATA_START		0x3B
35 
36 #define MPU6050_REG_PWR_MGMT1		0x6B
37 #define MPU6050_SLEEP_EN		BIT(6)
38 
39 /* measured in degrees/sec x10 to avoid floating point */
40 static const uint16_t mpu6050_gyro_sensitivity_x10[] = {
41 	1310, 655, 328, 164
42 };
43 
44 /* Device type, uses the correct offets for a particular device */
45 enum mpu6050_device_type {
46 	DEVICE_TYPE_MPU6050 = 0,
47 	DEVICE_TYPE_MPU6500,
48 };
49 
50 struct mpu6050_data {
51 	int16_t accel_x;
52 	int16_t accel_y;
53 	int16_t accel_z;
54 	uint16_t accel_sensitivity_shift;
55 
56 	int16_t temp;
57 
58 	int16_t gyro_x;
59 	int16_t gyro_y;
60 	int16_t gyro_z;
61 	uint16_t gyro_sensitivity_x10;
62 
63 	enum mpu6050_device_type device_type;
64 
65 #ifdef CONFIG_MPU6050_TRIGGER
66 	const struct device *dev;
67 	struct gpio_callback gpio_cb;
68 
69 	const struct sensor_trigger *data_ready_trigger;
70 	sensor_trigger_handler_t data_ready_handler;
71 
72 #if defined(CONFIG_MPU6050_TRIGGER_OWN_THREAD)
73 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_MPU6050_THREAD_STACK_SIZE);
74 	struct k_thread thread;
75 	struct k_sem gpio_sem;
76 #elif defined(CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD)
77 	struct k_work work;
78 #endif
79 
80 #endif /* CONFIG_MPU6050_TRIGGER */
81 };
82 
83 struct mpu6050_config {
84 	struct i2c_dt_spec i2c;
85 	uint8_t accel_fs;
86 	uint16_t gyro_fs;
87 	uint8_t smplrt_div;
88 #ifdef CONFIG_MPU6050_TRIGGER
89 	struct gpio_dt_spec int_gpio;
90 #endif /* CONFIG_MPU6050_TRIGGER */
91 };
92 
93 #ifdef CONFIG_MPU6050_TRIGGER
94 int mpu6050_trigger_set(const struct device *dev,
95 			const struct sensor_trigger *trig,
96 			sensor_trigger_handler_t handler);
97 
98 int mpu6050_init_interrupt(const struct device *dev);
99 #endif
100 
101 #endif /* __SENSOR_MPU6050__ */
102