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_GYRO_CFG		0x1B
24 #define MPU6050_GYRO_FS_SHIFT		3
25 
26 #define MPU6050_REG_ACCEL_CFG		0x1C
27 #define MPU6050_ACCEL_FS_SHIFT		3
28 
29 #define MPU6050_REG_INT_EN		0x38
30 #define MPU6050_DRDY_EN			BIT(0)
31 
32 #define MPU6050_REG_DATA_START		0x3B
33 
34 #define MPU6050_REG_PWR_MGMT1		0x6B
35 #define MPU6050_SLEEP_EN		BIT(6)
36 
37 /* measured in degrees/sec x10 to avoid floating point */
38 static const uint16_t mpu6050_gyro_sensitivity_x10[] = {
39 	1310, 655, 328, 164
40 };
41 
42 /* Device type, uses the correct offets for a particular device */
43 enum mpu6050_device_type {
44 	DEVICE_TYPE_MPU6050 = 0,
45 	DEVICE_TYPE_MPU6500,
46 };
47 
48 struct mpu6050_data {
49 	int16_t accel_x;
50 	int16_t accel_y;
51 	int16_t accel_z;
52 	uint16_t accel_sensitivity_shift;
53 
54 	int16_t temp;
55 
56 	int16_t gyro_x;
57 	int16_t gyro_y;
58 	int16_t gyro_z;
59 	uint16_t gyro_sensitivity_x10;
60 
61 	enum mpu6050_device_type device_type;
62 
63 #ifdef CONFIG_MPU6050_TRIGGER
64 	const struct device *dev;
65 	struct gpio_callback gpio_cb;
66 
67 	const struct sensor_trigger *data_ready_trigger;
68 	sensor_trigger_handler_t data_ready_handler;
69 
70 #if defined(CONFIG_MPU6050_TRIGGER_OWN_THREAD)
71 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_MPU6050_THREAD_STACK_SIZE);
72 	struct k_thread thread;
73 	struct k_sem gpio_sem;
74 #elif defined(CONFIG_MPU6050_TRIGGER_GLOBAL_THREAD)
75 	struct k_work work;
76 #endif
77 
78 #endif /* CONFIG_MPU6050_TRIGGER */
79 };
80 
81 struct mpu6050_config {
82 	struct i2c_dt_spec i2c;
83 #ifdef CONFIG_MPU6050_TRIGGER
84 	struct gpio_dt_spec int_gpio;
85 #endif /* CONFIG_MPU6050_TRIGGER */
86 };
87 
88 #ifdef CONFIG_MPU6050_TRIGGER
89 int mpu6050_trigger_set(const struct device *dev,
90 			const struct sensor_trigger *trig,
91 			sensor_trigger_handler_t handler);
92 
93 int mpu6050_init_interrupt(const struct device *dev);
94 #endif
95 
96 #endif /* __SENSOR_MPU6050__ */
97