2011-09-04 21:24:27 +02:00
|
|
|
/****************************************************************************
|
2022-08-01 11:31:49 +02:00
|
|
|
* drivers/power/pm/pm_changestate.c
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
2021-03-31 11:15:31 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
2021-03-31 11:15:31 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
2021-03-31 11:15:31 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-03-27 21:37:28 +02:00
|
|
|
#include <assert.h>
|
2018-11-10 14:30:23 +01:00
|
|
|
#include <stdlib.h>
|
2016-03-27 21:37:28 +02:00
|
|
|
|
2012-01-22 17:42:49 +01:00
|
|
|
#include <nuttx/power/pm.h>
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2015-12-30 01:29:24 +01:00
|
|
|
#include "pm.h"
|
2011-09-04 21:24:27 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-05-16 16:19:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_stats
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Statistic when domain on state change events.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dom - Identifies the target domain for Statistic
|
|
|
|
* curstate - Identifies the current PM state
|
|
|
|
* newstate - Identifies the new PM state
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#ifdef CONFIG_PM_PROCFS
|
|
|
|
static void pm_stats(FAR struct pm_domain_s *dom, int curstate, int newstate)
|
|
|
|
{
|
2024-05-24 08:47:38 +02:00
|
|
|
struct timespec now;
|
2024-05-16 16:19:54 +02:00
|
|
|
struct timespec ts;
|
|
|
|
|
2024-05-24 08:47:38 +02:00
|
|
|
clock_systime_timespec(&now);
|
|
|
|
ts = now;
|
2024-05-16 16:19:54 +02:00
|
|
|
clock_timespec_subtract(&ts, &dom->start, &ts);
|
|
|
|
|
2024-05-24 08:47:38 +02:00
|
|
|
/* Update start */
|
|
|
|
|
|
|
|
dom->start = now;
|
|
|
|
|
2024-05-16 16:19:54 +02:00
|
|
|
if (newstate == PM_RESTORE)
|
|
|
|
{
|
|
|
|
/* Wakeup from WFI */
|
|
|
|
|
|
|
|
clock_timespec_add(&ts, &dom->sleep[curstate], &dom->sleep[curstate]);
|
2024-05-08 17:21:22 +02:00
|
|
|
dom->in_sleep = false;
|
2024-05-16 16:19:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Sleep to WFI */
|
|
|
|
|
|
|
|
clock_timespec_add(&ts, &dom->wake[curstate], &dom->wake[curstate]);
|
2024-05-08 17:21:22 +02:00
|
|
|
dom->in_sleep = true;
|
2024-05-16 16:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_stats_preparefail
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Statistic the domain on drivers prepare failed.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* domain - Identifies the target domain for Statistic
|
|
|
|
* callback - The prepare failed callback
|
|
|
|
* newstate - The target new state to prepare
|
|
|
|
* ret - The driver prepare failed returned value
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pm_stats_preparefail(int domain,
|
|
|
|
FAR struct pm_callback_s *callback,
|
|
|
|
int newstate, int ret)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
2024-05-31 11:18:53 +02:00
|
|
|
FAR struct pm_preparefail_s *pf = &callback->preparefail;
|
2024-05-16 16:19:54 +02:00
|
|
|
|
|
|
|
if (pf->state != PM_RESTORE)
|
|
|
|
{
|
|
|
|
clock_systime_timespec(&ts);
|
|
|
|
clock_timespec_subtract(&ts, &pf->start, &ts);
|
|
|
|
clock_timespec_add(&ts, &pf->duration[pf->state],
|
|
|
|
&pf->duration[pf->state]);
|
|
|
|
pf->state = PM_RESTORE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
clock_systime_timespec(&pf->start);
|
|
|
|
pf->state = newstate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# define pm_stats(dom, curstate, newstate)
|
|
|
|
# define pm_stats_preparefail(domain, callback, newstate, ret)
|
|
|
|
#endif
|
|
|
|
|
2011-09-04 21:24:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_prepall
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Prepare every driver for the state change.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-09 16:09:33 +01:00
|
|
|
* domain - Identifies the domain of the new PM state
|
2011-09-05 00:16:10 +02:00
|
|
|
* newstate - Identifies the new PM state
|
2024-05-16 16:19:54 +02:00
|
|
|
* restore - Indicate currently in revert the preceding prepare stage.
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) means that the callback function for all registered drivers
|
|
|
|
* returned OK (meaning that they accept the state change). Non-zero
|
|
|
|
* means that one of the drivers refused the state change. In this case,
|
|
|
|
* the system will revert to the preceding state.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-05-16 16:19:54 +02:00
|
|
|
static int pm_prepall(int domain, enum pm_state_e newstate, bool restore)
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2024-05-31 11:18:53 +02:00
|
|
|
FAR struct pm_domain_s *pdom;
|
|
|
|
FAR struct pm_callback_s *cb;
|
2018-08-27 21:22:11 +02:00
|
|
|
FAR dq_entry_t *entry;
|
2011-09-04 21:24:27 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
pdom = &g_pmdomains[domain];
|
|
|
|
if (newstate <= pdom->state)
|
2018-08-27 21:22:11 +02:00
|
|
|
{
|
|
|
|
/* Visit each registered callback structure in normal order. */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
for (entry = dq_peek(&pdom->registry);
|
2018-08-27 21:22:11 +02:00
|
|
|
entry && ret == OK;
|
|
|
|
entry = dq_next(entry))
|
|
|
|
{
|
|
|
|
/* Is the prepare callback supported? */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
cb = (FAR struct pm_callback_s *)entry;
|
2018-08-27 21:22:11 +02:00
|
|
|
if (cb->prepare)
|
|
|
|
{
|
|
|
|
/* Yes.. prepare the driver */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-08-27 21:22:11 +02:00
|
|
|
ret = cb->prepare(cb, domain, newstate);
|
2024-05-16 16:19:54 +02:00
|
|
|
if (!restore)
|
|
|
|
{
|
|
|
|
pm_stats_preparefail(domain, cb, newstate, ret);
|
|
|
|
}
|
2018-08-27 21:22:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2018-08-27 21:22:11 +02:00
|
|
|
/* Visit each registered callback structure in reverse order. */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
for (entry = dq_tail(&pdom->registry);
|
2018-08-27 21:22:11 +02:00
|
|
|
entry && ret == OK;
|
|
|
|
entry = dq_prev(entry))
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2018-08-27 21:22:11 +02:00
|
|
|
/* Is the prepare callback supported? */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
cb = (FAR struct pm_callback_s *)entry;
|
2018-08-27 21:22:11 +02:00
|
|
|
if (cb->prepare)
|
|
|
|
{
|
|
|
|
/* Yes.. prepare the driver */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-08-27 21:22:11 +02:00
|
|
|
ret = cb->prepare(cb, domain, newstate);
|
2024-05-16 16:19:54 +02:00
|
|
|
if (!restore)
|
|
|
|
{
|
|
|
|
pm_stats_preparefail(domain, cb, newstate, ret);
|
|
|
|
}
|
2018-08-27 21:22:11 +02:00
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_changeall
|
|
|
|
*
|
|
|
|
* Description:
|
2016-03-27 19:19:39 +02:00
|
|
|
* domain - Identifies the domain of the new PM state
|
2011-09-04 21:24:27 +02:00
|
|
|
* Inform all drivers of the state change.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-09 16:09:33 +01:00
|
|
|
* domain - Identifies the domain of the new PM state
|
2011-09-05 00:16:10 +02:00
|
|
|
* newstate - Identifies the new PM state
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-27 19:19:39 +02:00
|
|
|
static inline void pm_changeall(int domain, enum pm_state_e newstate)
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2024-05-31 11:18:53 +02:00
|
|
|
FAR struct pm_domain_s *pdom;
|
|
|
|
FAR struct pm_callback_s *cb;
|
2018-08-27 21:22:11 +02:00
|
|
|
FAR dq_entry_t *entry;
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
pdom = &g_pmdomains[domain];
|
|
|
|
if (newstate <= pdom->state)
|
2018-08-27 21:22:11 +02:00
|
|
|
{
|
|
|
|
/* Visit each registered callback structure in normal order. */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
for (entry = dq_peek(&pdom->registry);
|
2019-11-09 16:09:33 +01:00
|
|
|
entry; entry = dq_next(entry))
|
2018-08-27 21:22:11 +02:00
|
|
|
{
|
|
|
|
/* Is the notification callback supported? */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
cb = (FAR struct pm_callback_s *)entry;
|
2018-08-27 21:22:11 +02:00
|
|
|
if (cb->notify)
|
|
|
|
{
|
|
|
|
/* Yes.. notify the driver */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-08-27 21:22:11 +02:00
|
|
|
cb->notify(cb, domain, newstate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2018-08-27 21:22:11 +02:00
|
|
|
/* Visit each registered callback structure in reverse order. */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
for (entry = dq_tail(&pdom->registry);
|
2019-11-09 16:09:33 +01:00
|
|
|
entry; entry = dq_prev(entry))
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2018-08-27 21:22:11 +02:00
|
|
|
/* Is the notification callback supported? */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
cb = (FAR struct pm_callback_s *)entry;
|
2018-08-27 21:22:11 +02:00
|
|
|
if (cb->notify)
|
|
|
|
{
|
|
|
|
/* Yes.. notify the driver */
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2018-08-27 21:22:11 +02:00
|
|
|
cb->notify(cb, domain, newstate);
|
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_changestate
|
|
|
|
*
|
|
|
|
* Description:
|
2011-09-05 00:16:10 +02:00
|
|
|
* This function is used by platform-specific power management logic. It
|
2011-09-04 21:24:27 +02:00
|
|
|
* will announce the power management power management state change to all
|
|
|
|
* drivers that have registered for power management event callbacks.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-11-09 16:09:33 +01:00
|
|
|
* domain - Identifies the domain of the new PM state
|
2011-09-05 00:16:10 +02:00
|
|
|
* newstate - Identifies the new PM state
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) means that the callback function for all registered drivers
|
|
|
|
* returned OK (meaning that they accept the state change). Non-zero
|
|
|
|
* means that one of the drivers refused the state change. In this case,
|
|
|
|
* the system will revert to the preceding state.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* It is assumed that interrupts are disabled when this function is
|
|
|
|
* called. This function is probably called from the IDLE loop... the
|
|
|
|
* lowest priority task in the system. Changing driver power management
|
|
|
|
* states may result in renewed system activity and, as a result, can
|
|
|
|
* suspend the IDLE thread before it completes the entire state change
|
|
|
|
* unless interrupts are disabled throughout the state change.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-27 19:19:39 +02:00
|
|
|
int pm_changestate(int domain, enum pm_state_e newstate)
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
2022-06-16 16:47:19 +02:00
|
|
|
int ret = OK;
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2019-10-29 01:28:41 +01:00
|
|
|
DEBUGASSERT(domain >= 0 && domain < CONFIG_PM_NDOMAINS);
|
2016-03-27 19:19:39 +02:00
|
|
|
|
2019-10-29 01:28:41 +01:00
|
|
|
/* Disable interrupts throughout this operation... changing driver states
|
2011-09-04 21:24:27 +02:00
|
|
|
* could cause additional driver activity that might interfere with the
|
|
|
|
* state change. When the state change is complete, interrupts will be
|
|
|
|
* re-enabled.
|
|
|
|
*/
|
|
|
|
|
2022-12-30 06:42:31 +01:00
|
|
|
flags = pm_domain_lock(domain);
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2022-06-16 16:47:19 +02:00
|
|
|
if (newstate != PM_RESTORE)
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2022-06-16 16:47:19 +02:00
|
|
|
/* First, prepare the drivers for the state change. In this phase,
|
|
|
|
* drivers may refuse the state state change.
|
2011-09-04 21:24:27 +02:00
|
|
|
*/
|
|
|
|
|
2024-05-16 16:19:54 +02:00
|
|
|
ret = pm_prepall(domain, newstate, false);
|
2022-06-16 16:47:19 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* One or more drivers is not ready for this state change.
|
|
|
|
* Revert to the preceding state.
|
|
|
|
*/
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
newstate = g_pmdomains[domain].state;
|
2024-05-16 16:19:54 +02:00
|
|
|
pm_prepall(domain, newstate, true);
|
2022-06-16 16:47:19 +02:00
|
|
|
}
|
2011-09-04 21:24:27 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 12:16:42 +02:00
|
|
|
/* Statistics */
|
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
pm_stats(&g_pmdomains[domain],
|
|
|
|
g_pmdomains[domain].state, newstate);
|
2022-06-20 12:16:42 +02:00
|
|
|
|
2011-09-04 21:24:27 +02:00
|
|
|
/* All drivers have agreed to the state change (or, one or more have
|
|
|
|
* disagreed and the state has been reverted). Set the new state.
|
|
|
|
*/
|
|
|
|
|
2016-03-27 19:19:39 +02:00
|
|
|
pm_changeall(domain, newstate);
|
2018-08-27 21:28:48 +02:00
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
/* Notify governor of (possible) state change */
|
2018-08-27 21:28:48 +02:00
|
|
|
|
2024-05-31 11:18:53 +02:00
|
|
|
if (g_pmdomains[domain].governor->statechanged)
|
2019-11-09 16:09:33 +01:00
|
|
|
{
|
2024-05-31 11:18:53 +02:00
|
|
|
g_pmdomains[domain].governor->statechanged(domain, newstate);
|
2018-08-27 21:26:33 +02:00
|
|
|
}
|
2012-07-27 02:14:38 +02:00
|
|
|
|
2024-04-12 17:47:19 +02:00
|
|
|
/* Domain state update after statechanged done */
|
|
|
|
|
|
|
|
if (newstate != PM_RESTORE)
|
|
|
|
{
|
2024-05-31 11:18:53 +02:00
|
|
|
g_pmdomains[domain].state = newstate;
|
2024-04-12 17:47:19 +02:00
|
|
|
}
|
|
|
|
|
2012-07-27 02:14:38 +02:00
|
|
|
/* Restore the interrupt state */
|
|
|
|
|
2022-12-30 06:42:31 +01:00
|
|
|
pm_domain_unlock(domain, flags);
|
2011-09-04 21:24:27 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-27 21:18:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pm_querystate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function returns the current power management state.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* domain - The PM domain to check
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The current power management state.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
enum pm_state_e pm_querystate(int domain)
|
|
|
|
{
|
2023-09-13 10:43:03 +02:00
|
|
|
DEBUGASSERT(domain >= 0 && domain < CONFIG_PM_NDOMAINS);
|
2024-05-31 11:18:53 +02:00
|
|
|
return g_pmdomains[domain].state;
|
2018-08-27 21:18:09 +02:00
|
|
|
}
|
|
|
|
|
2011-09-04 21:24:27 +02:00
|
|
|
#endif /* CONFIG_PM */
|