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
|
|
|
*
|
2018-11-10 14:30:23 +01:00
|
|
|
* Copyright (C) 2011-2012, 2016, 2018 Gregory Nutt. All rights reserved.
|
2012-01-22 17:42:49 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2019-11-09 16:09:33 +01:00
|
|
|
* Author: Matias Nitsche <mnitsche@dc.uba.ar>
|
2011-09-04 21:24:27 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
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
|
|
|
|
2019-11-09 16:09:33 +01:00
|
|
|
if (g_pmglobals.governor->statechanged)
|
|
|
|
{
|
|
|
|
g_pmglobals.governor->statechanged(domain, newstate);
|
2018-08-27 21:26:33 +02:00
|
|
|
}
|
2012-07-27 02:14:38 +02:00
|
|
|
|
|
|
|
/* Restore the interrupt state */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(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 */
|