uclass.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 (do not access outside driver model)
  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. /* Members of this uclass without aliases don't get a sequence number */
  42. #define DM_UC_FLAG_NO_AUTO_SEQ (1 << 1)
  43. /* Same as DM_FLAG_ALLOC_PRIV_DMA */
  44. #define DM_UC_FLAG_ALLOC_PRIV_DMA (1 << 5)
  45. /**
  46. * struct uclass_driver - Driver for the uclass
  47. *
  48. * A uclass_driver provides a consistent interface to a set of related
  49. * drivers.
  50. *
  51. * @name: Name of uclass driver
  52. * @id: ID number of this uclass
  53. * @post_bind: Called after a new device is bound to this uclass
  54. * @pre_unbind: Called before a device is unbound from this uclass
  55. * @pre_probe: Called before a new device is probed
  56. * @post_probe: Called after a new device is probed
  57. * @pre_remove: Called before a device is removed
  58. * @child_post_bind: Called after a child is bound to a device in this uclass
  59. * @child_pre_probe: Called before a child in this uclass is probed
  60. * @child_post_probe: Called after a child in this uclass is probed
  61. * @init: Called to set up the uclass
  62. * @destroy: Called to destroy the uclass
  63. * @priv_auto: If non-zero this is the size of the private data
  64. * to be allocated in the uclass's ->priv pointer. If zero, then the uclass
  65. * driver is responsible for allocating any data required.
  66. * @per_device_auto: Each device can hold private data owned
  67. * by the uclass. If required this will be automatically allocated if this
  68. * value is non-zero.
  69. * @per_device_plat_auto: Each device can hold platform data
  70. * owned by the uclass as 'dev->uclass_plat'. If the value is non-zero,
  71. * then this will be automatically allocated.
  72. * @per_child_auto: Each child device (of a parent in this
  73. * uclass) can hold parent data for the device/uclass. This value is only
  74. * used as a fallback if this member is 0 in the driver.
  75. * @per_child_plat_auto: A bus likes to store information about
  76. * its children. If non-zero this is the size of this data, to be allocated
  77. * in the child device's parent_plat pointer. This value is only used as
  78. * a fallback if this member is 0 in the driver.
  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;
  95. int per_device_auto;
  96. int per_device_plat_auto;
  97. int per_child_auto;
  98. int per_child_plat_auto;
  99. uint32_t flags;
  100. };
  101. /* Declare a new uclass_driver */
  102. #define UCLASS_DRIVER(__name) \
  103. ll_entry_declare(struct uclass_driver, __name, uclass_driver)
  104. /*
  105. * These two macros DM_UCLASS_DRIVER_REF and DM_UCLASS_DRIVER_REF are only
  106. * allowed in code generated by dtoc, because the ordering is important and if
  107. * other instances creep in then they may mess up the ordering expected by dtoc.
  108. *
  109. * It is OK to use them with 'extern' though, since that does not actually
  110. * add a new record to the linker_list.
  111. */
  112. /**
  113. * DM_UCLASS_DRIVER_REF() - Get a reference to a uclass driver
  114. *
  115. * This is useful in data structures and code for referencing a uclass_driver at
  116. * build time. Before this is used, an extern UCLASS_DRIVER() must have been
  117. * declared.
  118. *
  119. * For example:
  120. *
  121. * extern UCLASS_DRIVER(clk);
  122. *
  123. * struct uclass_driver *drvs[] = {
  124. * DM_UCLASS_DRIVER_REF(clk),
  125. * };
  126. *
  127. * @_name: Name of the uclass_driver. This must be a valid C identifier, used by
  128. * the linker_list.
  129. * @returns struct uclass_driver * for the uclass driver
  130. */
  131. #define DM_UCLASS_DRIVER_REF(_name) \
  132. ll_entry_ref(struct uclass_driver, _name, uclass_driver)
  133. /**
  134. * uclass_get_priv() - Get the private data for a uclass
  135. *
  136. * @uc Uclass to check
  137. * @return private data, or NULL if none
  138. */
  139. void *uclass_get_priv(const struct uclass *uc);
  140. /**
  141. * uclass_get() - Get a uclass based on an ID, creating it if needed
  142. *
  143. * Every uclass is identified by an ID, a number from 0 to n-1 where n is
  144. * the number of uclasses. This function allows looking up a uclass by its
  145. * ID.
  146. *
  147. * @key: ID to look up
  148. * @ucp: Returns pointer to uclass (there is only one per ID)
  149. * @return 0 if OK, -ve on error
  150. */
  151. int uclass_get(enum uclass_id key, struct uclass **ucp);
  152. /**
  153. * uclass_get_name() - Get the name of a uclass driver
  154. *
  155. * @id: ID to look up
  156. * @returns the name of the uclass driver for that ID, or NULL if none
  157. */
  158. const char *uclass_get_name(enum uclass_id id);
  159. /**
  160. * uclass_get_by_name() - Look up a uclass by its driver name
  161. *
  162. * @name: Name to look up
  163. * @returns the associated uclass ID, or UCLASS_INVALID if not found
  164. */
  165. enum uclass_id uclass_get_by_name(const char *name);
  166. /**
  167. * uclass_get_device() - Get a uclass device based on an ID and index
  168. *
  169. * The device is probed to activate it ready for use.
  170. *
  171. * @id: ID to look up
  172. * @index: Device number within that uclass (0=first)
  173. * @devp: Returns pointer to device (there is only one per for each ID)
  174. * @return 0 if OK, -ve on error
  175. */
  176. int uclass_get_device(enum uclass_id id, int index, struct udevice **devp);
  177. /**
  178. * uclass_get_device_by_name() - Get a uclass device by its name
  179. *
  180. * This searches the devices in the uclass for one with the exactly given name.
  181. *
  182. * The device is probed to activate it ready for use.
  183. *
  184. * @id: ID to look up
  185. * @name: name of a device to get
  186. * @devp: Returns pointer to device (the first one with the name)
  187. * @return 0 if OK, -ve on error
  188. */
  189. int uclass_get_device_by_name(enum uclass_id id, const char *name,
  190. struct udevice **devp);
  191. /**
  192. * uclass_get_device_by_seq() - Get a uclass device based on an ID and sequence
  193. *
  194. * If an active device has this sequence it will be returned. If there is no
  195. * such device then this will check for a device that is requesting this
  196. * sequence.
  197. *
  198. * The device is probed to activate it ready for use.
  199. *
  200. * @id: ID to look up
  201. * @seq: Sequence number to find (0=first)
  202. * @devp: Returns pointer to device (there is only one for each seq)
  203. * @return 0 if OK, -ve on error
  204. */
  205. int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp);
  206. /**
  207. * uclass_get_device_by_of_offset() - Get a uclass device by device tree node
  208. *
  209. * This searches the devices in the uclass for one attached to the given
  210. * device tree node.
  211. *
  212. * The device is probed to activate it ready for use.
  213. *
  214. * @id: ID to look up
  215. * @node: Device tree offset to search for (if -ve then -ENODEV is returned)
  216. * @devp: Returns pointer to device (there is only one for each node)
  217. * @return 0 if OK, -ve on error
  218. */
  219. int uclass_get_device_by_of_offset(enum uclass_id id, int node,
  220. struct udevice **devp);
  221. /**
  222. * uclass_get_device_by_ofnode() - Get a uclass device by device tree node
  223. *
  224. * This searches the devices in the uclass for one attached to the given
  225. * device tree node.
  226. *
  227. * The device is probed to activate it ready for use.
  228. *
  229. * @id: ID to look up
  230. * @np: Device tree node to search for (if NULL then -ENODEV is returned)
  231. * @devp: Returns pointer to device (there is only one for each node)
  232. * @return 0 if OK, -ve on error
  233. */
  234. int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
  235. struct udevice **devp);
  236. /**
  237. * uclass_get_device_by_phandle_id() - Get a uclass device by phandle id
  238. *
  239. * This searches the devices in the uclass for one with the given phandle id.
  240. *
  241. * The device is probed to activate it ready for use.
  242. *
  243. * @id: uclass ID to look up
  244. * @phandle_id: the phandle id to look up
  245. * @devp: Returns pointer to device (there is only one for each node). NULL if
  246. * there is no such device.
  247. * @return 0 if OK, -ENODEV if there is no device match the phandle, other
  248. * -ve on error
  249. */
  250. int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
  251. struct udevice **devp);
  252. /**
  253. * uclass_get_device_by_phandle() - Get a uclass device by phandle
  254. *
  255. * This searches the devices in the uclass for one with the given phandle.
  256. *
  257. * The device is probed to activate it ready for use.
  258. *
  259. * @id: uclass ID to look up
  260. * @parent: Parent device containing the phandle pointer
  261. * @name: Name of property in the parent device node
  262. * @devp: Returns pointer to device (there is only one for each node)
  263. * @return 0 if OK, -ENOENT if there is no @name present in the node, other
  264. * -ve on error
  265. */
  266. int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
  267. const char *name, struct udevice **devp);
  268. /**
  269. * uclass_get_device_by_driver() - Get a uclass device for a driver
  270. *
  271. * This searches the devices in the uclass for one that uses the given
  272. * driver. Use DM_DRIVER_GET(name) for the @drv argument, where 'name' is
  273. * the driver name - as used in U_BOOT_DRIVER(name).
  274. *
  275. * The device is probed to activate it ready for use.
  276. *
  277. * @id: ID to look up
  278. * @drv: Driver to look for
  279. * @devp: Returns pointer to the first device with that driver
  280. * @return 0 if OK, -ve on error
  281. */
  282. int uclass_get_device_by_driver(enum uclass_id id, const struct driver *drv,
  283. struct udevice **devp);
  284. /**
  285. * uclass_first_device() - Get the first device in a uclass
  286. *
  287. * The device returned is probed if necessary, and ready for use
  288. *
  289. * This function is useful to start iterating through a list of devices which
  290. * are functioning correctly and can be probed.
  291. *
  292. * @id: Uclass ID to look up
  293. * @devp: Returns pointer to the first device in that uclass if no error
  294. * occurred, or NULL if there is no first device, or an error occurred with
  295. * that device.
  296. * @return 0 if OK (found or not found), other -ve on error
  297. */
  298. int uclass_first_device(enum uclass_id id, struct udevice **devp);
  299. /**
  300. * uclass_first_device_err() - Get the first device in a uclass
  301. *
  302. * The device returned is probed if necessary, and ready for use
  303. *
  304. * @id: Uclass ID to look up
  305. * @devp: Returns pointer to the first device in that uclass, or NULL if none
  306. * @return 0 if found, -ENODEV if not found, other -ve on error
  307. */
  308. int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
  309. /**
  310. * uclass_next_device() - Get the next device in a uclass
  311. *
  312. * The device returned is probed if necessary, and ready for use
  313. *
  314. * This function is useful to iterate through a list of devices which
  315. * are functioning correctly and can be probed.
  316. *
  317. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  318. * to the next device in the uclass if no error occurred, or NULL if there is
  319. * no next device, or an error occurred with that next device.
  320. * @return 0 if OK (found or not found), other -ve on error
  321. */
  322. int uclass_next_device(struct udevice **devp);
  323. /**
  324. * uclass_next_device_err() - Get the next device in a uclass
  325. *
  326. * The device returned is probed if necessary, and ready for use
  327. *
  328. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  329. * to the next device in the uclass if no error occurred, or -ENODEV if
  330. * there is no next device.
  331. * @return 0 if found, -ENODEV if not found, other -ve on error
  332. */
  333. int uclass_next_device_err(struct udevice **devp);
  334. /**
  335. * uclass_first_device_check() - Get the first device in a uclass
  336. *
  337. * The device returned is probed if necessary, and ready for use
  338. *
  339. * This function is useful to start iterating through a list of devices which
  340. * are functioning correctly and can be probed.
  341. *
  342. * @id: Uclass ID to look up
  343. * @devp: Returns pointer to the first device in that uclass, or NULL if there
  344. * is no first device
  345. * @return 0 if OK (found or not found), other -ve on error. If an error occurs
  346. * it is still possible to move to the next device.
  347. */
  348. int uclass_first_device_check(enum uclass_id id, struct udevice **devp);
  349. /**
  350. * uclass_next_device_check() - Get the next device in a uclass
  351. *
  352. * The device returned is probed if necessary, and ready for use
  353. *
  354. * This function is useful to start iterating through a list of devices which
  355. * are functioning correctly and can be probed.
  356. *
  357. * @devp: On entry, pointer to device to lookup. On exit, returns pointer
  358. * to the next device in the uclass if any
  359. * @return 0 if OK (found or not found), other -ve on error. If an error occurs
  360. * it is still possible to move to the next device.
  361. */
  362. int uclass_next_device_check(struct udevice **devp);
  363. /**
  364. * uclass_first_device_drvdata() - Find the first device with given driver data
  365. *
  366. * This searches through the devices for a particular uclass looking for one
  367. * that has the given driver data.
  368. *
  369. * @id: Uclass ID to check
  370. * @driver_data: Driver data to search for
  371. * @devp: Returns pointer to the first matching device in that uclass, if found
  372. * @return 0 if found, -ENODEV if not found, other -ve on error
  373. */
  374. int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
  375. struct udevice **devp);
  376. /**
  377. * uclass_probe_all() - Probe all devices based on an uclass ID
  378. *
  379. * This function probes all devices associated with a uclass by
  380. * looking for its ID.
  381. *
  382. * @id: uclass ID to look up
  383. * @return 0 if OK, other -ve on error
  384. */
  385. int uclass_probe_all(enum uclass_id id);
  386. /**
  387. * uclass_id_foreach_dev() - Helper function to iteration through devices
  388. *
  389. * This creates a for() loop which works through the available devices in
  390. * a uclass ID in order from start to end.
  391. *
  392. * If for some reason the uclass cannot be found, this does nothing.
  393. *
  394. * @id: enum uclass_id ID to use
  395. * @pos: struct udevice * to hold the current device. Set to NULL when there
  396. * are no more devices.
  397. * @uc: temporary uclass variable (struct uclass *)
  398. */
  399. #define uclass_id_foreach_dev(id, pos, uc) \
  400. if (!uclass_get(id, &uc)) \
  401. list_for_each_entry(pos, &uc->dev_head, uclass_node)
  402. /**
  403. * uclass_foreach_dev() - Helper function to iteration through devices
  404. *
  405. * This creates a for() loop which works through the available devices in
  406. * a uclass in order from start to end.
  407. *
  408. * @pos: struct udevice * to hold the current device. Set to NULL when there
  409. * are no more devices.
  410. * @uc: uclass to scan
  411. */
  412. #define uclass_foreach_dev(pos, uc) \
  413. list_for_each_entry(pos, &uc->dev_head, uclass_node)
  414. /**
  415. * uclass_foreach_dev_safe() - Helper function to safely iteration through devs
  416. *
  417. * This creates a for() loop which works through the available devices in
  418. * a uclass in order from start to end. Inside the loop, it is safe to remove
  419. * @pos if required.
  420. *
  421. * @pos: struct udevice * to hold the current device. Set to NULL when there
  422. * are no more devices.
  423. * @next: struct udevice * to hold the next next
  424. * @uc: uclass to scan
  425. */
  426. #define uclass_foreach_dev_safe(pos, next, uc) \
  427. list_for_each_entry_safe(pos, next, &uc->dev_head, uclass_node)
  428. /**
  429. * uclass_foreach_dev_probe() - Helper function to iteration through devices
  430. * of given uclass
  431. *
  432. * This creates a for() loop which works through the available devices in
  433. * a uclass in order from start to end. Devices are probed if necessary,
  434. * and ready for use.
  435. *
  436. * @id: Uclass ID
  437. * @dev: struct udevice * to hold the current device. Set to NULL when there
  438. * are no more devices.
  439. */
  440. #define uclass_foreach_dev_probe(id, dev) \
  441. for (int _ret = uclass_first_device_err(id, &dev); !_ret && dev; \
  442. _ret = uclass_next_device_err(&dev))
  443. #endif