blob: 6005e7eeb5ab9e6d2b48de323c747482d0806c63 [file] [log] [blame]
Simon Glass6494d702014-02-26 15:59:18 -07001/*
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
18struct 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 */
Simon Glassf2bc6fc2014-06-11 23:29:54 -060024#define DM_FLAG_ALLOC_PDATA (1 << 1)
Simon Glass6494d702014-02-26 15:59:18 -070025
Simon Glass00606d72014-07-23 06:55:03 -060026/* DM should init this device prior to relocation */
27#define DM_FLAG_PRE_RELOC (1 << 2)
28
Simon Glass6494d702014-02-26 15:59:18 -070029/**
Heiko Schocher54c5d082014-05-22 12:43:05 +020030 * struct udevice - An instance of a driver
Simon Glass6494d702014-02-26 15:59:18 -070031 *
32 * This holds information about a device, which is a driver bound to a
33 * particular port or peripheral (essentially a driver instance).
34 *
35 * A device will come into existence through a 'bind' call, either due to
36 * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
37 * in the device tree (in which case of_offset is >= 0). In the latter case
38 * we translate the device tree information into platdata in a function
39 * implemented by the driver ofdata_to_platdata method (called just before the
40 * probe method if the device has a device tree node.
41 *
42 * All three of platdata, priv and uclass_priv can be allocated by the
43 * driver, or you can use the auto_alloc_size members of struct driver and
44 * struct uclass_driver to have driver model do this automatically.
45 *
46 * @driver: The driver used by this device
47 * @name: Name of device, typically the FDT node name
48 * @platdata: Configuration data for this device
49 * @of_offset: Device tree node offset for this device (- for none)
50 * @parent: Parent of this device, or NULL for the top level device
51 * @priv: Private data for this device
52 * @uclass: Pointer to uclass for this device
53 * @uclass_priv: The uclass's private data for this device
54 * @uclass_node: Used by uclass to link its devices
55 * @child_head: List of children of this device
56 * @sibling_node: Next device in list of all devices
57 * @flags: Flags for this device DM_FLAG_...
Simon Glass5a66a8f2014-07-23 06:55:12 -060058 * @req_seq: Requested sequence number for this device (-1 = any)
59 * @seq: Allocated sequence number for this device (-1 = none)
Simon Glass6494d702014-02-26 15:59:18 -070060 */
Heiko Schocher54c5d082014-05-22 12:43:05 +020061struct udevice {
Simon Glass6494d702014-02-26 15:59:18 -070062 struct driver *driver;
63 const char *name;
64 void *platdata;
65 int of_offset;
Heiko Schocher54c5d082014-05-22 12:43:05 +020066 struct udevice *parent;
Simon Glass6494d702014-02-26 15:59:18 -070067 void *priv;
68 struct uclass *uclass;
69 void *uclass_priv;
70 struct list_head uclass_node;
71 struct list_head child_head;
72 struct list_head sibling_node;
73 uint32_t flags;
Simon Glass5a66a8f2014-07-23 06:55:12 -060074 int req_seq;
75 int seq;
Simon Glass6494d702014-02-26 15:59:18 -070076};
77
Simon Glass5a66a8f2014-07-23 06:55:12 -060078/* Maximum sequence number supported */
79#define DM_MAX_SEQ 999
80
Simon Glass6494d702014-02-26 15:59:18 -070081/* Returns the operations for a device */
82#define device_get_ops(dev) (dev->driver->ops)
83
84/* Returns non-zero if the device is active (probed and not removed) */
85#define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
86
87/**
Simon Glassae7f4512014-06-11 23:29:45 -060088 * struct udevice_id - Lists the compatible strings supported by a driver
Simon Glass6494d702014-02-26 15:59:18 -070089 * @compatible: Compatible string
90 * @data: Data for this compatible string
91 */
Simon Glassae7f4512014-06-11 23:29:45 -060092struct udevice_id {
Simon Glass6494d702014-02-26 15:59:18 -070093 const char *compatible;
94 ulong data;
95};
96
97/**
98 * struct driver - A driver for a feature or peripheral
99 *
100 * This holds methods for setting up a new device, and also removing it.
101 * The device needs information to set itself up - this is provided either
102 * by platdata or a device tree node (which we find by looking up
103 * matching compatible strings with of_match).
104 *
105 * Drivers all belong to a uclass, representing a class of devices of the
106 * same type. Common elements of the drivers can be implemented in the uclass,
107 * or the uclass can provide a consistent interface to the drivers within
108 * it.
109 *
110 * @name: Device name
111 * @id: Identiies the uclass we belong to
112 * @of_match: List of compatible strings to match, and any identifying data
113 * for each.
114 * @bind: Called to bind a device to its driver
115 * @probe: Called to probe a device, i.e. activate it
116 * @remove: Called to remove a device, i.e. de-activate it
117 * @unbind: Called to unbind a device from its driver
118 * @ofdata_to_platdata: Called before probe to decode device tree data
119 * @priv_auto_alloc_size: If non-zero this is the size of the private data
120 * to be allocated in the device's ->priv pointer. If zero, then the driver
121 * is responsible for allocating any data required.
122 * @platdata_auto_alloc_size: If non-zero this is the size of the
123 * platform data to be allocated in the device's ->platdata pointer.
124 * This is typically only useful for device-tree-aware drivers (those with
125 * an of_match), since drivers which use platdata will have the data
126 * provided in the U_BOOT_DEVICE() instantiation.
127 * ops: Driver-specific operations. This is typically a list of function
128 * pointers defined by the driver, to implement driver functions required by
129 * the uclass.
Simon Glass00606d72014-07-23 06:55:03 -0600130 * @flags: driver flags - see DM_FLAGS_...
Simon Glass6494d702014-02-26 15:59:18 -0700131 */
132struct driver {
133 char *name;
134 enum uclass_id id;
Simon Glassae7f4512014-06-11 23:29:45 -0600135 const struct udevice_id *of_match;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200136 int (*bind)(struct udevice *dev);
137 int (*probe)(struct udevice *dev);
138 int (*remove)(struct udevice *dev);
139 int (*unbind)(struct udevice *dev);
140 int (*ofdata_to_platdata)(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700141 int priv_auto_alloc_size;
142 int platdata_auto_alloc_size;
143 const void *ops; /* driver-specific operations */
Simon Glass00606d72014-07-23 06:55:03 -0600144 uint32_t flags;
Simon Glass6494d702014-02-26 15:59:18 -0700145};
146
147/* Declare a new U-Boot driver */
148#define U_BOOT_DRIVER(__name) \
149 ll_entry_declare(struct driver, __name, driver)
150
151/**
152 * dev_get_platdata() - Get the platform data for a device
153 *
154 * This checks that dev is not NULL, but no other checks for now
155 *
156 * @dev Device to check
157 * @return platform data, or NULL if none
158 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200159void *dev_get_platdata(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700160
161/**
162 * dev_get_priv() - Get the private data for a device
163 *
164 * This checks that dev is not NULL, but no other checks for now
165 *
166 * @dev Device to check
167 * @return private data, or NULL if none
168 */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200169void *dev_get_priv(struct udevice *dev);
Simon Glass6494d702014-02-26 15:59:18 -0700170
Simon Glass5a66a8f2014-07-23 06:55:12 -0600171/**
172 * device_find_child_by_seq() - Find a child device based on a sequence
173 *
174 * This searches for a device with the given seq or req_seq.
175 *
176 * For seq, if an active device has this sequence it will be returned.
177 * If there is no such device then this will return -ENODEV.
178 *
179 * For req_seq, if a device (whether activated or not) has this req_seq
180 * value, that device will be returned. This is a strong indication that
181 * the device will receive that sequence when activated.
182 *
183 * @parent: Parent device
184 * @seq_or_req_seq: Sequence number to find (0=first)
185 * @find_req_seq: true to find req_seq, false to find seq
186 * @devp: Returns pointer to device (there is only one per for each seq).
187 * Set to NULL if none is found
188 * @return 0 if OK, -ve on error
189 */
190int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
191 bool find_req_seq, struct udevice **devp);
192
Simon Glass6494d702014-02-26 15:59:18 -0700193#endif