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