2011-09-04 21:24:27 +02:00
|
|
|
/****************************************************************************
|
2012-01-22 17:42:49 +01:00
|
|
|
* drivers/power/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 <queue.h>
|
|
|
|
#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>
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-27 19:19:39 +02:00
|
|
|
static int pm_prepall(int domain, enum pm_state_e newstate)
|
2011-09-04 21:24:27 +02:00
|
|
|
{
|
2018-08-27 21:22:11 +02:00
|
|
|
FAR dq_entry_t *entry;
|
2011-09-04 21:24:27 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
2018-08-27 21:22:11 +02:00
|
|
|
if (newstate <= g_pmglobals.domain[domain].state)
|
|
|
|
{
|
|
|
|
/* Visit each registered callback structure in normal order. */
|
|
|
|
|
|
|
|
for (entry = dq_peek(&g_pmglobals.registry);
|
|
|
|
entry && ret == OK;
|
|
|
|
entry = dq_next(entry))
|
|
|
|
{
|
|
|
|
/* Is the prepare callback supported? */
|
|
|
|
|
|
|
|
FAR struct pm_callback_s *cb = (FAR struct pm_callback_s *)entry;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2018-08-27 21:22:11 +02:00
|
|
|
for (entry = dq_tail(&g_pmglobals.registry);
|
|
|
|
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? */
|
|
|
|
|
|
|
|
FAR struct pm_callback_s *cb = (FAR struct pm_callback_s *)entry;
|
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
{
|
2018-08-27 21:22:11 +02:00
|
|
|
FAR dq_entry_t *entry;
|
2011-09-04 21:24:27 +02:00
|
|
|
|
2019-10-29 01:28:41 +01:00
|
|
|
if (newstate <= g_pmglobals.domain[domain].state)
|
2018-08-27 21:22:11 +02:00
|
|
|
{
|
|
|
|
/* Visit each registered callback structure in normal order. */
|
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
for (entry = dq_peek(&g_pmglobals.registry);
|
|
|
|
entry; entry = dq_next(entry))
|
2018-08-27 21:22:11 +02:00
|
|
|
{
|
|
|
|
/* Is the notification callback supported? */
|
|
|
|
|
|
|
|
FAR struct pm_callback_s *cb = (FAR struct pm_callback_s *)entry;
|
|
|
|
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
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
for (entry = dq_tail(&g_pmglobals.registry);
|
|
|
|
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? */
|
|
|
|
|
|
|
|
FAR struct pm_callback_s *cb = (FAR struct pm_callback_s *)entry;
|
|
|
|
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;
|
|
|
|
int ret;
|
|
|
|
|
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-03-15 13:42:34 +01:00
|
|
|
flags = pm_lock();
|
2011-09-04 21:24:27 +02:00
|
|
|
|
|
|
|
/* First, prepare the drivers for the state change. In this phase,
|
|
|
|
* drivers may refuse the state state change.
|
|
|
|
*/
|
|
|
|
|
2016-03-27 19:19:39 +02:00
|
|
|
ret = pm_prepall(domain, newstate);
|
2011-09-04 21:24:27 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* One or more drivers is not ready for this state change. Revert to
|
|
|
|
* the preceding state.
|
|
|
|
*/
|
|
|
|
|
2016-03-27 21:03:47 +02:00
|
|
|
newstate = g_pmglobals.domain[domain].state;
|
2020-01-02 17:49:34 +01:00
|
|
|
pm_prepall(domain, newstate);
|
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:26:33 +02:00
|
|
|
if (newstate != PM_RESTORE)
|
|
|
|
{
|
|
|
|
g_pmglobals.domain[domain].state = newstate;
|
2019-11-09 16:09:33 +01:00
|
|
|
}
|
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
|
|
|
|
2022-02-14 08:32:40 +01:00
|
|
|
if (g_pmglobals.domain[domain].governor->statechanged)
|
2019-11-09 16:09:33 +01:00
|
|
|
{
|
2022-02-14 08:32:40 +01:00
|
|
|
g_pmglobals.domain[domain].governor->statechanged(domain, newstate);
|
2018-08-27 21:26:33 +02:00
|
|
|
}
|
2012-07-27 02:14:38 +02:00
|
|
|
|
|
|
|
/* Restore the interrupt state */
|
|
|
|
|
2022-03-15 13:42:34 +01:00
|
|
|
pm_unlock(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)
|
|
|
|
{
|
|
|
|
return g_pmglobals.domain[domain].state;
|
|
|
|
}
|
|
|
|
|
2011-09-04 21:24:27 +02:00
|
|
|
#endif /* CONFIG_PM */
|