Skip to content

Commit

Permalink
os/board/rtl8730e: Prepare PM for GPIO
Browse files Browse the repository at this point in the history
- Prepare PM for GPIO
  • Loading branch information
edwakuwaku committed May 7, 2024
1 parent fd6b9f2 commit 7c6b0b1
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
153 changes: 153 additions & 0 deletions os/arch/arm/src/amebasmart/amebasmart_gpio_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
#include "gpio_irq_ex_api.h"
#include <tinyara/kmalloc.h>

#ifdef CONFIG_PM
#include <tinyara/pm/pm.h>
#endif

/* Power management definitions */
#if defined(CONFIG_PM) && !defined(CONFIG_RTL8730E_PM_GPIO_ACTIVITY)
#define CONFIG_RTL8730E_PM_GPIO_ACTIVITY 10
#endif

/****************************************************************************
* Private Types
****************************************************************************/
Expand All @@ -86,6 +95,22 @@ struct amebasmart_lowerhalf_s {
gpio_handler_t handler;
};

#ifdef CONFIG_PM
static void amebasmart_gpio_pmnotify(FAR struct pm_callback_s *cb, int domain,
enum pm_state_e pmstate);
static int amebasmart_gpio_pmprepare(FAR struct pm_callback_s *cb, int domain,
enum pm_state_e pmstate);
static struct
{
struct pm_callback_s pm_cb;
} g_gpiopm =
{
.pm_cb.name = "rtl8730e_gpio",
.pm_cb.notify = amebasmart_gpio_pmnotify,
.pm_cb.prepare = amebasmart_gpio_pmprepare,
};
#endif

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -221,6 +246,27 @@ static const struct gpio_ops_s amebasmart_gpio_ops = {
.enable = amebasmart_gpio_enable,
};

#ifdef CONFIG_PM
static uint32_t rtk_gpio_suspend(uint32_t expected_idle_time, void *param)
{
(void)expected_idle_time;
(void)param;

gpio_toggle_clck(0);
return 1;
}

static uint32_t rtk_gpio_resume(uint32_t expected_idle_time, void *param)
{
(void)expected_idle_time;
(void)param;

gpio_toggle_clck(1);
pm_activity(PM_IDLE_DOMAIN, CONFIG_RTL8730E_PM_GPIO_ACTIVITY);
return 1;
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -262,3 +308,110 @@ FAR struct gpio_lowerhalf_s *amebasmart_gpio_lowerhalf(u32 pinname, u32 pinmode,
lower->ops = &amebasmart_gpio_ops;
return (struct gpio_lowerhalf_s *)lower;
}

/****************************************************************************
* Name: amebasmart_gpio_pmnotify
*
* Description:
* Notify the driver of new power state. This callback is called after
* all drivers have had the opportunity to prepare for the new power state.
*
* Input Parameters:
*
* cb - Returned to the driver. The driver version of the callback
* structure may include additional, driver-specific state data at
* the end of the structure.
*
* pmstate - Identifies the new PM state
*
* Returned Value:
* None - The driver already agreed to transition to the low power
* consumption state when when it returned OK to the prepare() call.
*
*
****************************************************************************/

#ifdef CONFIG_PM
static void amebasmart_gpio_pmnotify(FAR struct pm_callback_s *cb, int domain,
enum pm_state_e pmstate)
{
switch (pmstate)
{
case PM_NORMAL:
pmu_unregister_sleep_callback(PMU_GPIO_DEVICE);
break;
case PM_IDLE:
case PM_STANDBY:
break;
case PM_SLEEP:
pmu_register_sleep_callback(PMU_GPIO_DEVICE, (PSM_HOOK_FUN)rtk_gpio_suspend, NULL, (PSM_HOOK_FUN)rtk_gpio_resume, NULL);
break;
default:
break;
}

return;
}
#endif

/****************************************************************************
* Name: amebasmart_gpio_pmprepare
*
* Description:
* Request the driver to prepare for a new power state. This is a warning
* that the system is about to enter into a new power state. The driver
* should begin whatever operations that may be required to enter power
* state. The driver may abort the state change mode by returning a
* non-zero value from the callback function.
*
* Input Parameters:
*
* cb - Returned to the driver. The driver version of the callback
* structure may include additional, driver-specific state data at
* the end of the structure.
*
* pmstate - Identifies the new PM state
*
* Returned Value:
* Zero - (OK) means the event was successfully processed and that the
* driver is prepared for the PM state change.
*
* Non-zero - means that the driver is not prepared to perform the tasks
* needed achieve this power setting and will cause the state
* change to be aborted. NOTE: The prepare() method will also
* be called when reverting from lower back to higher power
* consumption modes (say because another driver refused a
* lower power state change). Drivers are not permitted to
* return non-zero values when reverting back to higher power
* consumption modes!
*
****************************************************************************/

#ifdef CONFIG_PM
static int amebasmart_gpio_pmprepare(FAR struct pm_callback_s *cb, int domain, enum pm_state_e pmstate)
{
switch (pmstate)
{
case PM_NORMAL:
case PM_IDLE:
case PM_STANDBY:
case PM_SLEEP:
break;
default:
break;
}

return OK;
}
#endif

#ifdef CONFIG_PM
void gpio_pminitialize(void)
{
int ret;
/* Register to receive power management callbacks */
ret = pm_register(&g_gpiopm.pm_cb);
DEBUGASSERT(ret == OK);
UNUSED(ret);
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ void gpio_deinit(gpio_t *obj)
{
GPIO_DeInit(obj->pin);
}

#ifdef CONFIG_PM
void gpio_toggle_clck(bool is_lsapb)
{
(is_lsapb == 1) ? RCC_PeriphClockSource_GPIO(CKSL_GPIO_LS_APB) : RCC_PeriphClockSource_GPIO(CKSL_GPIO_32K);
}
#endif
/**
* @}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef enum {
PMU_BT_DEVICE = 17,
PMU_VAD_DEVICE = 18,
PMU_SPI_DEVICE = 19,
PMU_GPIO_DEVICE = 20,

PMU_MAX = 31
} PMU_DEVICE;
Expand Down
5 changes: 4 additions & 1 deletion os/board/rtl8730e/src/rtl8730e_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void board_gpio_initialize(void)
u32 pinmode;
u32 pinpull;
} pins[] = {

{PA_23, PIN_INPUT, PullNone},
/* NOTE: Do not open pins not for GPIO usage. E.g uart,SPI pins
Loguart pins
*/
Expand Down Expand Up @@ -298,6 +298,9 @@ void board_gpio_initialize(void)
lower = amebasmart_gpio_lowerhalf(pins[i].pinname, pins[i].pinmode, pins[i].pinpull);
gpio_register(pins[i].pinname, lower);
}
#ifdef CONFIG_PM
gpio_pminitialize();
#endif
#endif
}

Expand Down

0 comments on commit 7c6b0b1

Please sign in to comment.