2019-02-28 15:37:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zephyr.h>
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <device.h>
|
2019-06-26 22:33:39 +08:00
|
|
|
#include <sys/__assert.h>
|
2019-02-28 15:37:58 +08:00
|
|
|
|
2020-09-02 06:31:40 +08:00
|
|
|
#define LOG_LEVEL CONFIG_PM_LOG_LEVEL /* From power module Kconfig */
|
2019-02-28 15:37:58 +08:00
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_DECLARE(power);
|
|
|
|
|
|
|
|
/* Device PM request type */
|
|
|
|
#define DEVICE_PM_SYNC (0 << 0)
|
|
|
|
#define DEVICE_PM_ASYNC (1 << 0)
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static void device_pm_callback(const struct device *dev,
|
2019-02-28 15:37:58 +08:00
|
|
|
int retval, void *context, void *arg)
|
|
|
|
{
|
|
|
|
__ASSERT(retval == 0, "Device set power state failed");
|
|
|
|
|
|
|
|
/* Set the fsm_state */
|
2020-05-28 00:26:57 +08:00
|
|
|
if (*((uint32_t *)context) == DEVICE_PM_ACTIVE_STATE) {
|
2020-03-09 19:49:07 +08:00
|
|
|
atomic_set(&dev->pm->fsm_state,
|
2020-09-05 01:41:48 +08:00
|
|
|
DEVICE_PM_STATE_ACTIVE);
|
2019-02-28 15:37:58 +08:00
|
|
|
} else {
|
2020-03-09 19:49:07 +08:00
|
|
|
atomic_set(&dev->pm->fsm_state,
|
2020-09-05 01:41:48 +08:00
|
|
|
DEVICE_PM_STATE_SUSPENDED);
|
2019-02-28 15:37:58 +08:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:49:07 +08:00
|
|
|
k_work_submit(&dev->pm->work);
|
2019-02-28 15:37:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pm_work_handler(struct k_work *work)
|
|
|
|
{
|
|
|
|
struct device_pm *pm = CONTAINER_OF(work,
|
|
|
|
struct device_pm, work);
|
2020-05-01 02:33:38 +08:00
|
|
|
const struct device *dev = pm->dev;
|
2019-02-28 15:37:58 +08:00
|
|
|
int ret = 0;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t pm_state;
|
2019-02-28 15:37:58 +08:00
|
|
|
|
2020-03-09 19:49:07 +08:00
|
|
|
switch (atomic_get(&dev->pm->fsm_state)) {
|
2020-09-05 01:41:48 +08:00
|
|
|
case DEVICE_PM_STATE_ACTIVE:
|
2020-03-09 19:49:07 +08:00
|
|
|
if ((atomic_get(&dev->pm->usage) == 0) &&
|
|
|
|
dev->pm->enable) {
|
|
|
|
atomic_set(&dev->pm->fsm_state,
|
2020-09-05 01:41:48 +08:00
|
|
|
DEVICE_PM_STATE_SUSPENDING);
|
2019-02-28 15:37:58 +08:00
|
|
|
ret = device_set_power_state(dev,
|
|
|
|
DEVICE_PM_SUSPEND_STATE,
|
|
|
|
device_pm_callback, NULL);
|
|
|
|
} else {
|
|
|
|
pm_state = DEVICE_PM_ACTIVE_STATE;
|
|
|
|
goto fsm_out;
|
|
|
|
}
|
|
|
|
break;
|
2020-09-05 01:41:48 +08:00
|
|
|
case DEVICE_PM_STATE_SUSPENDED:
|
2020-03-09 19:49:07 +08:00
|
|
|
if ((atomic_get(&dev->pm->usage) > 0) ||
|
|
|
|
!dev->pm->enable) {
|
|
|
|
atomic_set(&dev->pm->fsm_state,
|
2020-09-05 01:41:48 +08:00
|
|
|
DEVICE_PM_STATE_RESUMING);
|
2019-02-28 15:37:58 +08:00
|
|
|
ret = device_set_power_state(dev,
|
|
|
|
DEVICE_PM_ACTIVE_STATE,
|
|
|
|
device_pm_callback, NULL);
|
|
|
|
} else {
|
|
|
|
pm_state = DEVICE_PM_SUSPEND_STATE;
|
|
|
|
goto fsm_out;
|
|
|
|
}
|
|
|
|
break;
|
2020-09-05 01:41:48 +08:00
|
|
|
case DEVICE_PM_STATE_SUSPENDING:
|
|
|
|
case DEVICE_PM_STATE_RESUMING:
|
2019-02-28 15:37:58 +08:00
|
|
|
/* Do nothing: We are waiting for device_pm_callback() */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERR("Invalid FSM state!!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
__ASSERT(ret == 0, "Set Power state error");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
fsm_out:
|
2020-03-09 19:49:07 +08:00
|
|
|
k_poll_signal_raise(&dev->pm->signal, pm_state);
|
2019-02-28 15:37:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int device_pm_request(const struct device *dev,
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t target_state, uint32_t pm_flags)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
|
|
|
int result, signaled = 0;
|
|
|
|
|
|
|
|
__ASSERT((target_state == DEVICE_PM_ACTIVE_STATE) ||
|
|
|
|
(target_state == DEVICE_PM_SUSPEND_STATE),
|
|
|
|
"Invalid device PM state requested");
|
|
|
|
|
|
|
|
if (target_state == DEVICE_PM_ACTIVE_STATE) {
|
2020-03-09 19:49:07 +08:00
|
|
|
if (atomic_inc(&dev->pm->usage) < 0) {
|
2019-02-28 15:37:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-09 19:49:07 +08:00
|
|
|
if (atomic_dec(&dev->pm->usage) > 1) {
|
2019-02-28 15:37:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 19:49:07 +08:00
|
|
|
k_work_submit(&dev->pm->work);
|
2019-02-28 15:37:58 +08:00
|
|
|
|
|
|
|
/* Return in case of Async request */
|
|
|
|
if (pm_flags & DEVICE_PM_ASYNC) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Incase of Sync request wait for completion event */
|
|
|
|
do {
|
2020-03-09 19:49:07 +08:00
|
|
|
(void)k_poll(&dev->pm->event, 1, K_FOREVER);
|
|
|
|
k_poll_signal_check(&dev->pm->signal,
|
2019-02-28 15:37:58 +08:00
|
|
|
&signaled, &result);
|
|
|
|
} while (!signaled);
|
|
|
|
|
2020-03-09 19:49:07 +08:00
|
|
|
dev->pm->event.state = K_POLL_STATE_NOT_READY;
|
|
|
|
k_poll_signal_reset(&dev->pm->signal);
|
2019-02-28 15:37:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
return result == target_state ? 0 : -EIO;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int device_pm_get(const struct device *dev)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
|
|
|
return device_pm_request(dev,
|
|
|
|
DEVICE_PM_ACTIVE_STATE, DEVICE_PM_ASYNC);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int device_pm_get_sync(const struct device *dev)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
|
|
|
return device_pm_request(dev, DEVICE_PM_ACTIVE_STATE, 0);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int device_pm_put(const struct device *dev)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
|
|
|
return device_pm_request(dev,
|
|
|
|
DEVICE_PM_SUSPEND_STATE, DEVICE_PM_ASYNC);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int device_pm_put_sync(const struct device *dev)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
|
|
|
return device_pm_request(dev, DEVICE_PM_SUSPEND_STATE, 0);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
void device_pm_enable(const struct device *dev)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
k_sem_take(&dev->pm->lock, K_FOREVER);
|
|
|
|
dev->pm->enable = true;
|
2019-02-28 15:37:58 +08:00
|
|
|
|
|
|
|
/* During the driver init, device can set the
|
|
|
|
* PM state accordingly. For later cases we need
|
|
|
|
* to check the usage and set the device PM state.
|
|
|
|
*/
|
2020-03-09 19:49:07 +08:00
|
|
|
if (!dev->pm->dev) {
|
|
|
|
dev->pm->dev = dev;
|
|
|
|
atomic_set(&dev->pm->fsm_state,
|
2020-09-05 01:41:48 +08:00
|
|
|
DEVICE_PM_STATE_SUSPENDED);
|
2020-03-09 19:49:07 +08:00
|
|
|
k_work_init(&dev->pm->work, pm_work_handler);
|
2019-02-28 15:37:58 +08:00
|
|
|
} else {
|
2020-03-09 19:49:07 +08:00
|
|
|
k_work_submit(&dev->pm->work);
|
2019-02-28 15:37:58 +08:00
|
|
|
}
|
2020-03-09 19:49:07 +08:00
|
|
|
k_sem_give(&dev->pm->lock);
|
2019-02-28 15:37:58 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
void device_pm_disable(const struct device *dev)
|
2019-02-28 15:37:58 +08:00
|
|
|
{
|
2020-03-09 19:49:07 +08:00
|
|
|
k_sem_take(&dev->pm->lock, K_FOREVER);
|
|
|
|
dev->pm->enable = false;
|
2019-02-28 15:37:58 +08:00
|
|
|
/* Bring up the device before disabling the Idle PM */
|
2020-03-09 19:49:07 +08:00
|
|
|
k_work_submit(&dev->pm->work);
|
|
|
|
k_sem_give(&dev->pm->lock);
|
2019-02-28 15:37:58 +08:00
|
|
|
}
|