device.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/compat.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/printk.h>
  19. struct driver_info;
  20. /* Driver is active (probed). Cleared when it is removed */
  21. #define DM_FLAG_ACTIVATED (1 << 0)
  22. /* DM is responsible for allocating and freeing platdata */
  23. #define DM_FLAG_ALLOC_PDATA (1 << 1)
  24. /* DM should init this device prior to relocation */
  25. #define DM_FLAG_PRE_RELOC (1 << 2)
  26. /* DM is responsible for allocating and freeing parent_platdata */
  27. #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
  28. /* DM is responsible for allocating and freeing uclass_platdata */
  29. #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
  30. /* Allocate driver private data on a DMA boundary */
  31. #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
  32. /* Device is bound */
  33. #define DM_FLAG_BOUND (1 << 6)
  34. /* Device name is allocated and should be freed on unbind() */
  35. #define DM_FLAG_NAME_ALLOCED (1 << 7)
  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. /*
  52. * One or multiple of these flags are passed to device_remove() so that
  53. * a selective device removal as specified by the remove-stage and the
  54. * driver flags can be done.
  55. */
  56. enum {
  57. /* Normal remove, remove all devices */
  58. DM_REMOVE_NORMAL = 1 << 0,
  59. /* Remove devices with active DMA */
  60. DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
  61. /* Remove devices which need some final OS preparation steps */
  62. DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
  63. /* Add more use cases here */
  64. /* Remove devices with any active flag */
  65. DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
  66. };
  67. /**
  68. * struct udevice - An instance of a driver
  69. *
  70. * This holds information about a device, which is a driver bound to a
  71. * particular port or peripheral (essentially a driver instance).
  72. *
  73. * A device will come into existence through a 'bind' call, either due to
  74. * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
  75. * in the device tree (in which case of_offset is >= 0). In the latter case
  76. * we translate the device tree information into platdata in a function
  77. * implemented by the driver ofdata_to_platdata method (called just before the
  78. * probe method if the device has a device tree node.
  79. *
  80. * All three of platdata, priv and uclass_priv can be allocated by the
  81. * driver, or you can use the auto_alloc_size members of struct driver and
  82. * struct uclass_driver to have driver model do this automatically.
  83. *
  84. * @driver: The driver used by this device
  85. * @name: Name of device, typically the FDT node name
  86. * @platdata: Configuration data for this device
  87. * @parent_platdata: The parent bus's configuration data for this device
  88. * @uclass_platdata: The uclass's configuration data for this device
  89. * @node: Reference to device tree node for this device
  90. * @driver_data: Driver data word for the entry that matched this device with
  91. * its driver
  92. * @parent: Parent of this device, or NULL for the top level device
  93. * @priv: Private data for this device
  94. * @uclass: Pointer to uclass for this device
  95. * @uclass_priv: The uclass's private data for this device
  96. * @parent_priv: The parent's private data for this device
  97. * @uclass_node: Used by uclass to link its devices
  98. * @child_head: List of children of this device
  99. * @sibling_node: Next device in list of all devices
  100. * @flags: Flags for this device DM_FLAG_...
  101. * @req_seq: Requested sequence number for this device (-1 = any)
  102. * @seq: Allocated sequence number for this device (-1 = none). This is set up
  103. * when the device is probed and will be unique within the device's uclass.
  104. * @devres_head: List of memory allocations associated with this device.
  105. * When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
  106. * add to this list. Memory so-allocated will be freed
  107. * automatically when the device is removed / unbound
  108. */
  109. struct udevice {
  110. const struct driver *driver;
  111. const char *name;
  112. void *platdata;
  113. void *parent_platdata;
  114. void *uclass_platdata;
  115. ofnode node;
  116. ulong driver_data;
  117. struct udevice *parent;
  118. void *priv;
  119. struct uclass *uclass;
  120. void *uclass_priv;
  121. void *parent_priv;
  122. struct list_head uclass_node;
  123. struct list_head child_head;
  124. struct list_head sibling_node;
  125. uint32_t flags;
  126. int req_seq;
  127. int seq;
  128. #ifdef CONFIG_DEVRES
  129. struct list_head devres_head;
  130. #endif
  131. };
  132. /* Maximum sequence number supported */
  133. #define DM_MAX_SEQ 999
  134. /* Returns the operations for a device */
  135. #define device_get_ops(dev) (dev->driver->ops)
  136. /* Returns non-zero if the device is active (probed and not removed) */
  137. #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
  138. static inline int dev_of_offset(const struct udevice *dev)
  139. {
  140. return ofnode_to_offset(dev->node);
  141. }
  142. static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
  143. {
  144. dev->node = offset_to_ofnode(of_offset);
  145. }
  146. static inline bool dev_has_of_node(struct udevice *dev)
  147. {
  148. return ofnode_valid(dev->node);
  149. }
  150. /**
  151. * struct udevice_id - Lists the compatible strings supported by a driver
  152. * @compatible: Compatible string
  153. * @data: Data for this compatible string
  154. */
  155. struct udevice_id {
  156. const char *compatible;
  157. ulong data;
  158. };
  159. #if CONFIG_IS_ENABLED(OF_CONTROL)
  160. #define of_match_ptr(_ptr) (_ptr)
  161. #else
  162. #define of_match_ptr(_ptr) NULL
  163. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  164. /**
  165. * struct driver - A driver for a feature or peripheral
  166. *
  167. * This holds methods for setting up a new device, and also removing it.
  168. * The device needs information to set itself up - this is provided either
  169. * by platdata or a device tree node (which we find by looking up
  170. * matching compatible strings with of_match).
  171. *
  172. * Drivers all belong to a uclass, representing a class of devices of the
  173. * same type. Common elements of the drivers can be implemented in the uclass,
  174. * or the uclass can provide a consistent interface to the drivers within
  175. * it.
  176. *
  177. * @name: Device name
  178. * @id: Identifies the uclass we belong to
  179. * @of_match: List of compatible strings to match, and any identifying data
  180. * for each.
  181. * @bind: Called to bind a device to its driver
  182. * @probe: Called to probe a device, i.e. activate it
  183. * @remove: Called to remove a device, i.e. de-activate it
  184. * @unbind: Called to unbind a device from its driver
  185. * @ofdata_to_platdata: Called before probe to decode device tree data
  186. * @child_post_bind: Called after a new child has been bound
  187. * @child_pre_probe: Called before a child device is probed. The device has
  188. * memory allocated but it has not yet been probed.
  189. * @child_post_remove: Called after a child device is removed. The device
  190. * has memory allocated but its device_remove() method has been called.
  191. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  192. * to be allocated in the device's ->priv pointer. If zero, then the driver
  193. * is responsible for allocating any data required.
  194. * @platdata_auto_alloc_size: If non-zero this is the size of the
  195. * platform data to be allocated in the device's ->platdata pointer.
  196. * This is typically only useful for device-tree-aware drivers (those with
  197. * an of_match), since drivers which use platdata will have the data
  198. * provided in the U_BOOT_DEVICE() instantiation.
  199. * @per_child_auto_alloc_size: Each device can hold private data owned by
  200. * its parent. If required this will be automatically allocated if this
  201. * value is non-zero.
  202. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  203. * its children. If non-zero this is the size of this data, to be allocated
  204. * in the child's parent_platdata pointer.
  205. * @ops: Driver-specific operations. This is typically a list of function
  206. * pointers defined by the driver, to implement driver functions required by
  207. * the uclass.
  208. * @flags: driver flags - see DM_FLAGS_...
  209. */
  210. struct driver {
  211. char *name;
  212. enum uclass_id id;
  213. const struct udevice_id *of_match;
  214. int (*bind)(struct udevice *dev);
  215. int (*probe)(struct udevice *dev);
  216. int (*remove)(struct udevice *dev);
  217. int (*unbind)(struct udevice *dev);
  218. int (*ofdata_to_platdata)(struct udevice *dev);
  219. int (*child_post_bind)(struct udevice *dev);
  220. int (*child_pre_probe)(struct udevice *dev);
  221. int (*child_post_remove)(struct udevice *dev);
  222. int priv_auto_alloc_size;
  223. int platdata_auto_alloc_size;
  224. int per_child_auto_alloc_size;
  225. int per_child_platdata_auto_alloc_size;
  226. const void *ops; /* driver-specific operations */
  227. uint32_t flags;
  228. };
  229. /* Declare a new U-Boot driver */
  230. #define U_BOOT_DRIVER(__name) \
  231. ll_entry_declare(struct driver, __name, driver)
  232. /* Get a pointer to a given driver */
  233. #define DM_GET_DRIVER(__name) \
  234. ll_entry_get(struct driver, __name, driver)
  235. /**
  236. * dev_get_platdata() - Get the platform data for a device
  237. *
  238. * This checks that dev is not NULL, but no other checks for now
  239. *
  240. * @dev Device to check
  241. * @return platform data, or NULL if none
  242. */
  243. void *dev_get_platdata(const struct udevice *dev);
  244. /**
  245. * dev_get_parent_platdata() - Get the parent platform data for a device
  246. *
  247. * This checks that dev is not NULL, but no other checks for now
  248. *
  249. * @dev Device to check
  250. * @return parent's platform data, or NULL if none
  251. */
  252. void *dev_get_parent_platdata(const struct udevice *dev);
  253. /**
  254. * dev_get_uclass_platdata() - Get the uclass platform data for a device
  255. *
  256. * This checks that dev is not NULL, but no other checks for now
  257. *
  258. * @dev Device to check
  259. * @return uclass's platform data, or NULL if none
  260. */
  261. void *dev_get_uclass_platdata(const struct udevice *dev);
  262. /**
  263. * dev_get_priv() - Get the private data for a device
  264. *
  265. * This checks that dev is not NULL, but no other checks for now
  266. *
  267. * @dev Device to check
  268. * @return private data, or NULL if none
  269. */
  270. void *dev_get_priv(const struct udevice *dev);
  271. /**
  272. * dev_get_parent_priv() - Get the parent private data for a device
  273. *
  274. * The parent private data is data stored in the device but owned by the
  275. * parent. For example, a USB device may have parent data which contains
  276. * information about how to talk to the device over USB.
  277. *
  278. * This checks that dev is not NULL, but no other checks for now
  279. *
  280. * @dev Device to check
  281. * @return parent data, or NULL if none
  282. */
  283. void *dev_get_parent_priv(const struct udevice *dev);
  284. /**
  285. * dev_get_uclass_priv() - Get the private uclass data for a device
  286. *
  287. * This checks that dev is not NULL, but no other checks for now
  288. *
  289. * @dev Device to check
  290. * @return private uclass data for this device, or NULL if none
  291. */
  292. void *dev_get_uclass_priv(const struct udevice *dev);
  293. /**
  294. * struct dev_get_parent() - Get the parent of a device
  295. *
  296. * @child: Child to check
  297. * @return parent of child, or NULL if this is the root device
  298. */
  299. struct udevice *dev_get_parent(const struct udevice *child);
  300. /**
  301. * dev_get_driver_data() - get the driver data used to bind a device
  302. *
  303. * When a device is bound using a device tree node, it matches a
  304. * particular compatible string in struct udevice_id. This function
  305. * returns the associated data value for that compatible string. This is
  306. * the 'data' field in struct udevice_id.
  307. *
  308. * As an example, consider this structure:
  309. * static const struct udevice_id tegra_i2c_ids[] = {
  310. * { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  311. * { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
  312. * { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
  313. * { }
  314. * };
  315. *
  316. * When driver model finds a driver for this it will store the 'data' value
  317. * corresponding to the compatible string it matches. This function returns
  318. * that value. This allows the driver to handle several variants of a device.
  319. *
  320. * For USB devices, this is the driver_info field in struct usb_device_id.
  321. *
  322. * @dev: Device to check
  323. * @return driver data (0 if none is provided)
  324. */
  325. ulong dev_get_driver_data(const struct udevice *dev);
  326. /**
  327. * dev_get_driver_ops() - get the device's driver's operations
  328. *
  329. * This checks that dev is not NULL, and returns the pointer to device's
  330. * driver's operations.
  331. *
  332. * @dev: Device to check
  333. * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
  334. */
  335. const void *dev_get_driver_ops(const struct udevice *dev);
  336. /**
  337. * device_get_uclass_id() - return the uclass ID of a device
  338. *
  339. * @dev: Device to check
  340. * @return uclass ID for the device
  341. */
  342. enum uclass_id device_get_uclass_id(const struct udevice *dev);
  343. /**
  344. * dev_get_uclass_name() - return the uclass name of a device
  345. *
  346. * This checks that dev is not NULL.
  347. *
  348. * @dev: Device to check
  349. * @return pointer to the uclass name for the device
  350. */
  351. const char *dev_get_uclass_name(const struct udevice *dev);
  352. /**
  353. * device_get_child() - Get the child of a device by index
  354. *
  355. * Returns the numbered child, 0 being the first. This does not use
  356. * sequence numbers, only the natural order.
  357. *
  358. * @dev: Parent device to check
  359. * @index: Child index
  360. * @devp: Returns pointer to device
  361. * @return 0 if OK, -ENODEV if no such device, other error if the device fails
  362. * to probe
  363. */
  364. int device_get_child(struct udevice *parent, int index, struct udevice **devp);
  365. /**
  366. * device_get_child_count() - Get the available child count of a device
  367. *
  368. * Returns the number of children to a device.
  369. *
  370. * @parent: Parent device to check
  371. */
  372. int device_get_child_count(struct udevice *parent);
  373. /**
  374. * device_find_child_by_seq() - Find a child device based on a sequence
  375. *
  376. * This searches for a device with the given seq or req_seq.
  377. *
  378. * For seq, if an active device has this sequence it will be returned.
  379. * If there is no such device then this will return -ENODEV.
  380. *
  381. * For req_seq, if a device (whether activated or not) has this req_seq
  382. * value, that device will be returned. This is a strong indication that
  383. * the device will receive that sequence when activated.
  384. *
  385. * @parent: Parent device
  386. * @seq_or_req_seq: Sequence number to find (0=first)
  387. * @find_req_seq: true to find req_seq, false to find seq
  388. * @devp: Returns pointer to device (there is only one per for each seq).
  389. * Set to NULL if none is found
  390. * @return 0 if OK, -ve on error
  391. */
  392. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  393. bool find_req_seq, struct udevice **devp);
  394. /**
  395. * device_get_child_by_seq() - Get a child device based on a sequence
  396. *
  397. * If an active device has this sequence it will be returned. If there is no
  398. * such device then this will check for a device that is requesting this
  399. * sequence.
  400. *
  401. * The device is probed to activate it ready for use.
  402. *
  403. * @parent: Parent device
  404. * @seq: Sequence number to find (0=first)
  405. * @devp: Returns pointer to device (there is only one per for each seq)
  406. * Set to NULL if none is found
  407. * @return 0 if OK, -ve on error
  408. */
  409. int device_get_child_by_seq(struct udevice *parent, int seq,
  410. struct udevice **devp);
  411. /**
  412. * device_find_child_by_of_offset() - Find a child device based on FDT offset
  413. *
  414. * Locates a child device by its device tree offset.
  415. *
  416. * @parent: Parent device
  417. * @of_offset: Device tree offset to find
  418. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  419. * @return 0 if OK, -ve on error
  420. */
  421. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  422. struct udevice **devp);
  423. /**
  424. * device_get_child_by_of_offset() - Get a child device based on FDT offset
  425. *
  426. * Locates a child device by its device tree offset.
  427. *
  428. * The device is probed to activate it ready for use.
  429. *
  430. * @parent: Parent device
  431. * @of_offset: Device tree offset to find
  432. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  433. * @return 0 if OK, -ve on error
  434. */
  435. int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
  436. struct udevice **devp);
  437. /**
  438. * device_find_global_by_ofnode() - Get a device based on ofnode
  439. *
  440. * Locates a device by its device tree ofnode, searching globally throughout
  441. * the all driver model devices.
  442. *
  443. * The device is NOT probed
  444. *
  445. * @node: Device tree ofnode to find
  446. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  447. * @return 0 if OK, -ve on error
  448. */
  449. int device_find_global_by_ofnode(ofnode node, struct udevice **devp);
  450. /**
  451. * device_get_global_by_ofnode() - Get a device based on ofnode
  452. *
  453. * Locates a device by its device tree ofnode, searching globally throughout
  454. * the all driver model devices.
  455. *
  456. * The device is probed to activate it ready for use.
  457. *
  458. * @node: Device tree ofnode to find
  459. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  460. * @return 0 if OK, -ve on error
  461. */
  462. int device_get_global_by_ofnode(ofnode node, struct udevice **devp);
  463. /**
  464. * device_find_first_child() - Find the first child of a device
  465. *
  466. * @parent: Parent device to search
  467. * @devp: Returns first child device, or NULL if none
  468. * @return 0
  469. */
  470. int device_find_first_child(struct udevice *parent, struct udevice **devp);
  471. /**
  472. * device_find_next_child() - Find the next child of a device
  473. *
  474. * @devp: Pointer to previous child device on entry. Returns pointer to next
  475. * child device, or NULL if none
  476. * @return 0
  477. */
  478. int device_find_next_child(struct udevice **devp);
  479. /**
  480. * device_find_first_inactive_child() - Find the first inactive child
  481. *
  482. * This is used to locate an existing child of a device which is of a given
  483. * uclass.
  484. *
  485. * The device is NOT probed
  486. *
  487. * @parent: Parent device to search
  488. * @uclass_id: Uclass to look for
  489. * @devp: Returns device found, if any
  490. * @return 0 if found, else -ENODEV
  491. */
  492. int device_find_first_inactive_child(struct udevice *parent,
  493. enum uclass_id uclass_id,
  494. struct udevice **devp);
  495. /**
  496. * device_find_first_child_by_uclass() - Find the first child of a device in uc
  497. *
  498. * @parent: Parent device to search
  499. * @uclass_id: Uclass to look for
  500. * @devp: Returns first child device in that uclass, if any
  501. * @return 0 if found, else -ENODEV
  502. */
  503. int device_find_first_child_by_uclass(struct udevice *parent,
  504. enum uclass_id uclass_id,
  505. struct udevice **devp);
  506. /**
  507. * device_find_child_by_name() - Find a child by device name
  508. *
  509. * @parent: Parent device to search
  510. * @name: Name to look for
  511. * @devp: Returns device found, if any
  512. * @return 0 if found, else -ENODEV
  513. */
  514. int device_find_child_by_name(struct udevice *parent, const char *name,
  515. struct udevice **devp);
  516. /**
  517. * device_has_children() - check if a device has any children
  518. *
  519. * @dev: Device to check
  520. * @return true if the device has one or more children
  521. */
  522. bool device_has_children(const struct udevice *dev);
  523. /**
  524. * device_has_active_children() - check if a device has any active children
  525. *
  526. * @dev: Device to check
  527. * @return true if the device has one or more children and at least one of
  528. * them is active (probed).
  529. */
  530. bool device_has_active_children(struct udevice *dev);
  531. /**
  532. * device_is_last_sibling() - check if a device is the last sibling
  533. *
  534. * This function can be useful for display purposes, when special action needs
  535. * to be taken when displaying the last sibling. This can happen when a tree
  536. * view of devices is being displayed.
  537. *
  538. * @dev: Device to check
  539. * @return true if there are no more siblings after this one - i.e. is it
  540. * last in the list.
  541. */
  542. bool device_is_last_sibling(struct udevice *dev);
  543. /**
  544. * device_set_name() - set the name of a device
  545. *
  546. * This must be called in the device's bind() method and no later. Normally
  547. * this is unnecessary but for probed devices which don't get a useful name
  548. * this function can be helpful.
  549. *
  550. * The name is allocated and will be freed automatically when the device is
  551. * unbound.
  552. *
  553. * @dev: Device to update
  554. * @name: New name (this string is allocated new memory and attached to
  555. * the device)
  556. * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
  557. * string
  558. */
  559. int device_set_name(struct udevice *dev, const char *name);
  560. /**
  561. * device_set_name_alloced() - note that a device name is allocated
  562. *
  563. * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
  564. * unbound the name will be freed. This avoids memory leaks.
  565. *
  566. * @dev: Device to update
  567. */
  568. void device_set_name_alloced(struct udevice *dev);
  569. /**
  570. * device_is_compatible() - check if the device is compatible with the compat
  571. *
  572. * This allows to check whether the device is comaptible with the compat.
  573. *
  574. * @dev: udevice pointer for which compatible needs to be verified.
  575. * @compat: Compatible string which needs to verified in the given
  576. * device
  577. * @return true if OK, false if the compatible is not found
  578. */
  579. bool device_is_compatible(struct udevice *dev, const char *compat);
  580. /**
  581. * of_machine_is_compatible() - check if the machine is compatible with
  582. * the compat
  583. *
  584. * This allows to check whether the machine is comaptible with the compat.
  585. *
  586. * @compat: Compatible string which needs to verified
  587. * @return true if OK, false if the compatible is not found
  588. */
  589. bool of_machine_is_compatible(const char *compat);
  590. /**
  591. * dev_disable_by_path() - Disable a device given its device tree path
  592. *
  593. * @path: The device tree path identifying the device to be disabled
  594. * @return 0 on success, -ve on error
  595. */
  596. int dev_disable_by_path(const char *path);
  597. /**
  598. * dev_enable_by_path() - Enable a device given its device tree path
  599. *
  600. * @path: The device tree path identifying the device to be enabled
  601. * @return 0 on success, -ve on error
  602. */
  603. int dev_enable_by_path(const char *path);
  604. /**
  605. * device_is_on_pci_bus - Test if a device is on a PCI bus
  606. *
  607. * @dev: device to test
  608. * @return: true if it is on a PCI bus, false otherwise
  609. */
  610. static inline bool device_is_on_pci_bus(struct udevice *dev)
  611. {
  612. return device_get_uclass_id(dev->parent) == UCLASS_PCI;
  613. }
  614. /**
  615. * device_foreach_child_safe() - iterate through child devices safely
  616. *
  617. * This allows the @pos child to be removed in the loop if required.
  618. *
  619. * @pos: struct udevice * for the current device
  620. * @next: struct udevice * for the next device
  621. * @parent: parent device to scan
  622. */
  623. #define device_foreach_child_safe(pos, next, parent) \
  624. list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
  625. /**
  626. * device_foreach_child() - iterate through child devices
  627. *
  628. * @pos: struct udevice * for the current device
  629. * @parent: parent device to scan
  630. */
  631. #define device_foreach_child(pos, parent) \
  632. list_for_each_entry(pos, &parent->child_head, sibling_node)
  633. /**
  634. * dm_scan_fdt_dev() - Bind child device in a the device tree
  635. *
  636. * This handles device which have sub-nodes in the device tree. It scans all
  637. * sub-nodes and binds drivers for each node where a driver can be found.
  638. *
  639. * If this is called prior to relocation, only pre-relocation devices will be
  640. * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
  641. * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
  642. * be bound.
  643. *
  644. * @dev: Device to scan
  645. * @return 0 if OK, -ve on error
  646. */
  647. int dm_scan_fdt_dev(struct udevice *dev);
  648. /* device resource management */
  649. typedef void (*dr_release_t)(struct udevice *dev, void *res);
  650. typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
  651. #ifdef CONFIG_DEVRES
  652. #ifdef CONFIG_DEBUG_DEVRES
  653. void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  654. const char *name);
  655. #define _devres_alloc(release, size, gfp) \
  656. __devres_alloc(release, size, gfp, #release)
  657. #else
  658. void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  659. #endif
  660. /**
  661. * devres_alloc() - Allocate device resource data
  662. * @release: Release function devres will be associated with
  663. * @size: Allocation size
  664. * @gfp: Allocation flags
  665. *
  666. * Allocate devres of @size bytes. The allocated area is associated
  667. * with @release. The returned pointer can be passed to
  668. * other devres_*() functions.
  669. *
  670. * RETURNS:
  671. * Pointer to allocated devres on success, NULL on failure.
  672. */
  673. #define devres_alloc(release, size, gfp) \
  674. _devres_alloc(release, size, gfp | __GFP_ZERO)
  675. /**
  676. * devres_free() - Free device resource data
  677. * @res: Pointer to devres data to free
  678. *
  679. * Free devres created with devres_alloc().
  680. */
  681. void devres_free(void *res);
  682. /**
  683. * devres_add() - Register device resource
  684. * @dev: Device to add resource to
  685. * @res: Resource to register
  686. *
  687. * Register devres @res to @dev. @res should have been allocated
  688. * using devres_alloc(). On driver detach, the associated release
  689. * function will be invoked and devres will be freed automatically.
  690. */
  691. void devres_add(struct udevice *dev, void *res);
  692. /**
  693. * devres_find() - Find device resource
  694. * @dev: Device to lookup resource from
  695. * @release: Look for resources associated with this release function
  696. * @match: Match function (optional)
  697. * @match_data: Data for the match function
  698. *
  699. * Find the latest devres of @dev which is associated with @release
  700. * and for which @match returns 1. If @match is NULL, it's considered
  701. * to match all.
  702. *
  703. * @return pointer to found devres, NULL if not found.
  704. */
  705. void *devres_find(struct udevice *dev, dr_release_t release,
  706. dr_match_t match, void *match_data);
  707. /**
  708. * devres_get() - Find devres, if non-existent, add one atomically
  709. * @dev: Device to lookup or add devres for
  710. * @new_res: Pointer to new initialized devres to add if not found
  711. * @match: Match function (optional)
  712. * @match_data: Data for the match function
  713. *
  714. * Find the latest devres of @dev which has the same release function
  715. * as @new_res and for which @match return 1. If found, @new_res is
  716. * freed; otherwise, @new_res is added atomically.
  717. *
  718. * @return ointer to found or added devres.
  719. */
  720. void *devres_get(struct udevice *dev, void *new_res,
  721. dr_match_t match, void *match_data);
  722. /**
  723. * devres_remove() - Find a device resource and remove it
  724. * @dev: Device to find resource from
  725. * @release: Look for resources associated with this release function
  726. * @match: Match function (optional)
  727. * @match_data: Data for the match function
  728. *
  729. * Find the latest devres of @dev associated with @release and for
  730. * which @match returns 1. If @match is NULL, it's considered to
  731. * match all. If found, the resource is removed atomically and
  732. * returned.
  733. *
  734. * @return ointer to removed devres on success, NULL if not found.
  735. */
  736. void *devres_remove(struct udevice *dev, dr_release_t release,
  737. dr_match_t match, void *match_data);
  738. /**
  739. * devres_destroy() - Find a device resource and destroy it
  740. * @dev: Device to find resource from
  741. * @release: Look for resources associated with this release function
  742. * @match: Match function (optional)
  743. * @match_data: Data for the match function
  744. *
  745. * Find the latest devres of @dev associated with @release and for
  746. * which @match returns 1. If @match is NULL, it's considered to
  747. * match all. If found, the resource is removed atomically and freed.
  748. *
  749. * Note that the release function for the resource will not be called,
  750. * only the devres-allocated data will be freed. The caller becomes
  751. * responsible for freeing any other data.
  752. *
  753. * @return 0 if devres is found and freed, -ENOENT if not found.
  754. */
  755. int devres_destroy(struct udevice *dev, dr_release_t release,
  756. dr_match_t match, void *match_data);
  757. /**
  758. * devres_release() - Find a device resource and destroy it, calling release
  759. * @dev: Device to find resource from
  760. * @release: Look for resources associated with this release function
  761. * @match: Match function (optional)
  762. * @match_data: Data for the match function
  763. *
  764. * Find the latest devres of @dev associated with @release and for
  765. * which @match returns 1. If @match is NULL, it's considered to
  766. * match all. If found, the resource is removed atomically, the
  767. * release function called and the resource freed.
  768. *
  769. * @return 0 if devres is found and freed, -ENOENT if not found.
  770. */
  771. int devres_release(struct udevice *dev, dr_release_t release,
  772. dr_match_t match, void *match_data);
  773. /* managed devm_k.alloc/kfree for device drivers */
  774. /**
  775. * devm_kmalloc() - Resource-managed kmalloc
  776. * @dev: Device to allocate memory for
  777. * @size: Allocation size
  778. * @gfp: Allocation gfp flags
  779. *
  780. * Managed kmalloc. Memory allocated with this function is
  781. * automatically freed on driver detach. Like all other devres
  782. * resources, guaranteed alignment is unsigned long long.
  783. *
  784. * @return pointer to allocated memory on success, NULL on failure.
  785. */
  786. void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
  787. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  788. {
  789. return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
  790. }
  791. static inline void *devm_kmalloc_array(struct udevice *dev,
  792. size_t n, size_t size, gfp_t flags)
  793. {
  794. if (size != 0 && n > SIZE_MAX / size)
  795. return NULL;
  796. return devm_kmalloc(dev, n * size, flags);
  797. }
  798. static inline void *devm_kcalloc(struct udevice *dev,
  799. size_t n, size_t size, gfp_t flags)
  800. {
  801. return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
  802. }
  803. /**
  804. * devm_kfree() - Resource-managed kfree
  805. * @dev: Device this memory belongs to
  806. * @ptr: Memory to free
  807. *
  808. * Free memory allocated with devm_kmalloc().
  809. */
  810. void devm_kfree(struct udevice *dev, void *ptr);
  811. #else /* ! CONFIG_DEVRES */
  812. static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
  813. {
  814. return kzalloc(size, gfp);
  815. }
  816. static inline void devres_free(void *res)
  817. {
  818. kfree(res);
  819. }
  820. static inline void devres_add(struct udevice *dev, void *res)
  821. {
  822. }
  823. static inline void *devres_find(struct udevice *dev, dr_release_t release,
  824. dr_match_t match, void *match_data)
  825. {
  826. return NULL;
  827. }
  828. static inline void *devres_get(struct udevice *dev, void *new_res,
  829. dr_match_t match, void *match_data)
  830. {
  831. return NULL;
  832. }
  833. static inline void *devres_remove(struct udevice *dev, dr_release_t release,
  834. dr_match_t match, void *match_data)
  835. {
  836. return NULL;
  837. }
  838. static inline int devres_destroy(struct udevice *dev, dr_release_t release,
  839. dr_match_t match, void *match_data)
  840. {
  841. return 0;
  842. }
  843. static inline int devres_release(struct udevice *dev, dr_release_t release,
  844. dr_match_t match, void *match_data)
  845. {
  846. return 0;
  847. }
  848. static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
  849. {
  850. return kmalloc(size, gfp);
  851. }
  852. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  853. {
  854. return kzalloc(size, gfp);
  855. }
  856. static inline void *devm_kmalloc_array(struct udevice *dev,
  857. size_t n, size_t size, gfp_t flags)
  858. {
  859. /* TODO: add kmalloc_array() to linux/compat.h */
  860. if (size != 0 && n > SIZE_MAX / size)
  861. return NULL;
  862. return kmalloc(n * size, flags);
  863. }
  864. static inline void *devm_kcalloc(struct udevice *dev,
  865. size_t n, size_t size, gfp_t flags)
  866. {
  867. /* TODO: add kcalloc() to linux/compat.h */
  868. return kmalloc(n * size, flags | __GFP_ZERO);
  869. }
  870. static inline void devm_kfree(struct udevice *dev, void *ptr)
  871. {
  872. kfree(ptr);
  873. }
  874. #endif /* ! CONFIG_DEVRES */
  875. /*
  876. * REVISIT:
  877. * remove the following after resolving conflicts with <linux/compat.h>
  878. */
  879. #ifdef dev_dbg
  880. #undef dev_dbg
  881. #endif
  882. #ifdef dev_vdbg
  883. #undef dev_vdbg
  884. #endif
  885. #ifdef dev_info
  886. #undef dev_info
  887. #endif
  888. #ifdef dev_err
  889. #undef dev_err
  890. #endif
  891. #ifdef dev_warn
  892. #undef dev_warn
  893. #endif
  894. /*
  895. * REVISIT:
  896. * print device name like Linux
  897. */
  898. #define dev_printk(dev, fmt, ...) \
  899. ({ \
  900. printk(fmt, ##__VA_ARGS__); \
  901. })
  902. #define __dev_printk(level, dev, fmt, ...) \
  903. ({ \
  904. if (level < CONFIG_VAL(LOGLEVEL)) \
  905. dev_printk(dev, fmt, ##__VA_ARGS__); \
  906. })
  907. #define dev_emerg(dev, fmt, ...) \
  908. __dev_printk(0, dev, fmt, ##__VA_ARGS__)
  909. #define dev_alert(dev, fmt, ...) \
  910. __dev_printk(1, dev, fmt, ##__VA_ARGS__)
  911. #define dev_crit(dev, fmt, ...) \
  912. __dev_printk(2, dev, fmt, ##__VA_ARGS__)
  913. #define dev_err(dev, fmt, ...) \
  914. __dev_printk(3, dev, fmt, ##__VA_ARGS__)
  915. #define dev_warn(dev, fmt, ...) \
  916. __dev_printk(4, dev, fmt, ##__VA_ARGS__)
  917. #define dev_notice(dev, fmt, ...) \
  918. __dev_printk(5, dev, fmt, ##__VA_ARGS__)
  919. #define dev_info(dev, fmt, ...) \
  920. __dev_printk(6, dev, fmt, ##__VA_ARGS__)
  921. #ifdef DEBUG
  922. #define dev_dbg(dev, fmt, ...) \
  923. __dev_printk(7, dev, fmt, ##__VA_ARGS__)
  924. #else
  925. #define dev_dbg(dev, fmt, ...) \
  926. ({ \
  927. if (0) \
  928. __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
  929. })
  930. #endif
  931. #ifdef VERBOSE_DEBUG
  932. #define dev_vdbg dev_dbg
  933. #else
  934. #define dev_vdbg(dev, fmt, ...) \
  935. ({ \
  936. if (0) \
  937. __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
  938. })
  939. #endif
  940. #endif