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_LSM6DSM_DEV_NAME	"lsm6dsm"
19 
20 enum st_lsm6dsx_hw_id {
21 	ST_LSM6DS3_ID,
22 	ST_LSM6DSM_ID,
23 };
24 
25 #define ST_LSM6DSX_CHAN_SIZE		2
26 #define ST_LSM6DSX_SAMPLE_SIZE		6
27 #define ST_LSM6DSX_SAMPLE_DEPTH		(ST_LSM6DSX_SAMPLE_SIZE / \
28 					 ST_LSM6DSX_CHAN_SIZE)
29 
30 #if defined(CONFIG_SPI_MASTER)
31 #define ST_LSM6DSX_RX_MAX_LENGTH	256
32 #define ST_LSM6DSX_TX_MAX_LENGTH	8
33 
34 struct st_lsm6dsx_transfer_buffer {
35 	u8 rx_buf[ST_LSM6DSX_RX_MAX_LENGTH];
36 	u8 tx_buf[ST_LSM6DSX_TX_MAX_LENGTH] ____cacheline_aligned;
37 };
38 #endif /* CONFIG_SPI_MASTER */
39 
40 struct st_lsm6dsx_transfer_function {
41 	int (*read)(struct device *dev, u8 addr, int len, u8 *data);
42 	int (*write)(struct device *dev, u8 addr, int len, u8 *data);
43 };
44 
45 struct st_lsm6dsx_reg {
46 	u8 addr;
47 	u8 mask;
48 };
49 
50 struct st_lsm6dsx_settings {
51 	u8 wai;
52 	u16 max_fifo_size;
53 	enum st_lsm6dsx_hw_id id;
54 };
55 
56 enum st_lsm6dsx_sensor_id {
57 	ST_LSM6DSX_ID_ACC,
58 	ST_LSM6DSX_ID_GYRO,
59 	ST_LSM6DSX_ID_MAX,
60 };
61 
62 enum st_lsm6dsx_fifo_mode {
63 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
64 	ST_LSM6DSX_FIFO_CONT = 0x6,
65 };
66 
67 /**
68  * struct st_lsm6dsx_sensor - ST IMU sensor instance
69  * @id: Sensor identifier.
70  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
71  * @gain: Configured sensor sensitivity.
72  * @odr: Output data rate of the sensor [Hz].
73  * @watermark: Sensor watermark level.
74  * @sip: Number of samples in a given pattern.
75  * @decimator: FIFO decimation factor.
76  * @decimator_mask: Sensor mask for decimation register.
77  * @delta_ts: Delta time between two consecutive interrupts.
78  * @ts: Latest timestamp from the interrupt handler.
79  */
80 struct st_lsm6dsx_sensor {
81 	enum st_lsm6dsx_sensor_id id;
82 	struct st_lsm6dsx_hw *hw;
83 
84 	u32 gain;
85 	u16 odr;
86 
87 	u16 watermark;
88 	u8 sip;
89 	u8 decimator;
90 	u8 decimator_mask;
91 
92 	s64 delta_ts;
93 	s64 ts;
94 };
95 
96 /**
97  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
98  * @dev: Pointer to instance of struct device (I2C or SPI).
99  * @irq: Device interrupt line (I2C or SPI).
100  * @lock: Mutex to protect read and write operations.
101  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
102  * @fifo_mode: FIFO operating mode supported by the device.
103  * @enable_mask: Enabled sensor bitmask.
104  * @sip: Total number of samples (acc/gyro) in a given pattern.
105  * @iio_devs: Pointers to acc/gyro iio_dev instances.
106  * @settings: Pointer to the specific sensor settings in use.
107  * @tf: Transfer function structure used by I/O operations.
108  * @tb: Transfer buffers used by SPI I/O operations.
109  */
110 struct st_lsm6dsx_hw {
111 	struct device *dev;
112 	int irq;
113 
114 	struct mutex lock;
115 	struct mutex fifo_lock;
116 
117 	enum st_lsm6dsx_fifo_mode fifo_mode;
118 	u8 enable_mask;
119 	u8 sip;
120 
121 	struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
122 
123 	const struct st_lsm6dsx_settings *settings;
124 
125 	const struct st_lsm6dsx_transfer_function *tf;
126 #if defined(CONFIG_SPI_MASTER)
127 	struct st_lsm6dsx_transfer_buffer tb;
128 #endif /* CONFIG_SPI_MASTER */
129 };
130 
131 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
132 		     const struct st_lsm6dsx_transfer_function *tf_ops);
133 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
134 int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
135 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
136 int st_lsm6dsx_write_with_mask(struct st_lsm6dsx_hw *hw, u8 addr, u8 mask,
137 			       u8 val);
138 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
139 				u16 watermark);
140 
141 #endif /* ST_LSM6DSX_H */
142