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 bool device_property_read_bool(const struct device *dev, const char *propname); 41 int device_property_read_u8_array(const struct device *dev, const char *propname, 42 u8 *val, size_t nval); 43 int device_property_read_u16_array(const struct device *dev, const char *propname, 44 u16 *val, size_t nval); 45 int device_property_read_u32_array(const struct device *dev, const char *propname, 46 u32 *val, size_t nval); 47 int device_property_read_u64_array(const struct device *dev, const char *propname, 48 u64 *val, size_t nval); 49 int device_property_read_string_array(const struct device *dev, const char *propname, 50 const char **val, size_t nval); 51 int device_property_read_string(const struct device *dev, const char *propname, 52 const char **val); 53 int device_property_match_string(const struct device *dev, 54 const char *propname, const char *string); 55 56 bool fwnode_property_present(const struct fwnode_handle *fwnode, 57 const char *propname); 58 bool fwnode_property_read_bool(const struct fwnode_handle *fwnode, 59 const char *propname); 60 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode, 61 const char *propname, u8 *val, 62 size_t nval); 63 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode, 64 const char *propname, u16 *val, 65 size_t nval); 66 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode, 67 const char *propname, u32 *val, 68 size_t nval); 69 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode, 70 const char *propname, u64 *val, 71 size_t nval); 72 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode, 73 const char *propname, const char **val, 74 size_t nval); 75 int fwnode_property_read_string(const struct fwnode_handle *fwnode, 76 const char *propname, const char **val); 77 int fwnode_property_match_string(const struct fwnode_handle *fwnode, 78 const char *propname, const char *string); 79 80 bool fwnode_device_is_available(const struct fwnode_handle *fwnode); 81 82 static 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 92 static inline 93 bool 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 */ 109 static 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 */ 121 static 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 126 int fwnode_property_match_property_string(const struct fwnode_handle *fwnode, 127 const char *propname, 128 const char * const *array, size_t n); 129 130 static inline 131 int 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 138 int 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 143 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, 144 const char *name, 145 unsigned int index); 146 147 const char *fwnode_get_name(const struct fwnode_handle *fwnode); 148 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode); 149 bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name); 150 151 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode); 152 struct 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 158 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn); 159 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn, 160 unsigned int depth); 161 struct fwnode_handle *fwnode_get_next_child_node( 162 const struct fwnode_handle *fwnode, struct fwnode_handle *child); 163 struct 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 174 struct 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 186 struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 187 const char *childname); 188 struct fwnode_handle *device_get_named_child_node(const struct device *dev, 189 const char *childname); 190 191 struct 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 */ 201 static inline void fwnode_handle_put(struct fwnode_handle *fwnode) 202 { 203 fwnode_call_void_op(fwnode, put); 204 } 205 206 DEFINE_FREE(fwnode_handle, struct fwnode_handle *, fwnode_handle_put(_T)) 207 208 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index); 209 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name); 210 211 unsigned int device_get_child_node_count(const struct device *dev); 212 213 static 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 219 static 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 225 static 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 231 static 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 237 static 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 242 static 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 247 static 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 252 static 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 257 static 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 263 static 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 269 static 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 275 static 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 281 static 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 287 static 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 293 static 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 299 static 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 305 static 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 311 static inline int 312 fwnode_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 318 struct 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 */ 326 struct 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 */ 348 struct 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 444 struct property_entry * 445 property_entries_dup(const struct property_entry *properties); 446 void property_entries_free(const struct property_entry *properties); 447 448 bool device_dma_supported(const struct device *dev); 449 enum dev_dma_attr device_get_dma_attr(const struct device *dev); 450 451 const void *device_get_match_data(const struct device *dev); 452 453 int device_get_phy_mode(struct device *dev); 454 int fwnode_get_phy_mode(const struct fwnode_handle *fwnode); 455 456 void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index); 457 458 struct fwnode_handle *fwnode_graph_get_next_endpoint( 459 const struct fwnode_handle *fwnode, struct fwnode_handle *prev); 460 struct fwnode_handle * 461 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode); 462 struct fwnode_handle *fwnode_graph_get_remote_port_parent( 463 const struct fwnode_handle *fwnode); 464 struct fwnode_handle *fwnode_graph_get_remote_port( 465 const struct fwnode_handle *fwnode); 466 struct fwnode_handle *fwnode_graph_get_remote_endpoint( 467 const struct fwnode_handle *fwnode); 468 469 static 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 488 struct fwnode_handle * 489 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode, 490 u32 port, u32 endpoint, unsigned long flags); 491 unsigned 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 498 int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, 499 struct fwnode_endpoint *endpoint); 500 501 typedef void *(*devcon_match_fn_t)(const struct fwnode_handle *fwnode, const char *id, 502 void *data); 503 504 void *fwnode_connection_find_match(const struct fwnode_handle *fwnode, 505 const char *con_id, void *data, 506 devcon_match_fn_t match); 507 508 static 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 515 int 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 */ 529 struct 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 542 bool is_software_node(const struct fwnode_handle *fwnode); 543 const struct software_node * 544 to_software_node(const struct fwnode_handle *fwnode); 545 struct fwnode_handle *software_node_fwnode(const struct software_node *node); 546 547 const struct software_node * 548 software_node_find_by_name(const struct software_node *parent, 549 const char *name); 550 551 int software_node_register_node_group(const struct software_node **node_group); 552 void software_node_unregister_node_group(const struct software_node **node_group); 553 554 int software_node_register(const struct software_node *node); 555 void software_node_unregister(const struct software_node *node); 556 557 struct fwnode_handle * 558 fwnode_create_software_node(const struct property_entry *properties, 559 const struct fwnode_handle *parent); 560 void fwnode_remove_software_node(struct fwnode_handle *fwnode); 561 562 int device_add_software_node(struct device *dev, const struct software_node *node); 563 void device_remove_software_node(struct device *dev); 564 565 int 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_ */ 570