xref: /optee_os/core/include/kernel/dt_driver.h (revision e3e4ce47ea3cac1746cdd860d4710208b4c40ceb)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  * Copyright (c) 2021, Bootlin
5  */
6 
7 #ifndef __DT_DRIVER_H
8 #define __DT_DRIVER_H
9 
10 #include <kernel/dt.h>
11 #include <stdint.h>
12 #include <sys/queue.h>
13 #include <tee_api_types.h>
14 
15 /* Opaque reference to DT driver device provider instance */
16 struct dt_driver_provider;
17 
18 /**
19  * struct dt_driver_phandle_args - Devicetree phandle arguments
20  * @args_count: Count of cells for the device
21  * @args: Device consumer specifiers
22  */
23 struct dt_driver_phandle_args {
24 	int args_count;
25 	uint32_t args[];
26 };
27 
28 /*
29  * get_of_device_func - Callback function for returning a driver private
30  *	instance based on a FDT phandle with possible arguments and the
31  *	registered dt_driver private data reference.
32  *
33  * @parg: phandle argument(s) referencing the device in the FDT.
34  * @data: driver private data registered in struct dt_driver.
35  *
36  * Return a device opaque reference, e.g. a struct clk pointer for a clock
37  * driver, or NULL if not found.
38  */
39 typedef void *(*get_of_device_func)(struct dt_driver_phandle_args *parg,
40 				    void *data);
41 
42 /**
43  * dt_driver_register_provider - Register a driver provider
44  *
45  * @fdt: Device tree to work on
46  * @nodeoffset: Node offset in the FDT
47  * @get_of_device: Function to match the devicetree with a device instance
48  * @data: Data which will be passed to the @get_of_device callback
49  * @type: Driver type
50  *
51  * @get_of_device returns a void *. Driver provider is expected to
52  * include a shim helper to cast to device reference into provider driver
53  * target structure reference (e.g (struct clk *) for clock devices).
54  */
55 TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset,
56 				       get_of_device_func get_of_device,
57 				       void *data, enum dt_driver_type type);
58 
59 /*
60  * dt_driver_device_from_node_idx_prop - Return a device instance based on a
61  *	property name and FDT information
62  *
63  * @prop_name: DT property name, e.g. "clocks" for clock resources
64  * @fdt: FDT base address
65  * @nodeoffset: node offset in the FDT
66  * @prop_idx: index of the phandle data in the property
67  *
68  * Return a device opaque reference, e.g. a struct clk pointer for a clock
69  * driver, or NULL if not found.
70  */
71 void *dt_driver_device_from_node_idx_prop(const char *prop_name,
72 					  const void *fdt, int nodeoffset,
73 					  unsigned int prop_idx);
74 
75 /*
76  * Return driver provider reference from its node offset value in the FDT
77  */
78 struct dt_driver_provider *dt_driver_get_provider_by_node(int nodeoffset);
79 
80 /*
81  * Return driver provider reference from its phandle value in the FDT
82  */
83 struct dt_driver_provider *dt_driver_get_provider_by_phandle(uint32_t phandle);
84 
85 /*
86  * Return number cells used for phandle arguments by a driver provider
87  */
88 unsigned int dt_driver_provider_cells(struct dt_driver_provider *prv);
89 
90 /*
91  * dt_driver_probe_device_by_node - Probe matching driver to create a device
92  *	from a FDT node
93  *
94  * @fdt: FDT base address
95  * @nodeoffset: Node byte offset from FDT base
96  * @type: Target driver to match or DT_DRIVER_ANY
97  *
98  * Read the dt_driver database. Compatible list is looked up in the order
99  * of the FDT "compatible" property list. @type can be used to probe only
100  * specific drivers.
101  *
102  */
103 TEE_Result dt_driver_probe_device_by_node(const void *fdt, int nodeoffset,
104 					  enum dt_driver_type type);
105 
106 /*
107  * Get cells count of a device node given its dt_driver type
108  *
109  * @fdt: FDT base address
110  * @nodeoffset: Node offset on the FDT for the device
111  * @type: One of the supported DT_DRIVER_* value.
112  *
113  * Currently supports type DT_DRIVER_CLK.
114  * Return a positive cell count value (>= 0) or a negative FDT_ error code
115  */
116 int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset,
117 			    enum dt_driver_type type);
118 #endif /* __DT_DRIVER_H */
119