property.h 11.5 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/bits.h>
17
#include <linux/fwnode.h>
18 19 20 21 22 23 24 25 26 27 28 29 30
#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,
};

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

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

39 40 41 42 43 44 45 46 47 48 49 50 51
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);
52 53
int device_property_match_string(struct device *dev,
				 const char *propname, const char *string);
54

55 56 57 58
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,
59 60
				  const char *propname, u8 *val,
				  size_t nval);
61
int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
62 63
				   const char *propname, u16 *val,
				   size_t nval);
64
int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
65 66
				   const char *propname, u32 *val,
				   size_t nval);
67
int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
68 69
				   const char *propname, u64 *val,
				   size_t nval);
70
int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
71 72
				      const char *propname, const char **val,
				      size_t nval);
73
int fwnode_property_read_string(const struct fwnode_handle *fwnode,
74
				const char *propname, const char **val);
75
int fwnode_property_match_string(const struct fwnode_handle *fwnode,
76
				 const char *propname, const char *string);
77 78 79 80
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);
81

82 83 84 85 86
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);
87 88
struct fwnode_handle *fwnode_get_next_available_child_node(
	const struct fwnode_handle *fwnode, struct fwnode_handle *child);
89 90 91 92

#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))
93

94 95 96 97
#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))

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

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

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

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

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

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

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 146
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);
}

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

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

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

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

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

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

209 210 211 212 213 214
/*
 * 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.
 */

215 216 217 218 219 220 221
#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_ } },			\
222 223 224
}

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

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

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

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

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

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

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

void property_entries_free(const struct property_entry *properties);

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

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

enum dev_dma_attr device_get_dma_attr(struct device *dev);
284

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

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

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

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

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/*
 * Fwnode lookup flags
 *
 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
 *				closest endpoint ID greater than the specified
 *				one.
 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
 *				  endpoint of the given endpoint belongs to,
 *				  may be disabled.
 */
#define FWNODE_GRAPH_ENDPOINT_NEXT	BIT(0)
#define FWNODE_GRAPH_DEVICE_DISABLED	BIT(1)

struct fwnode_handle *
fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
				u32 port, u32 endpoint, unsigned long flags);

325 326 327 328
#define fwnode_graph_for_each_endpoint(fwnode, child)			\
	for (child = NULL;						\
	     (child = fwnode_graph_get_next_endpoint(fwnode, child)); )

329
int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
330 331
				struct fwnode_endpoint *endpoint);

332 333 334 335 336 337 338 339 340 341 342 343
/* -------------------------------------------------------------------------- */
/* Software fwnode support - when HW description is incomplete or missing */

bool is_software_node(const struct fwnode_handle *fwnode);

int software_node_notify(struct device *dev, unsigned long action);

struct fwnode_handle *
fwnode_create_software_node(const struct property_entry *properties,
			    const struct fwnode_handle *parent);
void fwnode_remove_software_node(struct fwnode_handle *fwnode);

344
#endif /* _LINUX_PROPERTY_H_ */