1 /*
2  * STMicroelectronics st_lsm6dsx sensor driver
3  *
4  * Copyright 2016 STMicroelectronics Inc.
5  *
6  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7  * Denis Ciocca <denis.ciocca@st.com>
8  *
9  * Licensed under the GPL-2.
10  */
11 
12 #ifndef ST_LSM6DSX_H
13 #define ST_LSM6DSX_H
14 
15 #include <linux/device.h>
16 
17 #define ST_LSM6DS3_DEV_NAME	"lsm6ds3"
18 #define ST_LSM6DS3H_DEV_NAME	"lsm6ds3h"
19 #define ST_LSM6DSL_DEV_NAME	"lsm6dsl"
20 #define ST_LSM6DSM_DEV_NAME	"lsm6dsm"
21 
22 enum st_lsm6dsx_hw_id {
23 	ST_LSM6DS3_ID,
24 	ST_LSM6DS3H_ID,
25 	ST_LSM6DSL_ID,
26 	ST_LSM6DSM_ID,
27 	ST_LSM6DSX_MAX_ID,
28 };
29 
30 #define ST_LSM6DSX_BUFF_SIZE		256
31 #define ST_LSM6DSX_CHAN_SIZE		2
32 #define ST_LSM6DSX_SAMPLE_SIZE		6
33 #define ST_LSM6DSX_MAX_WORD_LEN		((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
34 					 ST_LSM6DSX_SAMPLE_SIZE)
35 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
36 
37 struct st_lsm6dsx_reg {
38 	u8 addr;
39 	u8 mask;
40 };
41 
42 /**
43  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
44  * @fifo_th: FIFO threshold register info (addr + mask).
45  * @fifo_diff: FIFO diff status register info (addr + mask).
46  * @th_wl: FIFO threshold word length.
47  */
48 struct st_lsm6dsx_fifo_ops {
49 	struct {
50 		u8 addr;
51 		u16 mask;
52 	} fifo_th;
53 	struct {
54 		u8 addr;
55 		u16 mask;
56 	} fifo_diff;
57 	u8 th_wl;
58 };
59 
60 /**
61  * struct st_lsm6dsx_settings - ST IMU sensor settings
62  * @wai: Sensor WhoAmI default value.
63  * @max_fifo_size: Sensor max fifo length in FIFO words.
64  * @id: List of hw id supported by the driver configuration.
65  * @decimator: List of decimator register info (addr + mask).
66  * @fifo_ops: Sensor hw FIFO parameters.
67  */
68 struct st_lsm6dsx_settings {
69 	u8 wai;
70 	u16 max_fifo_size;
71 	enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
72 	struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
73 	struct st_lsm6dsx_fifo_ops fifo_ops;
74 };
75 
76 enum st_lsm6dsx_sensor_id {
77 	ST_LSM6DSX_ID_ACC,
78 	ST_LSM6DSX_ID_GYRO,
79 	ST_LSM6DSX_ID_MAX,
80 };
81 
82 enum st_lsm6dsx_fifo_mode {
83 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
84 	ST_LSM6DSX_FIFO_CONT = 0x6,
85 };
86 
87 /**
88  * struct st_lsm6dsx_sensor - ST IMU sensor instance
89  * @name: Sensor name.
90  * @id: Sensor identifier.
91  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
92  * @gain: Configured sensor sensitivity.
93  * @odr: Output data rate of the sensor [Hz].
94  * @watermark: Sensor watermark level.
95  * @sip: Number of samples in a given pattern.
96  * @decimator: FIFO decimation factor.
97  * @delta_ts: Delta time between two consecutive interrupts.
98  * @ts: Latest timestamp from the interrupt handler.
99  */
100 struct st_lsm6dsx_sensor {
101 	char name[32];
102 	enum st_lsm6dsx_sensor_id id;
103 	struct st_lsm6dsx_hw *hw;
104 
105 	u32 gain;
106 	u16 odr;
107 
108 	u16 watermark;
109 	u8 sip;
110 	u8 decimator;
111 
112 	s64 delta_ts;
113 	s64 ts;
114 };
115 
116 /**
117  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
118  * @dev: Pointer to instance of struct device (I2C or SPI).
119  * @regmap: Register map of the device.
120  * @irq: Device interrupt line (I2C or SPI).
121  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
122  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
123  * @fifo_mode: FIFO operating mode supported by the device.
124  * @enable_mask: Enabled sensor bitmask.
125  * @sip: Total number of samples (acc/gyro) in a given pattern.
126  * @buff: Device read buffer.
127  * @iio_devs: Pointers to acc/gyro iio_dev instances.
128  * @settings: Pointer to the specific sensor settings in use.
129  */
130 struct st_lsm6dsx_hw {
131 	struct device *dev;
132 	struct regmap *regmap;
133 	int irq;
134 
135 	struct mutex fifo_lock;
136 	struct mutex conf_lock;
137 
138 	enum st_lsm6dsx_fifo_mode fifo_mode;
139 	u8 enable_mask;
140 	u8 sip;
141 
142 	u8 *buff;
143 
144 	struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
145 
146 	const struct st_lsm6dsx_settings *settings;
147 };
148 
149 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
150 
151 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
152 		     struct regmap *regmap);
153 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
154 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
155 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
156 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
157 				u16 watermark);
158 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
159 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
160 			     enum st_lsm6dsx_fifo_mode fifo_mode);
161 
162 #endif /* ST_LSM6DSX_H */
163