xref: /rk3399_rockchip-uboot/include/dm/device.h (revision ae7f4513087e7f7996cebc9db642917dde9ea561)
16494d708SSimon Glass /*
26494d708SSimon Glass  * Copyright (c) 2013 Google, Inc
36494d708SSimon Glass  *
46494d708SSimon Glass  * (C) Copyright 2012
56494d708SSimon Glass  * Pavel Herrmann <morpheus.ibis@gmail.com>
66494d708SSimon Glass  * Marek Vasut <marex@denx.de>
76494d708SSimon Glass  *
86494d708SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
96494d708SSimon Glass  */
106494d708SSimon Glass 
116494d708SSimon Glass #ifndef _DM_DEVICE_H
126494d708SSimon Glass #define _DM_DEVICE_H
136494d708SSimon Glass 
146494d708SSimon Glass #include <dm/uclass-id.h>
156494d708SSimon Glass #include <linker_lists.h>
166494d708SSimon Glass #include <linux/list.h>
176494d708SSimon Glass 
186494d708SSimon Glass struct driver_info;
196494d708SSimon Glass 
206494d708SSimon Glass /* Driver is active (probed). Cleared when it is removed */
216494d708SSimon Glass #define DM_FLAG_ACTIVATED	(1 << 0)
226494d708SSimon Glass 
236494d708SSimon Glass /* DM is responsible for allocating and freeing platdata */
246494d708SSimon Glass #define DM_FLAG_ALLOC_PDATA	(2 << 0)
256494d708SSimon Glass 
266494d708SSimon Glass /**
2754c5d08aSHeiko Schocher  * struct udevice - An instance of a driver
286494d708SSimon Glass  *
296494d708SSimon Glass  * This holds information about a device, which is a driver bound to a
306494d708SSimon Glass  * particular port or peripheral (essentially a driver instance).
316494d708SSimon Glass  *
326494d708SSimon Glass  * A device will come into existence through a 'bind' call, either due to
336494d708SSimon Glass  * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
346494d708SSimon Glass  * in the device tree (in which case of_offset is >= 0). In the latter case
356494d708SSimon Glass  * we translate the device tree information into platdata in a function
366494d708SSimon Glass  * implemented by the driver ofdata_to_platdata method (called just before the
376494d708SSimon Glass  * probe method if the device has a device tree node.
386494d708SSimon Glass  *
396494d708SSimon Glass  * All three of platdata, priv and uclass_priv can be allocated by the
406494d708SSimon Glass  * driver, or you can use the auto_alloc_size members of struct driver and
416494d708SSimon Glass  * struct uclass_driver to have driver model do this automatically.
426494d708SSimon Glass  *
436494d708SSimon Glass  * @driver: The driver used by this device
446494d708SSimon Glass  * @name: Name of device, typically the FDT node name
456494d708SSimon Glass  * @platdata: Configuration data for this device
466494d708SSimon Glass  * @of_offset: Device tree node offset for this device (- for none)
476494d708SSimon Glass  * @parent: Parent of this device, or NULL for the top level device
486494d708SSimon Glass  * @priv: Private data for this device
496494d708SSimon Glass  * @uclass: Pointer to uclass for this device
506494d708SSimon Glass  * @uclass_priv: The uclass's private data for this device
516494d708SSimon Glass  * @uclass_node: Used by uclass to link its devices
526494d708SSimon Glass  * @child_head: List of children of this device
536494d708SSimon Glass  * @sibling_node: Next device in list of all devices
546494d708SSimon Glass  * @flags: Flags for this device DM_FLAG_...
556494d708SSimon Glass  */
5654c5d08aSHeiko Schocher struct udevice {
576494d708SSimon Glass 	struct driver *driver;
586494d708SSimon Glass 	const char *name;
596494d708SSimon Glass 	void *platdata;
606494d708SSimon Glass 	int of_offset;
6154c5d08aSHeiko Schocher 	struct udevice *parent;
626494d708SSimon Glass 	void *priv;
636494d708SSimon Glass 	struct uclass *uclass;
646494d708SSimon Glass 	void *uclass_priv;
656494d708SSimon Glass 	struct list_head uclass_node;
666494d708SSimon Glass 	struct list_head child_head;
676494d708SSimon Glass 	struct list_head sibling_node;
686494d708SSimon Glass 	uint32_t flags;
696494d708SSimon Glass };
706494d708SSimon Glass 
716494d708SSimon Glass /* Returns the operations for a device */
726494d708SSimon Glass #define device_get_ops(dev)	(dev->driver->ops)
736494d708SSimon Glass 
746494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */
756494d708SSimon Glass #define device_active(dev)	((dev)->flags & DM_FLAG_ACTIVATED)
766494d708SSimon Glass 
776494d708SSimon Glass /**
78*ae7f4513SSimon Glass  * struct udevice_id - Lists the compatible strings supported by a driver
796494d708SSimon Glass  * @compatible: Compatible string
806494d708SSimon Glass  * @data: Data for this compatible string
816494d708SSimon Glass  */
82*ae7f4513SSimon Glass struct udevice_id {
836494d708SSimon Glass 	const char *compatible;
846494d708SSimon Glass 	ulong data;
856494d708SSimon Glass };
866494d708SSimon Glass 
876494d708SSimon Glass /**
886494d708SSimon Glass  * struct driver - A driver for a feature or peripheral
896494d708SSimon Glass  *
906494d708SSimon Glass  * This holds methods for setting up a new device, and also removing it.
916494d708SSimon Glass  * The device needs information to set itself up - this is provided either
926494d708SSimon Glass  * by platdata or a device tree node (which we find by looking up
936494d708SSimon Glass  * matching compatible strings with of_match).
946494d708SSimon Glass  *
956494d708SSimon Glass  * Drivers all belong to a uclass, representing a class of devices of the
966494d708SSimon Glass  * same type. Common elements of the drivers can be implemented in the uclass,
976494d708SSimon Glass  * or the uclass can provide a consistent interface to the drivers within
986494d708SSimon Glass  * it.
996494d708SSimon Glass  *
1006494d708SSimon Glass  * @name: Device name
1016494d708SSimon Glass  * @id: Identiies the uclass we belong to
1026494d708SSimon Glass  * @of_match: List of compatible strings to match, and any identifying data
1036494d708SSimon Glass  * for each.
1046494d708SSimon Glass  * @bind: Called to bind a device to its driver
1056494d708SSimon Glass  * @probe: Called to probe a device, i.e. activate it
1066494d708SSimon Glass  * @remove: Called to remove a device, i.e. de-activate it
1076494d708SSimon Glass  * @unbind: Called to unbind a device from its driver
1086494d708SSimon Glass  * @ofdata_to_platdata: Called before probe to decode device tree data
1096494d708SSimon Glass  * @priv_auto_alloc_size: If non-zero this is the size of the private data
1106494d708SSimon Glass  * to be allocated in the device's ->priv pointer. If zero, then the driver
1116494d708SSimon Glass  * is responsible for allocating any data required.
1126494d708SSimon Glass  * @platdata_auto_alloc_size: If non-zero this is the size of the
1136494d708SSimon Glass  * platform data to be allocated in the device's ->platdata pointer.
1146494d708SSimon Glass  * This is typically only useful for device-tree-aware drivers (those with
1156494d708SSimon Glass  * an of_match), since drivers which use platdata will have the data
1166494d708SSimon Glass  * provided in the U_BOOT_DEVICE() instantiation.
1176494d708SSimon Glass  * ops: Driver-specific operations. This is typically a list of function
1186494d708SSimon Glass  * pointers defined by the driver, to implement driver functions required by
1196494d708SSimon Glass  * the uclass.
1206494d708SSimon Glass  */
1216494d708SSimon Glass struct driver {
1226494d708SSimon Glass 	char *name;
1236494d708SSimon Glass 	enum uclass_id id;
124*ae7f4513SSimon Glass 	const struct udevice_id *of_match;
12554c5d08aSHeiko Schocher 	int (*bind)(struct udevice *dev);
12654c5d08aSHeiko Schocher 	int (*probe)(struct udevice *dev);
12754c5d08aSHeiko Schocher 	int (*remove)(struct udevice *dev);
12854c5d08aSHeiko Schocher 	int (*unbind)(struct udevice *dev);
12954c5d08aSHeiko Schocher 	int (*ofdata_to_platdata)(struct udevice *dev);
1306494d708SSimon Glass 	int priv_auto_alloc_size;
1316494d708SSimon Glass 	int platdata_auto_alloc_size;
1326494d708SSimon Glass 	const void *ops;	/* driver-specific operations */
1336494d708SSimon Glass };
1346494d708SSimon Glass 
1356494d708SSimon Glass /* Declare a new U-Boot driver */
1366494d708SSimon Glass #define U_BOOT_DRIVER(__name)						\
1376494d708SSimon Glass 	ll_entry_declare(struct driver, __name, driver)
1386494d708SSimon Glass 
1396494d708SSimon Glass /**
1406494d708SSimon Glass  * dev_get_platdata() - Get the platform data for a device
1416494d708SSimon Glass  *
1426494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
1436494d708SSimon Glass  *
1446494d708SSimon Glass  * @dev		Device to check
1456494d708SSimon Glass  * @return platform data, or NULL if none
1466494d708SSimon Glass  */
14754c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev);
1486494d708SSimon Glass 
1496494d708SSimon Glass /**
1506494d708SSimon Glass  * dev_get_priv() - Get the private data for a device
1516494d708SSimon Glass  *
1526494d708SSimon Glass  * This checks that dev is not NULL, but no other checks for now
1536494d708SSimon Glass  *
1546494d708SSimon Glass  * @dev		Device to check
1556494d708SSimon Glass  * @return private data, or NULL if none
1566494d708SSimon Glass  */
15754c5d08aSHeiko Schocher void *dev_get_priv(struct udevice *dev);
1586494d708SSimon Glass 
1596494d708SSimon Glass #endif
160