1 /*
2  * property.h - Unified device property interface.
3  *
4  * Copyright (C) 2014, Intel Corporation
5  * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6  *          Mika Westerberg <mika.westerberg@linux.intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #ifndef _LINUX_PROPERTY_H_
14 #define _LINUX_PROPERTY_H_
15 
16 #include <linux/fwnode.h>
17 #include <linux/types.h>
18 
19 struct device;
20 
21 enum 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_MAX,
28 };
29 
30 bool device_property_present(struct device *dev, const char *propname);
31 int device_property_read_u8_array(struct device *dev, const char *propname,
32 				  u8 *val, size_t nval);
33 int device_property_read_u16_array(struct device *dev, const char *propname,
34 				   u16 *val, size_t nval);
35 int device_property_read_u32_array(struct device *dev, const char *propname,
36 				   u32 *val, size_t nval);
37 int device_property_read_u64_array(struct device *dev, const char *propname,
38 				   u64 *val, size_t nval);
39 int device_property_read_string_array(struct device *dev, const char *propname,
40 				      const char **val, size_t nval);
41 int device_property_read_string(struct device *dev, const char *propname,
42 				const char **val);
43 
44 bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname);
45 int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,
46 				  const char *propname, u8 *val,
47 				  size_t nval);
48 int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,
49 				   const char *propname, u16 *val,
50 				   size_t nval);
51 int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,
52 				   const char *propname, u32 *val,
53 				   size_t nval);
54 int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,
55 				   const char *propname, u64 *val,
56 				   size_t nval);
57 int fwnode_property_read_string_array(struct fwnode_handle *fwnode,
58 				      const char *propname, const char **val,
59 				      size_t nval);
60 int fwnode_property_read_string(struct fwnode_handle *fwnode,
61 				const char *propname, const char **val);
62 
63 struct fwnode_handle *device_get_next_child_node(struct device *dev,
64 						 struct fwnode_handle *child);
65 
66 #define device_for_each_child_node(dev, child) \
67 	for (child = device_get_next_child_node(dev, NULL); child; \
68 	     child = device_get_next_child_node(dev, child))
69 
70 void fwnode_handle_put(struct fwnode_handle *fwnode);
71 
72 unsigned int device_get_child_node_count(struct device *dev);
73 
device_property_read_bool(struct device * dev,const char * propname)74 static inline bool device_property_read_bool(struct device *dev,
75 					     const char *propname)
76 {
77 	return device_property_present(dev, propname);
78 }
79 
device_property_read_u8(struct device * dev,const char * propname,u8 * val)80 static inline int device_property_read_u8(struct device *dev,
81 					  const char *propname, u8 *val)
82 {
83 	return device_property_read_u8_array(dev, propname, val, 1);
84 }
85 
device_property_read_u16(struct device * dev,const char * propname,u16 * val)86 static inline int device_property_read_u16(struct device *dev,
87 					   const char *propname, u16 *val)
88 {
89 	return device_property_read_u16_array(dev, propname, val, 1);
90 }
91 
device_property_read_u32(struct device * dev,const char * propname,u32 * val)92 static inline int device_property_read_u32(struct device *dev,
93 					   const char *propname, u32 *val)
94 {
95 	return device_property_read_u32_array(dev, propname, val, 1);
96 }
97 
device_property_read_u64(struct device * dev,const char * propname,u64 * val)98 static inline int device_property_read_u64(struct device *dev,
99 					   const char *propname, u64 *val)
100 {
101 	return device_property_read_u64_array(dev, propname, val, 1);
102 }
103 
fwnode_property_read_bool(struct fwnode_handle * fwnode,const char * propname)104 static inline bool fwnode_property_read_bool(struct fwnode_handle *fwnode,
105 					     const char *propname)
106 {
107 	return fwnode_property_present(fwnode, propname);
108 }
109 
fwnode_property_read_u8(struct fwnode_handle * fwnode,const char * propname,u8 * val)110 static inline int fwnode_property_read_u8(struct fwnode_handle *fwnode,
111 					  const char *propname, u8 *val)
112 {
113 	return fwnode_property_read_u8_array(fwnode, propname, val, 1);
114 }
115 
fwnode_property_read_u16(struct fwnode_handle * fwnode,const char * propname,u16 * val)116 static inline int fwnode_property_read_u16(struct fwnode_handle *fwnode,
117 					   const char *propname, u16 *val)
118 {
119 	return fwnode_property_read_u16_array(fwnode, propname, val, 1);
120 }
121 
fwnode_property_read_u32(struct fwnode_handle * fwnode,const char * propname,u32 * val)122 static inline int fwnode_property_read_u32(struct fwnode_handle *fwnode,
123 					   const char *propname, u32 *val)
124 {
125 	return fwnode_property_read_u32_array(fwnode, propname, val, 1);
126 }
127 
fwnode_property_read_u64(struct fwnode_handle * fwnode,const char * propname,u64 * val)128 static inline int fwnode_property_read_u64(struct fwnode_handle *fwnode,
129 					   const char *propname, u64 *val)
130 {
131 	return fwnode_property_read_u64_array(fwnode, propname, val, 1);
132 }
133 
134 /**
135  * struct property_entry - "Built-in" device property representation.
136  * @name: Name of the property.
137  * @type: Type of the property.
138  * @nval: Number of items of type @type making up the value.
139  * @value: Value of the property (an array of @nval items of type @type).
140  */
141 struct property_entry {
142 	const char *name;
143 	enum dev_prop_type type;
144 	size_t nval;
145 	union {
146 		void *raw_data;
147 		u8 *u8_data;
148 		u16 *u16_data;
149 		u32 *u32_data;
150 		u64 *u64_data;
151 		const char **str;
152 	} value;
153 };
154 
155 /**
156  * struct property_set - Collection of "built-in" device properties.
157  * @fwnode: Handle to be pointed to by the fwnode field of struct device.
158  * @properties: Array of properties terminated with a null entry.
159  */
160 struct property_set {
161 	struct fwnode_handle fwnode;
162 	struct property_entry *properties;
163 };
164 
165 void device_add_property_set(struct device *dev, struct property_set *pset);
166 
167 #endif /* _LINUX_PROPERTY_H_ */
168