Zephyr API Documentation 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
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
183typedef int (*pm_device_action_cb_t)(const struct device *dev,
184 enum pm_device_action action);
185
194typedef bool (*pm_device_action_failed_cb_t)(const struct device *dev,
195 int err);
196
210#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
213#endif /* CONFIG_PM_DEVICE_RUNTIME */
214#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
216 const struct device *domain;
217#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
218};
219
227struct pm_device {
230#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
232 const struct device *dev;
234 struct k_sem lock;
235#if defined(CONFIG_PM_DEVICE_RUNTIME_ASYNC) || defined(__DOXYGEN__)
240#endif /* CONFIG_PM_DEVICE_RUNTIME_ASYNC */
241#endif /* CONFIG_PM_DEVICE_RUNTIME */
242};
243
254#if defined(CONFIG_PM_DEVICE_RUNTIME) || defined(__DOXYGEN__)
257#endif
258};
259
260/* Base part must be the first element. */
261BUILD_ASSERT(offsetof(struct pm_device, base) == 0);
262BUILD_ASSERT(offsetof(struct pm_device_isr, base) == 0);
263
265
266#ifdef CONFIG_PM_DEVICE_RUNTIME_ASYNC
267#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
268 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1), \
269 .event = Z_EVENT_INITIALIZER(obj.event),
270#elif CONFIG_PM_DEVICE_RUNTIME
271#define Z_PM_DEVICE_RUNTIME_INIT(obj) \
272 .lock = Z_SEM_INITIALIZER(obj.lock, 1, 1),
273#else
274#define Z_PM_DEVICE_RUNTIME_INIT(obj)
275#endif /* CONFIG_PM_DEVICE_RUNTIME */
276
277#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
278#define Z_PM_DEVICE_POWER_DOMAIN_INIT(_node_id) \
279 .domain = DEVICE_DT_GET_OR_NULL(DT_PHANDLE(_node_id, \
280 power_domains)),
281#else
282#define Z_PM_DEVICE_POWER_DOMAIN_INIT(obj)
283#endif /* CONFIG_PM_DEVICE_POWER_DOMAIN */
284
290#define Z_PM_DEVICE_FLAGS(node_id) \
291 (COND_CODE_1( \
292 DT_NODE_EXISTS(node_id), \
293 ((DT_PROP_OR(node_id, wakeup_source, 0) \
294 << PM_DEVICE_FLAG_WS_CAPABLE) | \
295 (DT_PROP_OR(node_id, zephyr_pm_device_runtime_auto, 0) \
296 << PM_DEVICE_FLAG_RUNTIME_AUTO) | \
297 (DT_NODE_HAS_COMPAT(node_id, power_domain) << \
298 PM_DEVICE_FLAG_PD)), \
299 (0)))
300
312#define Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, _flags) \
313 { \
314 .flags = ATOMIC_INIT(Z_PM_DEVICE_FLAGS(node_id) | (_flags)), \
315 .state = PM_DEVICE_STATE_ACTIVE, \
316 .action_cb = pm_action_cb, \
317 Z_PM_DEVICE_POWER_DOMAIN_INIT(node_id) \
318 }
319
330#define Z_PM_DEVICE_INIT(obj, node_id, pm_action_cb, isr_safe) \
331 { \
332 .base = Z_PM_DEVICE_BASE_INIT(obj, node_id, pm_action_cb, \
333 isr_safe ? BIT(PM_DEVICE_FLAG_ISR_SAFE) : 0), \
334 COND_CODE_1(isr_safe, (), (Z_PM_DEVICE_RUNTIME_INIT(obj))) \
335 }
336
342#define Z_PM_DEVICE_NAME(dev_id) _CONCAT(__pm_device_, dev_id)
343
344#ifdef CONFIG_PM
356#define Z_PM_DEVICE_DEFINE_SLOT(dev_id) \
357 static STRUCT_SECTION_ITERABLE_ALTERNATE(pm_device_slots, device, \
358 _CONCAT(__pm_slot_, dev_id))
359#else
360#define Z_PM_DEVICE_DEFINE_SLOT(dev_id)
361#endif /* CONFIG_PM */
362
363#ifdef CONFIG_PM_DEVICE
371#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe) \
372 Z_PM_DEVICE_DEFINE_SLOT(dev_id); \
373 static struct COND_CODE_1(isr_safe, (pm_device_isr), (pm_device)) \
374 Z_PM_DEVICE_NAME(dev_id) = \
375 Z_PM_DEVICE_INIT(Z_PM_DEVICE_NAME(dev_id), node_id, \
376 pm_action_cb, isr_safe)
377
383#define Z_PM_DEVICE_GET(dev_id) ((struct pm_device_base *)&Z_PM_DEVICE_NAME(dev_id))
384
385#else
386#define Z_PM_DEVICE_DEFINE(node_id, dev_id, pm_action_cb, isr_safe)
387#define Z_PM_DEVICE_GET(dev_id) NULL
388#endif /* CONFIG_PM_DEVICE */
389
391
403#define PM_DEVICE_DEFINE(dev_id, pm_action_cb, ...) \
404 Z_PM_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, pm_action_cb, \
405 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
406
418#define PM_DEVICE_DT_DEFINE(node_id, pm_action_cb, ...) \
419 Z_PM_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), pm_action_cb, \
420 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
421
433#define PM_DEVICE_DT_INST_DEFINE(idx, pm_action_cb, ...) \
434 Z_PM_DEVICE_DEFINE(DT_DRV_INST(idx), \
435 Z_DEVICE_DT_DEV_ID(DT_DRV_INST(idx)), \
436 pm_action_cb, \
437 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (__VA_ARGS__)))
438
447#define PM_DEVICE_GET(dev_id) \
448 Z_PM_DEVICE_GET(dev_id)
449
458#define PM_DEVICE_DT_GET(node_id) \
459 PM_DEVICE_GET(Z_DEVICE_DT_DEV_ID(node_id))
460
469#define PM_DEVICE_DT_INST_GET(idx) \
470 PM_DEVICE_DT_GET(DT_DRV_INST(idx))
471
478
496int pm_device_action_run(const struct device *dev,
497 enum pm_device_action action);
498
510 enum pm_device_action action,
512
513#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
523int pm_device_state_get(const struct device *dev,
524 enum pm_device_state *state);
525
537static inline void pm_device_init_suspended(const struct device *dev)
538{
539 struct pm_device_base *pm = dev->pm_base;
540
542}
543
557static inline void pm_device_init_off(const struct device *dev)
558{
559 struct pm_device_base *pm = dev->pm_base;
560
562}
563
575void pm_device_busy_set(const struct device *dev);
576
584void pm_device_busy_clear(const struct device *dev);
585
593
602bool pm_device_is_busy(const struct device *dev);
603
617bool pm_device_wakeup_enable(const struct device *dev, bool enable);
618
627bool pm_device_wakeup_is_enabled(const struct device *dev);
628
637bool pm_device_wakeup_is_capable(const struct device *dev);
638
647bool pm_device_on_power_domain(const struct device *dev);
648
662int pm_device_power_domain_add(const struct device *dev,
663 const struct device *domain);
664
678 const struct device *domain);
679
689bool pm_device_is_powered(const struct device *dev);
690
714
733#else
734static inline int pm_device_state_get(const struct device *dev,
736{
737 ARG_UNUSED(dev);
738
740
741 return 0;
742}
743
744static inline void pm_device_init_suspended(const struct device *dev)
745{
746 ARG_UNUSED(dev);
747}
748static inline void pm_device_init_off(const struct device *dev)
749{
750 ARG_UNUSED(dev);
751}
752static inline void pm_device_busy_set(const struct device *dev)
753{
754 ARG_UNUSED(dev);
755}
756static inline void pm_device_busy_clear(const struct device *dev)
757{
758 ARG_UNUSED(dev);
759}
760static inline bool pm_device_is_any_busy(void) { return false; }
761static inline bool pm_device_is_busy(const struct device *dev)
762{
763 ARG_UNUSED(dev);
764 return false;
765}
766static inline bool pm_device_wakeup_enable(const struct device *dev,
767 bool enable)
768{
769 ARG_UNUSED(dev);
770 ARG_UNUSED(enable);
771 return false;
772}
773static inline bool pm_device_wakeup_is_enabled(const struct device *dev)
774{
775 ARG_UNUSED(dev);
776 return false;
777}
778static inline bool pm_device_wakeup_is_capable(const struct device *dev)
779{
780 ARG_UNUSED(dev);
781 return false;
782}
783static inline bool pm_device_on_power_domain(const struct device *dev)
784{
785 ARG_UNUSED(dev);
786 return false;
787}
788
789static inline int pm_device_power_domain_add(const struct device *dev,
790 const struct device *domain)
791{
792 ARG_UNUSED(dev);
793 ARG_UNUSED(domain);
794 return -ENOSYS;
795}
796
797static inline int pm_device_power_domain_remove(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 bool pm_device_is_powered(const struct device *dev)
806{
807 ARG_UNUSED(dev);
808 return true;
809}
810
811static inline int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb)
812{
813 int rc;
814
815 /* When power management is not enabled, all drivers should initialise to active state */
817 if ((rc < 0) && (rc != -ENOTSUP)) {
818 return rc;
819 }
820
822 if (rc < 0) {
823 return rc;
824 }
825
826 return 0;
827}
828
829static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
830{
832}
833
834#endif /* CONFIG_PM_DEVICE */
835
837
838#ifdef __cplusplus
839}
840#endif
841
842#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:537
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:183
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:194
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:557
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
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:2651
Semaphore structure.
Definition kernel.h:3663
Kernel Spin Lock.
Definition spinlock.h:45
A structure used to submit work after a delay.
Definition kernel.h:4603
Device PM info.
Definition device.h:203
uint32_t usage
Device usage count.
Definition device.h:212
pm_device_action_cb_t action_cb
Device PM action callback.
Definition device.h:209
enum pm_device_state state
Device power state.
Definition device.h:207
atomic_t flags
Device PM status flags.
Definition device.h:205
Runtime PM info for device with synchronous PM.
Definition device.h:251
struct k_spinlock lock
Lock to synchronize the synchronous get/put operations.
Definition device.h:256
struct pm_device_base base
Base info.
Definition device.h:253
Runtime PM info for device with generic PM.
Definition device.h:227
struct k_work_delayable work
Work object for asynchronous calls.
Definition device.h:239
struct k_event event
Event var to listen to the sync request events.
Definition device.h:237
struct k_sem lock
Lock to synchronize the get/put operations.
Definition device.h:234
const struct device * dev
Pointer to the device.
Definition device.h:232
struct pm_device_base base
Base info.
Definition device.h:229