1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Invensense, Inc.
6 #include <linux/module.h>
7 #include <linux/slab.h>
10 #include <linux/delay.h>
11 #include <linux/sysfs.h>
12 #include <linux/jiffies.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
15 #include <linux/acpi.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/math64.h>
19 #include <linux/minmax.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/property.h>
24 #include <linux/iio/common/inv_sensors_timestamp.h>
25 #include <linux/iio/iio.h>
27 #include "inv_mpu_iio.h"
28 #include "inv_mpu_magn.h"
31 * this is the gyro scale translated from dynamic range plus/minus
32 * {250, 500, 1000, 2000} to rad/s
34 static const int gyro_scale_6050
[] = {133090, 266181, 532362, 1064724};
37 * this is the accel scale translated from dynamic range plus/minus
38 * {2, 4, 8, 16} to m/s^2
40 static const int accel_scale
[] = {598, 1196, 2392, 4785};
42 static const struct inv_mpu6050_reg_map reg_set_icm20602
= {
43 .sample_rate_div
= INV_MPU6050_REG_SAMPLE_RATE_DIV
,
44 .lpf
= INV_MPU6050_REG_CONFIG
,
45 .accel_lpf
= INV_MPU6500_REG_ACCEL_CONFIG_2
,
46 .user_ctrl
= INV_MPU6050_REG_USER_CTRL
,
47 .fifo_en
= INV_MPU6050_REG_FIFO_EN
,
48 .gyro_config
= INV_MPU6050_REG_GYRO_CONFIG
,
49 .accl_config
= INV_MPU6050_REG_ACCEL_CONFIG
,
50 .fifo_count_h
= INV_MPU6050_REG_FIFO_COUNT_H
,
51 .fifo_r_w
= INV_MPU6050_REG_FIFO_R_W
,
52 .raw_gyro
= INV_MPU6050_REG_RAW_GYRO
,
53 .raw_accl
= INV_MPU6050_REG_RAW_ACCEL
,
54 .temperature
= INV_MPU6050_REG_TEMPERATURE
,
55 .int_enable
= INV_MPU6050_REG_INT_ENABLE
,
56 .int_status
= INV_MPU6050_REG_INT_STATUS
,
57 .pwr_mgmt_1
= INV_MPU6050_REG_PWR_MGMT_1
,
58 .pwr_mgmt_2
= INV_MPU6050_REG_PWR_MGMT_2
,
59 .int_pin_cfg
= INV_MPU6050_REG_INT_PIN_CFG
,
60 .accl_offset
= INV_MPU6500_REG_ACCEL_OFFSET
,
61 .gyro_offset
= INV_MPU6050_REG_GYRO_OFFSET
,
62 .i2c_if
= INV_ICM20602_REG_I2C_IF
,
65 static const struct inv_mpu6050_reg_map reg_set_6500
= {
66 .sample_rate_div
= INV_MPU6050_REG_SAMPLE_RATE_DIV
,
67 .lpf
= INV_MPU6050_REG_CONFIG
,
68 .accel_lpf
= INV_MPU6500_REG_ACCEL_CONFIG_2
,
69 .user_ctrl
= INV_MPU6050_REG_USER_CTRL
,
70 .fifo_en
= INV_MPU6050_REG_FIFO_EN
,
71 .gyro_config
= INV_MPU6050_REG_GYRO_CONFIG
,
72 .accl_config
= INV_MPU6050_REG_ACCEL_CONFIG
,
73 .fifo_count_h
= INV_MPU6050_REG_FIFO_COUNT_H
,
74 .fifo_r_w
= INV_MPU6050_REG_FIFO_R_W
,
75 .raw_gyro
= INV_MPU6050_REG_RAW_GYRO
,
76 .raw_accl
= INV_MPU6050_REG_RAW_ACCEL
,
77 .temperature
= INV_MPU6050_REG_TEMPERATURE
,
78 .int_enable
= INV_MPU6050_REG_INT_ENABLE
,
79 .int_status
= INV_MPU6050_REG_INT_STATUS
,
80 .pwr_mgmt_1
= INV_MPU6050_REG_PWR_MGMT_1
,
81 .pwr_mgmt_2
= INV_MPU6050_REG_PWR_MGMT_2
,
82 .int_pin_cfg
= INV_MPU6050_REG_INT_PIN_CFG
,
83 .accl_offset
= INV_MPU6500_REG_ACCEL_OFFSET
,
84 .gyro_offset
= INV_MPU6050_REG_GYRO_OFFSET
,
88 static const struct inv_mpu6050_reg_map reg_set_6050
= {
89 .sample_rate_div
= INV_MPU6050_REG_SAMPLE_RATE_DIV
,
90 .lpf
= INV_MPU6050_REG_CONFIG
,
91 .user_ctrl
= INV_MPU6050_REG_USER_CTRL
,
92 .fifo_en
= INV_MPU6050_REG_FIFO_EN
,
93 .gyro_config
= INV_MPU6050_REG_GYRO_CONFIG
,
94 .accl_config
= INV_MPU6050_REG_ACCEL_CONFIG
,
95 .fifo_count_h
= INV_MPU6050_REG_FIFO_COUNT_H
,
96 .fifo_r_w
= INV_MPU6050_REG_FIFO_R_W
,
97 .raw_gyro
= INV_MPU6050_REG_RAW_GYRO
,
98 .raw_accl
= INV_MPU6050_REG_RAW_ACCEL
,
99 .temperature
= INV_MPU6050_REG_TEMPERATURE
,
100 .int_enable
= INV_MPU6050_REG_INT_ENABLE
,
101 .pwr_mgmt_1
= INV_MPU6050_REG_PWR_MGMT_1
,
102 .pwr_mgmt_2
= INV_MPU6050_REG_PWR_MGMT_2
,
103 .int_pin_cfg
= INV_MPU6050_REG_INT_PIN_CFG
,
104 .accl_offset
= INV_MPU6050_REG_ACCEL_OFFSET
,
105 .gyro_offset
= INV_MPU6050_REG_GYRO_OFFSET
,
109 static const struct inv_mpu6050_chip_config chip_config_6050
= {
110 .clk
= INV_CLK_INTERNAL
,
111 .fsr
= INV_MPU6050_FSR_2000DPS
,
112 .lpf
= INV_MPU6050_FILTER_20HZ
,
113 .divider
= INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
118 .gyro_fifo_enable
= false,
119 .accl_fifo_enable
= false,
120 .temp_fifo_enable
= false,
121 .magn_fifo_enable
= false,
122 .accl_fs
= INV_MPU6050_FS_02G
,
126 static const struct inv_mpu6050_chip_config chip_config_6500
= {
128 .fsr
= INV_MPU6050_FSR_2000DPS
,
129 .lpf
= INV_MPU6050_FILTER_20HZ
,
130 .divider
= INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
135 .gyro_fifo_enable
= false,
136 .accl_fifo_enable
= false,
137 .temp_fifo_enable
= false,
138 .magn_fifo_enable
= false,
139 .accl_fs
= INV_MPU6050_FS_02G
,
143 /* Indexed by enum inv_devices */
144 static const struct inv_mpu6050_hw hw_info
[] = {
146 .whoami
= INV_MPU6050_WHOAMI_VALUE
,
148 .reg
= ®_set_6050
,
149 .config
= &chip_config_6050
,
151 .temp
= {INV_MPU6050_TEMP_OFFSET
, INV_MPU6050_TEMP_SCALE
},
152 .startup_time
= {INV_MPU6050_GYRO_STARTUP_TIME
, INV_MPU6050_ACCEL_STARTUP_TIME
},
155 .whoami
= INV_MPU6500_WHOAMI_VALUE
,
157 .reg
= ®_set_6500
,
158 .config
= &chip_config_6500
,
160 .temp
= {INV_MPU6500_TEMP_OFFSET
, INV_MPU6500_TEMP_SCALE
},
161 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
164 .whoami
= INV_MPU6515_WHOAMI_VALUE
,
166 .reg
= ®_set_6500
,
167 .config
= &chip_config_6500
,
169 .temp
= {INV_MPU6500_TEMP_OFFSET
, INV_MPU6500_TEMP_SCALE
},
170 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
173 .whoami
= INV_MPU6880_WHOAMI_VALUE
,
175 .reg
= ®_set_6500
,
176 .config
= &chip_config_6500
,
178 .temp
= {INV_MPU6500_TEMP_OFFSET
, INV_MPU6500_TEMP_SCALE
},
179 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
182 .whoami
= INV_MPU6000_WHOAMI_VALUE
,
184 .reg
= ®_set_6050
,
185 .config
= &chip_config_6050
,
187 .temp
= {INV_MPU6050_TEMP_OFFSET
, INV_MPU6050_TEMP_SCALE
},
188 .startup_time
= {INV_MPU6050_GYRO_STARTUP_TIME
, INV_MPU6050_ACCEL_STARTUP_TIME
},
191 .whoami
= INV_MPU9150_WHOAMI_VALUE
,
193 .reg
= ®_set_6050
,
194 .config
= &chip_config_6050
,
196 .temp
= {INV_MPU6050_TEMP_OFFSET
, INV_MPU6050_TEMP_SCALE
},
197 .startup_time
= {INV_MPU6050_GYRO_STARTUP_TIME
, INV_MPU6050_ACCEL_STARTUP_TIME
},
200 .whoami
= INV_MPU9250_WHOAMI_VALUE
,
202 .reg
= ®_set_6500
,
203 .config
= &chip_config_6500
,
205 .temp
= {INV_MPU6500_TEMP_OFFSET
, INV_MPU6500_TEMP_SCALE
},
206 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
209 .whoami
= INV_MPU9255_WHOAMI_VALUE
,
211 .reg
= ®_set_6500
,
212 .config
= &chip_config_6500
,
214 .temp
= {INV_MPU6500_TEMP_OFFSET
, INV_MPU6500_TEMP_SCALE
},
215 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
218 .whoami
= INV_ICM20608_WHOAMI_VALUE
,
220 .reg
= ®_set_6500
,
221 .config
= &chip_config_6500
,
223 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
224 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
227 .whoami
= INV_ICM20608D_WHOAMI_VALUE
,
229 .reg
= ®_set_6500
,
230 .config
= &chip_config_6500
,
232 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
233 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
236 .whoami
= INV_ICM20609_WHOAMI_VALUE
,
238 .reg
= ®_set_6500
,
239 .config
= &chip_config_6500
,
240 .fifo_size
= 4 * 1024,
241 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
242 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
245 .whoami
= INV_ICM20689_WHOAMI_VALUE
,
247 .reg
= ®_set_6500
,
248 .config
= &chip_config_6500
,
249 .fifo_size
= 4 * 1024,
250 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
251 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
254 .whoami
= INV_ICM20600_WHOAMI_VALUE
,
256 .reg
= ®_set_icm20602
,
257 .config
= &chip_config_6500
,
259 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
260 .startup_time
= {INV_ICM20602_GYRO_STARTUP_TIME
, INV_ICM20602_ACCEL_STARTUP_TIME
},
263 .whoami
= INV_ICM20602_WHOAMI_VALUE
,
265 .reg
= ®_set_icm20602
,
266 .config
= &chip_config_6500
,
268 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
269 .startup_time
= {INV_ICM20602_GYRO_STARTUP_TIME
, INV_ICM20602_ACCEL_STARTUP_TIME
},
272 .whoami
= INV_ICM20690_WHOAMI_VALUE
,
274 .reg
= ®_set_6500
,
275 .config
= &chip_config_6500
,
277 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
278 .startup_time
= {INV_ICM20690_GYRO_STARTUP_TIME
, INV_ICM20690_ACCEL_STARTUP_TIME
},
281 .whoami
= INV_IAM20680_WHOAMI_VALUE
,
283 .reg
= ®_set_6500
,
284 .config
= &chip_config_6500
,
286 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
287 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
290 .whoami
= INV_IAM20680HP_WHOAMI_VALUE
,
291 .name
= "IAM20680HP",
292 .reg
= ®_set_6500
,
293 .config
= &chip_config_6500
,
294 .fifo_size
= 4 * 1024,
295 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
296 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
299 .whoami
= INV_IAM20680HT_WHOAMI_VALUE
,
300 .name
= "IAM20680HT",
301 .reg
= ®_set_6500
,
302 .config
= &chip_config_6500
,
303 .fifo_size
= 4 * 1024,
304 .temp
= {INV_ICM20608_TEMP_OFFSET
, INV_ICM20608_TEMP_SCALE
},
305 .startup_time
= {INV_MPU6500_GYRO_STARTUP_TIME
, INV_MPU6500_ACCEL_STARTUP_TIME
},
309 static int inv_mpu6050_pwr_mgmt_1_write(struct inv_mpu6050_state
*st
, bool sleep
,
310 bool cycle
, int clock
, int temp_dis
)
315 clock
= st
->chip_config
.clk
;
317 temp_dis
= !st
->chip_config
.temp_en
;
319 val
= clock
& INV_MPU6050_BIT_CLK_MASK
;
321 val
|= INV_MPU6050_BIT_TEMP_DIS
;
323 val
|= INV_MPU6050_BIT_SLEEP
;
325 val
|= INV_MPU6050_BIT_CYCLE
;
327 dev_dbg(regmap_get_device(st
->map
), "pwr_mgmt_1: 0x%x\n", val
);
328 return regmap_write(st
->map
, st
->reg
->pwr_mgmt_1
, val
);
331 static int inv_mpu6050_clock_switch(struct inv_mpu6050_state
*st
,
336 switch (st
->chip_type
) {
340 /* old chips: switch clock manually */
341 ret
= inv_mpu6050_pwr_mgmt_1_write(st
, false, false, clock
, -1);
344 st
->chip_config
.clk
= clock
;
347 /* automatic clock switching, nothing to do */
354 int inv_mpu6050_switch_engine(struct inv_mpu6050_state
*st
, bool en
,
357 unsigned int sleep
, val
;
358 u8 pwr_mgmt2
, user_ctrl
;
361 /* delete useless requests */
362 if (mask
& INV_MPU6050_SENSOR_ACCL
&& en
== st
->chip_config
.accl_en
)
363 mask
&= ~INV_MPU6050_SENSOR_ACCL
;
364 if (mask
& INV_MPU6050_SENSOR_GYRO
&& en
== st
->chip_config
.gyro_en
)
365 mask
&= ~INV_MPU6050_SENSOR_GYRO
;
366 if (mask
& INV_MPU6050_SENSOR_TEMP
&& en
== st
->chip_config
.temp_en
)
367 mask
&= ~INV_MPU6050_SENSOR_TEMP
;
368 if (mask
& INV_MPU6050_SENSOR_MAGN
&& en
== st
->chip_config
.magn_en
)
369 mask
&= ~INV_MPU6050_SENSOR_MAGN
;
370 if (mask
& INV_MPU6050_SENSOR_WOM
&& en
== st
->chip_config
.wom_en
)
371 mask
&= ~INV_MPU6050_SENSOR_WOM
;
373 /* force accel on if WoM is on and not going off */
374 if (!en
&& (mask
& INV_MPU6050_SENSOR_ACCL
) && st
->chip_config
.wom_en
&&
375 !(mask
& INV_MPU6050_SENSOR_WOM
))
376 mask
&= ~INV_MPU6050_SENSOR_ACCL
;
381 /* turn on/off temperature sensor */
382 if (mask
& INV_MPU6050_SENSOR_TEMP
) {
383 ret
= inv_mpu6050_pwr_mgmt_1_write(st
, false, false, -1, !en
);
386 st
->chip_config
.temp_en
= en
;
389 /* update user_crtl for driving magnetometer */
390 if (mask
& INV_MPU6050_SENSOR_MAGN
) {
391 user_ctrl
= st
->chip_config
.user_ctrl
;
393 user_ctrl
|= INV_MPU6050_BIT_I2C_MST_EN
;
395 user_ctrl
&= ~INV_MPU6050_BIT_I2C_MST_EN
;
396 ret
= regmap_write(st
->map
, st
->reg
->user_ctrl
, user_ctrl
);
399 st
->chip_config
.user_ctrl
= user_ctrl
;
400 st
->chip_config
.magn_en
= en
;
403 /* manage accel & gyro engines */
404 if (mask
& (INV_MPU6050_SENSOR_ACCL
| INV_MPU6050_SENSOR_GYRO
)) {
405 /* compute power management 2 current value */
407 if (!st
->chip_config
.accl_en
)
408 pwr_mgmt2
|= INV_MPU6050_BIT_PWR_ACCL_STBY
;
409 if (!st
->chip_config
.gyro_en
)
410 pwr_mgmt2
|= INV_MPU6050_BIT_PWR_GYRO_STBY
;
412 /* update to new requested value */
413 if (mask
& INV_MPU6050_SENSOR_ACCL
) {
415 pwr_mgmt2
&= ~INV_MPU6050_BIT_PWR_ACCL_STBY
;
417 pwr_mgmt2
|= INV_MPU6050_BIT_PWR_ACCL_STBY
;
419 if (mask
& INV_MPU6050_SENSOR_GYRO
) {
421 pwr_mgmt2
&= ~INV_MPU6050_BIT_PWR_GYRO_STBY
;
423 pwr_mgmt2
|= INV_MPU6050_BIT_PWR_GYRO_STBY
;
426 /* switch clock to internal when turning gyro off */
427 if (mask
& INV_MPU6050_SENSOR_GYRO
&& !en
) {
428 ret
= inv_mpu6050_clock_switch(st
, INV_CLK_INTERNAL
);
433 /* update sensors engine */
434 dev_dbg(regmap_get_device(st
->map
), "pwr_mgmt_2: 0x%x\n",
436 ret
= regmap_write(st
->map
, st
->reg
->pwr_mgmt_2
, pwr_mgmt2
);
439 if (mask
& INV_MPU6050_SENSOR_ACCL
)
440 st
->chip_config
.accl_en
= en
;
441 if (mask
& INV_MPU6050_SENSOR_GYRO
)
442 st
->chip_config
.gyro_en
= en
;
444 /* compute required time to have sensors stabilized */
447 if (mask
& INV_MPU6050_SENSOR_ACCL
) {
448 if (sleep
< st
->hw
->startup_time
.accel
)
449 sleep
= st
->hw
->startup_time
.accel
;
451 if (mask
& INV_MPU6050_SENSOR_GYRO
) {
452 if (sleep
< st
->hw
->startup_time
.gyro
)
453 sleep
= st
->hw
->startup_time
.gyro
;
456 if (mask
& INV_MPU6050_SENSOR_GYRO
) {
457 if (sleep
< INV_MPU6050_GYRO_DOWN_TIME
)
458 sleep
= INV_MPU6050_GYRO_DOWN_TIME
;
464 /* switch clock to PLL when turning gyro on */
465 if (mask
& INV_MPU6050_SENSOR_GYRO
&& en
) {
466 ret
= inv_mpu6050_clock_switch(st
, INV_CLK_PLL
);
472 /* enable/disable accel intelligence control */
473 if (mask
& INV_MPU6050_SENSOR_WOM
) {
474 val
= en
? INV_MPU6500_BIT_ACCEL_INTEL_EN
|
475 INV_MPU6500_BIT_ACCEL_INTEL_MODE
: 0;
476 ret
= regmap_write(st
->map
, INV_MPU6500_REG_ACCEL_INTEL_CTRL
, val
);
479 st
->chip_config
.wom_en
= en
;
485 static int inv_mpu6050_set_power_itg(struct inv_mpu6050_state
*st
,
490 result
= inv_mpu6050_pwr_mgmt_1_write(st
, !power_on
, false, -1, -1);
495 usleep_range(INV_MPU6050_REG_UP_TIME_MIN
,
496 INV_MPU6050_REG_UP_TIME_MAX
);
501 static int inv_mpu6050_set_gyro_fsr(struct inv_mpu6050_state
*st
,
502 enum inv_mpu6050_fsr_e val
)
504 unsigned int gyro_shift
;
507 switch (st
->chip_type
) {
509 gyro_shift
= INV_ICM20690_GYRO_CONFIG_FSR_SHIFT
;
512 gyro_shift
= INV_MPU6050_GYRO_CONFIG_FSR_SHIFT
;
516 data
= val
<< gyro_shift
;
517 return regmap_write(st
->map
, st
->reg
->gyro_config
, data
);
520 static int inv_mpu6050_set_accel_lpf_regs(struct inv_mpu6050_state
*st
,
521 enum inv_mpu6050_filter_e val
)
523 switch (st
->chip_type
) {
527 /* old chips, nothing to do */
533 /* set FIFO size to maximum value */
534 val
|= INV_ICM20689_BITS_FIFO_SIZE_MAX
;
540 return regmap_write(st
->map
, st
->reg
->accel_lpf
, val
);
544 * inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent
546 * MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope
547 * MPU6500 and above have a dedicated register for accelerometer
549 static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state
*st
,
550 enum inv_mpu6050_filter_e val
)
554 result
= regmap_write(st
->map
, st
->reg
->lpf
, val
);
559 return inv_mpu6050_set_accel_lpf_regs(st
, val
);
563 * inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
565 * Initial configuration:
569 * Clock source: Gyro PLL
571 static int inv_mpu6050_init_config(struct iio_dev
*indio_dev
)
575 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
576 struct inv_sensors_timestamp_chip timestamp
;
578 result
= inv_mpu6050_set_gyro_fsr(st
, st
->chip_config
.fsr
);
582 result
= inv_mpu6050_set_lpf_regs(st
, st
->chip_config
.lpf
);
586 d
= st
->chip_config
.divider
;
587 result
= regmap_write(st
->map
, st
->reg
->sample_rate_div
, d
);
591 d
= (st
->chip_config
.accl_fs
<< INV_MPU6050_ACCL_CONFIG_FSR_SHIFT
);
592 result
= regmap_write(st
->map
, st
->reg
->accl_config
, d
);
596 result
= regmap_write(st
->map
, st
->reg
->int_pin_cfg
, st
->irq_mask
);
600 /* clock jitter is +/- 2% */
601 timestamp
.clock_period
= NSEC_PER_SEC
/ INV_MPU6050_INTERNAL_FREQ_HZ
;
602 timestamp
.jitter
= 20;
603 timestamp
.init_period
=
604 NSEC_PER_SEC
/ INV_MPU6050_DIVIDER_TO_FIFO_RATE(st
->chip_config
.divider
);
605 inv_sensors_timestamp_init(&st
->timestamp
, ×tamp
);
607 /* magn chip init, noop if not present in the chip */
608 result
= inv_mpu_magn_probe(st
);
615 static int inv_mpu6050_sensor_set(struct inv_mpu6050_state
*st
, int reg
,
619 __be16 d
= cpu_to_be16(val
);
621 ind
= (axis
- IIO_MOD_X
) * 2;
623 return regmap_bulk_write(st
->map
, reg
+ ind
, &d
, sizeof(d
));
626 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state
*st
, int reg
,
632 ind
= (axis
- IIO_MOD_X
) * 2;
633 result
= regmap_bulk_read(st
->map
, reg
+ ind
, &d
, sizeof(d
));
636 *val
= (short)be16_to_cpup(&d
);
641 static int inv_mpu6050_read_channel_data(struct iio_dev
*indio_dev
,
642 struct iio_chan_spec
const *chan
,
645 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
646 struct device
*pdev
= regmap_get_device(st
->map
);
647 unsigned int freq_hz
, period_us
, min_sleep_us
, max_sleep_us
;
651 /* compute sample period */
652 freq_hz
= INV_MPU6050_DIVIDER_TO_FIFO_RATE(st
->chip_config
.divider
);
653 period_us
= 1000000 / freq_hz
;
655 result
= pm_runtime_resume_and_get(pdev
);
659 switch (chan
->type
) {
661 if (!st
->chip_config
.gyro_en
) {
662 result
= inv_mpu6050_switch_engine(st
, true,
663 INV_MPU6050_SENSOR_GYRO
);
665 goto error_power_off
;
666 /* need to wait 2 periods to have first valid sample */
667 min_sleep_us
= 2 * period_us
;
668 max_sleep_us
= 2 * (period_us
+ period_us
/ 2);
669 usleep_range(min_sleep_us
, max_sleep_us
);
671 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->raw_gyro
,
672 chan
->channel2
, val
);
675 if (!st
->chip_config
.accl_en
) {
676 result
= inv_mpu6050_switch_engine(st
, true,
677 INV_MPU6050_SENSOR_ACCL
);
679 goto error_power_off
;
680 /* wait 1 period for first sample availability */
681 min_sleep_us
= period_us
;
682 max_sleep_us
= period_us
+ period_us
/ 2;
683 usleep_range(min_sleep_us
, max_sleep_us
);
685 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->raw_accl
,
686 chan
->channel2
, val
);
689 /* temperature sensor work only with accel and/or gyro */
690 if (!st
->chip_config
.accl_en
&& !st
->chip_config
.gyro_en
) {
692 goto error_power_off
;
694 if (!st
->chip_config
.temp_en
) {
695 result
= inv_mpu6050_switch_engine(st
, true,
696 INV_MPU6050_SENSOR_TEMP
);
698 goto error_power_off
;
699 /* wait 1 period for first sample availability */
700 min_sleep_us
= period_us
;
701 max_sleep_us
= period_us
+ period_us
/ 2;
702 usleep_range(min_sleep_us
, max_sleep_us
);
704 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->temperature
,
708 if (!st
->chip_config
.magn_en
) {
709 result
= inv_mpu6050_switch_engine(st
, true,
710 INV_MPU6050_SENSOR_MAGN
);
712 goto error_power_off
;
713 /* frequency is limited for magnetometer */
714 if (freq_hz
> INV_MPU_MAGN_FREQ_HZ_MAX
) {
715 freq_hz
= INV_MPU_MAGN_FREQ_HZ_MAX
;
716 period_us
= 1000000 / freq_hz
;
718 /* need to wait 2 periods to have first valid sample */
719 min_sleep_us
= 2 * period_us
;
720 max_sleep_us
= 2 * (period_us
+ period_us
/ 2);
721 usleep_range(min_sleep_us
, max_sleep_us
);
723 ret
= inv_mpu_magn_read(st
, chan
->channel2
, val
);
730 pm_runtime_mark_last_busy(pdev
);
731 pm_runtime_put_autosuspend(pdev
);
736 pm_runtime_put_autosuspend(pdev
);
741 inv_mpu6050_read_raw(struct iio_dev
*indio_dev
,
742 struct iio_chan_spec
const *chan
,
743 int *val
, int *val2
, long mask
)
745 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
749 case IIO_CHAN_INFO_RAW
:
750 ret
= iio_device_claim_direct_mode(indio_dev
);
753 mutex_lock(&st
->lock
);
754 ret
= inv_mpu6050_read_channel_data(indio_dev
, chan
, val
);
755 mutex_unlock(&st
->lock
);
756 iio_device_release_direct_mode(indio_dev
);
758 case IIO_CHAN_INFO_SCALE
:
759 switch (chan
->type
) {
761 mutex_lock(&st
->lock
);
763 *val2
= gyro_scale_6050
[st
->chip_config
.fsr
];
764 mutex_unlock(&st
->lock
);
766 return IIO_VAL_INT_PLUS_NANO
;
768 mutex_lock(&st
->lock
);
770 *val2
= accel_scale
[st
->chip_config
.accl_fs
];
771 mutex_unlock(&st
->lock
);
773 return IIO_VAL_INT_PLUS_MICRO
;
775 *val
= st
->hw
->temp
.scale
/ 1000000;
776 *val2
= st
->hw
->temp
.scale
% 1000000;
777 return IIO_VAL_INT_PLUS_MICRO
;
779 return inv_mpu_magn_get_scale(st
, chan
, val
, val2
);
783 case IIO_CHAN_INFO_OFFSET
:
784 switch (chan
->type
) {
786 *val
= st
->hw
->temp
.offset
;
791 case IIO_CHAN_INFO_CALIBBIAS
:
792 switch (chan
->type
) {
794 mutex_lock(&st
->lock
);
795 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->gyro_offset
,
796 chan
->channel2
, val
);
797 mutex_unlock(&st
->lock
);
800 mutex_lock(&st
->lock
);
801 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->accl_offset
,
802 chan
->channel2
, val
);
803 mutex_unlock(&st
->lock
);
814 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state
*st
, int val
,
822 for (i
= 0; i
< ARRAY_SIZE(gyro_scale_6050
); ++i
) {
823 if (gyro_scale_6050
[i
] == val2
) {
824 result
= inv_mpu6050_set_gyro_fsr(st
, i
);
828 st
->chip_config
.fsr
= i
;
836 static int inv_write_raw_get_fmt(struct iio_dev
*indio_dev
,
837 struct iio_chan_spec
const *chan
, long mask
)
840 case IIO_CHAN_INFO_SCALE
:
841 switch (chan
->type
) {
843 return IIO_VAL_INT_PLUS_NANO
;
845 return IIO_VAL_INT_PLUS_MICRO
;
848 return IIO_VAL_INT_PLUS_MICRO
;
854 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state
*st
, int val
,
863 for (i
= 0; i
< ARRAY_SIZE(accel_scale
); ++i
) {
864 if (accel_scale
[i
] == val2
) {
865 d
= (i
<< INV_MPU6050_ACCL_CONFIG_FSR_SHIFT
);
866 result
= regmap_write(st
->map
, st
->reg
->accl_config
, d
);
870 st
->chip_config
.accl_fs
= i
;
878 static int inv_mpu6050_write_raw(struct iio_dev
*indio_dev
,
879 struct iio_chan_spec
const *chan
,
880 int val
, int val2
, long mask
)
882 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
883 struct device
*pdev
= regmap_get_device(st
->map
);
887 * we should only update scale when the chip is disabled, i.e.
890 result
= iio_device_claim_direct_mode(indio_dev
);
894 mutex_lock(&st
->lock
);
895 result
= pm_runtime_resume_and_get(pdev
);
897 goto error_write_raw_unlock
;
900 case IIO_CHAN_INFO_SCALE
:
901 switch (chan
->type
) {
903 result
= inv_mpu6050_write_gyro_scale(st
, val
, val2
);
906 result
= inv_mpu6050_write_accel_scale(st
, val
, val2
);
913 case IIO_CHAN_INFO_CALIBBIAS
:
914 switch (chan
->type
) {
916 result
= inv_mpu6050_sensor_set(st
,
917 st
->reg
->gyro_offset
,
918 chan
->channel2
, val
);
921 result
= inv_mpu6050_sensor_set(st
,
922 st
->reg
->accl_offset
,
923 chan
->channel2
, val
);
935 pm_runtime_mark_last_busy(pdev
);
936 pm_runtime_put_autosuspend(pdev
);
937 error_write_raw_unlock
:
938 mutex_unlock(&st
->lock
);
939 iio_device_release_direct_mode(indio_dev
);
944 static u64
inv_mpu6050_convert_wom_to_roc(unsigned int threshold
, unsigned int freq_div
)
946 /* 4mg per LSB converted in m/s² in micro (1000000) */
947 const unsigned int convert
= 4U * 9807U;
950 value
= threshold
* convert
;
952 /* compute the differential by multiplying by the frequency */
953 return div_u64(value
* INV_MPU6050_INTERNAL_FREQ_HZ
, freq_div
);
956 static unsigned int inv_mpu6050_convert_roc_to_wom(u64 roc
, unsigned int freq_div
)
958 /* 4mg per LSB converted in m/s² in micro (1000000) */
959 const unsigned int convert
= 4U * 9807U;
962 /* return 0 only if roc is 0 */
966 value
= div_u64(roc
* freq_div
, convert
* INV_MPU6050_INTERNAL_FREQ_HZ
);
968 /* limit value to 8 bits and prevent 0 */
969 return min(255, max(1, value
));
972 static int inv_mpu6050_set_wom_int(struct inv_mpu6050_state
*st
, bool on
)
974 unsigned int reg_val
, val
;
976 switch (st
->chip_type
) {
985 reg_val
= INV_MPU6500_BIT_WOM_INT_EN
;
988 reg_val
= INV_ICM20608_BIT_WOM_INT_EN
;
992 val
= on
? reg_val
: 0;
994 return regmap_update_bits(st
->map
, st
->reg
->int_enable
, reg_val
, val
);
997 static int inv_mpu6050_set_wom_threshold(struct inv_mpu6050_state
*st
, u64 value
,
998 unsigned int freq_div
)
1000 unsigned int threshold
;
1003 /* convert roc to wom threshold and convert back to handle clipping */
1004 threshold
= inv_mpu6050_convert_roc_to_wom(value
, freq_div
);
1005 value
= inv_mpu6050_convert_wom_to_roc(threshold
, freq_div
);
1007 dev_dbg(regmap_get_device(st
->map
), "wom_threshold: 0x%x\n", threshold
);
1009 switch (st
->chip_type
) {
1015 st
->data
[0] = threshold
;
1016 st
->data
[1] = threshold
;
1017 st
->data
[2] = threshold
;
1018 result
= regmap_bulk_write(st
->map
, INV_ICM20609_REG_ACCEL_WOM_X_THR
,
1022 result
= regmap_write(st
->map
, INV_MPU6500_REG_WOM_THRESHOLD
, threshold
);
1028 st
->chip_config
.roc_threshold
= value
;
1033 static int inv_mpu6050_set_lp_odr(struct inv_mpu6050_state
*st
, unsigned int freq_div
,
1034 unsigned int *lp_div
)
1036 static const unsigned int freq_dividers
[] = {2, 4, 8, 16, 32, 64, 128, 256};
1037 static const unsigned int reg_values
[] = {
1038 INV_MPU6050_LPOSC_500HZ
, INV_MPU6050_LPOSC_250HZ
,
1039 INV_MPU6050_LPOSC_125HZ
, INV_MPU6050_LPOSC_62HZ
,
1040 INV_MPU6050_LPOSC_31HZ
, INV_MPU6050_LPOSC_16HZ
,
1041 INV_MPU6050_LPOSC_8HZ
, INV_MPU6050_LPOSC_4HZ
,
1043 unsigned int val
, i
;
1045 switch (st
->chip_type
) {
1052 *lp_div
= INV_MPU6050_FREQ_DIVIDER(st
);
1058 /* found the nearest superior frequency divider */
1059 i
= ARRAY_SIZE(reg_values
) - 1;
1060 val
= reg_values
[i
];
1061 *lp_div
= freq_dividers
[i
];
1062 for (i
= 0; i
< ARRAY_SIZE(freq_dividers
); ++i
) {
1063 if (freq_div
<= freq_dividers
[i
]) {
1064 val
= reg_values
[i
];
1065 *lp_div
= freq_dividers
[i
];
1070 dev_dbg(regmap_get_device(st
->map
), "lp_odr: 0x%x\n", val
);
1071 return regmap_write(st
->map
, INV_MPU6500_REG_LP_ODR
, val
);
1074 static int inv_mpu6050_set_wom_lp(struct inv_mpu6050_state
*st
, bool on
)
1076 unsigned int lp_div
;
1080 /* set low power ODR */
1081 result
= inv_mpu6050_set_lp_odr(st
, INV_MPU6050_FREQ_DIVIDER(st
), &lp_div
);
1084 /* disable accel low pass filter */
1085 result
= inv_mpu6050_set_accel_lpf_regs(st
, INV_MPU6050_FILTER_NOLPF
);
1088 /* update wom threshold with new low-power frequency divider */
1089 result
= inv_mpu6050_set_wom_threshold(st
, st
->chip_config
.roc_threshold
, lp_div
);
1092 /* set cycle mode */
1093 result
= inv_mpu6050_pwr_mgmt_1_write(st
, false, true, -1, -1);
1095 /* disable cycle mode */
1096 result
= inv_mpu6050_pwr_mgmt_1_write(st
, false, false, -1, -1);
1099 /* restore wom threshold */
1100 result
= inv_mpu6050_set_wom_threshold(st
, st
->chip_config
.roc_threshold
,
1101 INV_MPU6050_FREQ_DIVIDER(st
));
1104 /* restore accel low pass filter */
1105 result
= inv_mpu6050_set_accel_lpf_regs(st
, st
->chip_config
.lpf
);
1111 static int inv_mpu6050_enable_wom(struct inv_mpu6050_state
*st
, bool en
)
1113 struct device
*pdev
= regmap_get_device(st
->map
);
1118 result
= pm_runtime_resume_and_get(pdev
);
1122 mask
= INV_MPU6050_SENSOR_ACCL
| INV_MPU6050_SENSOR_WOM
;
1123 result
= inv_mpu6050_switch_engine(st
, true, mask
);
1127 result
= inv_mpu6050_set_wom_int(st
, true);
1131 result
= inv_mpu6050_set_wom_int(st
, false);
1133 dev_err(pdev
, "error %d disabling WoM interrupt bit", result
);
1135 /* disable only WoM and let accel be disabled by autosuspend */
1136 result
= inv_mpu6050_switch_engine(st
, false, INV_MPU6050_SENSOR_WOM
);
1138 dev_err(pdev
, "error %d disabling WoM force off", result
);
1140 st
->chip_config
.wom_en
= false;
1143 pm_runtime_mark_last_busy(pdev
);
1144 pm_runtime_put_autosuspend(pdev
);
1150 pm_runtime_mark_last_busy(pdev
);
1151 pm_runtime_put_autosuspend(pdev
);
1155 static int inv_mpu6050_read_event_config(struct iio_dev
*indio_dev
,
1156 const struct iio_chan_spec
*chan
,
1157 enum iio_event_type type
,
1158 enum iio_event_direction dir
)
1160 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1162 /* support only WoM (accel roc rising) event */
1163 if (chan
->type
!= IIO_ACCEL
|| type
!= IIO_EV_TYPE_ROC
||
1164 dir
!= IIO_EV_DIR_RISING
)
1167 guard(mutex
)(&st
->lock
);
1169 return st
->chip_config
.wom_en
? 1 : 0;
1172 static int inv_mpu6050_write_event_config(struct iio_dev
*indio_dev
,
1173 const struct iio_chan_spec
*chan
,
1174 enum iio_event_type type
,
1175 enum iio_event_direction dir
,
1178 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1180 /* support only WoM (accel roc rising) event */
1181 if (chan
->type
!= IIO_ACCEL
|| type
!= IIO_EV_TYPE_ROC
||
1182 dir
!= IIO_EV_DIR_RISING
)
1185 guard(mutex
)(&st
->lock
);
1187 if (st
->chip_config
.wom_en
== state
)
1190 return inv_mpu6050_enable_wom(st
, state
);
1193 static int inv_mpu6050_read_event_value(struct iio_dev
*indio_dev
,
1194 const struct iio_chan_spec
*chan
,
1195 enum iio_event_type type
,
1196 enum iio_event_direction dir
,
1197 enum iio_event_info info
,
1198 int *val
, int *val2
)
1200 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1203 /* support only WoM (accel roc rising) event value */
1204 if (chan
->type
!= IIO_ACCEL
|| type
!= IIO_EV_TYPE_ROC
||
1205 dir
!= IIO_EV_DIR_RISING
|| info
!= IIO_EV_INFO_VALUE
)
1208 guard(mutex
)(&st
->lock
);
1210 /* return value in micro */
1211 *val
= div_u64_rem(st
->chip_config
.roc_threshold
, 1000000U, &rem
);
1214 return IIO_VAL_INT_PLUS_MICRO
;
1217 static int inv_mpu6050_write_event_value(struct iio_dev
*indio_dev
,
1218 const struct iio_chan_spec
*chan
,
1219 enum iio_event_type type
,
1220 enum iio_event_direction dir
,
1221 enum iio_event_info info
,
1224 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1225 struct device
*pdev
= regmap_get_device(st
->map
);
1229 /* support only WoM (accel roc rising) event value */
1230 if (chan
->type
!= IIO_ACCEL
|| type
!= IIO_EV_TYPE_ROC
||
1231 dir
!= IIO_EV_DIR_RISING
|| info
!= IIO_EV_INFO_VALUE
)
1234 if (val
< 0 || val2
< 0)
1237 guard(mutex
)(&st
->lock
);
1239 result
= pm_runtime_resume_and_get(pdev
);
1243 value
= (u64
)val
* 1000000ULL + (u64
)val2
;
1244 result
= inv_mpu6050_set_wom_threshold(st
, value
, INV_MPU6050_FREQ_DIVIDER(st
));
1246 pm_runtime_mark_last_busy(pdev
);
1247 pm_runtime_put_autosuspend(pdev
);
1253 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
1255 * Based on the Nyquist principle, the bandwidth of the low
1256 * pass filter must not exceed the signal sampling rate divided
1257 * by 2, or there would be aliasing.
1258 * This function basically search for the correct low pass
1259 * parameters based on the fifo rate, e.g, sampling frequency.
1261 * lpf is set automatically when setting sampling rate to avoid any aliases.
1263 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state
*st
, int rate
)
1265 static const int hz
[] = {400, 200, 90, 40, 20, 10};
1266 static const int d
[] = {
1267 INV_MPU6050_FILTER_200HZ
, INV_MPU6050_FILTER_100HZ
,
1268 INV_MPU6050_FILTER_45HZ
, INV_MPU6050_FILTER_20HZ
,
1269 INV_MPU6050_FILTER_10HZ
, INV_MPU6050_FILTER_5HZ
1274 data
= INV_MPU6050_FILTER_5HZ
;
1275 for (i
= 0; i
< ARRAY_SIZE(hz
); ++i
) {
1276 if (rate
>= hz
[i
]) {
1281 result
= inv_mpu6050_set_lpf_regs(st
, data
);
1284 st
->chip_config
.lpf
= data
;
1290 * inv_mpu6050_fifo_rate_store() - Set fifo rate.
1293 inv_mpu6050_fifo_rate_store(struct device
*dev
, struct device_attribute
*attr
,
1294 const char *buf
, size_t count
)
1301 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1302 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1303 struct device
*pdev
= regmap_get_device(st
->map
);
1305 if (kstrtoint(buf
, 10, &fifo_rate
))
1307 if (fifo_rate
< INV_MPU6050_MIN_FIFO_RATE
||
1308 fifo_rate
> INV_MPU6050_MAX_FIFO_RATE
)
1311 /* compute the chip sample rate divider */
1312 d
= INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate
);
1313 /* compute back the fifo rate to handle truncation cases */
1314 fifo_rate
= INV_MPU6050_DIVIDER_TO_FIFO_RATE(d
);
1315 fifo_period
= NSEC_PER_SEC
/ fifo_rate
;
1317 mutex_lock(&st
->lock
);
1318 if (d
== st
->chip_config
.divider
) {
1320 goto fifo_rate_fail_unlock
;
1323 fifo_on
= st
->chip_config
.accl_fifo_enable
||
1324 st
->chip_config
.gyro_fifo_enable
||
1325 st
->chip_config
.magn_fifo_enable
;
1326 result
= inv_sensors_timestamp_update_odr(&st
->timestamp
, fifo_period
, fifo_on
);
1328 goto fifo_rate_fail_unlock
;
1330 result
= pm_runtime_resume_and_get(pdev
);
1332 goto fifo_rate_fail_unlock
;
1334 result
= regmap_write(st
->map
, st
->reg
->sample_rate_div
, d
);
1336 goto fifo_rate_fail_power_off
;
1337 st
->chip_config
.divider
= d
;
1339 result
= inv_mpu6050_set_lpf(st
, fifo_rate
);
1341 goto fifo_rate_fail_power_off
;
1343 /* update rate for magn, noop if not present in chip */
1344 result
= inv_mpu_magn_set_rate(st
, fifo_rate
);
1346 goto fifo_rate_fail_power_off
;
1348 /* update wom threshold since roc is dependent on sampling frequency */
1349 result
= inv_mpu6050_set_wom_threshold(st
, st
->chip_config
.roc_threshold
,
1350 INV_MPU6050_FREQ_DIVIDER(st
));
1352 goto fifo_rate_fail_power_off
;
1354 pm_runtime_mark_last_busy(pdev
);
1355 fifo_rate_fail_power_off
:
1356 pm_runtime_put_autosuspend(pdev
);
1357 fifo_rate_fail_unlock
:
1358 mutex_unlock(&st
->lock
);
1366 * inv_fifo_rate_show() - Get the current sampling rate.
1369 inv_fifo_rate_show(struct device
*dev
, struct device_attribute
*attr
,
1372 struct inv_mpu6050_state
*st
= iio_priv(dev_to_iio_dev(dev
));
1375 mutex_lock(&st
->lock
);
1376 fifo_rate
= INV_MPU6050_DIVIDER_TO_FIFO_RATE(st
->chip_config
.divider
);
1377 mutex_unlock(&st
->lock
);
1379 return scnprintf(buf
, PAGE_SIZE
, "%u\n", fifo_rate
);
1383 * inv_attr_show() - calling this function will show current
1386 * Deprecated in favor of IIO mounting matrix API.
1388 * See inv_get_mount_matrix()
1390 static ssize_t
inv_attr_show(struct device
*dev
, struct device_attribute
*attr
,
1393 struct inv_mpu6050_state
*st
= iio_priv(dev_to_iio_dev(dev
));
1394 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
1397 switch (this_attr
->address
) {
1399 * In MPU6050, the two matrix are the same because gyro and accel
1400 * are integrated in one chip
1402 case ATTR_GYRO_MATRIX
:
1403 case ATTR_ACCL_MATRIX
:
1404 m
= st
->plat_data
.orientation
;
1406 return scnprintf(buf
, PAGE_SIZE
,
1407 "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
1408 m
[0], m
[1], m
[2], m
[3], m
[4], m
[5], m
[6], m
[7], m
[8]);
1415 * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
1417 * @indio_dev: The IIO device
1418 * @trig: The new trigger
1420 * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
1421 * device, -EINVAL otherwise.
1423 static int inv_mpu6050_validate_trigger(struct iio_dev
*indio_dev
,
1424 struct iio_trigger
*trig
)
1426 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1428 if (st
->trig
!= trig
)
1434 static const struct iio_mount_matrix
*
1435 inv_get_mount_matrix(const struct iio_dev
*indio_dev
,
1436 const struct iio_chan_spec
*chan
)
1438 struct inv_mpu6050_state
*data
= iio_priv(indio_dev
);
1439 const struct iio_mount_matrix
*matrix
;
1441 if (chan
->type
== IIO_MAGN
)
1442 matrix
= &data
->magn_orient
;
1444 matrix
= &data
->orientation
;
1449 static const struct iio_chan_spec_ext_info inv_ext_info
[] = {
1450 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, inv_get_mount_matrix
),
1454 static const struct iio_event_spec inv_wom_events
[] = {
1456 .type
= IIO_EV_TYPE_ROC
,
1457 .dir
= IIO_EV_DIR_RISING
,
1458 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
1459 BIT(IIO_EV_INFO_VALUE
),
1463 #define INV_MPU6050_CHAN(_type, _channel2, _index) \
1467 .channel2 = _channel2, \
1468 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1469 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1470 BIT(IIO_CHAN_INFO_CALIBBIAS), \
1471 .scan_index = _index, \
1475 .storagebits = 16, \
1477 .endianness = IIO_BE, \
1479 .ext_info = inv_ext_info, \
1482 #define INV_MPU6050_TEMP_CHAN(_index) \
1485 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
1486 | BIT(IIO_CHAN_INFO_OFFSET) \
1487 | BIT(IIO_CHAN_INFO_SCALE), \
1488 .scan_index = _index, \
1492 .storagebits = 16, \
1494 .endianness = IIO_BE, \
1498 #define INV_MPU6050_EVENT_CHAN(_type, _channel2, _events, _events_nb) \
1502 .channel2 = _channel2, \
1503 .event_spec = _events, \
1504 .num_event_specs = _events_nb, \
1508 static const struct iio_chan_spec inv_mpu6050_channels
[] = {
1509 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP
),
1511 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP
),
1513 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_X
, INV_MPU6050_SCAN_GYRO_X
),
1514 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_GYRO_Y
),
1515 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_GYRO_Z
),
1517 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_X
, INV_MPU6050_SCAN_ACCL_X
),
1518 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_ACCL_Y
),
1519 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_ACCL_Z
),
1522 static const struct iio_chan_spec inv_mpu6500_channels
[] = {
1523 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP
),
1525 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP
),
1527 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_X
, INV_MPU6050_SCAN_GYRO_X
),
1528 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_GYRO_Y
),
1529 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_GYRO_Z
),
1531 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_X
, INV_MPU6050_SCAN_ACCL_X
),
1532 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_ACCL_Y
),
1533 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_ACCL_Z
),
1535 INV_MPU6050_EVENT_CHAN(IIO_ACCEL
, IIO_MOD_X_OR_Y_OR_Z
,
1536 inv_wom_events
, ARRAY_SIZE(inv_wom_events
)),
1539 #define INV_MPU6050_SCAN_MASK_3AXIS_ACCEL \
1540 (BIT(INV_MPU6050_SCAN_ACCL_X) \
1541 | BIT(INV_MPU6050_SCAN_ACCL_Y) \
1542 | BIT(INV_MPU6050_SCAN_ACCL_Z))
1544 #define INV_MPU6050_SCAN_MASK_3AXIS_GYRO \
1545 (BIT(INV_MPU6050_SCAN_GYRO_X) \
1546 | BIT(INV_MPU6050_SCAN_GYRO_Y) \
1547 | BIT(INV_MPU6050_SCAN_GYRO_Z))
1549 #define INV_MPU6050_SCAN_MASK_TEMP (BIT(INV_MPU6050_SCAN_TEMP))
1551 static const unsigned long inv_mpu_scan_masks
[] = {
1553 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
,
1554 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_TEMP
,
1556 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
,
1557 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
| INV_MPU6050_SCAN_MASK_TEMP
,
1558 /* 6-axis accel + gyro */
1559 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
,
1560 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1561 | INV_MPU6050_SCAN_MASK_TEMP
,
1565 #define INV_MPU9X50_MAGN_CHAN(_chan2, _bits, _index) \
1569 .channel2 = _chan2, \
1570 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) | \
1571 BIT(IIO_CHAN_INFO_RAW), \
1572 .scan_index = _index, \
1575 .realbits = _bits, \
1576 .storagebits = 16, \
1578 .endianness = IIO_BE, \
1580 .ext_info = inv_ext_info, \
1583 static const struct iio_chan_spec inv_mpu9150_channels
[] = {
1584 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP
),
1586 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP
),
1588 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_X
, INV_MPU6050_SCAN_GYRO_X
),
1589 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_GYRO_Y
),
1590 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_GYRO_Z
),
1592 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_X
, INV_MPU6050_SCAN_ACCL_X
),
1593 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_ACCL_Y
),
1594 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_ACCL_Z
),
1596 /* Magnetometer resolution is 13 bits */
1597 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X
, 13, INV_MPU9X50_SCAN_MAGN_X
),
1598 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y
, 13, INV_MPU9X50_SCAN_MAGN_Y
),
1599 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z
, 13, INV_MPU9X50_SCAN_MAGN_Z
),
1602 static const struct iio_chan_spec inv_mpu9250_channels
[] = {
1603 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP
),
1605 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP
),
1607 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_X
, INV_MPU6050_SCAN_GYRO_X
),
1608 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_GYRO_Y
),
1609 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_GYRO_Z
),
1611 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_X
, INV_MPU6050_SCAN_ACCL_X
),
1612 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_ACCL_Y
),
1613 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_ACCL_Z
),
1615 /* Magnetometer resolution is 16 bits */
1616 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X
, 16, INV_MPU9X50_SCAN_MAGN_X
),
1617 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y
, 16, INV_MPU9X50_SCAN_MAGN_Y
),
1618 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z
, 16, INV_MPU9X50_SCAN_MAGN_Z
),
1621 #define INV_MPU9X50_SCAN_MASK_3AXIS_MAGN \
1622 (BIT(INV_MPU9X50_SCAN_MAGN_X) \
1623 | BIT(INV_MPU9X50_SCAN_MAGN_Y) \
1624 | BIT(INV_MPU9X50_SCAN_MAGN_Z))
1626 static const unsigned long inv_mpu9x50_scan_masks
[] = {
1628 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
,
1629 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_TEMP
,
1631 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
,
1632 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
| INV_MPU6050_SCAN_MASK_TEMP
,
1634 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
,
1635 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
| INV_MPU6050_SCAN_MASK_TEMP
,
1636 /* 6-axis accel + gyro */
1637 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
,
1638 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1639 | INV_MPU6050_SCAN_MASK_TEMP
,
1640 /* 6-axis accel + magn */
1641 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
,
1642 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1643 | INV_MPU6050_SCAN_MASK_TEMP
,
1644 /* 6-axis gyro + magn */
1645 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
| INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
,
1646 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
| INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1647 | INV_MPU6050_SCAN_MASK_TEMP
,
1648 /* 9-axis accel + gyro + magn */
1649 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1650 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
,
1651 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1652 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1653 | INV_MPU6050_SCAN_MASK_TEMP
,
1657 static const unsigned long inv_icm20602_scan_masks
[] = {
1658 /* 3-axis accel + temp (mandatory) */
1659 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_TEMP
,
1660 /* 3-axis gyro + temp (mandatory) */
1661 INV_MPU6050_SCAN_MASK_3AXIS_GYRO
| INV_MPU6050_SCAN_MASK_TEMP
,
1662 /* 6-axis accel + gyro + temp (mandatory) */
1663 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL
| INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1664 | INV_MPU6050_SCAN_MASK_TEMP
,
1669 * The user can choose any frequency between INV_MPU6050_MIN_FIFO_RATE and
1670 * INV_MPU6050_MAX_FIFO_RATE, but only these frequencies are matched by the
1671 * low-pass filter. Specifically, each of these sampling rates are about twice
1672 * the bandwidth of a corresponding low-pass filter, which should eliminate
1673 * aliasing following the Nyquist principle. By picking a frequency different
1674 * from these, the user risks aliasing effects.
1676 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
1677 static IIO_CONST_ATTR(in_anglvel_scale_available
,
1678 "0.000133090 0.000266181 0.000532362 0.001064724");
1679 static IIO_CONST_ATTR(in_accel_scale_available
,
1680 "0.000598 0.001196 0.002392 0.004785");
1681 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO
| S_IWUSR
, inv_fifo_rate_show
,
1682 inv_mpu6050_fifo_rate_store
);
1684 /* Deprecated: kept for userspace backward compatibility. */
1685 static IIO_DEVICE_ATTR(in_gyro_matrix
, S_IRUGO
, inv_attr_show
, NULL
,
1687 static IIO_DEVICE_ATTR(in_accel_matrix
, S_IRUGO
, inv_attr_show
, NULL
,
1690 static struct attribute
*inv_attributes
[] = {
1691 &iio_dev_attr_in_gyro_matrix
.dev_attr
.attr
, /* deprecated */
1692 &iio_dev_attr_in_accel_matrix
.dev_attr
.attr
, /* deprecated */
1693 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
1694 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
1695 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
1696 &iio_const_attr_in_anglvel_scale_available
.dev_attr
.attr
,
1700 static const struct attribute_group inv_attribute_group
= {
1701 .attrs
= inv_attributes
1704 static int inv_mpu6050_reg_access(struct iio_dev
*indio_dev
,
1706 unsigned int writeval
,
1707 unsigned int *readval
)
1709 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
1712 mutex_lock(&st
->lock
);
1714 ret
= regmap_read(st
->map
, reg
, readval
);
1716 ret
= regmap_write(st
->map
, reg
, writeval
);
1717 mutex_unlock(&st
->lock
);
1722 static const struct iio_info mpu_info
= {
1723 .read_raw
= &inv_mpu6050_read_raw
,
1724 .write_raw
= &inv_mpu6050_write_raw
,
1725 .write_raw_get_fmt
= &inv_write_raw_get_fmt
,
1726 .attrs
= &inv_attribute_group
,
1727 .read_event_config
= inv_mpu6050_read_event_config
,
1728 .write_event_config
= inv_mpu6050_write_event_config
,
1729 .read_event_value
= inv_mpu6050_read_event_value
,
1730 .write_event_value
= inv_mpu6050_write_event_value
,
1731 .validate_trigger
= inv_mpu6050_validate_trigger
,
1732 .debugfs_reg_access
= &inv_mpu6050_reg_access
,
1736 * inv_check_and_setup_chip() - check and setup chip.
1738 static int inv_check_and_setup_chip(struct inv_mpu6050_state
*st
)
1741 unsigned int regval
, mask
;
1744 st
->hw
= &hw_info
[st
->chip_type
];
1745 st
->reg
= hw_info
[st
->chip_type
].reg
;
1746 memcpy(&st
->chip_config
, hw_info
[st
->chip_type
].config
,
1747 sizeof(st
->chip_config
));
1748 st
->data
= devm_kzalloc(regmap_get_device(st
->map
), st
->hw
->fifo_size
, GFP_KERNEL
);
1749 if (st
->data
== NULL
)
1752 /* check chip self-identification */
1753 result
= regmap_read(st
->map
, INV_MPU6050_REG_WHOAMI
, ®val
);
1756 if (regval
!= st
->hw
->whoami
) {
1757 /* check whoami against all possible values */
1758 for (i
= 0; i
< INV_NUM_PARTS
; ++i
) {
1759 if (regval
== hw_info
[i
].whoami
) {
1760 dev_warn(regmap_get_device(st
->map
),
1761 "whoami mismatch got 0x%02x (%s) expected 0x%02x (%s)\n",
1762 regval
, hw_info
[i
].name
,
1763 st
->hw
->whoami
, st
->hw
->name
);
1767 if (i
>= INV_NUM_PARTS
) {
1768 dev_err(regmap_get_device(st
->map
),
1769 "invalid whoami 0x%02x expected 0x%02x (%s)\n",
1770 regval
, st
->hw
->whoami
, st
->hw
->name
);
1775 /* reset to make sure previous state are not there */
1776 result
= regmap_write(st
->map
, st
->reg
->pwr_mgmt_1
,
1777 INV_MPU6050_BIT_H_RESET
);
1780 msleep(INV_MPU6050_POWER_UP_TIME
);
1781 switch (st
->chip_type
) {
1788 /* reset signal path (required for spi connection) */
1789 regval
= INV_MPU6050_BIT_TEMP_RST
| INV_MPU6050_BIT_ACCEL_RST
|
1790 INV_MPU6050_BIT_GYRO_RST
;
1791 result
= regmap_write(st
->map
, INV_MPU6050_REG_SIGNAL_PATH_RESET
,
1795 msleep(INV_MPU6050_POWER_UP_TIME
);
1802 * Turn power on. After reset, the sleep bit could be on
1803 * or off depending on the OTP settings. Turning power on
1804 * make it in a definite state as well as making the hardware
1805 * state align with the software state
1807 result
= inv_mpu6050_set_power_itg(st
, true);
1810 mask
= INV_MPU6050_SENSOR_ACCL
| INV_MPU6050_SENSOR_GYRO
|
1811 INV_MPU6050_SENSOR_TEMP
| INV_MPU6050_SENSOR_MAGN
;
1812 result
= inv_mpu6050_switch_engine(st
, false, mask
);
1814 goto error_power_off
;
1819 inv_mpu6050_set_power_itg(st
, false);
1823 static int inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state
*st
)
1827 result
= regulator_enable(st
->vddio_supply
);
1829 dev_err(regmap_get_device(st
->map
),
1830 "Failed to enable vddio regulator: %d\n", result
);
1832 /* Give the device a little bit of time to start up. */
1833 usleep_range(3000, 5000);
1839 static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state
*st
)
1843 result
= regulator_disable(st
->vddio_supply
);
1845 dev_err(regmap_get_device(st
->map
),
1846 "Failed to disable vddio regulator: %d\n", result
);
1851 static void inv_mpu_core_disable_regulator_action(void *_data
)
1853 struct inv_mpu6050_state
*st
= _data
;
1856 result
= regulator_disable(st
->vdd_supply
);
1858 dev_err(regmap_get_device(st
->map
),
1859 "Failed to disable vdd regulator: %d\n", result
);
1861 inv_mpu_core_disable_regulator_vddio(st
);
1864 static void inv_mpu_pm_disable(void *data
)
1866 struct device
*dev
= data
;
1868 pm_runtime_disable(dev
);
1871 int inv_mpu_core_probe(struct regmap
*regmap
, int irq
, const char *name
,
1872 int (*inv_mpu_bus_setup
)(struct iio_dev
*), int chip_type
)
1874 struct inv_mpu6050_state
*st
;
1875 struct iio_dev
*indio_dev
;
1876 struct inv_mpu6050_platform_data
*pdata
;
1877 struct device
*dev
= regmap_get_device(regmap
);
1881 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
1885 BUILD_BUG_ON(ARRAY_SIZE(hw_info
) != INV_NUM_PARTS
);
1886 if (chip_type
< 0 || chip_type
>= INV_NUM_PARTS
) {
1887 dev_err(dev
, "Bad invensense chip_type=%d name=%s\n",
1891 st
= iio_priv(indio_dev
);
1892 mutex_init(&st
->lock
);
1893 st
->chip_type
= chip_type
;
1897 st
->level_shifter
= device_property_read_bool(dev
,
1898 "invensense,level-shifter");
1899 pdata
= dev_get_platdata(dev
);
1901 result
= iio_read_mount_matrix(dev
, &st
->orientation
);
1903 dev_err(dev
, "Failed to retrieve mounting matrix %d\n",
1908 st
->plat_data
= *pdata
;
1912 irq_type
= irq_get_trigger_type(irq
);
1914 irq_type
= IRQF_TRIGGER_RISING
;
1916 /* Doesn't really matter, use the default */
1917 irq_type
= IRQF_TRIGGER_RISING
;
1920 if (irq_type
& IRQF_TRIGGER_RISING
) // rising or both-edge
1921 st
->irq_mask
= INV_MPU6050_ACTIVE_HIGH
;
1922 else if (irq_type
== IRQF_TRIGGER_FALLING
)
1923 st
->irq_mask
= INV_MPU6050_ACTIVE_LOW
;
1924 else if (irq_type
== IRQF_TRIGGER_HIGH
)
1925 st
->irq_mask
= INV_MPU6050_ACTIVE_HIGH
|
1926 INV_MPU6050_LATCH_INT_EN
;
1927 else if (irq_type
== IRQF_TRIGGER_LOW
)
1928 st
->irq_mask
= INV_MPU6050_ACTIVE_LOW
|
1929 INV_MPU6050_LATCH_INT_EN
;
1931 dev_err(dev
, "Invalid interrupt type 0x%x specified\n",
1935 device_set_wakeup_capable(dev
, true);
1937 st
->vdd_supply
= devm_regulator_get(dev
, "vdd");
1938 if (IS_ERR(st
->vdd_supply
))
1939 return dev_err_probe(dev
, PTR_ERR(st
->vdd_supply
),
1940 "Failed to get vdd regulator\n");
1942 st
->vddio_supply
= devm_regulator_get(dev
, "vddio");
1943 if (IS_ERR(st
->vddio_supply
))
1944 return dev_err_probe(dev
, PTR_ERR(st
->vddio_supply
),
1945 "Failed to get vddio regulator\n");
1947 result
= regulator_enable(st
->vdd_supply
);
1949 dev_err(dev
, "Failed to enable vdd regulator: %d\n", result
);
1952 msleep(INV_MPU6050_POWER_UP_TIME
);
1954 result
= inv_mpu_core_enable_regulator_vddio(st
);
1956 regulator_disable(st
->vdd_supply
);
1960 result
= devm_add_action_or_reset(dev
, inv_mpu_core_disable_regulator_action
,
1963 dev_err(dev
, "Failed to setup regulator cleanup action %d\n",
1968 /* fill magnetometer orientation */
1969 result
= inv_mpu_magn_set_orient(st
);
1973 /* power is turned on inside check chip type*/
1974 result
= inv_check_and_setup_chip(st
);
1978 result
= inv_mpu6050_init_config(indio_dev
);
1980 dev_err(dev
, "Could not initialize device.\n");
1981 goto error_power_off
;
1984 dev_set_drvdata(dev
, indio_dev
);
1985 /* name will be NULL when enumerated via ACPI */
1987 indio_dev
->name
= name
;
1989 indio_dev
->name
= dev_name(dev
);
1991 /* requires parent device set in indio_dev */
1992 if (inv_mpu_bus_setup
) {
1993 result
= inv_mpu_bus_setup(indio_dev
);
1995 goto error_power_off
;
1998 /* chip init is done, turning on runtime power management */
1999 result
= pm_runtime_set_active(dev
);
2001 goto error_power_off
;
2002 pm_runtime_get_noresume(dev
);
2003 pm_runtime_enable(dev
);
2004 pm_runtime_set_autosuspend_delay(dev
, INV_MPU6050_SUSPEND_DELAY_MS
);
2005 pm_runtime_use_autosuspend(dev
);
2006 pm_runtime_put(dev
);
2007 result
= devm_add_action_or_reset(dev
, inv_mpu_pm_disable
, dev
);
2011 switch (chip_type
) {
2014 indio_dev
->channels
= inv_mpu6050_channels
;
2015 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu6050_channels
);
2016 indio_dev
->available_scan_masks
= inv_mpu_scan_masks
;
2019 indio_dev
->channels
= inv_mpu9150_channels
;
2020 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu9150_channels
);
2021 indio_dev
->available_scan_masks
= inv_mpu9x50_scan_masks
;
2025 indio_dev
->channels
= inv_mpu9250_channels
;
2026 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu9250_channels
);
2027 indio_dev
->available_scan_masks
= inv_mpu9x50_scan_masks
;
2031 indio_dev
->channels
= inv_mpu6500_channels
;
2032 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu6500_channels
);
2033 indio_dev
->available_scan_masks
= inv_icm20602_scan_masks
;
2036 indio_dev
->channels
= inv_mpu6500_channels
;
2037 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu6500_channels
);
2038 indio_dev
->available_scan_masks
= inv_mpu_scan_masks
;
2042 * Use magnetometer inside the chip only if there is no i2c
2043 * auxiliary device in use. Otherwise Going back to 6-axis only.
2045 if (st
->magn_disabled
) {
2046 switch (chip_type
) {
2048 indio_dev
->channels
= inv_mpu6050_channels
;
2049 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu6050_channels
);
2050 indio_dev
->available_scan_masks
= inv_mpu_scan_masks
;
2053 indio_dev
->channels
= inv_mpu6500_channels
;
2054 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu6500_channels
);
2055 indio_dev
->available_scan_masks
= inv_mpu_scan_masks
;
2060 indio_dev
->info
= &mpu_info
;
2064 * The driver currently only supports buffered capture with its
2065 * own trigger. So no IRQ, no trigger, no buffer
2067 result
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
2068 iio_pollfunc_store_time
,
2069 inv_mpu6050_read_fifo
,
2072 dev_err(dev
, "configure buffer fail %d\n", result
);
2076 result
= inv_mpu6050_probe_trigger(indio_dev
, irq_type
);
2078 dev_err(dev
, "trigger probe fail %d\n", result
);
2083 result
= devm_iio_device_register(dev
, indio_dev
);
2085 dev_err(dev
, "IIO register fail %d\n", result
);
2092 inv_mpu6050_set_power_itg(st
, false);
2095 EXPORT_SYMBOL_NS_GPL(inv_mpu_core_probe
, "IIO_MPU6050");
2097 static int inv_mpu_resume(struct device
*dev
)
2099 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2100 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
2104 guard(mutex
)(&st
->lock
);
2106 wakeup
= device_may_wakeup(dev
) && st
->chip_config
.wom_en
;
2109 enable_irq(st
->irq
);
2110 disable_irq_wake(st
->irq
);
2111 result
= inv_mpu6050_set_wom_lp(st
, false);
2115 result
= inv_mpu_core_enable_regulator_vddio(st
);
2118 result
= inv_mpu6050_set_power_itg(st
, true);
2123 pm_runtime_disable(dev
);
2124 pm_runtime_set_active(dev
);
2125 pm_runtime_enable(dev
);
2127 result
= inv_mpu6050_switch_engine(st
, true, st
->suspended_sensors
);
2131 if (st
->chip_config
.wom_en
&& !wakeup
) {
2132 result
= inv_mpu6050_set_wom_int(st
, true);
2137 if (iio_buffer_enabled(indio_dev
))
2138 result
= inv_mpu6050_prepare_fifo(st
, true);
2143 static int inv_mpu_suspend(struct device
*dev
)
2145 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2146 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
2150 guard(mutex
)(&st
->lock
);
2152 st
->suspended_sensors
= 0;
2153 if (pm_runtime_suspended(dev
))
2156 if (iio_buffer_enabled(indio_dev
)) {
2157 result
= inv_mpu6050_prepare_fifo(st
, false);
2162 wakeup
= device_may_wakeup(dev
) && st
->chip_config
.wom_en
;
2164 if (st
->chip_config
.wom_en
&& !wakeup
) {
2165 result
= inv_mpu6050_set_wom_int(st
, false);
2170 if (st
->chip_config
.accl_en
&& !wakeup
)
2171 st
->suspended_sensors
|= INV_MPU6050_SENSOR_ACCL
;
2172 if (st
->chip_config
.gyro_en
)
2173 st
->suspended_sensors
|= INV_MPU6050_SENSOR_GYRO
;
2174 if (st
->chip_config
.temp_en
)
2175 st
->suspended_sensors
|= INV_MPU6050_SENSOR_TEMP
;
2176 if (st
->chip_config
.magn_en
)
2177 st
->suspended_sensors
|= INV_MPU6050_SENSOR_MAGN
;
2178 if (st
->chip_config
.wom_en
&& !wakeup
)
2179 st
->suspended_sensors
|= INV_MPU6050_SENSOR_WOM
;
2180 result
= inv_mpu6050_switch_engine(st
, false, st
->suspended_sensors
);
2185 result
= inv_mpu6050_set_wom_lp(st
, true);
2188 enable_irq_wake(st
->irq
);
2189 disable_irq(st
->irq
);
2191 result
= inv_mpu6050_set_power_itg(st
, false);
2194 inv_mpu_core_disable_regulator_vddio(st
);
2200 static int inv_mpu_runtime_suspend(struct device
*dev
)
2202 struct inv_mpu6050_state
*st
= iio_priv(dev_get_drvdata(dev
));
2203 unsigned int sensors
;
2206 mutex_lock(&st
->lock
);
2208 sensors
= INV_MPU6050_SENSOR_ACCL
| INV_MPU6050_SENSOR_GYRO
|
2209 INV_MPU6050_SENSOR_TEMP
| INV_MPU6050_SENSOR_MAGN
|
2210 INV_MPU6050_SENSOR_WOM
;
2211 ret
= inv_mpu6050_switch_engine(st
, false, sensors
);
2215 ret
= inv_mpu6050_set_power_itg(st
, false);
2219 inv_mpu_core_disable_regulator_vddio(st
);
2222 mutex_unlock(&st
->lock
);
2226 static int inv_mpu_runtime_resume(struct device
*dev
)
2228 struct inv_mpu6050_state
*st
= iio_priv(dev_get_drvdata(dev
));
2231 ret
= inv_mpu_core_enable_regulator_vddio(st
);
2235 return inv_mpu6050_set_power_itg(st
, true);
2238 EXPORT_NS_GPL_DEV_PM_OPS(inv_mpu_pmops
, IIO_MPU6050
) = {
2239 SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend
, inv_mpu_resume
)
2240 RUNTIME_PM_OPS(inv_mpu_runtime_suspend
, inv_mpu_runtime_resume
, NULL
)
2243 MODULE_AUTHOR("Invensense Corporation");
2244 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
2245 MODULE_LICENSE("GPL");
2246 MODULE_IMPORT_NS("IIO_INV_SENSORS_TIMESTAMP");