Skip to content

Commit a328825

Browse files
Zack Corneliuscarlescufi
authored andcommitted
drivers: flash: nrf_qspi_nor: Add runtime PM
Add PM_DEVICE_RUNTIME support to nordic QSPI NOR flash driver, putting the QSPI peripheral and flash ship into DPD if available Signed-off-by: Zack Cornelius <zcornelius@securityesys.com>
1 parent 7e4de93 commit a328825

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

drivers/flash/nrf_qspi_nor.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/drivers/flash.h>
1111
#include <zephyr/init.h>
1212
#include <zephyr/pm/device.h>
13+
#include <zephyr/pm/device_runtime.h>
1314
#include <zephyr/drivers/pinctrl.h>
1415
#include <soc.h>
1516
#include <string.h>
@@ -174,7 +175,9 @@ BUILD_ASSERT(DT_INST_PROP(0, address_size_32),
174175
"After entering 4 byte addressing mode, 4 byte addressing is expected");
175176
#endif
176177

178+
#ifndef CONFIG_PM_DEVICE_RUNTIME
177179
static bool qspi_initialized;
180+
#endif
178181

179182
static int qspi_device_init(const struct device *dev);
180183
static void qspi_device_uninit(const struct device *dev);
@@ -350,6 +353,9 @@ static void qspi_handler(nrfx_qspi_evt_t event, void *p_context)
350353

351354
static int qspi_device_init(const struct device *dev)
352355
{
356+
#ifdef CONFIG_PM_DEVICE_RUNTIME
357+
return pm_device_runtime_get(dev);
358+
#else
353359
struct qspi_nor_data *dev_data = dev->data;
354360
nrfx_err_t res;
355361
int ret = 0;
@@ -377,10 +383,18 @@ static int qspi_device_init(const struct device *dev)
377383
qspi_unlock(dev);
378384

379385
return ret;
386+
#endif
380387
}
381388

382389
static void qspi_device_uninit(const struct device *dev)
383390
{
391+
#ifdef CONFIG_PM_DEVICE_RUNTIME
392+
int ret = pm_device_runtime_put(dev);
393+
394+
if (ret < 0) {
395+
LOG_ERR("Failed to schedule device sleep: %d", ret);
396+
}
397+
#else
384398
bool last = true;
385399

386400
qspi_lock(dev);
@@ -408,6 +422,7 @@ static void qspi_device_uninit(const struct device *dev)
408422
}
409423

410424
qspi_unlock(dev);
425+
#endif
411426
}
412427

413428
/* QSPI send custom command.
@@ -1171,7 +1186,16 @@ static int qspi_nor_configure(const struct device *dev)
11711186
return ret;
11721187
}
11731188

1189+
#ifdef CONFIG_PM_DEVICE_RUNTIME
1190+
ret = pm_device_runtime_enable(dev);
1191+
if (ret < 0) {
1192+
LOG_ERR("Failed to enable runtime power management: %d", ret);
1193+
} else {
1194+
LOG_DBG("Runtime power management enabled");
1195+
}
1196+
#else
11741197
qspi_device_uninit(dev);
1198+
#endif
11751199

11761200
/* now the spi bus is configured, we can verify the flash id */
11771201
if (qspi_nor_read_id(dev) != 0) {
@@ -1317,10 +1341,13 @@ static int qspi_nor_pm_action(const struct device *dev,
13171341

13181342
switch (action) {
13191343
case PM_DEVICE_ACTION_SUSPEND:
1344+
#ifndef CONFIG_PM_DEVICE_RUNTIME
1345+
/* If PM_DEVICE_RUNTIME, we don't uninit after RESUME */
13201346
ret = qspi_device_init(dev);
13211347
if (ret < 0) {
13221348
return ret;
13231349
}
1350+
#endif
13241351

13251352
if (nrfx_qspi_mem_busy_check() != NRFX_SUCCESS) {
13261353
return -EBUSY;
@@ -1357,7 +1384,10 @@ static int qspi_nor_pm_action(const struct device *dev,
13571384
return ret;
13581385
}
13591386

1387+
#ifndef CONFIG_PM_DEVICE_RUNTIME
1388+
/* If PM_DEVICE_RUNTIME, we're immediately going to use the device */
13601389
qspi_device_uninit(dev);
1390+
#endif
13611391
break;
13621392

13631393
default:

0 commit comments

Comments
 (0)