1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * A general-purpose cyclic execution infrastructure, to allow "small"
4 * (run-time wise) functions to be executed at a specified frequency.
5 * Things like LED blinking or watchdog triggering are examples for such
8 * Copyright (C) 2022 Stefan Roese <sr@denx.de>
14 #include <linux/list.h>
15 #include <asm/types.h>
18 * struct cyclic_drv - Cyclic driver internal data
20 * @cyclic_list: Cylic list node
21 * @cyclic_ready: Flag if cyclic infrastructure is ready
24 struct list_head cyclic_list;
29 * struct cyclic_info - Information about cyclic execution function
31 * @func: Function to call periodically
32 * @ctx: Context pointer to get passed to this function
33 * @name: Name of the cyclic function, e.g. shown in the commands
34 * @delay_ns: Delay is ns after which this function shall get executed
35 * @start_time_us: Start time in us, when this function started its execution
36 * @cpu_time_us: Total CPU time of this function
37 * @run_cnt: Counter of executions occurances
38 * @next_call: Next time in us, when the function shall be executed again
40 * @already_warned: Flag that we've warned about exceeding CPU time usage
43 void (*func)(void *ctx);
47 uint64_t start_time_us;
51 struct list_head list;
55 /** Function type for cyclic functions */
56 typedef void (*cyclic_func_t)(void *ctx);
58 #if defined(CONFIG_CYCLIC)
60 * cyclic_register - Register a new cyclic function
62 * @func: Function to call periodically
63 * @delay_us: Delay is us after which this function shall get executed
64 * @name: Cyclic function name/id
65 * @ctx: Context to pass to the function
66 * @return: pointer to cyclic_struct if OK, NULL on error
68 struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
69 const char *name, void *ctx);
72 * cyclic_unregister - Unregister a cyclic function
74 * @cyclic: Pointer to cyclic_struct of the function that shall be removed
75 * @return: 0 if OK, -ve on error
77 int cyclic_unregister(struct cyclic_info *cyclic);
80 * cyclic_init() - Set up cyclic functions
82 * Init a list of cyclic functions, so that these can be added as needed
84 int cyclic_init(void);
87 * cyclic_uninit() - Clean up cyclic functions
89 * This removes all cyclic functions
91 int cyclic_uninit(void);
94 * cyclic_get_list() - Get cyclic list pointer
96 * Return the cyclic list pointer
98 * @return: pointer to cyclic_list
100 struct list_head *cyclic_get_list(void);
103 * cyclic_run() - Interate over all registered cyclic functions
105 * Interate over all registered cyclic functions and if the it's function
106 * needs to be executed, then call into these registered functions.
108 void cyclic_run(void);
111 * schedule() - Schedule all potentially waiting tasks
113 * Basically a wrapper for cyclic_run(), pontentially enhanced by some
114 * other parts, that need to get handled periodically.
118 static inline struct cyclic_info *cyclic_register(cyclic_func_t func,
126 static inline int cyclic_unregister(struct cyclic_info *cyclic)
131 static inline void cyclic_run(void)
135 static inline void schedule(void)
139 static inline int cyclic_init(void)
144 static inline int cyclic_uninit(void)