of.h 32 KB

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