of.h 28 KB

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