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>
19 #include <linux/pm_runtime.h>
20 #include <linux/property.h>
22 #include <linux/iio/common/inv_sensors_timestamp.h>
23 #include <linux/iio/iio.h>
25 #include "inv_mpu_iio.h"
26 #include "inv_mpu_magn.h"
29 * this is the gyro scale translated from dynamic range plus/minus
30 * {250, 500, 1000, 2000} to rad/s
32 static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
35 * this is the accel scale translated from dynamic range plus/minus
36 * {2, 4, 8, 16} to m/s^2
38 static const int accel_scale[] = {598, 1196, 2392, 4785};
40 static const struct inv_mpu6050_reg_map reg_set_icm20602 = {
41 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
42 .lpf = INV_MPU6050_REG_CONFIG,
43 .accel_lpf = INV_MPU6500_REG_ACCEL_CONFIG_2,
44 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
45 .fifo_en = INV_MPU6050_REG_FIFO_EN,
46 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
47 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
48 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
49 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
50 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
51 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
52 .temperature = INV_MPU6050_REG_TEMPERATURE,
53 .int_enable = INV_MPU6050_REG_INT_ENABLE,
54 .int_status = INV_MPU6050_REG_INT_STATUS,
55 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
56 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
57 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
58 .accl_offset = INV_MPU6500_REG_ACCEL_OFFSET,
59 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
60 .i2c_if = INV_ICM20602_REG_I2C_IF,
63 static const struct inv_mpu6050_reg_map reg_set_6500 = {
64 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
65 .lpf = INV_MPU6050_REG_CONFIG,
66 .accel_lpf = INV_MPU6500_REG_ACCEL_CONFIG_2,
67 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
68 .fifo_en = INV_MPU6050_REG_FIFO_EN,
69 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
70 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
71 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
72 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
73 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
74 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
75 .temperature = INV_MPU6050_REG_TEMPERATURE,
76 .int_enable = INV_MPU6050_REG_INT_ENABLE,
77 .int_status = INV_MPU6050_REG_INT_STATUS,
78 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
79 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
80 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
81 .accl_offset = INV_MPU6500_REG_ACCEL_OFFSET,
82 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
86 static const struct inv_mpu6050_reg_map reg_set_6050 = {
87 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
88 .lpf = INV_MPU6050_REG_CONFIG,
89 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
90 .fifo_en = INV_MPU6050_REG_FIFO_EN,
91 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
92 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
93 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
94 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
95 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
96 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
97 .temperature = INV_MPU6050_REG_TEMPERATURE,
98 .int_enable = INV_MPU6050_REG_INT_ENABLE,
99 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
100 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
101 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
102 .accl_offset = INV_MPU6050_REG_ACCEL_OFFSET,
103 .gyro_offset = INV_MPU6050_REG_GYRO_OFFSET,
107 static const struct inv_mpu6050_chip_config chip_config_6050 = {
108 .clk = INV_CLK_INTERNAL,
109 .fsr = INV_MPU6050_FSR_2000DPS,
110 .lpf = INV_MPU6050_FILTER_20HZ,
111 .divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
116 .gyro_fifo_enable = false,
117 .accl_fifo_enable = false,
118 .temp_fifo_enable = false,
119 .magn_fifo_enable = false,
120 .accl_fs = INV_MPU6050_FS_02G,
124 static const struct inv_mpu6050_chip_config chip_config_6500 = {
126 .fsr = INV_MPU6050_FSR_2000DPS,
127 .lpf = INV_MPU6050_FILTER_20HZ,
128 .divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
133 .gyro_fifo_enable = false,
134 .accl_fifo_enable = false,
135 .temp_fifo_enable = false,
136 .magn_fifo_enable = false,
137 .accl_fs = INV_MPU6050_FS_02G,
141 /* Indexed by enum inv_devices */
142 static const struct inv_mpu6050_hw hw_info[] = {
144 .whoami = INV_MPU6050_WHOAMI_VALUE,
146 .reg = ®_set_6050,
147 .config = &chip_config_6050,
149 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
150 .startup_time = {INV_MPU6050_GYRO_STARTUP_TIME, INV_MPU6050_ACCEL_STARTUP_TIME},
153 .whoami = INV_MPU6500_WHOAMI_VALUE,
155 .reg = ®_set_6500,
156 .config = &chip_config_6500,
158 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
159 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
162 .whoami = INV_MPU6515_WHOAMI_VALUE,
164 .reg = ®_set_6500,
165 .config = &chip_config_6500,
167 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
168 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
171 .whoami = INV_MPU6880_WHOAMI_VALUE,
173 .reg = ®_set_6500,
174 .config = &chip_config_6500,
176 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
177 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
180 .whoami = INV_MPU6000_WHOAMI_VALUE,
182 .reg = ®_set_6050,
183 .config = &chip_config_6050,
185 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
186 .startup_time = {INV_MPU6050_GYRO_STARTUP_TIME, INV_MPU6050_ACCEL_STARTUP_TIME},
189 .whoami = INV_MPU9150_WHOAMI_VALUE,
191 .reg = ®_set_6050,
192 .config = &chip_config_6050,
194 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
195 .startup_time = {INV_MPU6050_GYRO_STARTUP_TIME, INV_MPU6050_ACCEL_STARTUP_TIME},
198 .whoami = INV_MPU9250_WHOAMI_VALUE,
200 .reg = ®_set_6500,
201 .config = &chip_config_6500,
203 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
204 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
207 .whoami = INV_MPU9255_WHOAMI_VALUE,
209 .reg = ®_set_6500,
210 .config = &chip_config_6500,
212 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
213 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
216 .whoami = INV_ICM20608_WHOAMI_VALUE,
218 .reg = ®_set_6500,
219 .config = &chip_config_6500,
221 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
222 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
225 .whoami = INV_ICM20608D_WHOAMI_VALUE,
227 .reg = ®_set_6500,
228 .config = &chip_config_6500,
230 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
231 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
234 .whoami = INV_ICM20609_WHOAMI_VALUE,
236 .reg = ®_set_6500,
237 .config = &chip_config_6500,
238 .fifo_size = 4 * 1024,
239 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
240 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
243 .whoami = INV_ICM20689_WHOAMI_VALUE,
245 .reg = ®_set_6500,
246 .config = &chip_config_6500,
247 .fifo_size = 4 * 1024,
248 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
249 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
252 .whoami = INV_ICM20600_WHOAMI_VALUE,
254 .reg = ®_set_icm20602,
255 .config = &chip_config_6500,
257 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
258 .startup_time = {INV_ICM20602_GYRO_STARTUP_TIME, INV_ICM20602_ACCEL_STARTUP_TIME},
261 .whoami = INV_ICM20602_WHOAMI_VALUE,
263 .reg = ®_set_icm20602,
264 .config = &chip_config_6500,
266 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
267 .startup_time = {INV_ICM20602_GYRO_STARTUP_TIME, INV_ICM20602_ACCEL_STARTUP_TIME},
270 .whoami = INV_ICM20690_WHOAMI_VALUE,
272 .reg = ®_set_6500,
273 .config = &chip_config_6500,
275 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
276 .startup_time = {INV_ICM20690_GYRO_STARTUP_TIME, INV_ICM20690_ACCEL_STARTUP_TIME},
279 .whoami = INV_IAM20680_WHOAMI_VALUE,
281 .reg = ®_set_6500,
282 .config = &chip_config_6500,
284 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
285 .startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
289 static int inv_mpu6050_pwr_mgmt_1_write(struct inv_mpu6050_state *st, bool sleep,
290 int clock, int temp_dis)
295 clock = st->chip_config.clk;
297 temp_dis = !st->chip_config.temp_en;
299 val = clock & INV_MPU6050_BIT_CLK_MASK;
301 val |= INV_MPU6050_BIT_TEMP_DIS;
303 val |= INV_MPU6050_BIT_SLEEP;
305 dev_dbg(regmap_get_device(st->map), "pwr_mgmt_1: 0x%x\n", val);
306 return regmap_write(st->map, st->reg->pwr_mgmt_1, val);
309 static int inv_mpu6050_clock_switch(struct inv_mpu6050_state *st,
314 switch (st->chip_type) {
318 /* old chips: switch clock manually */
319 ret = inv_mpu6050_pwr_mgmt_1_write(st, false, clock, -1);
322 st->chip_config.clk = clock;
325 /* automatic clock switching, nothing to do */
332 int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en,
336 u8 pwr_mgmt2, user_ctrl;
339 /* delete useless requests */
340 if (mask & INV_MPU6050_SENSOR_ACCL && en == st->chip_config.accl_en)
341 mask &= ~INV_MPU6050_SENSOR_ACCL;
342 if (mask & INV_MPU6050_SENSOR_GYRO && en == st->chip_config.gyro_en)
343 mask &= ~INV_MPU6050_SENSOR_GYRO;
344 if (mask & INV_MPU6050_SENSOR_TEMP && en == st->chip_config.temp_en)
345 mask &= ~INV_MPU6050_SENSOR_TEMP;
346 if (mask & INV_MPU6050_SENSOR_MAGN && en == st->chip_config.magn_en)
347 mask &= ~INV_MPU6050_SENSOR_MAGN;
351 /* turn on/off temperature sensor */
352 if (mask & INV_MPU6050_SENSOR_TEMP) {
353 ret = inv_mpu6050_pwr_mgmt_1_write(st, false, -1, !en);
356 st->chip_config.temp_en = en;
359 /* update user_crtl for driving magnetometer */
360 if (mask & INV_MPU6050_SENSOR_MAGN) {
361 user_ctrl = st->chip_config.user_ctrl;
363 user_ctrl |= INV_MPU6050_BIT_I2C_MST_EN;
365 user_ctrl &= ~INV_MPU6050_BIT_I2C_MST_EN;
366 ret = regmap_write(st->map, st->reg->user_ctrl, user_ctrl);
369 st->chip_config.user_ctrl = user_ctrl;
370 st->chip_config.magn_en = en;
373 /* manage accel & gyro engines */
374 if (mask & (INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO)) {
375 /* compute power management 2 current value */
377 if (!st->chip_config.accl_en)
378 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_ACCL_STBY;
379 if (!st->chip_config.gyro_en)
380 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_GYRO_STBY;
382 /* update to new requested value */
383 if (mask & INV_MPU6050_SENSOR_ACCL) {
385 pwr_mgmt2 &= ~INV_MPU6050_BIT_PWR_ACCL_STBY;
387 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_ACCL_STBY;
389 if (mask & INV_MPU6050_SENSOR_GYRO) {
391 pwr_mgmt2 &= ~INV_MPU6050_BIT_PWR_GYRO_STBY;
393 pwr_mgmt2 |= INV_MPU6050_BIT_PWR_GYRO_STBY;
396 /* switch clock to internal when turning gyro off */
397 if (mask & INV_MPU6050_SENSOR_GYRO && !en) {
398 ret = inv_mpu6050_clock_switch(st, INV_CLK_INTERNAL);
403 /* update sensors engine */
404 dev_dbg(regmap_get_device(st->map), "pwr_mgmt_2: 0x%x\n",
406 ret = regmap_write(st->map, st->reg->pwr_mgmt_2, pwr_mgmt2);
409 if (mask & INV_MPU6050_SENSOR_ACCL)
410 st->chip_config.accl_en = en;
411 if (mask & INV_MPU6050_SENSOR_GYRO)
412 st->chip_config.gyro_en = en;
414 /* compute required time to have sensors stabilized */
417 if (mask & INV_MPU6050_SENSOR_ACCL) {
418 if (sleep < st->hw->startup_time.accel)
419 sleep = st->hw->startup_time.accel;
421 if (mask & INV_MPU6050_SENSOR_GYRO) {
422 if (sleep < st->hw->startup_time.gyro)
423 sleep = st->hw->startup_time.gyro;
426 if (mask & INV_MPU6050_SENSOR_GYRO) {
427 if (sleep < INV_MPU6050_GYRO_DOWN_TIME)
428 sleep = INV_MPU6050_GYRO_DOWN_TIME;
434 /* switch clock to PLL when turning gyro on */
435 if (mask & INV_MPU6050_SENSOR_GYRO && en) {
436 ret = inv_mpu6050_clock_switch(st, INV_CLK_PLL);
445 static int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st,
450 result = inv_mpu6050_pwr_mgmt_1_write(st, !power_on, -1, -1);
455 usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
456 INV_MPU6050_REG_UP_TIME_MAX);
461 static int inv_mpu6050_set_gyro_fsr(struct inv_mpu6050_state *st,
462 enum inv_mpu6050_fsr_e val)
464 unsigned int gyro_shift;
467 switch (st->chip_type) {
469 gyro_shift = INV_ICM20690_GYRO_CONFIG_FSR_SHIFT;
472 gyro_shift = INV_MPU6050_GYRO_CONFIG_FSR_SHIFT;
476 data = val << gyro_shift;
477 return regmap_write(st->map, st->reg->gyro_config, data);
481 * inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent
483 * MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope
484 * MPU6500 and above have a dedicated register for accelerometer
486 static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st,
487 enum inv_mpu6050_filter_e val)
491 result = regmap_write(st->map, st->reg->lpf, val);
496 switch (st->chip_type) {
500 /* old chips, nothing to do */
504 /* set FIFO size to maximum value */
505 val |= INV_ICM20689_BITS_FIFO_SIZE_MAX;
511 return regmap_write(st->map, st->reg->accel_lpf, val);
515 * inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
517 * Initial configuration:
521 * Clock source: Gyro PLL
523 static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
527 struct inv_mpu6050_state *st = iio_priv(indio_dev);
528 struct inv_sensors_timestamp_chip timestamp;
530 result = inv_mpu6050_set_gyro_fsr(st, st->chip_config.fsr);
534 result = inv_mpu6050_set_lpf_regs(st, st->chip_config.lpf);
538 d = st->chip_config.divider;
539 result = regmap_write(st->map, st->reg->sample_rate_div, d);
543 d = (st->chip_config.accl_fs << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
544 result = regmap_write(st->map, st->reg->accl_config, d);
548 result = regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
552 /* clock jitter is +/- 2% */
553 timestamp.clock_period = NSEC_PER_SEC / INV_MPU6050_INTERNAL_FREQ_HZ;
554 timestamp.jitter = 20;
555 timestamp.init_period =
556 NSEC_PER_SEC / INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
557 inv_sensors_timestamp_init(&st->timestamp, ×tamp);
559 /* magn chip init, noop if not present in the chip */
560 result = inv_mpu_magn_probe(st);
567 static int inv_mpu6050_sensor_set(struct inv_mpu6050_state *st, int reg,
571 __be16 d = cpu_to_be16(val);
573 ind = (axis - IIO_MOD_X) * 2;
574 result = regmap_bulk_write(st->map, reg + ind, &d, sizeof(d));
581 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state *st, int reg,
587 ind = (axis - IIO_MOD_X) * 2;
588 result = regmap_bulk_read(st->map, reg + ind, &d, sizeof(d));
591 *val = (short)be16_to_cpup(&d);
596 static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
597 struct iio_chan_spec const *chan,
600 struct inv_mpu6050_state *st = iio_priv(indio_dev);
601 struct device *pdev = regmap_get_device(st->map);
602 unsigned int freq_hz, period_us, min_sleep_us, max_sleep_us;
606 /* compute sample period */
607 freq_hz = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
608 period_us = 1000000 / freq_hz;
610 result = pm_runtime_resume_and_get(pdev);
614 switch (chan->type) {
616 if (!st->chip_config.gyro_en) {
617 result = inv_mpu6050_switch_engine(st, true,
618 INV_MPU6050_SENSOR_GYRO);
620 goto error_power_off;
621 /* need to wait 2 periods to have first valid sample */
622 min_sleep_us = 2 * period_us;
623 max_sleep_us = 2 * (period_us + period_us / 2);
624 usleep_range(min_sleep_us, max_sleep_us);
626 ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
627 chan->channel2, val);
630 if (!st->chip_config.accl_en) {
631 result = inv_mpu6050_switch_engine(st, true,
632 INV_MPU6050_SENSOR_ACCL);
634 goto error_power_off;
635 /* wait 1 period for first sample availability */
636 min_sleep_us = period_us;
637 max_sleep_us = period_us + period_us / 2;
638 usleep_range(min_sleep_us, max_sleep_us);
640 ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
641 chan->channel2, val);
644 /* temperature sensor work only with accel and/or gyro */
645 if (!st->chip_config.accl_en && !st->chip_config.gyro_en) {
647 goto error_power_off;
649 if (!st->chip_config.temp_en) {
650 result = inv_mpu6050_switch_engine(st, true,
651 INV_MPU6050_SENSOR_TEMP);
653 goto error_power_off;
654 /* wait 1 period for first sample availability */
655 min_sleep_us = period_us;
656 max_sleep_us = period_us + period_us / 2;
657 usleep_range(min_sleep_us, max_sleep_us);
659 ret = inv_mpu6050_sensor_show(st, st->reg->temperature,
663 if (!st->chip_config.magn_en) {
664 result = inv_mpu6050_switch_engine(st, true,
665 INV_MPU6050_SENSOR_MAGN);
667 goto error_power_off;
668 /* frequency is limited for magnetometer */
669 if (freq_hz > INV_MPU_MAGN_FREQ_HZ_MAX) {
670 freq_hz = INV_MPU_MAGN_FREQ_HZ_MAX;
671 period_us = 1000000 / freq_hz;
673 /* need to wait 2 periods to have first valid sample */
674 min_sleep_us = 2 * period_us;
675 max_sleep_us = 2 * (period_us + period_us / 2);
676 usleep_range(min_sleep_us, max_sleep_us);
678 ret = inv_mpu_magn_read(st, chan->channel2, val);
685 pm_runtime_mark_last_busy(pdev);
686 pm_runtime_put_autosuspend(pdev);
691 pm_runtime_put_autosuspend(pdev);
696 inv_mpu6050_read_raw(struct iio_dev *indio_dev,
697 struct iio_chan_spec const *chan,
698 int *val, int *val2, long mask)
700 struct inv_mpu6050_state *st = iio_priv(indio_dev);
704 case IIO_CHAN_INFO_RAW:
705 ret = iio_device_claim_direct_mode(indio_dev);
708 mutex_lock(&st->lock);
709 ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
710 mutex_unlock(&st->lock);
711 iio_device_release_direct_mode(indio_dev);
713 case IIO_CHAN_INFO_SCALE:
714 switch (chan->type) {
716 mutex_lock(&st->lock);
718 *val2 = gyro_scale_6050[st->chip_config.fsr];
719 mutex_unlock(&st->lock);
721 return IIO_VAL_INT_PLUS_NANO;
723 mutex_lock(&st->lock);
725 *val2 = accel_scale[st->chip_config.accl_fs];
726 mutex_unlock(&st->lock);
728 return IIO_VAL_INT_PLUS_MICRO;
730 *val = st->hw->temp.scale / 1000000;
731 *val2 = st->hw->temp.scale % 1000000;
732 return IIO_VAL_INT_PLUS_MICRO;
734 return inv_mpu_magn_get_scale(st, chan, val, val2);
738 case IIO_CHAN_INFO_OFFSET:
739 switch (chan->type) {
741 *val = st->hw->temp.offset;
746 case IIO_CHAN_INFO_CALIBBIAS:
747 switch (chan->type) {
749 mutex_lock(&st->lock);
750 ret = inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
751 chan->channel2, val);
752 mutex_unlock(&st->lock);
755 mutex_lock(&st->lock);
756 ret = inv_mpu6050_sensor_show(st, st->reg->accl_offset,
757 chan->channel2, val);
758 mutex_unlock(&st->lock);
769 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state *st, int val,
777 for (i = 0; i < ARRAY_SIZE(gyro_scale_6050); ++i) {
778 if (gyro_scale_6050[i] == val2) {
779 result = inv_mpu6050_set_gyro_fsr(st, i);
783 st->chip_config.fsr = i;
791 static int inv_write_raw_get_fmt(struct iio_dev *indio_dev,
792 struct iio_chan_spec const *chan, long mask)
795 case IIO_CHAN_INFO_SCALE:
796 switch (chan->type) {
798 return IIO_VAL_INT_PLUS_NANO;
800 return IIO_VAL_INT_PLUS_MICRO;
803 return IIO_VAL_INT_PLUS_MICRO;
809 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state *st, int val,
818 for (i = 0; i < ARRAY_SIZE(accel_scale); ++i) {
819 if (accel_scale[i] == val2) {
820 d = (i << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
821 result = regmap_write(st->map, st->reg->accl_config, d);
825 st->chip_config.accl_fs = i;
833 static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
834 struct iio_chan_spec const *chan,
835 int val, int val2, long mask)
837 struct inv_mpu6050_state *st = iio_priv(indio_dev);
838 struct device *pdev = regmap_get_device(st->map);
842 * we should only update scale when the chip is disabled, i.e.
845 result = iio_device_claim_direct_mode(indio_dev);
849 mutex_lock(&st->lock);
850 result = pm_runtime_resume_and_get(pdev);
852 goto error_write_raw_unlock;
855 case IIO_CHAN_INFO_SCALE:
856 switch (chan->type) {
858 result = inv_mpu6050_write_gyro_scale(st, val, val2);
861 result = inv_mpu6050_write_accel_scale(st, val, val2);
868 case IIO_CHAN_INFO_CALIBBIAS:
869 switch (chan->type) {
871 result = inv_mpu6050_sensor_set(st,
872 st->reg->gyro_offset,
873 chan->channel2, val);
876 result = inv_mpu6050_sensor_set(st,
877 st->reg->accl_offset,
878 chan->channel2, val);
890 pm_runtime_mark_last_busy(pdev);
891 pm_runtime_put_autosuspend(pdev);
892 error_write_raw_unlock:
893 mutex_unlock(&st->lock);
894 iio_device_release_direct_mode(indio_dev);
900 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
902 * Based on the Nyquist principle, the bandwidth of the low
903 * pass filter must not exceed the signal sampling rate divided
904 * by 2, or there would be aliasing.
905 * This function basically search for the correct low pass
906 * parameters based on the fifo rate, e.g, sampling frequency.
908 * lpf is set automatically when setting sampling rate to avoid any aliases.
910 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
912 static const int hz[] = {400, 200, 90, 40, 20, 10};
913 static const int d[] = {
914 INV_MPU6050_FILTER_200HZ, INV_MPU6050_FILTER_100HZ,
915 INV_MPU6050_FILTER_45HZ, INV_MPU6050_FILTER_20HZ,
916 INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ
921 data = INV_MPU6050_FILTER_5HZ;
922 for (i = 0; i < ARRAY_SIZE(hz); ++i) {
928 result = inv_mpu6050_set_lpf_regs(st, data);
931 st->chip_config.lpf = data;
937 * inv_mpu6050_fifo_rate_store() - Set fifo rate.
940 inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
941 const char *buf, size_t count)
948 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
949 struct inv_mpu6050_state *st = iio_priv(indio_dev);
950 struct device *pdev = regmap_get_device(st->map);
952 if (kstrtoint(buf, 10, &fifo_rate))
954 if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
955 fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
958 /* compute the chip sample rate divider */
959 d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate);
960 /* compute back the fifo rate to handle truncation cases */
961 fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(d);
962 fifo_period = NSEC_PER_SEC / fifo_rate;
964 mutex_lock(&st->lock);
965 if (d == st->chip_config.divider) {
967 goto fifo_rate_fail_unlock;
970 fifo_on = st->chip_config.accl_fifo_enable ||
971 st->chip_config.gyro_fifo_enable ||
972 st->chip_config.magn_fifo_enable;
973 result = inv_sensors_timestamp_update_odr(&st->timestamp, fifo_period, fifo_on);
975 goto fifo_rate_fail_unlock;
977 result = pm_runtime_resume_and_get(pdev);
979 goto fifo_rate_fail_unlock;
981 result = regmap_write(st->map, st->reg->sample_rate_div, d);
983 goto fifo_rate_fail_power_off;
984 st->chip_config.divider = d;
986 result = inv_mpu6050_set_lpf(st, fifo_rate);
988 goto fifo_rate_fail_power_off;
990 /* update rate for magn, noop if not present in chip */
991 result = inv_mpu_magn_set_rate(st, fifo_rate);
993 goto fifo_rate_fail_power_off;
995 pm_runtime_mark_last_busy(pdev);
996 fifo_rate_fail_power_off:
997 pm_runtime_put_autosuspend(pdev);
998 fifo_rate_fail_unlock:
999 mutex_unlock(&st->lock);
1007 * inv_fifo_rate_show() - Get the current sampling rate.
1010 inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
1013 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
1016 mutex_lock(&st->lock);
1017 fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
1018 mutex_unlock(&st->lock);
1020 return scnprintf(buf, PAGE_SIZE, "%u\n", fifo_rate);
1024 * inv_attr_show() - calling this function will show current
1027 * Deprecated in favor of IIO mounting matrix API.
1029 * See inv_get_mount_matrix()
1031 static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr,
1034 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
1035 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1038 switch (this_attr->address) {
1040 * In MPU6050, the two matrix are the same because gyro and accel
1041 * are integrated in one chip
1043 case ATTR_GYRO_MATRIX:
1044 case ATTR_ACCL_MATRIX:
1045 m = st->plat_data.orientation;
1047 return scnprintf(buf, PAGE_SIZE,
1048 "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
1049 m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1056 * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
1058 * @indio_dev: The IIO device
1059 * @trig: The new trigger
1061 * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
1062 * device, -EINVAL otherwise.
1064 static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
1065 struct iio_trigger *trig)
1067 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1069 if (st->trig != trig)
1075 static const struct iio_mount_matrix *
1076 inv_get_mount_matrix(const struct iio_dev *indio_dev,
1077 const struct iio_chan_spec *chan)
1079 struct inv_mpu6050_state *data = iio_priv(indio_dev);
1080 const struct iio_mount_matrix *matrix;
1082 if (chan->type == IIO_MAGN)
1083 matrix = &data->magn_orient;
1085 matrix = &data->orientation;
1090 static const struct iio_chan_spec_ext_info inv_ext_info[] = {
1091 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix),
1095 #define INV_MPU6050_CHAN(_type, _channel2, _index) \
1099 .channel2 = _channel2, \
1100 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1101 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1102 BIT(IIO_CHAN_INFO_CALIBBIAS), \
1103 .scan_index = _index, \
1107 .storagebits = 16, \
1109 .endianness = IIO_BE, \
1111 .ext_info = inv_ext_info, \
1114 #define INV_MPU6050_TEMP_CHAN(_index) \
1117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
1118 | BIT(IIO_CHAN_INFO_OFFSET) \
1119 | BIT(IIO_CHAN_INFO_SCALE), \
1120 .scan_index = _index, \
1124 .storagebits = 16, \
1126 .endianness = IIO_BE, \
1130 static const struct iio_chan_spec inv_mpu_channels[] = {
1131 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
1133 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1135 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1136 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1137 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1139 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1140 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1141 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1144 #define INV_MPU6050_SCAN_MASK_3AXIS_ACCEL \
1145 (BIT(INV_MPU6050_SCAN_ACCL_X) \
1146 | BIT(INV_MPU6050_SCAN_ACCL_Y) \
1147 | BIT(INV_MPU6050_SCAN_ACCL_Z))
1149 #define INV_MPU6050_SCAN_MASK_3AXIS_GYRO \
1150 (BIT(INV_MPU6050_SCAN_GYRO_X) \
1151 | BIT(INV_MPU6050_SCAN_GYRO_Y) \
1152 | BIT(INV_MPU6050_SCAN_GYRO_Z))
1154 #define INV_MPU6050_SCAN_MASK_TEMP (BIT(INV_MPU6050_SCAN_TEMP))
1156 static const unsigned long inv_mpu_scan_masks[] = {
1158 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
1159 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1161 INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1162 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1163 /* 6-axis accel + gyro */
1164 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1165 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1166 | INV_MPU6050_SCAN_MASK_TEMP,
1170 #define INV_MPU9X50_MAGN_CHAN(_chan2, _bits, _index) \
1174 .channel2 = _chan2, \
1175 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) | \
1176 BIT(IIO_CHAN_INFO_RAW), \
1177 .scan_index = _index, \
1180 .realbits = _bits, \
1181 .storagebits = 16, \
1183 .endianness = IIO_BE, \
1185 .ext_info = inv_ext_info, \
1188 static const struct iio_chan_spec inv_mpu9150_channels[] = {
1189 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
1191 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1193 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1194 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1195 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1197 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1198 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1199 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1201 /* Magnetometer resolution is 13 bits */
1202 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 13, INV_MPU9X50_SCAN_MAGN_X),
1203 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 13, INV_MPU9X50_SCAN_MAGN_Y),
1204 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 13, INV_MPU9X50_SCAN_MAGN_Z),
1207 static const struct iio_chan_spec inv_mpu9250_channels[] = {
1208 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
1210 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1212 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1213 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1214 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1216 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1217 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1218 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1220 /* Magnetometer resolution is 16 bits */
1221 INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 16, INV_MPU9X50_SCAN_MAGN_X),
1222 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 16, INV_MPU9X50_SCAN_MAGN_Y),
1223 INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 16, INV_MPU9X50_SCAN_MAGN_Z),
1226 #define INV_MPU9X50_SCAN_MASK_3AXIS_MAGN \
1227 (BIT(INV_MPU9X50_SCAN_MAGN_X) \
1228 | BIT(INV_MPU9X50_SCAN_MAGN_Y) \
1229 | BIT(INV_MPU9X50_SCAN_MAGN_Z))
1231 static const unsigned long inv_mpu9x50_scan_masks[] = {
1233 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
1234 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1236 INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1237 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1239 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1240 INV_MPU9X50_SCAN_MASK_3AXIS_MAGN | INV_MPU6050_SCAN_MASK_TEMP,
1241 /* 6-axis accel + gyro */
1242 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1243 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1244 | INV_MPU6050_SCAN_MASK_TEMP,
1245 /* 6-axis accel + magn */
1246 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1247 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1248 | INV_MPU6050_SCAN_MASK_TEMP,
1249 /* 6-axis gyro + magn */
1250 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1251 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1252 | INV_MPU6050_SCAN_MASK_TEMP,
1253 /* 9-axis accel + gyro + magn */
1254 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1255 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1256 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1257 | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1258 | INV_MPU6050_SCAN_MASK_TEMP,
1262 static const unsigned long inv_icm20602_scan_masks[] = {
1263 /* 3-axis accel + temp (mandatory) */
1264 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1265 /* 3-axis gyro + temp (mandatory) */
1266 INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1267 /* 6-axis accel + gyro + temp (mandatory) */
1268 INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1269 | INV_MPU6050_SCAN_MASK_TEMP,
1274 * The user can choose any frequency between INV_MPU6050_MIN_FIFO_RATE and
1275 * INV_MPU6050_MAX_FIFO_RATE, but only these frequencies are matched by the
1276 * low-pass filter. Specifically, each of these sampling rates are about twice
1277 * the bandwidth of a corresponding low-pass filter, which should eliminate
1278 * aliasing following the Nyquist principle. By picking a frequency different
1279 * from these, the user risks aliasing effects.
1281 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
1282 static IIO_CONST_ATTR(in_anglvel_scale_available,
1283 "0.000133090 0.000266181 0.000532362 0.001064724");
1284 static IIO_CONST_ATTR(in_accel_scale_available,
1285 "0.000598 0.001196 0.002392 0.004785");
1286 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
1287 inv_mpu6050_fifo_rate_store);
1289 /* Deprecated: kept for userspace backward compatibility. */
1290 static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
1292 static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
1295 static struct attribute *inv_attributes[] = {
1296 &iio_dev_attr_in_gyro_matrix.dev_attr.attr, /* deprecated */
1297 &iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */
1298 &iio_dev_attr_sampling_frequency.dev_attr.attr,
1299 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1300 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
1301 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
1305 static const struct attribute_group inv_attribute_group = {
1306 .attrs = inv_attributes
1309 static int inv_mpu6050_reg_access(struct iio_dev *indio_dev,
1311 unsigned int writeval,
1312 unsigned int *readval)
1314 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1317 mutex_lock(&st->lock);
1319 ret = regmap_read(st->map, reg, readval);
1321 ret = regmap_write(st->map, reg, writeval);
1322 mutex_unlock(&st->lock);
1327 static const struct iio_info mpu_info = {
1328 .read_raw = &inv_mpu6050_read_raw,
1329 .write_raw = &inv_mpu6050_write_raw,
1330 .write_raw_get_fmt = &inv_write_raw_get_fmt,
1331 .attrs = &inv_attribute_group,
1332 .validate_trigger = inv_mpu6050_validate_trigger,
1333 .debugfs_reg_access = &inv_mpu6050_reg_access,
1337 * inv_check_and_setup_chip() - check and setup chip.
1339 static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
1342 unsigned int regval, mask;
1345 st->hw = &hw_info[st->chip_type];
1346 st->reg = hw_info[st->chip_type].reg;
1347 memcpy(&st->chip_config, hw_info[st->chip_type].config,
1348 sizeof(st->chip_config));
1349 st->data = devm_kzalloc(regmap_get_device(st->map), st->hw->fifo_size, GFP_KERNEL);
1350 if (st->data == NULL)
1353 /* check chip self-identification */
1354 result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, ®val);
1357 if (regval != st->hw->whoami) {
1358 /* check whoami against all possible values */
1359 for (i = 0; i < INV_NUM_PARTS; ++i) {
1360 if (regval == hw_info[i].whoami) {
1361 dev_warn(regmap_get_device(st->map),
1362 "whoami mismatch got 0x%02x (%s) expected 0x%02x (%s)\n",
1363 regval, hw_info[i].name,
1364 st->hw->whoami, st->hw->name);
1368 if (i >= INV_NUM_PARTS) {
1369 dev_err(regmap_get_device(st->map),
1370 "invalid whoami 0x%02x expected 0x%02x (%s)\n",
1371 regval, st->hw->whoami, st->hw->name);
1376 /* reset to make sure previous state are not there */
1377 result = regmap_write(st->map, st->reg->pwr_mgmt_1,
1378 INV_MPU6050_BIT_H_RESET);
1381 msleep(INV_MPU6050_POWER_UP_TIME);
1382 switch (st->chip_type) {
1389 /* reset signal path (required for spi connection) */
1390 regval = INV_MPU6050_BIT_TEMP_RST | INV_MPU6050_BIT_ACCEL_RST |
1391 INV_MPU6050_BIT_GYRO_RST;
1392 result = regmap_write(st->map, INV_MPU6050_REG_SIGNAL_PATH_RESET,
1396 msleep(INV_MPU6050_POWER_UP_TIME);
1403 * Turn power on. After reset, the sleep bit could be on
1404 * or off depending on the OTP settings. Turning power on
1405 * make it in a definite state as well as making the hardware
1406 * state align with the software state
1408 result = inv_mpu6050_set_power_itg(st, true);
1411 mask = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
1412 INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN;
1413 result = inv_mpu6050_switch_engine(st, false, mask);
1415 goto error_power_off;
1420 inv_mpu6050_set_power_itg(st, false);
1424 static int inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state *st)
1428 result = regulator_enable(st->vddio_supply);
1430 dev_err(regmap_get_device(st->map),
1431 "Failed to enable vddio regulator: %d\n", result);
1433 /* Give the device a little bit of time to start up. */
1434 usleep_range(3000, 5000);
1440 static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
1444 result = regulator_disable(st->vddio_supply);
1446 dev_err(regmap_get_device(st->map),
1447 "Failed to disable vddio regulator: %d\n", result);
1452 static void inv_mpu_core_disable_regulator_action(void *_data)
1454 struct inv_mpu6050_state *st = _data;
1457 result = regulator_disable(st->vdd_supply);
1459 dev_err(regmap_get_device(st->map),
1460 "Failed to disable vdd regulator: %d\n", result);
1462 inv_mpu_core_disable_regulator_vddio(st);
1465 static void inv_mpu_pm_disable(void *data)
1467 struct device *dev = data;
1469 pm_runtime_disable(dev);
1472 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
1473 int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
1475 struct inv_mpu6050_state *st;
1476 struct iio_dev *indio_dev;
1477 struct inv_mpu6050_platform_data *pdata;
1478 struct device *dev = regmap_get_device(regmap);
1480 struct irq_data *desc;
1483 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1487 BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS);
1488 if (chip_type < 0 || chip_type >= INV_NUM_PARTS) {
1489 dev_err(dev, "Bad invensense chip_type=%d name=%s\n",
1493 st = iio_priv(indio_dev);
1494 mutex_init(&st->lock);
1495 st->chip_type = chip_type;
1499 st->level_shifter = device_property_read_bool(dev,
1500 "invensense,level-shifter");
1501 pdata = dev_get_platdata(dev);
1503 result = iio_read_mount_matrix(dev, &st->orientation);
1505 dev_err(dev, "Failed to retrieve mounting matrix %d\n",
1510 st->plat_data = *pdata;
1514 desc = irq_get_irq_data(irq);
1516 dev_err(dev, "Could not find IRQ %d\n", irq);
1520 irq_type = irqd_get_trigger_type(desc);
1522 irq_type = IRQF_TRIGGER_RISING;
1524 /* Doesn't really matter, use the default */
1525 irq_type = IRQF_TRIGGER_RISING;
1528 if (irq_type & IRQF_TRIGGER_RISING) // rising or both-edge
1529 st->irq_mask = INV_MPU6050_ACTIVE_HIGH;
1530 else if (irq_type == IRQF_TRIGGER_FALLING)
1531 st->irq_mask = INV_MPU6050_ACTIVE_LOW;
1532 else if (irq_type == IRQF_TRIGGER_HIGH)
1533 st->irq_mask = INV_MPU6050_ACTIVE_HIGH |
1534 INV_MPU6050_LATCH_INT_EN;
1535 else if (irq_type == IRQF_TRIGGER_LOW)
1536 st->irq_mask = INV_MPU6050_ACTIVE_LOW |
1537 INV_MPU6050_LATCH_INT_EN;
1539 dev_err(dev, "Invalid interrupt type 0x%x specified\n",
1544 st->vdd_supply = devm_regulator_get(dev, "vdd");
1545 if (IS_ERR(st->vdd_supply))
1546 return dev_err_probe(dev, PTR_ERR(st->vdd_supply),
1547 "Failed to get vdd regulator\n");
1549 st->vddio_supply = devm_regulator_get(dev, "vddio");
1550 if (IS_ERR(st->vddio_supply))
1551 return dev_err_probe(dev, PTR_ERR(st->vddio_supply),
1552 "Failed to get vddio regulator\n");
1554 result = regulator_enable(st->vdd_supply);
1556 dev_err(dev, "Failed to enable vdd regulator: %d\n", result);
1559 msleep(INV_MPU6050_POWER_UP_TIME);
1561 result = inv_mpu_core_enable_regulator_vddio(st);
1563 regulator_disable(st->vdd_supply);
1567 result = devm_add_action_or_reset(dev, inv_mpu_core_disable_regulator_action,
1570 dev_err(dev, "Failed to setup regulator cleanup action %d\n",
1575 /* fill magnetometer orientation */
1576 result = inv_mpu_magn_set_orient(st);
1580 /* power is turned on inside check chip type*/
1581 result = inv_check_and_setup_chip(st);
1585 result = inv_mpu6050_init_config(indio_dev);
1587 dev_err(dev, "Could not initialize device.\n");
1588 goto error_power_off;
1591 dev_set_drvdata(dev, indio_dev);
1592 /* name will be NULL when enumerated via ACPI */
1594 indio_dev->name = name;
1596 indio_dev->name = dev_name(dev);
1598 /* requires parent device set in indio_dev */
1599 if (inv_mpu_bus_setup) {
1600 result = inv_mpu_bus_setup(indio_dev);
1602 goto error_power_off;
1605 /* chip init is done, turning on runtime power management */
1606 result = pm_runtime_set_active(dev);
1608 goto error_power_off;
1609 pm_runtime_get_noresume(dev);
1610 pm_runtime_enable(dev);
1611 pm_runtime_set_autosuspend_delay(dev, INV_MPU6050_SUSPEND_DELAY_MS);
1612 pm_runtime_use_autosuspend(dev);
1613 pm_runtime_put(dev);
1614 result = devm_add_action_or_reset(dev, inv_mpu_pm_disable, dev);
1618 switch (chip_type) {
1620 indio_dev->channels = inv_mpu9150_channels;
1621 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9150_channels);
1622 indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1626 indio_dev->channels = inv_mpu9250_channels;
1627 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9250_channels);
1628 indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1632 indio_dev->channels = inv_mpu_channels;
1633 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1634 indio_dev->available_scan_masks = inv_icm20602_scan_masks;
1637 indio_dev->channels = inv_mpu_channels;
1638 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1639 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1643 * Use magnetometer inside the chip only if there is no i2c
1644 * auxiliary device in use. Otherwise Going back to 6-axis only.
1646 if (st->magn_disabled) {
1647 indio_dev->channels = inv_mpu_channels;
1648 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1649 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1652 indio_dev->info = &mpu_info;
1656 * The driver currently only supports buffered capture with its
1657 * own trigger. So no IRQ, no trigger, no buffer
1659 result = devm_iio_triggered_buffer_setup(dev, indio_dev,
1660 iio_pollfunc_store_time,
1661 inv_mpu6050_read_fifo,
1664 dev_err(dev, "configure buffer fail %d\n", result);
1668 result = inv_mpu6050_probe_trigger(indio_dev, irq_type);
1670 dev_err(dev, "trigger probe fail %d\n", result);
1675 result = devm_iio_device_register(dev, indio_dev);
1677 dev_err(dev, "IIO register fail %d\n", result);
1684 inv_mpu6050_set_power_itg(st, false);
1687 EXPORT_SYMBOL_NS_GPL(inv_mpu_core_probe, IIO_MPU6050);
1689 static int inv_mpu_resume(struct device *dev)
1691 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1692 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1695 mutex_lock(&st->lock);
1696 result = inv_mpu_core_enable_regulator_vddio(st);
1700 result = inv_mpu6050_set_power_itg(st, true);
1704 pm_runtime_disable(dev);
1705 pm_runtime_set_active(dev);
1706 pm_runtime_enable(dev);
1708 result = inv_mpu6050_switch_engine(st, true, st->suspended_sensors);
1712 if (iio_buffer_enabled(indio_dev))
1713 result = inv_mpu6050_prepare_fifo(st, true);
1716 mutex_unlock(&st->lock);
1721 static int inv_mpu_suspend(struct device *dev)
1723 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1724 struct inv_mpu6050_state *st = iio_priv(indio_dev);
1727 mutex_lock(&st->lock);
1729 st->suspended_sensors = 0;
1730 if (pm_runtime_suspended(dev)) {
1735 if (iio_buffer_enabled(indio_dev)) {
1736 result = inv_mpu6050_prepare_fifo(st, false);
1741 if (st->chip_config.accl_en)
1742 st->suspended_sensors |= INV_MPU6050_SENSOR_ACCL;
1743 if (st->chip_config.gyro_en)
1744 st->suspended_sensors |= INV_MPU6050_SENSOR_GYRO;
1745 if (st->chip_config.temp_en)
1746 st->suspended_sensors |= INV_MPU6050_SENSOR_TEMP;
1747 if (st->chip_config.magn_en)
1748 st->suspended_sensors |= INV_MPU6050_SENSOR_MAGN;
1749 result = inv_mpu6050_switch_engine(st, false, st->suspended_sensors);
1753 result = inv_mpu6050_set_power_itg(st, false);
1757 inv_mpu_core_disable_regulator_vddio(st);
1759 mutex_unlock(&st->lock);
1764 static int inv_mpu_runtime_suspend(struct device *dev)
1766 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1767 unsigned int sensors;
1770 mutex_lock(&st->lock);
1772 sensors = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
1773 INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN;
1774 ret = inv_mpu6050_switch_engine(st, false, sensors);
1778 ret = inv_mpu6050_set_power_itg(st, false);
1782 inv_mpu_core_disable_regulator_vddio(st);
1785 mutex_unlock(&st->lock);
1789 static int inv_mpu_runtime_resume(struct device *dev)
1791 struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1794 ret = inv_mpu_core_enable_regulator_vddio(st);
1798 return inv_mpu6050_set_power_itg(st, true);
1801 EXPORT_NS_GPL_DEV_PM_OPS(inv_mpu_pmops, IIO_MPU6050) = {
1802 SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
1803 RUNTIME_PM_OPS(inv_mpu_runtime_suspend, inv_mpu_runtime_resume, NULL)
1806 MODULE_AUTHOR("Invensense Corporation");
1807 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
1808 MODULE_LICENSE("GPL");
1809 MODULE_IMPORT_NS(IIO_INV_SENSORS_TIMESTAMP);