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 * 76494d708SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 86494d708SSimon Glass */ 96494d708SSimon Glass 106494d708SSimon Glass #ifndef _DM_UCLASS_H 116494d708SSimon Glass #define _DM_UCLASS_H 126494d708SSimon Glass 136494d708SSimon Glass #include <dm/uclass-id.h> 1442c23dd2SMasahiro Yamada #include <linker_lists.h> 156494d708SSimon Glass #include <linux/list.h> 166494d708SSimon Glass 176494d708SSimon Glass /** 186494d708SSimon Glass * struct uclass - a U-Boot drive class, collecting together similar drivers 196494d708SSimon Glass * 206494d708SSimon Glass * A uclass provides an interface to a particular function, which is 216494d708SSimon Glass * implemented by one or more drivers. Every driver belongs to a uclass even 226494d708SSimon Glass * if it is the only driver in that uclass. An example uclass is GPIO, which 236494d708SSimon Glass * provides the ability to change read inputs, set and clear outputs, etc. 246494d708SSimon Glass * There may be drivers for on-chip SoC GPIO banks, I2C GPIO expanders and 256494d708SSimon Glass * PMIC IO lines, all made available in a unified way through the uclass. 266494d708SSimon Glass * 276494d708SSimon Glass * @priv: Private data for this uclass 286494d708SSimon Glass * @uc_drv: The driver for the uclass itself, not to be confused with a 296494d708SSimon Glass * 'struct driver' 30f2bc6fc3SSimon Glass * @dev_head: List of devices in this uclass (devices are attached to their 316494d708SSimon Glass * uclass when their bind method is called) 326494d708SSimon Glass * @sibling_node: Next uclass in the linked list of uclasses 336494d708SSimon Glass */ 346494d708SSimon Glass struct uclass { 356494d708SSimon Glass void *priv; 366494d708SSimon Glass struct uclass_driver *uc_drv; 376494d708SSimon Glass struct list_head dev_head; 386494d708SSimon Glass struct list_head sibling_node; 396494d708SSimon Glass }; 406494d708SSimon Glass 4154c5d08aSHeiko Schocher struct udevice; 426494d708SSimon Glass 439cc36a2bSSimon Glass /* Members of this uclass sequence themselves with aliases */ 449cc36a2bSSimon Glass #define DM_UC_FLAG_SEQ_ALIAS (1 << 0) 459cc36a2bSSimon Glass 466494d708SSimon Glass /** 476494d708SSimon Glass * struct uclass_driver - Driver for the uclass 486494d708SSimon Glass * 496494d708SSimon Glass * A uclass_driver provides a consistent interface to a set of related 506494d708SSimon Glass * drivers. 516494d708SSimon Glass * 526494d708SSimon Glass * @name: Name of uclass driver 536494d708SSimon Glass * @id: ID number of this uclass 546494d708SSimon Glass * @post_bind: Called after a new device is bound to this uclass 556494d708SSimon Glass * @pre_unbind: Called before a device is unbound from this uclass 5602c07b37SSimon Glass * @pre_probe: Called before a new device is probed 576494d708SSimon Glass * @post_probe: Called after a new device is probed 586494d708SSimon Glass * @pre_remove: Called before a device is removed 59081f2fcbSSimon Glass * @child_post_bind: Called after a child is bound to a device in this uclass 606494d708SSimon Glass * @init: Called to set up the uclass 616494d708SSimon Glass * @destroy: Called to destroy the uclass 626494d708SSimon Glass * @priv_auto_alloc_size: If non-zero this is the size of the private data 636494d708SSimon Glass * to be allocated in the uclass's ->priv pointer. If zero, then the uclass 646494d708SSimon Glass * driver is responsible for allocating any data required. 656494d708SSimon Glass * @per_device_auto_alloc_size: Each device can hold private data owned 666494d708SSimon Glass * by the uclass. If required this will be automatically allocated if this 676494d708SSimon Glass * value is non-zero. 685eaed880SPrzemyslaw Marczak * @per_device_platdata_auto_alloc_size: Each device can hold platform data 695eaed880SPrzemyslaw Marczak * owned by the uclass as 'dev->uclass_platdata'. If the value is non-zero, 705eaed880SPrzemyslaw Marczak * then this will be automatically allocated. 71dac8db2cSSimon Glass * @per_child_auto_alloc_size: Each child device (of a parent in this 72dac8db2cSSimon Glass * uclass) can hold parent data for the device/uclass. This value is only 73dac8db2cSSimon Glass * used as a falback if this member is 0 in the driver. 74ba8da9dcSSimon Glass * @per_child_platdata_auto_alloc_size: A bus likes to store information about 75ba8da9dcSSimon Glass * its children. If non-zero this is the size of this data, to be allocated 76ba8da9dcSSimon Glass * in the child device's parent_platdata pointer. This value is only used as 77ba8da9dcSSimon Glass * a falback if this member is 0 in the driver. 786494d708SSimon Glass * @ops: Uclass operations, providing the consistent interface to devices 796494d708SSimon Glass * within the uclass. 809cc36a2bSSimon Glass * @flags: Flags for this uclass (DM_UC_...) 816494d708SSimon Glass */ 826494d708SSimon Glass struct uclass_driver { 836494d708SSimon Glass const char *name; 846494d708SSimon Glass enum uclass_id id; 8554c5d08aSHeiko Schocher int (*post_bind)(struct udevice *dev); 8654c5d08aSHeiko Schocher int (*pre_unbind)(struct udevice *dev); 8702c07b37SSimon Glass int (*pre_probe)(struct udevice *dev); 8854c5d08aSHeiko Schocher int (*post_probe)(struct udevice *dev); 8954c5d08aSHeiko Schocher int (*pre_remove)(struct udevice *dev); 90081f2fcbSSimon Glass int (*child_post_bind)(struct udevice *dev); 9183c7e434SSimon Glass int (*child_pre_probe)(struct udevice *dev); 926494d708SSimon Glass int (*init)(struct uclass *class); 936494d708SSimon Glass int (*destroy)(struct uclass *class); 946494d708SSimon Glass int priv_auto_alloc_size; 956494d708SSimon Glass int per_device_auto_alloc_size; 965eaed880SPrzemyslaw Marczak int per_device_platdata_auto_alloc_size; 97dac8db2cSSimon Glass int per_child_auto_alloc_size; 98ba8da9dcSSimon Glass int per_child_platdata_auto_alloc_size; 996494d708SSimon Glass const void *ops; 1009cc36a2bSSimon Glass uint32_t flags; 1016494d708SSimon Glass }; 1026494d708SSimon Glass 1036494d708SSimon Glass /* Declare a new uclass_driver */ 1046494d708SSimon Glass #define UCLASS_DRIVER(__name) \ 1056494d708SSimon Glass ll_entry_declare(struct uclass_driver, __name, uclass) 1066494d708SSimon Glass 1076494d708SSimon Glass /** 1086494d708SSimon Glass * uclass_get() - Get a uclass based on an ID, creating it if needed 1096494d708SSimon Glass * 1106494d708SSimon Glass * Every uclass is identified by an ID, a number from 0 to n-1 where n is 1116494d708SSimon Glass * the number of uclasses. This function allows looking up a uclass by its 1126494d708SSimon Glass * ID. 1136494d708SSimon Glass * 1146494d708SSimon Glass * @key: ID to look up 1156494d708SSimon Glass * @ucp: Returns pointer to uclass (there is only one per ID) 1166494d708SSimon Glass * @return 0 if OK, -ve on error 1176494d708SSimon Glass */ 1186494d708SSimon Glass int uclass_get(enum uclass_id key, struct uclass **ucp); 1196494d708SSimon Glass 1206494d708SSimon Glass /** 1216494d708SSimon Glass * uclass_get_device() - Get a uclass device based on an ID and index 1226494d708SSimon Glass * 123f2bc6fc3SSimon Glass * The device is probed to activate it ready for use. 124f2bc6fc3SSimon Glass * 1250040b944SSimon Glass * @id: ID to look up 1266494d708SSimon Glass * @index: Device number within that uclass (0=first) 127f2bc6fc3SSimon Glass * @devp: Returns pointer to device (there is only one per for each ID) 1286494d708SSimon Glass * @return 0 if OK, -ve on error 1296494d708SSimon Glass */ 130f2bc6fc3SSimon Glass int uclass_get_device(enum uclass_id id, int index, struct udevice **devp); 1316494d708SSimon Glass 1326494d708SSimon Glass /** 13374356d7fSSimon Glass * uclass_get_device_by_name() - Get a uclass device by its name 134b7af1a2dSPrzemyslaw Marczak * 135a7b82502SPrzemyslaw Marczak * This searches the devices in the uclass for one with the exactly given name. 136b7af1a2dSPrzemyslaw Marczak * 137b7af1a2dSPrzemyslaw Marczak * The device is probed to activate it ready for use. 138b7af1a2dSPrzemyslaw Marczak * 139b7af1a2dSPrzemyslaw Marczak * @id: ID to look up 140b7af1a2dSPrzemyslaw Marczak * @name: name of a device to get 141b7af1a2dSPrzemyslaw Marczak * @devp: Returns pointer to device (the first one with the name) 142b7af1a2dSPrzemyslaw Marczak * @return 0 if OK, -ve on error 143b7af1a2dSPrzemyslaw Marczak */ 144b7af1a2dSPrzemyslaw Marczak int uclass_get_device_by_name(enum uclass_id id, const char *name, 145b7af1a2dSPrzemyslaw Marczak struct udevice **devp); 146b7af1a2dSPrzemyslaw Marczak 147b7af1a2dSPrzemyslaw Marczak /** 1485a66a8ffSSimon Glass * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence 1495a66a8ffSSimon Glass * 1505a66a8ffSSimon Glass * If an active device has this sequence it will be returned. If there is no 1515a66a8ffSSimon Glass * such device then this will check for a device that is requesting this 1525a66a8ffSSimon Glass * sequence. 1535a66a8ffSSimon Glass * 1545a66a8ffSSimon Glass * The device is probed to activate it ready for use. 1555a66a8ffSSimon Glass * 1565a66a8ffSSimon Glass * @id: ID to look up 1575a66a8ffSSimon Glass * @seq: Sequence number to find (0=first) 1585a66a8ffSSimon Glass * @devp: Returns pointer to device (there is only one for each seq) 1595a66a8ffSSimon Glass * @return 0 if OK, -ve on error 1605a66a8ffSSimon Glass */ 1615a66a8ffSSimon Glass int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp); 1625a66a8ffSSimon Glass 1635a66a8ffSSimon Glass /** 164f4cdead2SSimon Glass * uclass_get_device_by_of_offset() - Get a uclass device by device tree node 165f4cdead2SSimon Glass * 166f4cdead2SSimon Glass * This searches the devices in the uclass for one attached to the given 167f4cdead2SSimon Glass * device tree node. 168f4cdead2SSimon Glass * 169f4cdead2SSimon Glass * The device is probed to activate it ready for use. 170f4cdead2SSimon Glass * 171f4cdead2SSimon Glass * @id: ID to look up 172f4cdead2SSimon Glass * @node: Device tree offset to search for (if -ve then -ENODEV is returned) 173f4cdead2SSimon Glass * @devp: Returns pointer to device (there is only one for each node) 174f4cdead2SSimon Glass * @return 0 if OK, -ve on error 175f4cdead2SSimon Glass */ 176f4cdead2SSimon Glass int uclass_get_device_by_of_offset(enum uclass_id id, int node, 177f4cdead2SSimon Glass struct udevice **devp); 178f4cdead2SSimon Glass 179f4cdead2SSimon Glass /** 180d82ba4c0SSimon Glass * uclass_get_device_by_phandle() - Get a uclass device by phandle 181d82ba4c0SSimon Glass * 182d82ba4c0SSimon Glass * This searches the devices in the uclass for one with the given phandle. 183d82ba4c0SSimon Glass * 184d82ba4c0SSimon Glass * The device is probed to activate it ready for use. 185d82ba4c0SSimon Glass * 186d82ba4c0SSimon Glass * @id: uclass ID to look up 187d82ba4c0SSimon Glass * @parent: Parent device containing the phandle pointer 188d82ba4c0SSimon Glass * @name: Name of property in the parent device node 189d82ba4c0SSimon Glass * @devp: Returns pointer to device (there is only one for each node) 190d82ba4c0SSimon Glass * @return 0 if OK, -ENOENT if there is no @name present in the node, other 191d82ba4c0SSimon Glass * -ve on error 192d82ba4c0SSimon Glass */ 193d82ba4c0SSimon Glass int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, 194d82ba4c0SSimon Glass const char *name, struct udevice **devp); 195d82ba4c0SSimon Glass 196d82ba4c0SSimon Glass /** 1976494d708SSimon Glass * uclass_first_device() - Get the first device in a uclass 1986494d708SSimon Glass * 199040b69afSSimon Glass * The device returned is probed if necessary, and ready for use 200040b69afSSimon Glass * 2016494d708SSimon Glass * @id: Uclass ID to look up 2026494d708SSimon Glass * @devp: Returns pointer to the first device in that uclass, or NULL if none 203*b0675050SSimon Glass * @return 0 if OK (found or not found), other -ve on error 2046494d708SSimon Glass */ 20554c5d08aSHeiko Schocher int uclass_first_device(enum uclass_id id, struct udevice **devp); 2066494d708SSimon Glass 2076494d708SSimon Glass /** 208*b0675050SSimon Glass * uclass_first_device_err() - Get the first device in a uclass 209*b0675050SSimon Glass * 210*b0675050SSimon Glass * The device returned is probed if necessary, and ready for use 211*b0675050SSimon Glass * 212*b0675050SSimon Glass * @id: Uclass ID to look up 213*b0675050SSimon Glass * @devp: Returns pointer to the first device in that uclass, or NULL if none 214*b0675050SSimon Glass * @return 0 if found, -ENODEV if not found, other -ve on error 215*b0675050SSimon Glass */ 216*b0675050SSimon Glass int uclass_first_device_err(enum uclass_id id, struct udevice **devp); 217*b0675050SSimon Glass 218*b0675050SSimon Glass /** 2196494d708SSimon Glass * uclass_next_device() - Get the next device in a uclass 2206494d708SSimon Glass * 221040b69afSSimon Glass * The device returned is probed if necessary, and ready for use 222040b69afSSimon Glass * 2236494d708SSimon Glass * @devp: On entry, pointer to device to lookup. On exit, returns pointer 2246494d708SSimon Glass * to the next device in the same uclass, or NULL if none 225*b0675050SSimon Glass * @return 0 if OK (found or not found), other -ve on error 2266494d708SSimon Glass */ 22754c5d08aSHeiko Schocher int uclass_next_device(struct udevice **devp); 2286494d708SSimon Glass 2296494d708SSimon Glass /** 2305a66a8ffSSimon Glass * uclass_resolve_seq() - Resolve a device's sequence number 2315a66a8ffSSimon Glass * 2325a66a8ffSSimon Glass * On entry dev->seq is -1, and dev->req_seq may be -1 (to allocate a 2335a66a8ffSSimon Glass * sequence number automatically, or >= 0 to select a particular number. 2345a66a8ffSSimon Glass * If the requested sequence number is in use, then this device will 2355a66a8ffSSimon Glass * be allocated another one. 2365a66a8ffSSimon Glass * 2375a66a8ffSSimon Glass * Note that the device's seq value is not changed by this function. 2385a66a8ffSSimon Glass * 2395a66a8ffSSimon Glass * @dev: Device for which to allocate sequence number 2405a66a8ffSSimon Glass * @return sequence number allocated, or -ve on error 2415a66a8ffSSimon Glass */ 2425a66a8ffSSimon Glass int uclass_resolve_seq(struct udevice *dev); 2435a66a8ffSSimon Glass 2445a66a8ffSSimon Glass /** 2456494d708SSimon Glass * uclass_foreach_dev() - Helper function to iteration through devices 2466494d708SSimon Glass * 2476494d708SSimon Glass * This creates a for() loop which works through the available devices in 2486494d708SSimon Glass * a uclass in order from start to end. 2496494d708SSimon Glass * 25054c5d08aSHeiko Schocher * @pos: struct udevice * to hold the current device. Set to NULL when there 2516494d708SSimon Glass * are no more devices. 252f2bc6fc3SSimon Glass * @uc: uclass to scan 2536494d708SSimon Glass */ 2546494d708SSimon Glass #define uclass_foreach_dev(pos, uc) \ 25571f1e3f1SMasahiro Yamada list_for_each_entry(pos, &uc->dev_head, uclass_node) 2566494d708SSimon Glass 2577aeac5bcSSimon Glass /** 2587aeac5bcSSimon Glass * uclass_foreach_dev_safe() - Helper function to safely iteration through devs 2597aeac5bcSSimon Glass * 2607aeac5bcSSimon Glass * This creates a for() loop which works through the available devices in 2617aeac5bcSSimon Glass * a uclass in order from start to end. Inside the loop, it is safe to remove 2627aeac5bcSSimon Glass * @pos if required. 2637aeac5bcSSimon Glass * 2647aeac5bcSSimon Glass * @pos: struct udevice * to hold the current device. Set to NULL when there 2657aeac5bcSSimon Glass * are no more devices. 2667aeac5bcSSimon Glass * @next: struct udevice * to hold the next next 2677aeac5bcSSimon Glass * @uc: uclass to scan 2687aeac5bcSSimon Glass */ 2697aeac5bcSSimon Glass #define uclass_foreach_dev_safe(pos, next, uc) \ 2707aeac5bcSSimon Glass list_for_each_entry_safe(pos, next, &uc->dev_head, uclass_node) 2717aeac5bcSSimon Glass 2726494d708SSimon Glass #endif 273