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 19 enum 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 27 enum dev_dma_attr { 28 DEV_DMA_NOT_SUPPORTED, 29 DEV_DMA_NON_COHERENT, 30 DEV_DMA_COHERENT, 31 }; 32 33 struct fwnode_handle *dev_fwnode(struct device *dev); 34 35 bool device_property_present(struct device *dev, const char *propname); 36 int device_property_read_u8_array(struct device *dev, const char *propname, 37 u8 *val, size_t nval); 38 int device_property_read_u16_array(struct device *dev, const char *propname, 39 u16 *val, size_t nval); 40 int device_property_read_u32_array(struct device *dev, const char *propname, 41 u32 *val, size_t nval); 42 int device_property_read_u64_array(struct device *dev, const char *propname, 43 u64 *val, size_t nval); 44 int device_property_read_string_array(struct device *dev, const char *propname, 45 const char **val, size_t nval); 46 int device_property_read_string(struct device *dev, const char *propname, 47 const char **val); 48 int device_property_match_string(struct device *dev, 49 const char *propname, const char *string); 50 51 bool fwnode_device_is_available(const struct fwnode_handle *fwnode); 52 bool fwnode_property_present(const struct fwnode_handle *fwnode, 53 const char *propname); 54 int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode, 55 const char *propname, u8 *val, 56 size_t nval); 57 int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode, 58 const char *propname, u16 *val, 59 size_t nval); 60 int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode, 61 const char *propname, u32 *val, 62 size_t nval); 63 int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode, 64 const char *propname, u64 *val, 65 size_t nval); 66 int fwnode_property_read_string_array(const struct fwnode_handle *fwnode, 67 const char *propname, const char **val, 68 size_t nval); 69 int fwnode_property_read_string(const struct fwnode_handle *fwnode, 70 const char *propname, const char **val); 71 int fwnode_property_match_string(const struct fwnode_handle *fwnode, 72 const char *propname, const char *string); 73 int 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 78 struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode, 79 const char *name, 80 unsigned int index); 81 82 const char *fwnode_get_name(const struct fwnode_handle *fwnode); 83 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode); 84 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode); 85 struct fwnode_handle *fwnode_get_next_parent( 86 struct fwnode_handle *fwnode); 87 unsigned int fwnode_count_parents(const struct fwnode_handle *fwn); 88 struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn, 89 unsigned int depth); 90 struct fwnode_handle *fwnode_get_next_child_node( 91 const struct fwnode_handle *fwnode, struct fwnode_handle *child); 92 struct 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 103 struct 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 110 struct fwnode_handle *fwnode_get_named_child_node( 111 const struct fwnode_handle *fwnode, const char *childname); 112 struct fwnode_handle *device_get_named_child_node(struct device *dev, 113 const char *childname); 114 115 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode); 116 void fwnode_handle_put(struct fwnode_handle *fwnode); 117 118 int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index); 119 120 unsigned int device_get_child_node_count(struct device *dev); 121 122 static inline bool device_property_read_bool(struct device *dev, 123 const char *propname) 124 { 125 return device_property_present(dev, propname); 126 } 127 128 static 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 134 static 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 140 static 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 146 static 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 152 static 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 157 static 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 162 static 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 167 static 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 172 static 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 178 static 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 184 static 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 190 static 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 196 static 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 202 static 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 208 static 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 214 static 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 220 static 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 */ 235 struct 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 316 struct property_entry * 317 property_entries_dup(const struct property_entry *properties); 318 319 void property_entries_free(const struct property_entry *properties); 320 321 int device_add_properties(struct device *dev, 322 const struct property_entry *properties); 323 void device_remove_properties(struct device *dev); 324 325 bool device_dma_supported(struct device *dev); 326 327 enum dev_dma_attr device_get_dma_attr(struct device *dev); 328 329 const void *device_get_match_data(struct device *dev); 330 331 int device_get_phy_mode(struct device *dev); 332 333 void *device_get_mac_address(struct device *dev, char *addr, int alen); 334 335 int fwnode_get_phy_mode(struct fwnode_handle *fwnode); 336 void *fwnode_get_mac_address(struct fwnode_handle *fwnode, 337 char *addr, int alen); 338 struct fwnode_handle *fwnode_graph_get_next_endpoint( 339 const struct fwnode_handle *fwnode, struct fwnode_handle *prev); 340 struct fwnode_handle * 341 fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode); 342 struct fwnode_handle *fwnode_graph_get_remote_port_parent( 343 const struct fwnode_handle *fwnode); 344 struct fwnode_handle *fwnode_graph_get_remote_port( 345 const struct fwnode_handle *fwnode); 346 struct fwnode_handle *fwnode_graph_get_remote_endpoint( 347 const struct fwnode_handle *fwnode); 348 struct fwnode_handle * 349 fwnode_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 365 struct fwnode_handle * 366 fwnode_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 373 int 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 379 struct 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 */ 387 struct 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 */ 399 struct 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 */ 412 struct 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 419 bool is_software_node(const struct fwnode_handle *fwnode); 420 const struct software_node * 421 to_software_node(const struct fwnode_handle *fwnode); 422 struct fwnode_handle *software_node_fwnode(const struct software_node *node); 423 424 const struct software_node * 425 software_node_find_by_name(const struct software_node *parent, 426 const char *name); 427 428 int software_node_register_nodes(const struct software_node *nodes); 429 void software_node_unregister_nodes(const struct software_node *nodes); 430 431 int software_node_register(const struct software_node *node); 432 433 int software_node_notify(struct device *dev, unsigned long action); 434 435 struct fwnode_handle * 436 fwnode_create_software_node(const struct property_entry *properties, 437 const struct fwnode_handle *parent); 438 void fwnode_remove_software_node(struct fwnode_handle *fwnode); 439 440 #endif /* _LINUX_PROPERTY_H_ */ 441