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 <zephyr/drivers/sensor.h>
15 #include <zephyr/types.h>
16 #include <zephyr/drivers/gpio.h>
17 #include <zephyr/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 <zephyr/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 <zephyr/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 struct iis2iclx_config {
36 	stmdev_ctx_t ctx;
37 	union {
38 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
39 		const struct i2c_dt_spec i2c;
40 #endif
41 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
42 		const struct spi_dt_spec spi;
43 #endif
44 	} stmemsc_cfg;
45 	uint8_t odr;
46 	uint8_t range;
47 #ifdef CONFIG_IIS2ICLX_TRIGGER
48 	bool trig_enabled;
49 	uint8_t int_pin;
50 	const struct gpio_dt_spec gpio_drdy;
51 #endif /* CONFIG_IIS2ICLX_TRIGGER */
52 };
53 
54 #define IIS2ICLX_SHUB_MAX_NUM_SLVS			2
55 
56 struct iis2iclx_data {
57 	const struct device *dev;
58 	int16_t acc[2];
59 	uint32_t acc_gain;
60 #if defined(CONFIG_IIS2ICLX_ENABLE_TEMP)
61 	int temp_sample;
62 #endif
63 #if defined(CONFIG_IIS2ICLX_SENSORHUB)
64 	uint8_t ext_data[2][6];
65 	uint16_t magn_gain;
66 
67 	struct hts221_data {
68 		int16_t x0;
69 		int16_t x1;
70 		int16_t y0;
71 		int16_t y1;
72 	} hts221;
73 
74 	bool shub_inited;
75 	uint8_t num_ext_dev;
76 	uint8_t shub_ext[IIS2ICLX_SHUB_MAX_NUM_SLVS];
77 #endif /* CONFIG_IIS2ICLX_SENSORHUB */
78 
79 	uint16_t accel_freq;
80 	uint8_t accel_fs;
81 
82 #ifdef CONFIG_IIS2ICLX_TRIGGER
83 	struct gpio_callback gpio_cb;
84 	sensor_trigger_handler_t handler_drdy_acc;
85 	const struct sensor_trigger *trig_drdy_acc;
86 	sensor_trigger_handler_t handler_drdy_temp;
87 	const struct sensor_trigger *trig_drdy_temp;
88 
89 #if defined(CONFIG_IIS2ICLX_TRIGGER_OWN_THREAD)
90 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_IIS2ICLX_THREAD_STACK_SIZE);
91 	struct k_thread thread;
92 	struct k_sem gpio_sem;
93 #elif defined(CONFIG_IIS2ICLX_TRIGGER_GLOBAL_THREAD)
94 	struct k_work work;
95 #endif
96 #endif /* CONFIG_IIS2ICLX_TRIGGER */
97 };
98 
99 #if defined(CONFIG_IIS2ICLX_SENSORHUB)
100 int iis2iclx_shub_init(const struct device *dev);
101 int iis2iclx_shub_fetch_external_devs(const struct device *dev);
102 int iis2iclx_shub_get_idx(const struct device *dev, enum sensor_channel type);
103 int iis2iclx_shub_config(const struct device *dev, enum sensor_channel chan,
104 			   enum sensor_attribute attr,
105 			   const struct sensor_value *val);
106 #endif /* CONFIG_IIS2ICLX_SENSORHUB */
107 
108 #ifdef CONFIG_IIS2ICLX_TRIGGER
109 int iis2iclx_trigger_set(const struct device *dev,
110 			   const struct sensor_trigger *trig,
111 			   sensor_trigger_handler_t handler);
112 
113 int iis2iclx_init_interrupt(const struct device *dev);
114 #endif
115 
116 #endif /* ZEPHYR_DRIVERS_SENSOR_IIS2ICLX_IIS2ICLX_H_ */
117