1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_HMC5883L_HMC5883L_H_
8 #define ZEPHYR_DRIVERS_SENSOR_HMC5883L_HMC5883L_H_
9 
10 #include <zephyr/device.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/types.h>
13 #include <zephyr/drivers/i2c.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/kernel.h>
16 
17 #define HMC5883L_REG_CONFIG_A           0x00
18 #define HMC5883L_ODR_SHIFT              2
19 
20 #define HMC5883L_REG_CONFIG_B           0x01
21 #define HMC5883L_GAIN_SHIFT             5
22 
23 #define HMC5883L_REG_MODE               0x02
24 #define HMC5883L_MODE_CONTINUOUS        0
25 
26 #define HMC5883L_REG_DATA_START         0x03
27 
28 #define HMC5883L_REG_CHIP_ID            0x0A
29 #define HMC5883L_CHIP_ID_A              'H'
30 #define HMC5883L_CHIP_ID_B              '4'
31 #define HMC5883L_CHIP_ID_C              '3'
32 
33 static const char *const hmc5883l_odr_strings[] = {
34 	"0.75", "1.5", "3", "7.5", "15", "30", "75"
35 };
36 
37 static const char *const hmc5883l_fs_strings[] = {
38 	"0.88", "1.3", "1.9", "2.5", "4", "4.7", "5.6", "8.1"
39 };
40 
41 static const uint16_t hmc5883l_gain[] = {
42 	1370, 1090, 820, 660, 440, 390, 330, 230
43 };
44 
45 struct hmc5883l_data {
46 	int16_t x_sample;
47 	int16_t y_sample;
48 	int16_t z_sample;
49 	uint8_t gain_idx;
50 
51 #ifdef CONFIG_HMC5883L_TRIGGER
52 	const struct device *dev;
53 	struct gpio_callback gpio_cb;
54 
55 	const struct sensor_trigger *data_ready_trigger;
56 	sensor_trigger_handler_t data_ready_handler;
57 
58 #if defined(CONFIG_HMC5883L_TRIGGER_OWN_THREAD)
59 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_HMC5883L_THREAD_STACK_SIZE);
60 	struct k_thread thread;
61 	struct k_sem gpio_sem;
62 #elif defined(CONFIG_HMC5883L_TRIGGER_GLOBAL_THREAD)
63 	struct k_work work;
64 #endif
65 
66 #endif /* CONFIG_HMC5883L_TRIGGER */
67 };
68 
69 struct hmc5883l_config {
70 	struct i2c_dt_spec i2c;
71 #ifdef CONFIG_HMC5883L_TRIGGER
72 	struct gpio_dt_spec int_gpio;
73 #endif
74 };
75 
76 #ifdef CONFIG_HMC5883L_TRIGGER
77 int hmc5883l_trigger_set(const struct device *dev,
78 			 const struct sensor_trigger *trig,
79 			 sensor_trigger_handler_t handler);
80 
81 int hmc5883l_init_interrupt(const struct device *dev);
82 #endif
83 
84 #endif /* __SENSOR_HMC5883L__ */
85