xref: /optee_os/core/include/drivers/clk_dt.h (revision 12fc37711783247b0d05fdc271ef007f4930767b)
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_driver.h>
10 #include <scattered_array.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 	DEFINE_DT_DRIVER(__name ## _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 TEE_ERROR_ITEM_NOT_FOUND if the DT does not provide a clock reference
43  * Return any other TEE_Result compliant code in case of error
44  */
45 TEE_Result clk_dt_get_by_index(const void *fdt, int nodeoffset,
46 			       unsigned int clk_idx, struct clk **clk);
47 
48 /**
49  * clk_dt_get_by_name - Get a clock matching a name in "clock-names" property
50  *
51  * @fdt: Device tree to work on
52  * @nodeoffset: Node offset of the subnode containing a clock property
53  * @name: Clock name to get
54  * @clk: Output clock reference upon success
55  *
56  * Return TEE_SUCCESS in case of success
57  * Return TEE_ERROR_DEFER_DRIVER_INIT if clock is not initialized
58  * Return TEE_ERROR_ITEM_NOT_FOUND if the DT does not provide a clock reference
59  * Return any other TEE_Result compliant code in case of error
60  */
61 TEE_Result clk_dt_get_by_name(const void *fdt, int nodeoffset,
62 			      const char *name, struct clk **clk);
63 
64 /**
65  * clk_dt_get_func - Typedef of function to get clock from devicetree properties
66  *
67  * @args: Pointer to devicetree description of the clock to parse
68  * @data: Pointer to data given at clk_dt_register_clk_provider() call
69  * @res: Output result code of the operation:
70  *	TEE_SUCCESS in case of success
71  *	TEE_ERROR_DEFER_DRIVER_INIT if clock is not initialized
72  *	Any TEE_Result compliant code in case of error.
73  *
74  * Returns a clk struct pointer pointing to a clock matching the devicetree
75  * description or NULL if invalid description in which case @res provides the
76  * error code.
77  */
78 typedef struct clk *(*clk_dt_get_func)(struct dt_pargs *args, void *data,
79 				       TEE_Result *res);
80 
81 /**
82  * clk_dt_register_clk_provider - Register a clock provider
83  *
84  * @fdt: Device tree to work on
85  * @nodeoffset: Node offset of the clock
86  * @get_dt_clk: Callback to match the devicetree clock with a clock struct
87  * @data: Data which will be passed to the get_dt_clk callback
88  * Returns TEE_Result value
89  */
90 static inline
91 TEE_Result clk_dt_register_clk_provider(const void *fdt, int nodeoffset,
92 					clk_dt_get_func get_dt_clk, void *data)
93 {
94 	return dt_driver_register_provider(fdt, nodeoffset,
95 					   (get_of_device_func)get_dt_clk,
96 					   data, DT_DRIVER_CLK);
97 }
98 
99 /**
100  * clk_dt_get_simple_clk: simple clock matching function for single clock
101  * providers
102  */
103 static inline struct clk *clk_dt_get_simple_clk(struct dt_pargs *args __unused,
104 						void *data, TEE_Result *res)
105 {
106 	*res = TEE_SUCCESS;
107 
108 	return data;
109 }
110 
111 #endif /* __DRIVERS_CLK_DT_H */
112