xref: /optee_os/core/include/drivers/clk_dt.h (revision 45f25897f4bcac14dc13e2adee7f1f96f117fcde)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2021, Bootlin
4  */
5 
6 #ifndef __DRIVERS_CLK_DT_H
7 #define __DRIVERS_CLK_DT_H
8 
9 #include <kernel/dt.h>
10 #include <kernel/dt_driver.h>
11 #include <stdint.h>
12 #include <sys/queue.h>
13 
14 /**
15  * CLK_DT_DECLARE - Declare a clock driver
16  * @__name: Clock driver name
17  * @__compat: Compatible string
18  * @__probe: Clock probe function
19  */
20 #define CLK_DT_DECLARE(__name, __compat, __probe) \
21 	static const struct dt_device_match __name ## _match_table[] = { \
22 		{ .compatible = __compat }, \
23 		{ } \
24 	}; \
25 	const struct dt_driver __name ## _dt_driver __dt_driver = { \
26 		.name = # __name, \
27 		.type = DT_DRIVER_CLK, \
28 		.match_table = __name ## _match_table, \
29 		.probe = __probe, \
30 	}
31 
32 /**
33  * clk_dt_get_by_index - Get a clock at a specific index in "clocks" property
34  *
35  * @fdt: Device tree to work on
36  * @nodeoffset: Node offset of the subnode containing a clock property
37  * @clk_idx: Clock index to get
38  * @clk: Output clock reference upon success
39  *
40  * Return TEE_SUCCESS in case of success
41  * Return TEE_ERROR_DEFER_DRIVER_INIT if clock is not initialized
42  * Return any other TEE_Result compliant code in case of error
43  */
44 TEE_Result clk_dt_get_by_index(const void *fdt, int nodeoffset,
45 			       unsigned int clk_idx, struct clk **clk);
46 
47 /**
48  * clk_dt_get_by_name - Get a clock matching a name in "clock-names" property
49  *
50  * @fdt: Device tree to work on
51  * @nodeoffset: Node offset of the subnode containing a clock property
52  * @name: Clock name to get
53  * @clk: Output clock reference upon success
54  *
55  * Return TEE_SUCCESS in case of success
56  * Return TEE_ERROR_DEFER_DRIVER_INIT if clock is not initialized
57  * Return any other TEE_Result compliant code in case of error
58  */
59 TEE_Result clk_dt_get_by_name(const void *fdt, int nodeoffset,
60 			      const char *name, struct clk **clk);
61 
62 /**
63  * clk_dt_get_func - Typedef of function to get clock from devicetree properties
64  *
65  * @args: Pointer to devicetree description of the clock to parse
66  * @data: Pointer to data given at clk_dt_register_clk_provider() call
67  * @res: Output result code of the operation:
68  *	TEE_SUCCESS in case of success
69  *	TEE_ERROR_DEFER_DRIVER_INIT if clock is not initialized
70  *	Any TEE_Result compliant code in case of error.
71  *
72  * Returns a clk struct pointer pointing to a clock matching the devicetree
73  * description or NULL if invalid description in which case @res provides the
74  * error code.
75  */
76 typedef struct clk *(*clk_dt_get_func)(struct dt_driver_phandle_args *args,
77 				       void *data, TEE_Result *res);
78 
79 /**
80  * clk_dt_register_clk_provider - Register a clock provider
81  *
82  * @fdt: Device tree to work on
83  * @nodeoffset: Node offset of the clock
84  * @get_dt_clk: Callback to match the devicetree clock with a clock struct
85  * @data: Data which will be passed to the get_dt_clk callback
86  * Returns TEE_Result value
87  */
88 static inline
89 TEE_Result clk_dt_register_clk_provider(const void *fdt, int nodeoffset,
90 					clk_dt_get_func get_dt_clk, void *data)
91 {
92 	return dt_driver_register_provider(fdt, nodeoffset,
93 					   (get_of_device_func)get_dt_clk,
94 					   data, DT_DRIVER_CLK);
95 }
96 
97 /**
98  * clk_dt_get_simple_clk: simple clock matching function for single clock
99  * providers
100  */
101 static inline
102 struct clk *clk_dt_get_simple_clk(struct dt_driver_phandle_args *args __unused,
103 				  void *data, TEE_Result *res)
104 {
105 	*res = TEE_SUCCESS;
106 
107 	return data;
108 }
109 
110 #endif /* __DRIVERS_CLK_DT_H */
111