131c24c1eSJean-Baptiste Maneyrol // SPDX-License-Identifier: GPL-2.0-or-later
231c24c1eSJean-Baptiste Maneyrol /*
331c24c1eSJean-Baptiste Maneyrol  * Copyright (C) 2020 Invensense, Inc.
431c24c1eSJean-Baptiste Maneyrol  */
531c24c1eSJean-Baptiste Maneyrol 
631c24c1eSJean-Baptiste Maneyrol #include <linux/kernel.h>
731c24c1eSJean-Baptiste Maneyrol #include <linux/device.h>
831c24c1eSJean-Baptiste Maneyrol #include <linux/module.h>
931c24c1eSJean-Baptiste Maneyrol #include <linux/slab.h>
1031c24c1eSJean-Baptiste Maneyrol #include <linux/delay.h>
1131c24c1eSJean-Baptiste Maneyrol #include <linux/mutex.h>
1231c24c1eSJean-Baptiste Maneyrol #include <linux/regulator/consumer.h>
1331c24c1eSJean-Baptiste Maneyrol #include <linux/pm_runtime.h>
1431c24c1eSJean-Baptiste Maneyrol #include <linux/regmap.h>
1531c24c1eSJean-Baptiste Maneyrol #include <linux/iio/iio.h>
1631c24c1eSJean-Baptiste Maneyrol 
1731c24c1eSJean-Baptiste Maneyrol #include "inv_icm42600.h"
1831c24c1eSJean-Baptiste Maneyrol 
1931c24c1eSJean-Baptiste Maneyrol static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = {
2031c24c1eSJean-Baptiste Maneyrol 	{
2131c24c1eSJean-Baptiste Maneyrol 		.name = "user banks",
2231c24c1eSJean-Baptiste Maneyrol 		.range_min = 0x0000,
2331c24c1eSJean-Baptiste Maneyrol 		.range_max = 0x4FFF,
2431c24c1eSJean-Baptiste Maneyrol 		.selector_reg = INV_ICM42600_REG_BANK_SEL,
2531c24c1eSJean-Baptiste Maneyrol 		.selector_mask = INV_ICM42600_BANK_SEL_MASK,
2631c24c1eSJean-Baptiste Maneyrol 		.selector_shift = 0,
2731c24c1eSJean-Baptiste Maneyrol 		.window_start = 0,
2831c24c1eSJean-Baptiste Maneyrol 		.window_len = 0x1000,
2931c24c1eSJean-Baptiste Maneyrol 	},
3031c24c1eSJean-Baptiste Maneyrol };
3131c24c1eSJean-Baptiste Maneyrol 
3231c24c1eSJean-Baptiste Maneyrol const struct regmap_config inv_icm42600_regmap_config = {
3331c24c1eSJean-Baptiste Maneyrol 	.reg_bits = 8,
3431c24c1eSJean-Baptiste Maneyrol 	.val_bits = 8,
3531c24c1eSJean-Baptiste Maneyrol 	.max_register = 0x4FFF,
3631c24c1eSJean-Baptiste Maneyrol 	.ranges = inv_icm42600_regmap_ranges,
3731c24c1eSJean-Baptiste Maneyrol 	.num_ranges = ARRAY_SIZE(inv_icm42600_regmap_ranges),
3831c24c1eSJean-Baptiste Maneyrol };
3931c24c1eSJean-Baptiste Maneyrol EXPORT_SYMBOL_GPL(inv_icm42600_regmap_config);
4031c24c1eSJean-Baptiste Maneyrol 
4131c24c1eSJean-Baptiste Maneyrol struct inv_icm42600_hw {
4231c24c1eSJean-Baptiste Maneyrol 	uint8_t whoami;
4331c24c1eSJean-Baptiste Maneyrol 	const char *name;
4431c24c1eSJean-Baptiste Maneyrol 	const struct inv_icm42600_conf *conf;
4531c24c1eSJean-Baptiste Maneyrol };
4631c24c1eSJean-Baptiste Maneyrol 
4731c24c1eSJean-Baptiste Maneyrol /* chip initial default configuration */
4831c24c1eSJean-Baptiste Maneyrol static const struct inv_icm42600_conf inv_icm42600_default_conf = {
4931c24c1eSJean-Baptiste Maneyrol 	.gyro = {
5031c24c1eSJean-Baptiste Maneyrol 		.mode = INV_ICM42600_SENSOR_MODE_OFF,
5131c24c1eSJean-Baptiste Maneyrol 		.fs = INV_ICM42600_GYRO_FS_2000DPS,
5231c24c1eSJean-Baptiste Maneyrol 		.odr = INV_ICM42600_ODR_50HZ,
5331c24c1eSJean-Baptiste Maneyrol 		.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
5431c24c1eSJean-Baptiste Maneyrol 	},
5531c24c1eSJean-Baptiste Maneyrol 	.accel = {
5631c24c1eSJean-Baptiste Maneyrol 		.mode = INV_ICM42600_SENSOR_MODE_OFF,
5731c24c1eSJean-Baptiste Maneyrol 		.fs = INV_ICM42600_ACCEL_FS_16G,
5831c24c1eSJean-Baptiste Maneyrol 		.odr = INV_ICM42600_ODR_50HZ,
5931c24c1eSJean-Baptiste Maneyrol 		.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
6031c24c1eSJean-Baptiste Maneyrol 	},
6131c24c1eSJean-Baptiste Maneyrol 	.temp_en = false,
6231c24c1eSJean-Baptiste Maneyrol };
6331c24c1eSJean-Baptiste Maneyrol 
6431c24c1eSJean-Baptiste Maneyrol static const struct inv_icm42600_hw inv_icm42600_hw[INV_CHIP_NB] = {
6531c24c1eSJean-Baptiste Maneyrol 	[INV_CHIP_ICM42600] = {
6631c24c1eSJean-Baptiste Maneyrol 		.whoami = INV_ICM42600_WHOAMI_ICM42600,
6731c24c1eSJean-Baptiste Maneyrol 		.name = "icm42600",
6831c24c1eSJean-Baptiste Maneyrol 		.conf = &inv_icm42600_default_conf,
6931c24c1eSJean-Baptiste Maneyrol 	},
7031c24c1eSJean-Baptiste Maneyrol 	[INV_CHIP_ICM42602] = {
7131c24c1eSJean-Baptiste Maneyrol 		.whoami = INV_ICM42600_WHOAMI_ICM42602,
7231c24c1eSJean-Baptiste Maneyrol 		.name = "icm42602",
7331c24c1eSJean-Baptiste Maneyrol 		.conf = &inv_icm42600_default_conf,
7431c24c1eSJean-Baptiste Maneyrol 	},
7531c24c1eSJean-Baptiste Maneyrol 	[INV_CHIP_ICM42605] = {
7631c24c1eSJean-Baptiste Maneyrol 		.whoami = INV_ICM42600_WHOAMI_ICM42605,
7731c24c1eSJean-Baptiste Maneyrol 		.name = "icm42605",
7831c24c1eSJean-Baptiste Maneyrol 		.conf = &inv_icm42600_default_conf,
7931c24c1eSJean-Baptiste Maneyrol 	},
8031c24c1eSJean-Baptiste Maneyrol 	[INV_CHIP_ICM42622] = {
8131c24c1eSJean-Baptiste Maneyrol 		.whoami = INV_ICM42600_WHOAMI_ICM42622,
8231c24c1eSJean-Baptiste Maneyrol 		.name = "icm42622",
8331c24c1eSJean-Baptiste Maneyrol 		.conf = &inv_icm42600_default_conf,
8431c24c1eSJean-Baptiste Maneyrol 	},
8531c24c1eSJean-Baptiste Maneyrol };
8631c24c1eSJean-Baptiste Maneyrol 
8731c24c1eSJean-Baptiste Maneyrol const struct iio_mount_matrix *
8831c24c1eSJean-Baptiste Maneyrol inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
8931c24c1eSJean-Baptiste Maneyrol 			      const struct iio_chan_spec *chan)
9031c24c1eSJean-Baptiste Maneyrol {
9131c24c1eSJean-Baptiste Maneyrol 	const struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
9231c24c1eSJean-Baptiste Maneyrol 
9331c24c1eSJean-Baptiste Maneyrol 	return &st->orientation;
9431c24c1eSJean-Baptiste Maneyrol }
9531c24c1eSJean-Baptiste Maneyrol 
9631c24c1eSJean-Baptiste Maneyrol uint32_t inv_icm42600_odr_to_period(enum inv_icm42600_odr odr)
9731c24c1eSJean-Baptiste Maneyrol {
9831c24c1eSJean-Baptiste Maneyrol 	static uint32_t odr_periods[INV_ICM42600_ODR_NB] = {
9931c24c1eSJean-Baptiste Maneyrol 		/* reserved values */
10031c24c1eSJean-Baptiste Maneyrol 		0, 0, 0,
10131c24c1eSJean-Baptiste Maneyrol 		/* 8kHz */
10231c24c1eSJean-Baptiste Maneyrol 		125000,
10331c24c1eSJean-Baptiste Maneyrol 		/* 4kHz */
10431c24c1eSJean-Baptiste Maneyrol 		250000,
10531c24c1eSJean-Baptiste Maneyrol 		/* 2kHz */
10631c24c1eSJean-Baptiste Maneyrol 		500000,
10731c24c1eSJean-Baptiste Maneyrol 		/* 1kHz */
10831c24c1eSJean-Baptiste Maneyrol 		1000000,
10931c24c1eSJean-Baptiste Maneyrol 		/* 200Hz */
11031c24c1eSJean-Baptiste Maneyrol 		5000000,
11131c24c1eSJean-Baptiste Maneyrol 		/* 100Hz */
11231c24c1eSJean-Baptiste Maneyrol 		10000000,
11331c24c1eSJean-Baptiste Maneyrol 		/* 50Hz */
11431c24c1eSJean-Baptiste Maneyrol 		20000000,
11531c24c1eSJean-Baptiste Maneyrol 		/* 25Hz */
11631c24c1eSJean-Baptiste Maneyrol 		40000000,
11731c24c1eSJean-Baptiste Maneyrol 		/* 12.5Hz */
11831c24c1eSJean-Baptiste Maneyrol 		80000000,
11931c24c1eSJean-Baptiste Maneyrol 		/* 6.25Hz */
12031c24c1eSJean-Baptiste Maneyrol 		160000000,
12131c24c1eSJean-Baptiste Maneyrol 		/* 3.125Hz */
12231c24c1eSJean-Baptiste Maneyrol 		320000000,
12331c24c1eSJean-Baptiste Maneyrol 		/* 1.5625Hz */
12431c24c1eSJean-Baptiste Maneyrol 		640000000,
12531c24c1eSJean-Baptiste Maneyrol 		/* 500Hz */
12631c24c1eSJean-Baptiste Maneyrol 		2000000,
12731c24c1eSJean-Baptiste Maneyrol 	};
12831c24c1eSJean-Baptiste Maneyrol 
12931c24c1eSJean-Baptiste Maneyrol 	return odr_periods[odr];
13031c24c1eSJean-Baptiste Maneyrol }
13131c24c1eSJean-Baptiste Maneyrol 
13231c24c1eSJean-Baptiste Maneyrol static int inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state *st,
13331c24c1eSJean-Baptiste Maneyrol 				      enum inv_icm42600_sensor_mode gyro,
13431c24c1eSJean-Baptiste Maneyrol 				      enum inv_icm42600_sensor_mode accel,
13531c24c1eSJean-Baptiste Maneyrol 				      bool temp, unsigned int *sleep_ms)
13631c24c1eSJean-Baptiste Maneyrol {
13731c24c1eSJean-Baptiste Maneyrol 	enum inv_icm42600_sensor_mode oldgyro = st->conf.gyro.mode;
13831c24c1eSJean-Baptiste Maneyrol 	enum inv_icm42600_sensor_mode oldaccel = st->conf.accel.mode;
13931c24c1eSJean-Baptiste Maneyrol 	bool oldtemp = st->conf.temp_en;
14031c24c1eSJean-Baptiste Maneyrol 	unsigned int sleepval;
14131c24c1eSJean-Baptiste Maneyrol 	unsigned int val;
14231c24c1eSJean-Baptiste Maneyrol 	int ret;
14331c24c1eSJean-Baptiste Maneyrol 
14431c24c1eSJean-Baptiste Maneyrol 	/* if nothing changed, exit */
14531c24c1eSJean-Baptiste Maneyrol 	if (gyro == oldgyro && accel == oldaccel && temp == oldtemp)
14631c24c1eSJean-Baptiste Maneyrol 		return 0;
14731c24c1eSJean-Baptiste Maneyrol 
14831c24c1eSJean-Baptiste Maneyrol 	val = INV_ICM42600_PWR_MGMT0_GYRO(gyro) |
14931c24c1eSJean-Baptiste Maneyrol 	      INV_ICM42600_PWR_MGMT0_ACCEL(accel);
15031c24c1eSJean-Baptiste Maneyrol 	if (!temp)
15131c24c1eSJean-Baptiste Maneyrol 		val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
15231c24c1eSJean-Baptiste Maneyrol 	ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
15331c24c1eSJean-Baptiste Maneyrol 	if (ret)
15431c24c1eSJean-Baptiste Maneyrol 		return ret;
15531c24c1eSJean-Baptiste Maneyrol 
15631c24c1eSJean-Baptiste Maneyrol 	st->conf.gyro.mode = gyro;
15731c24c1eSJean-Baptiste Maneyrol 	st->conf.accel.mode = accel;
15831c24c1eSJean-Baptiste Maneyrol 	st->conf.temp_en = temp;
15931c24c1eSJean-Baptiste Maneyrol 
16031c24c1eSJean-Baptiste Maneyrol 	/* compute required wait time for sensors to stabilize */
16131c24c1eSJean-Baptiste Maneyrol 	sleepval = 0;
16231c24c1eSJean-Baptiste Maneyrol 	/* temperature stabilization time */
16331c24c1eSJean-Baptiste Maneyrol 	if (temp && !oldtemp) {
16431c24c1eSJean-Baptiste Maneyrol 		if (sleepval < INV_ICM42600_TEMP_STARTUP_TIME_MS)
16531c24c1eSJean-Baptiste Maneyrol 			sleepval = INV_ICM42600_TEMP_STARTUP_TIME_MS;
16631c24c1eSJean-Baptiste Maneyrol 	}
16731c24c1eSJean-Baptiste Maneyrol 	/* accel startup time */
16831c24c1eSJean-Baptiste Maneyrol 	if (accel != oldaccel && oldaccel == INV_ICM42600_SENSOR_MODE_OFF) {
16931c24c1eSJean-Baptiste Maneyrol 		/* block any register write for at least 200 µs */
17031c24c1eSJean-Baptiste Maneyrol 		usleep_range(200, 300);
17131c24c1eSJean-Baptiste Maneyrol 		if (sleepval < INV_ICM42600_ACCEL_STARTUP_TIME_MS)
17231c24c1eSJean-Baptiste Maneyrol 			sleepval = INV_ICM42600_ACCEL_STARTUP_TIME_MS;
17331c24c1eSJean-Baptiste Maneyrol 	}
17431c24c1eSJean-Baptiste Maneyrol 	if (gyro != oldgyro) {
17531c24c1eSJean-Baptiste Maneyrol 		/* gyro startup time */
17631c24c1eSJean-Baptiste Maneyrol 		if (oldgyro == INV_ICM42600_SENSOR_MODE_OFF) {
17731c24c1eSJean-Baptiste Maneyrol 			/* block any register write for at least 200 µs */
17831c24c1eSJean-Baptiste Maneyrol 			usleep_range(200, 300);
17931c24c1eSJean-Baptiste Maneyrol 			if (sleepval < INV_ICM42600_GYRO_STARTUP_TIME_MS)
18031c24c1eSJean-Baptiste Maneyrol 				sleepval = INV_ICM42600_GYRO_STARTUP_TIME_MS;
18131c24c1eSJean-Baptiste Maneyrol 		/* gyro stop time */
18231c24c1eSJean-Baptiste Maneyrol 		} else if (gyro == INV_ICM42600_SENSOR_MODE_OFF) {
18331c24c1eSJean-Baptiste Maneyrol 			if (sleepval < INV_ICM42600_GYRO_STOP_TIME_MS)
18431c24c1eSJean-Baptiste Maneyrol 				sleepval =  INV_ICM42600_GYRO_STOP_TIME_MS;
18531c24c1eSJean-Baptiste Maneyrol 		}
18631c24c1eSJean-Baptiste Maneyrol 	}
18731c24c1eSJean-Baptiste Maneyrol 
18831c24c1eSJean-Baptiste Maneyrol 	/* deferred sleep value if sleep pointer is provided or direct sleep */
18931c24c1eSJean-Baptiste Maneyrol 	if (sleep_ms)
19031c24c1eSJean-Baptiste Maneyrol 		*sleep_ms = sleepval;
19131c24c1eSJean-Baptiste Maneyrol 	else if (sleepval)
19231c24c1eSJean-Baptiste Maneyrol 		msleep(sleepval);
19331c24c1eSJean-Baptiste Maneyrol 
19431c24c1eSJean-Baptiste Maneyrol 	return 0;
19531c24c1eSJean-Baptiste Maneyrol }
19631c24c1eSJean-Baptiste Maneyrol 
19731c24c1eSJean-Baptiste Maneyrol int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
19831c24c1eSJean-Baptiste Maneyrol 				struct inv_icm42600_sensor_conf *conf,
19931c24c1eSJean-Baptiste Maneyrol 				unsigned int *sleep_ms)
20031c24c1eSJean-Baptiste Maneyrol {
20131c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf *oldconf = &st->conf.accel;
20231c24c1eSJean-Baptiste Maneyrol 	unsigned int val;
20331c24c1eSJean-Baptiste Maneyrol 	int ret;
20431c24c1eSJean-Baptiste Maneyrol 
20531c24c1eSJean-Baptiste Maneyrol 	/* Sanitize missing values with current values */
20631c24c1eSJean-Baptiste Maneyrol 	if (conf->mode < 0)
20731c24c1eSJean-Baptiste Maneyrol 		conf->mode = oldconf->mode;
20831c24c1eSJean-Baptiste Maneyrol 	if (conf->fs < 0)
20931c24c1eSJean-Baptiste Maneyrol 		conf->fs = oldconf->fs;
21031c24c1eSJean-Baptiste Maneyrol 	if (conf->odr < 0)
21131c24c1eSJean-Baptiste Maneyrol 		conf->odr = oldconf->odr;
21231c24c1eSJean-Baptiste Maneyrol 	if (conf->filter < 0)
21331c24c1eSJean-Baptiste Maneyrol 		conf->filter = oldconf->filter;
21431c24c1eSJean-Baptiste Maneyrol 
21531c24c1eSJean-Baptiste Maneyrol 	/* set ACCEL_CONFIG0 register (accel fullscale & odr) */
21631c24c1eSJean-Baptiste Maneyrol 	if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
21731c24c1eSJean-Baptiste Maneyrol 		val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->fs) |
21831c24c1eSJean-Baptiste Maneyrol 		      INV_ICM42600_ACCEL_CONFIG0_ODR(conf->odr);
21931c24c1eSJean-Baptiste Maneyrol 		ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
22031c24c1eSJean-Baptiste Maneyrol 		if (ret)
22131c24c1eSJean-Baptiste Maneyrol 			return ret;
22231c24c1eSJean-Baptiste Maneyrol 		oldconf->fs = conf->fs;
22331c24c1eSJean-Baptiste Maneyrol 		oldconf->odr = conf->odr;
22431c24c1eSJean-Baptiste Maneyrol 	}
22531c24c1eSJean-Baptiste Maneyrol 
22631c24c1eSJean-Baptiste Maneyrol 	/* set GYRO_ACCEL_CONFIG0 register (accel filter) */
22731c24c1eSJean-Baptiste Maneyrol 	if (conf->filter != oldconf->filter) {
22831c24c1eSJean-Baptiste Maneyrol 		val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->filter) |
22931c24c1eSJean-Baptiste Maneyrol 		      INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(st->conf.gyro.filter);
23031c24c1eSJean-Baptiste Maneyrol 		ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
23131c24c1eSJean-Baptiste Maneyrol 		if (ret)
23231c24c1eSJean-Baptiste Maneyrol 			return ret;
23331c24c1eSJean-Baptiste Maneyrol 		oldconf->filter = conf->filter;
23431c24c1eSJean-Baptiste Maneyrol 	}
23531c24c1eSJean-Baptiste Maneyrol 
23631c24c1eSJean-Baptiste Maneyrol 	/* set PWR_MGMT0 register (accel sensor mode) */
23731c24c1eSJean-Baptiste Maneyrol 	return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode, conf->mode,
23831c24c1eSJean-Baptiste Maneyrol 					  st->conf.temp_en, sleep_ms);
23931c24c1eSJean-Baptiste Maneyrol }
24031c24c1eSJean-Baptiste Maneyrol 
24131c24c1eSJean-Baptiste Maneyrol int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
24231c24c1eSJean-Baptiste Maneyrol 			       struct inv_icm42600_sensor_conf *conf,
24331c24c1eSJean-Baptiste Maneyrol 			       unsigned int *sleep_ms)
24431c24c1eSJean-Baptiste Maneyrol {
24531c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_sensor_conf *oldconf = &st->conf.gyro;
24631c24c1eSJean-Baptiste Maneyrol 	unsigned int val;
24731c24c1eSJean-Baptiste Maneyrol 	int ret;
24831c24c1eSJean-Baptiste Maneyrol 
24931c24c1eSJean-Baptiste Maneyrol 	/* sanitize missing values with current values */
25031c24c1eSJean-Baptiste Maneyrol 	if (conf->mode < 0)
25131c24c1eSJean-Baptiste Maneyrol 		conf->mode = oldconf->mode;
25231c24c1eSJean-Baptiste Maneyrol 	if (conf->fs < 0)
25331c24c1eSJean-Baptiste Maneyrol 		conf->fs = oldconf->fs;
25431c24c1eSJean-Baptiste Maneyrol 	if (conf->odr < 0)
25531c24c1eSJean-Baptiste Maneyrol 		conf->odr = oldconf->odr;
25631c24c1eSJean-Baptiste Maneyrol 	if (conf->filter < 0)
25731c24c1eSJean-Baptiste Maneyrol 		conf->filter = oldconf->filter;
25831c24c1eSJean-Baptiste Maneyrol 
25931c24c1eSJean-Baptiste Maneyrol 	/* set GYRO_CONFIG0 register (gyro fullscale & odr) */
26031c24c1eSJean-Baptiste Maneyrol 	if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
26131c24c1eSJean-Baptiste Maneyrol 		val = INV_ICM42600_GYRO_CONFIG0_FS(conf->fs) |
26231c24c1eSJean-Baptiste Maneyrol 		      INV_ICM42600_GYRO_CONFIG0_ODR(conf->odr);
26331c24c1eSJean-Baptiste Maneyrol 		ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
26431c24c1eSJean-Baptiste Maneyrol 		if (ret)
26531c24c1eSJean-Baptiste Maneyrol 			return ret;
26631c24c1eSJean-Baptiste Maneyrol 		oldconf->fs = conf->fs;
26731c24c1eSJean-Baptiste Maneyrol 		oldconf->odr = conf->odr;
26831c24c1eSJean-Baptiste Maneyrol 	}
26931c24c1eSJean-Baptiste Maneyrol 
27031c24c1eSJean-Baptiste Maneyrol 	/* set GYRO_ACCEL_CONFIG0 register (gyro filter) */
27131c24c1eSJean-Baptiste Maneyrol 	if (conf->filter != oldconf->filter) {
27231c24c1eSJean-Baptiste Maneyrol 		val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(st->conf.accel.filter) |
27331c24c1eSJean-Baptiste Maneyrol 		      INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->filter);
27431c24c1eSJean-Baptiste Maneyrol 		ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
27531c24c1eSJean-Baptiste Maneyrol 		if (ret)
27631c24c1eSJean-Baptiste Maneyrol 			return ret;
27731c24c1eSJean-Baptiste Maneyrol 		oldconf->filter = conf->filter;
27831c24c1eSJean-Baptiste Maneyrol 	}
27931c24c1eSJean-Baptiste Maneyrol 
28031c24c1eSJean-Baptiste Maneyrol 	/* set PWR_MGMT0 register (gyro sensor mode) */
28131c24c1eSJean-Baptiste Maneyrol 	return inv_icm42600_set_pwr_mgmt0(st, conf->mode, st->conf.accel.mode,
28231c24c1eSJean-Baptiste Maneyrol 					  st->conf.temp_en, sleep_ms);
28331c24c1eSJean-Baptiste Maneyrol 
28431c24c1eSJean-Baptiste Maneyrol 	return 0;
28531c24c1eSJean-Baptiste Maneyrol }
28631c24c1eSJean-Baptiste Maneyrol 
28731c24c1eSJean-Baptiste Maneyrol int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
28831c24c1eSJean-Baptiste Maneyrol 			       unsigned int *sleep_ms)
28931c24c1eSJean-Baptiste Maneyrol {
29031c24c1eSJean-Baptiste Maneyrol 	return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode,
29131c24c1eSJean-Baptiste Maneyrol 					  st->conf.accel.mode, enable,
29231c24c1eSJean-Baptiste Maneyrol 					  sleep_ms);
29331c24c1eSJean-Baptiste Maneyrol }
29431c24c1eSJean-Baptiste Maneyrol 
29531c24c1eSJean-Baptiste Maneyrol int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
29631c24c1eSJean-Baptiste Maneyrol 			     unsigned int writeval, unsigned int *readval)
29731c24c1eSJean-Baptiste Maneyrol {
29831c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
29931c24c1eSJean-Baptiste Maneyrol 	int ret;
30031c24c1eSJean-Baptiste Maneyrol 
30131c24c1eSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
30231c24c1eSJean-Baptiste Maneyrol 
30331c24c1eSJean-Baptiste Maneyrol 	if (readval)
30431c24c1eSJean-Baptiste Maneyrol 		ret = regmap_read(st->map, reg, readval);
30531c24c1eSJean-Baptiste Maneyrol 	else
30631c24c1eSJean-Baptiste Maneyrol 		ret = regmap_write(st->map, reg, writeval);
30731c24c1eSJean-Baptiste Maneyrol 
30831c24c1eSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
30931c24c1eSJean-Baptiste Maneyrol 
31031c24c1eSJean-Baptiste Maneyrol 	return ret;
31131c24c1eSJean-Baptiste Maneyrol }
31231c24c1eSJean-Baptiste Maneyrol 
31331c24c1eSJean-Baptiste Maneyrol static int inv_icm42600_set_conf(struct inv_icm42600_state *st,
31431c24c1eSJean-Baptiste Maneyrol 				 const struct inv_icm42600_conf *conf)
31531c24c1eSJean-Baptiste Maneyrol {
31631c24c1eSJean-Baptiste Maneyrol 	unsigned int val;
31731c24c1eSJean-Baptiste Maneyrol 	int ret;
31831c24c1eSJean-Baptiste Maneyrol 
31931c24c1eSJean-Baptiste Maneyrol 	/* set PWR_MGMT0 register (gyro & accel sensor mode, temp enabled) */
32031c24c1eSJean-Baptiste Maneyrol 	val = INV_ICM42600_PWR_MGMT0_GYRO(conf->gyro.mode) |
32131c24c1eSJean-Baptiste Maneyrol 	      INV_ICM42600_PWR_MGMT0_ACCEL(conf->accel.mode);
32231c24c1eSJean-Baptiste Maneyrol 	if (!conf->temp_en)
32331c24c1eSJean-Baptiste Maneyrol 		val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
32431c24c1eSJean-Baptiste Maneyrol 	ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
32531c24c1eSJean-Baptiste Maneyrol 	if (ret)
32631c24c1eSJean-Baptiste Maneyrol 		return ret;
32731c24c1eSJean-Baptiste Maneyrol 
32831c24c1eSJean-Baptiste Maneyrol 	/* set GYRO_CONFIG0 register (gyro fullscale & odr) */
32931c24c1eSJean-Baptiste Maneyrol 	val = INV_ICM42600_GYRO_CONFIG0_FS(conf->gyro.fs) |
33031c24c1eSJean-Baptiste Maneyrol 	      INV_ICM42600_GYRO_CONFIG0_ODR(conf->gyro.odr);
33131c24c1eSJean-Baptiste Maneyrol 	ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
33231c24c1eSJean-Baptiste Maneyrol 	if (ret)
33331c24c1eSJean-Baptiste Maneyrol 		return ret;
33431c24c1eSJean-Baptiste Maneyrol 
33531c24c1eSJean-Baptiste Maneyrol 	/* set ACCEL_CONFIG0 register (accel fullscale & odr) */
33631c24c1eSJean-Baptiste Maneyrol 	val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->accel.fs) |
33731c24c1eSJean-Baptiste Maneyrol 	      INV_ICM42600_ACCEL_CONFIG0_ODR(conf->accel.odr);
33831c24c1eSJean-Baptiste Maneyrol 	ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
33931c24c1eSJean-Baptiste Maneyrol 	if (ret)
34031c24c1eSJean-Baptiste Maneyrol 		return ret;
34131c24c1eSJean-Baptiste Maneyrol 
34231c24c1eSJean-Baptiste Maneyrol 	/* set GYRO_ACCEL_CONFIG0 register (gyro & accel filters) */
34331c24c1eSJean-Baptiste Maneyrol 	val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->accel.filter) |
34431c24c1eSJean-Baptiste Maneyrol 	      INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->gyro.filter);
34531c24c1eSJean-Baptiste Maneyrol 	ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
34631c24c1eSJean-Baptiste Maneyrol 	if (ret)
34731c24c1eSJean-Baptiste Maneyrol 		return ret;
34831c24c1eSJean-Baptiste Maneyrol 
34931c24c1eSJean-Baptiste Maneyrol 	/* update internal conf */
35031c24c1eSJean-Baptiste Maneyrol 	st->conf = *conf;
35131c24c1eSJean-Baptiste Maneyrol 
35231c24c1eSJean-Baptiste Maneyrol 	return 0;
35331c24c1eSJean-Baptiste Maneyrol }
35431c24c1eSJean-Baptiste Maneyrol 
35531c24c1eSJean-Baptiste Maneyrol /**
35631c24c1eSJean-Baptiste Maneyrol  *  inv_icm42600_setup() - check and setup chip
35731c24c1eSJean-Baptiste Maneyrol  *  @st:	driver internal state
35831c24c1eSJean-Baptiste Maneyrol  *  @bus_setup:	callback for setting up bus specific registers
35931c24c1eSJean-Baptiste Maneyrol  *
36031c24c1eSJean-Baptiste Maneyrol  *  Returns 0 on success, a negative error code otherwise.
36131c24c1eSJean-Baptiste Maneyrol  */
36231c24c1eSJean-Baptiste Maneyrol static int inv_icm42600_setup(struct inv_icm42600_state *st,
36331c24c1eSJean-Baptiste Maneyrol 			      inv_icm42600_bus_setup bus_setup)
36431c24c1eSJean-Baptiste Maneyrol {
36531c24c1eSJean-Baptiste Maneyrol 	const struct inv_icm42600_hw *hw = &inv_icm42600_hw[st->chip];
36631c24c1eSJean-Baptiste Maneyrol 	const struct device *dev = regmap_get_device(st->map);
36731c24c1eSJean-Baptiste Maneyrol 	unsigned int val;
36831c24c1eSJean-Baptiste Maneyrol 	int ret;
36931c24c1eSJean-Baptiste Maneyrol 
37031c24c1eSJean-Baptiste Maneyrol 	/* check chip self-identification value */
37131c24c1eSJean-Baptiste Maneyrol 	ret = regmap_read(st->map, INV_ICM42600_REG_WHOAMI, &val);
37231c24c1eSJean-Baptiste Maneyrol 	if (ret)
37331c24c1eSJean-Baptiste Maneyrol 		return ret;
37431c24c1eSJean-Baptiste Maneyrol 	if (val != hw->whoami) {
37531c24c1eSJean-Baptiste Maneyrol 		dev_err(dev, "invalid whoami %#02x expected %#02x (%s)\n",
37631c24c1eSJean-Baptiste Maneyrol 			val, hw->whoami, hw->name);
37731c24c1eSJean-Baptiste Maneyrol 		return -ENODEV;
37831c24c1eSJean-Baptiste Maneyrol 	}
37931c24c1eSJean-Baptiste Maneyrol 	st->name = hw->name;
38031c24c1eSJean-Baptiste Maneyrol 
38131c24c1eSJean-Baptiste Maneyrol 	/* reset to make sure previous state are not there */
38231c24c1eSJean-Baptiste Maneyrol 	ret = regmap_write(st->map, INV_ICM42600_REG_DEVICE_CONFIG,
38331c24c1eSJean-Baptiste Maneyrol 			   INV_ICM42600_DEVICE_CONFIG_SOFT_RESET);
38431c24c1eSJean-Baptiste Maneyrol 	if (ret)
38531c24c1eSJean-Baptiste Maneyrol 		return ret;
38631c24c1eSJean-Baptiste Maneyrol 	msleep(INV_ICM42600_RESET_TIME_MS);
38731c24c1eSJean-Baptiste Maneyrol 
38831c24c1eSJean-Baptiste Maneyrol 	ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &val);
38931c24c1eSJean-Baptiste Maneyrol 	if (ret)
39031c24c1eSJean-Baptiste Maneyrol 		return ret;
39131c24c1eSJean-Baptiste Maneyrol 	if (!(val & INV_ICM42600_INT_STATUS_RESET_DONE)) {
39231c24c1eSJean-Baptiste Maneyrol 		dev_err(dev, "reset error, reset done bit not set\n");
39331c24c1eSJean-Baptiste Maneyrol 		return -ENODEV;
39431c24c1eSJean-Baptiste Maneyrol 	}
39531c24c1eSJean-Baptiste Maneyrol 
39631c24c1eSJean-Baptiste Maneyrol 	/* set chip bus configuration */
39731c24c1eSJean-Baptiste Maneyrol 	ret = bus_setup(st);
39831c24c1eSJean-Baptiste Maneyrol 	if (ret)
39931c24c1eSJean-Baptiste Maneyrol 		return ret;
40031c24c1eSJean-Baptiste Maneyrol 
40131c24c1eSJean-Baptiste Maneyrol 	/* sensor data in big-endian (default) */
40231c24c1eSJean-Baptiste Maneyrol 	ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
40331c24c1eSJean-Baptiste Maneyrol 				 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN,
40431c24c1eSJean-Baptiste Maneyrol 				 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN);
40531c24c1eSJean-Baptiste Maneyrol 	if (ret)
40631c24c1eSJean-Baptiste Maneyrol 		return ret;
40731c24c1eSJean-Baptiste Maneyrol 
40831c24c1eSJean-Baptiste Maneyrol 	return inv_icm42600_set_conf(st, hw->conf);
40931c24c1eSJean-Baptiste Maneyrol }
41031c24c1eSJean-Baptiste Maneyrol 
41131c24c1eSJean-Baptiste Maneyrol static int inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state *st)
41231c24c1eSJean-Baptiste Maneyrol {
41331c24c1eSJean-Baptiste Maneyrol 	int ret;
41431c24c1eSJean-Baptiste Maneyrol 
41531c24c1eSJean-Baptiste Maneyrol 	ret = regulator_enable(st->vddio_supply);
41631c24c1eSJean-Baptiste Maneyrol 	if (ret)
41731c24c1eSJean-Baptiste Maneyrol 		return ret;
41831c24c1eSJean-Baptiste Maneyrol 
41931c24c1eSJean-Baptiste Maneyrol 	/* wait a little for supply ramp */
42031c24c1eSJean-Baptiste Maneyrol 	usleep_range(3000, 4000);
42131c24c1eSJean-Baptiste Maneyrol 
42231c24c1eSJean-Baptiste Maneyrol 	return 0;
42331c24c1eSJean-Baptiste Maneyrol }
42431c24c1eSJean-Baptiste Maneyrol 
42531c24c1eSJean-Baptiste Maneyrol static void inv_icm42600_disable_vdd_reg(void *_data)
42631c24c1eSJean-Baptiste Maneyrol {
42731c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = _data;
42831c24c1eSJean-Baptiste Maneyrol 	const struct device *dev = regmap_get_device(st->map);
42931c24c1eSJean-Baptiste Maneyrol 	int ret;
43031c24c1eSJean-Baptiste Maneyrol 
43131c24c1eSJean-Baptiste Maneyrol 	ret = regulator_disable(st->vdd_supply);
43231c24c1eSJean-Baptiste Maneyrol 	if (ret)
43331c24c1eSJean-Baptiste Maneyrol 		dev_err(dev, "failed to disable vdd error %d\n", ret);
43431c24c1eSJean-Baptiste Maneyrol }
43531c24c1eSJean-Baptiste Maneyrol 
43631c24c1eSJean-Baptiste Maneyrol static void inv_icm42600_disable_vddio_reg(void *_data)
43731c24c1eSJean-Baptiste Maneyrol {
43831c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = _data;
43931c24c1eSJean-Baptiste Maneyrol 	const struct device *dev = regmap_get_device(st->map);
44031c24c1eSJean-Baptiste Maneyrol 	int ret;
44131c24c1eSJean-Baptiste Maneyrol 
44231c24c1eSJean-Baptiste Maneyrol 	ret = regulator_disable(st->vddio_supply);
44331c24c1eSJean-Baptiste Maneyrol 	if (ret)
44431c24c1eSJean-Baptiste Maneyrol 		dev_err(dev, "failed to disable vddio error %d\n", ret);
44531c24c1eSJean-Baptiste Maneyrol }
44631c24c1eSJean-Baptiste Maneyrol 
44731c24c1eSJean-Baptiste Maneyrol static void inv_icm42600_disable_pm(void *_data)
44831c24c1eSJean-Baptiste Maneyrol {
44931c24c1eSJean-Baptiste Maneyrol 	struct device *dev = _data;
45031c24c1eSJean-Baptiste Maneyrol 
45131c24c1eSJean-Baptiste Maneyrol 	pm_runtime_put_sync(dev);
45231c24c1eSJean-Baptiste Maneyrol 	pm_runtime_disable(dev);
45331c24c1eSJean-Baptiste Maneyrol }
45431c24c1eSJean-Baptiste Maneyrol 
45531c24c1eSJean-Baptiste Maneyrol int inv_icm42600_core_probe(struct regmap *regmap, int chip,
45631c24c1eSJean-Baptiste Maneyrol 			    inv_icm42600_bus_setup bus_setup)
45731c24c1eSJean-Baptiste Maneyrol {
45831c24c1eSJean-Baptiste Maneyrol 	struct device *dev = regmap_get_device(regmap);
45931c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st;
46031c24c1eSJean-Baptiste Maneyrol 	int ret;
46131c24c1eSJean-Baptiste Maneyrol 
46231c24c1eSJean-Baptiste Maneyrol 	if (chip < 0 || chip >= INV_CHIP_NB) {
46331c24c1eSJean-Baptiste Maneyrol 		dev_err(dev, "invalid chip = %d\n", chip);
46431c24c1eSJean-Baptiste Maneyrol 		return -ENODEV;
46531c24c1eSJean-Baptiste Maneyrol 	}
46631c24c1eSJean-Baptiste Maneyrol 
46731c24c1eSJean-Baptiste Maneyrol 	st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
46831c24c1eSJean-Baptiste Maneyrol 	if (!st)
46931c24c1eSJean-Baptiste Maneyrol 		return -ENOMEM;
47031c24c1eSJean-Baptiste Maneyrol 
47131c24c1eSJean-Baptiste Maneyrol 	dev_set_drvdata(dev, st);
47231c24c1eSJean-Baptiste Maneyrol 	mutex_init(&st->lock);
47331c24c1eSJean-Baptiste Maneyrol 	st->chip = chip;
47431c24c1eSJean-Baptiste Maneyrol 	st->map = regmap;
47531c24c1eSJean-Baptiste Maneyrol 
47631c24c1eSJean-Baptiste Maneyrol 	ret = iio_read_mount_matrix(dev, "mount-matrix", &st->orientation);
47731c24c1eSJean-Baptiste Maneyrol 	if (ret) {
47831c24c1eSJean-Baptiste Maneyrol 		dev_err(dev, "failed to retrieve mounting matrix %d\n", ret);
47931c24c1eSJean-Baptiste Maneyrol 		return ret;
48031c24c1eSJean-Baptiste Maneyrol 	}
48131c24c1eSJean-Baptiste Maneyrol 
48231c24c1eSJean-Baptiste Maneyrol 	st->vdd_supply = devm_regulator_get(dev, "vdd");
48331c24c1eSJean-Baptiste Maneyrol 	if (IS_ERR(st->vdd_supply))
48431c24c1eSJean-Baptiste Maneyrol 		return PTR_ERR(st->vdd_supply);
48531c24c1eSJean-Baptiste Maneyrol 
48631c24c1eSJean-Baptiste Maneyrol 	st->vddio_supply = devm_regulator_get(dev, "vddio");
48731c24c1eSJean-Baptiste Maneyrol 	if (IS_ERR(st->vddio_supply))
48831c24c1eSJean-Baptiste Maneyrol 		return PTR_ERR(st->vddio_supply);
48931c24c1eSJean-Baptiste Maneyrol 
49031c24c1eSJean-Baptiste Maneyrol 	ret = regulator_enable(st->vdd_supply);
49131c24c1eSJean-Baptiste Maneyrol 	if (ret)
49231c24c1eSJean-Baptiste Maneyrol 		return ret;
49331c24c1eSJean-Baptiste Maneyrol 	msleep(INV_ICM42600_POWER_UP_TIME_MS);
49431c24c1eSJean-Baptiste Maneyrol 
49531c24c1eSJean-Baptiste Maneyrol 	ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vdd_reg, st);
49631c24c1eSJean-Baptiste Maneyrol 	if (ret)
49731c24c1eSJean-Baptiste Maneyrol 		return ret;
49831c24c1eSJean-Baptiste Maneyrol 
49931c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_enable_regulator_vddio(st);
50031c24c1eSJean-Baptiste Maneyrol 	if (ret)
50131c24c1eSJean-Baptiste Maneyrol 		return ret;
50231c24c1eSJean-Baptiste Maneyrol 
50331c24c1eSJean-Baptiste Maneyrol 	ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vddio_reg, st);
50431c24c1eSJean-Baptiste Maneyrol 	if (ret)
50531c24c1eSJean-Baptiste Maneyrol 		return ret;
50631c24c1eSJean-Baptiste Maneyrol 
50731c24c1eSJean-Baptiste Maneyrol 	/* setup chip registers */
50831c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_setup(st, bus_setup);
50931c24c1eSJean-Baptiste Maneyrol 	if (ret)
51031c24c1eSJean-Baptiste Maneyrol 		return ret;
51131c24c1eSJean-Baptiste Maneyrol 
512a095fadbSJean-Baptiste Maneyrol 	st->indio_gyro = inv_icm42600_gyro_init(st);
513a095fadbSJean-Baptiste Maneyrol 	if (IS_ERR(st->indio_gyro))
514a095fadbSJean-Baptiste Maneyrol 		return PTR_ERR(st->indio_gyro);
515a095fadbSJean-Baptiste Maneyrol 
516a47c1cdcSJean-Baptiste Maneyrol 	st->indio_accel = inv_icm42600_accel_init(st);
517a47c1cdcSJean-Baptiste Maneyrol 	if (IS_ERR(st->indio_accel))
518a47c1cdcSJean-Baptiste Maneyrol 		return PTR_ERR(st->indio_accel);
519a47c1cdcSJean-Baptiste Maneyrol 
52031c24c1eSJean-Baptiste Maneyrol 	/* setup runtime power management */
52131c24c1eSJean-Baptiste Maneyrol 	ret = pm_runtime_set_active(dev);
52231c24c1eSJean-Baptiste Maneyrol 	if (ret)
52331c24c1eSJean-Baptiste Maneyrol 		return ret;
52431c24c1eSJean-Baptiste Maneyrol 	pm_runtime_get_noresume(dev);
52531c24c1eSJean-Baptiste Maneyrol 	pm_runtime_enable(dev);
52631c24c1eSJean-Baptiste Maneyrol 	pm_runtime_set_autosuspend_delay(dev, INV_ICM42600_SUSPEND_DELAY_MS);
52731c24c1eSJean-Baptiste Maneyrol 	pm_runtime_use_autosuspend(dev);
52831c24c1eSJean-Baptiste Maneyrol 	pm_runtime_put(dev);
52931c24c1eSJean-Baptiste Maneyrol 
53031c24c1eSJean-Baptiste Maneyrol 	return devm_add_action_or_reset(dev, inv_icm42600_disable_pm, dev);
53131c24c1eSJean-Baptiste Maneyrol }
53231c24c1eSJean-Baptiste Maneyrol EXPORT_SYMBOL_GPL(inv_icm42600_core_probe);
53331c24c1eSJean-Baptiste Maneyrol 
53431c24c1eSJean-Baptiste Maneyrol /*
53531c24c1eSJean-Baptiste Maneyrol  * Suspend saves sensors state and turns everything off.
53631c24c1eSJean-Baptiste Maneyrol  * Check first if runtime suspend has not already done the job.
53731c24c1eSJean-Baptiste Maneyrol  */
53831c24c1eSJean-Baptiste Maneyrol static int __maybe_unused inv_icm42600_suspend(struct device *dev)
53931c24c1eSJean-Baptiste Maneyrol {
54031c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
54131c24c1eSJean-Baptiste Maneyrol 	int ret;
54231c24c1eSJean-Baptiste Maneyrol 
54331c24c1eSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
54431c24c1eSJean-Baptiste Maneyrol 
54531c24c1eSJean-Baptiste Maneyrol 	st->suspended.gyro = st->conf.gyro.mode;
54631c24c1eSJean-Baptiste Maneyrol 	st->suspended.accel = st->conf.accel.mode;
54731c24c1eSJean-Baptiste Maneyrol 	st->suspended.temp = st->conf.temp_en;
54831c24c1eSJean-Baptiste Maneyrol 	if (pm_runtime_suspended(dev)) {
54931c24c1eSJean-Baptiste Maneyrol 		ret = 0;
55031c24c1eSJean-Baptiste Maneyrol 		goto out_unlock;
55131c24c1eSJean-Baptiste Maneyrol 	}
55231c24c1eSJean-Baptiste Maneyrol 
55331c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
55431c24c1eSJean-Baptiste Maneyrol 					 INV_ICM42600_SENSOR_MODE_OFF, false,
55531c24c1eSJean-Baptiste Maneyrol 					 NULL);
55631c24c1eSJean-Baptiste Maneyrol 	if (ret)
55731c24c1eSJean-Baptiste Maneyrol 		goto out_unlock;
55831c24c1eSJean-Baptiste Maneyrol 
55931c24c1eSJean-Baptiste Maneyrol 	regulator_disable(st->vddio_supply);
56031c24c1eSJean-Baptiste Maneyrol 
56131c24c1eSJean-Baptiste Maneyrol out_unlock:
56231c24c1eSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
56331c24c1eSJean-Baptiste Maneyrol 	return ret;
56431c24c1eSJean-Baptiste Maneyrol }
56531c24c1eSJean-Baptiste Maneyrol 
56631c24c1eSJean-Baptiste Maneyrol /*
56731c24c1eSJean-Baptiste Maneyrol  * System resume gets the system back on and restores the sensors state.
56831c24c1eSJean-Baptiste Maneyrol  * Manually put runtime power management in system active state.
56931c24c1eSJean-Baptiste Maneyrol  */
57031c24c1eSJean-Baptiste Maneyrol static int __maybe_unused inv_icm42600_resume(struct device *dev)
57131c24c1eSJean-Baptiste Maneyrol {
57231c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
57331c24c1eSJean-Baptiste Maneyrol 	int ret;
57431c24c1eSJean-Baptiste Maneyrol 
57531c24c1eSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
57631c24c1eSJean-Baptiste Maneyrol 
57731c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_enable_regulator_vddio(st);
57831c24c1eSJean-Baptiste Maneyrol 	if (ret)
57931c24c1eSJean-Baptiste Maneyrol 		goto out_unlock;
58031c24c1eSJean-Baptiste Maneyrol 
58131c24c1eSJean-Baptiste Maneyrol 	pm_runtime_disable(dev);
58231c24c1eSJean-Baptiste Maneyrol 	pm_runtime_set_active(dev);
58331c24c1eSJean-Baptiste Maneyrol 	pm_runtime_enable(dev);
58431c24c1eSJean-Baptiste Maneyrol 
58531c24c1eSJean-Baptiste Maneyrol 	/* restore sensors state */
58631c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_pwr_mgmt0(st, st->suspended.gyro,
58731c24c1eSJean-Baptiste Maneyrol 					 st->suspended.accel,
58831c24c1eSJean-Baptiste Maneyrol 					 st->suspended.temp, NULL);
58931c24c1eSJean-Baptiste Maneyrol 	if (ret)
59031c24c1eSJean-Baptiste Maneyrol 		goto out_unlock;
59131c24c1eSJean-Baptiste Maneyrol 
59231c24c1eSJean-Baptiste Maneyrol out_unlock:
59331c24c1eSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
59431c24c1eSJean-Baptiste Maneyrol 	return ret;
59531c24c1eSJean-Baptiste Maneyrol }
59631c24c1eSJean-Baptiste Maneyrol 
59731c24c1eSJean-Baptiste Maneyrol /* Runtime suspend will turn off sensors that are enabled by iio devices. */
59831c24c1eSJean-Baptiste Maneyrol static int __maybe_unused inv_icm42600_runtime_suspend(struct device *dev)
59931c24c1eSJean-Baptiste Maneyrol {
60031c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
60131c24c1eSJean-Baptiste Maneyrol 	int ret;
60231c24c1eSJean-Baptiste Maneyrol 
60331c24c1eSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
60431c24c1eSJean-Baptiste Maneyrol 
60531c24c1eSJean-Baptiste Maneyrol 	/* disable all sensors */
60631c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
60731c24c1eSJean-Baptiste Maneyrol 					 INV_ICM42600_SENSOR_MODE_OFF, false,
60831c24c1eSJean-Baptiste Maneyrol 					 NULL);
60931c24c1eSJean-Baptiste Maneyrol 	if (ret)
61031c24c1eSJean-Baptiste Maneyrol 		goto error_unlock;
61131c24c1eSJean-Baptiste Maneyrol 
61231c24c1eSJean-Baptiste Maneyrol 	regulator_disable(st->vddio_supply);
61331c24c1eSJean-Baptiste Maneyrol 
61431c24c1eSJean-Baptiste Maneyrol error_unlock:
61531c24c1eSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
61631c24c1eSJean-Baptiste Maneyrol 	return ret;
61731c24c1eSJean-Baptiste Maneyrol }
61831c24c1eSJean-Baptiste Maneyrol 
61931c24c1eSJean-Baptiste Maneyrol /* Sensors are enabled by iio devices, no need to turn them back on here. */
62031c24c1eSJean-Baptiste Maneyrol static int __maybe_unused inv_icm42600_runtime_resume(struct device *dev)
62131c24c1eSJean-Baptiste Maneyrol {
62231c24c1eSJean-Baptiste Maneyrol 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
62331c24c1eSJean-Baptiste Maneyrol 	int ret;
62431c24c1eSJean-Baptiste Maneyrol 
62531c24c1eSJean-Baptiste Maneyrol 	mutex_lock(&st->lock);
62631c24c1eSJean-Baptiste Maneyrol 
62731c24c1eSJean-Baptiste Maneyrol 	ret = inv_icm42600_enable_regulator_vddio(st);
62831c24c1eSJean-Baptiste Maneyrol 
62931c24c1eSJean-Baptiste Maneyrol 	mutex_unlock(&st->lock);
63031c24c1eSJean-Baptiste Maneyrol 	return ret;
63131c24c1eSJean-Baptiste Maneyrol }
63231c24c1eSJean-Baptiste Maneyrol 
63331c24c1eSJean-Baptiste Maneyrol const struct dev_pm_ops inv_icm42600_pm_ops = {
63431c24c1eSJean-Baptiste Maneyrol 	SET_SYSTEM_SLEEP_PM_OPS(inv_icm42600_suspend, inv_icm42600_resume)
63531c24c1eSJean-Baptiste Maneyrol 	SET_RUNTIME_PM_OPS(inv_icm42600_runtime_suspend,
63631c24c1eSJean-Baptiste Maneyrol 			   inv_icm42600_runtime_resume, NULL)
63731c24c1eSJean-Baptiste Maneyrol };
63831c24c1eSJean-Baptiste Maneyrol EXPORT_SYMBOL_GPL(inv_icm42600_pm_ops);
63931c24c1eSJean-Baptiste Maneyrol 
64031c24c1eSJean-Baptiste Maneyrol MODULE_AUTHOR("InvenSense, Inc.");
64131c24c1eSJean-Baptiste Maneyrol MODULE_DESCRIPTION("InvenSense ICM-426xx device driver");
64231c24c1eSJean-Baptiste Maneyrol MODULE_LICENSE("GPL");
643