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>
19 #include "inv_mpu_iio.h"
20 #include "inv_mpu_magn.h"
23 * this is the gyro scale translated from dynamic range plus/minus
24 * {250, 500, 1000, 2000} to rad/s
26 static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
29 * this is the accel scale translated from dynamic range plus/minus
30 * {2, 4, 8, 16} to m/s^2
32 static const int accel_scale[] = {598, 1196, 2392, 4785};
34 static const struct inv_mpu6050_reg_map reg_set_icm20602 = {
35 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
36 .lpf = INV_MPU6050_REG_CONFIG,
37 .accel_lpf = INV_MPU6500_REG_ACCEL_CONFIG_2,
38 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
39 .fifo_en = INV_MPU6050_REG_FIFO_EN,
40 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
41 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
42 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
43 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
44 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
45 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
46 .temperature = INV_MPU6050_REG_TEMPERATURE,
47 .int_enable = INV_MPU6050_REG_INT_ENABLE,
48 .int_status = INV_MPU6050_REG_INT_STATUS,
49 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
50 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
51 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
52 .accl_offset = INV_MPU6500_REG_ACCEL_OFFSET,
53 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
54 .i2c_if = INV_ICM20602_REG_I2C_IF,
57 static const struct inv_mpu6050_reg_map reg_set_6500 = {
58 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
59 .lpf = INV_MPU6050_REG_CONFIG,
60 .accel_lpf = INV_MPU6500_REG_ACCEL_CONFIG_2,
61 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
62 .fifo_en = INV_MPU6050_REG_FIFO_EN,
63 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
64 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
65 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
66 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
67 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
68 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
69 .temperature = INV_MPU6050_REG_TEMPERATURE,
70 .int_enable = INV_MPU6050_REG_INT_ENABLE,
71 .int_status = INV_MPU6050_REG_INT_STATUS,
72 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
73 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
74 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
75 .accl_offset = INV_MPU6500_REG_ACCEL_OFFSET,
76 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
80 static const struct inv_mpu6050_reg_map reg_set_6050 = {
81 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
82 .lpf = INV_MPU6050_REG_CONFIG,
83 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
84 .fifo_en = INV_MPU6050_REG_FIFO_EN,
85 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
86 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
87 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
88 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
89 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
90 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
91 .temperature = INV_MPU6050_REG_TEMPERATURE,
92 .int_enable = INV_MPU6050_REG_INT_ENABLE,
93 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
94 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
95 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
96 .accl_offset = INV_MPU6050_REG_ACCEL_OFFSET,
97 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
101 static const struct inv_mpu6050_chip_config chip_config_6050 = {
102 .fsr = INV_MPU6050_FSR_2000DPS,
103 .lpf = INV_MPU6050_FILTER_20HZ,
104 .divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(INV_MPU6050_INIT_FIFO_RATE),
105 .gyro_fifo_enable = false,
106 .accl_fifo_enable = false,
107 .temp_fifo_enable = false,
108 .magn_fifo_enable = false,
109 .accl_fs = INV_MPU6050_FS_02G,
113 /* Indexed by enum inv_devices */
114 static const struct inv_mpu6050_hw hw_info[] = {
116 .whoami = INV_MPU6050_WHOAMI_VALUE,
118 .reg = ®_set_6050,
119 .config = &chip_config_6050,
121 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
124 .whoami = INV_MPU6500_WHOAMI_VALUE,
126 .reg = ®_set_6500,
127 .config = &chip_config_6050,
129 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
132 .whoami = INV_MPU6515_WHOAMI_VALUE,
134 .reg = ®_set_6500,
135 .config = &chip_config_6050,
137 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
140 .whoami = INV_MPU6000_WHOAMI_VALUE,
142 .reg = ®_set_6050,
143 .config = &chip_config_6050,
145 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
148 .whoami = INV_MPU9150_WHOAMI_VALUE,
150 .reg = ®_set_6050,
151 .config = &chip_config_6050,
153 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
156 .whoami = INV_MPU9250_WHOAMI_VALUE,
158 .reg = ®_set_6500,
159 .config = &chip_config_6050,
161 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
164 .whoami = INV_MPU9255_WHOAMI_VALUE,
166 .reg = ®_set_6500,
167 .config = &chip_config_6050,
169 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
172 .whoami = INV_ICM20608_WHOAMI_VALUE,
174 .reg = ®_set_6500,
175 .config = &chip_config_6050,
177 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
180 .whoami = INV_ICM20602_WHOAMI_VALUE,
182 .reg = ®_set_icm20602,
183 .config = &chip_config_6050,
185 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
189 int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
191 unsigned int d, mgmt_1;
194 * switch clock needs to be careful. Only when gyro is on, can
195 * clock source be switched to gyro. Otherwise, it must be set to
198 if (mask == INV_MPU6050_BIT_PWR_GYRO_STBY) {
199 result = regmap_read(st->map, st->reg->pwr_mgmt_1, &mgmt_1);
203 mgmt_1 &= ~INV_MPU6050_BIT_CLK_MASK;
206 if ((mask == INV_MPU6050_BIT_PWR_GYRO_STBY) && (!en)) {
208 * turning off gyro requires switch to internal clock first.
209 * Then turn off gyro engine
211 mgmt_1 |= INV_CLK_INTERNAL;
212 result = regmap_write(st->map, st->reg->pwr_mgmt_1, mgmt_1);
217 result = regmap_read(st->map, st->reg->pwr_mgmt_2, &d);
224 result = regmap_write(st->map, st->reg->pwr_mgmt_2, d);
229 /* Wait for output to stabilize */
230 msleep(INV_MPU6050_TEMP_UP_TIME);
231 if (mask == INV_MPU6050_BIT_PWR_GYRO_STBY) {
232 /* switch internal clock to PLL */
233 mgmt_1 |= INV_CLK_PLL;
234 result = regmap_write(st->map,
235 st->reg->pwr_mgmt_1, mgmt_1);
244 int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
249 if (!st->powerup_count) {
250 result = regmap_write(st->map, st->reg->pwr_mgmt_1, 0);
253 usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
254 INV_MPU6050_REG_UP_TIME_MAX);
258 if (st->powerup_count == 1) {
259 result = regmap_write(st->map, st->reg->pwr_mgmt_1,
260 INV_MPU6050_BIT_SLEEP);
267 dev_dbg(regmap_get_device(st->map), "set power %d, count=%u\n",
268 power_on, st->powerup_count);
272 EXPORT_SYMBOL_GPL(inv_mpu6050_set_power_itg);
275 * inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent
277 * MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope
278 * MPU6500 and above have a dedicated register for accelerometer
280 static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st,
281 enum inv_mpu6050_filter_e val)
285 result = regmap_write(st->map, st->reg->lpf, val);
289 switch (st->chip_type) {
293 /* old chips, nothing to do */
298 result = regmap_write(st->map, st->reg->accel_lpf, val);
306 * inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
308 * Initial configuration:
312 * Clock source: Gyro PLL
314 static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
318 struct inv_mpu6050_state *st = iio_priv(indio_dev);
320 result = inv_mpu6050_set_power_itg(st, true);
323 d = (INV_MPU6050_FSR_2000DPS << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
324 result = regmap_write(st->map, st->reg->gyro_config, d);
326 goto error_power_off;
328 result = inv_mpu6050_set_lpf_regs(st, INV_MPU6050_FILTER_20HZ);
330 goto error_power_off;
332 d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(INV_MPU6050_INIT_FIFO_RATE);
333 result = regmap_write(st->map, st->reg->sample_rate_div, d);
335 goto error_power_off;
337 d = (INV_MPU6050_FS_02G << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
338 result = regmap_write(st->map, st->reg->accl_config, d);
340 goto error_power_off;
342 result = regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
346 memcpy(&st->chip_config, hw_info[st->chip_type].config,
347 sizeof(struct inv_mpu6050_chip_config));
350 * Internal chip period is 1ms (1kHz).
351 * Let's use at the beginning the theorical value before measuring
352 * with interrupt timestamps.
354 st->chip_period = NSEC_PER_MSEC;
356 /* magn chip init, noop if not present in the chip */
357 result = inv_mpu_magn_probe(st);
359 goto error_power_off;
361 return inv_mpu6050_set_power_itg(st, false);
364 inv_mpu6050_set_power_itg(st, false);
368 static int inv_mpu6050_sensor_set(struct inv_mpu6050_state *st, int reg,
372 __be16 d = cpu_to_be16(val);
374 ind = (axis - IIO_MOD_X) * 2;
375 result = regmap_bulk_write(st->map, reg + ind, (u8 *)&d, 2);
382 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state *st, int reg,
388 ind = (axis - IIO_MOD_X) * 2;
389 result = regmap_bulk_read(st->map, reg + ind, (u8 *)&d, 2);
392 *val = (short)be16_to_cpup(&d);
397 static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
398 struct iio_chan_spec const *chan,
401 struct inv_mpu6050_state *st = iio_priv(indio_dev);
405 result = inv_mpu6050_set_power_itg(st, true);
409 switch (chan->type) {
411 result = inv_mpu6050_switch_engine(st, true,
412 INV_MPU6050_BIT_PWR_GYRO_STBY);
414 goto error_power_off;
415 ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
416 chan->channel2, val);
417 result = inv_mpu6050_switch_engine(st, false,
418 INV_MPU6050_BIT_PWR_GYRO_STBY);
420 goto error_power_off;
423 result = inv_mpu6050_switch_engine(st, true,
424 INV_MPU6050_BIT_PWR_ACCL_STBY);
426 goto error_power_off;
427 ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
428 chan->channel2, val);
429 result = inv_mpu6050_switch_engine(st, false,
430 INV_MPU6050_BIT_PWR_ACCL_STBY);
432 goto error_power_off;
435 /* wait for stablization */
436 msleep(INV_MPU6050_SENSOR_UP_TIME);
437 ret = inv_mpu6050_sensor_show(st, st->reg->temperature,
441 ret = inv_mpu_magn_read(st, chan->channel2, val);
448 result = inv_mpu6050_set_power_itg(st, false);
450 goto error_power_off;
455 inv_mpu6050_set_power_itg(st, false);
460 inv_mpu6050_read_raw(struct iio_dev *indio_dev,
461 struct iio_chan_spec const *chan,
462 int *val, int *val2, long mask)
464 struct inv_mpu6050_state *st = iio_priv(indio_dev);
468 case IIO_CHAN_INFO_RAW:
469 ret = iio_device_claim_direct_mode(indio_dev);
472 mutex_lock(&st->lock);
473 ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
474 mutex_unlock(&st->lock);
475 iio_device_release_direct_mode(indio_dev);
477 case IIO_CHAN_INFO_SCALE:
478 switch (chan->type) {
480 mutex_lock(&st->lock);
482 *val2 = gyro_scale_6050[st->chip_config.fsr];
483 mutex_unlock(&st->lock);
485 return IIO_VAL_INT_PLUS_NANO;
487 mutex_lock(&st->lock);
489 *val2 = accel_scale[st->chip_config.accl_fs];
490 mutex_unlock(&st->lock);
492 return IIO_VAL_INT_PLUS_MICRO;
494 *val = st->hw->temp.scale / 1000000;
495 *val2 = st->hw->temp.scale % 1000000;
496 return IIO_VAL_INT_PLUS_MICRO;
498 return inv_mpu_magn_get_scale(st, chan, val, val2);
502 case IIO_CHAN_INFO_OFFSET:
503 switch (chan->type) {
505 *val = st->hw->temp.offset;
510 case IIO_CHAN_INFO_CALIBBIAS:
511 switch (chan->type) {
513 mutex_lock(&st->lock);
514 ret = inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
515 chan->channel2, val);
516 mutex_unlock(&st->lock);
519 mutex_lock(&st->lock);
520 ret = inv_mpu6050_sensor_show(st, st->reg->accl_offset,
521 chan->channel2, val);
522 mutex_unlock(&st->lock);
533 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state *st, int val)
538 for (i = 0; i < ARRAY_SIZE(gyro_scale_6050); ++i) {
539 if (gyro_scale_6050[i] == val) {
540 d = (i << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
541 result = regmap_write(st->map, st->reg->gyro_config, d);
545 st->chip_config.fsr = i;
553 static int inv_write_raw_get_fmt(struct iio_dev *indio_dev,
554 struct iio_chan_spec const *chan, long mask)
557 case IIO_CHAN_INFO_SCALE:
558 switch (chan->type) {
560 return IIO_VAL_INT_PLUS_NANO;
562 return IIO_VAL_INT_PLUS_MICRO;
565 return IIO_VAL_INT_PLUS_MICRO;
571 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state *st, int val)
576 for (i = 0; i < ARRAY_SIZE(accel_scale); ++i) {
577 if (accel_scale[i] == val) {
578 d = (i << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
579 result = regmap_write(st->map, st->reg->accl_config, d);
583 st->chip_config.accl_fs = i;
591 static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
592 struct iio_chan_spec const *chan,
593 int val, int val2, long mask)
595 struct inv_mpu6050_state *st = iio_priv(indio_dev);
599 * we should only update scale when the chip is disabled, i.e.
602 result = iio_device_claim_direct_mode(indio_dev);
606 mutex_lock(&st->lock);
607 result = inv_mpu6050_set_power_itg(st, true);
609 goto error_write_raw_unlock;
612 case IIO_CHAN_INFO_SCALE:
613 switch (chan->type) {
615 result = inv_mpu6050_write_gyro_scale(st, val2);
618 result = inv_mpu6050_write_accel_scale(st, val2);
625 case IIO_CHAN_INFO_CALIBBIAS:
626 switch (chan->type) {
628 result = inv_mpu6050_sensor_set(st,
629 st->reg->gyro_offset,
630 chan->channel2, val);
633 result = inv_mpu6050_sensor_set(st,
634 st->reg->accl_offset,
635 chan->channel2, val);
647 result |= inv_mpu6050_set_power_itg(st, false);
648 error_write_raw_unlock:
649 mutex_unlock(&st->lock);
650 iio_device_release_direct_mode(indio_dev);
656 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
658 * Based on the Nyquist principle, the sampling rate must
659 * exceed twice of the bandwidth of the signal, or there
660 * would be alising. This function basically search for the
661 * correct low pass parameters based on the fifo rate, e.g,
662 * sampling frequency.
664 * lpf is set automatically when setting sampling rate to avoid any aliases.
666 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
668 static const int hz[] = {188, 98, 42, 20, 10, 5};
669 static const int d[] = {
670 INV_MPU6050_FILTER_188HZ, INV_MPU6050_FILTER_98HZ,
671 INV_MPU6050_FILTER_42HZ, INV_MPU6050_FILTER_20HZ,
672 INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ
679 while ((h < hz[i]) && (i < ARRAY_SIZE(d) - 1))
682 result = inv_mpu6050_set_lpf_regs(st, data);
685 st->chip_config.lpf = data;
691 * inv_mpu6050_fifo_rate_store() - Set fifo rate.
694 inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
700 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
701 struct inv_mpu6050_state *st = iio_priv(indio_dev);
703 if (kstrtoint(buf, 10, &fifo_rate))
705 if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
706 fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
709 result = iio_device_claim_direct_mode(indio_dev);
713 /* compute the chip sample rate divider */
714 d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate);
715 /* compute back the fifo rate to handle truncation cases */
716 fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(d);
718 mutex_lock(&st->lock);
719 if (d == st->chip_config.divider) {
721 goto fifo_rate_fail_unlock;
723 result = inv_mpu6050_set_power_itg(st, true);
725 goto fifo_rate_fail_unlock;
727 result = regmap_write(st->map, st->reg->sample_rate_div, d);
729 goto fifo_rate_fail_power_off;
730 st->chip_config.divider = d;
732 result = inv_mpu6050_set_lpf(st, fifo_rate);
734 goto fifo_rate_fail_power_off;
736 /* update rate for magn, noop if not present in chip */
737 result = inv_mpu_magn_set_rate(st, fifo_rate);
739 goto fifo_rate_fail_power_off;
741 fifo_rate_fail_power_off:
742 result |= inv_mpu6050_set_power_itg(st, false);
743 fifo_rate_fail_unlock:
744 mutex_unlock(&st->lock);
745 iio_device_release_direct_mode(indio_dev);
753 * inv_fifo_rate_show() - Get the current sampling rate.
756 inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
759 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
762 mutex_lock(&st->lock);
763 fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
764 mutex_unlock(&st->lock);
766 return scnprintf(buf, PAGE_SIZE, "%u\n", fifo_rate);
770 * inv_attr_show() - calling this function will show current
773 * Deprecated in favor of IIO mounting matrix API.
775 * See inv_get_mount_matrix()
777 static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr,
780 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
781 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
784 switch (this_attr->address) {
786 * In MPU6050, the two matrix are the same because gyro and accel
787 * are integrated in one chip
789 case ATTR_GYRO_MATRIX:
790 case ATTR_ACCL_MATRIX:
791 m = st->plat_data.orientation;
793 return scnprintf(buf, PAGE_SIZE,
794 "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
795 m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
802 * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
804 * @indio_dev: The IIO device
805 * @trig: The new trigger
807 * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
808 * device, -EINVAL otherwise.
810 static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
811 struct iio_trigger *trig)
813 struct inv_mpu6050_state *st = iio_priv(indio_dev);
815 if (st->trig != trig)
821 static const struct iio_mount_matrix *
822 inv_get_mount_matrix(const struct iio_dev *indio_dev,
823 const struct iio_chan_spec *chan)
825 struct inv_mpu6050_state *data = iio_priv(indio_dev);
826 const struct iio_mount_matrix *matrix;
828 if (chan->type == IIO_MAGN)
829 matrix = &data->magn_orient;
831 matrix = &data->orientation;
836 static const struct iio_chan_spec_ext_info inv_ext_info[] = {
837 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix),
841 #define INV_MPU6050_CHAN(_type, _channel2, _index) \
845 .channel2 = _channel2, \
846 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
847 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
848 BIT(IIO_CHAN_INFO_CALIBBIAS), \
849 .scan_index = _index, \
855 .endianness = IIO_BE, \
857 .ext_info = inv_ext_info, \
860 #define INV_MPU6050_TEMP_CHAN(_index) \
863 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
864 | BIT(IIO_CHAN_INFO_OFFSET) \
865 | BIT(IIO_CHAN_INFO_SCALE), \
866 .scan_index = _index, \
872 .endianness = IIO_BE, \
876 static const struct iio_chan_spec inv_mpu_channels[] = {
877 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
879 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
881 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
882 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
883 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
885 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
886 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
887 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
890 #define INV_MPU6050_SCAN_MASK_3AXIS_ACCEL \
891 (BIT(INV_MPU6050_SCAN_ACCL_X) \
892 | BIT(INV_MPU6050_SCAN_ACCL_Y) \
893 | BIT(INV_MPU6050_SCAN_ACCL_Z))
895 #define INV_MPU6050_SCAN_MASK_3AXIS_GYRO \
896 (BIT(INV_MPU6050_SCAN_GYRO_X) \
897 | BIT(INV_MPU6050_SCAN_GYRO_Y) \
898 | BIT(INV_MPU6050_SCAN_GYRO_Z))
900 #define INV_MPU6050_SCAN_MASK_TEMP (BIT(INV_MPU6050_SCAN_TEMP))
902 static const unsigned long inv_mpu_scan_masks[] = {
904 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
905 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
907 INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
908 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
909 /* 6-axis accel + gyro */
910 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
911 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
912 | INV_MPU6050_SCAN_MASK_TEMP,
916 #define INV_MPU9X50_MAGN_CHAN(_chan2, _bits, _index) \
920 .channel2 = _chan2, \
921 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) | \
922 BIT(IIO_CHAN_INFO_RAW), \
923 .scan_index = _index, \
929 .endianness = IIO_BE, \
931 .ext_info = inv_ext_info, \
934 static const struct iio_chan_spec inv_mpu9150_channels[] = {
935 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
937 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
939 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
940 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
941 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
943 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
944 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
945 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
947 /* Magnetometer resolution is 13 bits */
948 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 13, INV_MPU9X50_SCAN_MAGN_X),
949 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 13, INV_MPU9X50_SCAN_MAGN_Y),
950 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 13, INV_MPU9X50_SCAN_MAGN_Z),
953 static const struct iio_chan_spec inv_mpu9250_channels[] = {
954 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
956 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
958 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
959 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
960 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
962 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
963 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
964 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
966 /* Magnetometer resolution is 16 bits */
967 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 16, INV_MPU9X50_SCAN_MAGN_X),
968 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 16, INV_MPU9X50_SCAN_MAGN_Y),
969 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 16, INV_MPU9X50_SCAN_MAGN_Z),
972 #define INV_MPU9X50_SCAN_MASK_3AXIS_MAGN \
973 (BIT(INV_MPU9X50_SCAN_MAGN_X) \
974 | BIT(INV_MPU9X50_SCAN_MAGN_Y) \
975 | BIT(INV_MPU9X50_SCAN_MAGN_Z))
977 static const unsigned long inv_mpu9x50_scan_masks[] = {
979 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
980 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
982 INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
983 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
985 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
986 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN | INV_MPU6050_SCAN_MASK_TEMP,
987 /* 6-axis accel + gyro */
988 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
989 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
990 | INV_MPU6050_SCAN_MASK_TEMP,
991 /* 6-axis accel + magn */
992 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
993 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
994 | INV_MPU6050_SCAN_MASK_TEMP,
995 /* 6-axis gyro + magn */
996 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
997 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
998 | INV_MPU6050_SCAN_MASK_TEMP,
999 /* 9-axis accel + gyro + magn */
1000 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1001 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1002 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1003 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1004 | INV_MPU6050_SCAN_MASK_TEMP,
1008 static const unsigned long inv_icm20602_scan_masks[] = {
1009 /* 3-axis accel + temp (mandatory) */
1010 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1011 /* 3-axis gyro + temp (mandatory) */
1012 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1013 /* 6-axis accel + gyro + temp (mandatory) */
1014 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1015 | INV_MPU6050_SCAN_MASK_TEMP,
1020 * The user can choose any frequency between INV_MPU6050_MIN_FIFO_RATE and
1021 * INV_MPU6050_MAX_FIFO_RATE, but only these frequencies are matched by the
1022 * low-pass filter. Specifically, each of these sampling rates are about twice
1023 * the bandwidth of a corresponding low-pass filter, which should eliminate
1024 * aliasing following the Nyquist principle. By picking a frequency different
1025 * from these, the user risks aliasing effects.
1027 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
1028 static IIO_CONST_ATTR(in_anglvel_scale_available,
1029 "0.000133090 0.000266181 0.000532362 0.001064724");
1030 static IIO_CONST_ATTR(in_accel_scale_available,
1031 "0.000598 0.001196 0.002392 0.004785");
1032 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
1033 inv_mpu6050_fifo_rate_store);
1035 /* Deprecated: kept for userspace backward compatibility. */
1036 static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
1038 static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
1041 static struct attribute *inv_attributes[] = {
1042 &iio_dev_attr_in_gyro_matrix.dev_attr.attr, /* deprecated */
1043 &iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */
1044 &iio_dev_attr_sampling_frequency.dev_attr.attr,
1045 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1046 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
1047 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
1051 static const struct attribute_group inv_attribute_group = {
1052 .attrs = inv_attributes
1055 static const struct iio_info mpu_info = {
1056 .read_raw = &inv_mpu6050_read_raw,
1057 .write_raw = &inv_mpu6050_write_raw,
1058 .write_raw_get_fmt = &inv_write_raw_get_fmt,
1059 .attrs = &inv_attribute_group,
1060 .validate_trigger = inv_mpu6050_validate_trigger,
1064 * inv_check_and_setup_chip() - check and setup chip.
1066 static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
1069 unsigned int regval;
1072 st->hw = &hw_info[st->chip_type];
1073 st->reg = hw_info[st->chip_type].reg;
1075 /* check chip self-identification */
1076 result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, ®val);
1079 if (regval != st->hw->whoami) {
1080 /* check whoami against all possible values */
1081 for (i = 0; i < INV_NUM_PARTS; ++i) {
1082 if (regval == hw_info[i].whoami) {
1083 dev_warn(regmap_get_device(st->map),
1084 "whoami mismatch got %#02x (%s)"
1085 "expected %#02hhx (%s)\n",
1086 regval, hw_info[i].name,
1087 st->hw->whoami, st->hw->name);
1091 if (i >= INV_NUM_PARTS) {
1092 dev_err(regmap_get_device(st->map),
1093 "invalid whoami %#02x expected %#02hhx (%s)\n",
1094 regval, st->hw->whoami, st->hw->name);
1099 /* reset to make sure previous state are not there */
1100 result = regmap_write(st->map, st->reg->pwr_mgmt_1,
1101 INV_MPU6050_BIT_H_RESET);
1104 msleep(INV_MPU6050_POWER_UP_TIME);
1107 * Turn power on. After reset, the sleep bit could be on
1108 * or off depending on the OTP settings. Turning power on
1109 * make it in a definite state as well as making the hardware
1110 * state align with the software state
1112 result = inv_mpu6050_set_power_itg(st, true);
1116 result = inv_mpu6050_switch_engine(st, false,
1117 INV_MPU6050_BIT_PWR_ACCL_STBY);
1119 goto error_power_off;
1120 result = inv_mpu6050_switch_engine(st, false,
1121 INV_MPU6050_BIT_PWR_GYRO_STBY);
1123 goto error_power_off;
1125 return inv_mpu6050_set_power_itg(st, false);
1128 inv_mpu6050_set_power_itg(st, false);
1132 static int inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state *st)
1136 result = regulator_enable(st->vddio_supply);
1138 dev_err(regmap_get_device(st->map),
1139 "Failed to enable vddio regulator: %d\n", result);
1141 /* Give the device a little bit of time to start up. */
1142 usleep_range(35000, 70000);
1148 static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
1152 result = regulator_disable(st->vddio_supply);
1154 dev_err(regmap_get_device(st->map),
1155 "Failed to disable vddio regulator: %d\n", result);
1160 static void inv_mpu_core_disable_regulator_action(void *_data)
1162 struct inv_mpu6050_state *st = _data;
1165 result = regulator_disable(st->vdd_supply);
1167 dev_err(regmap_get_device(st->map),
1168 "Failed to disable vdd regulator: %d\n", result);
1170 inv_mpu_core_disable_regulator_vddio(st);
1173 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
1174 int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
1176 struct inv_mpu6050_state *st;
1177 struct iio_dev *indio_dev;
1178 struct inv_mpu6050_platform_data *pdata;
1179 struct device *dev = regmap_get_device(regmap);
1181 struct irq_data *desc;
1184 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1188 BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS);
1189 if (chip_type < 0 || chip_type >= INV_NUM_PARTS) {
1190 dev_err(dev, "Bad invensense chip_type=%d name=%s\n",
1194 st = iio_priv(indio_dev);
1195 mutex_init(&st->lock);
1196 st->chip_type = chip_type;
1197 st->powerup_count = 0;
1201 pdata = dev_get_platdata(dev);
1203 result = iio_read_mount_matrix(dev, "mount-matrix",
1206 dev_err(dev, "Failed to retrieve mounting matrix %d\n",
1211 st->plat_data = *pdata;
1214 desc = irq_get_irq_data(irq);
1216 dev_err(dev, "Could not find IRQ %d\n", irq);
1220 irq_type = irqd_get_trigger_type(desc);
1222 irq_type = IRQF_TRIGGER_RISING;
1223 if (irq_type & IRQF_TRIGGER_RISING) // rising or both-edge
1224 st->irq_mask = INV_MPU6050_ACTIVE_HIGH;
1225 else if (irq_type == IRQF_TRIGGER_FALLING)
1226 st->irq_mask = INV_MPU6050_ACTIVE_LOW;
1227 else if (irq_type == IRQF_TRIGGER_HIGH)
1228 st->irq_mask = INV_MPU6050_ACTIVE_HIGH |
1229 INV_MPU6050_LATCH_INT_EN;
1230 else if (irq_type == IRQF_TRIGGER_LOW)
1231 st->irq_mask = INV_MPU6050_ACTIVE_LOW |
1232 INV_MPU6050_LATCH_INT_EN;
1234 dev_err(dev, "Invalid interrupt type 0x%x specified\n",
1239 st->vdd_supply = devm_regulator_get(dev, "vdd");
1240 if (IS_ERR(st->vdd_supply)) {
1241 if (PTR_ERR(st->vdd_supply) != -EPROBE_DEFER)
1242 dev_err(dev, "Failed to get vdd regulator %d\n",
1243 (int)PTR_ERR(st->vdd_supply));
1245 return PTR_ERR(st->vdd_supply);
1248 st->vddio_supply = devm_regulator_get(dev, "vddio");
1249 if (IS_ERR(st->vddio_supply)) {
1250 if (PTR_ERR(st->vddio_supply) != -EPROBE_DEFER)
1251 dev_err(dev, "Failed to get vddio regulator %d\n",
1252 (int)PTR_ERR(st->vddio_supply));
1254 return PTR_ERR(st->vddio_supply);
1257 result = regulator_enable(st->vdd_supply);
1259 dev_err(dev, "Failed to enable vdd regulator: %d\n", result);
1263 result = inv_mpu_core_enable_regulator_vddio(st);
1265 regulator_disable(st->vdd_supply);
1269 result = devm_add_action_or_reset(dev, inv_mpu_core_disable_regulator_action,
1272 dev_err(dev, "Failed to setup regulator cleanup action %d\n",
1277 /* fill magnetometer orientation */
1278 result = inv_mpu_magn_set_orient(st);
1282 /* power is turned on inside check chip type*/
1283 result = inv_check_and_setup_chip(st);
1287 result = inv_mpu6050_init_config(indio_dev);
1289 dev_err(dev, "Could not initialize device.\n");
1293 dev_set_drvdata(dev, indio_dev);
1294 indio_dev->dev.parent = dev;
1295 /* name will be NULL when enumerated via ACPI */
1297 indio_dev->name = name;
1299 indio_dev->name = dev_name(dev);
1301 /* requires parent device set in indio_dev */
1302 if (inv_mpu_bus_setup)
1303 inv_mpu_bus_setup(indio_dev);
1305 switch (chip_type) {
1307 indio_dev->channels = inv_mpu9150_channels;
1308 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9150_channels);
1309 indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1313 indio_dev->channels = inv_mpu9250_channels;
1314 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9250_channels);
1315 indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1318 indio_dev->channels = inv_mpu_channels;
1319 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1320 indio_dev->available_scan_masks = inv_icm20602_scan_masks;
1323 indio_dev->channels = inv_mpu_channels;
1324 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1325 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1329 * Use magnetometer inside the chip only if there is no i2c
1330 * auxiliary device in use. Otherwise Going back to 6-axis only.
1332 if (st->magn_disabled) {
1333 indio_dev->channels = inv_mpu_channels;
1334 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1335 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1338 indio_dev->info = &mpu_info;
1339 indio_dev->modes = INDIO_BUFFER_TRIGGERED;
1341 result = devm_iio_triggered_buffer_setup(dev, indio_dev,
1342 iio_pollfunc_store_time,
1343 inv_mpu6050_read_fifo,
1346 dev_err(dev, "configure buffer fail %d\n", result);
1349 result = inv_mpu6050_probe_trigger(indio_dev, irq_type);
1351 dev_err(dev, "trigger probe fail %d\n", result);
1355 result = devm_iio_device_register(dev, indio_dev);
1357 dev_err(dev, "IIO register fail %d\n", result);
1363 EXPORT_SYMBOL_GPL(inv_mpu_core_probe);
1365 #ifdef CONFIG_PM_SLEEP
1367 static int inv_mpu_resume(struct device *dev)
1369 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1372 mutex_lock(&st->lock);
1373 result = inv_mpu_core_enable_regulator_vddio(st);
1377 result = inv_mpu6050_set_power_itg(st, true);
1379 mutex_unlock(&st->lock);
1384 static int inv_mpu_suspend(struct device *dev)
1386 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1389 mutex_lock(&st->lock);
1390 result = inv_mpu6050_set_power_itg(st, false);
1391 inv_mpu_core_disable_regulator_vddio(st);
1392 mutex_unlock(&st->lock);
1396 #endif /* CONFIG_PM_SLEEP */
1398 SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
1399 EXPORT_SYMBOL_GPL(inv_mpu_pmops);
1401 MODULE_AUTHOR("Invensense Corporation");
1402 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
1403 MODULE_LICENSE("GPL");