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