From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6C4D5CD4F58 for ; Mon, 18 May 2026 20:08:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=WnlnkLRwI3OhX35qYdoBX+91FDK476qvRK7sdZ9++cw=; b=2m+yZiXMQQam0j ItEJcdZ+HAqn60aNBg+i/Cru8BEE952IjDFBkIFjfnzqYurp9VV0zMMZhzqueRvoJ3CGuXPz/s48f n0uotXExpQ0nm4CNXb8Yh9yh3Jr1IXnPTkM3BU1vbMU16O9RrsbWHPJUZtNN98qms7obzMby+ENYT FxF6V3ULZLy+2fe5wrlRtoh3z5CRV6tIDISgDcfcpI6JeUVwikKcAxqkv6NtagIKrRHMoTefoYIaE GTmmBuCWtMP6DncO/kyOSS3K8iCfW6n0qSsDV3TRXhD9JHj7sXpdhXNXxhe2Q12MdrLrF9I1ScdM6 k9m/a9/fImE7H01Y4DNA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wP4Fq-0000000GqId-1eEj; Mon, 18 May 2026 20:07:58 +0000 Received: from mail-oo1-xc2e.google.com ([2607:f8b0:4864:20::c2e]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wP4Fn-0000000GqEv-2Ram for linux-rockchip@lists.infradead.org; Mon, 18 May 2026 20:07:56 +0000 Received: by mail-oo1-xc2e.google.com with SMTP id 006d021491bc7-69b8bac50dcso587827eaf.3 for ; Mon, 18 May 2026 13:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779134875; x=1779739675; darn=lists.infradead.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; bh=3xJ+xVgnZz6J8/ZOxTiHdKoDuz0ndhEP3E/chsRZjhM=; b=KTH2YRqy4hukuj9/26ulN8PSGZ1bBHeRUWOGL8+VglVr4BbFf6Oup19XhLTWb1DOse adi0hBsRF0nkcs3wguJA+ev7SX5JRnv1g+qoDecTixQ0yh8TerVQua9SXywTQkqIkGI2 p0XKU6ymUh/pokQCtb7C4C6So4iE0LZ4DQ6N+qCjjkGEd3K3JOB+50DMqS+rOg3pK6oa Ruvq96cnlAte5Hj0roOijHYm7FHFLcE9oTuC/PFzgV4TxgW8vkPvrxS3Bbt625fLrRSQ 49cj0cuwhUrcSSJUao1YKfc9csTLqrrfVKiP5xVe1tfwG1KQqeB43HKjwZzF+IlZ8bkF 0E+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779134875; x=1779739675; 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; bh=3xJ+xVgnZz6J8/ZOxTiHdKoDuz0ndhEP3E/chsRZjhM=; b=LmgqFAaLvBNCRXs9R0LIfterB/9u7pUl4YWMofW/PY/RQlzOdEQEdiFuC/kx1pxNyR 03Z9hW5Nhe+mPBtH/GzYW5rQcn8Xi0lgekarrIKPLnxhcMt4lEhRbbiXlk8OFzip3dHa 3YM2KPE7ZGOPPGzQxXvhBTyDU15EggcpOnKwz333XwTumLFbwvmqTRG987WaS649CKPX 5f6eh++rRqCDgL/pPtKJ61ashA+9YM3ap/zGNiSY7JBFFAy4TW0wjMR3Q5fiuxdKHAs4 HTSyCUdrlPsRocdkor0uStOdVComUrj6skmHXTWAM7kqxl+3V22v6A7jkTFWzKbbJBtu oQ+A== X-Forwarded-Encrypted: i=1; AFNElJ/PsIEDcXQYNooYLOUBaQya9dZnuLKQi6vE/t+UA+v1zpEK+81STMZGLsuWLX2aFQQg2K1/kz+p9zgLWXdJjg==@lists.infradead.org X-Gm-Message-State: AOJu0YwkB3UZ/G4/+pwCzED0j1xIza1uPfsAvk74Uy/Wwx1Pf6yEICge 2NZpkRWVRPpWSmT53X8CDRilGkMcsbHUKlK7cMJuOynflLYpfH1n/0z2 X-Gm-Gg: Acq92OGBUL5xqWn1hPOx7yfMcWhIUIlyuTyLyYVWZtKxx/SNqak/JQ1OLwtcRwIrcFJ At7Q7kfnE+96OgFqhq+PJjYH7iEucXp5HwEYxhjGyTTk6TZ1h0RxYhJCWjVkl/xX8q91zsjvPKs rfFhTTxECQKZvhDJB0TqFB+kXWWyo9ANQxMppEqDvHFwIhkWhZdeuIm/s/7rZg+NMzXSORNzQxe TxFv2/k/oKSyb3w4ezxN9uNsGPxQfVZbiDsBiRdiADCHA1jTnEMHZ4eGdSUVeJWZ/pWzBdETPdU YDkqRGQu1pfBwR9nSLkSSP13hzfQdCeF7k4EBF/4syOLHwkmFpTRfspR0gdXKPOy2YPLHAcRGgX jPLgGd5TjG8jwRfg99lGjAc8m8+1+aGEKlXBXGYWgf/BxTP9gD4vOhO4mPqgo2x7vCYK2aLazAr VG1FmV+6G28QaAmtfPuOsa X-Received: by 2002:a05:6820:2210:b0:696:17a3:4673 with SMTP id 006d021491bc7-69c9bfcf81dmr9991865eaf.50.1779134874819; Mon, 18 May 2026 13:07:54 -0700 (PDT) Received: from localhost.localdomain ([2600:1700:fb0:1bc0::54]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-69d0462e85esm6113360eaf.6.2026.05.18.13.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2026 13:07:54 -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 V8 05/10] iio: imu: inv_icm42607: Add PM support for icm42607 Date: Mon, 18 May 2026 15:05:20 -0500 Message-ID: <20260518200526.458421-6-macroalpha82@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260518200526.458421-1-macroalpha82@gmail.com> References: <20260518200526.458421-1-macroalpha82@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260518_130755_641483_92F4BA86 X-CRM114-Status: GOOD ( 22.26 ) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-rockchip" Errors-To: linux-rockchip-bounces+linux-rockchip=archiver.kernel.org@lists.infradead.org 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 | 18 +++ .../iio/imu/inv_icm42607/inv_icm42607_core.c | 147 ++++++++++++++++++ .../iio/imu/inv_icm42607/inv_icm42607_i2c.c | 1 + .../iio/imu/inv_icm42607/inv_icm42607_spi.c | 1 + 4 files changed, 167 insertions(+) diff --git a/drivers/iio/imu/inv_icm42607/inv_icm42607.h b/drivers/iio/imu/inv_icm42607/inv_icm42607.h index 74d8d3d7c890..b05828415053 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607.h +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -96,24 +97,34 @@ struct inv_icm42607_hw { const struct inv_icm42607_conf *conf; }; +struct inv_icm42607_suspended { + enum inv_icm42607_sensor_mode gyro; + enum inv_icm42607_sensor_mode accel; + bool temp; +}; + /** * struct inv_icm42607_state - driver state variables * @lock: lock for serializing multiple registers access. * @hw: Hardware specific data. * @map: regmap pointer. * @vddio_supply: I/O voltage regulator for the chip. + * @vddio_en: I/O voltage status for runtime PM. * @irq: chip irq, required to enable/disable and set wakeup * @orientation: sensor chip orientation relative to main hardware. * @conf: chip sensors configurations. + * @suspended: suspended sensors configuration. */ struct inv_icm42607_state { struct mutex lock; const struct inv_icm42607_hw *hw; struct regmap *map; struct regulator *vddio_supply; + bool vddio_en; int irq; struct iio_mount_matrix orientation; struct inv_icm42607_conf conf; + struct inv_icm42607_suspended suspended; }; /* Virtual register addresses: @bank on MSB (4 upper bits), @address on LSB */ @@ -332,11 +343,18 @@ struct inv_icm42607_state { #define INV_ICM42607_GYRO_STARTUP_TIME_MS 30 #define INV_ICM42607_TEMP_STARTUP_TIME_MS 77 +/* + * Suspend delay assumed from other icm42600 series device, not + * documented in datasheet. + */ +#define INV_ICM42607_SUSPEND_DELAY_MS 2000 + 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_core_probe(struct regmap *regmap, const struct inv_icm42607_hw *hw, inv_icm42607_bus_setup bus_setup); diff --git a/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c b/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c index e9c81b52f9ef..bc0cefa2fb77 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607_core.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,62 @@ const struct inv_icm42607_hw inv_icm42607p_hw_data = { }; EXPORT_SYMBOL_NS_GPL(inv_icm42607p_hw_data, "IIO_ICM42607"); +static int inv_icm42607_set_pwr_mgmt0(struct inv_icm42607_state *st, + enum inv_icm42607_sensor_mode gyro, + enum inv_icm42607_sensor_mode accel, + bool temp, unsigned int *sleep_ms) +{ + enum inv_icm42607_sensor_mode oldgyro = st->conf.gyro.mode; + enum inv_icm42607_sensor_mode oldaccel = st->conf.accel.mode; + bool oldtemp = st->conf.temp_en; + unsigned int sleepval; + unsigned int val; + int ret; + + if (gyro == oldgyro && accel == oldaccel && temp == oldtemp) + return 0; + + val = FIELD_PREP(INV_ICM42607_PWR_MGMT0_GYRO_MODE_MASK, gyro); + val |= FIELD_PREP(INV_ICM42607_PWR_MGMT0_ACCEL_MODE_MASK, accel); + if (!temp) + val |= INV_ICM42607_PWR_MGMT0_ACCEL_LP_CLK_SEL; + ret = regmap_write(st->map, INV_ICM42607_REG_PWR_MGMT0, val); + if (ret) + return ret; + + st->conf.gyro.mode = gyro; + st->conf.accel.mode = accel; + st->conf.temp_en = temp; + + sleepval = 0; + if (temp && !oldtemp) { + if (sleepval < INV_ICM42607_TEMP_STARTUP_TIME_MS) + sleepval = INV_ICM42607_TEMP_STARTUP_TIME_MS; + } + if (accel != oldaccel && oldaccel == INV_ICM42607_SENSOR_MODE_OFF) { + usleep_range(200, 300); + if (sleepval < INV_ICM42607_ACCEL_STARTUP_TIME_MS) + sleepval = INV_ICM42607_ACCEL_STARTUP_TIME_MS; + } + if (gyro != oldgyro) { + if (oldgyro == INV_ICM42607_SENSOR_MODE_OFF) { + usleep_range(200, 300); + if (sleepval < INV_ICM42607_GYRO_STARTUP_TIME_MS) + sleepval = INV_ICM42607_GYRO_STARTUP_TIME_MS; + } else if (gyro == INV_ICM42607_SENSOR_MODE_OFF) { + if (sleepval < INV_ICM42607_GYRO_STARTUP_TIME_MS) + sleepval = INV_ICM42607_GYRO_STARTUP_TIME_MS; + } + } + + if (sleep_ms) + *sleep_ms = sleepval; + else if (sleepval) + msleep(sleepval); + + return 0; +} + static int inv_icm42607_set_conf(struct inv_icm42607_state *st, const struct inv_icm42607_conf *conf) { @@ -179,12 +236,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; } @@ -192,7 +254,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, const struct inv_icm42607_hw *hw, @@ -249,10 +316,90 @@ int inv_icm42607_core_probe(struct regmap *regmap, const struct inv_icm42607_hw 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; + + guard(mutex)(&st->lock); + + if (pm_runtime_suspended(dev)) + return 0; + + ret = inv_icm42607_set_pwr_mgmt0(st, INV_ICM42607_SENSOR_MODE_OFF, + INV_ICM42607_SENSOR_MODE_OFF, + false, NULL); + 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; + + guard(mutex)(&st->lock); + + if (pm_runtime_suspended(dev)) + return 0; + + ret = inv_icm42607_enable_vddio_reg(st); + if (ret) + return ret; + + /* Nothing else to restore at this time. */ + + return 0; +} + +static int inv_icm42607_runtime_suspend(struct device *dev) +{ + struct inv_icm42607_state *st = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&st->lock); + + ret = inv_icm42607_set_pwr_mgmt0(st, INV_ICM42607_SENSOR_MODE_OFF, + INV_ICM42607_SENSOR_MODE_OFF, false, + NULL); + if (ret) + return ret; + + inv_icm42607_disable_vddio_reg(st); + + return 0; +} + +static int inv_icm42607_runtime_resume(struct device *dev) +{ + struct inv_icm42607_state *st = dev_get_drvdata(dev); + + guard(mutex)(&st->lock); + + return inv_icm42607_enable_vddio_reg(st); +} + +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, + inv_icm42607_runtime_resume, NULL) +}; + MODULE_AUTHOR("InvenSense, Inc."); MODULE_DESCRIPTION("InvenSense ICM-42607x 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 8631579a609a..a51c764bf1dc 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607_i2c.c +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607_i2c.c @@ -77,6 +77,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 960b5d3e9abc..9a37ad8d986a 100644 --- a/drivers/iio/imu/inv_icm42607/inv_icm42607_spi.c +++ b/drivers/iio/imu/inv_icm42607/inv_icm42607_spi.c @@ -89,6 +89,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 _______________________________________________ Linux-rockchip mailing list Linux-rockchip@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-rockchip