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