Zephyr Project API 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
device.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_PM_DEVICE_H_
8#define ZEPHYR_INCLUDE_PM_DEVICE_H_
9
10#include <zephyr/device.h>
11#include <zephyr/kernel.h>
12#include <zephyr/sys/atomic.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
25
27
28struct device;
29
31enum pm_device_flag {
33 PM_DEVICE_FLAG_BUSY,
35 PM_DEVICE_FLAG_TURN_ON_FAILED,
37 PM_DEVICE_FLAG_PD_CLAIMED,
42 PM_DEVICE_FLAG_WS_CAPABLE,
44 PM_DEVICE_FLAG_WS_ENABLED,
46 PM_DEVICE_FLAG_RUNTIME_ENABLED,
48 PM_DEVICE_FLAG_PD,
50 PM_DEVICE_FLAG_RUNTIME_AUTO,
52 PM_DEVICE_FLAG_ISR_SAFE,
53};
54
56
65#define PM_DEVICE_ISR_SAFE 1
66
142
173
184typedef int (*pm_device_action_cb_t)(const struct device *dev,
185 enum pm_device_action action);
186
195typedef bool (*pm_device_action_failed_cb_t)(const struct device *dev,
196 int err);
197
211#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
214#endif /* CONFIG_PM_DEVICE_RUNTIME */
215#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
217 const struct device *domain;
218#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
219};
220
228struct pm_device {
231#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
233 const struct device *dev;
235 struct k_sem lock;
236#if defined(CONFIG_PM_DEVICE_RUNTIME_ASYNC) || defined(__DOXYGEN__)
241#endif /* CONFIG_PM_DEVICE_RUNTIME_ASYNC */
242#endif /* CONFIG_PM_DEVICE_RUNTIME */
243};
244
255#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
258#endif
259};
260
261/* Base part must be the first element. */
262BUILD_ASSERT(offsetof(struct pm_device, base) == 0);
263BUILD_ASSERT(offsetof(struct pm_device_isr, base) == 0);
264
266
267#ifdef CONFIG_PM_DEVICE_RUNTIME_ASYNC
268#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
269 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1), \
270 .event = Z_EVENT_INITIALIZER(obj.event),
271#elif CONFIG_PM_DEVICE_RUNTIME
272#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
273 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1),
274#else
275#define Z_PM_DEVICE_RUNTIME_INIT(obj)
276#endif /* CONFIG_PM_DEVICE_RUNTIME */
277
278#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
279#define Z_PM_DEVICE_POWER_DOMAIN_INIT(_node_id) \
280 .domain = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(_node_id, \
281 power_domains)),
282#else
283#define Z_PM_DEVICE_POWER_DOMAIN_INIT(obj)
284#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
285
291#define Z_PM_DEVICE_FLAGS(node_id) \
292 (COND_CODE_1( \
293 DT_NODE_EXISTS(node_id), \
294 ((DT_PROP_OR(node_id, wakeup_source, 0) \
295 << PM_DEVICE_FLAG_WS_CAPABLE) | \
296 (DT_PROP_OR(node_id, zephyr_pm_device_runtime_auto, 0) \
297 << PM_DEVICE_FLAG_RUNTIME_AUTO) | \
298 (DT_NODE_HAS_COMPAT(node_id, power_domain) << \
299 PM_DEVICE_FLAG_PD)), \
300 (0)))
301
313#define Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, _flags) \
314 { \
315 .flags = ATOMIC_INIT(Z_PM_DEVICE_FLAGS(node_id) | (_flags)), \
316 .state = PM_DEVICE_STATE_ACTIVE, \
317 .action_cb = pm_action_cb, \
318 Z_PM_DEVICE_POWER_DOMAIN_INIT(node_id) \
319 }
320
331#define Z_PM_DEVICE_INIT(obj, node_id, pm_action_cb, isr_safe) \
332 { \
333 .base = Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, \
334 isr_safe ? BIT(PM_DEVICE_FLAG_ISR_SAFE) : 0), \
335 COND_CODE_1(isr_safe, (), (Z_PM_DEVICE_RUNTIME_INIT(obj))) \
336 }
337
343#define Z_PM_DEVICE_NAME(dev_id) _CONCAT(__pm_device_, dev_id)
344
345#ifdef CONFIG_PM
357#define Z_PM_DEVICE_DEFINE_SLOT(dev_id) \
358 static STRUCT_SECTION_ITERABLE_ALTERNATE(pm_device_slots, device, \
359 _CONCAT(__pm_slot_, dev_id))
360#else
361#define Z_PM_DEVICE_DEFINE_SLOT(dev_id)
362#endif /* CONFIG_PM */
363
364#ifdef CONFIG_PM_DEVICE
372#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe) \
373 Z_PM_DEVICE_DEFINE_SLOT(dev_id); \
374 static struct COND_CODE_1(isr_safe, (pm_device_isr), (pm_device)) \
375 Z_PM_DEVICE_NAME(dev_id) = \
376 Z_PM_DEVICE_INIT(Z_PM_DEVICE_NAME(dev_id), node_id, \
377 pm_action_cb, isr_safe)
378
384#define Z_PM_DEVICE_GET(dev_id) ((struct pm_device_base *)&Z_PM_DEVICE_NAME(dev_id))
385
386#else
387#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe)
388#define Z_PM_DEVICE_GET(dev_id) NULL
389#endif /* CONFIG_PM_DEVICE */
390
392
404#define PM_DEVICE_DEFINE(dev_id, pm_action_cb, ...) \
405 Z_PM_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, pm_action_cb, \
406 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
407
419#define PM_DEVICE_DT_DEFINE(node_id, pm_action_cb, ...) \
420 Z_PM_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), pm_action_cb, \
421 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
422
434#define PM_DEVICE_DT_INST_DEFINE(idx, pm_action_cb, ...) \
435 Z_PM_DEVICE_DEFINE(DT_DRV_INST(idx), \
436 Z_DEVICE_DT_DEV_ID(DT_DRV_INST(idx)), \
437 pm_action_cb, \
438 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
439
448#define PM_DEVICE_GET(dev_id) \
449 Z_PM_DEVICE_GET(dev_id)
450
459#define PM_DEVICE_DT_GET(node_id) \
460 PM_DEVICE_GET(Z_DEVICE_DT_DEV_ID(node_id))
461
470#define PM_DEVICE_DT_INST_GET(idx) \
471 PM_DEVICE_DT_GET(DT_DRV_INST(idx))
472
479
497int pm_device_action_run(const struct device *dev,
498 enum pm_device_action action);
499
511 enum pm_device_action action,
513
514#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
524int pm_device_state_get(const struct device *dev,
525 enum pm_device_state *state);
526
538static inline void pm_device_init_suspended(const struct device *dev)
539{
540 struct pm_device_base *pm = dev->pm_base;
541
543}
544
558static inline void pm_device_init_off(const struct device *dev)
559{
560 struct pm_device_base *pm = dev->pm_base;
561
563}
564
576void pm_device_busy_set(const struct device *dev);
577
585void pm_device_busy_clear(const struct device *dev);
586
594
603bool pm_device_is_busy(const struct device *dev);
604
618bool pm_device_wakeup_enable(const struct device *dev, bool enable);
619
628bool pm_device_wakeup_is_enabled(const struct device *dev);
629
638bool pm_device_wakeup_is_capable(const struct device *dev);
639
648bool pm_device_on_power_domain(const struct device *dev);
649
663int pm_device_power_domain_add(const struct device *dev,
664 const struct device *domain);
665
679 const struct device *domain);
680
690bool pm_device_is_powered(const struct device *dev);
691
716
736#else
737static inline int pm_device_state_get(const struct device *dev,
739{
740 ARG_UNUSED(dev);
741
743
744 return 0;
745}
746
747static inline void pm_device_init_suspended(const struct device *dev)
748{
749 ARG_UNUSED(dev);
750}
751static inline void pm_device_init_off(const struct device *dev)
752{
753 ARG_UNUSED(dev);
754}
755static inline void pm_device_busy_set(const struct device *dev)
756{
757 ARG_UNUSED(dev);
758}
759static inline void pm_device_busy_clear(const struct device *dev)
760{
761 ARG_UNUSED(dev);
762}
763static inline bool pm_device_is_any_busy(void) { return false; }
764static inline bool pm_device_is_busy(const struct device *dev)
765{
766 ARG_UNUSED(dev);
767 return false;
768}
769static inline bool pm_device_wakeup_enable(const struct device *dev,
770 bool enable)
771{
772 ARG_UNUSED(dev);
773 ARG_UNUSED(enable);
774 return false;
775}
776static inline bool pm_device_wakeup_is_enabled(const struct device *dev)
777{
778 ARG_UNUSED(dev);
779 return false;
780}
781static inline bool pm_device_wakeup_is_capable(const struct device *dev)
782{
783 ARG_UNUSED(dev);
784 return false;
785}
786static inline bool pm_device_on_power_domain(const struct device *dev)
787{
788 ARG_UNUSED(dev);
789 return false;
790}
791
792static inline int pm_device_power_domain_add(const struct device *dev,
793 const struct device *domain)
794{
795 ARG_UNUSED(dev);
796 ARG_UNUSED(domain);
797 return -ENOSYS;
798}
799
800static inline int pm_device_power_domain_remove(const struct device *dev,
801 const struct device *domain)
802{
803 ARG_UNUSED(dev);
804 ARG_UNUSED(domain);
805 return -ENOSYS;
806}
807
808static inline bool pm_device_is_powered(const struct device *dev)
809{
810 ARG_UNUSED(dev);
811 return true;
812}
813
814static inline int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb)
815{
816 int rc;
817
818 /* When power management is not enabled, all drivers should initialise to active state */
820 if ((rc < 0) && (rc != -ENOTSUP)) {
821 return rc;
822 }
823
825 if (rc < 0) {
826 return rc;
827 }
828
829 return 0;
830}
831
832static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
833{
835}
836
837#endif /* CONFIG_PM_DEVICE */
838
840
841#ifdef __cplusplus
842}
843#endif
844
845#endif
long atomic_t
Definition atomic_types.h:15
bool pm_device_wakeup_is_enabled(const struct device *dev)
Check if a device is enabled as a wake up source.
int pm_device_power_domain_remove(const struct device *dev, const struct device *domain)
Remove a device from a power domain.
bool pm_device_on_power_domain(const struct device *dev)
Check if the device is on a switchable power domain.
int pm_device_action_run(const struct device *dev, enum pm_device_action action)
Run a pm action on a device.
static void pm_device_init_suspended(const struct device *dev)
Initialize a device state to PM_DEVICE_STATE_SUSPENDED.
Definition device.h:538
pm_device_state
Device power states.
Definition device.h:68
bool pm_device_wakeup_enable(const struct device *dev, bool enable)
Enable or disable a device as a wake up source.
void pm_device_children_action_run(const struct device *dev, enum pm_device_action action, pm_device_action_failed_cb_t failure_cb)
Run a pm action on all children of a device.
void pm_device_busy_set(const struct device *dev)
Mark a device as busy.
int(* pm_device_action_cb_t)(const struct device *dev, enum pm_device_action action)
Device PM action callback.
Definition device.h:184
void pm_device_busy_clear(const struct device *dev)
Clear a device busy status.
bool pm_device_is_busy(const struct device *dev)
Check if a device is busy.
bool(* pm_device_action_failed_cb_t)(const struct device *dev, int err)
Device PM action failed callback.
Definition device.h:195
bool pm_device_is_powered(const struct device *dev)
Check if the device is currently powered.
int pm_device_power_domain_add(const struct device *dev, const struct device *domain)
Add a device to a power domain.
bool pm_device_wakeup_is_capable(const struct device *dev)
Check if a device is wake up capable.
const char * pm_device_state_str(enum pm_device_state state)
Get name of device PM state.
int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb)
Move a device driver into its initial device power state.
int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
Prepare PM device for device driver deinit.
bool pm_device_is_any_busy(void)
Check if any device is busy.
pm_device_action
Device PM actions.
Definition device.h:144
static void pm_device_init_off(const struct device *dev)
Initialize a device state to PM_DEVICE_STATE_OFF.
Definition device.h:558
int pm_device_state_get(const struct device *dev, enum pm_device_state *state)
Obtain the power state of a device.
@ PM_DEVICE_STATE_SUSPENDED
Device hardware is powered, but the device is not needed by the system.
Definition device.h:99
@ PM_DEVICE_STATE_OFF
Device hardware is not powered.
Definition device.h:140
@ PM_DEVICE_STATE_SUSPENDING
Device hardware is powered, but the device has been scheduled to be suspended, as it is no longer nee...
Definition device.h:116
@ PM_DEVICE_STATE_ACTIVE
Device hardware is powered, and the device is needed by the system.
Definition device.h:75
@ PM_DEVICE_ACTION_TURN_OFF
Turn off.
Definition device.h:163
@ PM_DEVICE_ACTION_SUSPEND
Suspend.
Definition device.h:146
@ PM_DEVICE_ACTION_RESUME
Resume.
Definition device.h:148
@ PM_DEVICE_ACTION_TURN_ON
Turn on.
Definition device.h:171
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define BUILD_ASSERT(EXPR, MSG...)
Definition llvm.h:51
Public kernel APIs.
state
Definition parser_state.h:29
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
struct pm_device_base * pm_base
Definition device.h:545
Event Structure.
Definition kernel.h:2631
Semaphore structure.
Definition kernel.h:3607
Kernel Spin Lock.
Definition spinlock.h:45
A structure used to submit work after a delay.
Definition kernel.h:4531
Device PM info.
Definition device.h:204
uint32_t usage
Device usage count.
Definition device.h:213
pm_device_action_cb_t action_cb
Device PM action callback.
Definition device.h:210
enum pm_device_state state
Device power state.
Definition device.h:208
atomic_t flags
Device PM status flags.
Definition device.h:206
Runtime PM info for device with synchronous PM.
Definition device.h:252
struct k_spinlock lock
Lock to synchronize the synchronous get/put operations.
Definition device.h:257
struct pm_device_base base
Base info.
Definition device.h:254
Runtime PM info for device with generic PM.
Definition device.h:228
struct k_work_delayable work
Work object for asynchronous calls.
Definition device.h:240
struct k_event event
Event var to listen to the sync request events.
Definition device.h:238
struct k_sem lock
Lock to synchronize the get/put operations.
Definition device.h:235
const struct device * dev
Pointer to the device.
Definition device.h:233
struct pm_device_base base
Base info.
Definition device.h:230