Skip to content

Commit 2a88cb5

Browse files
JordanYatescfriedt
authored andcommitted
spi: stm32: initialise according to zephyr,pm-device-runtime-auto
Don't automatically enable device runtime PM on the SPI port just because `PM_DEVICE_RUNTIME` is enabled. Require the user to explicitly call `pm_device_runtime_enable` on the port, or add `zephyr,pm-device-runtime-auto` to the devicetree node. Through the usage of `pm_device_driver_init`, the default clock control and pinctrl handling can all be contained in `spi_stm32_pm_action`. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent d8f87a6 commit 2a88cb5

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

drivers/spi/spi_ll_stm32.c

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,25 +1403,38 @@ static inline bool spi_stm32_is_subghzspi(const struct device *dev)
14031403
#endif /* st_stm32_spi_subghz */
14041404
}
14051405

1406-
#ifdef CONFIG_PM_DEVICE
1406+
static int spi_stm32_pinctrl_apply(const struct device *dev, uint8_t id)
1407+
{
1408+
const struct spi_stm32_config *config = dev->config;
1409+
int err;
1410+
1411+
if (spi_stm32_is_subghzspi(dev)) {
1412+
return 0;
1413+
}
1414+
1415+
/* Move pins to requested state */
1416+
err = pinctrl_apply_state(config->pcfg, id);
1417+
if ((id == PINCTRL_STATE_SLEEP) && (err == -ENOENT)) {
1418+
/* Sleep state is optional */
1419+
err = 0;
1420+
}
1421+
return err;
1422+
}
1423+
14071424
static int spi_stm32_pm_action(const struct device *dev,
14081425
enum pm_device_action action)
14091426
{
14101427
const struct spi_stm32_config *config = dev->config;
14111428
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
14121429
int err;
14131430

1414-
14151431
switch (action) {
14161432
case PM_DEVICE_ACTION_RESUME:
1417-
if (!spi_stm32_is_subghzspi(dev)) {
1418-
/* Set pins to active state */
1419-
err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
1420-
if (err < 0) {
1421-
return err;
1422-
}
1433+
/* Configure pins for active mode */
1434+
err = spi_stm32_pinctrl_apply(dev, PINCTRL_STATE_DEFAULT);
1435+
if (err < 0) {
1436+
return err;
14231437
}
1424-
14251438
/* enable clock */
14261439
err = clock_control_on(clk, (clock_control_subsys_t)&config->pclken[0]);
14271440
if (err != 0) {
@@ -1436,30 +1449,19 @@ static int spi_stm32_pm_action(const struct device *dev,
14361449
LOG_ERR("Could not disable SPI clock");
14371450
return err;
14381451
}
1439-
1440-
if (!spi_stm32_is_subghzspi(dev)) {
1441-
/* Move pins to sleep state */
1442-
err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
1443-
if ((err < 0) && (err != -ENOENT)) {
1444-
/*
1445-
* If returning -ENOENT, no pins where defined for sleep mode :
1446-
* Do not output on console (might sleep already) when going to
1447-
* sleep,
1448-
* "SPI pinctrl sleep state not available"
1449-
* and don't block PM suspend.
1450-
* Else return the error.
1451-
*/
1452-
return err;
1453-
}
1454-
}
1452+
/* Configure pins for sleep mode */
1453+
return spi_stm32_pinctrl_apply(dev, PINCTRL_STATE_SLEEP);
1454+
case PM_DEVICE_ACTION_TURN_ON:
1455+
/* Configure pins for sleep mode */
1456+
return spi_stm32_pinctrl_apply(dev, PINCTRL_STATE_SLEEP);
1457+
case PM_DEVICE_ACTION_TURN_OFF:
14551458
break;
14561459
default:
14571460
return -ENOTSUP;
14581461
}
14591462

14601463
return 0;
14611464
}
1462-
#endif /* CONFIG_PM_DEVICE */
14631465

14641466
static int spi_stm32_init(const struct device *dev)
14651467
{
@@ -1472,13 +1474,6 @@ static int spi_stm32_init(const struct device *dev)
14721474
return -ENODEV;
14731475
}
14741476

1475-
err = clock_control_on(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
1476-
(clock_control_subsys_t) &cfg->pclken[0]);
1477-
if (err < 0) {
1478-
LOG_ERR("Could not enable SPI clock");
1479-
return err;
1480-
}
1481-
14821477
if (IS_ENABLED(STM32_SPI_DOMAIN_CLOCK_SUPPORT) && (cfg->pclk_len > 1)) {
14831478
err = clock_control_configure(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
14841479
(clock_control_subsys_t) &cfg->pclken[1],
@@ -1489,15 +1484,6 @@ static int spi_stm32_init(const struct device *dev)
14891484
}
14901485
}
14911486

1492-
if (!spi_stm32_is_subghzspi(dev)) {
1493-
/* Configure dt provided device signals when available */
1494-
err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
1495-
if (err < 0) {
1496-
LOG_ERR("SPI pinctrl setup failed (%d)", err);
1497-
return err;
1498-
}
1499-
}
1500-
15011487
#ifdef CONFIG_SPI_STM32_INTERRUPT
15021488
cfg->irq_config(dev);
15031489
#endif /* CONFIG_SPI_STM32_INTERRUPT */
@@ -1526,7 +1512,7 @@ static int spi_stm32_init(const struct device *dev)
15261512

15271513
spi_context_unlock_unconditionally(&data->ctx);
15281514

1529-
return pm_device_runtime_enable(dev);
1515+
return pm_device_driver_init(dev, spi_stm32_pm_action);
15301516
}
15311517

15321518
#ifdef CONFIG_SPI_STM32_INTERRUPT

0 commit comments

Comments
 (0)