device.h 31 KB

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