Skip to content

Commit 09a4933

Browse files
committed
drivers/spi: stm32: Use alt clock freq if available
Add support for an alternate clock. If available, alternate clock is enabled and used to get the device clock rate. Fixes #41650 Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 0d3e1ae commit 09a4933

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

drivers/spi/spi_ll_stm32.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,18 @@ static int spi_stm32_configure(const struct device *dev,
489489
#endif
490490
}
491491

492-
if (clock_control_get_rate(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
493-
(clock_control_subsys_t) &cfg->pclken, &clock) < 0) {
494-
LOG_ERR("Failed call clock_control_get_rate");
495-
return -EIO;
492+
if (IS_ENABLED(STM32_SPI_OPT_CLOCK_SUPPORT) && (cfg->pclk_len > 1)) {
493+
if (clock_control_get_rate(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
494+
(clock_control_subsys_t) &cfg->pclken[1], &clock) < 0) {
495+
LOG_ERR("Failed call clock_control_get_rate(pclk[1])");
496+
return -EIO;
497+
}
498+
} else {
499+
if (clock_control_get_rate(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
500+
(clock_control_subsys_t) &cfg->pclken[0], &clock) < 0) {
501+
LOG_ERR("Failed call clock_control_get_rate(pclk[0])");
502+
return -EIO;
503+
}
496504
}
497505

498506
for (br = 1 ; br <= ARRAY_SIZE(scaler) ; ++br) {
@@ -861,11 +869,20 @@ static int spi_stm32_init(const struct device *dev)
861869
int err;
862870

863871
if (clock_control_on(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
864-
(clock_control_subsys_t) &cfg->pclken) != 0) {
872+
(clock_control_subsys_t) &cfg->pclken[0]) != 0) {
865873
LOG_ERR("Could not enable SPI clock");
866874
return -EIO;
867875
}
868876

877+
if (IS_ENABLED(STM32_SPI_OPT_CLOCK_SUPPORT) && (cfg->pclk_len > 1)) {
878+
if (clock_control_configure(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
879+
(clock_control_subsys_t) &cfg->pclken[1],
880+
NULL) != 0) {
881+
LOG_ERR("Could not select SPI soutce clock");
882+
return -EIO;
883+
}
884+
}
885+
869886
if (!spi_stm32_is_subghzspi(dev)) {
870887
/* Configure dt provided device signals when available */
871888
err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
@@ -971,17 +988,20 @@ static void spi_stm32_irq_config_func_##id(const struct device *dev) \
971988
#define STM32_SPI_USE_SUBGHZSPI_NSS_CONFIG(id)
972989
#endif
973990

991+
992+
974993
#define STM32_SPI_INIT(id) \
975994
STM32_SPI_IRQ_HANDLER_DECL(id); \
976995
\
977996
PINCTRL_DT_INST_DEFINE(id); \
978997
\
998+
static const struct stm32_pclken pclken_##id[] = \
999+
STM32_DT_INST_CLOCKS(id);\
1000+
\
9791001
static const struct spi_stm32_config spi_stm32_cfg_##id = { \
9801002
.spi = (SPI_TypeDef *) DT_INST_REG_ADDR(id), \
981-
.pclken = { \
982-
.enr = DT_INST_CLOCKS_CELL(id, bits), \
983-
.bus = DT_INST_CLOCKS_CELL(id, bus) \
984-
}, \
1003+
.pclken = pclken_##id, \
1004+
.pclk_len = DT_INST_NUM_CLOCKS(id), \
9851005
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(id), \
9861006
STM32_SPI_IRQ_HANDLER_FUNC(id) \
9871007
STM32_SPI_USE_SUBGHZSPI_NSS_CONFIG(id) \

drivers/spi/spi_ll_stm32.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111

1212
typedef void (*irq_config_func_t)(const struct device *port);
1313

14+
/* This symbol takes the value 1 if one of the device instances */
15+
/* is configured in dts with an optional clock */
16+
#if STM32_DT_INST_DEV_OPT_CLOCK_SUPPORT
17+
#define STM32_SPI_OPT_CLOCK_SUPPORT 1
18+
#else
19+
#define STM32_SPI_OPT_CLOCK_SUPPORT 0
20+
#endif
21+
1422
struct spi_stm32_config {
15-
struct stm32_pclken pclken;
1623
SPI_TypeDef *spi;
1724
const struct pinctrl_dev_config *pcfg;
1825
#ifdef CONFIG_SPI_STM32_INTERRUPT
@@ -21,6 +28,8 @@ struct spi_stm32_config {
2128
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_spi_subghz)
2229
bool use_subghzspi_nss;
2330
#endif
31+
size_t pclk_len;
32+
const struct stm32_pclken *pclken;
2433
};
2534

2635
#ifdef CONFIG_SPI_STM32_DMA

0 commit comments

Comments
 (0)