1
1
/*
2
- * Copyright (c) 2024 Renesas Electronics Corporation
2
+ * Copyright (c) 2024-2025 Renesas Electronics Corporation
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
@@ -25,6 +25,13 @@ LOG_MODULE_REGISTER(pwm_renesas_rz_gpt, CONFIG_PWM_LOG_LEVEL);
25
25
#define CAPTURE_BOTH_MODE_FIRST_EVENT_IS_CAPTURE_PULSE 1
26
26
#define CAPTURE_BOTH_MODE_SECOND_EVENT_IS_CAPTURE_PERIOD 2
27
27
28
+ #define RZ_GPT_GTIOR_OAE_Msk (0x100UL)
29
+ #define RZ_GPT_GTIOR_OADFLT_Pos (6UL)
30
+ #define RZ_GPT_GTIOR_GTIOA_Pos (0UL)
31
+ #define RZ_GPT_GTIOR_GTIOB_Pos (16UL)
32
+ #define RZ_GPT_GTIOR_NFAEN_Pos (13UL)
33
+ #define RZ_GPT_GTIOR_NFBEN_Pos (29UL)
34
+
28
35
struct pwm_rz_gpt_capture_data {
29
36
pwm_capture_callback_handler_t callback ;
30
37
void * user_data ;
@@ -53,8 +60,8 @@ struct pwm_rz_gpt_config {
53
60
54
61
static uint32_t pwm_rz_gpt_gtior_calculate (gpt_pin_level_t const stop_level )
55
62
{
56
- /* The stop level is used as both the initial level and the stop level. */
57
- uint32_t gtior = R_GPT0_GTIOR_OAE_Msk | ((uint32_t )stop_level << R_GPT0_GTIOR_OADFLT_Pos ) |
63
+ /* The stop level is used as both the initial level and the stop level */
64
+ uint32_t gtior = RZ_GPT_GTIOR_OAE_Msk | ((uint32_t )stop_level << RZ_GPT_GTIOR_OADFLT_Pos ) |
58
65
((uint32_t )stop_level << GPT_PRV_GTIOR_INITIAL_LEVEL_BIT );
59
66
60
67
uint32_t gtion = GPT_PRV_GTIO_LOW_COMPARE_MATCH_HIGH_CYCLE_END ;
@@ -69,37 +76,28 @@ static int pwm_rz_gpt_apply_gtior_config(gpt_instance_ctrl_t *const p_ctrl,
69
76
timer_cfg_t const * const p_cfg )
70
77
{
71
78
gpt_extended_cfg_t * p_extend = (gpt_extended_cfg_t * )p_cfg -> p_extend ;
72
- uint32_t gtior = p_extend -> gtior_setting . gtior ;
79
+ uint32_t gtior = 0 ;
73
80
74
81
#if GPT_CFG_OUTPUT_SUPPORT_ENABLE
75
- /* Check if custom GTIOR settings are provided. */
76
- if (p_extend -> gtior_setting .gtior == 0 ) {
77
- /* If custom GTIOR settings are not provided, calculate GTIOR. */
78
- if (p_extend -> gtioca .output_enabled ) {
79
- uint32_t gtioca_gtior =
80
- pwm_rz_gpt_gtior_calculate (p_extend -> gtioca .stop_level );
81
- gtior |= gtioca_gtior << R_GPT0_GTIOR_GTIOA_Pos ;
82
- }
82
+ /* Calculate GTIOR */
83
+ if (p_extend -> gtioca .output_enabled ) {
84
+ uint32_t gtioca_gtior = pwm_rz_gpt_gtior_calculate (p_extend -> gtioca .stop_level );
83
85
84
- if (p_extend -> gtiocb .output_enabled ) {
85
- uint32_t gtiocb_gtior =
86
- pwm_rz_gpt_gtior_calculate (p_extend -> gtiocb .stop_level );
87
- gtior |= gtiocb_gtior << R_GPT0_GTIOR_GTIOB_Pos ;
88
- }
86
+ gtior |= gtioca_gtior << RZ_GPT_GTIOR_GTIOA_Pos ;
89
87
}
90
- #endif
91
88
92
- /* Check if custom GTIOR settings are provided. */
93
- if (p_extend -> gtior_setting .gtior == 0 ) {
94
- /*
95
- * If custom GTIOR settings are not provided, configure the noise filter for
96
- * the GTIOC pins.
97
- */
98
- gtior |= (uint32_t )(p_extend -> capture_filter_gtioca << R_GPT0_GTIOR_NFAEN_Pos );
99
- gtior |= (uint32_t )(p_extend -> capture_filter_gtiocb << R_GPT0_GTIOR_NFBEN_Pos );
89
+ if (p_extend -> gtiocb .output_enabled ) {
90
+ uint32_t gtiocb_gtior = pwm_rz_gpt_gtior_calculate (p_extend -> gtiocb .stop_level );
91
+
92
+ gtior |= gtiocb_gtior << RZ_GPT_GTIOR_GTIOB_Pos ;
100
93
}
94
+ #endif
95
+
96
+ /* Configure the noise filter for the GTIOC pins */
97
+ gtior |= (uint32_t )(p_extend -> capture_filter_gtioca << RZ_GPT_GTIOR_NFAEN_Pos );
98
+ gtior |= (uint32_t )(p_extend -> capture_filter_gtiocb << RZ_GPT_GTIOR_NFBEN_Pos );
101
99
102
- /* Set the I/O control register. */
100
+ /* Set the I/O control register */
103
101
p_ctrl -> p_reg -> GTIOR = gtior ;
104
102
105
103
return 0 ;
@@ -115,7 +113,7 @@ static int pwm_rz_gpt_set_cycles(const struct device *dev, uint32_t channel, uin
115
113
fsp_err_t err ;
116
114
uint32_t pin ;
117
115
118
- /* gtioca and gtiocb setting */
116
+ /* GTIOCA and GTIOCB setting */
119
117
if (channel == RZ_PWM_GPT_IO_A ) {
120
118
pin = GPT_IO_PIN_GTIOCA ;
121
119
fsp_cfg_extend -> gtioca .output_enabled = true;
@@ -187,12 +185,7 @@ static int pwm_rz_gpt_get_cycles_per_sec(const struct device *dev, uint32_t chan
187
185
return 0 ;
188
186
};
189
187
190
- extern void gpt_capture_a_isr (void );
191
- extern void gpt_capture_b_isr (void );
192
- extern void gpt_counter_overflow_isr (void );
193
-
194
188
#ifdef CONFIG_PWM_CAPTURE
195
-
196
189
static int pwm_rz_gpt_configure_capture (const struct device * dev , uint32_t channel ,
197
190
pwm_flags_t flags , pwm_capture_callback_handler_t cb ,
198
191
void * user_data )
@@ -546,7 +539,6 @@ static void fsp_callback(timer_callback_args_t *p_args)
546
539
}
547
540
}
548
541
}
549
-
550
542
#endif /* CONFIG_PWM_CAPTURE */
551
543
552
544
static DEVICE_API (pwm , pwm_rz_gpt_driver_api ) = {
@@ -572,10 +564,10 @@ static int pwm_rz_gpt_init(const struct device *dev)
572
564
return err ;
573
565
}
574
566
575
- #if defined( CONFIG_PWM_CAPTURE )
567
+ #ifdef CONFIG_PWM_CAPTURE
576
568
data -> fsp_cfg -> p_callback = fsp_callback ;
577
569
data -> fsp_cfg -> p_context = dev ;
578
- #endif /* defined( CONFIG_PWM_CAPTURE) */
570
+ #endif /* CONFIG_PWM_CAPTURE */
579
571
580
572
err = cfg -> fsp_api -> open (data -> fsp_ctrl , data -> fsp_cfg );
581
573
if (err != FSP_SUCCESS ) {
@@ -591,20 +583,50 @@ static int pwm_rz_gpt_init(const struct device *dev)
591
583
592
584
#define GPT (idx ) DT_INST_PARENT(idx)
593
585
586
+ #ifdef CONFIG_PWM_CAPTURE
587
+ extern void gpt_capture_compare_a_isr (void );
588
+ extern void gpt_capture_compare_b_isr (void );
589
+ extern void gpt_counter_overflow_isr (void );
590
+
591
+ static void pwm_rz_gpt_ccmpa_isr (const struct device * dev )
592
+ {
593
+ ARG_UNUSED (dev );
594
+ gpt_capture_compare_a_isr ();
595
+ }
596
+
597
+ static void pwm_rz_gpt_ccmpb_isr (const struct device * dev )
598
+ {
599
+ ARG_UNUSED (dev );
600
+ gpt_capture_compare_b_isr ();
601
+ }
602
+
603
+ static void pwm_rz_gpt_ovf_isr (const struct device * dev )
604
+ {
605
+ ARG_UNUSED (dev );
606
+ gpt_counter_overflow_isr ();
607
+ }
608
+
609
+ #ifdef CONFIG_CPU_CORTEX_M
610
+ #define GPT_GET_IRQ_FLAGS (idx , irq_name ) 0
611
+ #else /* Cortex-A/R */
612
+ #define GPT_GET_IRQ_FLAGS (idx , irq_name ) DT_IRQ_BY_NAME(GPT(idx), irq_name, flags)
613
+ #endif
614
+
594
615
#define PWM_RZ_IRQ_CONFIG_INIT (inst ) \
595
616
do { \
596
617
IRQ_CONNECT(DT_IRQ_BY_NAME(GPT(inst), ccmpa, irq), \
597
- DT_IRQ_BY_NAME(GPT(inst), ccmpa, priority), gpt_capture_a_isr, NULL, \
598
- 0); \
618
+ DT_IRQ_BY_NAME(GPT(inst), ccmpa, priority), pwm_rz_gpt_ccmpa_isr, \
619
+ DEVICE_DT_INST_GET(inst), GPT_GET_IRQ_FLAGS(inst, ccmpa)); \
599
620
IRQ_CONNECT(DT_IRQ_BY_NAME(GPT(inst), ccmpb, irq), \
600
- DT_IRQ_BY_NAME(GPT(inst), ccmpb, priority), gpt_capture_b_isr, NULL, \
601
- 0); \
621
+ DT_IRQ_BY_NAME(GPT(inst), ccmpb, priority), pwm_rz_gpt_ccmpb_isr, \
622
+ DEVICE_DT_INST_GET(inst), GPT_GET_IRQ_FLAGS(inst, ccmpb)); \
602
623
IRQ_CONNECT(DT_IRQ_BY_NAME(GPT(inst), ovf, irq), \
603
- DT_IRQ_BY_NAME(GPT(inst), ovf, priority), gpt_counter_overflow_isr, \
604
- NULL, 0); \
624
+ DT_IRQ_BY_NAME(GPT(inst), ovf, priority), pwm_rz_gpt_ovf_isr, \
625
+ DEVICE_DT_INST_GET(inst), GPT_GET_IRQ_FLAGS(inst, ovf)); \
605
626
} while (0)
627
+ #endif /* CONFIG_PWM_CAPTURE */
606
628
607
- #define PWM_RZG_INIT (inst ) \
629
+ #define PWM_RZ_INIT (inst ) \
608
630
PINCTRL_DT_INST_DEFINE(inst); \
609
631
static gpt_instance_ctrl_t g_timer##inst##_ctrl; \
610
632
static gpt_extended_cfg_t g_timer##inst##_extend = { \
@@ -632,7 +654,6 @@ static int pwm_rz_gpt_init(const struct device *dev)
632
654
.capture_filter_gtioca = GPT_CAPTURE_FILTER_NONE, \
633
655
.capture_filter_gtiocb = GPT_CAPTURE_FILTER_NONE, \
634
656
.p_pwm_cfg = NULL, \
635
- .gtior_setting.gtior = (0x0U), \
636
657
}; \
637
658
static timer_cfg_t g_timer##inst##_cfg = { \
638
659
.mode = TIMER_MODE_PWM, \
@@ -650,7 +671,8 @@ static int pwm_rz_gpt_init(const struct device *dev)
650
671
.fsp_cfg = &g_timer##inst##_cfg, .fsp_ctrl = &g_timer##inst##_ctrl}; \
651
672
static int pwm_rz_gpt_init_##inst(const struct device *dev) \
652
673
{ \
653
- PWM_RZ_IRQ_CONFIG_INIT(inst); \
674
+ IF_ENABLED(CONFIG_PWM_CAPTURE, \
675
+ (PWM_RZ_IRQ_CONFIG_INIT(inst);)) \
654
676
int err = pwm_rz_gpt_init(dev); \
655
677
if (err != 0) { \
656
678
return err; \
@@ -661,4 +683,4 @@ static int pwm_rz_gpt_init(const struct device *dev)
661
683
&pwm_rz_gpt_config_##inst, POST_KERNEL, CONFIG_PWM_INIT_PRIORITY, \
662
684
&pwm_rz_gpt_driver_api);
663
685
664
- DT_INST_FOREACH_STATUS_OKAY (PWM_RZG_INIT );
686
+ DT_INST_FOREACH_STATUS_OKAY (PWM_RZ_INIT );
0 commit comments