1 /* ST Microelectronics ISM330DHCX 6-axis IMU sensor driver
2  *
3  * Copyright (c) 2020 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * https://www.st.com/resource/en/datasheet/ism330dhcx.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_ISM330DHCX_ISM330DHCX_H_
12 #define ZEPHYR_DRIVERS_SENSOR_ISM330DHCX_ISM330DHCX_H_
13 
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/types.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <zephyr/drivers/spi.h>
18 #include <zephyr/drivers/i2c.h>
19 #include <zephyr/kernel.h>
20 #include <zephyr/sys/util.h>
21 #include <stmemsc.h>
22 #include "ism330dhcx_reg.h"
23 
24 #define ISM330DHCX_EN_BIT					0x01
25 #define ISM330DHCX_DIS_BIT					0x00
26 
27 /* Accel sensor sensitivity grain is 61 ug/LSB */
28 #define GAIN_UNIT_XL				(61LL)
29 
30 /* Gyro sensor sensitivity grain is 4.375 udps/LSB */
31 #define GAIN_UNIT_G				(4375LL)
32 
33 struct ism330dhcx_config {
34 	int (*bus_init)(const struct device *dev);
35 	uint8_t accel_odr;
36 	uint16_t gyro_odr;
37 	uint8_t accel_range;
38 	uint16_t gyro_range;
39 #ifdef CONFIG_ISM330DHCX_TRIGGER
40 	uint8_t int_pin;
41 	struct gpio_dt_spec drdy_gpio;
42 #endif /* CONFIG_ISM330DHCX_TRIGGER */
43 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
44 	struct i2c_dt_spec i2c;
45 #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
46 	struct spi_dt_spec spi;
47 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
48 };
49 
50 union samples {
51 	uint8_t raw[6];
52 	struct {
53 		int16_t axis[3];
54 	};
55 } __aligned(2);
56 
57 #define ISM330DHCX_SHUB_MAX_NUM_SLVS			2
58 
59 struct ism330dhcx_data {
60 	const struct device *dev;
61 	int16_t acc[3];
62 	uint32_t acc_gain;
63 	int16_t gyro[3];
64 	uint32_t gyro_gain;
65 #if defined(CONFIG_ISM330DHCX_ENABLE_TEMP)
66 	int temp_sample;
67 #endif
68 #if defined(CONFIG_ISM330DHCX_SENSORHUB)
69 	uint8_t ext_data[2][6];
70 	uint16_t magn_gain;
71 
72 	struct hts221_data {
73 		int16_t x0;
74 		int16_t x1;
75 		int16_t y0;
76 		int16_t y1;
77 	} hts221;
78 #endif /* CONFIG_ISM330DHCX_SENSORHUB */
79 
80 	stmdev_ctx_t *ctx;
81 
82 	#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
83 	stmdev_ctx_t ctx_i2c;
84 	#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
85 	stmdev_ctx_t ctx_spi;
86 	#endif
87 
88 	uint16_t accel_freq;
89 	uint8_t accel_fs;
90 	uint16_t gyro_freq;
91 	uint8_t gyro_fs;
92 
93 #ifdef CONFIG_ISM330DHCX_TRIGGER
94 	struct gpio_callback gpio_cb;
95 	sensor_trigger_handler_t handler_drdy_acc;
96 	const struct sensor_trigger *trig_drdy_acc;
97 	sensor_trigger_handler_t handler_drdy_gyr;
98 	const struct sensor_trigger *trig_drdy_gyr;
99 	sensor_trigger_handler_t handler_drdy_temp;
100 	const struct sensor_trigger *trig_drdy_temp;
101 
102 #if defined(CONFIG_ISM330DHCX_TRIGGER_OWN_THREAD)
103 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ISM330DHCX_THREAD_STACK_SIZE);
104 	struct k_thread thread;
105 	struct k_sem gpio_sem;
106 #elif defined(CONFIG_ISM330DHCX_TRIGGER_GLOBAL_THREAD)
107 	struct k_work work;
108 #endif
109 #endif /* CONFIG_ISM330DHCX_TRIGGER */
110 };
111 
112 int ism330dhcx_spi_init(const struct device *dev);
113 int ism330dhcx_i2c_init(const struct device *dev);
114 #if defined(CONFIG_ISM330DHCX_SENSORHUB)
115 int ism330dhcx_shub_init(const struct device *dev);
116 int ism330dhcx_shub_fetch_external_devs(const struct device *dev);
117 int ism330dhcx_shub_get_idx(enum sensor_channel type);
118 int ism330dhcx_shub_config(const struct device *dev, enum sensor_channel chan,
119 			   enum sensor_attribute attr,
120 			   const struct sensor_value *val);
121 #endif /* CONFIG_ISM330DHCX_SENSORHUB */
122 
123 #ifdef CONFIG_ISM330DHCX_TRIGGER
124 int ism330dhcx_trigger_set(const struct device *dev,
125 			   const struct sensor_trigger *trig,
126 			   sensor_trigger_handler_t handler);
127 
128 int ism330dhcx_init_interrupt(const struct device *dev);
129 #endif
130 
131 #endif /* ZEPHYR_DRIVERS_SENSOR_ISM330DHCX_ISM330DHCX_H_ */
132