From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EE7B3AB5B7 for ; Thu, 16 Jul 2026 18:29:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784226548; cv=none; b=M3iZq1EiDMP8O96UvnYJJ7/oBTtjNfl26fwWaz166xb6Vgxn0QZW+FMMTa5xYkePDlcp4B9R6noaCILyD3jfPULQcjPMmENxZezgP8yA04UZLD2SwGRlvL7bCYCcdsh4nquI8EgriMNcskfJOoWfdw6z8M4dnnwiT4YuByaoLlU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784226548; c=relaxed/simple; bh=uPDPY9gACe0LopohOvUmUjvEtgK31xKBPobuK+Z+vDY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WQWPElK7OCMyegb0XRx4wGopUVOCKoVV6ZkkLEN3Gyif3ZNMoqA2f03ytntetgWXK1iM8DIixTlNI9KkHWyrYSoa+yVXxdj9TINi51007zYONuvCQbQ6eYfvhzLl18g2zLNKHXXZl6fCa68IVcjMkqKEB4L/esV0ETczt1wb1fw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eBO9lucM; arc=none smtp.client-ip=209.85.210.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eBO9lucM" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-7e9f69ee6f4so2743057a34.2 for ; Thu, 16 Jul 2026 11:29:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784226545; x=1784831345; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ij5Orqbp8V9YdfTMS/PBUpdTGEYoQLLtDuuiw/2VkVU=; b=eBO9lucMGetV5QJ1kPWiEpDldzdAJpo/2yOgXSlHkwFSD5ww9Gb3agTmKqxh6FYlbg PgRLW+V7omsx3b+VUdtP5RDzuLxFkEFrRASjQynGucWdbjFXc4Zdv9SDwdsvqn2fTN1D Yo207VZ3OiE93zp6LmzLiTNDrNfjhdEts76o+wp3UQV2tBJtT6W317kiGrdtxXsKKqD3 CfxMfshpDIphtuFspW+4UIkd1SDYNyXDqy/1eekH1tDCQarsqs+g1Uo80I7pt5R6CUC9 iB+9GrHDtw8AdcLPnIP6s+2W+fPG9iOb5ITD43/tu2bq+PW3XhVvMuyrZ1McnJsvaSpW c16g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784226545; x=1784831345; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ij5Orqbp8V9YdfTMS/PBUpdTGEYoQLLtDuuiw/2VkVU=; b=aqJg5k/szz2Uc5sDfU9+U2oOHSpdcrlIrbbMwhF98qj7d2SIuZZdfhSP9FIgFWU47q zSd3c90Nl/6NZXW0U7wKQofKqMrXtpiRzB4SBxIpyfVZmBRN5HyAvMAUc6br86kFWUU0 k2eWyazTp4fMcKE8+OKm5UaCilHj0i/TELt8Nl0RBQDRHrRirURnlSSm28Nm+D082UPj h5bVzEOgXuCJq7Orrfdfg9Tu4ZxJ3DpNmE+7MJVfTQA3hCr2c+qZmIAdbX5pp1vXfEpZ Yz8/kD3ujBJKLpze+NOF1hokY50Cqa3Rzc4lpWxfmL9S/QOg7qKgOdVH4EcBrq0ZDI7U Rd1Q== X-Gm-Message-State: AOJu0YyiP96pqHyxrZzmLrF6kMQPMLqGTjUJ8M8YoHPaZaP7Ko7sAhjU uuYDwbqZ1JPrJJXwNLX1cx0+QSAwIHr6djX0Ca7SUJUZxin3VUzoiKE+vXZnxQ== X-Gm-Gg: AfdE7cnU+WiAltdC5E8gerIxN0BiAJ5iEWTuww4N7q6OJVGwr8hO5T9VQiXyhMbpQXs r4zCSSjiOjBfEQA2FDbh5L2ZCwODolluLQ4oZWHhUETYp/RYjwFbDwnZno9RP9RgZaAetHFI4Lz ROi3g6A6AWN2Qo7lLPCTEqdWjwHnounM+lTSOQPVHTdWPEdZVnYp0kT9TNQgwsJIMRxpvDP71eh 3Qtp6sur7+Xp6H0u9gCYrbz1Zl6caB1PfNXWTIV2iY4KBedUAt7wTigQEnuFQWVYN9fOfyLNM3N bieRQTPgSOzAnL+y0A4n0alYiR2PC+IHae0O9QnE3JwuKqnYBzZdgIM8eF1oiYmOQyYUfQe0u+Y ALmHDvCDOV+f6CWFKkL5spCiVCF3q1vsmgvohky6hWvpeSSb8dSx3zoNN/kEnbsAZ3P6CnlhxeI Gq X-Received: by 2002:a05:6830:6519:b0:7ec:7528:2498 with SMTP id 46e09a7af769-7ec9fe49185mr505269a34.18.1784226544893; Thu, 16 Jul 2026 11:29:04 -0700 (PDT) Received: from localhost.localdomain ([2600:1700:fb0:1bc0::54]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7ebcb3f6826sm19939993a34.27.2026.07.16.11.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2026 11:29:04 -0700 (PDT) From: Chris Morgan To: linux-iio@vger.kernel.org Cc: andy@kernel.org, nuno.sa@analog.com, dlechner@baylibre.com, jic23@kernel.org, jean-baptiste.maneyrol@tdk.com, linux-rockchip@lists.infradead.org, devicetree@vger.kernel.org, heiko@sntech.de, conor+dt@kernel.org, krzk+dt@kernel.org, robh@kernel.org, andriy.shevchenko@intel.com, Chris Morgan Subject: [PATCH V17 5/9] iio: imu: inv_icm42607: Add PM support for icm42607 Date: Thu, 16 Jul 2026 13:26:32 -0500 Message-ID: <20260716182637.24310-6-macroalpha82@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260716182637.24310-1-macroalpha82@gmail.com> References: <20260716182637.24310-1-macroalpha82@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Chris Morgan Add power management support for the ICM42607 device driver. Signed-off-by: Chris Morgan --- drivers/iio/imu/inv_icm42607/inv_icm42607.h | 14 ++ .../iio/imu/inv_icm42607/inv_icm42607_core.c | 162 ++++++++++++++++++ .../iio/imu/inv_icm42607/inv_icm42607_i2c.c | 1 + .../iio/imu/inv_icm42607/inv_icm42607_spi.c | 1 + 4 files changed, 178 insertions(+) diff --git a/drivers/iio/imu/inv_icm42607/inv_icm42607.h b/drivers/iio/imu/inv_icm42607/inv_icm42607.h index 169e848caf2e..61927a6c2663 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607.h +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -125,6 +126,7 @@ struct inv_icm42607_hw { * @lock: lock for serializing multiple registers access. * @map: regmap pointer. * @vddio_supply: I/O voltage regulator for the chip. + * @vddio_en: I/O voltage status for runtime PM. * @conf: chip sensors configurations. * @orientation: sensor chip orientation relative to main hardware. */ @@ -133,6 +135,7 @@ struct inv_icm42607_state { struct mutex lock; struct regmap *map; struct regulator *vddio_supply; + bool vddio_en; struct inv_icm42607_conf conf; struct iio_mount_matrix orientation; }; @@ -352,11 +355,22 @@ struct inv_icm42607_state { #define INV_ICM42607_GYRO_STOP_TIME_US (45 * USEC_PER_MSEC) #define INV_ICM42607_TEMP_STARTUP_TIME_US 77 +/* + * Suspend delay assumed from other icm42600 series device, not + * documented in datasheet. + */ +#define INV_ICM42607_SUSPEND_DELAY_MS (2 * MSEC_PER_SEC) + typedef int (*inv_icm42607_bus_setup)(struct inv_icm42607_state *); extern const struct regmap_config inv_icm42607_regmap_config; extern const struct inv_icm42607_hw inv_icm42607_hw_data; extern const struct inv_icm42607_hw inv_icm42607p_hw_data; +extern const struct dev_pm_ops inv_icm42607_pm_ops; + +int inv_icm42607_get_pwr_mgmt0(struct inv_icm42607_state *st, + enum inv_icm42607_sensor_mode *gyro, + enum inv_icm42607_sensor_mode *accel); int inv_icm42607_core_probe(struct regmap *regmap, const struct inv_icm42607_hw *hw, diff --git a/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c b/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c index 6ec1d730017e..9613cba025c7 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c @@ -4,15 +4,19 @@ */ #include +#include #include #include #include #include +#include #include #include +#include #include #include #include +#include #include #include "inv_icm42607.h" @@ -100,6 +104,93 @@ const struct inv_icm42607_hw inv_icm42607p_hw_data = { }; EXPORT_SYMBOL_NS_GPL(inv_icm42607p_hw_data, "IIO_ICM42607"); +int inv_icm42607_get_pwr_mgmt0(struct inv_icm42607_state *st, + enum inv_icm42607_sensor_mode *gyro, + enum inv_icm42607_sensor_mode *accel) +{ + unsigned int val; + int ret; + + ret = regmap_read(st->map, INV_ICM42607_REG_PWR_MGMT0, &val); + if (ret) + return ret; + + *gyro = FIELD_GET(INV_ICM42607_PWR_MGMT0_GYRO_MODE_MASK, val); + *accel = FIELD_GET(INV_ICM42607_PWR_MGMT0_ACCEL_MODE_MASK, val); + + return 0; +} + +static int inv_icm42607_set_pwr_mgmt0(struct inv_icm42607_state *st, + enum inv_icm42607_sensor_mode gyro, + enum inv_icm42607_sensor_mode accel) +{ + enum inv_icm42607_sensor_mode oldaccel, oldgyro; + unsigned int sleepval_us; + unsigned int val; + s64 disable_wait; + int ret; + + ret = inv_icm42607_get_pwr_mgmt0(st, &oldgyro, &oldaccel); + if (ret) + return ret; + + if (gyro == oldgyro && accel == oldaccel) + return 0; + + /* + * Datasheet on page 14.26 says we need to ensure the gyro sensor is on + * for a minimum of 45ms. So if we transition from an on state to an + * off state make sure at least 45ms have passed before power off and + * wait if it hasn't. In case some platforms don't respond well to a + * sleep of 0, make sure the fsleep duration is > 0. + */ + if (!gyro && oldgyro) { + disable_wait = clamp(ktime_us_delta(st->conf.gyro_stop, ktime_get()), + 0, INV_ICM42607_GYRO_STOP_TIME_US); + + if (disable_wait > 0) + fsleep(disable_wait); + } + + val = FIELD_PREP(INV_ICM42607_PWR_MGMT0_GYRO_MODE_MASK, gyro) | + FIELD_PREP(INV_ICM42607_PWR_MGMT0_ACCEL_MODE_MASK, accel); + ret = regmap_write(st->map, INV_ICM42607_REG_PWR_MGMT0, val); + if (ret) + return ret; + + /* + * If a state change occurs from off to on, sleep for the startup + * time of the sensor, unless a sleep_ms is specified. Since more + * than one sensor can be transitioned from off to on, select the + * maximum time from each of the sensors changing from off to on. + * The startup time for the temp sensor is considerably smaller + * than the startup time for the other sensors and one or more are + * required to be on for the temp sensor to function, so any start + * delay should be enough. + */ + sleepval_us = 0; + if (accel && !oldaccel) + sleepval_us = max(sleepval_us, INV_ICM42607_ACCEL_STARTUP_TIME_US); + + if (gyro && !oldgyro) { + sleepval_us = max(sleepval_us, INV_ICM42607_GYRO_STARTUP_TIME_US); + /* Track the earliest we can turn off the gyroscope. */ + st->conf.gyro_stop = ktime_add_us(ktime_get(), + INV_ICM42607_GYRO_STOP_TIME_US); + } + + /* + * Only sleep if sleepval_us is greater than 0 in case some + * platforms have issues with a 0 delay. The 0 delay can happen + * if one or both sensors is shut down. + */ + if (sleepval_us > 0) + fsleep(sleepval_us); + + return 0; +} + static int inv_icm42607_set_init_conf(struct inv_icm42607_state *st, const struct inv_icm42607_conf *conf) { @@ -212,12 +303,17 @@ static int inv_icm42607_enable_vddio_reg(struct inv_icm42607_state *st) { int ret; + if (st->vddio_en) + return 0; + ret = regulator_enable(st->vddio_supply); if (ret) return ret; fsleep(INV_ICM42607_POWER_UP_TIME_US); + st->vddio_en = true; + return 0; } @@ -225,7 +321,12 @@ static void inv_icm42607_disable_vddio_reg(void *_data) { struct inv_icm42607_state *st = _data; + if (!st->vddio_en) + return; + regulator_disable(st->vddio_supply); + + st->vddio_en = false; } int inv_icm42607_core_probe(struct regmap *regmap, @@ -240,6 +341,8 @@ int inv_icm42607_core_probe(struct regmap *regmap, if (!st) return -ENOMEM; + dev_set_drvdata(dev, st); + ret = devm_mutex_init(dev, &st->lock); if (ret) return ret; @@ -275,10 +378,69 @@ int inv_icm42607_core_probe(struct regmap *regmap, if (ret) return ret; + ret = devm_pm_runtime_set_active_enabled(dev); + if (ret) + return ret; + + pm_runtime_set_autosuspend_delay(dev, INV_ICM42607_SUSPEND_DELAY_MS); + pm_runtime_use_autosuspend(dev); + return 0; } EXPORT_SYMBOL_NS_GPL(inv_icm42607_core_probe, "IIO_ICM42607"); +static int inv_icm42607_suspend(struct device *dev) +{ + struct inv_icm42607_state *st = dev_get_drvdata(dev); + int ret; + + ret = pm_runtime_force_suspend(dev); + if (ret) + return ret; + + inv_icm42607_disable_vddio_reg(st); + + return 0; +} + +static int inv_icm42607_resume(struct device *dev) +{ + struct inv_icm42607_state *st = dev_get_drvdata(dev); + int ret; + + ret = inv_icm42607_enable_vddio_reg(st); + if (ret) + return ret; + + return pm_runtime_force_resume(dev); +} + +static int inv_icm42607_runtime_suspend(struct device *dev) +{ + struct inv_icm42607_state *st = dev_get_drvdata(dev); + + guard(mutex)(&st->lock); + + /* + * Set sensors state to off. Since we only support one-shot + * today we can use runtime PM to turn sensors off when not + * in use, and then when needed the reads/writes will + * re-enable the sensors as needed. This reduces complexity, + * however the tradeoff is that an unused sensor won't be + * turned off until the entire chip is no longer in use. + */ + st->conf.gyro.mode = INV_ICM42607_SENSOR_MODE_OFF; + st->conf.accel.mode = INV_ICM42607_SENSOR_MODE_OFF; + + return inv_icm42607_set_pwr_mgmt0(st, st->conf.gyro.mode, + st->conf.accel.mode); +} + +EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42607_pm_ops, IIO_ICM42607) = { + SYSTEM_SLEEP_PM_OPS(inv_icm42607_suspend, inv_icm42607_resume) + RUNTIME_PM_OPS(inv_icm42607_runtime_suspend, NULL, NULL) +}; + MODULE_AUTHOR("InvenSense, Inc."); MODULE_DESCRIPTION("InvenSense ICM-42607 device driver"); MODULE_LICENSE("GPL"); diff --git a/drivers/iio/imu/inv_icm42607/inv_icm42607_i2c.c b/drivers/iio/imu/inv_icm42607/inv_icm42607_i2c.c index 8f74bf38aad1..dffd8d7d0e0e 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607_i2c.c +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607_i2c.c @@ -82,6 +82,7 @@ static struct i2c_driver inv_icm42607_driver = { .driver = { .name = "inv-icm42607-i2c", .of_match_table = inv_icm42607_of_matches, + .pm = pm_ptr(&inv_icm42607_pm_ops), }, .id_table = inv_icm42607_id, .probe = inv_icm42607_probe, diff --git a/drivers/iio/imu/inv_icm42607/inv_icm42607_spi.c b/drivers/iio/imu/inv_icm42607/inv_icm42607_spi.c index 8eee7f162126..1afe3af6e292 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607_spi.c +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607_spi.c @@ -95,6 +95,7 @@ static struct spi_driver inv_icm42607_driver = { .driver = { .name = "inv-icm42607-spi", .of_match_table = inv_icm42607_of_matches, + .pm = pm_ptr(&inv_icm42607_pm_ops), }, .id_table = inv_icm42607_spi_id_table, .probe = inv_icm42607_probe, -- 2.43.0