of.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. #ifndef _LINUX_OF_H
  2. #define _LINUX_OF_H
  3. /*
  4. * Definitions for talking to the Open Firmware PROM on
  5. * Power Macintosh and other computers.
  6. *
  7. * Copyright (C) 1996-2005 Paul Mackerras.
  8. *
  9. * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
  10. * Updates for SPARC64 by David S. Miller
  11. * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/types.h>
  19. #include <linux/bitops.h>
  20. #include <linux/errno.h>
  21. #include <linux/kobject.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/topology.h>
  25. #include <linux/notifier.h>
  26. #include <linux/property.h>
  27. #include <linux/list.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/errno.h>
  30. typedef u32 phandle;
  31. typedef u32 ihandle;
  32. struct property {
  33. char *name;
  34. int length;
  35. void *value;
  36. struct property *next;
  37. unsigned long _flags;
  38. unsigned int unique_id;
  39. struct bin_attribute attr;
  40. };
  41. #if defined(CONFIG_SPARC)
  42. struct of_irq_controller;
  43. #endif
  44. struct device_node {
  45. const char *name;
  46. const char *type;
  47. phandle phandle;
  48. const char *full_name;
  49. struct fwnode_handle fwnode;
  50. struct property *properties;
  51. struct property *deadprops; /* removed properties */
  52. struct device_node *parent;
  53. struct device_node *child;
  54. struct device_node *sibling;
  55. struct kobject kobj;
  56. unsigned long _flags;
  57. void *data;
  58. #if defined(CONFIG_SPARC)
  59. const char *path_component_name;
  60. unsigned int unique_id;
  61. struct of_irq_controller *irq_trans;
  62. #endif
  63. };
  64. #define MAX_PHANDLE_ARGS 16
  65. struct of_phandle_args {
  66. struct device_node *np;
  67. int args_count;
  68. uint32_t args[MAX_PHANDLE_ARGS];
  69. };
  70. struct of_reconfig_data {
  71. struct device_node *dn;
  72. struct property *prop;
  73. struct property *old_prop;
  74. };
  75. /* initialize a node */
  76. extern struct kobj_type of_node_ktype;
  77. static inline void of_node_init(struct device_node *node)
  78. {
  79. kobject_init(&node->kobj, &of_node_ktype);
  80. node->fwnode.type = FWNODE_OF;
  81. }
  82. /* true when node is initialized */
  83. static inline int of_node_is_initialized(struct device_node *node)
  84. {
  85. return node && node->kobj.state_initialized;
  86. }
  87. /* true when node is attached (i.e. present on sysfs) */
  88. static inline int of_node_is_attached(struct device_node *node)
  89. {
  90. return node && node->kobj.state_in_sysfs;
  91. }
  92. #ifdef CONFIG_OF_DYNAMIC
  93. extern struct device_node *of_node_get(struct device_node *node);
  94. extern void of_node_put(struct device_node *node);
  95. #else /* CONFIG_OF_DYNAMIC */
  96. /* Dummy ref counting routines - to be implemented later */
  97. static inline struct device_node *of_node_get(struct device_node *node)
  98. {
  99. return node;
  100. }
  101. static inline void of_node_put(struct device_node *node) { }
  102. #endif /* !CONFIG_OF_DYNAMIC */
  103. /* Pointer for first entry in chain of all nodes. */
  104. extern struct device_node *of_root;
  105. extern struct device_node *of_chosen;
  106. extern struct device_node *of_aliases;
  107. extern struct device_node *of_stdout;
  108. extern raw_spinlock_t devtree_lock;
  109. /* flag descriptions (need to be visible even when !CONFIG_OF) */
  110. #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
  111. #define OF_DETACHED 2 /* node has been detached from the device tree */
  112. #define OF_POPULATED 3 /* device already created for the node */
  113. #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
  114. #ifdef CONFIG_OF
  115. static inline bool is_of_node(struct fwnode_handle *fwnode)
  116. {
  117. return fwnode && fwnode->type == FWNODE_OF;
  118. }
  119. static inline struct device_node *of_node(struct fwnode_handle *fwnode)
  120. {
  121. return fwnode ? container_of(fwnode, struct device_node, fwnode) : NULL;
  122. }
  123. static inline bool of_have_populated_dt(void)
  124. {
  125. return of_root != NULL;
  126. }
  127. static inline bool of_node_is_root(const struct device_node *node)
  128. {
  129. return node && (node->parent == NULL);
  130. }
  131. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  132. {
  133. return test_bit(flag, &n->_flags);
  134. }
  135. static inline int of_node_test_and_set_flag(struct device_node *n,
  136. unsigned long flag)
  137. {
  138. return test_and_set_bit(flag, &n->_flags);
  139. }
  140. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  141. {
  142. set_bit(flag, &n->_flags);
  143. }
  144. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  145. {
  146. clear_bit(flag, &n->_flags);
  147. }
  148. static inline int of_property_check_flag(struct property *p, unsigned long flag)
  149. {
  150. return test_bit(flag, &p->_flags);
  151. }
  152. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  153. {
  154. set_bit(flag, &p->_flags);
  155. }
  156. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  157. {
  158. clear_bit(flag, &p->_flags);
  159. }
  160. extern struct device_node *__of_find_all_nodes(struct device_node *prev);
  161. extern struct device_node *of_find_all_nodes(struct device_node *prev);
  162. /*
  163. * OF address retrieval & translation
  164. */
  165. /* Helper to read a big number; size is in cells (not bytes) */
  166. static inline u64 of_read_number(const __be32 *cell, int size)
  167. {
  168. u64 r = 0;
  169. while (size--)
  170. r = (r << 32) | be32_to_cpu(*(cell++));
  171. return r;
  172. }
  173. /* Like of_read_number, but we want an unsigned long result */
  174. static inline unsigned long of_read_ulong(const __be32 *cell, int size)
  175. {
  176. /* toss away upper bits if unsigned long is smaller than u64 */
  177. return of_read_number(cell, size);
  178. }
  179. #if defined(CONFIG_SPARC)
  180. #include <asm/prom.h>
  181. #endif
  182. /* Default #address and #size cells. Allow arch asm/prom.h to override */
  183. #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
  184. #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
  185. #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
  186. #endif
  187. /* Default string compare functions, Allow arch asm/prom.h to override */
  188. #if !defined(of_compat_cmp)
  189. #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
  190. #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
  191. #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
  192. #endif
  193. #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
  194. #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
  195. #define OF_BAD_ADDR ((u64)-1)
  196. static inline const char *of_node_full_name(const struct device_node *np)
  197. {
  198. return np ? np->full_name : "<no-node>";
  199. }
  200. #define for_each_of_allnodes_from(from, dn) \
  201. for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
  202. #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
  203. extern struct device_node *of_find_node_by_name(struct device_node *from,
  204. const char *name);
  205. extern struct device_node *of_find_node_by_type(struct device_node *from,
  206. const char *type);
  207. extern struct device_node *of_find_compatible_node(struct device_node *from,
  208. const char *type, const char *compat);
  209. extern struct device_node *of_find_matching_node_and_match(
  210. struct device_node *from,
  211. const struct of_device_id *matches,
  212. const struct of_device_id **match);
  213. extern struct device_node *of_find_node_opts_by_path(const char *path,
  214. const char **opts);
  215. static inline struct device_node *of_find_node_by_path(const char *path)
  216. {
  217. return of_find_node_opts_by_path(path, NULL);
  218. }
  219. extern struct device_node *of_find_node_by_phandle(phandle handle);
  220. extern struct device_node *of_get_parent(const struct device_node *node);
  221. extern struct device_node *of_get_next_parent(struct device_node *node);
  222. extern struct device_node *of_get_next_child(const struct device_node *node,
  223. struct device_node *prev);
  224. extern struct device_node *of_get_next_available_child(
  225. const struct device_node *node, struct device_node *prev);
  226. extern struct device_node *of_get_child_by_name(const struct device_node *node,
  227. const char *name);
  228. /* cache lookup */
  229. extern struct device_node *of_find_next_cache_node(const struct device_node *);
  230. extern struct device_node *of_find_node_with_property(
  231. struct device_node *from, const char *prop_name);
  232. extern struct property *of_find_property(const struct device_node *np,
  233. const char *name,
  234. int *lenp);
  235. extern int of_property_count_elems_of_size(const struct device_node *np,
  236. const char *propname, int elem_size);
  237. extern int of_property_read_u32_index(const struct device_node *np,
  238. const char *propname,
  239. u32 index, u32 *out_value);
  240. extern int of_property_read_u8_array(const struct device_node *np,
  241. const char *propname, u8 *out_values, size_t sz);
  242. extern int of_property_read_u16_array(const struct device_node *np,
  243. const char *propname, u16 *out_values, size_t sz);
  244. extern int of_property_read_u32_array(const struct device_node *np,
  245. const char *propname,
  246. u32 *out_values,
  247. size_t sz);
  248. extern int of_property_read_u64(const struct device_node *np,
  249. const char *propname, u64 *out_value);
  250. extern int of_property_read_u64_array(const struct device_node *np,
  251. const char *propname,
  252. u64 *out_values,
  253. size_t sz);
  254. extern int of_property_read_string(struct device_node *np,
  255. const char *propname,
  256. const char **out_string);
  257. extern int of_property_match_string(struct device_node *np,
  258. const char *propname,
  259. const char *string);
  260. extern int of_property_read_string_helper(struct device_node *np,
  261. const char *propname,
  262. const char **out_strs, size_t sz, int index);
  263. extern int of_device_is_compatible(const struct device_node *device,
  264. const char *);
  265. extern bool of_device_is_available(const struct device_node *device);
  266. extern bool of_device_is_big_endian(const struct device_node *device);
  267. extern const void *of_get_property(const struct device_node *node,
  268. const char *name,
  269. int *lenp);
  270. extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
  271. #define for_each_property_of_node(dn, pp) \
  272. for (pp = dn->properties; pp != NULL; pp = pp->next)
  273. extern int of_n_addr_cells(struct device_node *np);
  274. extern int of_n_size_cells(struct device_node *np);
  275. extern const struct of_device_id *of_match_node(
  276. const struct of_device_id *matches, const struct device_node *node);
  277. extern int of_modalias_node(struct device_node *node, char *modalias, int len);
  278. extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
  279. extern struct device_node *of_parse_phandle(const struct device_node *np,
  280. const char *phandle_name,
  281. int index);
  282. extern int of_parse_phandle_with_args(const struct device_node *np,
  283. const char *list_name, const char *cells_name, int index,
  284. struct of_phandle_args *out_args);
  285. extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
  286. const char *list_name, int cells_count, int index,
  287. struct of_phandle_args *out_args);
  288. extern int of_count_phandle_with_args(const struct device_node *np,
  289. const char *list_name, const char *cells_name);
  290. extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
  291. extern int of_alias_get_id(struct device_node *np, const char *stem);
  292. extern int of_alias_get_highest_id(const char *stem);
  293. extern int of_machine_is_compatible(const char *compat);
  294. extern int of_add_property(struct device_node *np, struct property *prop);
  295. extern int of_remove_property(struct device_node *np, struct property *prop);
  296. extern int of_update_property(struct device_node *np, struct property *newprop);
  297. /* For updating the device tree at runtime */
  298. #define OF_RECONFIG_ATTACH_NODE 0x0001
  299. #define OF_RECONFIG_DETACH_NODE 0x0002
  300. #define OF_RECONFIG_ADD_PROPERTY 0x0003
  301. #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
  302. #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
  303. extern int of_attach_node(struct device_node *);
  304. extern int of_detach_node(struct device_node *);
  305. #define of_match_ptr(_ptr) (_ptr)
  306. /*
  307. * struct property *prop;
  308. * const __be32 *p;
  309. * u32 u;
  310. *
  311. * of_property_for_each_u32(np, "propname", prop, p, u)
  312. * printk("U32 value: %x\n", u);
  313. */
  314. const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
  315. u32 *pu);
  316. /*
  317. * struct property *prop;
  318. * const char *s;
  319. *
  320. * of_property_for_each_string(np, "propname", prop, s)
  321. * printk("String value: %s\n", s);
  322. */
  323. const char *of_prop_next_string(struct property *prop, const char *cur);
  324. bool of_console_check(struct device_node *dn, char *name, int index);
  325. #else /* CONFIG_OF */
  326. static inline bool is_of_node(struct fwnode_handle *fwnode)
  327. {
  328. return false;
  329. }
  330. static inline struct device_node *of_node(struct fwnode_handle *fwnode)
  331. {
  332. return NULL;
  333. }
  334. static inline const char* of_node_full_name(const struct device_node *np)
  335. {
  336. return "<no-node>";
  337. }
  338. static inline struct device_node *of_find_node_by_name(struct device_node *from,
  339. const char *name)
  340. {
  341. return NULL;
  342. }
  343. static inline struct device_node *of_find_node_by_type(struct device_node *from,
  344. const char *type)
  345. {
  346. return NULL;
  347. }
  348. static inline struct device_node *of_find_matching_node_and_match(
  349. struct device_node *from,
  350. const struct of_device_id *matches,
  351. const struct of_device_id **match)
  352. {
  353. return NULL;
  354. }
  355. static inline struct device_node *of_find_node_by_path(const char *path)
  356. {
  357. return NULL;
  358. }
  359. static inline struct device_node *of_find_node_opts_by_path(const char *path,
  360. const char **opts)
  361. {
  362. return NULL;
  363. }
  364. static inline struct device_node *of_find_node_by_phandle(phandle handle)
  365. {
  366. return NULL;
  367. }
  368. static inline struct device_node *of_get_parent(const struct device_node *node)
  369. {
  370. return NULL;
  371. }
  372. static inline struct device_node *of_get_next_child(
  373. const struct device_node *node, struct device_node *prev)
  374. {
  375. return NULL;
  376. }
  377. static inline struct device_node *of_get_next_available_child(
  378. const struct device_node *node, struct device_node *prev)
  379. {
  380. return NULL;
  381. }
  382. static inline struct device_node *of_find_node_with_property(
  383. struct device_node *from, const char *prop_name)
  384. {
  385. return NULL;
  386. }
  387. static inline bool of_have_populated_dt(void)
  388. {
  389. return false;
  390. }
  391. static inline struct device_node *of_get_child_by_name(
  392. const struct device_node *node,
  393. const char *name)
  394. {
  395. return NULL;
  396. }
  397. static inline int of_device_is_compatible(const struct device_node *device,
  398. const char *name)
  399. {
  400. return 0;
  401. }
  402. static inline bool of_device_is_available(const struct device_node *device)
  403. {
  404. return false;
  405. }
  406. static inline bool of_device_is_big_endian(const struct device_node *device)
  407. {
  408. return false;
  409. }
  410. static inline struct property *of_find_property(const struct device_node *np,
  411. const char *name,
  412. int *lenp)
  413. {
  414. return NULL;
  415. }
  416. static inline struct device_node *of_find_compatible_node(
  417. struct device_node *from,
  418. const char *type,
  419. const char *compat)
  420. {
  421. return NULL;
  422. }
  423. static inline int of_property_count_elems_of_size(const struct device_node *np,
  424. const char *propname, int elem_size)
  425. {
  426. return -ENOSYS;
  427. }
  428. static inline int of_property_read_u32_index(const struct device_node *np,
  429. const char *propname, u32 index, u32 *out_value)
  430. {
  431. return -ENOSYS;
  432. }
  433. static inline int of_property_read_u8_array(const struct device_node *np,
  434. const char *propname, u8 *out_values, size_t sz)
  435. {
  436. return -ENOSYS;
  437. }
  438. static inline int of_property_read_u16_array(const struct device_node *np,
  439. const char *propname, u16 *out_values, size_t sz)
  440. {
  441. return -ENOSYS;
  442. }
  443. static inline int of_property_read_u32_array(const struct device_node *np,
  444. const char *propname,
  445. u32 *out_values, size_t sz)
  446. {
  447. return -ENOSYS;
  448. }
  449. static inline int of_property_read_u64_array(const struct device_node *np,
  450. const char *propname,
  451. u64 *out_values, size_t sz)
  452. {
  453. return -ENOSYS;
  454. }
  455. static inline int of_property_read_string(struct device_node *np,
  456. const char *propname,
  457. const char **out_string)
  458. {
  459. return -ENOSYS;
  460. }
  461. static inline int of_property_read_string_helper(struct device_node *np,
  462. const char *propname,
  463. const char **out_strs, size_t sz, int index)
  464. {
  465. return -ENOSYS;
  466. }
  467. static inline const void *of_get_property(const struct device_node *node,
  468. const char *name,
  469. int *lenp)
  470. {
  471. return NULL;
  472. }
  473. static inline struct device_node *of_get_cpu_node(int cpu,
  474. unsigned int *thread)
  475. {
  476. return NULL;
  477. }
  478. static inline int of_property_read_u64(const struct device_node *np,
  479. const char *propname, u64 *out_value)
  480. {
  481. return -ENOSYS;
  482. }
  483. static inline int of_property_match_string(struct device_node *np,
  484. const char *propname,
  485. const char *string)
  486. {
  487. return -ENOSYS;
  488. }
  489. static inline struct device_node *of_parse_phandle(const struct device_node *np,
  490. const char *phandle_name,
  491. int index)
  492. {
  493. return NULL;
  494. }
  495. static inline int of_parse_phandle_with_args(struct device_node *np,
  496. const char *list_name,
  497. const char *cells_name,
  498. int index,
  499. struct of_phandle_args *out_args)
  500. {
  501. return -ENOSYS;
  502. }
  503. static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
  504. const char *list_name, int cells_count, int index,
  505. struct of_phandle_args *out_args)
  506. {
  507. return -ENOSYS;
  508. }
  509. static inline int of_count_phandle_with_args(struct device_node *np,
  510. const char *list_name,
  511. const char *cells_name)
  512. {
  513. return -ENOSYS;
  514. }
  515. static inline int of_alias_get_id(struct device_node *np, const char *stem)
  516. {
  517. return -ENOSYS;
  518. }
  519. static inline int of_alias_get_highest_id(const char *stem)
  520. {
  521. return -ENOSYS;
  522. }
  523. static inline int of_machine_is_compatible(const char *compat)
  524. {
  525. return 0;
  526. }
  527. static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
  528. {
  529. return false;
  530. }
  531. static inline const __be32 *of_prop_next_u32(struct property *prop,
  532. const __be32 *cur, u32 *pu)
  533. {
  534. return NULL;
  535. }
  536. static inline const char *of_prop_next_string(struct property *prop,
  537. const char *cur)
  538. {
  539. return NULL;
  540. }
  541. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  542. {
  543. return 0;
  544. }
  545. static inline int of_node_test_and_set_flag(struct device_node *n,
  546. unsigned long flag)
  547. {
  548. return 0;
  549. }
  550. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  551. {
  552. }
  553. static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
  554. {
  555. }
  556. static inline int of_property_check_flag(struct property *p, unsigned long flag)
  557. {
  558. return 0;
  559. }
  560. static inline void of_property_set_flag(struct property *p, unsigned long flag)
  561. {
  562. }
  563. static inline void of_property_clear_flag(struct property *p, unsigned long flag)
  564. {
  565. }
  566. #define of_match_ptr(_ptr) NULL
  567. #define of_match_node(_matches, _node) NULL
  568. #endif /* CONFIG_OF */
  569. #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
  570. extern int of_node_to_nid(struct device_node *np);
  571. #else
  572. static inline int of_node_to_nid(struct device_node *device)
  573. {
  574. return NUMA_NO_NODE;
  575. }
  576. #endif
  577. static inline struct device_node *of_find_matching_node(
  578. struct device_node *from,
  579. const struct of_device_id *matches)
  580. {
  581. return of_find_matching_node_and_match(from, matches, NULL);
  582. }
  583. /**
  584. * of_property_count_u8_elems - Count the number of u8 elements in a property
  585. *
  586. * @np: device node from which the property value is to be read.
  587. * @propname: name of the property to be searched.
  588. *
  589. * Search for a property in a device node and count the number of u8 elements
  590. * in it. Returns number of elements on sucess, -EINVAL if the property does
  591. * not exist or its length does not match a multiple of u8 and -ENODATA if the
  592. * property does not have a value.
  593. */
  594. static inline int of_property_count_u8_elems(const struct device_node *np,
  595. const char *propname)
  596. {
  597. return of_property_count_elems_of_size(np, propname, sizeof(u8));
  598. }
  599. /**
  600. * of_property_count_u16_elems - Count the number of u16 elements in a property
  601. *
  602. * @np: device node from which the property value is to be read.
  603. * @propname: name of the property to be searched.
  604. *
  605. * Search for a property in a device node and count the number of u16 elements
  606. * in it. Returns number of elements on sucess, -EINVAL if the property does
  607. * not exist or its length does not match a multiple of u16 and -ENODATA if the
  608. * property does not have a value.
  609. */
  610. static inline int of_property_count_u16_elems(const struct device_node *np,
  611. const char *propname)
  612. {
  613. return of_property_count_elems_of_size(np, propname, sizeof(u16));
  614. }
  615. /**
  616. * of_property_count_u32_elems - Count the number of u32 elements in a property
  617. *
  618. * @np: device node from which the property value is to be read.
  619. * @propname: name of the property to be searched.
  620. *
  621. * Search for a property in a device node and count the number of u32 elements
  622. * in it. Returns number of elements on sucess, -EINVAL if the property does
  623. * not exist or its length does not match a multiple of u32 and -ENODATA if the
  624. * property does not have a value.
  625. */
  626. static inline int of_property_count_u32_elems(const struct device_node *np,
  627. const char *propname)
  628. {
  629. return of_property_count_elems_of_size(np, propname, sizeof(u32));
  630. }
  631. /**
  632. * of_property_count_u64_elems - Count the number of u64 elements in a property
  633. *
  634. * @np: device node from which the property value is to be read.
  635. * @propname: name of the property to be searched.
  636. *
  637. * Search for a property in a device node and count the number of u64 elements
  638. * in it. Returns number of elements on sucess, -EINVAL if the property does
  639. * not exist or its length does not match a multiple of u64 and -ENODATA if the
  640. * property does not have a value.
  641. */
  642. static inline int of_property_count_u64_elems(const struct device_node *np,
  643. const char *propname)
  644. {
  645. return of_property_count_elems_of_size(np, propname, sizeof(u64));
  646. }
  647. /**
  648. * of_property_read_string_array() - Read an array of strings from a multiple
  649. * strings property.
  650. * @np: device node from which the property value is to be read.
  651. * @propname: name of the property to be searched.
  652. * @out_strs: output array of string pointers.
  653. * @sz: number of array elements to read.
  654. *
  655. * Search for a property in a device tree node and retrieve a list of
  656. * terminated string values (pointer to data, not a copy) in that property.
  657. *
  658. * If @out_strs is NULL, the number of strings in the property is returned.
  659. */
  660. static inline int of_property_read_string_array(struct device_node *np,
  661. const char *propname, const char **out_strs,
  662. size_t sz)
  663. {
  664. return of_property_read_string_helper(np, propname, out_strs, sz, 0);
  665. }
  666. /**
  667. * of_property_count_strings() - Find and return the number of strings from a
  668. * multiple strings property.
  669. * @np: device node from which the property value is to be read.
  670. * @propname: name of the property to be searched.
  671. *
  672. * Search for a property in a device tree node and retrieve the number of null
  673. * terminated string contain in it. Returns the number of strings on
  674. * success, -EINVAL if the property does not exist, -ENODATA if property
  675. * does not have a value, and -EILSEQ if the string is not null-terminated
  676. * within the length of the property data.
  677. */
  678. static inline int of_property_count_strings(struct device_node *np,
  679. const char *propname)
  680. {
  681. return of_property_read_string_helper(np, propname, NULL, 0, 0);
  682. }
  683. /**
  684. * of_property_read_string_index() - Find and read a string from a multiple
  685. * strings property.
  686. * @np: device node from which the property value is to be read.
  687. * @propname: name of the property to be searched.
  688. * @index: index of the string in the list of strings
  689. * @out_string: pointer to null terminated return string, modified only if
  690. * return value is 0.
  691. *
  692. * Search for a property in a device tree node and retrieve a null
  693. * terminated string value (pointer to data, not a copy) in the list of strings
  694. * contained in that property.
  695. * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
  696. * property does not have a value, and -EILSEQ if the string is not
  697. * null-terminated within the length of the property data.
  698. *
  699. * The out_string pointer is modified only if a valid string can be decoded.
  700. */
  701. static inline int of_property_read_string_index(struct device_node *np,
  702. const char *propname,
  703. int index, const char **output)
  704. {
  705. int rc = of_property_read_string_helper(np, propname, output, 1, index);
  706. return rc < 0 ? rc : 0;
  707. }
  708. /**
  709. * of_property_read_bool - Findfrom a property
  710. * @np: device node from which the property value is to be read.
  711. * @propname: name of the property to be searched.
  712. *
  713. * Search for a property in a device node.
  714. * Returns true if the property exists false otherwise.
  715. */
  716. static inline bool of_property_read_bool(const struct device_node *np,
  717. const char *propname)
  718. {
  719. struct property *prop = of_find_property(np, propname, NULL);
  720. return prop ? true : false;
  721. }
  722. static inline int of_property_read_u8(const struct device_node *np,
  723. const char *propname,
  724. u8 *out_value)
  725. {
  726. return of_property_read_u8_array(np, propname, out_value, 1);
  727. }
  728. static inline int of_property_read_u16(const struct device_node *np,
  729. const char *propname,
  730. u16 *out_value)
  731. {
  732. return of_property_read_u16_array(np, propname, out_value, 1);
  733. }
  734. static inline int of_property_read_u32(const struct device_node *np,
  735. const char *propname,
  736. u32 *out_value)
  737. {
  738. return of_property_read_u32_array(np, propname, out_value, 1);
  739. }
  740. static inline int of_property_read_s32(const struct device_node *np,
  741. const char *propname,
  742. s32 *out_value)
  743. {
  744. return of_property_read_u32(np, propname, (u32*) out_value);
  745. }
  746. #define of_property_for_each_u32(np, propname, prop, p, u) \
  747. for (prop = of_find_property(np, propname, NULL), \
  748. p = of_prop_next_u32(prop, NULL, &u); \
  749. p; \
  750. p = of_prop_next_u32(prop, p, &u))
  751. #define of_property_for_each_string(np, propname, prop, s) \
  752. for (prop = of_find_property(np, propname, NULL), \
  753. s = of_prop_next_string(prop, NULL); \
  754. s; \
  755. s = of_prop_next_string(prop, s))
  756. #define for_each_node_by_name(dn, name) \
  757. for (dn = of_find_node_by_name(NULL, name); dn; \
  758. dn = of_find_node_by_name(dn, name))
  759. #define for_each_node_by_type(dn, type) \
  760. for (dn = of_find_node_by_type(NULL, type); dn; \
  761. dn = of_find_node_by_type(dn, type))
  762. #define for_each_compatible_node(dn, type, compatible) \
  763. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  764. dn = of_find_compatible_node(dn, type, compatible))
  765. #define for_each_matching_node(dn, matches) \
  766. for (dn = of_find_matching_node(NULL, matches); dn; \
  767. dn = of_find_matching_node(dn, matches))
  768. #define for_each_matching_node_and_match(dn, matches, match) \
  769. for (dn = of_find_matching_node_and_match(NULL, matches, match); \
  770. dn; dn = of_find_matching_node_and_match(dn, matches, match))
  771. #define for_each_child_of_node(parent, child) \
  772. for (child = of_get_next_child(parent, NULL); child != NULL; \
  773. child = of_get_next_child(parent, child))
  774. #define for_each_available_child_of_node(parent, child) \
  775. for (child = of_get_next_available_child(parent, NULL); child != NULL; \
  776. child = of_get_next_available_child(parent, child))
  777. #define for_each_node_with_property(dn, prop_name) \
  778. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  779. dn = of_find_node_with_property(dn, prop_name))
  780. static inline int of_get_child_count(const struct device_node *np)
  781. {
  782. struct device_node *child;
  783. int num = 0;
  784. for_each_child_of_node(np, child)
  785. num++;
  786. return num;
  787. }
  788. static inline int of_get_available_child_count(const struct device_node *np)
  789. {
  790. struct device_node *child;
  791. int num = 0;
  792. for_each_available_child_of_node(np, child)
  793. num++;
  794. return num;
  795. }
  796. #ifdef CONFIG_OF
  797. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  798. static const struct of_device_id __of_table_##name \
  799. __used __section(__##table##_of_table) \
  800. = { .compatible = compat, \
  801. .data = (fn == (fn_type)NULL) ? fn : fn }
  802. #else
  803. #define _OF_DECLARE(table, name, compat, fn, fn_type) \
  804. static const struct of_device_id __of_table_##name \
  805. __attribute__((unused)) \
  806. = { .compatible = compat, \
  807. .data = (fn == (fn_type)NULL) ? fn : fn }
  808. #endif
  809. typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
  810. typedef void (*of_init_fn_1)(struct device_node *);
  811. #define OF_DECLARE_1(table, name, compat, fn) \
  812. _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
  813. #define OF_DECLARE_2(table, name, compat, fn) \
  814. _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
  815. /**
  816. * struct of_changeset_entry - Holds a changeset entry
  817. *
  818. * @node: list_head for the log list
  819. * @action: notifier action
  820. * @np: pointer to the device node affected
  821. * @prop: pointer to the property affected
  822. * @old_prop: hold a pointer to the original property
  823. *
  824. * Every modification of the device tree during a changeset
  825. * is held in a list of of_changeset_entry structures.
  826. * That way we can recover from a partial application, or we can
  827. * revert the changeset
  828. */
  829. struct of_changeset_entry {
  830. struct list_head node;
  831. unsigned long action;
  832. struct device_node *np;
  833. struct property *prop;
  834. struct property *old_prop;
  835. };
  836. /**
  837. * struct of_changeset - changeset tracker structure
  838. *
  839. * @entries: list_head for the changeset entries
  840. *
  841. * changesets are a convenient way to apply bulk changes to the
  842. * live tree. In case of an error, changes are rolled-back.
  843. * changesets live on after initial application, and if not
  844. * destroyed after use, they can be reverted in one single call.
  845. */
  846. struct of_changeset {
  847. struct list_head entries;
  848. };
  849. enum of_reconfig_change {
  850. OF_RECONFIG_NO_CHANGE = 0,
  851. OF_RECONFIG_CHANGE_ADD,
  852. OF_RECONFIG_CHANGE_REMOVE,
  853. };
  854. #ifdef CONFIG_OF_DYNAMIC
  855. extern int of_reconfig_notifier_register(struct notifier_block *);
  856. extern int of_reconfig_notifier_unregister(struct notifier_block *);
  857. extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
  858. extern int of_reconfig_get_state_change(unsigned long action,
  859. struct of_reconfig_data *arg);
  860. extern void of_changeset_init(struct of_changeset *ocs);
  861. extern void of_changeset_destroy(struct of_changeset *ocs);
  862. extern int of_changeset_apply(struct of_changeset *ocs);
  863. extern int of_changeset_revert(struct of_changeset *ocs);
  864. extern int of_changeset_action(struct of_changeset *ocs,
  865. unsigned long action, struct device_node *np,
  866. struct property *prop);
  867. static inline int of_changeset_attach_node(struct of_changeset *ocs,
  868. struct device_node *np)
  869. {
  870. return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
  871. }
  872. static inline int of_changeset_detach_node(struct of_changeset *ocs,
  873. struct device_node *np)
  874. {
  875. return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
  876. }
  877. static inline int of_changeset_add_property(struct of_changeset *ocs,
  878. struct device_node *np, struct property *prop)
  879. {
  880. return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
  881. }
  882. static inline int of_changeset_remove_property(struct of_changeset *ocs,
  883. struct device_node *np, struct property *prop)
  884. {
  885. return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
  886. }
  887. static inline int of_changeset_update_property(struct of_changeset *ocs,
  888. struct device_node *np, struct property *prop)
  889. {
  890. return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
  891. }
  892. #else /* CONFIG_OF_DYNAMIC */
  893. static inline int of_reconfig_notifier_register(struct notifier_block *nb)
  894. {
  895. return -EINVAL;
  896. }
  897. static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
  898. {
  899. return -EINVAL;
  900. }
  901. static inline int of_reconfig_notify(unsigned long action,
  902. struct of_reconfig_data *arg)
  903. {
  904. return -EINVAL;
  905. }
  906. static inline int of_reconfig_get_state_change(unsigned long action,
  907. struct of_reconfig_data *arg)
  908. {
  909. return -EINVAL;
  910. }
  911. #endif /* CONFIG_OF_DYNAMIC */
  912. /* CONFIG_OF_RESOLVE api */
  913. extern int of_resolve_phandles(struct device_node *tree);
  914. /**
  915. * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
  916. * @np: Pointer to the given device_node
  917. *
  918. * return true if present false otherwise
  919. */
  920. static inline bool of_device_is_system_power_controller(const struct device_node *np)
  921. {
  922. return of_property_read_bool(np, "system-power-controller");
  923. }
  924. /**
  925. * Overlay support
  926. */
  927. #ifdef CONFIG_OF_OVERLAY
  928. /* ID based overlays; the API for external users */
  929. int of_overlay_create(struct device_node *tree);
  930. int of_overlay_destroy(int id);
  931. int of_overlay_destroy_all(void);
  932. #else
  933. static inline int of_overlay_create(struct device_node *tree)
  934. {
  935. return -ENOTSUPP;
  936. }
  937. static inline int of_overlay_destroy(int id)
  938. {
  939. return -ENOTSUPP;
  940. }
  941. static inline int of_overlay_destroy_all(void)
  942. {
  943. return -ENOTSUPP;
  944. }
  945. #endif
  946. #endif /* _LINUX_OF_H */