of.h 29 KB

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