of.h 33 KB

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