at v6.14 20 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * property.h - Unified device property interface. 4 * 5 * Copyright (C) 2014, Intel Corporation 6 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com> 7 * Mika Westerberg <mika.westerberg@linux.intel.com> 8 */ 9 10#ifndef _LINUX_PROPERTY_H_ 11#define _LINUX_PROPERTY_H_ 12 13#include <linux/args.h> 14#include <linux/array_size.h> 15#include <linux/bits.h> 16#include <linux/cleanup.h> 17#include <linux/fwnode.h> 18#include <linux/stddef.h> 19#include <linux/types.h> 20 21struct device; 22 23enum dev_prop_type { 24 DEV_PROP_U8, 25 DEV_PROP_U16, 26 DEV_PROP_U32, 27 DEV_PROP_U64, 28 DEV_PROP_STRING, 29 DEV_PROP_REF, 30}; 31 32const struct fwnode_handle *__dev_fwnode_const(const struct device *dev); 33struct fwnode_handle *__dev_fwnode(struct device *dev); 34#define dev_fwnode(dev) \ 35 _Generic((dev), \ 36 const struct device *: __dev_fwnode_const, \ 37 struct device *: __dev_fwnode)(dev) 38 39bool device_property_present(const struct device *dev, const char *propname); 40bool device_property_read_bool(const struct device *dev, const char *propname); 41int device_property_read_u8_array(const struct device *dev, const char *propname, 42 u8 *val, size_t nval); 43int device_property_read_u16_array(const struct device *dev, const char *propname, 44 u16 *val, size_t nval); 45int device_property_read_u32_array(const struct device *dev, const char *propname, 46 u32 *val, size_t nval); 47int device_property_read_u64_array(const struct device *dev, const char *propname, 48 u64 *val, size_t nval); 49int device_property_read_string_array(const struct device *dev, const char *propname, 50 const char **val, size_t nval); 51int device_property_read_string(const struct device *dev, const char *propname, 52 const char **val); 53int device_property_match_string(const struct device *dev, 54 const char *propname, const char *string); 55 56bool fwnode_property_present(const struct fwnode_handle *fwnode, 57 const char *propname); 58bool fwnode_property_read_bool(const struct fwnode_handle *fwnode, 59 const char *propname); 60int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode, 61 const char *propname, u8 *val, 62 size_t nval); 63int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode, 64 const char *propname, u16 *val, 65 size_t nval); 66int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode, 67 const char *propname, u32 *val, 68 size_t nval); 69int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode, 70 const char *propname, u64 *val, 71 size_t nval); 72int fwnode_property_read_string_array(const struct fwnode_handle *fwnode, 73 const char *propname, const char **val, 74 size_t nval); 75int fwnode_property_read_string(const struct fwnode_handle *fwnode, 76 const char *propname, const char **val); 77int fwnode_property_match_string(const struct fwnode_handle *fwnode, 78 const char *propname, const char *string); 79 80bool fwnode_device_is_available(const struct fwnode_handle *fwnode); 81 82static inline bool fwnode_device_is_big_endian(const struct fwnode_handle *fwnode) 83{ 84 if (fwnode_property_present(fwnode, "big-endian")) 85 return true; 86 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && 87 fwnode_property_present(fwnode, "native-endian")) 88 return true; 89 return false; 90} 91 92static inline 93bool fwnode_device_is_compatible(const struct fwnode_handle *fwnode, const char *compat) 94{ 95 return fwnode_property_match_string(fwnode, "compatible", compat) >= 0; 96} 97 98/** 99 * device_is_big_endian - check if a device has BE registers 100 * @dev: Pointer to the struct device 101 * 102 * Returns: true if the device has a "big-endian" property, or if the kernel 103 * was compiled for BE *and* the device has a "native-endian" property. 104 * Returns false otherwise. 105 * 106 * Callers would nominally use ioread32be/iowrite32be if 107 * device_is_big_endian() == true, or readl/writel otherwise. 108 */ 109static inline bool device_is_big_endian(const struct device *dev) 110{ 111 return fwnode_device_is_big_endian(dev_fwnode(dev)); 112} 113 114/** 115 * device_is_compatible - match 'compatible' property of the device with a given string 116 * @dev: Pointer to the struct device 117 * @compat: The string to match 'compatible' property with 118 * 119 * Returns: true if matches, otherwise false. 120 */ 121static inline bool device_is_compatible(const struct device *dev, const char *compat) 122{ 123 return fwnode_device_is_compatible(dev_fwnode(dev), compat); 124} 125 126int fwnode_property_match_property_string(const struct fwnode_handle *fwnode, 127 const char *propname, 128 const char * const *array, size_t n); 129 130static inline 131int device_property_match_property_string(const struct device *dev, 132 const char *propname, 133 const char * const *array, size_t n) 134{ 135 return fwnode_property_match_property_string(dev_fwnode(dev), propname, array, n); 136} 137 138int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode, 139 const char *prop, const char *nargs_prop, 140 unsigned int nargs, unsigned int index, 141 struct fwnode_reference_args *args); 142 143struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, 144 const char *name, 145 unsigned int index); 146 147const char *fwnode_get_name(const struct fwnode_handle *fwnode); 148const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode); 149bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name); 150 151struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode); 152struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode); 153 154#define fwnode_for_each_parent_node(fwnode, parent) \ 155 for (parent = fwnode_get_parent(fwnode); parent; \ 156 parent = fwnode_get_next_parent(parent)) 157 158unsigned int fwnode_count_parents(const struct fwnode_handle *fwn); 159struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn, 160 unsigned int depth); 161struct fwnode_handle *fwnode_get_next_child_node( 162 const struct fwnode_handle *fwnode, struct fwnode_handle *child); 163struct fwnode_handle *fwnode_get_next_available_child_node( 164 const struct fwnode_handle *fwnode, struct fwnode_handle *child); 165 166#define fwnode_for_each_child_node(fwnode, child) \ 167 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \ 168 child = fwnode_get_next_child_node(fwnode, child)) 169 170#define fwnode_for_each_available_child_node(fwnode, child) \ 171 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\ 172 child = fwnode_get_next_available_child_node(fwnode, child)) 173 174struct fwnode_handle *device_get_next_child_node(const struct device *dev, 175 struct fwnode_handle *child); 176 177#define device_for_each_child_node(dev, child) \ 178 for (child = device_get_next_child_node(dev, NULL); child; \ 179 child = device_get_next_child_node(dev, child)) 180 181#define device_for_each_child_node_scoped(dev, child) \ 182 for (struct fwnode_handle *child __free(fwnode_handle) = \ 183 device_get_next_child_node(dev, NULL); \ 184 child; child = device_get_next_child_node(dev, child)) 185 186struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 187 const char *childname); 188struct fwnode_handle *device_get_named_child_node(const struct device *dev, 189 const char *childname); 190 191struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode); 192 193/** 194 * fwnode_handle_put - Drop reference to a device node 195 * @fwnode: Pointer to the device node to drop the reference to. 196 * 197 * This has to be used when terminating device_for_each_child_node() iteration 198 * with break or return to prevent stale device node references from being left 199 * behind. 200 */ 201static inline void fwnode_handle_put(struct fwnode_handle *fwnode) 202{ 203 fwnode_call_void_op(fwnode, put); 204} 205 206DEFINE_FREE(fwnode_handle, struct fwnode_handle *, fwnode_handle_put(_T)) 207 208int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index); 209int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name); 210 211unsigned int device_get_child_node_count(const struct device *dev); 212 213static inline int device_property_read_u8(const struct device *dev, 214 const char *propname, u8 *val) 215{ 216 return device_property_read_u8_array(dev, propname, val, 1); 217} 218 219static inline int device_property_read_u16(const struct device *dev, 220 const char *propname, u16 *val) 221{ 222 return device_property_read_u16_array(dev, propname, val, 1); 223} 224 225static inline int device_property_read_u32(const struct device *dev, 226 const char *propname, u32 *val) 227{ 228 return device_property_read_u32_array(dev, propname, val, 1); 229} 230 231static inline int device_property_read_u64(const struct device *dev, 232 const char *propname, u64 *val) 233{ 234 return device_property_read_u64_array(dev, propname, val, 1); 235} 236 237static inline int device_property_count_u8(const struct device *dev, const char *propname) 238{ 239 return device_property_read_u8_array(dev, propname, NULL, 0); 240} 241 242static inline int device_property_count_u16(const struct device *dev, const char *propname) 243{ 244 return device_property_read_u16_array(dev, propname, NULL, 0); 245} 246 247static inline int device_property_count_u32(const struct device *dev, const char *propname) 248{ 249 return device_property_read_u32_array(dev, propname, NULL, 0); 250} 251 252static inline int device_property_count_u64(const struct device *dev, const char *propname) 253{ 254 return device_property_read_u64_array(dev, propname, NULL, 0); 255} 256 257static inline int device_property_string_array_count(const struct device *dev, 258 const char *propname) 259{ 260 return device_property_read_string_array(dev, propname, NULL, 0); 261} 262 263static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode, 264 const char *propname, u8 *val) 265{ 266 return fwnode_property_read_u8_array(fwnode, propname, val, 1); 267} 268 269static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode, 270 const char *propname, u16 *val) 271{ 272 return fwnode_property_read_u16_array(fwnode, propname, val, 1); 273} 274 275static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode, 276 const char *propname, u32 *val) 277{ 278 return fwnode_property_read_u32_array(fwnode, propname, val, 1); 279} 280 281static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode, 282 const char *propname, u64 *val) 283{ 284 return fwnode_property_read_u64_array(fwnode, propname, val, 1); 285} 286 287static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode, 288 const char *propname) 289{ 290 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0); 291} 292 293static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode, 294 const char *propname) 295{ 296 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0); 297} 298 299static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode, 300 const char *propname) 301{ 302 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0); 303} 304 305static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode, 306 const char *propname) 307{ 308 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0); 309} 310 311static inline int 312fwnode_property_string_array_count(const struct fwnode_handle *fwnode, 313 const char *propname) 314{ 315 return fwnode_property_read_string_array(fwnode, propname, NULL, 0); 316} 317 318struct software_node; 319 320/** 321 * struct software_node_ref_args - Reference property with additional arguments 322 * @node: Reference to a software node 323 * @nargs: Number of elements in @args array 324 * @args: Integer arguments 325 */ 326struct software_node_ref_args { 327 const struct software_node *node; 328 unsigned int nargs; 329 u64 args[NR_FWNODE_REFERENCE_ARGS]; 330}; 331 332#define SOFTWARE_NODE_REFERENCE(_ref_, ...) \ 333(const struct software_node_ref_args) { \ 334 .node = _ref_, \ 335 .nargs = COUNT_ARGS(__VA_ARGS__), \ 336 .args = { __VA_ARGS__ }, \ 337} 338 339/** 340 * struct property_entry - "Built-in" device property representation. 341 * @name: Name of the property. 342 * @length: Length of data making up the value. 343 * @is_inline: True when the property value is stored inline. 344 * @type: Type of the data in unions. 345 * @pointer: Pointer to the property when it is not stored inline. 346 * @value: Value of the property when it is stored inline. 347 */ 348struct property_entry { 349 const char *name; 350 size_t length; 351 bool is_inline; 352 enum dev_prop_type type; 353 union { 354 const void *pointer; 355 union { 356 u8 u8_data[sizeof(u64) / sizeof(u8)]; 357 u16 u16_data[sizeof(u64) / sizeof(u16)]; 358 u32 u32_data[sizeof(u64) / sizeof(u32)]; 359 u64 u64_data[sizeof(u64) / sizeof(u64)]; 360 const char *str[sizeof(u64) / sizeof(char *)]; 361 } value; 362 }; 363}; 364 365/* 366 * Note: the below initializers for the anonymous union are carefully 367 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions 368 * and structs. 369 */ 370#define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_) \ 371(struct property_entry) { \ 372 .name = _name_, \ 373 .length = (_len_) * sizeof_field(struct property_entry, value._elem_[0]), \ 374 .type = DEV_PROP_##_Type_, \ 375 { .pointer = _val_ }, \ 376} 377 378#define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \ 379 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_) 380#define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \ 381 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_) 382#define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \ 383 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_) 384#define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \ 385 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_) 386#define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \ 387 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_) 388 389#define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \ 390(struct property_entry) { \ 391 .name = _name_, \ 392 .length = (_len_) * sizeof(struct software_node_ref_args), \ 393 .type = DEV_PROP_REF, \ 394 { .pointer = _val_ }, \ 395} 396 397#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \ 398 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_)) 399#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \ 400 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_)) 401#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \ 402 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_)) 403#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \ 404 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_)) 405#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \ 406 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_)) 407#define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \ 408 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_)) 409 410#define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \ 411(struct property_entry) { \ 412 .name = _name_, \ 413 .length = sizeof_field(struct property_entry, value._elem_[0]), \ 414 .is_inline = true, \ 415 .type = DEV_PROP_##_Type_, \ 416 { .value = { ._elem_[0] = _val_ } }, \ 417} 418 419#define PROPERTY_ENTRY_U8(_name_, _val_) \ 420 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_) 421#define PROPERTY_ENTRY_U16(_name_, _val_) \ 422 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_) 423#define PROPERTY_ENTRY_U32(_name_, _val_) \ 424 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_) 425#define PROPERTY_ENTRY_U64(_name_, _val_) \ 426 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_) 427#define PROPERTY_ENTRY_STRING(_name_, _val_) \ 428 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_) 429 430#define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \ 431(struct property_entry) { \ 432 .name = _name_, \ 433 .length = sizeof(struct software_node_ref_args), \ 434 .type = DEV_PROP_REF, \ 435 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \ 436} 437 438#define PROPERTY_ENTRY_BOOL(_name_) \ 439(struct property_entry) { \ 440 .name = _name_, \ 441 .is_inline = true, \ 442} 443 444struct property_entry * 445property_entries_dup(const struct property_entry *properties); 446void property_entries_free(const struct property_entry *properties); 447 448bool device_dma_supported(const struct device *dev); 449enum dev_dma_attr device_get_dma_attr(const struct device *dev); 450 451const void *device_get_match_data(const struct device *dev); 452 453int device_get_phy_mode(struct device *dev); 454int fwnode_get_phy_mode(const struct fwnode_handle *fwnode); 455 456void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index); 457 458struct fwnode_handle *fwnode_graph_get_next_endpoint( 459 const struct fwnode_handle *fwnode, struct fwnode_handle *prev); 460struct fwnode_handle * 461fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode); 462struct fwnode_handle *fwnode_graph_get_remote_port_parent( 463 const struct fwnode_handle *fwnode); 464struct fwnode_handle *fwnode_graph_get_remote_port( 465 const struct fwnode_handle *fwnode); 466struct fwnode_handle *fwnode_graph_get_remote_endpoint( 467 const struct fwnode_handle *fwnode); 468 469static inline bool fwnode_graph_is_endpoint(const struct fwnode_handle *fwnode) 470{ 471 return fwnode_property_present(fwnode, "remote-endpoint"); 472} 473 474/* 475 * Fwnode lookup flags 476 * 477 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the 478 * closest endpoint ID greater than the specified 479 * one. 480 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote 481 * endpoint of the given endpoint belongs to, 482 * may be disabled, or that the endpoint is not 483 * connected. 484 */ 485#define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0) 486#define FWNODE_GRAPH_DEVICE_DISABLED BIT(1) 487 488struct fwnode_handle * 489fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode, 490 u32 port, u32 endpoint, unsigned long flags); 491unsigned int fwnode_graph_get_endpoint_count(const struct fwnode_handle *fwnode, 492 unsigned long flags); 493 494#define fwnode_graph_for_each_endpoint(fwnode, child) \ 495 for (child = fwnode_graph_get_next_endpoint(fwnode, NULL); child; \ 496 child = fwnode_graph_get_next_endpoint(fwnode, child)) 497 498int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, 499 struct fwnode_endpoint *endpoint); 500 501typedef void *(*devcon_match_fn_t)(const struct fwnode_handle *fwnode, const char *id, 502 void *data); 503 504void *fwnode_connection_find_match(const struct fwnode_handle *fwnode, 505 const char *con_id, void *data, 506 devcon_match_fn_t match); 507 508static inline void *device_connection_find_match(const struct device *dev, 509 const char *con_id, void *data, 510 devcon_match_fn_t match) 511{ 512 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match); 513} 514 515int fwnode_connection_find_matches(const struct fwnode_handle *fwnode, 516 const char *con_id, void *data, 517 devcon_match_fn_t match, 518 void **matches, unsigned int matches_len); 519 520/* -------------------------------------------------------------------------- */ 521/* Software fwnode support - when HW description is incomplete or missing */ 522 523/** 524 * struct software_node - Software node description 525 * @name: Name of the software node 526 * @parent: Parent of the software node 527 * @properties: Array of device properties 528 */ 529struct software_node { 530 const char *name; 531 const struct software_node *parent; 532 const struct property_entry *properties; 533}; 534 535#define SOFTWARE_NODE(_name_, _properties_, _parent_) \ 536 (struct software_node) { \ 537 .name = _name_, \ 538 .properties = _properties_, \ 539 .parent = _parent_, \ 540 } 541 542bool is_software_node(const struct fwnode_handle *fwnode); 543const struct software_node * 544to_software_node(const struct fwnode_handle *fwnode); 545struct fwnode_handle *software_node_fwnode(const struct software_node *node); 546 547const struct software_node * 548software_node_find_by_name(const struct software_node *parent, 549 const char *name); 550 551int software_node_register_node_group(const struct software_node **node_group); 552void software_node_unregister_node_group(const struct software_node **node_group); 553 554int software_node_register(const struct software_node *node); 555void software_node_unregister(const struct software_node *node); 556 557struct fwnode_handle * 558fwnode_create_software_node(const struct property_entry *properties, 559 const struct fwnode_handle *parent); 560void fwnode_remove_software_node(struct fwnode_handle *fwnode); 561 562int device_add_software_node(struct device *dev, const struct software_node *node); 563void device_remove_software_node(struct device *dev); 564 565int device_create_managed_software_node(struct device *dev, 566 const struct property_entry *properties, 567 const struct software_node *parent); 568 569#endif /* _LINUX_PROPERTY_H_ */