|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Realtek, SIBG-SD7 |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT realtek_rts5912_pwm |
| 8 | + |
| 9 | +#include <soc.h> |
| 10 | +#include <errno.h> |
| 11 | +#include <zephyr/device.h> |
| 12 | +#include <zephyr/drivers/pwm.h> |
| 13 | +#include <zephyr/drivers/pinctrl.h> |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | +#include <zephyr/drivers/clock_control.h> |
| 16 | +#include <zephyr/drivers/clock_control/clock_control_rts5912.h> |
| 17 | + |
| 18 | +#include "reg/reg_pwm.h" |
| 19 | + |
| 20 | +LOG_MODULE_REGISTER(pwm, CONFIG_PWM_LOG_LEVEL); |
| 21 | + |
| 22 | +#define PWM_CYCLE_PER_SEC MHZ(50) |
| 23 | + |
| 24 | +struct pwm_rts5912_config { |
| 25 | + volatile struct pwm_regs *pwm_regs; |
| 26 | + uint32_t pwm_clk_grp; |
| 27 | + uint32_t pwm_clk_idx; |
| 28 | + const struct device *clk_dev; |
| 29 | + const struct pinctrl_dev_config *pcfg; |
| 30 | +}; |
| 31 | + |
| 32 | +static int pwm_rts5912_set_cycles(const struct device *dev, uint32_t channel, |
| 33 | + uint32_t period_cycles, uint32_t pulse_cycles, pwm_flags_t flags) |
| 34 | +{ |
| 35 | + const struct pwm_rts5912_config *const pwm_config = dev->config; |
| 36 | + volatile struct pwm_regs *pwm_regs = pwm_config->pwm_regs; |
| 37 | + |
| 38 | + uint32_t pwm_div, pwm_duty; |
| 39 | + |
| 40 | + if (channel > 0) { |
| 41 | + return -EIO; |
| 42 | + } |
| 43 | + |
| 44 | + pwm_div = period_cycles; |
| 45 | + pwm_duty = pulse_cycles; |
| 46 | + |
| 47 | + pwm_regs->div = pwm_div; |
| 48 | + pwm_regs->duty = pwm_duty; |
| 49 | + |
| 50 | + LOG_DBG("period_cycles=%d, pulse_cycles=%d, pwm_div=%d, pwm_duty=%d", period_cycles, |
| 51 | + pulse_cycles, pwm_div, pwm_duty); |
| 52 | + |
| 53 | + if (flags == PWM_POLARITY_INVERTED) { |
| 54 | + pwm_regs->ctrl |= PWM_CTRL_INVT; |
| 55 | + } |
| 56 | + pwm_regs->ctrl |= PWM_CTRL_EN; |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static int pwm_rts5912_get_cycles_per_sec(const struct device *dev, uint32_t channel, |
| 62 | + uint64_t *cycles) |
| 63 | +{ |
| 64 | + ARG_UNUSED(dev); |
| 65 | + |
| 66 | + if (channel > 0) { |
| 67 | + return -EIO; |
| 68 | + } |
| 69 | + |
| 70 | + if (cycles) { |
| 71 | + *cycles = PWM_CYCLE_PER_SEC; |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static DEVICE_API(pwm, pwm_rts5912_driver_api) = { |
| 78 | + .set_cycles = pwm_rts5912_set_cycles, |
| 79 | + .get_cycles_per_sec = pwm_rts5912_get_cycles_per_sec, |
| 80 | +}; |
| 81 | + |
| 82 | +static int pwm_rts5912_init(const struct device *dev) |
| 83 | +{ |
| 84 | + const struct pwm_rts5912_config *const pwm_config = dev->config; |
| 85 | + struct rts5912_sccon_subsys sccon; |
| 86 | + |
| 87 | + int rc = 0; |
| 88 | +#ifdef CONFIG_PINCTRL |
| 89 | + rc = pinctrl_apply_state(pwm_config->pcfg, PINCTRL_STATE_DEFAULT); |
| 90 | + if (rc < 0) { |
| 91 | + LOG_ERR("PWM pinctrl setup failed (%d)", rc); |
| 92 | + return rc; |
| 93 | + } |
| 94 | +#endif |
| 95 | +#ifdef CONFIG_CLOCK_CONTROL |
| 96 | + if (!device_is_ready(pwm_config->clk_dev)) { |
| 97 | + return -ENODEV; |
| 98 | + } |
| 99 | + |
| 100 | + sccon.clk_grp = pwm_config->pwm_clk_grp; |
| 101 | + sccon.clk_idx = pwm_config->pwm_clk_idx; |
| 102 | + rc = clock_control_on(pwm_config->clk_dev, (clock_control_subsys_t)&sccon); |
| 103 | + if (rc != 0) { |
| 104 | + return rc; |
| 105 | + } |
| 106 | +#endif |
| 107 | + return rc; |
| 108 | +} |
| 109 | + |
| 110 | +#define RTS5912_PWM_PINCTRL_DEF(inst) PINCTRL_DT_INST_DEFINE(inst) |
| 111 | + |
| 112 | +#define RTS5912_PWM_CONFIG(inst) \ |
| 113 | + static struct pwm_rts5912_config pwm_rts5912_config_##inst = { \ |
| 114 | + .pwm_regs = (struct pwm_regs *)DT_INST_REG_ADDR(inst), \ |
| 115 | + .pwm_clk_grp = DT_INST_CLOCKS_CELL(inst, clk_grp), \ |
| 116 | + .pwm_clk_idx = DT_INST_CLOCKS_CELL(inst, clk_idx), \ |
| 117 | + .clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \ |
| 118 | + .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ |
| 119 | + }; |
| 120 | + |
| 121 | +#define RTS5912_PWM_DEVICE_INIT(index) \ |
| 122 | + RTS5912_PWM_PINCTRL_DEF(index); \ |
| 123 | + RTS5912_PWM_CONFIG(index); \ |
| 124 | + DEVICE_DT_INST_DEFINE(index, &pwm_rts5912_init, NULL, NULL, &pwm_rts5912_config_##index, \ |
| 125 | + POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 126 | + &pwm_rts5912_driver_api); |
| 127 | + |
| 128 | +DT_INST_FOREACH_STATUS_OKAY(RTS5912_PWM_DEVICE_INIT) |
0 commit comments