Merge tag 'staging-4.21-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[sfrench/cifs-2.6.git] / drivers / iio / imu / st_lsm6dsx / st_lsm6dsx.h
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 #define ST_ISM330DLC_DEV_NAME   "ism330dlc"
22 #define ST_LSM6DSO_DEV_NAME     "lsm6dso"
23
24 enum st_lsm6dsx_hw_id {
25         ST_LSM6DS3_ID,
26         ST_LSM6DS3H_ID,
27         ST_LSM6DSL_ID,
28         ST_LSM6DSM_ID,
29         ST_ISM330DLC_ID,
30         ST_LSM6DSO_ID,
31         ST_LSM6DSX_MAX_ID,
32 };
33
34 #define ST_LSM6DSX_BUFF_SIZE            512
35 #define ST_LSM6DSX_CHAN_SIZE            2
36 #define ST_LSM6DSX_SAMPLE_SIZE          6
37 #define ST_LSM6DSX_TAG_SIZE             1
38 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE   (ST_LSM6DSX_SAMPLE_SIZE + \
39                                          ST_LSM6DSX_TAG_SIZE)
40 #define ST_LSM6DSX_MAX_WORD_LEN         ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
41                                          ST_LSM6DSX_SAMPLE_SIZE)
42 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN  ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
43                                          * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
44 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
45
46 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx)              \
47 {                                                                       \
48         .type = chan_type,                                              \
49         .address = addr,                                                \
50         .modified = 1,                                                  \
51         .channel2 = mod,                                                \
52         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |                  \
53                               BIT(IIO_CHAN_INFO_SCALE),                 \
54         .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),        \
55         .scan_index = scan_idx,                                         \
56         .scan_type = {                                                  \
57                 .sign = 's',                                            \
58                 .realbits = 16,                                         \
59                 .storagebits = 16,                                      \
60                 .endianness = IIO_LE,                                   \
61         },                                                              \
62 }
63
64 struct st_lsm6dsx_reg {
65         u8 addr;
66         u8 mask;
67 };
68
69 struct st_lsm6dsx_hw;
70
71 struct st_lsm6dsx_odr {
72         u16 hz;
73         u8 val;
74 };
75
76 #define ST_LSM6DSX_ODR_LIST_SIZE        6
77 struct st_lsm6dsx_odr_table_entry {
78         struct st_lsm6dsx_reg reg;
79         struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
80 };
81
82 struct st_lsm6dsx_fs {
83         u32 gain;
84         u8 val;
85 };
86
87 #define ST_LSM6DSX_FS_LIST_SIZE         4
88 struct st_lsm6dsx_fs_table_entry {
89         struct st_lsm6dsx_reg reg;
90         struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
91 };
92
93 /**
94  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
95  * @read_fifo: Read FIFO callback.
96  * @fifo_th: FIFO threshold register info (addr + mask).
97  * @fifo_diff: FIFO diff status register info (addr + mask).
98  * @th_wl: FIFO threshold word length.
99  */
100 struct st_lsm6dsx_fifo_ops {
101         int (*read_fifo)(struct st_lsm6dsx_hw *hw);
102         struct {
103                 u8 addr;
104                 u16 mask;
105         } fifo_th;
106         struct {
107                 u8 addr;
108                 u16 mask;
109         } fifo_diff;
110         u8 th_wl;
111 };
112
113 /**
114  * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
115  * @timer_en: Hw timer enable register info (addr + mask).
116  * @hr_timer: Hw timer resolution register info (addr + mask).
117  * @fifo_en: Hw timer FIFO enable register info (addr + mask).
118  * @decimator: Hw timer FIFO decimator register info (addr + mask).
119  */
120 struct st_lsm6dsx_hw_ts_settings {
121         struct st_lsm6dsx_reg timer_en;
122         struct st_lsm6dsx_reg hr_timer;
123         struct st_lsm6dsx_reg fifo_en;
124         struct st_lsm6dsx_reg decimator;
125 };
126
127 /**
128  * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
129  * @page_mux: register page mux info (addr + mask).
130  * @master_en: master config register info (addr + mask).
131  * @pullup_en: i2c controller pull-up register info (addr + mask).
132  * @aux_sens: aux sensor register info (addr + mask).
133  * @wr_once: write_once register info (addr + mask).
134  * @shub_out: sensor hub first output register info.
135  * @slv0_addr: slave0 address in secondary page.
136  * @dw_slv0_addr: slave0 write register address in secondary page.
137  * @batch_en: Enable/disable FIFO batching.
138  */
139 struct st_lsm6dsx_shub_settings {
140         struct st_lsm6dsx_reg page_mux;
141         struct st_lsm6dsx_reg master_en;
142         struct st_lsm6dsx_reg pullup_en;
143         struct st_lsm6dsx_reg aux_sens;
144         struct st_lsm6dsx_reg wr_once;
145         u8 shub_out;
146         u8 slv0_addr;
147         u8 dw_slv0_addr;
148         u8 batch_en;
149 };
150
151 enum st_lsm6dsx_ext_sensor_id {
152         ST_LSM6DSX_ID_MAGN,
153 };
154
155 /**
156  * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
157  * @i2c_addr: I2c slave address list.
158  * @wai: Wai address info.
159  * @id: external sensor id.
160  * @odr: Output data rate of the sensor [Hz].
161  * @gain: Configured sensor sensitivity.
162  * @temp_comp: Temperature compensation register info (addr + mask).
163  * @pwr_table: Power on register info (addr + mask).
164  * @off_canc: Offset cancellation register info (addr + mask).
165  * @bdu: Block data update register info (addr + mask).
166  * @out: Output register info.
167  */
168 struct st_lsm6dsx_ext_dev_settings {
169         u8 i2c_addr[2];
170         struct {
171                 u8 addr;
172                 u8 val;
173         } wai;
174         enum st_lsm6dsx_ext_sensor_id id;
175         struct st_lsm6dsx_odr_table_entry odr_table;
176         struct st_lsm6dsx_fs_table_entry fs_table;
177         struct st_lsm6dsx_reg temp_comp;
178         struct {
179                 struct st_lsm6dsx_reg reg;
180                 u8 off_val;
181                 u8 on_val;
182         } pwr_table;
183         struct st_lsm6dsx_reg off_canc;
184         struct st_lsm6dsx_reg bdu;
185         struct {
186                 u8 addr;
187                 u8 len;
188         } out;
189 };
190
191 /**
192  * struct st_lsm6dsx_settings - ST IMU sensor settings
193  * @wai: Sensor WhoAmI default value.
194  * @max_fifo_size: Sensor max fifo length in FIFO words.
195  * @id: List of hw id supported by the driver configuration.
196  * @decimator: List of decimator register info (addr + mask).
197  * @batch: List of FIFO batching register info (addr + mask).
198  * @fifo_ops: Sensor hw FIFO parameters.
199  * @ts_settings: Hw timer related settings.
200  * @shub_settings: i2c controller related settings.
201  */
202 struct st_lsm6dsx_settings {
203         u8 wai;
204         u16 max_fifo_size;
205         enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
206         struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
207         struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
208         struct st_lsm6dsx_fifo_ops fifo_ops;
209         struct st_lsm6dsx_hw_ts_settings ts_settings;
210         struct st_lsm6dsx_shub_settings shub_settings;
211 };
212
213 enum st_lsm6dsx_sensor_id {
214         ST_LSM6DSX_ID_GYRO,
215         ST_LSM6DSX_ID_ACC,
216         ST_LSM6DSX_ID_EXT0,
217         ST_LSM6DSX_ID_EXT1,
218         ST_LSM6DSX_ID_EXT2,
219         ST_LSM6DSX_ID_MAX,
220 };
221
222 enum st_lsm6dsx_fifo_mode {
223         ST_LSM6DSX_FIFO_BYPASS = 0x0,
224         ST_LSM6DSX_FIFO_CONT = 0x6,
225 };
226
227 /**
228  * struct st_lsm6dsx_sensor - ST IMU sensor instance
229  * @name: Sensor name.
230  * @id: Sensor identifier.
231  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
232  * @gain: Configured sensor sensitivity.
233  * @odr: Output data rate of the sensor [Hz].
234  * @watermark: Sensor watermark level.
235  * @sip: Number of samples in a given pattern.
236  * @decimator: FIFO decimation factor.
237  * @ts_ref: Sensor timestamp reference for hw one.
238  * @ext_info: Sensor settings if it is connected to i2c controller
239  */
240 struct st_lsm6dsx_sensor {
241         char name[32];
242         enum st_lsm6dsx_sensor_id id;
243         struct st_lsm6dsx_hw *hw;
244
245         u32 gain;
246         u16 odr;
247
248         u16 watermark;
249         u8 sip;
250         u8 decimator;
251         s64 ts_ref;
252
253         struct {
254                 const struct st_lsm6dsx_ext_dev_settings *settings;
255                 u8 addr;
256         } ext_info;
257 };
258
259 /**
260  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
261  * @dev: Pointer to instance of struct device (I2C or SPI).
262  * @regmap: Register map of the device.
263  * @irq: Device interrupt line (I2C or SPI).
264  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
265  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
266  * @page_lock: Mutex to prevent concurrent memory page configuration.
267  * @fifo_mode: FIFO operating mode supported by the device.
268  * @enable_mask: Enabled sensor bitmask.
269  * @ts_sip: Total number of timestamp samples in a given pattern.
270  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
271  * @buff: Device read buffer.
272  * @iio_devs: Pointers to acc/gyro iio_dev instances.
273  * @settings: Pointer to the specific sensor settings in use.
274  */
275 struct st_lsm6dsx_hw {
276         struct device *dev;
277         struct regmap *regmap;
278         int irq;
279
280         struct mutex fifo_lock;
281         struct mutex conf_lock;
282         struct mutex page_lock;
283
284         enum st_lsm6dsx_fifo_mode fifo_mode;
285         u8 enable_mask;
286         u8 ts_sip;
287         u8 sip;
288
289         u8 *buff;
290
291         struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
292
293         const struct st_lsm6dsx_settings *settings;
294 };
295
296 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
297 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
298
299 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
300                      struct regmap *regmap);
301 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
302                                  bool enable);
303 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
304 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
305 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
306                                 u16 watermark);
307 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
308 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
309                              enum st_lsm6dsx_fifo_mode fifo_mode);
310 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
311 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
312 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
313 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
314 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
315 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
316
317 static inline int
318 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
319                               unsigned int mask, unsigned int val)
320 {
321         int err;
322
323         mutex_lock(&hw->page_lock);
324         err = regmap_update_bits(hw->regmap, addr, mask, val);
325         mutex_unlock(&hw->page_lock);
326
327         return err;
328 }
329
330 static inline int
331 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
332                        void *val, unsigned int len)
333 {
334         int err;
335
336         mutex_lock(&hw->page_lock);
337         err = regmap_bulk_read(hw->regmap, addr, val, len);
338         mutex_unlock(&hw->page_lock);
339
340         return err;
341 }
342
343 static inline int
344 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
345                         unsigned int val)
346 {
347         int err;
348
349         mutex_lock(&hw->page_lock);
350         err = regmap_write(hw->regmap, addr, val);
351         mutex_unlock(&hw->page_lock);
352
353         return err;
354 }
355
356 #endif /* ST_LSM6DSX_H */