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