uclass.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. */
  8. #ifndef _DM_UCLASS_H
  9. #define _DM_UCLASS_H
  10. #include <dm/ofnode.h>
  11. #include <dm/uclass-id.h>
  12. #include <linker_lists.h>
  13. #include <linux/list.h>
  14. /**
  15. * struct uclass - a U-Boot drive class, collecting together similar drivers
  16. *
  17. * A uclass provides an interface to a particular function, which is
  18. * implemented by one or more drivers. Every driver belongs to a uclass even
  19. * if it is the only driver in that uclass. An example uclass is GPIO, which
  20. * provides the ability to change read inputs, set and clear outputs, etc.
  21. * There may be drivers for on-chip SoC GPIO banks, I2C GPIO expanders and
  22. * PMIC IO lines, all made available in a unified way through the uclass.
  23. *
  24. * @priv: Private data for this uclass
  25. * @uc_drv: The driver for the uclass itself, not to be confused with a
  26. * 'struct driver'
  27. * @dev_head: List of devices in this uclass (devices are attached to their
  28. * uclass when their bind method is called)
  29. * @sibling_node: Next uclass in the linked list of uclasses
  30. */
  31. struct uclass {
  32. void *priv;
  33. struct uclass_driver *uc_drv;
  34. struct list_head dev_head;
  35. struct list_head sibling_node;
  36. };
  37. struct driver;
  38. struct udevice;
  39. /* Members of this uclass sequence themselves with aliases */
  40. #define DM_UC_FLAG_SEQ_ALIAS (1 << 0)
  41. /* Same as DM_FLAG_ALLOC_PRIV_DMA */
  42. #define DM_UC_FLAG_ALLOC_PRIV_DMA (1 << 5)
  43. /**
  44. * struct uclass_driver - Driver for the uclass
  45. *
  46. * A uclass_driver provides a consistent interface to a set of related
  47. * drivers.
  48. *
  49. * @name: Name of uclass driver
  50. * @id: ID number of this uclass
  51. * @post_bind: Called after a new device is bound to this uclass
  52. * @pre_unbind: Called before a device is unbound from this uclass
  53. * @pre_probe: Called before a new device is probed
  54. * @post_probe: Called after a new device is probed
  55. * @pre_remove: Called before a device is removed
  56. * @child_post_bind: Called after a child is bound to a device in this uclass
  57. * @child_pre_probe: Called before a child in this uclass is probed
  58. * @child_post_probe: Called after a child in this uclass is probed
  59. * @init: Called to set up the uclass
  60. * @destroy: Called to destroy the uclass
  61. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  62. * to be allocated in the uclass's ->priv pointer. If zero, then the uclass
  63. * driver is responsible for allocating any data required.
  64. * @per_device_auto_alloc_size: Each device can hold private data owned
  65. * by the uclass. If required this will be automatically allocated if this
  66. * value is non-zero.
  67. * @per_device_platdata_auto_alloc_size: Each device can hold platform data
  68. * owned by the uclass as 'dev->uclass_platdata'. If the value is non-zero,
  69. * then this will be automatically allocated.
  70. * @per_child_auto_alloc_size: Each child device (of a parent in this
  71. * uclass) can hold parent data for the device/uclass. This value is only
  72. * used as a fallback if this member is 0 in the driver.
  73. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  74. * its children. If non-zero this is the size of this data, to be allocated
  75. * in the child device's parent_platdata pointer. This value is only used as
  76. * a fallback if this member is 0 in the driver.
  77. * @ops: Uclass operations, providing the consistent interface to devices
  78. * within the uclass.
  79. * @flags: Flags for this uclass (DM_UC_...)
  80. */
  81. struct uclass_driver {
  82. const char *name;
  83. enum uclass_id id;
  84. int (*post_bind)(struct udevice *dev);
  85. int (*pre_unbind)(struct udevice *dev);
  86. int (*pre_probe)(struct udevice *dev);
  87. int (*post_probe)(struct udevice *dev);
  88. int (*pre_remove)(struct udevice *dev);
  89. int (*child_post_bind)(struct udevice *dev);
  90. int (*child_pre_probe)(struct udevice *dev);
  91. int (*child_post_probe)(struct udevice *dev);
  92. int (*init)(struct uclass *class);
  93. int (*destroy)(struct uclass *class);
  94. int priv_auto_alloc_size;
  95. int per_device_auto_alloc_size;
  96. int per_device_platdata_auto_alloc_size;
  97. int per_child_auto_alloc_size;
  98. int per_child_platdata_auto_alloc_size;
  99. const void *ops;
  100. uint32_t flags;
  101. };
  102. /* Declare a new uclass_driver */
  103. #define UCLASS_DRIVER(__name) \
  104. ll_entry_declare(struct uclass_driver, __name, uclass)
  105. /**
  106. * uclass_get() - Get a uclass based on an ID, creating it if needed
  107. *
  108. * Every uclass is identified by an ID, a number from 0 to n-1 where n is
  109. * the number of uclasses. This function allows looking up a uclass by its
  110. * ID.
  111. *
  112. * @key: ID to look up
  113. * @ucp: Returns pointer to uclass (there is only one per ID)
  114. * @return 0 if OK, -ve on error
  115. */
  116. int uclass_get(enum uclass_id key, struct uclass **ucp);
  117. /**
  118. * uclass_get_name() - Get the name of a uclass driver
  119. *
  120. * @id: ID to look up
  121. * @returns the name of the uclass driver for that ID, or NULL if none
  122. */
  123. const char *uclass_get_name(enum uclass_id id);
  124. /**
  125. * uclass_get_by_name() - Look up a uclass by its driver name
  126. *
  127. * @name: Name to look up
  128. * @returns the associated uclass ID, or UCLASS_INVALID if not found
  129. */
  130. enum uclass_id uclass_get_by_name(const char *name);
  131. /**
  132. * uclass_get_device() - Get a uclass device based on an ID and index
  133. *
  134. * The device is probed to activate it ready for use.
  135. *
  136. * @id: ID to look up
  137. * @index: Device number within that uclass (0=first)
  138. * @devp: Returns pointer to device (there is only one per for each ID)
  139. * @return 0 if OK, -ve on error
  140. */
  141. int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
  142. /**
  143. * uclass_get_device_by_name() - Get a uclass device by its name
  144. *
  145. * This searches the devices in the uclass for one with the exactly given name.
  146. *
  147. * The device is probed to activate it ready for use.
  148. *
  149. * @id: ID to look up
  150. * @name: name of a device to get
  151. * @devp: Returns pointer to device (the first one with the name)
  152. * @return 0 if OK, -ve on error
  153. */
  154. int uclass_get_device_by_name(enum uclass_id id, const char *name,
  155. struct udevice **devp);
  156. /**
  157. * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence
  158. *
  159. * If an active device has this sequence it will be returned. If there is no
  160. * such device then this will check for a device that is requesting this
  161. * sequence.
  162. *
  163. * The device is probed to activate it ready for use.
  164. *
  165. * @id: ID to look up
  166. * @seq: Sequence number to find (0=first)
  167. * @devp: Returns pointer to device (there is only one for each seq)
  168. * @return 0 if OK, -ve on error
  169. */
  170. int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
  171. /**
  172. * uclass_get_device_by_of_offset() - Get a uclass device by device tree node
  173. *
  174. * This searches the devices in the uclass for one attached to the given
  175. * device tree node.
  176. *
  177. * The device is probed to activate it ready for use.
  178. *
  179. * @id: ID to look up
  180. * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
  181. * @devp: Returns pointer to device (there is only one for each node)
  182. * @return 0 if OK, -ve on error
  183. */
  184. int uclass_get_device_by_of_offset(enum uclass_id id, int node,
  185. struct udevice **devp);
  186. /**
  187. * uclass_get_device_by_ofnode() - Get a uclass device by device tree node
  188. *
  189. * This searches the devices in the uclass for one attached to the given
  190. * device tree node.
  191. *
  192. * The device is probed to activate it ready for use.
  193. *
  194. * @id: ID to look up
  195. * @np: Device tree node to search for (if NULL then -ENODEV is returned)
  196. * @devp: Returns pointer to device (there is only one for each node)
  197. * @return 0 if OK, -ve on error
  198. */
  199. int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
  200. struct udevice **devp);
  201. /**
  202. * uclass_get_device_by_phandle_id() - Get a uclass device by phandle id
  203. *
  204. * This searches the devices in the uclass for one with the given phandle id.
  205. *
  206. * The device is probed to activate it ready for use.
  207. *
  208. * @id: uclass ID to look up
  209. * @phandle_id: the phandle id to look up
  210. * @devp: Returns pointer to device (there is only one for each node). NULL if
  211. * there is no such device.
  212. * @return 0 if OK, -ENODEV if there is no device match the phandle, other
  213. * -ve on error
  214. */
  215. int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
  216. struct udevice **devp);
  217. /**
  218. * uclass_get_device_by_phandle() - Get a uclass device by phandle
  219. *
  220. * This searches the devices in the uclass for one with the given phandle.
  221. *
  222. * The device is probed to activate it ready for use.
  223. *
  224. * @id: uclass ID to look up
  225. * @parent: Parent device containing the phandle pointer
  226. * @name: Name of property in the parent device node
  227. * @devp: Returns pointer to device (there is only one for each node)
  228. * @return 0 if OK, -ENOENT if there is no @name present in the node, other
  229. * -ve on error
  230. */
  231. int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
  232. const char *name, struct udevice **devp);
  233. /**
  234. * uclass_get_device_by_driver() - Get a uclass device for a driver
  235. *
  236. * This searches the devices in the uclass for one that uses the given
  237. * driver. Use DM_GET_DRIVER(name) for the @drv argument, where 'name' is
  238. * the driver name - as used in U_BOOT_DRIVER(name).
  239. *
  240. * The device is probed to activate it ready for use.
  241. *
  242. * @id: ID to look up
  243. * @drv: Driver to look for
  244. * @devp: Returns pointer to the first device with that driver
  245. * @return 0 if OK, -ve on error
  246. */
  247. int uclass_get_device_by_driver(enum uclass_id id, const struct driver *drv,
  248. struct udevice **devp);
  249. /**
  250. * uclass_first_device() - Get the first device in a uclass
  251. *
  252. * The device returned is probed if necessary, and ready for use
  253. *
  254. * This function is useful to start iterating through a list of devices which
  255. * are functioning correctly and can be probed.
  256. *
  257. * @id: Uclass ID to look up
  258. * @devp: Returns pointer to the first device in that uclass if no error
  259. * occurred, or NULL if there is no first device, or an error occurred with
  260. * that device.
  261. * @return 0 if OK (found or not found), other -ve on error
  262. */
  263. int uclass_first_device(enum uclass_id id, struct udevice **devp);
  264. /**
  265. * uclass_first_device_err() - Get the first device in a uclass
  266. *
  267. * The device returned is probed if necessary, and ready for use
  268. *
  269. * @id: Uclass ID to look up
  270. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  271. * @return 0 if found, -ENODEV if not found, other -ve on error
  272. */
  273. int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
  274. /**
  275. * uclass_next_device() - Get the next device in a uclass
  276. *
  277. * The device returned is probed if necessary, and ready for use
  278. *
  279. * This function is useful to iterate through a list of devices which
  280. * are functioning correctly and can be probed.
  281. *
  282. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  283. * to the next device in the uclass if no error occurred, or NULL if there is
  284. * no next device, or an error occurred with that next device.
  285. * @return 0 if OK (found or not found), other -ve on error
  286. */
  287. int uclass_next_device(struct udevice **devp);
  288. /**
  289. * uclass_next_device_err() - Get the next device in a uclass
  290. *
  291. * The device returned is probed if necessary, and ready for use
  292. *
  293. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  294. * to the next device in the uclass if no error occurred, or -ENODEV if
  295. * there is no next device.
  296. * @return 0 if found, -ENODEV if not found, other -ve on error
  297. */
  298. int uclass_next_device_err(struct udevice **devp);
  299. /**
  300. * uclass_first_device_check() - Get the first device in a uclass
  301. *
  302. * The device returned is probed if necessary, and ready for use
  303. *
  304. * This function is useful to start iterating through a list of devices which
  305. * are functioning correctly and can be probed.
  306. *
  307. * @id: Uclass ID to look up
  308. * @devp: Returns pointer to the first device in that uclass, or NULL if there
  309. * is no first device
  310. * @return 0 if OK (found or not found), other -ve on error. If an error occurs
  311. * it is still possible to move to the next device.
  312. */
  313. int uclass_first_device_check(enum uclass_id id, struct udevice **devp);
  314. /**
  315. * uclass_next_device_check() - Get the next device in a uclass
  316. *
  317. * The device returned is probed if necessary, and ready for use
  318. *
  319. * This function is useful to start iterating through a list of devices which
  320. * are functioning correctly and can be probed.
  321. *
  322. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  323. * to the next device in the uclass if any
  324. * @return 0 if OK (found or not found), other -ve on error. If an error occurs
  325. * it is still possible to move to the next device.
  326. */
  327. int uclass_next_device_check(struct udevice **devp);
  328. /**
  329. * uclass_first_device_drvdata() - Find the first device with given driver data
  330. *
  331. * This searches through the devices for a particular uclass looking for one
  332. * that has the given driver data.
  333. *
  334. * @id: Uclass ID to check
  335. * @driver_data: Driver data to search for
  336. * @devp: Returns pointer to the first matching device in that uclass, if found
  337. * @return 0 if found, -ENODEV if not found, other -ve on error
  338. */
  339. int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
  340. struct udevice **devp);
  341. /**
  342. * uclass_resolve_seq() - Resolve a device's sequence number
  343. *
  344. * On entry dev->seq is -1, and dev->req_seq may be -1 (to allocate a
  345. * sequence number automatically, or >= 0 to select a particular number.
  346. * If the requested sequence number is in use, then this device will
  347. * be allocated another one.
  348. *
  349. * Note that the device's seq value is not changed by this function.
  350. *
  351. * @dev: Device for which to allocate sequence number
  352. * @return sequence number allocated, or -ve on error
  353. */
  354. int uclass_resolve_seq(struct udevice *dev);
  355. /**
  356. * uclass_id_foreach_dev() - Helper function to iteration through devices
  357. *
  358. * This creates a for() loop which works through the available devices in
  359. * a uclass ID in order from start to end.
  360. *
  361. * If for some reason the uclass cannot be found, this does nothing.
  362. *
  363. * @id: enum uclass_id ID to use
  364. * @pos: struct udevice * to hold the current device. Set to NULL when there
  365. * are no more devices.
  366. * @uc: temporary uclass variable (struct uclass *)
  367. */
  368. #define uclass_id_foreach_dev(id, pos, uc) \
  369. if (!uclass_get(id, &uc)) \
  370. list_for_each_entry(pos, &uc->dev_head, uclass_node)
  371. /**
  372. * uclass_foreach_dev() - Helper function to iteration through devices
  373. *
  374. * This creates a for() loop which works through the available devices in
  375. * a uclass in order from start to end.
  376. *
  377. * @pos: struct udevice * to hold the current device. Set to NULL when there
  378. * are no more devices.
  379. * @uc: uclass to scan
  380. */
  381. #define uclass_foreach_dev(pos, uc) \
  382. list_for_each_entry(pos, &uc->dev_head, uclass_node)
  383. /**
  384. * uclass_foreach_dev_safe() - Helper function to safely iteration through devs
  385. *
  386. * This creates a for() loop which works through the available devices in
  387. * a uclass in order from start to end. Inside the loop, it is safe to remove
  388. * @pos if required.
  389. *
  390. * @pos: struct udevice * to hold the current device. Set to NULL when there
  391. * are no more devices.
  392. * @next: struct udevice * to hold the next next
  393. * @uc: uclass to scan
  394. */
  395. #define uclass_foreach_dev_safe(pos, next, uc) \
  396. list_for_each_entry_safe(pos, next, &uc->dev_head, uclass_node)
  397. /**
  398. * uclass_foreach_dev_probe() - Helper function to iteration through devices
  399. * of given uclass
  400. *
  401. * This creates a for() loop which works through the available devices in
  402. * a uclass in order from start to end. Devices are probed if necessary,
  403. * and ready for use.
  404. *
  405. * @id: Uclass ID
  406. * @dev: struct udevice * to hold the current device. Set to NULL when there
  407. * are no more devices.
  408. */
  409. #define uclass_foreach_dev_probe(id, dev) \
  410. for (int _ret = uclass_first_device_err(id, &dev); !_ret && dev; \
  411. _ret = uclass_next_device_err(&dev))
  412. #endif