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/iio/iio.h>
16 #include <linux/acpi.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
20 #include <linux/pm_runtime.h>
21 #include "inv_mpu_iio.h"
22 #include "inv_mpu_magn.h"
25 * this is the gyro scale translated from dynamic range plus/minus
26 * {250, 500, 1000, 2000} to rad/s
28 static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
31 * this is the accel scale translated from dynamic range plus/minus
32 * {2, 4, 8, 16} to m/s^2
34 static const int accel_scale[] = {598, 1196, 2392, 4785};
36 static const struct inv_mpu6050_reg_map reg_set_icm20602 = {
37 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
38 .lpf = INV_MPU6050_REG_CONFIG,
39 .accel_lpf = INV_MPU6500_REG_ACCEL_CONFIG_2,
40 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
41 .fifo_en = INV_MPU6050_REG_FIFO_EN,
42 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
43 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
44 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
45 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
46 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
47 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
48 .temperature = INV_MPU6050_REG_TEMPERATURE,
49 .int_enable = INV_MPU6050_REG_INT_ENABLE,
50 .int_status = INV_MPU6050_REG_INT_STATUS,
51 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
52 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
53 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
54 .accl_offset = INV_MPU6500_REG_ACCEL_OFFSET,
55 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
56 .i2c_if = INV_ICM20602_REG_I2C_IF,
59 static const struct inv_mpu6050_reg_map reg_set_6500 = {
60 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
61 .lpf = INV_MPU6050_REG_CONFIG,
62 .accel_lpf = INV_MPU6500_REG_ACCEL_CONFIG_2,
63 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
64 .fifo_en = INV_MPU6050_REG_FIFO_EN,
65 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
66 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
67 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
68 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
69 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
70 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
71 .temperature = INV_MPU6050_REG_TEMPERATURE,
72 .int_enable = INV_MPU6050_REG_INT_ENABLE,
73 .int_status = INV_MPU6050_REG_INT_STATUS,
74 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
75 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
76 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
77 .accl_offset = INV_MPU6500_REG_ACCEL_OFFSET,
78 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
82 static const struct inv_mpu6050_reg_map reg_set_6050 = {
83 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
84 .lpf = INV_MPU6050_REG_CONFIG,
85 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
86 .fifo_en = INV_MPU6050_REG_FIFO_EN,
87 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
88 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
89 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
90 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
91 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
92 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
93 .temperature = INV_MPU6050_REG_TEMPERATURE,
94 .int_enable = INV_MPU6050_REG_INT_ENABLE,
95 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
96 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
97 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
98 .accl_offset = INV_MPU6050_REG_ACCEL_OFFSET,
99 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
103 static const struct inv_mpu6050_chip_config chip_config_6050 = {
104 .clk = INV_CLK_INTERNAL,
105 .fsr = INV_MPU6050_FSR_2000DPS,
106 .lpf = INV_MPU6050_FILTER_20HZ,
107 .divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
112 .gyro_fifo_enable = false,
113 .accl_fifo_enable = false,
114 .temp_fifo_enable = false,
115 .magn_fifo_enable = false,
116 .accl_fs = INV_MPU6050_FS_02G,
120 static const struct inv_mpu6050_chip_config chip_config_6500 = {
122 .fsr = INV_MPU6050_FSR_2000DPS,
123 .lpf = INV_MPU6050_FILTER_20HZ,
124 .divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
129 .gyro_fifo_enable = false,
130 .accl_fifo_enable = false,
131 .temp_fifo_enable = false,
132 .magn_fifo_enable = false,
133 .accl_fs = INV_MPU6050_FS_02G,
137 /* Indexed by enum inv_devices */
138 static const struct inv_mpu6050_hw hw_info[] = {
140 .whoami = INV_MPU6050_WHOAMI_VALUE,
142 .reg = ®_set_6050,
143 .config = &chip_config_6050,
145 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
148 .whoami = INV_MPU6500_WHOAMI_VALUE,
150 .reg = ®_set_6500,
151 .config = &chip_config_6500,
153 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
156 .whoami = INV_MPU6515_WHOAMI_VALUE,
158 .reg = ®_set_6500,
159 .config = &chip_config_6500,
161 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
164 .whoami = INV_MPU6880_WHOAMI_VALUE,
166 .reg = ®_set_6500,
167 .config = &chip_config_6500,
169 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
172 .whoami = INV_MPU6000_WHOAMI_VALUE,
174 .reg = ®_set_6050,
175 .config = &chip_config_6050,
177 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
180 .whoami = INV_MPU9150_WHOAMI_VALUE,
182 .reg = ®_set_6050,
183 .config = &chip_config_6050,
185 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
188 .whoami = INV_MPU9250_WHOAMI_VALUE,
190 .reg = ®_set_6500,
191 .config = &chip_config_6500,
193 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
196 .whoami = INV_MPU9255_WHOAMI_VALUE,
198 .reg = ®_set_6500,
199 .config = &chip_config_6500,
201 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
204 .whoami = INV_ICM20608_WHOAMI_VALUE,
206 .reg = ®_set_6500,
207 .config = &chip_config_6500,
209 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
212 .whoami = INV_ICM20609_WHOAMI_VALUE,
214 .reg = ®_set_6500,
215 .config = &chip_config_6500,
216 .fifo_size = 4 * 1024,
217 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
220 .whoami = INV_ICM20689_WHOAMI_VALUE,
222 .reg = ®_set_6500,
223 .config = &chip_config_6500,
224 .fifo_size = 4 * 1024,
225 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
228 .whoami = INV_ICM20602_WHOAMI_VALUE,
230 .reg = ®_set_icm20602,
231 .config = &chip_config_6500,
233 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
236 .whoami = INV_ICM20690_WHOAMI_VALUE,
238 .reg = ®_set_6500,
239 .config = &chip_config_6500,
241 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
244 .whoami = INV_IAM20680_WHOAMI_VALUE,
246 .reg = ®_set_6500,
247 .config = &chip_config_6500,
249 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
253 static int inv_mpu6050_pwr_mgmt_1_write(struct inv_mpu6050_state *st, bool sleep,
254 int clock, int temp_dis)
259 clock = st->chip_config.clk;
261 temp_dis = !st->chip_config.temp_en;
263 val = clock & INV_MPU6050_BIT_CLK_MASK;
265 val |= INV_MPU6050_BIT_TEMP_DIS;
267 val |= INV_MPU6050_BIT_SLEEP;
269 dev_dbg(regmap_get_device(st->map), "pwr_mgmt_1: 0x%x\n", val);
270 return regmap_write(st->map, st->reg->pwr_mgmt_1, val);
273 static int inv_mpu6050_clock_switch(struct inv_mpu6050_state *st,
278 switch (st->chip_type) {
282 /* old chips: switch clock manually */
283 ret = inv_mpu6050_pwr_mgmt_1_write(st, false, clock, -1);
286 st->chip_config.clk = clock;
289 /* automatic clock switching, nothing to do */
296 int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en,
300 u8 pwr_mgmt2, user_ctrl;
303 /* delete useless requests */
304 if (mask & INV_MPU6050_SENSOR_ACCL && en == st->chip_config.accl_en)
305 mask &= ~INV_MPU6050_SENSOR_ACCL;
306 if (mask & INV_MPU6050_SENSOR_GYRO && en == st->chip_config.gyro_en)
307 mask &= ~INV_MPU6050_SENSOR_GYRO;
308 if (mask & INV_MPU6050_SENSOR_TEMP && en == st->chip_config.temp_en)
309 mask &= ~INV_MPU6050_SENSOR_TEMP;
310 if (mask & INV_MPU6050_SENSOR_MAGN && en == st->chip_config.magn_en)
311 mask &= ~INV_MPU6050_SENSOR_MAGN;
315 /* turn on/off temperature sensor */
316 if (mask & INV_MPU6050_SENSOR_TEMP) {
317 ret = inv_mpu6050_pwr_mgmt_1_write(st, false, -1, !en);
320 st->chip_config.temp_en = en;
323 /* update user_crtl for driving magnetometer */
324 if (mask & INV_MPU6050_SENSOR_MAGN) {
325 user_ctrl = st->chip_config.user_ctrl;
327 user_ctrl |= INV_MPU6050_BIT_I2C_MST_EN;
329 user_ctrl &= ~INV_MPU6050_BIT_I2C_MST_EN;
330 ret = regmap_write(st->map, st->reg->user_ctrl, user_ctrl);
333 st->chip_config.user_ctrl = user_ctrl;
334 st->chip_config.magn_en = en;
337 /* manage accel & gyro engines */
338 if (mask & (INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO)) {
339 /* compute power management 2 current value */
341 if (!st->chip_config.accl_en)
342 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_ACCL_STBY;
343 if (!st->chip_config.gyro_en)
344 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_GYRO_STBY;
346 /* update to new requested value */
347 if (mask & INV_MPU6050_SENSOR_ACCL) {
349 pwr_mgmt2 &= ~INV_MPU6050_BIT_PWR_ACCL_STBY;
351 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_ACCL_STBY;
353 if (mask & INV_MPU6050_SENSOR_GYRO) {
355 pwr_mgmt2 &= ~INV_MPU6050_BIT_PWR_GYRO_STBY;
357 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_GYRO_STBY;
360 /* switch clock to internal when turning gyro off */
361 if (mask & INV_MPU6050_SENSOR_GYRO && !en) {
362 ret = inv_mpu6050_clock_switch(st, INV_CLK_INTERNAL);
367 /* update sensors engine */
368 dev_dbg(regmap_get_device(st->map), "pwr_mgmt_2: 0x%x\n",
370 ret = regmap_write(st->map, st->reg->pwr_mgmt_2, pwr_mgmt2);
373 if (mask & INV_MPU6050_SENSOR_ACCL)
374 st->chip_config.accl_en = en;
375 if (mask & INV_MPU6050_SENSOR_GYRO)
376 st->chip_config.gyro_en = en;
378 /* compute required time to have sensors stabilized */
381 if (mask & INV_MPU6050_SENSOR_ACCL) {
382 if (sleep < INV_MPU6050_ACCEL_UP_TIME)
383 sleep = INV_MPU6050_ACCEL_UP_TIME;
385 if (mask & INV_MPU6050_SENSOR_GYRO) {
386 if (sleep < INV_MPU6050_GYRO_UP_TIME)
387 sleep = INV_MPU6050_GYRO_UP_TIME;
390 if (mask & INV_MPU6050_SENSOR_GYRO) {
391 if (sleep < INV_MPU6050_GYRO_DOWN_TIME)
392 sleep = INV_MPU6050_GYRO_DOWN_TIME;
398 /* switch clock to PLL when turning gyro on */
399 if (mask & INV_MPU6050_SENSOR_GYRO && en) {
400 ret = inv_mpu6050_clock_switch(st, INV_CLK_PLL);
409 static int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st,
414 result = inv_mpu6050_pwr_mgmt_1_write(st, !power_on, -1, -1);
419 usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
420 INV_MPU6050_REG_UP_TIME_MAX);
425 static int inv_mpu6050_set_gyro_fsr(struct inv_mpu6050_state *st,
426 enum inv_mpu6050_fsr_e val)
428 unsigned int gyro_shift;
431 switch (st->chip_type) {
433 gyro_shift = INV_ICM20690_GYRO_CONFIG_FSR_SHIFT;
436 gyro_shift = INV_MPU6050_GYRO_CONFIG_FSR_SHIFT;
440 data = val << gyro_shift;
441 return regmap_write(st->map, st->reg->gyro_config, data);
445 * inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent
447 * MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope
448 * MPU6500 and above have a dedicated register for accelerometer
450 static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st,
451 enum inv_mpu6050_filter_e val)
455 result = regmap_write(st->map, st->reg->lpf, val);
460 switch (st->chip_type) {
464 /* old chips, nothing to do */
468 /* set FIFO size to maximum value */
469 val |= INV_ICM20689_BITS_FIFO_SIZE_MAX;
475 return regmap_write(st->map, st->reg->accel_lpf, val);
479 * inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
481 * Initial configuration:
485 * Clock source: Gyro PLL
487 static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
491 struct inv_mpu6050_state *st = iio_priv(indio_dev);
493 result = inv_mpu6050_set_gyro_fsr(st, st->chip_config.fsr);
497 result = inv_mpu6050_set_lpf_regs(st, st->chip_config.lpf);
501 d = st->chip_config.divider;
502 result = regmap_write(st->map, st->reg->sample_rate_div, d);
506 d = (st->chip_config.accl_fs << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
507 result = regmap_write(st->map, st->reg->accl_config, d);
511 result = regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
516 * Internal chip period is 1ms (1kHz).
517 * Let's use at the beginning the theorical value before measuring
518 * with interrupt timestamps.
520 st->chip_period = NSEC_PER_MSEC;
522 /* magn chip init, noop if not present in the chip */
523 result = inv_mpu_magn_probe(st);
530 static int inv_mpu6050_sensor_set(struct inv_mpu6050_state *st, int reg,
534 __be16 d = cpu_to_be16(val);
536 ind = (axis - IIO_MOD_X) * 2;
537 result = regmap_bulk_write(st->map, reg + ind, &d, sizeof(d));
544 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state *st, int reg,
550 ind = (axis - IIO_MOD_X) * 2;
551 result = regmap_bulk_read(st->map, reg + ind, &d, sizeof(d));
554 *val = (short)be16_to_cpup(&d);
559 static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
560 struct iio_chan_spec const *chan,
563 struct inv_mpu6050_state *st = iio_priv(indio_dev);
564 struct device *pdev = regmap_get_device(st->map);
565 unsigned int freq_hz, period_us, min_sleep_us, max_sleep_us;
569 /* compute sample period */
570 freq_hz = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
571 period_us = 1000000 / freq_hz;
573 result = pm_runtime_get_sync(pdev);
575 pm_runtime_put_noidle(pdev);
579 switch (chan->type) {
581 if (!st->chip_config.gyro_en) {
582 result = inv_mpu6050_switch_engine(st, true,
583 INV_MPU6050_SENSOR_GYRO);
585 goto error_power_off;
586 /* need to wait 2 periods to have first valid sample */
587 min_sleep_us = 2 * period_us;
588 max_sleep_us = 2 * (period_us + period_us / 2);
589 usleep_range(min_sleep_us, max_sleep_us);
591 ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
592 chan->channel2, val);
595 if (!st->chip_config.accl_en) {
596 result = inv_mpu6050_switch_engine(st, true,
597 INV_MPU6050_SENSOR_ACCL);
599 goto error_power_off;
600 /* wait 1 period for first sample availability */
601 min_sleep_us = period_us;
602 max_sleep_us = period_us + period_us / 2;
603 usleep_range(min_sleep_us, max_sleep_us);
605 ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
606 chan->channel2, val);
609 /* temperature sensor work only with accel and/or gyro */
610 if (!st->chip_config.accl_en && !st->chip_config.gyro_en) {
612 goto error_power_off;
614 if (!st->chip_config.temp_en) {
615 result = inv_mpu6050_switch_engine(st, true,
616 INV_MPU6050_SENSOR_TEMP);
618 goto error_power_off;
619 /* wait 1 period for first sample availability */
620 min_sleep_us = period_us;
621 max_sleep_us = period_us + period_us / 2;
622 usleep_range(min_sleep_us, max_sleep_us);
624 ret = inv_mpu6050_sensor_show(st, st->reg->temperature,
628 if (!st->chip_config.magn_en) {
629 result = inv_mpu6050_switch_engine(st, true,
630 INV_MPU6050_SENSOR_MAGN);
632 goto error_power_off;
633 /* frequency is limited for magnetometer */
634 if (freq_hz > INV_MPU_MAGN_FREQ_HZ_MAX) {
635 freq_hz = INV_MPU_MAGN_FREQ_HZ_MAX;
636 period_us = 1000000 / freq_hz;
638 /* need to wait 2 periods to have first valid sample */
639 min_sleep_us = 2 * period_us;
640 max_sleep_us = 2 * (period_us + period_us / 2);
641 usleep_range(min_sleep_us, max_sleep_us);
643 ret = inv_mpu_magn_read(st, chan->channel2, val);
650 pm_runtime_mark_last_busy(pdev);
651 pm_runtime_put_autosuspend(pdev);
656 pm_runtime_put_autosuspend(pdev);
661 inv_mpu6050_read_raw(struct iio_dev *indio_dev,
662 struct iio_chan_spec const *chan,
663 int *val, int *val2, long mask)
665 struct inv_mpu6050_state *st = iio_priv(indio_dev);
669 case IIO_CHAN_INFO_RAW:
670 ret = iio_device_claim_direct_mode(indio_dev);
673 mutex_lock(&st->lock);
674 ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
675 mutex_unlock(&st->lock);
676 iio_device_release_direct_mode(indio_dev);
678 case IIO_CHAN_INFO_SCALE:
679 switch (chan->type) {
681 mutex_lock(&st->lock);
683 *val2 = gyro_scale_6050[st->chip_config.fsr];
684 mutex_unlock(&st->lock);
686 return IIO_VAL_INT_PLUS_NANO;
688 mutex_lock(&st->lock);
690 *val2 = accel_scale[st->chip_config.accl_fs];
691 mutex_unlock(&st->lock);
693 return IIO_VAL_INT_PLUS_MICRO;
695 *val = st->hw->temp.scale / 1000000;
696 *val2 = st->hw->temp.scale % 1000000;
697 return IIO_VAL_INT_PLUS_MICRO;
699 return inv_mpu_magn_get_scale(st, chan, val, val2);
703 case IIO_CHAN_INFO_OFFSET:
704 switch (chan->type) {
706 *val = st->hw->temp.offset;
711 case IIO_CHAN_INFO_CALIBBIAS:
712 switch (chan->type) {
714 mutex_lock(&st->lock);
715 ret = inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
716 chan->channel2, val);
717 mutex_unlock(&st->lock);
720 mutex_lock(&st->lock);
721 ret = inv_mpu6050_sensor_show(st, st->reg->accl_offset,
722 chan->channel2, val);
723 mutex_unlock(&st->lock);
734 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state *st, int val)
738 for (i = 0; i < ARRAY_SIZE(gyro_scale_6050); ++i) {
739 if (gyro_scale_6050[i] == val) {
740 result = inv_mpu6050_set_gyro_fsr(st, i);
744 st->chip_config.fsr = i;
752 static int inv_write_raw_get_fmt(struct iio_dev *indio_dev,
753 struct iio_chan_spec const *chan, long mask)
756 case IIO_CHAN_INFO_SCALE:
757 switch (chan->type) {
759 return IIO_VAL_INT_PLUS_NANO;
761 return IIO_VAL_INT_PLUS_MICRO;
764 return IIO_VAL_INT_PLUS_MICRO;
770 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state *st, int val)
775 for (i = 0; i < ARRAY_SIZE(accel_scale); ++i) {
776 if (accel_scale[i] == val) {
777 d = (i << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
778 result = regmap_write(st->map, st->reg->accl_config, d);
782 st->chip_config.accl_fs = i;
790 static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
791 struct iio_chan_spec const *chan,
792 int val, int val2, long mask)
794 struct inv_mpu6050_state *st = iio_priv(indio_dev);
795 struct device *pdev = regmap_get_device(st->map);
799 * we should only update scale when the chip is disabled, i.e.
802 result = iio_device_claim_direct_mode(indio_dev);
806 mutex_lock(&st->lock);
807 result = pm_runtime_get_sync(pdev);
809 pm_runtime_put_noidle(pdev);
810 goto error_write_raw_unlock;
814 case IIO_CHAN_INFO_SCALE:
815 switch (chan->type) {
817 result = inv_mpu6050_write_gyro_scale(st, val2);
820 result = inv_mpu6050_write_accel_scale(st, val2);
827 case IIO_CHAN_INFO_CALIBBIAS:
828 switch (chan->type) {
830 result = inv_mpu6050_sensor_set(st,
831 st->reg->gyro_offset,
832 chan->channel2, val);
835 result = inv_mpu6050_sensor_set(st,
836 st->reg->accl_offset,
837 chan->channel2, val);
849 pm_runtime_mark_last_busy(pdev);
850 pm_runtime_put_autosuspend(pdev);
851 error_write_raw_unlock:
852 mutex_unlock(&st->lock);
853 iio_device_release_direct_mode(indio_dev);
859 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
861 * Based on the Nyquist principle, the bandwidth of the low
862 * pass filter must not exceed the signal sampling rate divided
863 * by 2, or there would be aliasing.
864 * This function basically search for the correct low pass
865 * parameters based on the fifo rate, e.g, sampling frequency.
867 * lpf is set automatically when setting sampling rate to avoid any aliases.
869 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
871 static const int hz[] = {400, 200, 90, 40, 20, 10};
872 static const int d[] = {
873 INV_MPU6050_FILTER_200HZ, INV_MPU6050_FILTER_100HZ,
874 INV_MPU6050_FILTER_45HZ, INV_MPU6050_FILTER_20HZ,
875 INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ
880 data = INV_MPU6050_FILTER_5HZ;
881 for (i = 0; i < ARRAY_SIZE(hz); ++i) {
887 result = inv_mpu6050_set_lpf_regs(st, data);
890 st->chip_config.lpf = data;
896 * inv_mpu6050_fifo_rate_store() - Set fifo rate.
899 inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
900 const char *buf, size_t count)
905 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
906 struct inv_mpu6050_state *st = iio_priv(indio_dev);
907 struct device *pdev = regmap_get_device(st->map);
909 if (kstrtoint(buf, 10, &fifo_rate))
911 if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
912 fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
915 /* compute the chip sample rate divider */
916 d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate);
917 /* compute back the fifo rate to handle truncation cases */
918 fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(d);
920 mutex_lock(&st->lock);
921 if (d == st->chip_config.divider) {
923 goto fifo_rate_fail_unlock;
925 result = pm_runtime_get_sync(pdev);
927 pm_runtime_put_noidle(pdev);
928 goto fifo_rate_fail_unlock;
931 result = regmap_write(st->map, st->reg->sample_rate_div, d);
933 goto fifo_rate_fail_power_off;
934 st->chip_config.divider = d;
936 result = inv_mpu6050_set_lpf(st, fifo_rate);
938 goto fifo_rate_fail_power_off;
940 /* update rate for magn, noop if not present in chip */
941 result = inv_mpu_magn_set_rate(st, fifo_rate);
943 goto fifo_rate_fail_power_off;
945 pm_runtime_mark_last_busy(pdev);
946 fifo_rate_fail_power_off:
947 pm_runtime_put_autosuspend(pdev);
948 fifo_rate_fail_unlock:
949 mutex_unlock(&st->lock);
957 * inv_fifo_rate_show() - Get the current sampling rate.
960 inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
963 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
966 mutex_lock(&st->lock);
967 fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
968 mutex_unlock(&st->lock);
970 return scnprintf(buf, PAGE_SIZE, "%u\n", fifo_rate);
974 * inv_attr_show() - calling this function will show current
977 * Deprecated in favor of IIO mounting matrix API.
979 * See inv_get_mount_matrix()
981 static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr,
984 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
985 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
988 switch (this_attr->address) {
990 * In MPU6050, the two matrix are the same because gyro and accel
991 * are integrated in one chip
993 case ATTR_GYRO_MATRIX:
994 case ATTR_ACCL_MATRIX:
995 m = st->plat_data.orientation;
997 return scnprintf(buf, PAGE_SIZE,
998 "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
999 m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1006 * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
1008 * @indio_dev: The IIO device
1009 * @trig: The new trigger
1011 * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
1012 * device, -EINVAL otherwise.
1014 static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
1015 struct iio_trigger *trig)
1017 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1019 if (st->trig != trig)
1025 static const struct iio_mount_matrix *
1026 inv_get_mount_matrix(const struct iio_dev *indio_dev,
1027 const struct iio_chan_spec *chan)
1029 struct inv_mpu6050_state *data = iio_priv(indio_dev);
1030 const struct iio_mount_matrix *matrix;
1032 if (chan->type == IIO_MAGN)
1033 matrix = &data->magn_orient;
1035 matrix = &data->orientation;
1040 static const struct iio_chan_spec_ext_info inv_ext_info[] = {
1041 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix),
1045 #define INV_MPU6050_CHAN(_type, _channel2, _index) \
1049 .channel2 = _channel2, \
1050 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1051 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1052 BIT(IIO_CHAN_INFO_CALIBBIAS), \
1053 .scan_index = _index, \
1057 .storagebits = 16, \
1059 .endianness = IIO_BE, \
1061 .ext_info = inv_ext_info, \
1064 #define INV_MPU6050_TEMP_CHAN(_index) \
1067 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
1068 | BIT(IIO_CHAN_INFO_OFFSET) \
1069 | BIT(IIO_CHAN_INFO_SCALE), \
1070 .scan_index = _index, \
1074 .storagebits = 16, \
1076 .endianness = IIO_BE, \
1080 static const struct iio_chan_spec inv_mpu_channels[] = {
1081 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
1083 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1085 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1086 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1087 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1089 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1090 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1091 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1094 #define INV_MPU6050_SCAN_MASK_3AXIS_ACCEL \
1095 (BIT(INV_MPU6050_SCAN_ACCL_X) \
1096 | BIT(INV_MPU6050_SCAN_ACCL_Y) \
1097 | BIT(INV_MPU6050_SCAN_ACCL_Z))
1099 #define INV_MPU6050_SCAN_MASK_3AXIS_GYRO \
1100 (BIT(INV_MPU6050_SCAN_GYRO_X) \
1101 | BIT(INV_MPU6050_SCAN_GYRO_Y) \
1102 | BIT(INV_MPU6050_SCAN_GYRO_Z))
1104 #define INV_MPU6050_SCAN_MASK_TEMP (BIT(INV_MPU6050_SCAN_TEMP))
1106 static const unsigned long inv_mpu_scan_masks[] = {
1108 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
1109 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1111 INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1112 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1113 /* 6-axis accel + gyro */
1114 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1115 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1116 | INV_MPU6050_SCAN_MASK_TEMP,
1120 #define INV_MPU9X50_MAGN_CHAN(_chan2, _bits, _index) \
1124 .channel2 = _chan2, \
1125 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) | \
1126 BIT(IIO_CHAN_INFO_RAW), \
1127 .scan_index = _index, \
1130 .realbits = _bits, \
1131 .storagebits = 16, \
1133 .endianness = IIO_BE, \
1135 .ext_info = inv_ext_info, \
1138 static const struct iio_chan_spec inv_mpu9150_channels[] = {
1139 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
1141 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1143 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1144 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1145 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1147 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1148 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1149 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1151 /* Magnetometer resolution is 13 bits */
1152 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 13, INV_MPU9X50_SCAN_MAGN_X),
1153 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 13, INV_MPU9X50_SCAN_MAGN_Y),
1154 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 13, INV_MPU9X50_SCAN_MAGN_Z),
1157 static const struct iio_chan_spec inv_mpu9250_channels[] = {
1158 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
1160 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1162 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1163 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1164 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1166 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1167 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1168 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1170 /* Magnetometer resolution is 16 bits */
1171 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 16, INV_MPU9X50_SCAN_MAGN_X),
1172 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 16, INV_MPU9X50_SCAN_MAGN_Y),
1173 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 16, INV_MPU9X50_SCAN_MAGN_Z),
1176 #define INV_MPU9X50_SCAN_MASK_3AXIS_MAGN \
1177 (BIT(INV_MPU9X50_SCAN_MAGN_X) \
1178 | BIT(INV_MPU9X50_SCAN_MAGN_Y) \
1179 | BIT(INV_MPU9X50_SCAN_MAGN_Z))
1181 static const unsigned long inv_mpu9x50_scan_masks[] = {
1183 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
1184 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1186 INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1187 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1189 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1190 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN | INV_MPU6050_SCAN_MASK_TEMP,
1191 /* 6-axis accel + gyro */
1192 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1193 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1194 | INV_MPU6050_SCAN_MASK_TEMP,
1195 /* 6-axis accel + magn */
1196 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1197 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1198 | INV_MPU6050_SCAN_MASK_TEMP,
1199 /* 6-axis gyro + magn */
1200 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1201 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1202 | INV_MPU6050_SCAN_MASK_TEMP,
1203 /* 9-axis accel + gyro + magn */
1204 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1205 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1206 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1207 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1208 | INV_MPU6050_SCAN_MASK_TEMP,
1212 static const unsigned long inv_icm20602_scan_masks[] = {
1213 /* 3-axis accel + temp (mandatory) */
1214 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1215 /* 3-axis gyro + temp (mandatory) */
1216 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1217 /* 6-axis accel + gyro + temp (mandatory) */
1218 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1219 | INV_MPU6050_SCAN_MASK_TEMP,
1224 * The user can choose any frequency between INV_MPU6050_MIN_FIFO_RATE and
1225 * INV_MPU6050_MAX_FIFO_RATE, but only these frequencies are matched by the
1226 * low-pass filter. Specifically, each of these sampling rates are about twice
1227 * the bandwidth of a corresponding low-pass filter, which should eliminate
1228 * aliasing following the Nyquist principle. By picking a frequency different
1229 * from these, the user risks aliasing effects.
1231 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
1232 static IIO_CONST_ATTR(in_anglvel_scale_available,
1233 "0.000133090 0.000266181 0.000532362 0.001064724");
1234 static IIO_CONST_ATTR(in_accel_scale_available,
1235 "0.000598 0.001196 0.002392 0.004785");
1236 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
1237 inv_mpu6050_fifo_rate_store);
1239 /* Deprecated: kept for userspace backward compatibility. */
1240 static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
1242 static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
1245 static struct attribute *inv_attributes[] = {
1246 &iio_dev_attr_in_gyro_matrix.dev_attr.attr, /* deprecated */
1247 &iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */
1248 &iio_dev_attr_sampling_frequency.dev_attr.attr,
1249 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1250 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
1251 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
1255 static const struct attribute_group inv_attribute_group = {
1256 .attrs = inv_attributes
1259 static int inv_mpu6050_reg_access(struct iio_dev *indio_dev,
1261 unsigned int writeval,
1262 unsigned int *readval)
1264 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1267 mutex_lock(&st->lock);
1269 ret = regmap_read(st->map, reg, readval);
1271 ret = regmap_write(st->map, reg, writeval);
1272 mutex_unlock(&st->lock);
1277 static const struct iio_info mpu_info = {
1278 .read_raw = &inv_mpu6050_read_raw,
1279 .write_raw = &inv_mpu6050_write_raw,
1280 .write_raw_get_fmt = &inv_write_raw_get_fmt,
1281 .attrs = &inv_attribute_group,
1282 .validate_trigger = inv_mpu6050_validate_trigger,
1283 .debugfs_reg_access = &inv_mpu6050_reg_access,
1287 * inv_check_and_setup_chip() - check and setup chip.
1289 static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
1292 unsigned int regval, mask;
1295 st->hw = &hw_info[st->chip_type];
1296 st->reg = hw_info[st->chip_type].reg;
1297 memcpy(&st->chip_config, hw_info[st->chip_type].config,
1298 sizeof(st->chip_config));
1300 /* check chip self-identification */
1301 result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, ®val);
1304 if (regval != st->hw->whoami) {
1305 /* check whoami against all possible values */
1306 for (i = 0; i < INV_NUM_PARTS; ++i) {
1307 if (regval == hw_info[i].whoami) {
1308 dev_warn(regmap_get_device(st->map),
1309 "whoami mismatch got %#02x (%s)"
1310 "expected %#02hhx (%s)\n",
1311 regval, hw_info[i].name,
1312 st->hw->whoami, st->hw->name);
1316 if (i >= INV_NUM_PARTS) {
1317 dev_err(regmap_get_device(st->map),
1318 "invalid whoami %#02x expected %#02hhx (%s)\n",
1319 regval, st->hw->whoami, st->hw->name);
1324 /* reset to make sure previous state are not there */
1325 result = regmap_write(st->map, st->reg->pwr_mgmt_1,
1326 INV_MPU6050_BIT_H_RESET);
1329 msleep(INV_MPU6050_POWER_UP_TIME);
1330 switch (st->chip_type) {
1337 /* reset signal path (required for spi connection) */
1338 regval = INV_MPU6050_BIT_TEMP_RST | INV_MPU6050_BIT_ACCEL_RST |
1339 INV_MPU6050_BIT_GYRO_RST;
1340 result = regmap_write(st->map, INV_MPU6050_REG_SIGNAL_PATH_RESET,
1344 msleep(INV_MPU6050_POWER_UP_TIME);
1351 * Turn power on. After reset, the sleep bit could be on
1352 * or off depending on the OTP settings. Turning power on
1353 * make it in a definite state as well as making the hardware
1354 * state align with the software state
1356 result = inv_mpu6050_set_power_itg(st, true);
1359 mask = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
1360 INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN;
1361 result = inv_mpu6050_switch_engine(st, false, mask);
1363 goto error_power_off;
1368 inv_mpu6050_set_power_itg(st, false);
1372 static int inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state *st)
1376 result = regulator_enable(st->vddio_supply);
1378 dev_err(regmap_get_device(st->map),
1379 "Failed to enable vddio regulator: %d\n", result);
1381 /* Give the device a little bit of time to start up. */
1382 usleep_range(3000, 5000);
1388 static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
1392 result = regulator_disable(st->vddio_supply);
1394 dev_err(regmap_get_device(st->map),
1395 "Failed to disable vddio regulator: %d\n", result);
1400 static void inv_mpu_core_disable_regulator_action(void *_data)
1402 struct inv_mpu6050_state *st = _data;
1405 result = regulator_disable(st->vdd_supply);
1407 dev_err(regmap_get_device(st->map),
1408 "Failed to disable vdd regulator: %d\n", result);
1410 inv_mpu_core_disable_regulator_vddio(st);
1413 static void inv_mpu_pm_disable(void *data)
1415 struct device *dev = data;
1417 pm_runtime_put_sync_suspend(dev);
1418 pm_runtime_disable(dev);
1421 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
1422 int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
1424 struct inv_mpu6050_state *st;
1425 struct iio_dev *indio_dev;
1426 struct inv_mpu6050_platform_data *pdata;
1427 struct device *dev = regmap_get_device(regmap);
1429 struct irq_data *desc;
1432 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1436 BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS);
1437 if (chip_type < 0 || chip_type >= INV_NUM_PARTS) {
1438 dev_err(dev, "Bad invensense chip_type=%d name=%s\n",
1442 st = iio_priv(indio_dev);
1443 mutex_init(&st->lock);
1444 st->chip_type = chip_type;
1448 pdata = dev_get_platdata(dev);
1450 result = iio_read_mount_matrix(dev, "mount-matrix",
1453 dev_err(dev, "Failed to retrieve mounting matrix %d\n",
1458 st->plat_data = *pdata;
1462 desc = irq_get_irq_data(irq);
1464 dev_err(dev, "Could not find IRQ %d\n", irq);
1468 irq_type = irqd_get_trigger_type(desc);
1470 irq_type = IRQF_TRIGGER_RISING;
1472 /* Doesn't really matter, use the default */
1473 irq_type = IRQF_TRIGGER_RISING;
1476 if (irq_type & IRQF_TRIGGER_RISING) // rising or both-edge
1477 st->irq_mask = INV_MPU6050_ACTIVE_HIGH;
1478 else if (irq_type == IRQF_TRIGGER_FALLING)
1479 st->irq_mask = INV_MPU6050_ACTIVE_LOW;
1480 else if (irq_type == IRQF_TRIGGER_HIGH)
1481 st->irq_mask = INV_MPU6050_ACTIVE_HIGH |
1482 INV_MPU6050_LATCH_INT_EN;
1483 else if (irq_type == IRQF_TRIGGER_LOW)
1484 st->irq_mask = INV_MPU6050_ACTIVE_LOW |
1485 INV_MPU6050_LATCH_INT_EN;
1487 dev_err(dev, "Invalid interrupt type 0x%x specified\n",
1492 st->vdd_supply = devm_regulator_get(dev, "vdd");
1493 if (IS_ERR(st->vdd_supply))
1494 return dev_err_probe(dev, PTR_ERR(st->vdd_supply),
1495 "Failed to get vdd regulator\n");
1497 st->vddio_supply = devm_regulator_get(dev, "vddio");
1498 if (IS_ERR(st->vddio_supply))
1499 return dev_err_probe(dev, PTR_ERR(st->vddio_supply),
1500 "Failed to get vddio regulator\n");
1502 result = regulator_enable(st->vdd_supply);
1504 dev_err(dev, "Failed to enable vdd regulator: %d\n", result);
1507 msleep(INV_MPU6050_POWER_UP_TIME);
1509 result = inv_mpu_core_enable_regulator_vddio(st);
1511 regulator_disable(st->vdd_supply);
1515 result = devm_add_action_or_reset(dev, inv_mpu_core_disable_regulator_action,
1518 dev_err(dev, "Failed to setup regulator cleanup action %d\n",
1523 /* fill magnetometer orientation */
1524 result = inv_mpu_magn_set_orient(st);
1528 /* power is turned on inside check chip type*/
1529 result = inv_check_and_setup_chip(st);
1533 result = inv_mpu6050_init_config(indio_dev);
1535 dev_err(dev, "Could not initialize device.\n");
1536 goto error_power_off;
1539 dev_set_drvdata(dev, indio_dev);
1540 /* name will be NULL when enumerated via ACPI */
1542 indio_dev->name = name;
1544 indio_dev->name = dev_name(dev);
1546 /* requires parent device set in indio_dev */
1547 if (inv_mpu_bus_setup) {
1548 result = inv_mpu_bus_setup(indio_dev);
1550 goto error_power_off;
1553 /* chip init is done, turning on runtime power management */
1554 result = pm_runtime_set_active(dev);
1556 goto error_power_off;
1557 pm_runtime_get_noresume(dev);
1558 pm_runtime_enable(dev);
1559 pm_runtime_set_autosuspend_delay(dev, INV_MPU6050_SUSPEND_DELAY_MS);
1560 pm_runtime_use_autosuspend(dev);
1561 pm_runtime_put(dev);
1562 result = devm_add_action_or_reset(dev, inv_mpu_pm_disable, dev);
1566 switch (chip_type) {
1568 indio_dev->channels = inv_mpu9150_channels;
1569 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9150_channels);
1570 indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1574 indio_dev->channels = inv_mpu9250_channels;
1575 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9250_channels);
1576 indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1579 indio_dev->channels = inv_mpu_channels;
1580 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1581 indio_dev->available_scan_masks = inv_icm20602_scan_masks;
1584 indio_dev->channels = inv_mpu_channels;
1585 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1586 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1590 * Use magnetometer inside the chip only if there is no i2c
1591 * auxiliary device in use. Otherwise Going back to 6-axis only.
1593 if (st->magn_disabled) {
1594 indio_dev->channels = inv_mpu_channels;
1595 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1596 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1599 indio_dev->info = &mpu_info;
1603 * The driver currently only supports buffered capture with its
1604 * own trigger. So no IRQ, no trigger, no buffer
1606 result = devm_iio_triggered_buffer_setup(dev, indio_dev,
1607 iio_pollfunc_store_time,
1608 inv_mpu6050_read_fifo,
1611 dev_err(dev, "configure buffer fail %d\n", result);
1615 result = inv_mpu6050_probe_trigger(indio_dev, irq_type);
1617 dev_err(dev, "trigger probe fail %d\n", result);
1622 result = devm_iio_device_register(dev, indio_dev);
1624 dev_err(dev, "IIO register fail %d\n", result);
1631 inv_mpu6050_set_power_itg(st, false);
1634 EXPORT_SYMBOL_GPL(inv_mpu_core_probe);
1636 static int __maybe_unused inv_mpu_resume(struct device *dev)
1638 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1639 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1642 mutex_lock(&st->lock);
1643 result = inv_mpu_core_enable_regulator_vddio(st);
1647 result = inv_mpu6050_set_power_itg(st, true);
1651 pm_runtime_disable(dev);
1652 pm_runtime_set_active(dev);
1653 pm_runtime_enable(dev);
1655 result = inv_mpu6050_switch_engine(st, true, st->suspended_sensors);
1659 if (iio_buffer_enabled(indio_dev))
1660 result = inv_mpu6050_prepare_fifo(st, true);
1663 mutex_unlock(&st->lock);
1668 static int __maybe_unused inv_mpu_suspend(struct device *dev)
1670 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1671 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1674 mutex_lock(&st->lock);
1676 st->suspended_sensors = 0;
1677 if (pm_runtime_suspended(dev)) {
1682 if (iio_buffer_enabled(indio_dev)) {
1683 result = inv_mpu6050_prepare_fifo(st, false);
1688 if (st->chip_config.accl_en)
1689 st->suspended_sensors |= INV_MPU6050_SENSOR_ACCL;
1690 if (st->chip_config.gyro_en)
1691 st->suspended_sensors |= INV_MPU6050_SENSOR_GYRO;
1692 if (st->chip_config.temp_en)
1693 st->suspended_sensors |= INV_MPU6050_SENSOR_TEMP;
1694 if (st->chip_config.magn_en)
1695 st->suspended_sensors |= INV_MPU6050_SENSOR_MAGN;
1696 result = inv_mpu6050_switch_engine(st, false, st->suspended_sensors);
1700 result = inv_mpu6050_set_power_itg(st, false);
1704 inv_mpu_core_disable_regulator_vddio(st);
1706 mutex_unlock(&st->lock);
1711 static int __maybe_unused inv_mpu_runtime_suspend(struct device *dev)
1713 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1714 unsigned int sensors;
1717 mutex_lock(&st->lock);
1719 sensors = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
1720 INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN;
1721 ret = inv_mpu6050_switch_engine(st, false, sensors);
1725 ret = inv_mpu6050_set_power_itg(st, false);
1729 inv_mpu_core_disable_regulator_vddio(st);
1732 mutex_unlock(&st->lock);
1736 static int __maybe_unused inv_mpu_runtime_resume(struct device *dev)
1738 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1741 ret = inv_mpu_core_enable_regulator_vddio(st);
1745 return inv_mpu6050_set_power_itg(st, true);
1748 const struct dev_pm_ops inv_mpu_pmops = {
1749 SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
1750 SET_RUNTIME_PM_OPS(inv_mpu_runtime_suspend, inv_mpu_runtime_resume, NULL)
1752 EXPORT_SYMBOL_GPL(inv_mpu_pmops);
1754 MODULE_AUTHOR("Invensense Corporation");
1755 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
1756 MODULE_LICENSE("GPL");