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#define ST_SENSORS_DEFAULT_STAT_ADDR 0x27
41
42#define ST_SENSORS_MAX_NAME 17
43#define ST_SENSORS_MAX_4WAI 7
44
45#define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
46 ch2, s, endian, rbits, sbits, addr) \
47{ \
48 .type = device_type, \
49 .modified = mod, \
50 .info_mask_separate = mask, \
51 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
52 .scan_index = index, \
53 .channel2 = ch2, \
54 .address = addr, \
55 .scan_type = { \
56 .sign = s, \
57 .realbits = rbits, \
58 .shift = sbits - rbits, \
59 .storagebits = sbits, \
60 .endianness = endian, \
61 }, \
62}
63
64#define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
65 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
66 st_sensors_sysfs_sampling_frequency_avail)
67
68#define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
69 IIO_DEVICE_ATTR(name, S_IRUGO, \
70 st_sensors_sysfs_scale_avail, NULL , 0);
71
72struct st_sensor_odr_avl {
73 unsigned int hz;
74 u8 value;
75};
76
77struct st_sensor_odr {
78 u8 addr;
79 u8 mask;
80 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
81};
82
83struct st_sensor_power {
84 u8 addr;
85 u8 mask;
86 u8 value_off;
87 u8 value_on;
88};
89
90struct st_sensor_axis {
91 u8 addr;
92 u8 mask;
93};
94
95struct st_sensor_fullscale_avl {
96 unsigned int num;
97 u8 value;
98 unsigned int gain;
99 unsigned int gain2;
100};
101
102struct st_sensor_fullscale {
103 u8 addr;
104 u8 mask;
105 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
106};
107
108struct st_sensor_sim {
109 u8 addr;
110 u8 value;
111};
112
113
114
115
116
117
118struct st_sensor_bdu {
119 u8 addr;
120 u8 mask;
121};
122
123
124
125
126
127
128struct st_sensor_das {
129 u8 addr;
130 u8 mask;
131};
132
133
134
135
136
137
138
139
140struct st_sensor_int_drdy {
141 u8 addr;
142 u8 mask;
143 u8 addr_od;
144 u8 mask_od;
145};
146
147
148
149
150
151
152
153
154
155
156
157
158struct st_sensor_data_ready_irq {
159 struct st_sensor_int_drdy int1;
160 struct st_sensor_int_drdy int2;
161 u8 addr_ihl;
162 u8 mask_ihl;
163 struct {
164 u8 addr;
165 u8 mask;
166 } stat_drdy;
167 struct {
168 u8 en_addr;
169 u8 en_mask;
170 } ig1;
171};
172
173
174
175
176
177
178
179
180
181struct st_sensor_transfer_buffer {
182 struct mutex buf_lock;
183 u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
184 u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
185};
186
187
188
189
190
191
192
193struct st_sensor_transfer_function {
194 int (*read_byte) (struct st_sensor_transfer_buffer *tb,
195 struct device *dev, u8 reg_addr, u8 *res_byte);
196 int (*write_byte) (struct st_sensor_transfer_buffer *tb,
197 struct device *dev, u8 reg_addr, u8 data);
198 int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
199 struct device *dev, u8 reg_addr, int len, u8 *data,
200 bool multiread_bit);
201};
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220struct st_sensor_settings {
221 u8 wai;
222 u8 wai_addr;
223 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
224 struct iio_chan_spec *ch;
225 int num_ch;
226 struct st_sensor_odr odr;
227 struct st_sensor_power pw;
228 struct st_sensor_axis enable_axis;
229 struct st_sensor_fullscale fs;
230 struct st_sensor_bdu bdu;
231 struct st_sensor_das das;
232 struct st_sensor_data_ready_irq drdy_irq;
233 struct st_sensor_sim sim;
234 bool multi_read_bit;
235 unsigned int bootime;
236};
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260struct st_sensor_data {
261 struct device *dev;
262 struct iio_trigger *trig;
263 struct st_sensor_settings *sensor_settings;
264 struct st_sensor_fullscale_avl *current_fullscale;
265 struct regulator *vdd;
266 struct regulator *vdd_io;
267
268 bool enabled;
269 bool multiread_bit;
270
271 char *buffer_data;
272
273 unsigned int odr;
274 unsigned int num_data_channels;
275
276 u8 drdy_int_pin;
277 bool int_pin_open_drain;
278
279 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
280
281 const struct st_sensor_transfer_function *tf;
282 struct st_sensor_transfer_buffer tb;
283
284 bool edge_irq;
285 bool hw_irq_trigger;
286 s64 hw_timestamp;
287};
288
289#ifdef CONFIG_IIO_BUFFER
290irqreturn_t st_sensors_trigger_handler(int irq, void *p);
291#endif
292
293#ifdef CONFIG_IIO_TRIGGER
294int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
295 const struct iio_trigger_ops *trigger_ops);
296
297void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
298int st_sensors_validate_device(struct iio_trigger *trig,
299 struct iio_dev *indio_dev);
300#else
301static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
302 const struct iio_trigger_ops *trigger_ops)
303{
304 return 0;
305}
306static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
307{
308 return;
309}
310#define st_sensors_validate_device NULL
311#endif
312
313int st_sensors_init_sensor(struct iio_dev *indio_dev,
314 struct st_sensors_platform_data *pdata);
315
316int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
317
318int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
319
320int st_sensors_power_enable(struct iio_dev *indio_dev);
321
322void st_sensors_power_disable(struct iio_dev *indio_dev);
323
324int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
325 unsigned reg, unsigned writeval,
326 unsigned *readval);
327
328int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
329
330int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
331
332int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
333
334int st_sensors_read_info_raw(struct iio_dev *indio_dev,
335 struct iio_chan_spec const *ch, int *val);
336
337int st_sensors_check_device_support(struct iio_dev *indio_dev,
338 int num_sensors_list, const struct st_sensor_settings *sensor_settings);
339
340ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
341 struct device_attribute *attr, char *buf);
342
343ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
344 struct device_attribute *attr, char *buf);
345
346#ifdef CONFIG_OF
347void st_sensors_of_name_probe(struct device *dev,
348 const struct of_device_id *match,
349 char *name, int len);
350#else
351static inline void st_sensors_of_name_probe(struct device *dev,
352 const struct of_device_id *match,
353 char *name, int len)
354{
355}
356#endif
357
358#endif
359