Skip to content

Commit 105af6f

Browse files
MulinChaonashif
authored andcommitted
pm: power: npcx: add console expired mechanism.
This CL adds support for console expired mechanism. It implements the notification to power management module that the module for console is in use. If the interval that module doesn't receive any input message exceeds CONFIG_SOC_POWER_CONSOLE_EXPIRED_TIMEOUT, the power management module is allowed to enter deep sleep mode. This mechanism gives a window in which the users can organize console input. Signed-off-by: Mulin Chao <[email protected]>
1 parent d68cc3d commit 105af6f

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed

drivers/serial/uart_npcx.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct uart_npcx_config {
3434
struct uart_npcx_data {
3535
/* Baud rate */
3636
uint32_t baud_rate;
37+
struct miwu_dev_callback uart_rx_cb;
3738
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
3839
uart_irq_callback_user_data_t user_cb;
3940
void *user_data;
@@ -209,6 +210,12 @@ static void uart_npcx_isr(const struct device *dev)
209210
{
210211
struct uart_npcx_data *data = DRV_DATA(dev);
211212

213+
/* Refresh console expired time if got UART Rx event */
214+
if (IS_ENABLED(CONFIG_UART_CONSOLE_INPUT_EXPIRED) &&
215+
uart_npcx_irq_rx_ready(dev)) {
216+
npcx_power_console_is_in_use_refresh();
217+
}
218+
212219
if (data->user_cb) {
213220
data->user_cb(dev, data->user_data);
214221
}
@@ -285,6 +292,15 @@ static int uart_npcx_err_check(const struct device *dev)
285292
return err;
286293
}
287294

295+
static __unused void uart_npcx_rx_wk_isr(const struct device *dev,
296+
struct npcx_wui *wui)
297+
{
298+
/* Refresh console expired time if got UART Rx wake-up event */
299+
if (IS_ENABLED(CONFIG_UART_CONSOLE_INPUT_EXPIRED)) {
300+
npcx_power_console_is_in_use_refresh();
301+
}
302+
}
303+
288304
/* UART driver registration */
289305
static const struct uart_driver_api uart_npcx_driver_api = {
290306
.poll_in = uart_npcx_poll_in,
@@ -311,7 +327,7 @@ static const struct uart_driver_api uart_npcx_driver_api = {
311327
static int uart_npcx_init(const struct device *dev)
312328
{
313329
const struct uart_npcx_config *const config = DRV_CONFIG(dev);
314-
const struct uart_npcx_data *const data = DRV_DATA(dev);
330+
struct uart_npcx_data *const data = DRV_DATA(dev);
315331
struct uart_reg *const inst = HAL_INSTANCE(dev);
316332
const struct device *const clk_dev =
317333
device_get_binding(NPCX_CLK_CTRL_NAME);
@@ -366,17 +382,22 @@ static int uart_npcx_init(const struct device *dev)
366382
config->uconf.irq_config_func(dev);
367383
#endif
368384

369-
#if defined(CONFIG_PM)
370-
/*
371-
* Configure the UART wake-up event triggered from a falling edge
372-
* on CR_SIN pin. No need for callback function.
373-
*/
374-
npcx_miwu_interrupt_configure(&config->uart_rx_wui,
375-
NPCX_MIWU_MODE_EDGE, NPCX_MIWU_TRIG_LOW);
385+
if (IS_ENABLED(CONFIG_PM)) {
386+
/* Initialize a miwu device input and its callback function */
387+
npcx_miwu_init_dev_callback(&data->uart_rx_cb,
388+
&config->uart_rx_wui,
389+
uart_npcx_rx_wk_isr, dev);
390+
npcx_miwu_manage_dev_callback(&data->uart_rx_cb, true);
391+
/*
392+
* Configure the UART wake-up event triggered from a falling
393+
* edge on CR_SIN pin. No need for callback function.
394+
*/
395+
npcx_miwu_interrupt_configure(&config->uart_rx_wui,
396+
NPCX_MIWU_MODE_EDGE, NPCX_MIWU_TRIG_LOW);
376397

377-
/* Enable irq of interrupt-input module */
378-
npcx_miwu_irq_enable(&config->uart_rx_wui);
379-
#endif
398+
/* Enable irq of interrupt-input module */
399+
npcx_miwu_irq_enable(&config->uart_rx_wui);
400+
}
380401

381402
/* Configure pin-mux for uart device */
382403
npcx_pinctrl_mux_configure(config->alts_list, config->alts_size, 1);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2021 Nuvoton Technology Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _NUVOTON_NPCX_SOC_POWER_H_
8+
#define _NUVOTON_NPCX_SOC_POWER_H_
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
/**
15+
* @brief Receive whether the module for the console is in use.
16+
*
17+
* @return 1 if console is in use. Otherwise
18+
* @return
19+
* - True if the console is in use.
20+
* - False otherwise, the module for console is reday to enter low power mode.
21+
*/
22+
bool npcx_power_console_is_in_use(void);
23+
24+
/**
25+
* @brief Notify the power module that the module for the console is in use.
26+
*
27+
* Notify the power module that the module for the console is in use. It also
28+
* extends expired time by CONFIG_UART_CONSOLE_INPUT_EXPIRED_TIMEOUT.
29+
*/
30+
void npcx_power_console_is_in_use_refresh(void);
31+
32+
/**
33+
* @brief Set expired time-out directly for the console is in use.
34+
*
35+
* @param timeout Expired time-out for the console is in use.
36+
*/
37+
void npcx_power_set_console_in_use_timeout(int64_t timeout);
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
#endif /* _NUVOTON_NPCX_SOC_POWER_H_ */

soc/arm/nuvoton_npcx/npcx7/power.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ enum {
8282
NPCX_STANDARD_WAKE_UP,
8383
};
8484

85+
#ifdef CONFIG_UART_CONSOLE_INPUT_EXPIRED
86+
static int64_t expired_timeout = CONFIG_UART_CONSOLE_INPUT_EXPIRED_TIMEOUT;
87+
static int64_t console_expired_time = CONFIG_UART_CONSOLE_INPUT_EXPIRED_TIMEOUT;
88+
89+
/* Platform specific power control functions */
90+
bool npcx_power_console_is_in_use(void)
91+
{
92+
return (k_uptime_get() < console_expired_time);
93+
}
94+
95+
void npcx_power_console_is_in_use_refresh(void)
96+
{
97+
console_expired_time = k_uptime_get() + expired_timeout;
98+
}
99+
100+
void npcx_power_set_console_in_use_timeout(int64_t timeout)
101+
{
102+
expired_timeout = timeout;
103+
}
104+
#endif
105+
85106
static void npcx_power_enter_system_sleep(int slp_mode, int wk_mode)
86107
{
87108
/* Disable interrupts */

soc/arm/nuvoton_npcx/npcx7/soc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
#include <soc_dt.h>
2020
#include <soc_clock.h>
2121
#include <soc_pins.h>
22+
#include <soc_power.h>
2223

2324
#endif /* _NUVOTON_NPCX_SOC_H_ */

0 commit comments

Comments
 (0)