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