1
2
3
4
5
6
7
8
9
10
11#ifndef ST_SENSORS_H
12#define ST_SENSORS_H
13
14#include <linux/i2c.h>
15#include <linux/spi/spi.h>
16#include <linux/irqreturn.h>
17#include <linux/iio/trigger.h>
18#include <linux/bitops.h>
19#include <linux/regulator/consumer.h>
20
21#include <linux/platform_data/st_sensors_pdata.h>
22
23#define ST_SENSORS_TX_MAX_LENGTH 2
24#define ST_SENSORS_RX_MAX_LENGTH 6
25
26#define ST_SENSORS_ODR_LIST_MAX 10
27#define ST_SENSORS_FULLSCALE_AVL_MAX 10
28
29#define ST_SENSORS_NUMBER_ALL_CHANNELS 4
30#define ST_SENSORS_ENABLE_ALL_AXIS 0x07
31#define ST_SENSORS_SCAN_X 0
32#define ST_SENSORS_SCAN_Y 1
33#define ST_SENSORS_SCAN_Z 2
34#define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01
35#define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00
36#define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f
37#define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20
38#define ST_SENSORS_DEFAULT_AXIS_MASK 0x07
39#define ST_SENSORS_DEFAULT_AXIS_N_BIT 3
40
41#define ST_SENSORS_MAX_NAME 17
42#define ST_SENSORS_MAX_4WAI 7
43
44#define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
45 ch2, s, endian, rbits, sbits, addr) \
46{ \
47 .type = device_type, \
48 .modified = mod, \
49 .info_mask_separate = mask, \
50 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
51 .scan_index = index, \
52 .channel2 = ch2, \
53 .address = addr, \
54 .scan_type = { \
55 .sign = s, \
56 .realbits = rbits, \
57 .shift = sbits - rbits, \
58 .storagebits = sbits, \
59 .endianness = endian, \
60 }, \
61}
62
63#define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
64 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
65 st_sensors_sysfs_sampling_frequency_avail)
66
67#define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
68 IIO_DEVICE_ATTR(name, S_IRUGO, \
69 st_sensors_sysfs_scale_avail, NULL , 0);
70
71struct st_sensor_odr_avl {
72 unsigned int hz;
73 u8 value;
74};
75
76struct st_sensor_odr {
77 u8 addr;
78 u8 mask;
79 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
80};
81
82struct st_sensor_power {
83 u8 addr;
84 u8 mask;
85 u8 value_off;
86 u8 value_on;
87};
88
89struct st_sensor_axis {
90 u8 addr;
91 u8 mask;
92};
93
94struct st_sensor_fullscale_avl {
95 unsigned int num;
96 u8 value;
97 unsigned int gain;
98 unsigned int gain2;
99};
100
101struct st_sensor_fullscale {
102 u8 addr;
103 u8 mask;
104 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
105};
106
107
108
109
110
111
112struct st_sensor_bdu {
113 u8 addr;
114 u8 mask;
115};
116
117
118
119
120
121
122
123
124
125
126
127
128struct st_sensor_data_ready_irq {
129 u8 addr;
130 u8 mask_int1;
131 u8 mask_int2;
132 u8 addr_ihl;
133 u8 mask_ihl;
134 struct {
135 u8 en_addr;
136 u8 en_mask;
137 } ig1;
138};
139
140
141
142
143
144
145
146
147
148struct st_sensor_transfer_buffer {
149 struct mutex buf_lock;
150 u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
151 u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
152};
153
154
155
156
157
158
159
160struct st_sensor_transfer_function {
161 int (*read_byte) (struct st_sensor_transfer_buffer *tb,
162 struct device *dev, u8 reg_addr, u8 *res_byte);
163 int (*write_byte) (struct st_sensor_transfer_buffer *tb,
164 struct device *dev, u8 reg_addr, u8 data);
165 int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
166 struct device *dev, u8 reg_addr, int len, u8 *data,
167 bool multiread_bit);
168};
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185struct st_sensor_settings {
186 u8 wai;
187 u8 wai_addr;
188 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
189 struct iio_chan_spec *ch;
190 int num_ch;
191 struct st_sensor_odr odr;
192 struct st_sensor_power pw;
193 struct st_sensor_axis enable_axis;
194 struct st_sensor_fullscale fs;
195 struct st_sensor_bdu bdu;
196 struct st_sensor_data_ready_irq drdy_irq;
197 bool multi_read_bit;
198 unsigned int bootime;
199};
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219struct st_sensor_data {
220 struct device *dev;
221 struct iio_trigger *trig;
222 struct st_sensor_settings *sensor_settings;
223 struct st_sensor_fullscale_avl *current_fullscale;
224 struct regulator *vdd;
225 struct regulator *vdd_io;
226
227 bool enabled;
228 bool multiread_bit;
229
230 char *buffer_data;
231
232 unsigned int odr;
233 unsigned int num_data_channels;
234
235 u8 drdy_int_pin;
236
237 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
238
239 const struct st_sensor_transfer_function *tf;
240 struct st_sensor_transfer_buffer tb;
241};
242
243#ifdef CONFIG_IIO_BUFFER
244irqreturn_t st_sensors_trigger_handler(int irq, void *p);
245
246int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf);
247#endif
248
249#ifdef CONFIG_IIO_TRIGGER
250int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
251 const struct iio_trigger_ops *trigger_ops);
252
253void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
254
255#else
256static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
257 const struct iio_trigger_ops *trigger_ops)
258{
259 return 0;
260}
261static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
262{
263 return;
264}
265#endif
266
267int st_sensors_init_sensor(struct iio_dev *indio_dev,
268 struct st_sensors_platform_data *pdata);
269
270int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
271
272int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
273
274void st_sensors_power_enable(struct iio_dev *indio_dev);
275
276void st_sensors_power_disable(struct iio_dev *indio_dev);
277
278int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
279 unsigned reg, unsigned writeval,
280 unsigned *readval);
281
282int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
283
284int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
285
286int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
287
288int st_sensors_read_info_raw(struct iio_dev *indio_dev,
289 struct iio_chan_spec const *ch, int *val);
290
291int st_sensors_check_device_support(struct iio_dev *indio_dev,
292 int num_sensors_list, const struct st_sensor_settings *sensor_settings);
293
294ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
295 struct device_attribute *attr, char *buf);
296
297ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
298 struct device_attribute *attr, char *buf);
299
300#endif
301