From ec44816ae7a2010261b628491e12e4db084d3ece Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 22 Jun 2023 17:01:38 +1000 Subject: [PATCH] drivers: flash: spi_nor: support device PM Support device power management in spi_nor driver. Only use SUSPEND/RESUME if `CONFIG_SPI_NOR_IDLE_IN_DPD` is not enabled to avoid state conflicts. Signed-off-by: Jordan Yates --- drivers/flash/spi_nor.c | 42 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/drivers/flash/spi_nor.c b/drivers/flash/spi_nor.c index acde8d59992..ceb30f39c2a 100644 --- a/drivers/flash/spi_nor.c +++ b/drivers/flash/spi_nor.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "spi_nor.h" #include "jesd216.h" @@ -1246,6 +1247,44 @@ static int spi_nor_configure(const struct device *dev) return 0; } +#ifdef CONFIG_PM_DEVICE + +static int spi_nor_pm_control(const struct device *dev, enum pm_device_action action) +{ + int rc = 0; + + switch (action) { +#ifdef CONFIG_SPI_NOR_IDLE_IN_DPD + case PM_DEVICE_ACTION_SUSPEND: + case PM_DEVICE_ACTION_RESUME: + break; +#else + case PM_DEVICE_ACTION_SUSPEND: + acquire_device(dev); + rc = enter_dpd(dev); + release_device(dev); + break; + case PM_DEVICE_ACTION_RESUME: + acquire_device(dev); + rc = exit_dpd(dev); + release_device(dev); + break; +#endif /* CONFIG_SPI_NOR_IDLE_IN_DPD */ + case PM_DEVICE_ACTION_TURN_ON: + /* Coming out of power off */ + rc = spi_nor_configure(dev); + break; + case PM_DEVICE_ACTION_TURN_OFF: + break; + default: + rc = -ENOSYS; + } + + return rc; +} + +#endif /* CONFIG_PM_DEVICE */ + /** * @brief Initialize and configure the flash * @@ -1389,7 +1428,8 @@ static const struct spi_nor_config spi_nor_config_0 = { static struct spi_nor_data spi_nor_data_0; -DEVICE_DT_INST_DEFINE(0, &spi_nor_init, NULL, +PM_DEVICE_DT_INST_DEFINE(0, spi_nor_pm_control); +DEVICE_DT_INST_DEFINE(0, &spi_nor_init, PM_DEVICE_DT_INST_GET(0), &spi_nor_data_0, &spi_nor_config_0, POST_KERNEL, CONFIG_SPI_NOR_INIT_PRIORITY, &spi_nor_api);