property.h 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * property.h - Unified device property interface.
 *
 * Copyright (C) 2014, Intel Corporation
 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
 *          Mika Westerberg <mika.westerberg@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef _LINUX_PROPERTY_H_
#define _LINUX_PROPERTY_H_

16
#include <linux/fwnode.h>
17 18 19 20 21 22 23 24 25 26 27 28 29
#include <linux/types.h>

struct device;

enum dev_prop_type {
	DEV_PROP_U8,
	DEV_PROP_U16,
	DEV_PROP_U32,
	DEV_PROP_U64,
	DEV_PROP_STRING,
	DEV_PROP_MAX,
};

30 31 32 33 34 35
enum dev_dma_attr {
	DEV_DMA_NOT_SUPPORTED,
	DEV_DMA_NON_COHERENT,
	DEV_DMA_COHERENT,
};

36 37
struct fwnode_handle *dev_fwnode(struct device *dev);

38 39 40 41 42 43 44 45 46 47 48 49 50
bool device_property_present(struct device *dev, const char *propname);
int device_property_read_u8_array(struct device *dev, const char *propname,
				  u8 *val, size_t nval);
int device_property_read_u16_array(struct device *dev, const char *propname,
				   u16 *val, size_t nval);
int device_property_read_u32_array(struct device *dev, const char *propname,
				   u32 *val, size_t nval);
int device_property_read_u64_array(struct device *dev, const char *propname,
				   u64 *val, size_t nval);
int device_property_read_string_array(struct device *dev, const char *propname,
				      const char **val, size_t nval);
int device_property_read_string(struct device *dev, const char *propname,
				const char **val);
51 52
int device_property_match_string(struct device *dev,
				 const char *propname, const char *string);
53

54 55 56 57
bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
bool fwnode_property_present(const struct fwnode_handle *fwnode,
			     const char *propname);
int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
58 59
				  const char *propname, u8 *val,
				  size_t nval);
60
int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
61 62
				   const char *propname, u16 *val,
				   size_t nval);
63
int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
64 65
				   const char *propname, u32 *val,
				   size_t nval);
66
int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
67 68
				   const char *propname, u64 *val,
				   size_t nval);
69
int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
70 71
				      const char *propname, const char **val,
				      size_t nval);
72
int fwnode_property_read_string(const struct fwnode_handle *fwnode,
73
				const char *propname, const char **val);
74
int fwnode_property_match_string(const struct fwnode_handle *fwnode,
75
				 const char *propname, const char *string);
76 77 78 79
int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
				       const char *prop, const char *nargs_prop,
				       unsigned int nargs, unsigned int index,
				       struct fwnode_reference_args *args);
80

81 82 83 84 85
struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
struct fwnode_handle *fwnode_get_next_parent(
	struct fwnode_handle *fwnode);
struct fwnode_handle *fwnode_get_next_child_node(
	const struct fwnode_handle *fwnode, struct fwnode_handle *child);
86 87
struct fwnode_handle *fwnode_get_next_available_child_node(
	const struct fwnode_handle *fwnode, struct fwnode_handle *child);
88 89 90 91

#define fwnode_for_each_child_node(fwnode, child)			\
	for (child = fwnode_get_next_child_node(fwnode, NULL); child;	\
	     child = fwnode_get_next_child_node(fwnode, child))
92

93 94 95 96
#define fwnode_for_each_available_child_node(fwnode, child)		       \
	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
	     child = fwnode_get_next_available_child_node(fwnode, child))

97 98
struct fwnode_handle *device_get_next_child_node(
	struct device *dev, struct fwnode_handle *child);
99

100 101
#define device_for_each_child_node(dev, child)				\
	for (child = device_get_next_child_node(dev, NULL); child;	\
102 103
	     child = device_get_next_child_node(dev, child))

104 105
struct fwnode_handle *fwnode_get_named_child_node(
	const struct fwnode_handle *fwnode, const char *childname);
106 107 108
struct fwnode_handle *device_get_named_child_node(struct device *dev,
						  const char *childname);

109
struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
110 111
void fwnode_handle_put(struct fwnode_handle *fwnode);

112 113
int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);

114 115
unsigned int device_get_child_node_count(struct device *dev);

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static inline bool device_property_read_bool(struct device *dev,
					     const char *propname)
{
	return device_property_present(dev, propname);
}

static inline int device_property_read_u8(struct device *dev,
					  const char *propname, u8 *val)
{
	return device_property_read_u8_array(dev, propname, val, 1);
}

static inline int device_property_read_u16(struct device *dev,
					   const char *propname, u16 *val)
{
	return device_property_read_u16_array(dev, propname, val, 1);
}

static inline int device_property_read_u32(struct device *dev,
					   const char *propname, u32 *val)
{
	return device_property_read_u32_array(dev, propname, val, 1);
}

static inline int device_property_read_u64(struct device *dev,
					   const char *propname, u64 *val)
{
	return device_property_read_u64_array(dev, propname, val, 1);
}

146
static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
147 148 149 150 151
					     const char *propname)
{
	return fwnode_property_present(fwnode, propname);
}

152
static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
153 154 155 156 157
					  const char *propname, u8 *val)
{
	return fwnode_property_read_u8_array(fwnode, propname, val, 1);
}

158
static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
159 160 161 162 163
					   const char *propname, u16 *val)
{
	return fwnode_property_read_u16_array(fwnode, propname, val, 1);
}

164
static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
165 166 167 168 169
					   const char *propname, u32 *val)
{
	return fwnode_property_read_u32_array(fwnode, propname, val, 1);
}

170
static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
171 172 173 174 175
					   const char *propname, u64 *val)
{
	return fwnode_property_read_u64_array(fwnode, propname, val, 1);
}

176 177 178
/**
 * struct property_entry - "Built-in" device property representation.
 * @name: Name of the property.
179
 * @length: Length of data making up the value.
180
 * @is_array: True when the property is an array.
181
 * @type: Type of the data in unions.
182 183
 * @pointer: Pointer to the property (an array of items of the given type).
 * @value: Value of the property (when it is a single item of the given type).
184 185 186
 */
struct property_entry {
	const char *name;
187
	size_t length;
188
	bool is_array;
189
	enum dev_prop_type type;
190
	union {
191
		union {
192 193 194 195 196
			const u8 *u8_data;
			const u16 *u16_data;
			const u32 *u32_data;
			const u64 *u64_data;
			const char * const *str;
197 198 199 200 201 202 203 204 205
		} pointer;
		union {
			u8 u8_data;
			u16 u16_data;
			u32 u32_data;
			u64 u64_data;
			const char *str;
		} value;
	};
206 207
};

208 209 210 211 212 213
/*
 * Note: the below four initializers for the anonymous union are carefully
 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
 * and structs.
 */

214 215 216 217 218 219 220
#define PROPERTY_ENTRY_INTEGER_ARRAY(_name_, _type_, _Type_, _val_)	\
(struct property_entry) {						\
	.name = _name_,							\
	.length = ARRAY_SIZE(_val_) * sizeof(_type_),			\
	.is_array = true,						\
	.type = DEV_PROP_##_Type_,					\
	{ .pointer = { ._type_##_data = _val_ } },			\
221 222 223
}

#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_)			\
224
	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u8, U8, _val_)
225
#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_)			\
226
	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u16, U16, _val_)
227
#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_)			\
228
	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u32, U32, _val_)
229
#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_)			\
230
	PROPERTY_ENTRY_INTEGER_ARRAY(_name_, u64, U64, _val_)
231 232

#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_)		\
233
(struct property_entry) {					\
234 235 236
	.name = _name_,						\
	.length = ARRAY_SIZE(_val_) * sizeof(const char *),	\
	.is_array = true,					\
237
	.type = DEV_PROP_STRING,				\
238
	{ .pointer = { .str = _val_ } },			\
239 240
}

241 242 243 244 245 246
#define PROPERTY_ENTRY_INTEGER(_name_, _type_, _Type_, _val_)	\
(struct property_entry) {					\
	.name = _name_,						\
	.length = sizeof(_type_),				\
	.type = DEV_PROP_##_Type_,				\
	{ .value = { ._type_##_data = _val_ } },		\
247 248 249
}

#define PROPERTY_ENTRY_U8(_name_, _val_)		\
250
	PROPERTY_ENTRY_INTEGER(_name_, u8, U8, _val_)
251
#define PROPERTY_ENTRY_U16(_name_, _val_)		\
252
	PROPERTY_ENTRY_INTEGER(_name_, u16, U16, _val_)
253
#define PROPERTY_ENTRY_U32(_name_, _val_)		\
254
	PROPERTY_ENTRY_INTEGER(_name_, u32, U32, _val_)
255
#define PROPERTY_ENTRY_U64(_name_, _val_)		\
256
	PROPERTY_ENTRY_INTEGER(_name_, u64, U64, _val_)
257 258

#define PROPERTY_ENTRY_STRING(_name_, _val_)		\
259
(struct property_entry) {				\
260 261
	.name = _name_,					\
	.length = sizeof(_val_),			\
262
	.type = DEV_PROP_STRING,			\
263
	{ .value = { .str = _val_ } },			\
264 265 266
}

#define PROPERTY_ENTRY_BOOL(_name_)		\
267
(struct property_entry) {			\
268 269 270
	.name = _name_,				\
}

271 272 273 274 275
struct property_entry *
property_entries_dup(const struct property_entry *properties);

void property_entries_free(const struct property_entry *properties);

276
int device_add_properties(struct device *dev,
277
			  const struct property_entry *properties);
278
void device_remove_properties(struct device *dev);
279

280 281 282
bool device_dma_supported(struct device *dev);

enum dev_dma_attr device_get_dma_attr(struct device *dev);
283

284
const void *device_get_match_data(struct device *dev);
285

286 287 288 289
int device_get_phy_mode(struct device *dev);

void *device_get_mac_address(struct device *dev, char *addr, int alen);

290
int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
291 292
void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
			     char *addr, int alen);
293
struct fwnode_handle *fwnode_graph_get_next_endpoint(
294
	const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
295
struct fwnode_handle *
296
fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
297
struct fwnode_handle *fwnode_graph_get_remote_port_parent(
298
	const struct fwnode_handle *fwnode);
299
struct fwnode_handle *fwnode_graph_get_remote_port(
300
	const struct fwnode_handle *fwnode);
301
struct fwnode_handle *fwnode_graph_get_remote_endpoint(
302 303 304 305
	const struct fwnode_handle *fwnode);
struct fwnode_handle *
fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port,
			     u32 endpoint);
306

307 308 309 310
#define fwnode_graph_for_each_endpoint(fwnode, child)			\
	for (child = NULL;						\
	     (child = fwnode_graph_get_next_endpoint(fwnode, child)); )

311
int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
312 313
				struct fwnode_endpoint *endpoint);

314
#endif /* _LINUX_PROPERTY_H_ */