device.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. *
  5. * (C) Copyright 2012
  6. * Pavel Herrmann <morpheus.ibis@gmail.com>
  7. * Marek Vasut <marex@denx.de>
  8. */
  9. #ifndef _DM_DEVICE_H
  10. #define _DM_DEVICE_H
  11. #include <dm/ofnode.h>
  12. #include <dm/uclass-id.h>
  13. #include <fdtdec.h>
  14. #include <linker_lists.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/printk.h>
  18. struct driver_info;
  19. /* Driver is active (probed). Cleared when it is removed */
  20. #define DM_FLAG_ACTIVATED (1 << 0)
  21. /* DM is responsible for allocating and freeing plat */
  22. #define DM_FLAG_ALLOC_PDATA (1 << 1)
  23. /* DM should init this device prior to relocation */
  24. #define DM_FLAG_PRE_RELOC (1 << 2)
  25. /* DM is responsible for allocating and freeing parent_plat */
  26. #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
  27. /* DM is responsible for allocating and freeing uclass_plat */
  28. #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
  29. /* Allocate driver private data on a DMA boundary */
  30. #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
  31. /* Device is bound */
  32. #define DM_FLAG_BOUND (1 << 6)
  33. /* Device name is allocated and should be freed on unbind() */
  34. #define DM_FLAG_NAME_ALLOCED (1 << 7)
  35. /* Device has platform data provided by of-platdata */
  36. #define DM_FLAG_OF_PLATDATA (1 << 8)
  37. /*
  38. * Call driver remove function to stop currently active DMA transfers or
  39. * give DMA buffers back to the HW / controller. This may be needed for
  40. * some drivers to do some final stage cleanup before the OS is called
  41. * (U-Boot exit)
  42. */
  43. #define DM_FLAG_ACTIVE_DMA (1 << 9)
  44. /*
  45. * Call driver remove function to do some final configuration, before
  46. * U-Boot exits and the OS is started
  47. */
  48. #define DM_FLAG_OS_PREPARE (1 << 10)
  49. /* DM does not enable/disable the power domains corresponding to this device */
  50. #define DM_FLAG_DEFAULT_PD_CTRL_OFF (1 << 11)
  51. /* Driver plat has been read. Cleared when the device is removed */
  52. #define DM_FLAG_PLATDATA_VALID (1 << 12)
  53. /*
  54. * Device is removed without switching off its power domain. This might
  55. * be required, i. e. for serial console (debug) output when booting OS.
  56. */
  57. #define DM_FLAG_REMOVE_WITH_PD_ON (1 << 13)
  58. /*
  59. * One or multiple of these flags are passed to device_remove() so that
  60. * a selective device removal as specified by the remove-stage and the
  61. * driver flags can be done.
  62. */
  63. enum {
  64. /* Normal remove, remove all devices */
  65. DM_REMOVE_NORMAL = 1 << 0,
  66. /* Remove devices with active DMA */
  67. DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
  68. /* Remove devices which need some final OS preparation steps */
  69. DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
  70. /* Add more use cases here */
  71. /* Remove devices with any active flag */
  72. DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
  73. /* Don't power down any attached power domains */
  74. DM_REMOVE_NO_PD = 1 << 1,
  75. };
  76. /**
  77. * struct udevice - An instance of a driver
  78. *
  79. * This holds information about a device, which is a driver bound to a
  80. * particular port or peripheral (essentially a driver instance).
  81. *
  82. * A device will come into existence through a 'bind' call, either due to
  83. * a U_BOOT_DRVINFO() macro (in which case plat is non-NULL) or a node
  84. * in the device tree (in which case of_offset is >= 0). In the latter case
  85. * we translate the device tree information into plat in a function
  86. * implemented by the driver of_to_plat method (called just before the
  87. * probe method if the device has a device tree node.
  88. *
  89. * All three of plat, priv and uclass_priv can be allocated by the
  90. * driver, or you can use the auto_alloc_size members of struct driver and
  91. * struct uclass_driver to have driver model do this automatically.
  92. *
  93. * @driver: The driver used by this device
  94. * @name: Name of device, typically the FDT node name
  95. * @plat_: Configuration data for this device (do not access outside driver
  96. * model)
  97. * @parent_plat_: The parent bus's configuration data for this device (do not
  98. * access outside driver model)
  99. * @uclass_plat_: The uclass's configuration data for this device (do not access
  100. * outside driver model)
  101. * @driver_data: Driver data word for the entry that matched this device with
  102. * its driver
  103. * @parent: Parent of this device, or NULL for the top level device
  104. * @priv_: Private data for this device (do not access outside driver model)
  105. * @uclass: Pointer to uclass for this device
  106. * @uclass_priv_: The uclass's private data for this device (do not access
  107. * outside driver model)
  108. * @parent_priv_: The parent's private data for this device (do not access
  109. * outside driver model)
  110. * @uclass_node: Used by uclass to link its devices
  111. * @child_head: List of children of this device
  112. * @sibling_node: Next device in list of all devices
  113. * @flags_: Flags for this device DM_FLAG_... (do not access outside driver
  114. * model)
  115. * @seq_: Allocated sequence number for this device (-1 = none). This is set up
  116. * when the device is bound and is unique within the device's uclass. If the
  117. * device has an alias in the devicetree then that is used to set the sequence
  118. * number. Otherwise, the next available number is used. Sequence numbers are
  119. * used by certain commands that need device to be numbered (e.g. 'mmc dev').
  120. * (do not access outside driver model)
  121. * @node_: Reference to device tree node for this device (do not access outside
  122. * driver model)
  123. * @devres_head: List of memory allocations associated with this device.
  124. * When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
  125. * add to this list. Memory so-allocated will be freed
  126. * automatically when the device is removed / unbound
  127. */
  128. struct udevice {
  129. const struct driver *driver;
  130. const char *name;
  131. void *plat_;
  132. void *parent_plat_;
  133. void *uclass_plat_;
  134. ulong driver_data;
  135. struct udevice *parent;
  136. void *priv_;
  137. struct uclass *uclass;
  138. void *uclass_priv_;
  139. void *parent_priv_;
  140. struct list_head uclass_node;
  141. struct list_head child_head;
  142. struct list_head sibling_node;
  143. u32 flags_;
  144. int seq_;
  145. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  146. ofnode node_;
  147. #endif
  148. #ifdef CONFIG_DEVRES
  149. struct list_head devres_head;
  150. #endif
  151. };
  152. /* Maximum sequence number supported */
  153. #define DM_MAX_SEQ 999
  154. /* Returns the operations for a device */
  155. #define device_get_ops(dev) (dev->driver->ops)
  156. static inline u32 dev_get_flags(const struct udevice *dev)
  157. {
  158. return dev->flags_;
  159. }
  160. static inline void dev_or_flags(struct udevice *dev, u32 or)
  161. {
  162. dev->flags_ |= or;
  163. }
  164. static inline void dev_bic_flags(struct udevice *dev, u32 bic)
  165. {
  166. dev->flags_ &= ~bic;
  167. }
  168. /**
  169. * dev_ofnode() - get the DT node reference associated with a udevice
  170. *
  171. * @dev: device to check
  172. * @return reference of the the device's DT node
  173. */
  174. static inline ofnode dev_ofnode(const struct udevice *dev)
  175. {
  176. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  177. return dev->node_;
  178. #else
  179. return ofnode_null();
  180. #endif
  181. }
  182. /* Returns non-zero if the device is active (probed and not removed) */
  183. #define device_active(dev) (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
  184. static inline int dev_of_offset(const struct udevice *dev)
  185. {
  186. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  187. return ofnode_to_offset(dev_ofnode(dev));
  188. #else
  189. return -1;
  190. #endif
  191. }
  192. static inline bool dev_has_ofnode(const struct udevice *dev)
  193. {
  194. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  195. return ofnode_valid(dev_ofnode(dev));
  196. #else
  197. return false;
  198. #endif
  199. }
  200. static inline void dev_set_ofnode(struct udevice *dev, ofnode node)
  201. {
  202. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  203. dev->node_ = node;
  204. #endif
  205. }
  206. static inline int dev_seq(const struct udevice *dev)
  207. {
  208. return dev->seq_;
  209. }
  210. /**
  211. * struct udevice_id - Lists the compatible strings supported by a driver
  212. * @compatible: Compatible string
  213. * @data: Data for this compatible string
  214. */
  215. struct udevice_id {
  216. const char *compatible;
  217. ulong data;
  218. };
  219. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  220. #define of_match_ptr(_ptr) (_ptr)
  221. #else
  222. #define of_match_ptr(_ptr) NULL
  223. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  224. /**
  225. * struct driver - A driver for a feature or peripheral
  226. *
  227. * This holds methods for setting up a new device, and also removing it.
  228. * The device needs information to set itself up - this is provided either
  229. * by plat or a device tree node (which we find by looking up
  230. * matching compatible strings with of_match).
  231. *
  232. * Drivers all belong to a uclass, representing a class of devices of the
  233. * same type. Common elements of the drivers can be implemented in the uclass,
  234. * or the uclass can provide a consistent interface to the drivers within
  235. * it.
  236. *
  237. * @name: Device name
  238. * @id: Identifies the uclass we belong to
  239. * @of_match: List of compatible strings to match, and any identifying data
  240. * for each.
  241. * @bind: Called to bind a device to its driver
  242. * @probe: Called to probe a device, i.e. activate it
  243. * @remove: Called to remove a device, i.e. de-activate it
  244. * @unbind: Called to unbind a device from its driver
  245. * @of_to_plat: Called before probe to decode device tree data
  246. * @child_post_bind: Called after a new child has been bound
  247. * @child_pre_probe: Called before a child device is probed. The device has
  248. * memory allocated but it has not yet been probed.
  249. * @child_post_remove: Called after a child device is removed. The device
  250. * has memory allocated but its device_remove() method has been called.
  251. * @priv_auto: If non-zero this is the size of the private data
  252. * to be allocated in the device's ->priv pointer. If zero, then the driver
  253. * is responsible for allocating any data required.
  254. * @plat_auto: If non-zero this is the size of the
  255. * platform data to be allocated in the device's ->plat pointer.
  256. * This is typically only useful for device-tree-aware drivers (those with
  257. * an of_match), since drivers which use plat will have the data
  258. * provided in the U_BOOT_DRVINFO() instantiation.
  259. * @per_child_auto: Each device can hold private data owned by
  260. * its parent. If required this will be automatically allocated if this
  261. * value is non-zero.
  262. * @per_child_plat_auto: A bus likes to store information about
  263. * its children. If non-zero this is the size of this data, to be allocated
  264. * in the child's parent_plat pointer.
  265. * @ops: Driver-specific operations. This is typically a list of function
  266. * pointers defined by the driver, to implement driver functions required by
  267. * the uclass.
  268. * @flags: driver flags - see DM_FLAGS_...
  269. * @acpi_ops: Advanced Configuration and Power Interface (ACPI) operations,
  270. * allowing the device to add things to the ACPI tables passed to Linux
  271. */
  272. struct driver {
  273. char *name;
  274. enum uclass_id id;
  275. const struct udevice_id *of_match;
  276. int (*bind)(struct udevice *dev);
  277. int (*probe)(struct udevice *dev);
  278. int (*remove)(struct udevice *dev);
  279. int (*unbind)(struct udevice *dev);
  280. int (*of_to_plat)(struct udevice *dev);
  281. int (*child_post_bind)(struct udevice *dev);
  282. int (*child_pre_probe)(struct udevice *dev);
  283. int (*child_post_remove)(struct udevice *dev);
  284. int priv_auto;
  285. int plat_auto;
  286. int per_child_auto;
  287. int per_child_plat_auto;
  288. const void *ops; /* driver-specific operations */
  289. uint32_t flags;
  290. #if CONFIG_IS_ENABLED(ACPIGEN)
  291. struct acpi_ops *acpi_ops;
  292. #endif
  293. };
  294. /* Declare a new U-Boot driver */
  295. #define U_BOOT_DRIVER(__name) \
  296. ll_entry_declare(struct driver, __name, driver)
  297. /* Get a pointer to a given driver */
  298. #define DM_DRIVER_GET(__name) \
  299. ll_entry_get(struct driver, __name, driver)
  300. /**
  301. * Declare a macro to state a alias for a driver name. This macro will
  302. * produce no code but its information will be parsed by tools like
  303. * dtoc
  304. */
  305. #define DM_DRIVER_ALIAS(__name, __alias)
  306. /**
  307. * dev_get_plat() - Get the platform data for a device
  308. *
  309. * This checks that dev is not NULL, but no other checks for now
  310. *
  311. * @dev Device to check
  312. * @return platform data, or NULL if none
  313. */
  314. void *dev_get_plat(const struct udevice *dev);
  315. /**
  316. * dev_get_parent_plat() - Get the parent platform data for a device
  317. *
  318. * This checks that dev is not NULL, but no other checks for now
  319. *
  320. * @dev Device to check
  321. * @return parent's platform data, or NULL if none
  322. */
  323. void *dev_get_parent_plat(const struct udevice *dev);
  324. /**
  325. * dev_get_uclass_plat() - Get the uclass platform data for a device
  326. *
  327. * This checks that dev is not NULL, but no other checks for now
  328. *
  329. * @dev Device to check
  330. * @return uclass's platform data, or NULL if none
  331. */
  332. void *dev_get_uclass_plat(const struct udevice *dev);
  333. /**
  334. * dev_get_priv() - Get the private data for a device
  335. *
  336. * This checks that dev is not NULL, but no other checks for now
  337. *
  338. * @dev Device to check
  339. * @return private data, or NULL if none
  340. */
  341. void *dev_get_priv(const struct udevice *dev);
  342. /**
  343. * dev_get_parent_priv() - Get the parent private data for a device
  344. *
  345. * The parent private data is data stored in the device but owned by the
  346. * parent. For example, a USB device may have parent data which contains
  347. * information about how to talk to the device over USB.
  348. *
  349. * This checks that dev is not NULL, but no other checks for now
  350. *
  351. * @dev Device to check
  352. * @return parent data, or NULL if none
  353. */
  354. void *dev_get_parent_priv(const struct udevice *dev);
  355. /**
  356. * dev_get_uclass_priv() - Get the private uclass data for a device
  357. *
  358. * This checks that dev is not NULL, but no other checks for now
  359. *
  360. * @dev Device to check
  361. * @return private uclass data for this device, or NULL if none
  362. */
  363. void *dev_get_uclass_priv(const struct udevice *dev);
  364. /**
  365. * struct dev_get_parent() - Get the parent of a device
  366. *
  367. * @child: Child to check
  368. * @return parent of child, or NULL if this is the root device
  369. */
  370. struct udevice *dev_get_parent(const struct udevice *child);
  371. /**
  372. * dev_get_driver_data() - get the driver data used to bind a device
  373. *
  374. * When a device is bound using a device tree node, it matches a
  375. * particular compatible string in struct udevice_id. This function
  376. * returns the associated data value for that compatible string. This is
  377. * the 'data' field in struct udevice_id.
  378. *
  379. * As an example, consider this structure:
  380. * static const struct udevice_id tegra_i2c_ids[] = {
  381. * { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  382. * { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
  383. * { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
  384. * { }
  385. * };
  386. *
  387. * When driver model finds a driver for this it will store the 'data' value
  388. * corresponding to the compatible string it matches. This function returns
  389. * that value. This allows the driver to handle several variants of a device.
  390. *
  391. * For USB devices, this is the driver_info field in struct usb_device_id.
  392. *
  393. * @dev: Device to check
  394. * @return driver data (0 if none is provided)
  395. */
  396. ulong dev_get_driver_data(const struct udevice *dev);
  397. /**
  398. * dev_get_driver_ops() - get the device's driver's operations
  399. *
  400. * This checks that dev is not NULL, and returns the pointer to device's
  401. * driver's operations.
  402. *
  403. * @dev: Device to check
  404. * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
  405. */
  406. const void *dev_get_driver_ops(const struct udevice *dev);
  407. /**
  408. * device_get_uclass_id() - return the uclass ID of a device
  409. *
  410. * @dev: Device to check
  411. * @return uclass ID for the device
  412. */
  413. enum uclass_id device_get_uclass_id(const struct udevice *dev);
  414. /**
  415. * dev_get_uclass_name() - return the uclass name of a device
  416. *
  417. * This checks that dev is not NULL.
  418. *
  419. * @dev: Device to check
  420. * @return pointer to the uclass name for the device
  421. */
  422. const char *dev_get_uclass_name(const struct udevice *dev);
  423. /**
  424. * device_get_child() - Get the child of a device by index
  425. *
  426. * Returns the numbered child, 0 being the first. This does not use
  427. * sequence numbers, only the natural order.
  428. *
  429. * @dev: Parent device to check
  430. * @index: Child index
  431. * @devp: Returns pointer to device
  432. * @return 0 if OK, -ENODEV if no such device, other error if the device fails
  433. * to probe
  434. */
  435. int device_get_child(const struct udevice *parent, int index,
  436. struct udevice **devp);
  437. /**
  438. * device_get_child_count() - Get the available child count of a device
  439. *
  440. * Returns the number of children to a device.
  441. *
  442. * @parent: Parent device to check
  443. */
  444. int device_get_child_count(const struct udevice *parent);
  445. /**
  446. * device_find_child_by_seq() - Find a child device based on a sequence
  447. *
  448. * This searches for a device with the given seq.
  449. *
  450. * @parent: Parent device
  451. * @seq: Sequence number to find (0=first)
  452. * @devp: Returns pointer to device (there is only one per for each seq).
  453. * Set to NULL if none is found
  454. * @return 0 if OK, -ENODEV if not found
  455. */
  456. int device_find_child_by_seq(const struct udevice *parent, int seq,
  457. struct udevice **devp);
  458. /**
  459. * device_get_child_by_seq() - Get a child device based on a sequence
  460. *
  461. * If an active device has this sequence it will be returned. If there is no
  462. * such device then this will check for a device that is requesting this
  463. * sequence.
  464. *
  465. * The device is probed to activate it ready for use.
  466. *
  467. * @parent: Parent device
  468. * @seq: Sequence number to find (0=first)
  469. * @devp: Returns pointer to device (there is only one per for each seq)
  470. * Set to NULL if none is found
  471. * @return 0 if OK, -ve on error
  472. */
  473. int device_get_child_by_seq(const struct udevice *parent, int seq,
  474. struct udevice **devp);
  475. /**
  476. * device_find_child_by_of_offset() - Find a child device based on FDT offset
  477. *
  478. * Locates a child device by its device tree offset.
  479. *
  480. * @parent: Parent device
  481. * @of_offset: Device tree offset to find
  482. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  483. * @return 0 if OK, -ve on error
  484. */
  485. int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
  486. struct udevice **devp);
  487. /**
  488. * device_get_child_by_of_offset() - Get a child device based on FDT offset
  489. *
  490. * Locates a child device by its device tree offset.
  491. *
  492. * The device is probed to activate it ready for use.
  493. *
  494. * @parent: Parent device
  495. * @of_offset: Device tree offset to find
  496. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  497. * @return 0 if OK, -ve on error
  498. */
  499. int device_get_child_by_of_offset(const struct udevice *parent, int of_offset,
  500. struct udevice **devp);
  501. /**
  502. * device_find_global_by_ofnode() - Get a device based on ofnode
  503. *
  504. * Locates a device by its device tree ofnode, searching globally throughout
  505. * the all driver model devices.
  506. *
  507. * The device is NOT probed
  508. *
  509. * @node: Device tree ofnode to find
  510. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  511. * @return 0 if OK, -ve on error
  512. */
  513. int device_find_global_by_ofnode(ofnode node, struct udevice **devp);
  514. /**
  515. * device_get_global_by_ofnode() - Get a device based on ofnode
  516. *
  517. * Locates a device by its device tree ofnode, searching globally throughout
  518. * the all driver model devices.
  519. *
  520. * The device is probed to activate it ready for use.
  521. *
  522. * @node: Device tree ofnode to find
  523. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  524. * @return 0 if OK, -ve on error
  525. */
  526. int device_get_global_by_ofnode(ofnode node, struct udevice **devp);
  527. /**
  528. * device_get_by_driver_info() - Get a device based on driver_info
  529. *
  530. * Locates a device by its struct driver_info, by using its reference which
  531. * is updated during the bind process.
  532. *
  533. * The device is probed to activate it ready for use.
  534. *
  535. * @info: Struct driver_info
  536. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  537. * @return 0 if OK, -ve on error
  538. */
  539. int device_get_by_driver_info(const struct driver_info *info,
  540. struct udevice **devp);
  541. /**
  542. * device_get_by_driver_info_idx() - Get a device based on driver_info index
  543. *
  544. * Locates a device by its struct driver_info, by using its index number which
  545. * is written into the idx field of struct phandle_1_arg, etc.
  546. *
  547. * The device is probed to activate it ready for use.
  548. *
  549. * @idx: Index number of the driver_info structure (0=first)
  550. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  551. * @return 0 if OK, -ve on error
  552. */
  553. int device_get_by_driver_info_idx(uint idx, struct udevice **devp);
  554. /**
  555. * device_find_first_child() - Find the first child of a device
  556. *
  557. * @parent: Parent device to search
  558. * @devp: Returns first child device, or NULL if none
  559. * @return 0
  560. */
  561. int device_find_first_child(const struct udevice *parent,
  562. struct udevice **devp);
  563. /**
  564. * device_find_next_child() - Find the next child of a device
  565. *
  566. * @devp: Pointer to previous child device on entry. Returns pointer to next
  567. * child device, or NULL if none
  568. * @return 0
  569. */
  570. int device_find_next_child(struct udevice **devp);
  571. /**
  572. * device_find_first_inactive_child() - Find the first inactive child
  573. *
  574. * This is used to locate an existing child of a device which is of a given
  575. * uclass.
  576. *
  577. * The device is NOT probed
  578. *
  579. * @parent: Parent device to search
  580. * @uclass_id: Uclass to look for
  581. * @devp: Returns device found, if any
  582. * @return 0 if found, else -ENODEV
  583. */
  584. int device_find_first_inactive_child(const struct udevice *parent,
  585. enum uclass_id uclass_id,
  586. struct udevice **devp);
  587. /**
  588. * device_find_first_child_by_uclass() - Find the first child of a device in uc
  589. *
  590. * @parent: Parent device to search
  591. * @uclass_id: Uclass to look for
  592. * @devp: Returns first child device in that uclass, if any
  593. * @return 0 if found, else -ENODEV
  594. */
  595. int device_find_first_child_by_uclass(const struct udevice *parent,
  596. enum uclass_id uclass_id,
  597. struct udevice **devp);
  598. /**
  599. * device_find_child_by_name() - Find a child by device name
  600. *
  601. * @parent: Parent device to search
  602. * @name: Name to look for
  603. * @devp: Returns device found, if any
  604. * @return 0 if found, else -ENODEV
  605. */
  606. int device_find_child_by_name(const struct udevice *parent, const char *name,
  607. struct udevice **devp);
  608. /**
  609. * device_first_child_ofdata_err() - Find the first child and reads its plat
  610. *
  611. * The of_to_plat() method is called on the child before it is returned,
  612. * but the child is not probed.
  613. *
  614. * @parent: Parent to check
  615. * @devp: Returns child that was found, if any
  616. * @return 0 on success, -ENODEV if no children, other -ve on error
  617. */
  618. int device_first_child_ofdata_err(struct udevice *parent,
  619. struct udevice **devp);
  620. /*
  621. * device_next_child_ofdata_err() - Find the next child and read its plat
  622. *
  623. * The of_to_plat() method is called on the child before it is returned,
  624. * but the child is not probed.
  625. *
  626. * @devp: On entry, points to the previous child; on exit returns the child that
  627. * was found, if any
  628. * @return 0 on success, -ENODEV if no children, other -ve on error
  629. */
  630. int device_next_child_ofdata_err(struct udevice **devp);
  631. /**
  632. * device_first_child_err() - Get the first child of a device
  633. *
  634. * The device returned is probed if necessary, and ready for use
  635. *
  636. * @parent: Parent device to search
  637. * @devp: Returns device found, if any
  638. * @return 0 if found, -ENODEV if not, -ve error if device failed to probe
  639. */
  640. int device_first_child_err(struct udevice *parent, struct udevice **devp);
  641. /**
  642. * device_next_child_err() - Get the next child of a parent device
  643. *
  644. * The device returned is probed if necessary, and ready for use
  645. *
  646. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  647. * to the next sibling if no error occurred
  648. * @return 0 if found, -ENODEV if not, -ve error if device failed to probe
  649. */
  650. int device_next_child_err(struct udevice **devp);
  651. /**
  652. * device_has_children() - check if a device has any children
  653. *
  654. * @dev: Device to check
  655. * @return true if the device has one or more children
  656. */
  657. bool device_has_children(const struct udevice *dev);
  658. /**
  659. * device_has_active_children() - check if a device has any active children
  660. *
  661. * @dev: Device to check
  662. * @return true if the device has one or more children and at least one of
  663. * them is active (probed).
  664. */
  665. bool device_has_active_children(const struct udevice *dev);
  666. /**
  667. * device_is_last_sibling() - check if a device is the last sibling
  668. *
  669. * This function can be useful for display purposes, when special action needs
  670. * to be taken when displaying the last sibling. This can happen when a tree
  671. * view of devices is being displayed.
  672. *
  673. * @dev: Device to check
  674. * @return true if there are no more siblings after this one - i.e. is it
  675. * last in the list.
  676. */
  677. bool device_is_last_sibling(const struct udevice *dev);
  678. /**
  679. * device_set_name() - set the name of a device
  680. *
  681. * This must be called in the device's bind() method and no later. Normally
  682. * this is unnecessary but for probed devices which don't get a useful name
  683. * this function can be helpful.
  684. *
  685. * The name is allocated and will be freed automatically when the device is
  686. * unbound.
  687. *
  688. * @dev: Device to update
  689. * @name: New name (this string is allocated new memory and attached to
  690. * the device)
  691. * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
  692. * string
  693. */
  694. int device_set_name(struct udevice *dev, const char *name);
  695. /**
  696. * device_set_name_alloced() - note that a device name is allocated
  697. *
  698. * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
  699. * unbound the name will be freed. This avoids memory leaks.
  700. *
  701. * @dev: Device to update
  702. */
  703. void device_set_name_alloced(struct udevice *dev);
  704. /**
  705. * device_is_compatible() - check if the device is compatible with the compat
  706. *
  707. * This allows to check whether the device is comaptible with the compat.
  708. *
  709. * @dev: udevice pointer for which compatible needs to be verified.
  710. * @compat: Compatible string which needs to verified in the given
  711. * device
  712. * @return true if OK, false if the compatible is not found
  713. */
  714. bool device_is_compatible(const struct udevice *dev, const char *compat);
  715. /**
  716. * of_machine_is_compatible() - check if the machine is compatible with
  717. * the compat
  718. *
  719. * This allows to check whether the machine is comaptible with the compat.
  720. *
  721. * @compat: Compatible string which needs to verified
  722. * @return true if OK, false if the compatible is not found
  723. */
  724. bool of_machine_is_compatible(const char *compat);
  725. /**
  726. * dev_disable_by_path() - Disable a device given its device tree path
  727. *
  728. * @path: The device tree path identifying the device to be disabled
  729. * @return 0 on success, -ve on error
  730. */
  731. int dev_disable_by_path(const char *path);
  732. /**
  733. * dev_enable_by_path() - Enable a device given its device tree path
  734. *
  735. * @path: The device tree path identifying the device to be enabled
  736. * @return 0 on success, -ve on error
  737. */
  738. int dev_enable_by_path(const char *path);
  739. /**
  740. * device_is_on_pci_bus - Test if a device is on a PCI bus
  741. *
  742. * @dev: device to test
  743. * @return: true if it is on a PCI bus, false otherwise
  744. */
  745. static inline bool device_is_on_pci_bus(const struct udevice *dev)
  746. {
  747. return dev->parent && device_get_uclass_id(dev->parent) == UCLASS_PCI;
  748. }
  749. /**
  750. * device_foreach_child_safe() - iterate through child devices safely
  751. *
  752. * This allows the @pos child to be removed in the loop if required.
  753. *
  754. * @pos: struct udevice * for the current device
  755. * @next: struct udevice * for the next device
  756. * @parent: parent device to scan
  757. */
  758. #define device_foreach_child_safe(pos, next, parent) \
  759. list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
  760. /**
  761. * device_foreach_child() - iterate through child devices
  762. *
  763. * @pos: struct udevice * for the current device
  764. * @parent: parent device to scan
  765. */
  766. #define device_foreach_child(pos, parent) \
  767. list_for_each_entry(pos, &parent->child_head, sibling_node)
  768. /**
  769. * device_foreach_child_of_to_plat() - iterate through children
  770. *
  771. * This stops when it gets an error, with @pos set to the device that failed to
  772. * read ofdata.
  773. * This creates a for() loop which works through the available children of
  774. * a device in order from start to end. Device ofdata is read by calling
  775. * device_of_to_plat() on each one. The devices are not probed.
  776. *
  777. * @pos: struct udevice * for the current device
  778. * @parent: parent device to scan
  779. */
  780. #define device_foreach_child_of_to_plat(pos, parent) \
  781. for (int _ret = device_first_child_ofdata_err(parent, &dev); !_ret; \
  782. _ret = device_next_child_ofdata_err(&dev))
  783. /**
  784. * device_foreach_child_probe() - iterate through children, probing them
  785. *
  786. * This creates a for() loop which works through the available children of
  787. * a device in order from start to end. Devices are probed if necessary,
  788. * and ready for use.
  789. *
  790. * This stops when it gets an error, with @pos set to the device that failed to
  791. * probe
  792. *
  793. * @pos: struct udevice * for the current device
  794. * @parent: parent device to scan
  795. */
  796. #define device_foreach_child_probe(pos, parent) \
  797. for (int _ret = device_first_child_err(parent, &dev); !_ret; \
  798. _ret = device_next_child_err(&dev))
  799. /**
  800. * dm_scan_fdt_dev() - Bind child device in the device tree
  801. *
  802. * This handles device which have sub-nodes in the device tree. It scans all
  803. * sub-nodes and binds drivers for each node where a driver can be found.
  804. *
  805. * If this is called prior to relocation, only pre-relocation devices will be
  806. * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
  807. * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
  808. * be bound.
  809. *
  810. * @dev: Device to scan
  811. * @return 0 if OK, -ve on error
  812. */
  813. int dm_scan_fdt_dev(struct udevice *dev);
  814. #endif