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
22 * @cyclic_running: Flag if cyclic infrastructure is running
25 struct list_head cyclic_list;
31 * struct cyclic_info - Information about cyclic execution function
33 * @func: Function to call periodically
34 * @ctx: Context pointer to get passed to this function
35 * @name: Name of the cyclic function, e.g. shown in the commands
36 * @delay_ns: Delay is ns after which this function shall get executed
37 * @start_time_us: Start time in us, when this function started its execution
38 * @cpu_time_us: Total CPU time of this function
39 * @run_cnt: Counter of executions occurances
40 * @next_call: Next time in us, when the function shall be executed again
42 * @already_warned: Flag that we've warned about exceeding CPU time usage
45 void (*func)(void *ctx);
49 uint64_t start_time_us;
53 struct list_head list;
57 /** Function type for cyclic functions */
58 typedef void (*cyclic_func_t)(void *ctx);
60 #if defined(CONFIG_CYCLIC)
62 * cyclic_register - Register a new cyclic function
64 * @func: Function to call periodically
65 * @delay_us: Delay is us after which this function shall get executed
66 * @name: Cyclic function name/id
67 * @ctx: Context to pass to the function
68 * @return: pointer to cyclic_struct if OK, NULL on error
70 struct cyclic_info *cyclic_register(cyclic_func_t func, uint64_t delay_us,
71 const char *name, void *ctx);
74 * cyclic_unregister - Unregister a cyclic function
76 * @cyclic: Pointer to cyclic_struct of the function that shall be removed
77 * @return: 0 if OK, -ve on error
79 int cyclic_unregister(struct cyclic_info *cyclic);
82 * cyclic_init() - Set up cyclic functions
84 * Init a list of cyclic functions, so that these can be added as needed
86 int cyclic_init(void);
89 * cyclic_uninit() - Clean up cyclic functions
91 * This removes all cyclic functions
93 int cyclic_uninit(void);
96 * cyclic_get_list() - Get cyclic list pointer
98 * Return the cyclic list pointer
100 * @return: pointer to cyclic_list
102 struct list_head *cyclic_get_list(void);
105 * cyclic_run() - Interate over all registered cyclic functions
107 * Interate over all registered cyclic functions and if the it's function
108 * needs to be executed, then call into these registered functions.
110 void cyclic_run(void);
113 * schedule() - Schedule all potentially waiting tasks
115 * Basically a wrapper for cyclic_run(), pontentially enhanced by some
116 * other parts, that need to get handled periodically.
120 static inline struct cyclic_info *cyclic_register(cyclic_func_t func,
128 static inline int cyclic_unregister(struct cyclic_info *cyclic)
133 static inline void cyclic_run(void)
137 static inline void schedule(void)
141 static inline int cyclic_init(void)
146 static inline int cyclic_uninit(void)