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
31
33
34struct device;
35
37enum pm_device_flag {
39 PM_DEVICE_FLAG_BUSY,
41 PM_DEVICE_FLAG_TURN_ON_FAILED,
43 PM_DEVICE_FLAG_PD_CLAIMED,
48 PM_DEVICE_FLAG_WS_CAPABLE,
50 PM_DEVICE_FLAG_WS_ENABLED,
52 PM_DEVICE_FLAG_RUNTIME_ENABLED,
54 PM_DEVICE_FLAG_PD,
56 PM_DEVICE_FLAG_RUNTIME_AUTO,
58 PM_DEVICE_FLAG_ISR_SAFE,
59};
60
62
71#define PM_DEVICE_ISR_SAFE 1
72
148
179
189typedef int (*pm_device_action_cb_t)(const struct device *dev,
190 enum pm_device_action action);
191
200typedef bool (*pm_device_action_failed_cb_t)(const struct device *dev,
201 int err);
202
216#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
219#endif /* CONFIG_PM_DEVICE_RUNTIME */
220#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
222 const struct device *domain;
223#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
224};
225
233struct pm_device {
236#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
238 const struct device *dev;
240 struct k_sem lock;
241#if defined(CONFIG_PM_DEVICE_RUNTIME_ASYNC) || defined(__DOXYGEN__)
246#endif /* CONFIG_PM_DEVICE_RUNTIME_ASYNC */
247#endif /* CONFIG_PM_DEVICE_RUNTIME */
248};
249
260#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
263#endif
264};
265
266/* Base part must be the first element. */
267BUILD_ASSERT(offsetof(struct pm_device, base) == 0);
268BUILD_ASSERT(offsetof(struct pm_device_isr, base) == 0);
269
271
272#ifdef CONFIG_PM_DEVICE_RUNTIME_ASYNC
273#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
274 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1), \
275 .event = Z_EVENT_INITIALIZER(obj.event),
276#elif CONFIG_PM_DEVICE_RUNTIME
277#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
278 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1),
279#else
280#define Z_PM_DEVICE_RUNTIME_INIT(obj)
281#endif /* CONFIG_PM_DEVICE_RUNTIME */
282
283#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
284#define Z_PM_DEVICE_POWER_DOMAIN_INIT(_node_id) \
285 .domain = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(_node_id, \
286 power_domains)),
287#else
288#define Z_PM_DEVICE_POWER_DOMAIN_INIT(obj)
289#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
290
296#define Z_PM_DEVICE_FLAGS(node_id) \
297 (COND_CODE_1( \
298 DT_NODE_EXISTS(node_id), \
299 ((DT_PROP_OR(node_id, wakeup_source, 0) \
300 << PM_DEVICE_FLAG_WS_CAPABLE) | \
301 (DT_PROP_OR(node_id, zephyr_pm_device_runtime_auto, 0) \
302 << PM_DEVICE_FLAG_RUNTIME_AUTO) | \
303 (DT_NODE_HAS_COMPAT(node_id, power_domain) << \
304 PM_DEVICE_FLAG_PD)), \
305 (0)))
306
318#define Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, _flags) \
319 { \
320 .flags = ATOMIC_INIT(Z_PM_DEVICE_FLAGS(node_id) | (_flags)), \
321 .state = PM_DEVICE_STATE_ACTIVE, \
322 .action_cb = pm_action_cb, \
323 Z_PM_DEVICE_POWER_DOMAIN_INIT(node_id) \
324 }
325
336#define Z_PM_DEVICE_INIT(obj, node_id, pm_action_cb, isr_safe) \
337 { \
338 .base = Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, \
339 isr_safe ? BIT(PM_DEVICE_FLAG_ISR_SAFE) : 0), \
340 COND_CODE_1(isr_safe, (), (Z_PM_DEVICE_RUNTIME_INIT(obj))) \
341 }
342
348#define Z_PM_DEVICE_NAME(dev_id) _CONCAT(__pm_device_, dev_id)
349
350#ifdef CONFIG_PM
362#define Z_PM_DEVICE_DEFINE_SLOT(dev_id) \
363 static STRUCT_SECTION_ITERABLE_ALTERNATE(pm_device_slots, device, \
364 _CONCAT(__pm_slot_, dev_id))
365#else
366#define Z_PM_DEVICE_DEFINE_SLOT(dev_id)
367#endif /* CONFIG_PM */
368
369#ifdef CONFIG_PM_DEVICE
377#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe) \
378 Z_PM_DEVICE_DEFINE_SLOT(dev_id); \
379 static struct COND_CODE_1(isr_safe, (pm_device_isr), (pm_device)) \
380 Z_PM_DEVICE_NAME(dev_id) = \
381 Z_PM_DEVICE_INIT(Z_PM_DEVICE_NAME(dev_id), node_id, \
382 pm_action_cb, isr_safe)
383
389#define Z_PM_DEVICE_GET(dev_id) ((struct pm_device_base *)&Z_PM_DEVICE_NAME(dev_id))
390
391#else
392#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe)
393#define Z_PM_DEVICE_GET(dev_id) NULL
394#endif /* CONFIG_PM_DEVICE */
395
397
409#define PM_DEVICE_DEFINE(dev_id, pm_action_cb, ...) \
410 Z_PM_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, pm_action_cb, \
411 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
412
424#define PM_DEVICE_DT_DEFINE(node_id, pm_action_cb, ...) \
425 Z_PM_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), pm_action_cb, \
426 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
427
439#define PM_DEVICE_DT_INST_DEFINE(idx, pm_action_cb, ...) \
440 Z_PM_DEVICE_DEFINE(DT_DRV_INST(idx), \
441 Z_DEVICE_DT_DEV_ID(DT_DRV_INST(idx)), \
442 pm_action_cb, \
443 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
444
453#define PM_DEVICE_GET(dev_id) \
454 Z_PM_DEVICE_GET(dev_id)
455
464#define PM_DEVICE_DT_GET(node_id) \
465 PM_DEVICE_GET(Z_DEVICE_DT_DEV_ID(node_id))
466
475#define PM_DEVICE_DT_INST_GET(idx) \
476 PM_DEVICE_DT_GET(DT_DRV_INST(idx))
477
484
502int pm_device_action_run(const struct device *dev,
503 enum pm_device_action action);
504
516 enum pm_device_action action,
518
519#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
529int pm_device_state_get(const struct device *dev,
530 enum pm_device_state *state);
531
543static inline void pm_device_init_suspended(const struct device *dev)
544{
545 struct pm_device_base *pm = dev->pm_base;
546
548}
549
563static inline void pm_device_init_off(const struct device *dev)
564{
565 struct pm_device_base *pm = dev->pm_base;
566
568}
569
581void pm_device_busy_set(const struct device *dev);
582
590void pm_device_busy_clear(const struct device *dev);
591
599
608bool pm_device_is_busy(const struct device *dev);
609
623bool pm_device_wakeup_enable(const struct device *dev, bool enable);
624
633bool pm_device_wakeup_is_enabled(const struct device *dev);
634
643bool pm_device_wakeup_is_capable(const struct device *dev);
644
653bool pm_device_on_power_domain(const struct device *dev);
654
668int pm_device_power_domain_add(const struct device *dev,
669 const struct device *domain);
670
684 const struct device *domain);
685
695bool pm_device_is_powered(const struct device *dev);
696
720
739#else
740static inline int pm_device_state_get(const struct device *dev,
742{
743 ARG_UNUSED(dev);
744
746
747 return 0;
748}
749
750static inline void pm_device_init_suspended(const struct device *dev)
751{
752 ARG_UNUSED(dev);
753}
754static inline void pm_device_init_off(const struct device *dev)
755{
756 ARG_UNUSED(dev);
757}
758static inline void pm_device_busy_set(const struct device *dev)
759{
760 ARG_UNUSED(dev);
761}
762static inline void pm_device_busy_clear(const struct device *dev)
763{
764 ARG_UNUSED(dev);
765}
766static inline bool pm_device_is_any_busy(void) { return false; }
767static inline bool pm_device_is_busy(const struct device *dev)
768{
769 ARG_UNUSED(dev);
770 return false;
771}
772static inline bool pm_device_wakeup_enable(const struct device *dev,
773 bool enable)
774{
775 ARG_UNUSED(dev);
776 ARG_UNUSED(enable);
777 return false;
778}
779static inline bool pm_device_wakeup_is_enabled(const struct device *dev)
780{
781 ARG_UNUSED(dev);
782 return false;
783}
784static inline bool pm_device_wakeup_is_capable(const struct device *dev)
785{
786 ARG_UNUSED(dev);
787 return false;
788}
789static inline bool pm_device_on_power_domain(const struct device *dev)
790{
791 ARG_UNUSED(dev);
792 return false;
793}
794
795static inline int pm_device_power_domain_add(const struct device *dev,
796 const struct device *domain)
797{
798 ARG_UNUSED(dev);
799 ARG_UNUSED(domain);
800 return -ENOSYS;
801}
802
803static inline int pm_device_power_domain_remove(const struct device *dev,
804 const struct device *domain)
805{
806 ARG_UNUSED(dev);
807 ARG_UNUSED(domain);
808 return -ENOSYS;
809}
810
811static inline bool pm_device_is_powered(const struct device *dev)
812{
813 ARG_UNUSED(dev);
814 return true;
815}
816
817static inline int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb)
818{
819 int rc;
820
821 /* When power management is not enabled, all drivers should initialise to active state */
823 if ((rc < 0) && (rc != -ENOTSUP)) {
824 return rc;
825 }
826
828 if (rc < 0) {
829 return rc;
830 }
831
832 return 0;
833}
834
835static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
836{
838}
839
840#endif /* CONFIG_PM_DEVICE */
841
843
844#ifdef __cplusplus
845}
846#endif
847
848#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:543
pm_device_state
Device power states.
Definition device.h:74
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:189
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:200
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:150
static void pm_device_init_off(const struct device *dev)
Initialize a device state to PM_DEVICE_STATE_OFF.
Definition device.h:563
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:105
@ PM_DEVICE_STATE_OFF
Device hardware is not powered.
Definition device.h:146
@ 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:122
@ PM_DEVICE_STATE_ACTIVE
Device hardware is powered, and the device is needed by the system.
Definition device.h:81
@ PM_DEVICE_ACTION_TURN_OFF
Turn off.
Definition device.h:169
@ PM_DEVICE_ACTION_SUSPEND
Suspend.
Definition device.h:152
@ PM_DEVICE_ACTION_RESUME
Resume.
Definition device.h:154
@ PM_DEVICE_ACTION_TURN_ON
Turn on.
Definition device.h:177
#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:2691
Semaphore structure.
Definition kernel.h:3703
Kernel Spin Lock.
Definition spinlock.h:45
A structure used to submit work after a delay.
Definition kernel.h:4643
Device PM info.
Definition device.h:209
uint32_t usage
Device usage count.
Definition device.h:218
pm_device_action_cb_t action_cb
Device PM action callback.
Definition device.h:215
enum pm_device_state state
Device power state.
Definition device.h:213
atomic_t flags
Device PM status flags.
Definition device.h:211
Runtime PM info for device with synchronous PM.
Definition device.h:257
struct k_spinlock lock
Lock to synchronize the synchronous get/put operations.
Definition device.h:262
struct pm_device_base base
Base info.
Definition device.h:259
Runtime PM info for device with generic PM.
Definition device.h:233
struct k_work_delayable work
Work object for asynchronous calls.
Definition device.h:245
struct k_event event
Event var to listen to the sync request events.
Definition device.h:243
struct k_sem lock
Lock to synchronize the get/put operations.
Definition device.h:240
const struct device * dev
Pointer to the device.
Definition device.h:238
struct pm_device_base base
Base info.
Definition device.h:235
Header file for the Atomic operations API.
Iterable sections helpers.