10
10
#include <zephyr/drivers/pinctrl.h>
11
11
#include <zephyr/sys/util.h>
12
12
#include <zephyr/irq.h>
13
+ #include <zephyr/pm/device.h>
14
+ #include <zephyr/pm/policy.h>
15
+ #include <zephyr/pm/device_runtime.h>
13
16
#include <soc.h>
14
17
#include "r_sci_b_uart.h"
15
18
#include "r_dtc.h"
@@ -73,8 +76,78 @@ struct uart_ra_sci_b_data {
73
76
uart_callback_t async_user_cb ;
74
77
void * async_user_cb_data ;
75
78
#endif
79
+ #ifdef CONFIG_PM
80
+ bool rx_ongoing ;
81
+ bool tx_ongoing ;
82
+ #endif
76
83
};
77
84
85
+ #if CONFIG_PM
86
+ static inline void uart_ra_sci_b_rx_pm_policy_state_lock_get (const struct device * dev )
87
+ {
88
+ struct uart_ra_sci_b_data * data = dev -> data ;
89
+
90
+ if (!data -> rx_ongoing ) {
91
+ data -> rx_ongoing = true;
92
+ #if CONFIG_PM_NEED_ALL_DEVICES_IDLE
93
+ pm_device_busy_set (dev );
94
+ #else
95
+ pm_policy_state_lock_get (PM_STATE_RUNTIME_IDLE , PM_ALL_SUBSTATES );
96
+ pm_policy_state_lock_get (PM_STATE_STANDBY , PM_ALL_SUBSTATES );
97
+ #endif
98
+ }
99
+ }
100
+
101
+ static inline void uart_ra_sci_b_rx_pm_policy_state_lock_put (const struct device * dev )
102
+ {
103
+ struct uart_ra_sci_b_data * data = dev -> data ;
104
+
105
+ if (data -> rx_ongoing ) {
106
+ data -> rx_ongoing = false;
107
+ #if CONFIG_PM_NEED_ALL_DEVICES_IDLE
108
+ if (!data -> tx_ongoing ) {
109
+ pm_device_busy_clear (dev );
110
+ }
111
+ #else
112
+ pm_policy_state_lock_put (PM_STATE_RUNTIME_IDLE , PM_ALL_SUBSTATES );
113
+ pm_policy_state_lock_put (PM_STATE_STANDBY , PM_ALL_SUBSTATES );
114
+ #endif
115
+ }
116
+ }
117
+
118
+ static inline void uart_ra_sci_b_tx_pm_policy_state_lock_get (const struct device * dev )
119
+ {
120
+ struct uart_ra_sci_b_data * data = dev -> data ;
121
+
122
+ if (!data -> tx_ongoing ) {
123
+ data -> tx_ongoing = true;
124
+ #if CONFIG_PM_NEED_ALL_DEVICES_IDLE
125
+ pm_device_busy_set (dev );
126
+ #else
127
+ pm_policy_state_lock_get (PM_STATE_RUNTIME_IDLE , PM_ALL_SUBSTATES );
128
+ pm_policy_state_lock_get (PM_STATE_STANDBY , PM_ALL_SUBSTATES );
129
+ #endif
130
+ }
131
+ }
132
+
133
+ static inline void uart_ra_sci_b_tx_pm_policy_state_lock_put (const struct device * dev )
134
+ {
135
+ struct uart_ra_sci_b_data * data = dev -> data ;
136
+
137
+ if (data -> tx_ongoing ) {
138
+ data -> tx_ongoing = false;
139
+ #if CONFIG_PM_NEED_ALL_DEVICES_IDLE
140
+ if (!data -> rx_ongoing ) {
141
+ pm_device_busy_clear (dev );
142
+ }
143
+ #else
144
+ pm_policy_state_lock_put (PM_STATE_RUNTIME_IDLE , PM_ALL_SUBSTATES );
145
+ pm_policy_state_lock_put (PM_STATE_STANDBY , PM_ALL_SUBSTATES );
146
+ #endif
147
+ }
148
+ }
149
+ #endif
150
+
78
151
static int uart_ra_sci_b_poll_in (const struct device * dev , unsigned char * c )
79
152
{
80
153
const struct uart_ra_sci_b_config * cfg = dev -> config ;
@@ -100,10 +173,21 @@ static void uart_ra_sci_b_poll_out(const struct device *dev, unsigned char c)
100
173
{
101
174
const struct uart_ra_sci_b_config * cfg = dev -> config ;
102
175
176
+ #if CONFIG_PM
177
+ uart_ra_sci_b_tx_pm_policy_state_lock_get (dev );
178
+ #endif
179
+
103
180
while (cfg -> regs -> CSR_b .TEND == 0U ) {
104
181
}
105
182
106
183
cfg -> regs -> TDR_BY = c ;
184
+
185
+ while (cfg -> regs -> CSR_b .TEND == 0U ) {
186
+ }
187
+
188
+ #if CONFIG_PM
189
+ uart_ra_sci_b_tx_pm_policy_state_lock_put (dev );
190
+ #endif
107
191
}
108
192
109
193
static int uart_ra_sci_b_err_check (const struct device * dev )
@@ -311,6 +395,10 @@ static void uart_ra_sci_b_irq_tx_enable(const struct device *dev)
311
395
{
312
396
const struct uart_ra_sci_b_config * cfg = dev -> config ;
313
397
398
+ #if CONFIG_PM
399
+ uart_ra_sci_b_tx_pm_policy_state_lock_get (dev );
400
+ #endif
401
+
314
402
cfg -> regs -> CCR0 |= (BIT (R_SCI_B0_CCR0_TIE_Pos ) | BIT (R_SCI_B0_CCR0_TEIE_Pos ));
315
403
}
316
404
@@ -319,6 +407,10 @@ static void uart_ra_sci_b_irq_tx_disable(const struct device *dev)
319
407
const struct uart_ra_sci_b_config * cfg = dev -> config ;
320
408
321
409
cfg -> regs -> CCR0 &= ~(BIT (R_SCI_B0_CCR0_TIE_Pos ) | BIT (R_SCI_B0_CCR0_TEIE_Pos ));
410
+
411
+ #if CONFIG_PM
412
+ uart_ra_sci_b_tx_pm_policy_state_lock_put (dev );
413
+ #endif
322
414
}
323
415
324
416
static int uart_ra_sci_b_irq_tx_ready (const struct device * dev )
@@ -342,6 +434,10 @@ static void uart_ra_sci_b_irq_rx_enable(const struct device *dev)
342
434
{
343
435
const struct uart_ra_sci_b_config * cfg = dev -> config ;
344
436
437
+ #if CONFIG_PM
438
+ uart_ra_sci_b_rx_pm_policy_state_lock_get (dev );
439
+ #endif
440
+
345
441
cfg -> regs -> CCR0_b .RIE = 1U ;
346
442
}
347
443
@@ -350,6 +446,10 @@ static void uart_ra_sci_b_irq_rx_disable(const struct device *dev)
350
446
const struct uart_ra_sci_b_config * cfg = dev -> config ;
351
447
352
448
cfg -> regs -> CCR0_b .RIE = 0U ;
449
+
450
+ #if CONFIG_PM
451
+ uart_ra_sci_b_rx_pm_policy_state_lock_put (dev );
452
+ #endif
353
453
}
354
454
355
455
static int uart_ra_sci_b_irq_rx_ready (const struct device * dev )
@@ -637,6 +737,10 @@ static int uart_ra_sci_b_async_tx(const struct device *dev, const uint8_t *buf,
637
737
data -> tx_buffer = (uint8_t * )buf ;
638
738
data -> tx_buffer_cap = len ;
639
739
740
+ #if CONFIG_PM
741
+ uart_ra_sci_b_tx_pm_policy_state_lock_get (dev );
742
+ #endif
743
+
640
744
uart_ra_sci_b_async_timer_start (& data -> tx_timeout_work , timeout );
641
745
642
746
unlock :
@@ -687,6 +791,10 @@ static int uart_ra_sci_b_async_tx_abort(const struct device *dev)
687
791
688
792
async_tx_abort (dev );
689
793
794
+ #if CONFIG_PM
795
+ uart_ra_sci_b_tx_pm_policy_state_lock_put (dev );
796
+ #endif
797
+
690
798
return 0 ;
691
799
}
692
800
@@ -720,6 +828,10 @@ static int uart_ra_sci_b_async_rx_enable(const struct device *dev, uint8_t *buf,
720
828
goto unlock ;
721
829
}
722
830
831
+ #if CONFIG_PM
832
+ uart_ra_sci_b_rx_pm_policy_state_lock_get (dev );
833
+ #endif
834
+
723
835
data -> rx_timeout = timeout ;
724
836
data -> rx_buffer = buf ;
725
837
data -> rx_buffer_cap = len ;
@@ -772,6 +884,10 @@ static int uart_ra_sci_b_async_rx_disable(const struct device *dev)
772
884
cfg -> regs -> CFCLR_b .RDRFC = 1U ;
773
885
774
886
unlock :
887
+ #if CONFIG_PM
888
+ uart_ra_sci_b_rx_pm_policy_state_lock_put (dev );
889
+ #endif
890
+
775
891
irq_unlock (key );
776
892
return err ;
777
893
}
@@ -833,6 +949,40 @@ static void uart_ra_sci_b_callback_adapter(struct st_uart_callback_arg *fsp_args
833
949
834
950
#endif /* CONFIG_UART_ASYNC_API */
835
951
952
+ #ifdef CONFIG_PM_DEVICE
953
+ static int uart_ra_sci_b_pm_action (const struct device * dev , enum pm_device_action action )
954
+ {
955
+ struct uart_ra_sci_b_data * data = dev -> data ;
956
+ fsp_err_t fsp_err ;
957
+
958
+ switch (action ) {
959
+ case PM_DEVICE_ACTION_SUSPEND :
960
+ /* Deinitialize the device */
961
+ fsp_err = R_SCI_B_UART_Close (& data -> sci );
962
+ __ASSERT (fsp_err == 0 , "sci_uart: initialization: close failed" );
963
+ break ;
964
+
965
+ case PM_DEVICE_ACTION_RESUME :
966
+ /* Reinitialize the device */
967
+ int ret = uart_ra_sci_b_apply_config (& data -> uart_config , & data -> fsp_config ,
968
+ & data -> fsp_config_extend ,
969
+ & data -> fsp_baud_setting );
970
+ if (ret < 0 ) {
971
+ return ret ;
972
+ }
973
+
974
+ fsp_err = R_SCI_B_UART_Open (& data -> sci , & data -> fsp_config );
975
+ __ASSERT (fsp_err == 0 , "sci_uart: initialization: open failed" );
976
+ break ;
977
+
978
+ default :
979
+ return - ENOTSUP ;
980
+ }
981
+
982
+ return 0 ;
983
+ }
984
+ #endif /* CONFIG_PM_DEVICE */
985
+
836
986
static DEVICE_API (uart , uart_ra_sci_b_driver_api ) = {
837
987
.poll_in = uart_ra_sci_b_poll_in ,
838
988
.poll_out = uart_ra_sci_b_poll_out ,
@@ -971,6 +1121,9 @@ static void uart_ra_sci_b_tei_isr(const struct device *dev)
971
1121
#if defined(CONFIG_UART_ASYNC_API )
972
1122
k_work_cancel_delayable (& data -> tx_timeout_work );
973
1123
sci_b_uart_tei_isr ();
1124
+ #if CONFIG_PM
1125
+ uart_ra_sci_b_tx_pm_policy_state_lock_put (dev );
1126
+ #endif
974
1127
#else
975
1128
R_ICU -> IELSR_b [data -> fsp_config .tei_irq ].IR = 0U ;
976
1129
#endif
@@ -1152,7 +1305,8 @@ static void uart_ra_sci_b_eri_isr(const struct device *dev)
1152
1305
return 0; \
1153
1306
} \
1154
1307
\
1155
- DEVICE_DT_INST_DEFINE(index, uart_ra_sci_b_init_##index, NULL, \
1308
+ PM_DEVICE_DT_INST_DEFINE(index, uart_ra_sci_b_pm_action); \
1309
+ DEVICE_DT_INST_DEFINE(index, uart_ra_sci_b_init_##index, PM_DEVICE_DT_INST_GET(index), \
1156
1310
&uart_ra_sci_b_data_##index, &uart_ra_sci_b_config_##index, \
1157
1311
PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, \
1158
1312
&uart_ra_sci_b_driver_api);
0 commit comments