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