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