|
10 | 10 | #include <zephyr/drivers/flash.h> |
11 | 11 | #include <zephyr/init.h> |
12 | 12 | #include <zephyr/pm/device.h> |
| 13 | +#include <zephyr/pm/device_runtime.h> |
13 | 14 | #include <zephyr/drivers/pinctrl.h> |
14 | 15 | #include <soc.h> |
15 | 16 | #include <string.h> |
@@ -174,7 +175,9 @@ BUILD_ASSERT(DT_INST_PROP(0, address_size_32), |
174 | 175 | "After entering 4 byte addressing mode, 4 byte addressing is expected"); |
175 | 176 | #endif |
176 | 177 |
|
| 178 | +#ifndef CONFIG_PM_DEVICE_RUNTIME |
177 | 179 | static bool qspi_initialized; |
| 180 | +#endif |
178 | 181 |
|
179 | 182 | static int qspi_device_init(const struct device *dev); |
180 | 183 | 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) |
350 | 353 |
|
351 | 354 | static int qspi_device_init(const struct device *dev) |
352 | 355 | { |
| 356 | +#ifdef CONFIG_PM_DEVICE_RUNTIME |
| 357 | + return pm_device_runtime_get(dev); |
| 358 | +#else |
353 | 359 | struct qspi_nor_data *dev_data = dev->data; |
354 | 360 | nrfx_err_t res; |
355 | 361 | int ret = 0; |
@@ -377,10 +383,18 @@ static int qspi_device_init(const struct device *dev) |
377 | 383 | qspi_unlock(dev); |
378 | 384 |
|
379 | 385 | return ret; |
| 386 | +#endif |
380 | 387 | } |
381 | 388 |
|
382 | 389 | static void qspi_device_uninit(const struct device *dev) |
383 | 390 | { |
| 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 |
384 | 398 | bool last = true; |
385 | 399 |
|
386 | 400 | qspi_lock(dev); |
@@ -408,6 +422,7 @@ static void qspi_device_uninit(const struct device *dev) |
408 | 422 | } |
409 | 423 |
|
410 | 424 | qspi_unlock(dev); |
| 425 | +#endif |
411 | 426 | } |
412 | 427 |
|
413 | 428 | /* QSPI send custom command. |
@@ -1171,7 +1186,16 @@ static int qspi_nor_configure(const struct device *dev) |
1171 | 1186 | return ret; |
1172 | 1187 | } |
1173 | 1188 |
|
| 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 |
1174 | 1197 | qspi_device_uninit(dev); |
| 1198 | +#endif |
1175 | 1199 |
|
1176 | 1200 | /* now the spi bus is configured, we can verify the flash id */ |
1177 | 1201 | if (qspi_nor_read_id(dev) != 0) { |
@@ -1317,10 +1341,13 @@ static int qspi_nor_pm_action(const struct device *dev, |
1317 | 1341 |
|
1318 | 1342 | switch (action) { |
1319 | 1343 | case PM_DEVICE_ACTION_SUSPEND: |
| 1344 | +#ifndef CONFIG_PM_DEVICE_RUNTIME |
| 1345 | + /* If PM_DEVICE_RUNTIME, we don't uninit after RESUME */ |
1320 | 1346 | ret = qspi_device_init(dev); |
1321 | 1347 | if (ret < 0) { |
1322 | 1348 | return ret; |
1323 | 1349 | } |
| 1350 | +#endif |
1324 | 1351 |
|
1325 | 1352 | if (nrfx_qspi_mem_busy_check() != NRFX_SUCCESS) { |
1326 | 1353 | return -EBUSY; |
@@ -1357,7 +1384,10 @@ static int qspi_nor_pm_action(const struct device *dev, |
1357 | 1384 | return ret; |
1358 | 1385 | } |
1359 | 1386 |
|
| 1387 | +#ifndef CONFIG_PM_DEVICE_RUNTIME |
| 1388 | + /* If PM_DEVICE_RUNTIME, we're immediately going to use the device */ |
1360 | 1389 | qspi_device_uninit(dev); |
| 1390 | +#endif |
1361 | 1391 | break; |
1362 | 1392 |
|
1363 | 1393 | default: |
|
0 commit comments