xref: /openbmc/u-boot/include/dm/device.h (revision 9fc2ed40)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * (C) Copyright 2012
5  * Pavel Herrmann <morpheus.ibis@gmail.com>
6  * Marek Vasut <marex@denx.de>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #ifndef _DM_DEVICE_H
12 #define _DM_DEVICE_H
13 
14 #include <dm/uclass-id.h>
15 #include <linker_lists.h>
16 #include <linux/list.h>
17 
18 struct driver_info;
19 
20 /* Driver is active (probed). Cleared when it is removed */
21 #define DM_FLAG_ACTIVATED	(1 << 0)
22 
23 /* DM is responsible for allocating and freeing platdata */
24 #define DM_FLAG_ALLOC_PDATA	(2 << 0)
25 
26 /**
27  * struct device - An instance of a driver
28  *
29  * This holds information about a device, which is a driver bound to a
30  * particular port or peripheral (essentially a driver instance).
31  *
32  * A device will come into existence through a 'bind' call, either due to
33  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
34  * in the device tree (in which case of_offset is >= 0). In the latter case
35  * we translate the device tree information into platdata in a function
36  * implemented by the driver ofdata_to_platdata method (called just before the
37  * probe method if the device has a device tree node.
38  *
39  * All three of platdata, priv and uclass_priv can be allocated by the
40  * driver, or you can use the auto_alloc_size members of struct driver and
41  * struct uclass_driver to have driver model do this automatically.
42  *
43  * @driver: The driver used by this device
44  * @name: Name of device, typically the FDT node name
45  * @platdata: Configuration data for this device
46  * @of_offset: Device tree node offset for this device (- for none)
47  * @parent: Parent of this device, or NULL for the top level device
48  * @priv: Private data for this device
49  * @uclass: Pointer to uclass for this device
50  * @uclass_priv: The uclass's private data for this device
51  * @uclass_node: Used by uclass to link its devices
52  * @child_head: List of children of this device
53  * @sibling_node: Next device in list of all devices
54  * @flags: Flags for this device DM_FLAG_...
55  */
56 struct device {
57 	struct driver *driver;
58 	const char *name;
59 	void *platdata;
60 	int of_offset;
61 	struct device *parent;
62 	void *priv;
63 	struct uclass *uclass;
64 	void *uclass_priv;
65 	struct list_head uclass_node;
66 	struct list_head child_head;
67 	struct list_head sibling_node;
68 	uint32_t flags;
69 };
70 
71 /* Returns the operations for a device */
72 #define device_get_ops(dev)	(dev->driver->ops)
73 
74 /* Returns non-zero if the device is active (probed and not removed) */
75 #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
76 
77 /**
78  * struct device_id - Lists the compatible strings supported by a driver
79  * @compatible: Compatible string
80  * @data: Data for this compatible string
81  */
82 struct device_id {
83 	const char *compatible;
84 	ulong data;
85 };
86 
87 /**
88  * struct driver - A driver for a feature or peripheral
89  *
90  * This holds methods for setting up a new device, and also removing it.
91  * The device needs information to set itself up - this is provided either
92  * by platdata or a device tree node (which we find by looking up
93  * matching compatible strings with of_match).
94  *
95  * Drivers all belong to a uclass, representing a class of devices of the
96  * same type. Common elements of the drivers can be implemented in the uclass,
97  * or the uclass can provide a consistent interface to the drivers within
98  * it.
99  *
100  * @name: Device name
101  * @id: Identiies the uclass we belong to
102  * @of_match: List of compatible strings to match, and any identifying data
103  * for each.
104  * @bind: Called to bind a device to its driver
105  * @probe: Called to probe a device, i.e. activate it
106  * @remove: Called to remove a device, i.e. de-activate it
107  * @unbind: Called to unbind a device from its driver
108  * @ofdata_to_platdata: Called before probe to decode device tree data
109  * @priv_auto_alloc_size: If non-zero this is the size of the private data
110  * to be allocated in the device's ->priv pointer. If zero, then the driver
111  * is responsible for allocating any data required.
112  * @platdata_auto_alloc_size: If non-zero this is the size of the
113  * platform data to be allocated in the device's ->platdata pointer.
114  * This is typically only useful for device-tree-aware drivers (those with
115  * an of_match), since drivers which use platdata will have the data
116  * provided in the U_BOOT_DEVICE() instantiation.
117  * ops: Driver-specific operations. This is typically a list of function
118  * pointers defined by the driver, to implement driver functions required by
119  * the uclass.
120  */
121 struct driver {
122 	char *name;
123 	enum uclass_id id;
124 	const struct device_id *of_match;
125 	int (*bind)(struct device *dev);
126 	int (*probe)(struct device *dev);
127 	int (*remove)(struct device *dev);
128 	int (*unbind)(struct device *dev);
129 	int (*ofdata_to_platdata)(struct device *dev);
130 	int priv_auto_alloc_size;
131 	int platdata_auto_alloc_size;
132 	const void *ops;	/* driver-specific operations */
133 };
134 
135 /* Declare a new U-Boot driver */
136 #define U_BOOT_DRIVER(__name)						\
137 	ll_entry_declare(struct driver, __name, driver)
138 
139 /**
140  * dev_get_platdata() - Get the platform data for a device
141  *
142  * This checks that dev is not NULL, but no other checks for now
143  *
144  * @dev		Device to check
145  * @return platform data, or NULL if none
146  */
147 void *dev_get_platdata(struct device *dev);
148 
149 /**
150  * dev_get_priv() - Get the private data for a device
151  *
152  * This checks that dev is not NULL, but no other checks for now
153  *
154  * @dev		Device to check
155  * @return private data, or NULL if none
156  */
157 void *dev_get_priv(struct device *dev);
158 
159 #endif
160