1 /* ST Microelectronics IIS2ICLX 2-axis accelerometer 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/iis2iclx.pdf
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_IIS2ICLX_IIS2ICLX_H_
12 #define ZEPHYR_DRIVERS_SENSOR_IIS2ICLX_IIS2ICLX_H_
13 
14 #include <drivers/sensor.h>
15 #include <zephyr/types.h>
16 #include <drivers/gpio.h>
17 #include <sys/util.h>
18 #include <stmemsc.h>
19 #include "iis2iclx_reg.h"
20 
21 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
22 #include <drivers/spi.h>
23 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
24 
25 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
26 #include <drivers/i2c.h>
27 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
28 
29 #define IIS2ICLX_EN_BIT					0x01
30 #define IIS2ICLX_DIS_BIT					0x00
31 
32 /* Accel sensor sensitivity grain is 15 ug/LSB */
33 #define GAIN_UNIT_XL				(15LL)
34 
35 #define SENSOR_PI_DOUBLE			(SENSOR_PI / 1000000.0)
36 #define SENSOR_DEG2RAD_DOUBLE			(SENSOR_PI_DOUBLE / 180)
37 #define SENSOR_G_DOUBLE				(SENSOR_G / 1000000.0)
38 
39 struct iis2iclx_config {
40 	stmdev_ctx_t ctx;
41 	union {
42 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
43 		const struct stmemsc_cfg_i2c i2c;
44 #endif
45 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
46 		const struct stmemsc_cfg_spi spi;
47 #endif
48 	} stmemsc_cfg;
49 	uint8_t odr;
50 	uint8_t range;
51 #ifdef CONFIG_IIS2ICLX_TRIGGER
52 	bool trig_enabled;
53 	uint8_t int_pin;
54 	const struct gpio_dt_spec gpio_drdy;
55 #endif /* CONFIG_IIS2ICLX_TRIGGER */
56 };
57 
58 #define IIS2ICLX_SHUB_MAX_NUM_SLVS			2
59 
60 struct iis2iclx_data {
61 	const struct device *dev;
62 	int16_t acc[2];
63 	uint32_t acc_gain;
64 #if defined(CONFIG_IIS2ICLX_ENABLE_TEMP)
65 	int temp_sample;
66 #endif
67 #if defined(CONFIG_IIS2ICLX_SENSORHUB)
68 	uint8_t ext_data[2][6];
69 	uint16_t magn_gain;
70 
71 	struct hts221_data {
72 		int16_t x0;
73 		int16_t x1;
74 		int16_t y0;
75 		int16_t y1;
76 	} hts221;
77 
78 	bool shub_inited;
79 	uint8_t num_ext_dev;
80 	uint8_t shub_ext[IIS2ICLX_SHUB_MAX_NUM_SLVS];
81 #endif /* CONFIG_IIS2ICLX_SENSORHUB */
82 
83 	uint16_t accel_freq;
84 	uint8_t accel_fs;
85 
86 #ifdef CONFIG_IIS2ICLX_TRIGGER
87 	struct gpio_callback gpio_cb;
88 	sensor_trigger_handler_t handler_drdy_acc;
89 	sensor_trigger_handler_t handler_drdy_temp;
90 
91 #if defined(CONFIG_IIS2ICLX_TRIGGER_OWN_THREAD)
92 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_IIS2ICLX_THREAD_STACK_SIZE);
93 	struct k_thread thread;
94 	struct k_sem gpio_sem;
95 #elif defined(CONFIG_IIS2ICLX_TRIGGER_GLOBAL_THREAD)
96 	struct k_work work;
97 #endif
98 #endif /* CONFIG_IIS2ICLX_TRIGGER */
99 };
100 
101 #if defined(CONFIG_IIS2ICLX_SENSORHUB)
102 int iis2iclx_shub_init(const struct device *dev);
103 int iis2iclx_shub_fetch_external_devs(const struct device *dev);
104 int iis2iclx_shub_get_idx(const struct device *dev, enum sensor_channel type);
105 int iis2iclx_shub_config(const struct device *dev, enum sensor_channel chan,
106 			   enum sensor_attribute attr,
107 			   const struct sensor_value *val);
108 #endif /* CONFIG_IIS2ICLX_SENSORHUB */
109 
110 #ifdef CONFIG_IIS2ICLX_TRIGGER
111 int iis2iclx_trigger_set(const struct device *dev,
112 			   const struct sensor_trigger *trig,
113 			   sensor_trigger_handler_t handler);
114 
115 int iis2iclx_init_interrupt(const struct device *dev);
116 #endif
117 
118 #endif /* ZEPHYR_DRIVERS_SENSOR_IIS2ICLX_IIS2ICLX_H_ */
119