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