]>
Commit | Line | Data |
---|---|---|
fda8d26e | 1 | // SPDX-License-Identifier: GPL-2.0-only |
290a6ce1 LB |
2 | /* |
3 | * STMicroelectronics st_lsm6dsx sensor driver | |
4 | * | |
5 | * The ST LSM6DSx IMU MEMS series consists of 3D digital accelerometer | |
6 | * and 3D digital gyroscope system-in-package with a digital I2C/SPI serial | |
7 | * interface standard output. | |
8 | * LSM6DSx IMU MEMS series has a dynamic user-selectable full-scale | |
9 | * acceleration range of +-2/+-4/+-8/+-16 g and an angular rate range of | |
10 | * +-125/+-245/+-500/+-1000/+-2000 dps | |
11 | * LSM6DSx series has an integrated First-In-First-Out (FIFO) buffer | |
12 | * allowing dynamic batching of sensor data. | |
13 | * | |
14 | * Supported sensors: | |
15 | * - LSM6DS3: | |
16 | * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416 | |
17 | * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16 | |
18 | * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000 | |
19 | * - FIFO size: 8KB | |
20 | * | |
179c8d60 | 21 | * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC: |
290a6ce1 LB |
22 | * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416 |
23 | * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16 | |
24 | * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000 | |
25 | * - FIFO size: 4KB | |
26 | * | |
43901008 | 27 | * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR |
801a6e0a LB |
28 | * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416 |
29 | * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16 | |
30 | * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000 | |
31 | * - FIFO size: 3KB | |
32 | * | |
290a6ce1 LB |
33 | * Copyright 2016 STMicroelectronics Inc. |
34 | * | |
35 | * Lorenzo Bianconi <lorenzo.bianconi@st.com> | |
36 | * Denis Ciocca <denis.ciocca@st.com> | |
290a6ce1 LB |
37 | */ |
38 | ||
39 | #include <linux/kernel.h> | |
40 | #include <linux/module.h> | |
41 | #include <linux/delay.h> | |
42 | #include <linux/iio/iio.h> | |
43 | #include <linux/iio/sysfs.h> | |
d3f77058 | 44 | #include <linux/pm.h> |
51a8b707 LB |
45 | #include <linux/regmap.h> |
46 | #include <linux/bitfield.h> | |
290a6ce1 | 47 | |
dba32904 LB |
48 | #include <linux/platform_data/st_sensors_pdata.h> |
49 | ||
290a6ce1 LB |
50 | #include "st_lsm6dsx.h" |
51 | ||
290a6ce1 | 52 | #define ST_LSM6DSX_REG_INT1_ADDR 0x0d |
dba32904 | 53 | #define ST_LSM6DSX_REG_INT2_ADDR 0x0e |
290a6ce1 LB |
54 | #define ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK BIT(3) |
55 | #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f | |
56 | #define ST_LSM6DSX_REG_RESET_ADDR 0x12 | |
57 | #define ST_LSM6DSX_REG_RESET_MASK BIT(0) | |
19435425 | 58 | #define ST_LSM6DSX_REG_BOOT_MASK BIT(7) |
290a6ce1 LB |
59 | #define ST_LSM6DSX_REG_BDU_ADDR 0x12 |
60 | #define ST_LSM6DSX_REG_BDU_MASK BIT(6) | |
61 | #define ST_LSM6DSX_REG_INT2_ON_INT1_ADDR 0x13 | |
62 | #define ST_LSM6DSX_REG_INT2_ON_INT1_MASK BIT(5) | |
290a6ce1 | 63 | |
290a6ce1 LB |
64 | #define ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR 0x28 |
65 | #define ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR 0x2a | |
66 | #define ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR 0x2c | |
67 | ||
290a6ce1 LB |
68 | #define ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR 0x22 |
69 | #define ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR 0x24 | |
70 | #define ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR 0x26 | |
71 | ||
290a6ce1 LB |
72 | static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = { |
73 | [ST_LSM6DSX_ID_ACC] = { | |
74 | .reg = { | |
b9fedb0a LB |
75 | .addr = 0x10, |
76 | .mask = GENMASK(7, 4), | |
290a6ce1 LB |
77 | }, |
78 | .odr_avl[0] = { 13, 0x01 }, | |
79 | .odr_avl[1] = { 26, 0x02 }, | |
80 | .odr_avl[2] = { 52, 0x03 }, | |
81 | .odr_avl[3] = { 104, 0x04 }, | |
82 | .odr_avl[4] = { 208, 0x05 }, | |
83 | .odr_avl[5] = { 416, 0x06 }, | |
84 | }, | |
85 | [ST_LSM6DSX_ID_GYRO] = { | |
86 | .reg = { | |
b9fedb0a LB |
87 | .addr = 0x11, |
88 | .mask = GENMASK(7, 4), | |
290a6ce1 LB |
89 | }, |
90 | .odr_avl[0] = { 13, 0x01 }, | |
91 | .odr_avl[1] = { 26, 0x02 }, | |
92 | .odr_avl[2] = { 52, 0x03 }, | |
93 | .odr_avl[3] = { 104, 0x04 }, | |
94 | .odr_avl[4] = { 208, 0x05 }, | |
95 | .odr_avl[5] = { 416, 0x06 }, | |
96 | } | |
97 | }; | |
98 | ||
290a6ce1 LB |
99 | static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = { |
100 | [ST_LSM6DSX_ID_ACC] = { | |
101 | .reg = { | |
b9fedb0a LB |
102 | .addr = 0x10, |
103 | .mask = GENMASK(3, 2), | |
290a6ce1 | 104 | }, |
b9fedb0a LB |
105 | .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 }, |
106 | .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 }, | |
107 | .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 }, | |
108 | .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 }, | |
290a6ce1 LB |
109 | }, |
110 | [ST_LSM6DSX_ID_GYRO] = { | |
111 | .reg = { | |
b9fedb0a LB |
112 | .addr = 0x11, |
113 | .mask = GENMASK(3, 2), | |
290a6ce1 | 114 | }, |
b9fedb0a LB |
115 | .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 }, |
116 | .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 }, | |
117 | .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 }, | |
118 | .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 }, | |
290a6ce1 LB |
119 | } |
120 | }; | |
121 | ||
122 | static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = { | |
123 | { | |
d068e4a0 | 124 | .wai = 0x69, |
8f2a88a2 | 125 | .max_fifo_size = 1365, |
d068e4a0 LB |
126 | .id = { |
127 | [0] = ST_LSM6DS3_ID, | |
128 | }, | |
7ca3ac9e LB |
129 | .decimator = { |
130 | [ST_LSM6DSX_ID_ACC] = { | |
131 | .addr = 0x08, | |
132 | .mask = GENMASK(2, 0), | |
133 | }, | |
134 | [ST_LSM6DSX_ID_GYRO] = { | |
135 | .addr = 0x08, | |
136 | .mask = GENMASK(5, 3), | |
137 | }, | |
138 | }, | |
92617c15 | 139 | .fifo_ops = { |
50ff457d | 140 | .read_fifo = st_lsm6dsx_read_fifo, |
92617c15 LB |
141 | .fifo_th = { |
142 | .addr = 0x06, | |
143 | .mask = GENMASK(11, 0), | |
144 | }, | |
145 | .fifo_diff = { | |
146 | .addr = 0x3a, | |
147 | .mask = GENMASK(11, 0), | |
148 | }, | |
149 | .th_wl = 3, /* 1LSB = 2B */ | |
150 | }, | |
21345107 LB |
151 | .ts_settings = { |
152 | .timer_en = { | |
153 | .addr = 0x58, | |
154 | .mask = BIT(7), | |
155 | }, | |
156 | .hr_timer = { | |
157 | .addr = 0x5c, | |
158 | .mask = BIT(4), | |
159 | }, | |
160 | .fifo_en = { | |
161 | .addr = 0x07, | |
162 | .mask = BIT(7), | |
163 | }, | |
164 | .decimator = { | |
165 | .addr = 0x09, | |
166 | .mask = GENMASK(5, 3), | |
167 | }, | |
168 | }, | |
290a6ce1 | 169 | }, |
df47710a LB |
170 | { |
171 | .wai = 0x69, | |
8f2a88a2 | 172 | .max_fifo_size = 682, |
df47710a LB |
173 | .id = { |
174 | [0] = ST_LSM6DS3H_ID, | |
175 | }, | |
7ca3ac9e LB |
176 | .decimator = { |
177 | [ST_LSM6DSX_ID_ACC] = { | |
178 | .addr = 0x08, | |
179 | .mask = GENMASK(2, 0), | |
180 | }, | |
181 | [ST_LSM6DSX_ID_GYRO] = { | |
182 | .addr = 0x08, | |
183 | .mask = GENMASK(5, 3), | |
184 | }, | |
185 | }, | |
92617c15 | 186 | .fifo_ops = { |
50ff457d | 187 | .read_fifo = st_lsm6dsx_read_fifo, |
92617c15 LB |
188 | .fifo_th = { |
189 | .addr = 0x06, | |
190 | .mask = GENMASK(11, 0), | |
191 | }, | |
192 | .fifo_diff = { | |
193 | .addr = 0x3a, | |
194 | .mask = GENMASK(11, 0), | |
195 | }, | |
196 | .th_wl = 3, /* 1LSB = 2B */ | |
197 | }, | |
21345107 LB |
198 | .ts_settings = { |
199 | .timer_en = { | |
200 | .addr = 0x58, | |
201 | .mask = BIT(7), | |
202 | }, | |
203 | .hr_timer = { | |
204 | .addr = 0x5c, | |
205 | .mask = BIT(4), | |
206 | }, | |
207 | .fifo_en = { | |
208 | .addr = 0x07, | |
209 | .mask = BIT(7), | |
210 | }, | |
211 | .decimator = { | |
212 | .addr = 0x09, | |
213 | .mask = GENMASK(5, 3), | |
214 | }, | |
215 | }, | |
df47710a | 216 | }, |
290a6ce1 | 217 | { |
d068e4a0 | 218 | .wai = 0x6a, |
8f2a88a2 | 219 | .max_fifo_size = 682, |
d068e4a0 | 220 | .id = { |
0b2a3e5f LB |
221 | [0] = ST_LSM6DSL_ID, |
222 | [1] = ST_LSM6DSM_ID, | |
179c8d60 | 223 | [2] = ST_ISM330DLC_ID, |
d068e4a0 | 224 | }, |
7ca3ac9e LB |
225 | .decimator = { |
226 | [ST_LSM6DSX_ID_ACC] = { | |
227 | .addr = 0x08, | |
228 | .mask = GENMASK(2, 0), | |
229 | }, | |
230 | [ST_LSM6DSX_ID_GYRO] = { | |
231 | .addr = 0x08, | |
232 | .mask = GENMASK(5, 3), | |
233 | }, | |
234 | }, | |
92617c15 | 235 | .fifo_ops = { |
50ff457d | 236 | .read_fifo = st_lsm6dsx_read_fifo, |
92617c15 LB |
237 | .fifo_th = { |
238 | .addr = 0x06, | |
be75eb86 | 239 | .mask = GENMASK(10, 0), |
92617c15 LB |
240 | }, |
241 | .fifo_diff = { | |
242 | .addr = 0x3a, | |
be75eb86 | 243 | .mask = GENMASK(10, 0), |
92617c15 LB |
244 | }, |
245 | .th_wl = 3, /* 1LSB = 2B */ | |
246 | }, | |
21345107 LB |
247 | .ts_settings = { |
248 | .timer_en = { | |
249 | .addr = 0x19, | |
250 | .mask = BIT(5), | |
251 | }, | |
252 | .hr_timer = { | |
253 | .addr = 0x5c, | |
254 | .mask = BIT(4), | |
255 | }, | |
256 | .fifo_en = { | |
257 | .addr = 0x07, | |
258 | .mask = BIT(7), | |
259 | }, | |
260 | .decimator = { | |
261 | .addr = 0x09, | |
262 | .mask = GENMASK(5, 3), | |
263 | }, | |
264 | }, | |
290a6ce1 | 265 | }, |
801a6e0a LB |
266 | { |
267 | .wai = 0x6c, | |
268 | .max_fifo_size = 512, | |
269 | .id = { | |
270 | [0] = ST_LSM6DSO_ID, | |
6af0e8a9 | 271 | [1] = ST_LSM6DSOX_ID, |
801a6e0a LB |
272 | }, |
273 | .batch = { | |
274 | [ST_LSM6DSX_ID_ACC] = { | |
275 | .addr = 0x09, | |
276 | .mask = GENMASK(3, 0), | |
277 | }, | |
278 | [ST_LSM6DSX_ID_GYRO] = { | |
279 | .addr = 0x09, | |
280 | .mask = GENMASK(7, 4), | |
281 | }, | |
282 | }, | |
283 | .fifo_ops = { | |
284 | .read_fifo = st_lsm6dsx_read_tagged_fifo, | |
285 | .fifo_th = { | |
286 | .addr = 0x07, | |
287 | .mask = GENMASK(8, 0), | |
288 | }, | |
289 | .fifo_diff = { | |
290 | .addr = 0x3a, | |
291 | .mask = GENMASK(8, 0), | |
292 | }, | |
293 | .th_wl = 1, | |
294 | }, | |
295 | .ts_settings = { | |
296 | .timer_en = { | |
297 | .addr = 0x19, | |
298 | .mask = BIT(5), | |
299 | }, | |
300 | .decimator = { | |
301 | .addr = 0x0a, | |
302 | .mask = GENMASK(7, 6), | |
303 | }, | |
304 | }, | |
c91c1c84 LB |
305 | .shub_settings = { |
306 | .page_mux = { | |
307 | .addr = 0x01, | |
308 | .mask = BIT(6), | |
309 | }, | |
310 | .master_en = { | |
311 | .addr = 0x14, | |
312 | .mask = BIT(2), | |
313 | }, | |
314 | .pullup_en = { | |
315 | .addr = 0x14, | |
316 | .mask = BIT(3), | |
317 | }, | |
318 | .aux_sens = { | |
319 | .addr = 0x14, | |
320 | .mask = GENMASK(1, 0), | |
321 | }, | |
6d0205fd LB |
322 | .wr_once = { |
323 | .addr = 0x14, | |
324 | .mask = BIT(6), | |
325 | }, | |
c91c1c84 LB |
326 | .shub_out = 0x02, |
327 | .slv0_addr = 0x15, | |
328 | .dw_slv0_addr = 0x21, | |
6d0205fd | 329 | .batch_en = BIT(3), |
c91c1c84 | 330 | } |
801a6e0a | 331 | }, |
3054c4ff LB |
332 | { |
333 | .wai = 0x6b, | |
334 | .max_fifo_size = 512, | |
335 | .id = { | |
336 | [0] = ST_ASM330LHH_ID, | |
337 | }, | |
338 | .batch = { | |
339 | [ST_LSM6DSX_ID_ACC] = { | |
340 | .addr = 0x09, | |
341 | .mask = GENMASK(3, 0), | |
342 | }, | |
343 | [ST_LSM6DSX_ID_GYRO] = { | |
344 | .addr = 0x09, | |
345 | .mask = GENMASK(7, 4), | |
346 | }, | |
347 | }, | |
348 | .fifo_ops = { | |
349 | .read_fifo = st_lsm6dsx_read_tagged_fifo, | |
350 | .fifo_th = { | |
351 | .addr = 0x07, | |
352 | .mask = GENMASK(8, 0), | |
353 | }, | |
354 | .fifo_diff = { | |
355 | .addr = 0x3a, | |
356 | .mask = GENMASK(8, 0), | |
357 | }, | |
358 | .th_wl = 1, | |
359 | }, | |
360 | .ts_settings = { | |
361 | .timer_en = { | |
362 | .addr = 0x19, | |
363 | .mask = BIT(5), | |
364 | }, | |
365 | .decimator = { | |
366 | .addr = 0x0a, | |
367 | .mask = GENMASK(7, 6), | |
368 | }, | |
369 | }, | |
370 | }, | |
43901008 LB |
371 | { |
372 | .wai = 0x6b, | |
373 | .max_fifo_size = 512, | |
374 | .id = { | |
375 | [0] = ST_LSM6DSR_ID, | |
376 | }, | |
377 | .batch = { | |
378 | [ST_LSM6DSX_ID_ACC] = { | |
379 | .addr = 0x09, | |
380 | .mask = GENMASK(3, 0), | |
381 | }, | |
382 | [ST_LSM6DSX_ID_GYRO] = { | |
383 | .addr = 0x09, | |
384 | .mask = GENMASK(7, 4), | |
385 | }, | |
386 | }, | |
387 | .fifo_ops = { | |
388 | .read_fifo = st_lsm6dsx_read_tagged_fifo, | |
389 | .fifo_th = { | |
390 | .addr = 0x07, | |
391 | .mask = GENMASK(8, 0), | |
392 | }, | |
393 | .fifo_diff = { | |
394 | .addr = 0x3a, | |
395 | .mask = GENMASK(8, 0), | |
396 | }, | |
397 | .th_wl = 1, | |
398 | }, | |
399 | .ts_settings = { | |
400 | .timer_en = { | |
401 | .addr = 0x19, | |
402 | .mask = BIT(5), | |
403 | }, | |
404 | .decimator = { | |
405 | .addr = 0x0a, | |
406 | .mask = GENMASK(7, 6), | |
407 | }, | |
408 | }, | |
409 | .shub_settings = { | |
410 | .page_mux = { | |
411 | .addr = 0x01, | |
412 | .mask = BIT(6), | |
413 | }, | |
414 | .master_en = { | |
415 | .addr = 0x14, | |
416 | .mask = BIT(2), | |
417 | }, | |
418 | .pullup_en = { | |
419 | .addr = 0x14, | |
420 | .mask = BIT(3), | |
421 | }, | |
422 | .aux_sens = { | |
423 | .addr = 0x14, | |
424 | .mask = GENMASK(1, 0), | |
425 | }, | |
426 | .wr_once = { | |
427 | .addr = 0x14, | |
428 | .mask = BIT(6), | |
429 | }, | |
430 | .shub_out = 0x02, | |
431 | .slv0_addr = 0x15, | |
432 | .dw_slv0_addr = 0x21, | |
433 | .batch_en = BIT(3), | |
434 | } | |
435 | }, | |
290a6ce1 LB |
436 | }; |
437 | ||
290a6ce1 LB |
438 | static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = { |
439 | ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR, | |
440 | IIO_MOD_X, 0), | |
441 | ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR, | |
442 | IIO_MOD_Y, 1), | |
443 | ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR, | |
444 | IIO_MOD_Z, 2), | |
445 | IIO_CHAN_SOFT_TIMESTAMP(3), | |
446 | }; | |
447 | ||
448 | static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = { | |
449 | ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR, | |
450 | IIO_MOD_X, 0), | |
451 | ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR, | |
452 | IIO_MOD_Y, 1), | |
453 | ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR, | |
454 | IIO_MOD_Z, 2), | |
455 | IIO_CHAN_SOFT_TIMESTAMP(3), | |
456 | }; | |
457 | ||
c91c1c84 LB |
458 | int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable) |
459 | { | |
460 | const struct st_lsm6dsx_shub_settings *hub_settings; | |
461 | unsigned int data; | |
462 | int err; | |
463 | ||
464 | hub_settings = &hw->settings->shub_settings; | |
465 | data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask); | |
466 | err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr, | |
467 | hub_settings->page_mux.mask, data); | |
468 | usleep_range(100, 150); | |
469 | ||
470 | return err; | |
471 | } | |
472 | ||
290a6ce1 LB |
473 | static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id) |
474 | { | |
51a8b707 | 475 | int err, i, j, data; |
290a6ce1 LB |
476 | |
477 | for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) { | |
d068e4a0 LB |
478 | for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) { |
479 | if (id == st_lsm6dsx_sensor_settings[i].id[j]) | |
480 | break; | |
481 | } | |
482 | if (j < ST_LSM6DSX_MAX_ID) | |
290a6ce1 LB |
483 | break; |
484 | } | |
485 | ||
486 | if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) { | |
487 | dev_err(hw->dev, "unsupported hw id [%02x]\n", id); | |
488 | return -ENODEV; | |
489 | } | |
490 | ||
51a8b707 | 491 | err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data); |
290a6ce1 LB |
492 | if (err < 0) { |
493 | dev_err(hw->dev, "failed to read whoami register\n"); | |
494 | return err; | |
495 | } | |
496 | ||
497 | if (data != st_lsm6dsx_sensor_settings[i].wai) { | |
498 | dev_err(hw->dev, "unsupported whoami [%02x]\n", data); | |
499 | return -ENODEV; | |
500 | } | |
501 | ||
502 | hw->settings = &st_lsm6dsx_sensor_settings[i]; | |
503 | ||
504 | return 0; | |
505 | } | |
506 | ||
507 | static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor, | |
508 | u32 gain) | |
509 | { | |
51a8b707 LB |
510 | struct st_lsm6dsx_hw *hw = sensor->hw; |
511 | const struct st_lsm6dsx_reg *reg; | |
739aff87 | 512 | unsigned int data; |
290a6ce1 LB |
513 | int i, err; |
514 | u8 val; | |
515 | ||
516 | for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++) | |
51a8b707 | 517 | if (st_lsm6dsx_fs_table[sensor->id].fs_avl[i].gain == gain) |
290a6ce1 LB |
518 | break; |
519 | ||
520 | if (i == ST_LSM6DSX_FS_LIST_SIZE) | |
521 | return -EINVAL; | |
522 | ||
51a8b707 LB |
523 | val = st_lsm6dsx_fs_table[sensor->id].fs_avl[i].val; |
524 | reg = &st_lsm6dsx_fs_table[sensor->id].reg; | |
739aff87 LB |
525 | data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask); |
526 | err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data); | |
290a6ce1 LB |
527 | if (err < 0) |
528 | return err; | |
529 | ||
530 | sensor->gain = gain; | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
54a6d0c6 | 535 | int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val) |
290a6ce1 | 536 | { |
2ccc1503 | 537 | int i; |
290a6ce1 LB |
538 | |
539 | for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) | |
6ffb55e5 LB |
540 | /* |
541 | * ext devices can run at different odr respect to | |
542 | * accel sensor | |
543 | */ | |
544 | if (st_lsm6dsx_odr_table[sensor->id].odr_avl[i].hz >= odr) | |
290a6ce1 LB |
545 | break; |
546 | ||
547 | if (i == ST_LSM6DSX_ODR_LIST_SIZE) | |
548 | return -EINVAL; | |
549 | ||
2ccc1503 | 550 | *val = st_lsm6dsx_odr_table[sensor->id].odr_avl[i].val; |
290a6ce1 LB |
551 | |
552 | return 0; | |
553 | } | |
554 | ||
6ffb55e5 LB |
555 | static u16 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u16 odr, |
556 | enum st_lsm6dsx_sensor_id id) | |
2ccc1503 | 557 | { |
6ffb55e5 LB |
558 | struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]); |
559 | ||
560 | if (odr > 0) { | |
561 | if (hw->enable_mask & BIT(id)) | |
562 | return max_t(u16, ref->odr, odr); | |
563 | else | |
564 | return odr; | |
565 | } else { | |
566 | return (hw->enable_mask & BIT(id)) ? ref->odr : 0; | |
567 | } | |
568 | } | |
569 | ||
570 | static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 req_odr) | |
571 | { | |
572 | struct st_lsm6dsx_sensor *ref_sensor = sensor; | |
51a8b707 LB |
573 | struct st_lsm6dsx_hw *hw = sensor->hw; |
574 | const struct st_lsm6dsx_reg *reg; | |
739aff87 | 575 | unsigned int data; |
6ffb55e5 | 576 | u8 val = 0; |
2ccc1503 | 577 | int err; |
2ccc1503 | 578 | |
6ffb55e5 LB |
579 | switch (sensor->id) { |
580 | case ST_LSM6DSX_ID_EXT0: | |
581 | case ST_LSM6DSX_ID_EXT1: | |
582 | case ST_LSM6DSX_ID_EXT2: | |
583 | case ST_LSM6DSX_ID_ACC: { | |
584 | u16 odr; | |
585 | int i; | |
586 | ||
587 | /* | |
588 | * i2c embedded controller relies on the accelerometer sensor as | |
589 | * bus read/write trigger so we need to enable accel device | |
590 | * at odr = max(accel_odr, ext_odr) in order to properly | |
591 | * communicate with i2c slave devices | |
592 | */ | |
593 | ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); | |
594 | for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) { | |
595 | if (!hw->iio_devs[i] || i == sensor->id) | |
596 | continue; | |
597 | ||
598 | odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i); | |
599 | if (odr != req_odr) | |
600 | /* device already configured */ | |
601 | return 0; | |
602 | } | |
603 | break; | |
604 | } | |
605 | default: | |
606 | break; | |
607 | } | |
2ccc1503 | 608 | |
6ffb55e5 LB |
609 | if (req_odr > 0) { |
610 | err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val); | |
611 | if (err < 0) | |
612 | return err; | |
613 | } | |
614 | ||
615 | reg = &st_lsm6dsx_odr_table[ref_sensor->id].reg; | |
739aff87 LB |
616 | data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask); |
617 | return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data); | |
2ccc1503 LB |
618 | } |
619 | ||
17750443 LB |
620 | int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor, |
621 | bool enable) | |
290a6ce1 | 622 | { |
51a8b707 | 623 | struct st_lsm6dsx_hw *hw = sensor->hw; |
17750443 | 624 | u16 odr = enable ? sensor->odr : 0; |
290a6ce1 LB |
625 | int err; |
626 | ||
17750443 | 627 | err = st_lsm6dsx_set_odr(sensor, odr); |
290a6ce1 LB |
628 | if (err < 0) |
629 | return err; | |
630 | ||
17750443 LB |
631 | if (enable) |
632 | hw->enable_mask |= BIT(sensor->id); | |
633 | else | |
634 | hw->enable_mask &= ~BIT(sensor->id); | |
290a6ce1 LB |
635 | |
636 | return 0; | |
637 | } | |
638 | ||
639 | static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor, | |
640 | u8 addr, int *val) | |
641 | { | |
51a8b707 | 642 | struct st_lsm6dsx_hw *hw = sensor->hw; |
290a6ce1 LB |
643 | int err, delay; |
644 | __le16 data; | |
645 | ||
17750443 | 646 | err = st_lsm6dsx_sensor_set_enable(sensor, true); |
290a6ce1 LB |
647 | if (err < 0) |
648 | return err; | |
649 | ||
650 | delay = 1000000 / sensor->odr; | |
651 | usleep_range(delay, 2 * delay); | |
652 | ||
739aff87 | 653 | err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data)); |
290a6ce1 LB |
654 | if (err < 0) |
655 | return err; | |
656 | ||
17750443 | 657 | st_lsm6dsx_sensor_set_enable(sensor, false); |
290a6ce1 | 658 | |
7b9ebe42 | 659 | *val = (s16)le16_to_cpu(data); |
290a6ce1 LB |
660 | |
661 | return IIO_VAL_INT; | |
662 | } | |
663 | ||
664 | static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev, | |
665 | struct iio_chan_spec const *ch, | |
666 | int *val, int *val2, long mask) | |
667 | { | |
668 | struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); | |
669 | int ret; | |
670 | ||
671 | switch (mask) { | |
672 | case IIO_CHAN_INFO_RAW: | |
673 | ret = iio_device_claim_direct_mode(iio_dev); | |
674 | if (ret) | |
675 | break; | |
676 | ||
677 | ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val); | |
678 | iio_device_release_direct_mode(iio_dev); | |
679 | break; | |
680 | case IIO_CHAN_INFO_SAMP_FREQ: | |
681 | *val = sensor->odr; | |
682 | ret = IIO_VAL_INT; | |
683 | break; | |
684 | case IIO_CHAN_INFO_SCALE: | |
685 | *val = 0; | |
686 | *val2 = sensor->gain; | |
687 | ret = IIO_VAL_INT_PLUS_MICRO; | |
688 | break; | |
689 | default: | |
690 | ret = -EINVAL; | |
691 | break; | |
692 | } | |
693 | ||
694 | return ret; | |
695 | } | |
696 | ||
697 | static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev, | |
698 | struct iio_chan_spec const *chan, | |
699 | int val, int val2, long mask) | |
700 | { | |
701 | struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); | |
702 | int err; | |
703 | ||
704 | err = iio_device_claim_direct_mode(iio_dev); | |
705 | if (err) | |
706 | return err; | |
707 | ||
708 | switch (mask) { | |
709 | case IIO_CHAN_INFO_SCALE: | |
710 | err = st_lsm6dsx_set_full_scale(sensor, val2); | |
711 | break; | |
2ccc1503 LB |
712 | case IIO_CHAN_INFO_SAMP_FREQ: { |
713 | u8 data; | |
714 | ||
715 | err = st_lsm6dsx_check_odr(sensor, val, &data); | |
5e3c3e33 LB |
716 | if (!err) |
717 | sensor->odr = val; | |
290a6ce1 | 718 | break; |
2ccc1503 | 719 | } |
290a6ce1 LB |
720 | default: |
721 | err = -EINVAL; | |
722 | break; | |
723 | } | |
724 | ||
725 | iio_device_release_direct_mode(iio_dev); | |
726 | ||
727 | return err; | |
728 | } | |
729 | ||
d40464f3 | 730 | int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val) |
290a6ce1 LB |
731 | { |
732 | struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); | |
733 | struct st_lsm6dsx_hw *hw = sensor->hw; | |
8f2a88a2 | 734 | int err; |
290a6ce1 | 735 | |
8f2a88a2 | 736 | if (val < 1 || val > hw->settings->max_fifo_size) |
290a6ce1 LB |
737 | return -EINVAL; |
738 | ||
335eaedc LB |
739 | mutex_lock(&hw->conf_lock); |
740 | ||
290a6ce1 | 741 | err = st_lsm6dsx_update_watermark(sensor, val); |
335eaedc LB |
742 | |
743 | mutex_unlock(&hw->conf_lock); | |
744 | ||
290a6ce1 LB |
745 | if (err < 0) |
746 | return err; | |
747 | ||
748 | sensor->watermark = val; | |
749 | ||
750 | return 0; | |
751 | } | |
752 | ||
753 | static ssize_t | |
754 | st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev, | |
755 | struct device_attribute *attr, | |
756 | char *buf) | |
757 | { | |
758 | struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev)); | |
759 | enum st_lsm6dsx_sensor_id id = sensor->id; | |
760 | int i, len = 0; | |
761 | ||
762 | for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) | |
763 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", | |
764 | st_lsm6dsx_odr_table[id].odr_avl[i].hz); | |
765 | buf[len - 1] = '\n'; | |
766 | ||
767 | return len; | |
768 | } | |
769 | ||
770 | static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev, | |
771 | struct device_attribute *attr, | |
772 | char *buf) | |
773 | { | |
774 | struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev)); | |
775 | enum st_lsm6dsx_sensor_id id = sensor->id; | |
776 | int i, len = 0; | |
777 | ||
778 | for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++) | |
779 | len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ", | |
780 | st_lsm6dsx_fs_table[id].fs_avl[i].gain); | |
781 | buf[len - 1] = '\n'; | |
782 | ||
783 | return len; | |
784 | } | |
785 | ||
786 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail); | |
787 | static IIO_DEVICE_ATTR(in_accel_scale_available, 0444, | |
788 | st_lsm6dsx_sysfs_scale_avail, NULL, 0); | |
789 | static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444, | |
790 | st_lsm6dsx_sysfs_scale_avail, NULL, 0); | |
791 | ||
792 | static struct attribute *st_lsm6dsx_acc_attributes[] = { | |
793 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | |
794 | &iio_dev_attr_in_accel_scale_available.dev_attr.attr, | |
795 | NULL, | |
796 | }; | |
797 | ||
798 | static const struct attribute_group st_lsm6dsx_acc_attribute_group = { | |
799 | .attrs = st_lsm6dsx_acc_attributes, | |
800 | }; | |
801 | ||
802 | static const struct iio_info st_lsm6dsx_acc_info = { | |
290a6ce1 LB |
803 | .attrs = &st_lsm6dsx_acc_attribute_group, |
804 | .read_raw = st_lsm6dsx_read_raw, | |
805 | .write_raw = st_lsm6dsx_write_raw, | |
806 | .hwfifo_set_watermark = st_lsm6dsx_set_watermark, | |
807 | }; | |
808 | ||
809 | static struct attribute *st_lsm6dsx_gyro_attributes[] = { | |
810 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, | |
811 | &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr, | |
812 | NULL, | |
813 | }; | |
814 | ||
815 | static const struct attribute_group st_lsm6dsx_gyro_attribute_group = { | |
816 | .attrs = st_lsm6dsx_gyro_attributes, | |
817 | }; | |
818 | ||
819 | static const struct iio_info st_lsm6dsx_gyro_info = { | |
290a6ce1 LB |
820 | .attrs = &st_lsm6dsx_gyro_attribute_group, |
821 | .read_raw = st_lsm6dsx_read_raw, | |
822 | .write_raw = st_lsm6dsx_write_raw, | |
823 | .hwfifo_set_watermark = st_lsm6dsx_set_watermark, | |
824 | }; | |
825 | ||
dba32904 LB |
826 | static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin) |
827 | { | |
828 | struct device_node *np = hw->dev->of_node; | |
dba32904 LB |
829 | |
830 | if (!np) | |
831 | return -EINVAL; | |
832 | ||
bf235277 | 833 | return of_property_read_u32(np, "st,drdy-int-pin", drdy_pin); |
dba32904 LB |
834 | } |
835 | ||
836 | static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw, u8 *drdy_reg) | |
837 | { | |
838 | int err = 0, drdy_pin; | |
839 | ||
840 | if (st_lsm6dsx_of_get_drdy_pin(hw, &drdy_pin) < 0) { | |
841 | struct st_sensors_platform_data *pdata; | |
842 | struct device *dev = hw->dev; | |
843 | ||
844 | pdata = (struct st_sensors_platform_data *)dev->platform_data; | |
845 | drdy_pin = pdata ? pdata->drdy_int_pin : 1; | |
846 | } | |
847 | ||
848 | switch (drdy_pin) { | |
849 | case 1: | |
850 | *drdy_reg = ST_LSM6DSX_REG_INT1_ADDR; | |
851 | break; | |
852 | case 2: | |
853 | *drdy_reg = ST_LSM6DSX_REG_INT2_ADDR; | |
854 | break; | |
855 | default: | |
856 | dev_err(hw->dev, "unsupported data ready pin\n"); | |
857 | err = -EINVAL; | |
858 | break; | |
859 | } | |
860 | ||
861 | return err; | |
862 | } | |
863 | ||
c91c1c84 LB |
864 | static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw) |
865 | { | |
866 | const struct st_lsm6dsx_shub_settings *hub_settings; | |
867 | struct device_node *np = hw->dev->of_node; | |
868 | struct st_sensors_platform_data *pdata; | |
869 | unsigned int data; | |
870 | int err = 0; | |
871 | ||
872 | hub_settings = &hw->settings->shub_settings; | |
873 | ||
874 | pdata = (struct st_sensors_platform_data *)hw->dev->platform_data; | |
875 | if ((np && of_property_read_bool(np, "st,pullups")) || | |
876 | (pdata && pdata->pullups)) { | |
877 | err = st_lsm6dsx_set_page(hw, true); | |
878 | if (err < 0) | |
879 | return err; | |
880 | ||
881 | data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask); | |
882 | err = regmap_update_bits(hw->regmap, | |
883 | hub_settings->pullup_en.addr, | |
884 | hub_settings->pullup_en.mask, data); | |
885 | ||
886 | st_lsm6dsx_set_page(hw, false); | |
887 | ||
888 | if (err < 0) | |
889 | return err; | |
890 | } | |
891 | ||
892 | if (hub_settings->aux_sens.addr) { | |
893 | /* configure aux sensors */ | |
894 | err = st_lsm6dsx_set_page(hw, true); | |
895 | if (err < 0) | |
896 | return err; | |
897 | ||
898 | data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask); | |
899 | err = regmap_update_bits(hw->regmap, | |
900 | hub_settings->aux_sens.addr, | |
901 | hub_settings->aux_sens.mask, data); | |
902 | ||
903 | st_lsm6dsx_set_page(hw, false); | |
904 | } | |
905 | ||
906 | return err; | |
907 | } | |
908 | ||
21345107 LB |
909 | static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw) |
910 | { | |
911 | const struct st_lsm6dsx_hw_ts_settings *ts_settings; | |
912 | int err, val; | |
913 | ||
914 | ts_settings = &hw->settings->ts_settings; | |
915 | /* enable hw timestamp generation if necessary */ | |
916 | if (ts_settings->timer_en.addr) { | |
917 | val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask); | |
918 | err = regmap_update_bits(hw->regmap, | |
919 | ts_settings->timer_en.addr, | |
920 | ts_settings->timer_en.mask, val); | |
921 | if (err < 0) | |
922 | return err; | |
923 | } | |
924 | ||
925 | /* enable high resolution for hw ts timer if necessary */ | |
926 | if (ts_settings->hr_timer.addr) { | |
927 | val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask); | |
928 | err = regmap_update_bits(hw->regmap, | |
929 | ts_settings->hr_timer.addr, | |
930 | ts_settings->hr_timer.mask, val); | |
931 | if (err < 0) | |
932 | return err; | |
933 | } | |
934 | ||
935 | /* enable ts queueing in FIFO if necessary */ | |
936 | if (ts_settings->fifo_en.addr) { | |
937 | val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask); | |
938 | err = regmap_update_bits(hw->regmap, | |
939 | ts_settings->fifo_en.addr, | |
940 | ts_settings->fifo_en.mask, val); | |
941 | if (err < 0) | |
942 | return err; | |
943 | } | |
944 | return 0; | |
945 | } | |
946 | ||
290a6ce1 LB |
947 | static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw) |
948 | { | |
51a8b707 | 949 | u8 drdy_int_reg; |
290a6ce1 | 950 | int err; |
290a6ce1 | 951 | |
19435425 LB |
952 | /* device sw reset */ |
953 | err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR, | |
954 | ST_LSM6DSX_REG_RESET_MASK, | |
955 | FIELD_PREP(ST_LSM6DSX_REG_RESET_MASK, 1)); | |
290a6ce1 LB |
956 | if (err < 0) |
957 | return err; | |
958 | ||
19435425 LB |
959 | msleep(50); |
960 | ||
961 | /* reload trimming parameter */ | |
962 | err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR, | |
963 | ST_LSM6DSX_REG_BOOT_MASK, | |
964 | FIELD_PREP(ST_LSM6DSX_REG_BOOT_MASK, 1)); | |
965 | if (err < 0) | |
966 | return err; | |
967 | ||
968 | msleep(50); | |
290a6ce1 | 969 | |
290a6ce1 | 970 | /* enable Block Data Update */ |
51a8b707 LB |
971 | err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_BDU_ADDR, |
972 | ST_LSM6DSX_REG_BDU_MASK, | |
973 | FIELD_PREP(ST_LSM6DSX_REG_BDU_MASK, 1)); | |
290a6ce1 LB |
974 | if (err < 0) |
975 | return err; | |
976 | ||
290a6ce1 | 977 | /* enable FIFO watermak interrupt */ |
dba32904 | 978 | err = st_lsm6dsx_get_drdy_reg(hw, &drdy_int_reg); |
290a6ce1 LB |
979 | if (err < 0) |
980 | return err; | |
981 | ||
21345107 LB |
982 | err = regmap_update_bits(hw->regmap, drdy_int_reg, |
983 | ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK, | |
984 | FIELD_PREP(ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK, | |
985 | 1)); | |
986 | if (err < 0) | |
987 | return err; | |
988 | ||
c91c1c84 LB |
989 | err = st_lsm6dsx_init_shub(hw); |
990 | if (err < 0) | |
991 | return err; | |
992 | ||
21345107 | 993 | return st_lsm6dsx_init_hw_timer(hw); |
290a6ce1 LB |
994 | } |
995 | ||
996 | static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw, | |
510c0106 LB |
997 | enum st_lsm6dsx_sensor_id id, |
998 | const char *name) | |
290a6ce1 LB |
999 | { |
1000 | struct st_lsm6dsx_sensor *sensor; | |
1001 | struct iio_dev *iio_dev; | |
1002 | ||
1003 | iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor)); | |
1004 | if (!iio_dev) | |
1005 | return NULL; | |
1006 | ||
1007 | iio_dev->modes = INDIO_DIRECT_MODE; | |
1008 | iio_dev->dev.parent = hw->dev; | |
1009 | iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks; | |
1010 | ||
1011 | sensor = iio_priv(iio_dev); | |
1012 | sensor->id = id; | |
1013 | sensor->hw = hw; | |
1014 | sensor->odr = st_lsm6dsx_odr_table[id].odr_avl[0].hz; | |
1015 | sensor->gain = st_lsm6dsx_fs_table[id].fs_avl[0].gain; | |
1016 | sensor->watermark = 1; | |
1017 | ||
1018 | switch (id) { | |
1019 | case ST_LSM6DSX_ID_ACC: | |
1020 | iio_dev->channels = st_lsm6dsx_acc_channels; | |
1021 | iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_acc_channels); | |
290a6ce1 LB |
1022 | iio_dev->info = &st_lsm6dsx_acc_info; |
1023 | ||
510c0106 LB |
1024 | scnprintf(sensor->name, sizeof(sensor->name), "%s_accel", |
1025 | name); | |
290a6ce1 LB |
1026 | break; |
1027 | case ST_LSM6DSX_ID_GYRO: | |
1028 | iio_dev->channels = st_lsm6dsx_gyro_channels; | |
1029 | iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_gyro_channels); | |
290a6ce1 LB |
1030 | iio_dev->info = &st_lsm6dsx_gyro_info; |
1031 | ||
510c0106 LB |
1032 | scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro", |
1033 | name); | |
290a6ce1 LB |
1034 | break; |
1035 | default: | |
1036 | return NULL; | |
1037 | } | |
510c0106 | 1038 | iio_dev->name = sensor->name; |
290a6ce1 LB |
1039 | |
1040 | return iio_dev; | |
1041 | } | |
1042 | ||
510c0106 | 1043 | int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name, |
51a8b707 | 1044 | struct regmap *regmap) |
290a6ce1 | 1045 | { |
c91c1c84 | 1046 | const struct st_lsm6dsx_shub_settings *hub_settings; |
290a6ce1 LB |
1047 | struct st_lsm6dsx_hw *hw; |
1048 | int i, err; | |
1049 | ||
1050 | hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL); | |
1051 | if (!hw) | |
1052 | return -ENOMEM; | |
1053 | ||
1054 | dev_set_drvdata(dev, (void *)hw); | |
1055 | ||
290a6ce1 | 1056 | mutex_init(&hw->fifo_lock); |
335eaedc | 1057 | mutex_init(&hw->conf_lock); |
739aff87 | 1058 | mutex_init(&hw->page_lock); |
290a6ce1 | 1059 | |
91a6b841 LB |
1060 | hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL); |
1061 | if (!hw->buff) | |
1062 | return -ENOMEM; | |
1063 | ||
290a6ce1 LB |
1064 | hw->dev = dev; |
1065 | hw->irq = irq; | |
51a8b707 | 1066 | hw->regmap = regmap; |
290a6ce1 LB |
1067 | |
1068 | err = st_lsm6dsx_check_whoami(hw, hw_id); | |
1069 | if (err < 0) | |
1070 | return err; | |
1071 | ||
6ffb55e5 | 1072 | for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) { |
510c0106 | 1073 | hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name); |
290a6ce1 LB |
1074 | if (!hw->iio_devs[i]) |
1075 | return -ENOMEM; | |
1076 | } | |
1077 | ||
1078 | err = st_lsm6dsx_init_device(hw); | |
1079 | if (err < 0) | |
1080 | return err; | |
1081 | ||
c91c1c84 LB |
1082 | hub_settings = &hw->settings->shub_settings; |
1083 | if (hub_settings->master_en.addr) { | |
1084 | err = st_lsm6dsx_shub_probe(hw, name); | |
1085 | if (err < 0) | |
1086 | return err; | |
1087 | } | |
1088 | ||
290a6ce1 LB |
1089 | if (hw->irq > 0) { |
1090 | err = st_lsm6dsx_fifo_setup(hw); | |
1091 | if (err < 0) | |
1092 | return err; | |
1093 | } | |
1094 | ||
1095 | for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { | |
6ffb55e5 LB |
1096 | if (!hw->iio_devs[i]) |
1097 | continue; | |
1098 | ||
290a6ce1 LB |
1099 | err = devm_iio_device_register(hw->dev, hw->iio_devs[i]); |
1100 | if (err) | |
1101 | return err; | |
1102 | } | |
1103 | ||
1104 | return 0; | |
1105 | } | |
1106 | EXPORT_SYMBOL(st_lsm6dsx_probe); | |
1107 | ||
3cec4850 | 1108 | static int __maybe_unused st_lsm6dsx_suspend(struct device *dev) |
d3f77058 LB |
1109 | { |
1110 | struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev); | |
1111 | struct st_lsm6dsx_sensor *sensor; | |
51a8b707 | 1112 | const struct st_lsm6dsx_reg *reg; |
739aff87 | 1113 | unsigned int data; |
d3f77058 LB |
1114 | int i, err = 0; |
1115 | ||
1116 | for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { | |
6ffb55e5 LB |
1117 | if (!hw->iio_devs[i]) |
1118 | continue; | |
1119 | ||
d3f77058 LB |
1120 | sensor = iio_priv(hw->iio_devs[i]); |
1121 | if (!(hw->enable_mask & BIT(sensor->id))) | |
1122 | continue; | |
1123 | ||
51a8b707 | 1124 | reg = &st_lsm6dsx_odr_table[sensor->id].reg; |
739aff87 LB |
1125 | data = ST_LSM6DSX_SHIFT_VAL(0, reg->mask); |
1126 | err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, | |
1127 | data); | |
d3f77058 LB |
1128 | if (err < 0) |
1129 | return err; | |
1130 | } | |
1131 | ||
1132 | if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS) | |
1133 | err = st_lsm6dsx_flush_fifo(hw); | |
1134 | ||
1135 | return err; | |
1136 | } | |
1137 | ||
3cec4850 | 1138 | static int __maybe_unused st_lsm6dsx_resume(struct device *dev) |
d3f77058 LB |
1139 | { |
1140 | struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev); | |
1141 | struct st_lsm6dsx_sensor *sensor; | |
1142 | int i, err = 0; | |
1143 | ||
1144 | for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { | |
6ffb55e5 LB |
1145 | if (!hw->iio_devs[i]) |
1146 | continue; | |
1147 | ||
d3f77058 LB |
1148 | sensor = iio_priv(hw->iio_devs[i]); |
1149 | if (!(hw->enable_mask & BIT(sensor->id))) | |
1150 | continue; | |
1151 | ||
1152 | err = st_lsm6dsx_set_odr(sensor, sensor->odr); | |
1153 | if (err < 0) | |
1154 | return err; | |
1155 | } | |
1156 | ||
1157 | if (hw->enable_mask) | |
1158 | err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT); | |
1159 | ||
1160 | return err; | |
1161 | } | |
d3f77058 LB |
1162 | |
1163 | const struct dev_pm_ops st_lsm6dsx_pm_ops = { | |
1164 | SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume) | |
1165 | }; | |
1166 | EXPORT_SYMBOL(st_lsm6dsx_pm_ops); | |
1167 | ||
290a6ce1 LB |
1168 | MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>"); |
1169 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); | |
1170 | MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver"); | |
1171 | MODULE_LICENSE("GPL v2"); |