of.h 29 KB

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