-
Notifications
You must be signed in to change notification settings - Fork 7.3k
drivers: pwm: rts5912: port pwm driver on Zephyr #86275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Titan-Realtek
wants to merge
1
commit into
zephyrproject-rtos:main
Choose a base branch
from
Titan-Realtek:drivers_pwm_rts5912
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2025, Realtek, SIBG-SD7 | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
config PWM_REALTEK_RTS5912 | ||
bool "Realtek RTS5912 PWM Driver" | ||
default y | ||
depends on DT_HAS_REALTEK_RTS5912_PWM_ENABLED | ||
help | ||
Enable PWM driver for Realtek RTS5912 EC. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (c) 2025 Realtek, SIBG-SD7 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#define DT_DRV_COMPAT realtek_rts5912_pwm | ||
|
||
#include <zephyr/drivers/pwm.h> | ||
#include <zephyr/drivers/pinctrl.h> | ||
#include <zephyr/logging/log.h> | ||
#include <zephyr/drivers/clock_control.h> | ||
#include <zephyr/drivers/clock_control/clock_control_rts5912.h> | ||
|
||
#include "reg/reg_pwm.h" | ||
|
||
LOG_MODULE_REGISTER(pwm, CONFIG_PWM_LOG_LEVEL); | ||
|
||
#define PWM_CYCLE_PER_SEC MHZ(50) | ||
|
||
struct pwm_rts5912_config { | ||
volatile struct pwm_regs *pwm_regs; | ||
uint32_t pwm_clk_grp; | ||
uint32_t pwm_clk_idx; | ||
const struct device *clk_dev; | ||
const struct pinctrl_dev_config *pcfg; | ||
}; | ||
|
||
static int pwm_rts5912_set_cycles(const struct device *dev, uint32_t channel, | ||
uint32_t period_cycles, uint32_t pulse_cycles, pwm_flags_t flags) | ||
{ | ||
const struct pwm_rts5912_config *const pwm_config = dev->config; | ||
volatile struct pwm_regs *pwm_regs = pwm_config->pwm_regs; | ||
|
||
uint32_t pwm_div, pwm_duty; | ||
|
||
if (channel > 0) { | ||
return -EIO; | ||
} | ||
|
||
pwm_div = period_cycles; | ||
pwm_duty = pulse_cycles; | ||
|
||
pwm_regs->div = pwm_div; | ||
pwm_regs->duty = pwm_duty; | ||
|
||
LOG_DBG("period_cycles=%d, pulse_cycles=%d, pwm_div=%d, pwm_duty=%d", period_cycles, | ||
pulse_cycles, pwm_div, pwm_duty); | ||
|
||
if (flags == PWM_POLARITY_INVERTED) { | ||
pwm_regs->ctrl |= PWM_CTRL_INVT; | ||
} | ||
pwm_regs->ctrl |= PWM_CTRL_EN; | ||
|
||
return 0; | ||
} | ||
|
||
static int pwm_rts5912_get_cycles_per_sec(const struct device *dev, uint32_t channel, | ||
uint64_t *cycles) | ||
{ | ||
ARG_UNUSED(dev); | ||
|
||
if (channel > 0) { | ||
return -EIO; | ||
} | ||
|
||
if (cycles) { | ||
*cycles = PWM_CYCLE_PER_SEC; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static DEVICE_API(pwm, pwm_rts5912_driver_api) = { | ||
.set_cycles = pwm_rts5912_set_cycles, | ||
.get_cycles_per_sec = pwm_rts5912_get_cycles_per_sec, | ||
}; | ||
|
||
static int pwm_rts5912_init(const struct device *dev) | ||
{ | ||
const struct pwm_rts5912_config *const pwm_config = dev->config; | ||
struct rts5912_sccon_subsys sccon; | ||
|
||
int rc = 0; | ||
#ifdef CONFIG_PINCTRL | ||
rc = pinctrl_apply_state(pwm_config->pcfg, PINCTRL_STATE_DEFAULT); | ||
if (rc < 0) { | ||
LOG_ERR("PWM pinctrl setup failed (%d)", rc); | ||
return rc; | ||
} | ||
#endif | ||
#ifdef CONFIG_CLOCK_CONTROL | ||
if (!device_is_ready(pwm_config->clk_dev)) { | ||
return -ENODEV; | ||
} | ||
|
||
sccon.clk_grp = pwm_config->pwm_clk_grp; | ||
sccon.clk_idx = pwm_config->pwm_clk_idx; | ||
rc = clock_control_on(pwm_config->clk_dev, (clock_control_subsys_t)&sccon); | ||
if (rc != 0) { | ||
return rc; | ||
} | ||
#endif | ||
return rc; | ||
} | ||
|
||
#define RTS5912_PWM_PINCTRL_DEF(inst) PINCTRL_DT_INST_DEFINE(inst) | ||
|
||
#define RTS5912_PWM_CONFIG(inst) \ | ||
static struct pwm_rts5912_config pwm_rts5912_config_##inst = { \ | ||
.pwm_regs = (struct pwm_regs *)DT_INST_REG_ADDR(inst), \ | ||
.pwm_clk_grp = DT_INST_CLOCKS_CELL(inst, clk_grp), \ | ||
.pwm_clk_idx = DT_INST_CLOCKS_CELL(inst, clk_idx), \ | ||
.clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \ | ||
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ | ||
}; | ||
|
||
#define RTS5912_PWM_DEVICE_INIT(index) \ | ||
RTS5912_PWM_PINCTRL_DEF(index); \ | ||
RTS5912_PWM_CONFIG(index); \ | ||
DEVICE_DT_INST_DEFINE(index, &pwm_rts5912_init, NULL, NULL, &pwm_rts5912_config_##index, \ | ||
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ | ||
&pwm_rts5912_driver_api); | ||
|
||
DT_INST_FOREACH_STATUS_OKAY(RTS5912_PWM_DEVICE_INIT) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright (c) 2025, Realtek, SIBG-SD7 | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
description: Realtek RTS5912 PWM | ||
|
||
include: [pwm-controller.yaml, base.yaml, pinctrl-device.yaml] | ||
|
||
compatible: "realtek,rts5912-pwm" | ||
|
||
properties: | ||
reg: | ||
required: true | ||
|
||
"#pwm-cells": | ||
const: 3 | ||
|
||
pwm-cells: | ||
- channel | ||
- period | ||
- flags |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2025 Realtek, SIBG-SD7 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_SOC_REALTEK_RTS5912_REG_PWM_H | ||
#define ZEPHYR_SOC_REALTEK_RTS5912_REG_PWM_H | ||
|
||
/* | ||
* @brief PWM Controller (PWM) | ||
*/ | ||
|
||
struct pwm_regs { | ||
uint32_t duty; | ||
uint32_t div; | ||
uint32_t ctrl; | ||
}; | ||
|
||
/* CTRL */ | ||
#define PWM_CTRL_CLKSRC BIT(28) | ||
#define PWM_CTRL_INVT BIT(29) | ||
#define PWM_CTRL_RST BIT(30) | ||
#define PWM_CTRL_EN BIT(31) | ||
|
||
#endif /* ZEPHYR_SOC_REALTEK_RTS5912_REG_PWM_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright (c) 2025 Realtek Corporation. All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/ { | ||
aliases { | ||
pwm-0 = &pwm0; | ||
}; | ||
}; | ||
|
||
&pwm0 { | ||
status = "okay"; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Titan-Realtek ,
please remove unused header.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.