Zephyr Project API 4.4.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
12
13#ifndef ZEPHYR_INCLUDE_PM_DEVICE_H_
14#define ZEPHYR_INCLUDE_PM_DEVICE_H_
15
16#include <zephyr/device.h>
17#include <zephyr/kernel.h>
18#include <zephyr/sys/atomic.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
33
35
36struct device;
37
39enum pm_device_flag {
41 PM_DEVICE_FLAG_BUSY,
43 PM_DEVICE_FLAG_TURN_ON_FAILED,
45 PM_DEVICE_FLAG_PD_CLAIMED,
50 PM_DEVICE_FLAG_WS_CAPABLE,
52 PM_DEVICE_FLAG_WS_ENABLED,
54 PM_DEVICE_FLAG_RUNTIME_ENABLED,
56 PM_DEVICE_FLAG_PD,
58 PM_DEVICE_FLAG_RUNTIME_AUTO,
60 PM_DEVICE_FLAG_ISR_SAFE,
61};
62
64
73#define PM_DEVICE_ISR_SAFE 1
74
150
181
191typedef int (*pm_device_action_cb_t)(const struct device *dev,
192 enum pm_device_action action);
193
202typedef bool (*pm_device_action_failed_cb_t)(const struct device *dev,
203 int err);
204
218#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
221#endif /* CONFIG_PM_DEVICE_RUNTIME */
222#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
224 const struct device *domain;
225#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
226};
227
235struct pm_device {
238#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
240 const struct device *dev;
242 struct k_sem lock;
243#if defined(CONFIG_PM_DEVICE_RUNTIME_ASYNC) || defined(__DOXYGEN__)
248#endif /* CONFIG_PM_DEVICE_RUNTIME_ASYNC */
249#endif /* CONFIG_PM_DEVICE_RUNTIME */
250};
251
262#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
265#endif
266};
267
268/* Base part must be the first element. */
269BUILD_ASSERT(offsetof(struct pm_device, base) == 0);
270BUILD_ASSERT(offsetof(struct pm_device_isr, base) == 0);
271
273
274#ifdef CONFIG_PM_DEVICE_RUNTIME_ASYNC
275#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
276 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1), \
277 .event = Z_EVENT_INITIALIZER(obj.event),
278#elif CONFIG_PM_DEVICE_RUNTIME
279#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
280 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1),
281#else
282#define Z_PM_DEVICE_RUNTIME_INIT(obj)
283#endif /* CONFIG_PM_DEVICE_RUNTIME */
284
285#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
286#define Z_PM_DEVICE_POWER_DOMAIN_INIT(_node_id) \
287 .domain = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(_node_id, \
288 power_domains)),
289#else
290#define Z_PM_DEVICE_POWER_DOMAIN_INIT(obj)
291#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
292
298#define Z_PM_DEVICE_FLAGS(node_id) \
299 (COND_CODE_1( \
300 DT_NODE_EXISTS(node_id), \
301 ((DT_PROP_OR(node_id, wakeup_source, 0) \
302 << PM_DEVICE_FLAG_WS_CAPABLE) | \
303 (DT_PROP_OR(node_id, zephyr_pm_device_runtime_auto, 0) \
304 << PM_DEVICE_FLAG_RUNTIME_AUTO) | \
305 (DT_NODE_HAS_COMPAT(node_id, power_domain) << \
306 PM_DEVICE_FLAG_PD)), \
307 (0)))
308
320#define Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, _flags) \
321 { \
322 .flags = ATOMIC_INIT(Z_PM_DEVICE_FLAGS(node_id) | (_flags)), \
323 .state = PM_DEVICE_STATE_ACTIVE, \
324 .action_cb = pm_action_cb, \
325 Z_PM_DEVICE_POWER_DOMAIN_INIT(node_id) \
326 }
327
338#define Z_PM_DEVICE_INIT(obj, node_id, pm_action_cb, isr_safe) \
339 { \
340 .base = Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, \
341 isr_safe ? BIT(PM_DEVICE_FLAG_ISR_SAFE) : 0), \
342 COND_CODE_1(isr_safe, (), (Z_PM_DEVICE_RUNTIME_INIT(obj))) \
343 }
344
350#define Z_PM_DEVICE_NAME(dev_id) _CONCAT(__pm_device_, dev_id)
351
352#ifdef CONFIG_PM
364#define Z_PM_DEVICE_DEFINE_SLOT(dev_id) \
365 static STRUCT_SECTION_ITERABLE_ALTERNATE(pm_device_slots, device, \
366 _CONCAT(__pm_slot_, dev_id))
367#else
368#define Z_PM_DEVICE_DEFINE_SLOT(dev_id)
369#endif /* CONFIG_PM */
370
371#ifdef CONFIG_PM_DEVICE
379#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe) \
380 Z_PM_DEVICE_DEFINE_SLOT(dev_id); \
381 static struct COND_CODE_1(isr_safe, (pm_device_isr), (pm_device)) \
382 Z_PM_DEVICE_NAME(dev_id) = \
383 Z_PM_DEVICE_INIT(Z_PM_DEVICE_NAME(dev_id), node_id, \
384 pm_action_cb, isr_safe)
385
391#define Z_PM_DEVICE_GET(dev_id) ((struct pm_device_base *)&Z_PM_DEVICE_NAME(dev_id))
392
393#else
394#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe)
395#define Z_PM_DEVICE_GET(dev_id) NULL
396#endif /* CONFIG_PM_DEVICE */
397
399
411#define PM_DEVICE_DEFINE(dev_id, pm_action_cb, ...) \
412 Z_PM_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, pm_action_cb, \
413 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
414
426#define PM_DEVICE_DT_DEFINE(node_id, pm_action_cb, ...) \
427 Z_PM_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), pm_action_cb, \
428 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
429
441#define PM_DEVICE_DT_INST_DEFINE(idx, pm_action_cb, ...) \
442 Z_PM_DEVICE_DEFINE(DT_DRV_INST(idx), \
443 Z_DEVICE_DT_DEV_ID(DT_DRV_INST(idx)), \
444 pm_action_cb, \
445 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
446
455#define PM_DEVICE_GET(dev_id) \
456 Z_PM_DEVICE_GET(dev_id)
457
466#define PM_DEVICE_DT_GET(node_id) \
467 PM_DEVICE_GET(Z_DEVICE_DT_DEV_ID(node_id))
468
477#define PM_DEVICE_DT_INST_GET(idx) \
478 PM_DEVICE_DT_GET(DT_DRV_INST(idx))
479
486
504int pm_device_action_run(const struct device *dev,
505 enum pm_device_action action);
506
518 enum pm_device_action action,
520
521#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
531int pm_device_state_get(const struct device *dev,
532 enum pm_device_state *state);
533
545static inline void pm_device_init_suspended(const struct device *dev)
546{
547 struct pm_device_base *pm = dev->pm_base;
548
550}
551
565static inline void pm_device_init_off(const struct device *dev)
566{
567 struct pm_device_base *pm = dev->pm_base;
568
570}
571
583void pm_device_busy_set(const struct device *dev);
584
592void pm_device_busy_clear(const struct device *dev);
593
601
610bool pm_device_is_busy(const struct device *dev);
611
625bool pm_device_wakeup_enable(const struct device *dev, bool enable);
626
635bool pm_device_wakeup_is_enabled(const struct device *dev);
636
645bool pm_device_wakeup_is_capable(const struct device *dev);
646
655bool pm_device_on_power_domain(const struct device *dev);
656
670int pm_device_power_domain_add(const struct device *dev,
671 const struct device *domain);
672
686 const struct device *domain);
687
697bool pm_device_is_powered(const struct device *dev);
698
722
741#else
742static inline int pm_device_state_get(const struct device *dev,
744{
745 ARG_UNUSED(dev);
746
748
749 return 0;
750}
751
752static inline void pm_device_init_suspended(const struct device *dev)
753{
754 ARG_UNUSED(dev);
755}
756static inline void pm_device_init_off(const struct device *dev)
757{
758 ARG_UNUSED(dev);
759}
760static inline void pm_device_busy_set(const struct device *dev)
761{
762 ARG_UNUSED(dev);
763}
764static inline void pm_device_busy_clear(const struct device *dev)
765{
766 ARG_UNUSED(dev);
767}
768static inline bool pm_device_is_any_busy(void) { return false; }
769static inline bool pm_device_is_busy(const struct device *dev)
770{
771 ARG_UNUSED(dev);
772 return false;
773}
774static inline bool pm_device_wakeup_enable(const struct device *dev,
775 bool enable)
776{
777 ARG_UNUSED(dev);
778 ARG_UNUSED(enable);
779 return false;
780}
781static inline bool pm_device_wakeup_is_enabled(const struct device *dev)
782{
783 ARG_UNUSED(dev);
784 return false;
785}
786static inline bool pm_device_wakeup_is_capable(const struct device *dev)
787{
788 ARG_UNUSED(dev);
789 return false;
790}
791static inline bool pm_device_on_power_domain(const struct device *dev)
792{
793 ARG_UNUSED(dev);
794 return false;
795}
796
797static inline int pm_device_power_domain_add(const struct device *dev,
798 const struct device *domain)
799{
800 ARG_UNUSED(dev);
801 ARG_UNUSED(domain);
802 return -ENOSYS;
803}
804
805static inline int pm_device_power_domain_remove(const struct device *dev,
806 const struct device *domain)
807{
808 ARG_UNUSED(dev);
809 ARG_UNUSED(domain);
810 return -ENOSYS;
811}
812
813static inline bool pm_device_is_powered(const struct device *dev)
814{
815 ARG_UNUSED(dev);
816 return true;
817}
818
819static inline int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb)
820{
821 int rc;
822
823 /* When power management is not enabled, all drivers should initialise to active state */
825 if ((rc < 0) && (rc != -ENOTSUP)) {
826 return rc;
827 }
828
830 if (rc < 0) {
831 return rc;
832 }
833
834 return 0;
835}
836
837static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
838{
840}
841
842#endif /* CONFIG_PM_DEVICE */
843
845
846#ifdef __cplusplus
847}
848#endif
849
850#endif
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
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:545
pm_device_state
Device power states.
Definition device.h:76
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:191
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:202
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:152
static void pm_device_init_off(const struct device *dev)
Initialize a device state to PM_DEVICE_STATE_OFF.
Definition device.h:565
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:107
@ PM_DEVICE_STATE_OFF
Device hardware is not powered.
Definition device.h:148
@ 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:124
@ PM_DEVICE_STATE_ACTIVE
Device hardware is powered, and the device is needed by the system.
Definition device.h:83
@ PM_DEVICE_ACTION_TURN_OFF
Turn off.
Definition device.h:171
@ PM_DEVICE_ACTION_SUSPEND
Suspend.
Definition device.h:154
@ PM_DEVICE_ACTION_RESUME
Resume.
Definition device.h:156
@ PM_DEVICE_ACTION_TURN_ON
Turn on.
Definition device.h:179
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define ENOTSUP
Unsupported value.
Definition errno.h:115
#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:2777
Semaphore structure.
Definition kernel.h:3798
Kernel Spin Lock.
Definition spinlock.h:45
A structure used to submit work after a delay.
Definition kernel.h:4750
Device PM info.
Definition device.h:211
uint32_t usage
Device usage count.
Definition device.h:220
pm_device_action_cb_t action_cb
Device PM action callback.
Definition device.h:217
enum pm_device_state state
Device power state.
Definition device.h:215
atomic_t flags
Device PM status flags.
Definition device.h:213
Runtime PM info for device with synchronous PM.
Definition device.h:259
struct k_spinlock lock
Lock to synchronize the synchronous get/put operations.
Definition device.h:264
struct pm_device_base base
Base info.
Definition device.h:261
Runtime PM info for device with generic PM.
Definition device.h:235
struct k_work_delayable work
Work object for asynchronous calls.
Definition device.h:247
struct k_event event
Event var to listen to the sync request events.
Definition device.h:245
struct k_sem lock
Lock to synchronize the get/put operations.
Definition device.h:242
const struct device * dev
Pointer to the device.
Definition device.h:240
struct pm_device_base base
Base info.
Definition device.h:237
Header file for the Atomic operations API.
Iterable sections helpers.