|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Savoir-faire Linux, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <soc.h> |
| 8 | +#include <stm32_ll_bus.h> |
| 9 | +#include <stm32_ll_rcc.h> |
| 10 | +#include <zephyr/arch/cpu.h> |
| 11 | +#include <zephyr/drivers/clock_control/stm32_clock_control.h> |
| 12 | +#include <zephyr/sys/util.h> |
| 13 | + |
| 14 | +static int stm32_clock_control_on(const struct device *dev, clock_control_subsys_t sub_system) |
| 15 | +{ |
| 16 | + struct stm32_pclken *pclken = (struct stm32_pclken *) sub_system; |
| 17 | + |
| 18 | + ARG_UNUSED(dev); |
| 19 | + |
| 20 | + if (!IN_RANGE(pclken->bus, STM32_CLOCK_PERIPH_MIN, STM32_CLOCK_PERIPH_MAX)) { |
| 21 | + /* Attempt to change a wrong periph clock bit */ |
| 22 | + return -ENOTSUP; |
| 23 | + } |
| 24 | + |
| 25 | + sys_set_bits(DT_REG_ADDR(DT_NODELABEL(rcc)) + pclken->bus, pclken->enr); |
| 26 | + |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +static int stm32_clock_control_off(const struct device *dev, clock_control_subsys_t sub_system) |
| 31 | +{ |
| 32 | + struct stm32_pclken *pclken = (struct stm32_pclken *) sub_system; |
| 33 | + |
| 34 | + ARG_UNUSED(dev); |
| 35 | + |
| 36 | + if (!IN_RANGE(pclken->bus, STM32_CLOCK_PERIPH_MIN, STM32_CLOCK_PERIPH_MAX)) { |
| 37 | + /* Attempt to toggle a wrong periph clock bit */ |
| 38 | + return -ENOTSUP; |
| 39 | + } |
| 40 | + |
| 41 | + sys_clear_bits(DT_REG_ADDR(DT_NODELABEL(rcc)) + pclken->bus, pclken->enr); |
| 42 | + |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +static int stm32_clock_control_get_subsys_rate(const struct device *dev, |
| 47 | + clock_control_subsys_t sub_system, uint32_t *rate) |
| 48 | +{ |
| 49 | + ARG_UNUSED(dev); |
| 50 | + ARG_UNUSED(sub_system); |
| 51 | + ARG_UNUSED(rate); |
| 52 | + return -ENOTSUP; |
| 53 | +} |
| 54 | + |
| 55 | +static DEVICE_API(clock_control, stm32_clock_control_api) = { |
| 56 | + .on = stm32_clock_control_on, |
| 57 | + .off = stm32_clock_control_off, |
| 58 | + .get_rate = stm32_clock_control_get_subsys_rate, |
| 59 | +}; |
| 60 | + |
| 61 | +static int stm32_clock_control_init(const struct device *dev) |
| 62 | +{ |
| 63 | + ARG_UNUSED(dev); |
| 64 | + return 0; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief RCC device, note that priority is intentionally set to 1 so |
| 69 | + * that the device init runs just after SOC init |
| 70 | + */ |
| 71 | +DEVICE_DT_DEFINE(DT_NODELABEL(rcc), stm32_clock_control_init, NULL, NULL, NULL, PRE_KERNEL_1, |
| 72 | + CONFIG_CLOCK_CONTROL_INIT_PRIORITY, &stm32_clock_control_api); |
0 commit comments