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