1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 Invensense, Inc.
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/delay.h>
11 #include <linux/mutex.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
19 #include <linux/iio/iio.h>
21 #include "inv_icm42600.h"
22 #include "inv_icm42600_buffer.h"
24 static const struct regmap_range_cfg inv_icm42600_regmap_ranges
[] = {
29 .selector_reg
= INV_ICM42600_REG_BANK_SEL
,
30 .selector_mask
= INV_ICM42600_BANK_SEL_MASK
,
37 static const struct regmap_range inv_icm42600_regmap_volatile_yes_ranges
[] = {
38 /* Sensor data registers */
39 regmap_reg_range(0x001D, 0x002A),
40 /* INT status, FIFO, APEX data */
41 regmap_reg_range(0x002D, 0x0038),
42 /* Signal path reset */
43 regmap_reg_range(0x004B, 0x004B),
44 /* FIFO lost packets */
45 regmap_reg_range(0x006C, 0x006D),
47 regmap_reg_range(0x1062, 0x1064),
50 static const struct regmap_range inv_icm42600_regmap_volatile_no_ranges
[] = {
51 regmap_reg_range(0x0000, 0x001C),
52 regmap_reg_range(0x006E, 0x1061),
53 regmap_reg_range(0x1065, 0x4FFF),
56 static const struct regmap_access_table inv_icm42600_regmap_volatile_accesses
[] = {
58 .yes_ranges
= inv_icm42600_regmap_volatile_yes_ranges
,
59 .n_yes_ranges
= ARRAY_SIZE(inv_icm42600_regmap_volatile_yes_ranges
),
60 .no_ranges
= inv_icm42600_regmap_volatile_no_ranges
,
61 .n_no_ranges
= ARRAY_SIZE(inv_icm42600_regmap_volatile_no_ranges
),
65 static const struct regmap_range inv_icm42600_regmap_rd_noinc_no_ranges
[] = {
66 regmap_reg_range(0x0000, INV_ICM42600_REG_FIFO_DATA
- 1),
67 regmap_reg_range(INV_ICM42600_REG_FIFO_DATA
+ 1, 0x4FFF),
70 static const struct regmap_access_table inv_icm42600_regmap_rd_noinc_accesses
[] = {
72 .no_ranges
= inv_icm42600_regmap_rd_noinc_no_ranges
,
73 .n_no_ranges
= ARRAY_SIZE(inv_icm42600_regmap_rd_noinc_no_ranges
),
77 const struct regmap_config inv_icm42600_regmap_config
= {
78 .name
= "inv_icm42600",
81 .max_register
= 0x4FFF,
82 .ranges
= inv_icm42600_regmap_ranges
,
83 .num_ranges
= ARRAY_SIZE(inv_icm42600_regmap_ranges
),
84 .volatile_table
= inv_icm42600_regmap_volatile_accesses
,
85 .rd_noinc_table
= inv_icm42600_regmap_rd_noinc_accesses
,
86 .cache_type
= REGCACHE_RBTREE
,
88 EXPORT_SYMBOL_NS_GPL(inv_icm42600_regmap_config
, "IIO_ICM42600");
90 struct inv_icm42600_hw
{
93 const struct inv_icm42600_conf
*conf
;
96 /* chip initial default configuration */
97 static const struct inv_icm42600_conf inv_icm42600_default_conf
= {
99 .mode
= INV_ICM42600_SENSOR_MODE_OFF
,
100 .fs
= INV_ICM42600_GYRO_FS_2000DPS
,
101 .odr
= INV_ICM42600_ODR_50HZ
,
102 .filter
= INV_ICM42600_FILTER_BW_ODR_DIV_2
,
105 .mode
= INV_ICM42600_SENSOR_MODE_OFF
,
106 .fs
= INV_ICM42600_ACCEL_FS_16G
,
107 .odr
= INV_ICM42600_ODR_50HZ
,
108 .filter
= INV_ICM42600_FILTER_BW_ODR_DIV_2
,
113 static const struct inv_icm42600_conf inv_icm42686_default_conf
= {
115 .mode
= INV_ICM42600_SENSOR_MODE_OFF
,
116 .fs
= INV_ICM42686_GYRO_FS_4000DPS
,
117 .odr
= INV_ICM42600_ODR_50HZ
,
118 .filter
= INV_ICM42600_FILTER_BW_ODR_DIV_2
,
121 .mode
= INV_ICM42600_SENSOR_MODE_OFF
,
122 .fs
= INV_ICM42686_ACCEL_FS_32G
,
123 .odr
= INV_ICM42600_ODR_50HZ
,
124 .filter
= INV_ICM42600_FILTER_BW_ODR_DIV_2
,
129 static const struct inv_icm42600_hw inv_icm42600_hw
[INV_CHIP_NB
] = {
130 [INV_CHIP_ICM42600
] = {
131 .whoami
= INV_ICM42600_WHOAMI_ICM42600
,
133 .conf
= &inv_icm42600_default_conf
,
135 [INV_CHIP_ICM42602
] = {
136 .whoami
= INV_ICM42600_WHOAMI_ICM42602
,
138 .conf
= &inv_icm42600_default_conf
,
140 [INV_CHIP_ICM42605
] = {
141 .whoami
= INV_ICM42600_WHOAMI_ICM42605
,
143 .conf
= &inv_icm42600_default_conf
,
145 [INV_CHIP_ICM42686
] = {
146 .whoami
= INV_ICM42600_WHOAMI_ICM42686
,
148 .conf
= &inv_icm42686_default_conf
,
150 [INV_CHIP_ICM42622
] = {
151 .whoami
= INV_ICM42600_WHOAMI_ICM42622
,
153 .conf
= &inv_icm42600_default_conf
,
155 [INV_CHIP_ICM42688
] = {
156 .whoami
= INV_ICM42600_WHOAMI_ICM42688
,
158 .conf
= &inv_icm42600_default_conf
,
160 [INV_CHIP_ICM42631
] = {
161 .whoami
= INV_ICM42600_WHOAMI_ICM42631
,
163 .conf
= &inv_icm42600_default_conf
,
167 const struct iio_mount_matrix
*
168 inv_icm42600_get_mount_matrix(const struct iio_dev
*indio_dev
,
169 const struct iio_chan_spec
*chan
)
171 const struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
173 return &st
->orientation
;
176 uint32_t inv_icm42600_odr_to_period(enum inv_icm42600_odr odr
)
178 static uint32_t odr_periods
[INV_ICM42600_ODR_NB
] = {
179 /* reserved values */
209 return odr_periods
[odr
];
212 static int inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state
*st
,
213 enum inv_icm42600_sensor_mode gyro
,
214 enum inv_icm42600_sensor_mode accel
,
215 bool temp
, unsigned int *sleep_ms
)
217 enum inv_icm42600_sensor_mode oldgyro
= st
->conf
.gyro
.mode
;
218 enum inv_icm42600_sensor_mode oldaccel
= st
->conf
.accel
.mode
;
219 bool oldtemp
= st
->conf
.temp_en
;
220 unsigned int sleepval
;
224 /* if nothing changed, exit */
225 if (gyro
== oldgyro
&& accel
== oldaccel
&& temp
== oldtemp
)
228 val
= INV_ICM42600_PWR_MGMT0_GYRO(gyro
) |
229 INV_ICM42600_PWR_MGMT0_ACCEL(accel
);
231 val
|= INV_ICM42600_PWR_MGMT0_TEMP_DIS
;
232 ret
= regmap_write(st
->map
, INV_ICM42600_REG_PWR_MGMT0
, val
);
236 st
->conf
.gyro
.mode
= gyro
;
237 st
->conf
.accel
.mode
= accel
;
238 st
->conf
.temp_en
= temp
;
240 /* compute required wait time for sensors to stabilize */
242 /* temperature stabilization time */
243 if (temp
&& !oldtemp
) {
244 if (sleepval
< INV_ICM42600_TEMP_STARTUP_TIME_MS
)
245 sleepval
= INV_ICM42600_TEMP_STARTUP_TIME_MS
;
247 /* accel startup time */
248 if (accel
!= oldaccel
&& oldaccel
== INV_ICM42600_SENSOR_MODE_OFF
) {
249 /* block any register write for at least 200 µs */
250 usleep_range(200, 300);
251 if (sleepval
< INV_ICM42600_ACCEL_STARTUP_TIME_MS
)
252 sleepval
= INV_ICM42600_ACCEL_STARTUP_TIME_MS
;
254 if (gyro
!= oldgyro
) {
255 /* gyro startup time */
256 if (oldgyro
== INV_ICM42600_SENSOR_MODE_OFF
) {
257 /* block any register write for at least 200 µs */
258 usleep_range(200, 300);
259 if (sleepval
< INV_ICM42600_GYRO_STARTUP_TIME_MS
)
260 sleepval
= INV_ICM42600_GYRO_STARTUP_TIME_MS
;
262 } else if (gyro
== INV_ICM42600_SENSOR_MODE_OFF
) {
263 if (sleepval
< INV_ICM42600_GYRO_STOP_TIME_MS
)
264 sleepval
= INV_ICM42600_GYRO_STOP_TIME_MS
;
268 /* deferred sleep value if sleep pointer is provided or direct sleep */
270 *sleep_ms
= sleepval
;
277 int inv_icm42600_set_accel_conf(struct inv_icm42600_state
*st
,
278 struct inv_icm42600_sensor_conf
*conf
,
279 unsigned int *sleep_ms
)
281 struct inv_icm42600_sensor_conf
*oldconf
= &st
->conf
.accel
;
285 /* Sanitize missing values with current values */
287 conf
->mode
= oldconf
->mode
;
289 conf
->fs
= oldconf
->fs
;
291 conf
->odr
= oldconf
->odr
;
292 if (conf
->filter
< 0)
293 conf
->filter
= oldconf
->filter
;
295 /* force power mode against ODR when sensor is on */
296 switch (conf
->mode
) {
297 case INV_ICM42600_SENSOR_MODE_LOW_POWER
:
298 case INV_ICM42600_SENSOR_MODE_LOW_NOISE
:
299 if (conf
->odr
<= INV_ICM42600_ODR_1KHZ_LN
) {
300 conf
->mode
= INV_ICM42600_SENSOR_MODE_LOW_NOISE
;
301 conf
->filter
= INV_ICM42600_FILTER_BW_ODR_DIV_2
;
302 } else if (conf
->odr
>= INV_ICM42600_ODR_6_25HZ_LP
&&
303 conf
->odr
<= INV_ICM42600_ODR_1_5625HZ_LP
) {
304 conf
->mode
= INV_ICM42600_SENSOR_MODE_LOW_POWER
;
305 conf
->filter
= INV_ICM42600_FILTER_AVG_16X
;
312 /* set ACCEL_CONFIG0 register (accel fullscale & odr) */
313 if (conf
->fs
!= oldconf
->fs
|| conf
->odr
!= oldconf
->odr
) {
314 val
= INV_ICM42600_ACCEL_CONFIG0_FS(conf
->fs
) |
315 INV_ICM42600_ACCEL_CONFIG0_ODR(conf
->odr
);
316 ret
= regmap_write(st
->map
, INV_ICM42600_REG_ACCEL_CONFIG0
, val
);
319 oldconf
->fs
= conf
->fs
;
320 oldconf
->odr
= conf
->odr
;
323 /* set GYRO_ACCEL_CONFIG0 register (accel filter) */
324 if (conf
->filter
!= oldconf
->filter
) {
325 val
= INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf
->filter
) |
326 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(st
->conf
.gyro
.filter
);
327 ret
= regmap_write(st
->map
, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0
, val
);
330 oldconf
->filter
= conf
->filter
;
333 /* set PWR_MGMT0 register (accel sensor mode) */
334 return inv_icm42600_set_pwr_mgmt0(st
, st
->conf
.gyro
.mode
, conf
->mode
,
335 st
->conf
.temp_en
, sleep_ms
);
338 int inv_icm42600_set_gyro_conf(struct inv_icm42600_state
*st
,
339 struct inv_icm42600_sensor_conf
*conf
,
340 unsigned int *sleep_ms
)
342 struct inv_icm42600_sensor_conf
*oldconf
= &st
->conf
.gyro
;
346 /* sanitize missing values with current values */
348 conf
->mode
= oldconf
->mode
;
350 conf
->fs
= oldconf
->fs
;
352 conf
->odr
= oldconf
->odr
;
353 if (conf
->filter
< 0)
354 conf
->filter
= oldconf
->filter
;
356 /* set GYRO_CONFIG0 register (gyro fullscale & odr) */
357 if (conf
->fs
!= oldconf
->fs
|| conf
->odr
!= oldconf
->odr
) {
358 val
= INV_ICM42600_GYRO_CONFIG0_FS(conf
->fs
) |
359 INV_ICM42600_GYRO_CONFIG0_ODR(conf
->odr
);
360 ret
= regmap_write(st
->map
, INV_ICM42600_REG_GYRO_CONFIG0
, val
);
363 oldconf
->fs
= conf
->fs
;
364 oldconf
->odr
= conf
->odr
;
367 /* set GYRO_ACCEL_CONFIG0 register (gyro filter) */
368 if (conf
->filter
!= oldconf
->filter
) {
369 val
= INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(st
->conf
.accel
.filter
) |
370 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf
->filter
);
371 ret
= regmap_write(st
->map
, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0
, val
);
374 oldconf
->filter
= conf
->filter
;
377 /* set PWR_MGMT0 register (gyro sensor mode) */
378 return inv_icm42600_set_pwr_mgmt0(st
, conf
->mode
, st
->conf
.accel
.mode
,
379 st
->conf
.temp_en
, sleep_ms
);
384 int inv_icm42600_set_temp_conf(struct inv_icm42600_state
*st
, bool enable
,
385 unsigned int *sleep_ms
)
387 return inv_icm42600_set_pwr_mgmt0(st
, st
->conf
.gyro
.mode
,
388 st
->conf
.accel
.mode
, enable
,
392 int inv_icm42600_debugfs_reg(struct iio_dev
*indio_dev
, unsigned int reg
,
393 unsigned int writeval
, unsigned int *readval
)
395 struct inv_icm42600_state
*st
= iio_device_get_drvdata(indio_dev
);
398 mutex_lock(&st
->lock
);
401 ret
= regmap_read(st
->map
, reg
, readval
);
403 ret
= regmap_write(st
->map
, reg
, writeval
);
405 mutex_unlock(&st
->lock
);
410 static int inv_icm42600_set_conf(struct inv_icm42600_state
*st
,
411 const struct inv_icm42600_conf
*conf
)
416 /* set PWR_MGMT0 register (gyro & accel sensor mode, temp enabled) */
417 val
= INV_ICM42600_PWR_MGMT0_GYRO(conf
->gyro
.mode
) |
418 INV_ICM42600_PWR_MGMT0_ACCEL(conf
->accel
.mode
);
420 val
|= INV_ICM42600_PWR_MGMT0_TEMP_DIS
;
421 ret
= regmap_write(st
->map
, INV_ICM42600_REG_PWR_MGMT0
, val
);
425 /* set GYRO_CONFIG0 register (gyro fullscale & odr) */
426 val
= INV_ICM42600_GYRO_CONFIG0_FS(conf
->gyro
.fs
) |
427 INV_ICM42600_GYRO_CONFIG0_ODR(conf
->gyro
.odr
);
428 ret
= regmap_write(st
->map
, INV_ICM42600_REG_GYRO_CONFIG0
, val
);
432 /* set ACCEL_CONFIG0 register (accel fullscale & odr) */
433 val
= INV_ICM42600_ACCEL_CONFIG0_FS(conf
->accel
.fs
) |
434 INV_ICM42600_ACCEL_CONFIG0_ODR(conf
->accel
.odr
);
435 ret
= regmap_write(st
->map
, INV_ICM42600_REG_ACCEL_CONFIG0
, val
);
439 /* set GYRO_ACCEL_CONFIG0 register (gyro & accel filters) */
440 val
= INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf
->accel
.filter
) |
441 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf
->gyro
.filter
);
442 ret
= regmap_write(st
->map
, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0
, val
);
446 /* update internal conf */
453 * inv_icm42600_setup() - check and setup chip
454 * @st: driver internal state
455 * @bus_setup: callback for setting up bus specific registers
457 * Returns 0 on success, a negative error code otherwise.
459 static int inv_icm42600_setup(struct inv_icm42600_state
*st
,
460 inv_icm42600_bus_setup bus_setup
)
462 const struct inv_icm42600_hw
*hw
= &inv_icm42600_hw
[st
->chip
];
463 const struct device
*dev
= regmap_get_device(st
->map
);
467 /* check chip self-identification value */
468 ret
= regmap_read(st
->map
, INV_ICM42600_REG_WHOAMI
, &val
);
471 if (val
!= hw
->whoami
) {
472 dev_err(dev
, "invalid whoami %#02x expected %#02x (%s)\n",
473 val
, hw
->whoami
, hw
->name
);
478 /* reset to make sure previous state are not there */
479 ret
= regmap_write(st
->map
, INV_ICM42600_REG_DEVICE_CONFIG
,
480 INV_ICM42600_DEVICE_CONFIG_SOFT_RESET
);
483 msleep(INV_ICM42600_RESET_TIME_MS
);
485 ret
= regmap_read(st
->map
, INV_ICM42600_REG_INT_STATUS
, &val
);
488 if (!(val
& INV_ICM42600_INT_STATUS_RESET_DONE
)) {
489 dev_err(dev
, "reset error, reset done bit not set\n");
493 /* set chip bus configuration */
498 /* sensor data in big-endian (default) */
499 ret
= regmap_set_bits(st
->map
, INV_ICM42600_REG_INTF_CONFIG0
,
500 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN
);
505 * Use RC clock for accel low-power to fix glitches when switching
506 * gyro on/off while accel low-power is on.
508 ret
= regmap_update_bits(st
->map
, INV_ICM42600_REG_INTF_CONFIG1
,
509 INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC
,
510 INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC
);
514 return inv_icm42600_set_conf(st
, hw
->conf
);
517 static irqreturn_t
inv_icm42600_irq_timestamp(int irq
, void *_data
)
519 struct inv_icm42600_state
*st
= _data
;
521 st
->timestamp
.gyro
= iio_get_time_ns(st
->indio_gyro
);
522 st
->timestamp
.accel
= iio_get_time_ns(st
->indio_accel
);
524 return IRQ_WAKE_THREAD
;
527 static irqreturn_t
inv_icm42600_irq_handler(int irq
, void *_data
)
529 struct inv_icm42600_state
*st
= _data
;
530 struct device
*dev
= regmap_get_device(st
->map
);
534 mutex_lock(&st
->lock
);
536 ret
= regmap_read(st
->map
, INV_ICM42600_REG_INT_STATUS
, &status
);
541 if (status
& INV_ICM42600_INT_STATUS_FIFO_FULL
)
542 dev_warn(dev
, "FIFO full data lost!\n");
544 /* FIFO threshold reached */
545 if (status
& INV_ICM42600_INT_STATUS_FIFO_THS
) {
546 ret
= inv_icm42600_buffer_fifo_read(st
, 0);
548 dev_err(dev
, "FIFO read error %d\n", ret
);
551 ret
= inv_icm42600_buffer_fifo_parse(st
);
553 dev_err(dev
, "FIFO parsing error %d\n", ret
);
557 mutex_unlock(&st
->lock
);
562 * inv_icm42600_irq_init() - initialize int pin and interrupt handler
563 * @st: driver internal state
565 * @irq_type: irq trigger type
566 * @open_drain: true if irq is open drain, false for push-pull
568 * Returns 0 on success, a negative error code otherwise.
570 static int inv_icm42600_irq_init(struct inv_icm42600_state
*st
, int irq
,
571 int irq_type
, bool open_drain
)
573 struct device
*dev
= regmap_get_device(st
->map
);
577 /* configure INT1 interrupt: default is active low on edge */
579 case IRQF_TRIGGER_RISING
:
580 case IRQF_TRIGGER_HIGH
:
581 val
= INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH
;
584 val
= INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW
;
589 case IRQF_TRIGGER_LOW
:
590 case IRQF_TRIGGER_HIGH
:
591 val
|= INV_ICM42600_INT_CONFIG_INT1_LATCHED
;
598 val
|= INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL
;
600 ret
= regmap_write(st
->map
, INV_ICM42600_REG_INT_CONFIG
, val
);
604 /* Deassert async reset for proper INT pin operation (cf datasheet) */
605 ret
= regmap_clear_bits(st
->map
, INV_ICM42600_REG_INT_CONFIG1
,
606 INV_ICM42600_INT_CONFIG1_ASYNC_RESET
);
610 irq_type
|= IRQF_ONESHOT
;
611 return devm_request_threaded_irq(dev
, irq
, inv_icm42600_irq_timestamp
,
612 inv_icm42600_irq_handler
, irq_type
,
616 static int inv_icm42600_timestamp_setup(struct inv_icm42600_state
*st
)
620 /* enable timestamp register */
621 val
= INV_ICM42600_TMST_CONFIG_TMST_TO_REGS_EN
|
622 INV_ICM42600_TMST_CONFIG_TMST_EN
;
623 return regmap_update_bits(st
->map
, INV_ICM42600_REG_TMST_CONFIG
,
624 INV_ICM42600_TMST_CONFIG_MASK
, val
);
627 static int inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state
*st
)
631 ret
= regulator_enable(st
->vddio_supply
);
635 /* wait a little for supply ramp */
636 usleep_range(3000, 4000);
641 static void inv_icm42600_disable_vdd_reg(void *_data
)
643 struct inv_icm42600_state
*st
= _data
;
644 const struct device
*dev
= regmap_get_device(st
->map
);
647 ret
= regulator_disable(st
->vdd_supply
);
649 dev_err(dev
, "failed to disable vdd error %d\n", ret
);
652 static void inv_icm42600_disable_vddio_reg(void *_data
)
654 struct inv_icm42600_state
*st
= _data
;
655 const struct device
*dev
= regmap_get_device(st
->map
);
658 ret
= regulator_disable(st
->vddio_supply
);
660 dev_err(dev
, "failed to disable vddio error %d\n", ret
);
663 static void inv_icm42600_disable_pm(void *_data
)
665 struct device
*dev
= _data
;
667 pm_runtime_put_sync(dev
);
668 pm_runtime_disable(dev
);
671 int inv_icm42600_core_probe(struct regmap
*regmap
, int chip
, int irq
,
672 inv_icm42600_bus_setup bus_setup
)
674 struct device
*dev
= regmap_get_device(regmap
);
675 struct inv_icm42600_state
*st
;
680 if (chip
<= INV_CHIP_INVALID
|| chip
>= INV_CHIP_NB
) {
681 dev_err(dev
, "invalid chip = %d\n", chip
);
685 irq_type
= irq_get_trigger_type(irq
);
687 irq_type
= IRQF_TRIGGER_FALLING
;
689 open_drain
= device_property_read_bool(dev
, "drive-open-drain");
691 st
= devm_kzalloc(dev
, sizeof(*st
), GFP_KERNEL
);
695 dev_set_drvdata(dev
, st
);
696 mutex_init(&st
->lock
);
700 ret
= iio_read_mount_matrix(dev
, &st
->orientation
);
702 dev_err(dev
, "failed to retrieve mounting matrix %d\n", ret
);
706 st
->vdd_supply
= devm_regulator_get(dev
, "vdd");
707 if (IS_ERR(st
->vdd_supply
))
708 return PTR_ERR(st
->vdd_supply
);
710 st
->vddio_supply
= devm_regulator_get(dev
, "vddio");
711 if (IS_ERR(st
->vddio_supply
))
712 return PTR_ERR(st
->vddio_supply
);
714 ret
= regulator_enable(st
->vdd_supply
);
717 msleep(INV_ICM42600_POWER_UP_TIME_MS
);
719 ret
= devm_add_action_or_reset(dev
, inv_icm42600_disable_vdd_reg
, st
);
723 ret
= inv_icm42600_enable_regulator_vddio(st
);
727 ret
= devm_add_action_or_reset(dev
, inv_icm42600_disable_vddio_reg
, st
);
731 /* setup chip registers */
732 ret
= inv_icm42600_setup(st
, bus_setup
);
736 ret
= inv_icm42600_timestamp_setup(st
);
740 ret
= inv_icm42600_buffer_init(st
);
744 st
->indio_gyro
= inv_icm42600_gyro_init(st
);
745 if (IS_ERR(st
->indio_gyro
))
746 return PTR_ERR(st
->indio_gyro
);
748 st
->indio_accel
= inv_icm42600_accel_init(st
);
749 if (IS_ERR(st
->indio_accel
))
750 return PTR_ERR(st
->indio_accel
);
752 ret
= inv_icm42600_irq_init(st
, irq
, irq_type
, open_drain
);
756 /* setup runtime power management */
757 ret
= pm_runtime_set_active(dev
);
760 pm_runtime_get_noresume(dev
);
761 pm_runtime_enable(dev
);
762 pm_runtime_set_autosuspend_delay(dev
, INV_ICM42600_SUSPEND_DELAY_MS
);
763 pm_runtime_use_autosuspend(dev
);
766 return devm_add_action_or_reset(dev
, inv_icm42600_disable_pm
, dev
);
768 EXPORT_SYMBOL_NS_GPL(inv_icm42600_core_probe
, "IIO_ICM42600");
771 * Suspend saves sensors state and turns everything off.
772 * Check first if runtime suspend has not already done the job.
774 static int inv_icm42600_suspend(struct device
*dev
)
776 struct inv_icm42600_state
*st
= dev_get_drvdata(dev
);
779 mutex_lock(&st
->lock
);
781 st
->suspended
.gyro
= st
->conf
.gyro
.mode
;
782 st
->suspended
.accel
= st
->conf
.accel
.mode
;
783 st
->suspended
.temp
= st
->conf
.temp_en
;
784 if (pm_runtime_suspended(dev
)) {
789 /* disable FIFO data streaming */
791 ret
= regmap_write(st
->map
, INV_ICM42600_REG_FIFO_CONFIG
,
792 INV_ICM42600_FIFO_CONFIG_BYPASS
);
797 ret
= inv_icm42600_set_pwr_mgmt0(st
, INV_ICM42600_SENSOR_MODE_OFF
,
798 INV_ICM42600_SENSOR_MODE_OFF
, false,
803 regulator_disable(st
->vddio_supply
);
806 mutex_unlock(&st
->lock
);
811 * System resume gets the system back on and restores the sensors state.
812 * Manually put runtime power management in system active state.
814 static int inv_icm42600_resume(struct device
*dev
)
816 struct inv_icm42600_state
*st
= dev_get_drvdata(dev
);
819 mutex_lock(&st
->lock
);
821 ret
= inv_icm42600_enable_regulator_vddio(st
);
825 pm_runtime_disable(dev
);
826 pm_runtime_set_active(dev
);
827 pm_runtime_enable(dev
);
829 /* restore sensors state */
830 ret
= inv_icm42600_set_pwr_mgmt0(st
, st
->suspended
.gyro
,
832 st
->suspended
.temp
, NULL
);
836 /* restore FIFO data streaming */
838 ret
= regmap_write(st
->map
, INV_ICM42600_REG_FIFO_CONFIG
,
839 INV_ICM42600_FIFO_CONFIG_STREAM
);
842 mutex_unlock(&st
->lock
);
846 /* Runtime suspend will turn off sensors that are enabled by iio devices. */
847 static int inv_icm42600_runtime_suspend(struct device
*dev
)
849 struct inv_icm42600_state
*st
= dev_get_drvdata(dev
);
852 mutex_lock(&st
->lock
);
854 /* disable all sensors */
855 ret
= inv_icm42600_set_pwr_mgmt0(st
, INV_ICM42600_SENSOR_MODE_OFF
,
856 INV_ICM42600_SENSOR_MODE_OFF
, false,
861 regulator_disable(st
->vddio_supply
);
864 mutex_unlock(&st
->lock
);
868 /* Sensors are enabled by iio devices, no need to turn them back on here. */
869 static int inv_icm42600_runtime_resume(struct device
*dev
)
871 struct inv_icm42600_state
*st
= dev_get_drvdata(dev
);
874 mutex_lock(&st
->lock
);
876 ret
= inv_icm42600_enable_regulator_vddio(st
);
878 mutex_unlock(&st
->lock
);
882 EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42600_pm_ops
, IIO_ICM42600
) = {
883 SYSTEM_SLEEP_PM_OPS(inv_icm42600_suspend
, inv_icm42600_resume
)
884 RUNTIME_PM_OPS(inv_icm42600_runtime_suspend
,
885 inv_icm42600_runtime_resume
, NULL
)
888 MODULE_AUTHOR("InvenSense, Inc.");
889 MODULE_DESCRIPTION("InvenSense ICM-426xx device driver");
890 MODULE_LICENSE("GPL");
891 MODULE_IMPORT_NS("IIO_INV_SENSORS_TIMESTAMP");