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