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