Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
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
65#define PM_DEVICE_ISR_SAFE 1
66
88
108
119typedef int (*pm_device_action_cb_t)(const struct device *dev,
120 enum pm_device_action action);
121
130typedef bool (*pm_device_action_failed_cb_t)(const struct device *dev,
131 int err);
132
146#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
149#endif /* CONFIG_PM_DEVICE_RUNTIME */
150#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
152 const struct device *domain;
153#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
154};
155
163struct pm_device {
166#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
168 const struct device *dev;
170 struct k_sem lock;
173#if defined(CONFIG_PM_DEVICE_RUNTIME_ASYNC) || defined(__DOXYGEN__)
176#endif /* CONFIG_PM_DEVICE_RUNTIME_ASYNC */
177#endif /* CONFIG_PM_DEVICE_RUNTIME */
178};
179
190#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
193#endif
194};
195
196/* Base part must be the first element. */
197BUILD_ASSERT(offsetof(struct pm_device, base) == 0);
198BUILD_ASSERT(offsetof(struct pm_device_isr, base) == 0);
199
202#ifdef CONFIG_PM_DEVICE_RUNTIME
203#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
204 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1), \
205 .event = Z_EVENT_INITIALIZER(obj.event),
206#else
207#define Z_PM_DEVICE_RUNTIME_INIT(obj)
208#endif /* CONFIG_PM_DEVICE_RUNTIME */
209
210#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
211#define Z_PM_DEVICE_POWER_DOMAIN_INIT(_node_id) \
212 .domain = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(_node_id, \
213 power_domains)),
214#else
215#define Z_PM_DEVICE_POWER_DOMAIN_INIT(obj)
216#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
217
223#define Z_PM_DEVICE_FLAGS(node_id) \
224 (COND_CODE_1( \
225 DT_NODE_EXISTS(node_id), \
226 ((DT_PROP_OR(node_id, wakeup_source, 0) \
227 << PM_DEVICE_FLAG_WS_CAPABLE) | \
228 (DT_PROP_OR(node_id, zephyr_pm_device_runtime_auto, 0) \
229 << PM_DEVICE_FLAG_RUNTIME_AUTO) | \
230 (DT_NODE_HAS_COMPAT(node_id, power_domain) << \
231 PM_DEVICE_FLAG_PD)), \
232 (0)))
233
245#define Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, _flags) \
246 { \
247 .flags = ATOMIC_INIT(Z_PM_DEVICE_FLAGS(node_id) | (_flags)), \
248 .state = PM_DEVICE_STATE_ACTIVE, \
249 .action_cb = pm_action_cb, \
250 Z_PM_DEVICE_POWER_DOMAIN_INIT(node_id) \
251 }
252
263#define Z_PM_DEVICE_INIT(obj, node_id, pm_action_cb, isr_safe) \
264 { \
265 .base = Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, \
266 isr_safe ? BIT(PM_DEVICE_FLAG_ISR_SAFE) : 0), \
267 COND_CODE_1(isr_safe, (), (Z_PM_DEVICE_RUNTIME_INIT(obj))) \
268 }
269
275#define Z_PM_DEVICE_NAME(dev_id) _CONCAT(__pm_device_, dev_id)
276
277#ifdef CONFIG_PM
289#define Z_PM_DEVICE_DEFINE_SLOT(dev_id) \
290 static STRUCT_SECTION_ITERABLE_ALTERNATE(pm_device_slots, device, \
291 _CONCAT(__pm_slot_, dev_id))
292#else
293#define Z_PM_DEVICE_DEFINE_SLOT(dev_id)
294#endif /* CONFIG_PM */
295
296#ifdef CONFIG_PM_DEVICE
304#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe) \
305 Z_PM_DEVICE_DEFINE_SLOT(dev_id); \
306 static struct COND_CODE_1(isr_safe, (pm_device_isr), (pm_device)) \
307 Z_PM_DEVICE_NAME(dev_id) = \
308 Z_PM_DEVICE_INIT(Z_PM_DEVICE_NAME(dev_id), node_id, \
309 pm_action_cb, isr_safe)
310
316#define Z_PM_DEVICE_GET(dev_id) ((struct pm_device_base *)&Z_PM_DEVICE_NAME(dev_id))
317
318#else
319#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe)
320#define Z_PM_DEVICE_GET(dev_id) NULL
321#endif /* CONFIG_PM_DEVICE */
322
336#define PM_DEVICE_DEFINE(dev_id, pm_action_cb, ...) \
337 Z_PM_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, pm_action_cb, \
338 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
339
351#define PM_DEVICE_DT_DEFINE(node_id, pm_action_cb, ...) \
352 Z_PM_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), pm_action_cb, \
353 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
354
366#define PM_DEVICE_DT_INST_DEFINE(idx, pm_action_cb, ...) \
367 Z_PM_DEVICE_DEFINE(DT_DRV_INST(idx), \
368 Z_DEVICE_DT_DEV_ID(DT_DRV_INST(idx)), \
369 pm_action_cb, \
370 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
371
380#define PM_DEVICE_GET(dev_id) \
381 Z_PM_DEVICE_GET(dev_id)
382
391#define PM_DEVICE_DT_GET(node_id) \
392 PM_DEVICE_GET(Z_DEVICE_DT_DEV_ID(node_id))
393
402#define PM_DEVICE_DT_INST_GET(idx) \
403 PM_DEVICE_DT_GET(DT_DRV_INST(idx))
404
411
429int pm_device_action_run(const struct device *dev,
430 enum pm_device_action action);
431
443 enum pm_device_action action,
445
446#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
456int pm_device_state_get(const struct device *dev,
457 enum pm_device_state *state);
458
470static inline void pm_device_init_suspended(const struct device *dev)
471{
472 struct pm_device_base *pm = dev->pm_base;
473
475}
476
490static inline void pm_device_init_off(const struct device *dev)
491{
492 struct pm_device_base *pm = dev->pm_base;
493
495}
496
508void pm_device_busy_set(const struct device *dev);
509
517void pm_device_busy_clear(const struct device *dev);
518
526
535bool pm_device_is_busy(const struct device *dev);
536
550bool pm_device_wakeup_enable(const struct device *dev, bool enable);
551
560bool pm_device_wakeup_is_enabled(const struct device *dev);
561
570bool pm_device_wakeup_is_capable(const struct device *dev);
571
580bool pm_device_on_power_domain(const struct device *dev);
581
595int pm_device_power_domain_add(const struct device *dev,
596 const struct device *domain);
597
611 const struct device *domain);
612
622bool pm_device_is_powered(const struct device *dev);
623
639
640#else
641static inline int pm_device_state_get(const struct device *dev,
643{
644 ARG_UNUSED(dev);
645
647
648 return 0;
649}
650
651static inline void pm_device_init_suspended(const struct device *dev)
652{
653 ARG_UNUSED(dev);
654}
655static inline void pm_device_init_off(const struct device *dev)
656{
657 ARG_UNUSED(dev);
658}
659static inline void pm_device_busy_set(const struct device *dev)
660{
661 ARG_UNUSED(dev);
662}
663static inline void pm_device_busy_clear(const struct device *dev)
664{
665 ARG_UNUSED(dev);
666}
667static inline bool pm_device_is_any_busy(void) { return false; }
668static inline bool pm_device_is_busy(const struct device *dev)
669{
670 ARG_UNUSED(dev);
671 return false;
672}
673static inline bool pm_device_wakeup_enable(const struct device *dev,
674 bool enable)
675{
676 ARG_UNUSED(dev);
677 ARG_UNUSED(enable);
678 return false;
679}
680static inline bool pm_device_wakeup_is_enabled(const struct device *dev)
681{
682 ARG_UNUSED(dev);
683 return false;
684}
685static inline bool pm_device_wakeup_is_capable(const struct device *dev)
686{
687 ARG_UNUSED(dev);
688 return false;
689}
690static inline bool pm_device_on_power_domain(const struct device *dev)
691{
692 ARG_UNUSED(dev);
693 return false;
694}
695
696static inline int pm_device_power_domain_add(const struct device *dev,
697 const struct device *domain)
698{
699 ARG_UNUSED(dev);
700 ARG_UNUSED(domain);
701 return -ENOSYS;
702}
703
704static inline int pm_device_power_domain_remove(const struct device *dev,
705 const struct device *domain)
706{
707 ARG_UNUSED(dev);
708 ARG_UNUSED(domain);
709 return -ENOSYS;
710}
711
712static inline bool pm_device_is_powered(const struct device *dev)
713{
714 ARG_UNUSED(dev);
715 return true;
716}
717
718static inline int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb)
719{
720 int rc;
721
722 /* When power management is not enabled, all drivers should initialise to active state */
724 if ((rc < 0) && (rc != -ENOTSUP)) {
725 return rc;
726 }
727
729 if (rc < 0) {
730 return rc;
731 }
732
733 return 0;
734}
735
736#endif /* CONFIG_PM_DEVICE */
737
740#ifdef __cplusplus
741}
742#endif
743
744#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:470
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:119
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:130
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)
Setup a device driver into the lowest valid power mode.
bool pm_device_is_any_busy(void)
Check if any device is busy.
pm_device_action
Device PM actions.
Definition device.h:90
static void pm_device_init_off(const struct device *dev)
Initialize a device state to PM_DEVICE_STATE_OFF.
Definition device.h:490
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 is suspended.
Definition device.h:77
@ PM_DEVICE_STATE_OFF
Device is turned off (power removed).
Definition device.h:86
@ PM_DEVICE_STATE_SUSPENDING
Device is being suspended.
Definition device.h:79
@ PM_DEVICE_STATE_ACTIVE
Device is in active or regular state.
Definition device.h:70
@ PM_DEVICE_ACTION_TURN_OFF
Turn off.
Definition device.h:100
@ PM_DEVICE_ACTION_SUSPEND
Suspend.
Definition device.h:92
@ PM_DEVICE_ACTION_RESUME
Resume.
Definition device.h:94
@ PM_DEVICE_ACTION_TURN_ON
Turn on.
Definition device.h:106
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ENOTSUP
Unsupported value.
Definition errno.h:114
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:510
struct pm_device_base * pm_base
Definition device.h:542
Event Structure.
Definition kernel.h:2356
Semaphore structure.
Definition kernel.h:3275
Kernel Spin Lock.
Definition spinlock.h:45
A structure used to submit work after a delay.
Definition kernel.h:4101
Device PM info.
Definition device.h:139
uint32_t usage
Device usage count.
Definition device.h:148
pm_device_action_cb_t action_cb
Device PM action callback.
Definition device.h:145
enum pm_device_state state
Device power state.
Definition device.h:143
atomic_t flags
Device PM status flags.
Definition device.h:141
Runtime PM info for device with synchronous PM.
Definition device.h:187
struct k_spinlock lock
Lock to synchronize the synchronous get/put operations.
Definition device.h:192
struct pm_device_base base
Base info.
Definition device.h:189
Runtime PM info for device with generic PM.
Definition device.h:163
struct k_work_delayable work
Work object for asynchronous calls.
Definition device.h:175
struct k_event event
Event var to listen to the sync request events.
Definition device.h:172
struct k_sem lock
Lock to synchronize the get/put operations.
Definition device.h:170
const struct device * dev
Pointer to the device.
Definition device.h:168
struct pm_device_base base
Base info.
Definition device.h:165