Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Titan-Realtek
Copy link
Contributor

Add PWM driver support for Realtek RTS5912

pwm0: pwm@4000f000 {
compatible = "realtek,rts5912-pwm";
clocks = <&sccon RTS5912_SCCON_PERIPH_GRP0 PERIPH_GRP0_PWM0_CLKPWR>;
clock-names = "pwm";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking comment: you only have one clock specifier, so you don't need to assign a clock-names property. You can use DT_INST_CLOCKS_CELL as a shorthand for DT_INST_CLOCKS_CELL_BY_IDX(inst, 0, cell).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

Comment on lines 21 to 27
struct {
uint32_t: 28;
volatile uint32_t CLKSRC: 1;
volatile uint32_t INVT: 1;
volatile uint32_t RST: 1;
volatile uint32_t EN: 1;
} CTRL_b;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have also defined macros to access these bits below. You should pick one style here - bit fields or macros.

For single bits, I find C macros for the bit names easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

Comment on lines 32 to 39
#define PWM_CTRL_CLKSRC_Pos (28UL)
#define PWM_CTRL_CLKSRC_Msk BIT(28)
#define PWM_CTRL_INVT_Pos (29UL)
#define PWM_CTRL_INVT_Msk BIT(29)
#define PWM_CTRL_RST_Pos (30UL)
#define PWM_CTRL_RST_Msk BIT(30)
#define PWM_CTRL_EN_Pos (31UL)
#define PWM_CTRL_EN_Msk BIT(31)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you keep these macros - delete the _Pos macros as they aren't used. You can also drop the _Msk' suffix for single-bit fields.

Suggested change
#define PWM_CTRL_CLKSRC_Pos (28UL)
#define PWM_CTRL_CLKSRC_Msk BIT(28)
#define PWM_CTRL_INVT_Pos (29UL)
#define PWM_CTRL_INVT_Msk BIT(29)
#define PWM_CTRL_RST_Pos (30UL)
#define PWM_CTRL_RST_Msk BIT(30)
#define PWM_CTRL_EN_Pos (31UL)
#define PWM_CTRL_EN_Msk BIT(31)
#define PWM_CTRL_CLKSR BIT(28)
#define PWM_CTRL_INVT BIT(29)
#define PWM_CTRL_RST BIT(30)
#define PWM_CTRL_EN BIT(31)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

struct rts5912_sccon_subsys sccon;

int rc = 0;
#if defined(CONFIG_PINCTRL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use #ifdef CONFIG_PINCTRL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

return rc;
}
#endif
#if defined(CONFIG_CLOCK_CONTROL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if defined(CONFIG_CLOCK_CONTROL)
#ifdef CONFIG_CLOCK_CONTROL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

Comment on lines 115 to 116
.pwm_clk_grp = DT_INST_CLOCKS_CELL(inst, 0, clk_grp), \
.pwm_clk_idx = DT_INST_CLOCKS_CELL(inst, 0, clk_idx), \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.pwm_clk_grp = DT_INST_CLOCKS_CELL(inst, 0, clk_grp), \
.pwm_clk_idx = DT_INST_CLOCKS_CELL(inst, 0, clk_idx), \
.pwm_clk_grp = DT_INST_CLOCKS_CELL(inst, clk_grp), \
.pwm_clk_idx = DT_INST_CLOCKS_CELL(inst, clk_idx), \

Wrong number of arguments passed to DT_INST_CLOCKS_CELL. Does the rts5912 EVB support PWM? Can you add a commit to the PR to enable PWM on the EVB? That will help catch compile errors like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done.

@Titan-Realtek Titan-Realtek force-pushed the drivers_pwm_rts5912 branch 2 times, most recently from 54ba42a to 5d04790 Compare March 12, 2025 23:55
Copy link
Collaborator

@keith-zephyr keith-zephyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to this to the pwm_api test by adding an entry for your board.

* @brief PWM Controller (PWM)
*/

typedef struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't declare new typedefs. https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs

Use struct pwm or struct pwm_regs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#define PWM_CYCLE_PER_SEC MHZ(50)

struct pwm_rts5912_config {
uint32_t pwm_base;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the struct pwm_regs type directly to avoid casting every time you use this. This also needs to be declared volatile to prevent compiler reorderng.

Suggested change
uint32_t pwm_base;
volatile struct pwm_regs *pwm_regs;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Titan-Realtek Titan-Realtek force-pushed the drivers_pwm_rts5912 branch 2 times, most recently from a63db21 to 8fea7df Compare April 14, 2025 12:04
Copy link
Collaborator

@keith-zephyr keith-zephyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add this to the tests/drivers/build_all/pwm test. You can add a rts5912_evb_defconfig.overlay file to the board directory and enable one of the "pwm" drivers.

Comment on lines 15 to 17
uint32_t DUTY;
uint32_t DIV;
uint32_t CTRL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Titan-Realtek Titan-Realtek force-pushed the drivers_pwm_rts5912 branch 2 times, most recently from d2a3734 to d372d12 Compare April 15, 2025 06:55
};
};

&pwm0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be called rts5912_evb.overlay - drop the the "defconfig" portion of the name.

You also need to add a testcase entry for the RTS5912 so that the rts5912_evb board and PWM driver are built.

You can verify this locally using ./scripts/twister -ivc -T tests/drivers/build_all/pwm/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@Titan-Realtek Titan-Realtek force-pushed the drivers_pwm_rts5912 branch 2 times, most recently from 4fc4d5c to 42cfb15 Compare April 16, 2025 01:03
keith-zephyr
keith-zephyr previously approved these changes Apr 16, 2025
@keith-zephyr
Copy link
Collaborator

@anangl please take a look.

#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>
Copy link
Collaborator

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Add PWM driver support for Realtek RTS5912

Signed-off-by: Titan Chen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: PWM Pulse Width Modulation platform: Realtek EC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants