Blame view

include/regmap.h 11.4 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  /* SPDX-License-Identifier: GPL-2.0+ */
6f98b7504   Simon Glass   dm: Add support f...
2
3
4
  /*
   * Copyright (c) 2015 Google, Inc
   * Written by Simon Glass <sjg@chromium.org>
6f98b7504   Simon Glass   dm: Add support f...
5
6
7
8
9
10
   */
  
  #ifndef __REGMAP_H
  #define __REGMAP_H
  
  /**
a6d4b0608   Mario Six   regmap: Add overv...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
   * DOC: Overview
   *
   * Regmaps are an abstraction mechanism that allows device drivers to access
   * register maps irrespective of the underlying bus architecture. This entails
   * that for devices that support multiple busses (e.g. I2C and SPI for a GPIO
   * expander chip) only one driver has to be written. This driver will
   * instantiate a regmap with a backend depending on the bus the device is
   * attached to, and use the regmap API to access the register map through that
   * bus transparently.
   *
   * Read and write functions are supplied, which can read/write data of
   * arbitrary length from/to the regmap.
   *
   * The endianness of regmap accesses is selectable for each map through device
   * tree settings via the boolean "little-endian", "big-endian", and
   * "native-endian" properties.
   *
   * Furthermore, the register map described by a regmap can be split into
   * multiple disjoint areas called ranges. In this way, register maps with
   * "holes", i.e. areas of addressable memory that are not part of the register
   * map, can be accessed in a concise manner.
   *
   * Currently, only a bare "mem" backend for regmaps is supported, which
   * accesses the register map as regular IO-mapped memory.
   */
  
  /**
84ff8f622   Mario Six   regmap: Add raw r...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
   * enum regmap_size_t - Access sizes for regmap reads and writes
   *
   * @REGMAP_SIZE_8: 8-bit read/write access size
   * @REGMAP_SIZE_16: 16-bit read/write access size
   * @REGMAP_SIZE_32: 32-bit read/write access size
   * @REGMAP_SIZE_64: 64-bit read/write access size
   */
  enum regmap_size_t {
  	REGMAP_SIZE_8 = 1,
  	REGMAP_SIZE_16 = 2,
  	REGMAP_SIZE_32 = 4,
  	REGMAP_SIZE_64 = 8,
  };
  
  /**
9b77fe3b8   Mario Six   regmap: Add endia...
53
54
55
56
57
58
59
60
61
62
63
64
65
   * enum regmap_endianness_t - Endianness for regmap reads and writes
   *
   * @REGMAP_NATIVE_ENDIAN: Native endian read/write accesses
   * @REGMAP_LITTLE_ENDIAN: Little endian read/write accesses
   * @REGMAP_BIG_ENDIAN: Big endian read/write accesses
   */
  enum regmap_endianness_t {
  	REGMAP_NATIVE_ENDIAN,
  	REGMAP_LITTLE_ENDIAN,
  	REGMAP_BIG_ENDIAN,
  };
  
  /**
6f98b7504   Simon Glass   dm: Add support f...
66
67
68
69
70
71
72
73
74
75
76
77
78
   * struct regmap_range - a register map range
   *
   * @start:	Start address
   * @size:	Size in bytes
   */
  struct regmap_range {
  	ulong start;
  	ulong size;
  };
  
  /**
   * struct regmap - a way of accessing hardware/bus registers
   *
604b6696e   Mario Six   regmap: Fix docum...
79
80
   * @range_count:	Number of ranges available within the map
   * @ranges:		Array of ranges
6f98b7504   Simon Glass   dm: Add support f...
81
82
   */
  struct regmap {
9b77fe3b8   Mario Six   regmap: Add endia...
83
  	enum regmap_endianness_t endianness;
6f98b7504   Simon Glass   dm: Add support f...
84
  	int range_count;
8c1de5e08   Masahiro Yamada   regmap: clean up ...
85
  	struct regmap_range ranges[0];
6f98b7504   Simon Glass   dm: Add support f...
86
87
88
89
90
91
  };
  
  /*
   * Interface to provide access to registers either through a direct memory
   * bus or through a peripheral bus like I2C, SPI.
   */
604b6696e   Mario Six   regmap: Fix docum...
92
93
94
95
96
97
98
99
  
  /**
   * regmap_write() - Write a 32-bit value to a regmap
   *
   * @map:	Regmap to write to
   * @offset:	Offset in the regmap to write to
   * @val:	Data to write to the regmap at the specified offset
   *
84ff8f622   Mario Six   regmap: Add raw r...
100
101
102
103
   * Note that this function will only write values of 32 bit width to the
   * regmap; if the size of data to be read is different, the regmap_raw_write
   * function can be used.
   *
604b6696e   Mario Six   regmap: Fix docum...
104
105
   * Return: 0 if OK, -ve on error
   */
6f98b7504   Simon Glass   dm: Add support f...
106
  int regmap_write(struct regmap *map, uint offset, uint val);
604b6696e   Mario Six   regmap: Fix docum...
107
108
109
110
111
112
113
114
115
  
  /**
   * regmap_read() - Read a 32-bit value from a regmap
   *
   * @map:	Regmap to read from
   * @offset:	Offset in the regmap to read from
   * @valp:	Pointer to the buffer to receive the data read from the regmap
   *		at the specified offset
   *
84ff8f622   Mario Six   regmap: Add raw r...
116
117
118
119
   * Note that this function will only read values of 32 bit width from the
   * regmap; if the size of data to be read is different, the regmap_raw_read
   * function can be used.
   *
604b6696e   Mario Six   regmap: Fix docum...
120
121
   * Return: 0 if OK, -ve on error
   */
6f98b7504   Simon Glass   dm: Add support f...
122
  int regmap_read(struct regmap *map, uint offset, uint *valp);
84ff8f622   Mario Six   regmap: Add raw r...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  /**
   * regmap_raw_write() - Write a value of specified length to a regmap
   *
   * @map:	Regmap to write to
   * @offset:	Offset in the regmap to write to
   * @val:	Value to write to the regmap at the specified offset
   * @val_len:	Length of the data to be written to the regmap
   *
   * Note that this function will, as opposed to regmap_write, write data of
   * arbitrary length to the regmap, and not just 32-bit values, and is thus a
   * generalized version of regmap_write.
   *
   * Return: 0 if OK, -ve on error
   */
  int regmap_raw_write(struct regmap *map, uint offset, const void *val,
  		     size_t val_len);
  
  /**
   * regmap_raw_read() - Read a value of specified length from a regmap
   *
   * @map:	Regmap to read from
   * @offset:	Offset in the regmap to read from
   * @valp:	Pointer to the buffer to receive the data read from the regmap
   *		at the specified offset
   * @val_len:	Length of the data to be read from the regmap
   *
   * Note that this function will, as opposed to regmap_read, read data of
   * arbitrary length from the regmap, and not just 32-bit values, and is thus a
   * generalized version of regmap_read.
   *
   * Return: 0 if OK, -ve on error
   */
  int regmap_raw_read(struct regmap *map, uint offset, void *valp,
  		    size_t val_len);
d5c7bd985   Mario Six   regmap: Support r...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  /**
   * regmap_raw_write_range() - Write a value of specified length to a range of a
   *			      regmap
   *
   * @map:	Regmap to write to
   * @range_num:	Number of the range in the regmap to write to
   * @offset:	Offset in the regmap to write to
   * @val:	Value to write to the regmap at the specified offset
   * @val_len:	Length of the data to be written to the regmap
   *
   * Return: 0 if OK, -ve on error
   */
  int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
  			   const void *val, size_t val_len);
  
  /**
   * regmap_raw_read_range() - Read a value of specified length from a range of a
   *			     regmap
   *
   * @map:	Regmap to read from
   * @range_num:	Number of the range in the regmap to write to
   * @offset:	Offset in the regmap to read from
   * @valp:	Pointer to the buffer to receive the data read from the regmap
   *		at the specified offset
   * @val_len:	Length of the data to be read from the regmap
   *
   * Return: 0 if OK, -ve on error
   */
  int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
  			  void *valp, size_t val_len);
e936397ae   Mario Six   regmap: Define re...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  /**
   * regmap_range_set() - Set a value in a regmap range described by a struct
   * @map:    Regmap in which a value should be set
   * @range:  Range of the regmap in which a value should be set
   * @type:   Structure type that describes the memory layout of the regmap range
   * @member: Member of the describing structure that should be set in the regmap
   *          range
   * @val:    Value which should be written to the regmap range
   */
  #define regmap_range_set(map, range, type, member, val) \
  	do { \
  		typeof(((type *)0)->member) __tmp = val; \
  		regmap_raw_write_range(map, range, offsetof(type, member), \
  				       &__tmp, sizeof(((type *)0)->member)); \
  	} while (0)
  
  /**
   * regmap_set() - Set a value in a regmap described by a struct
   * @map:    Regmap in which a value should be set
   * @type:   Structure type that describes the memory layout of the regmap
   * @member: Member of the describing structure that should be set in the regmap
   * @val:    Value which should be written to the regmap
   */
  #define regmap_set(map, type, member, val) \
  	regmap_range_set(map, 0, type, member, val)
6f98b7504   Simon Glass   dm: Add support f...
212

e936397ae   Mario Six   regmap: Define re...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  /**
   * regmap_range_get() - Get a value from a regmap range described by a struct
   * @map:    Regmap from which a value should be read
   * @range:  Range of the regmap from which a value should be read
   * @type:   Structure type that describes the memory layout of the regmap
   *          range
   * @member: Member of the describing structure that should be read in the
   *          regmap range
   * @valp:   Variable that receives the value read from the regmap range
   */
  #define regmap_range_get(map, range, type, member, valp) \
  	regmap_raw_read_range(map, range, offsetof(type, member), \
  			      (void *)valp, sizeof(((type *)0)->member))
  
  /**
   * regmap_get() - Get a value from a regmap described by a struct
   * @map:    Regmap from which a value should be read
   * @type:   Structure type that describes the memory layout of the regmap
   *          range
   * @member: Member of the describing structure that should be read in the
   *          regmap
   * @valp:   Variable that receives the value read from the regmap
   */
  #define regmap_get(map, type, member, valp) \
  	regmap_range_get(map, 0, type, member, valp)
6f98b7504   Simon Glass   dm: Add support f...
238
239
  
  /**
d13801ef1   Neil Armstrong   regmap: add regma...
240
241
242
243
244
245
246
247
   * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
   *
   * @map:	Regmap to read from
   * @addr:	Offset to poll
   * @val:	Unsigned integer variable to read the value into
   * @cond:	Break condition (usually involving @val)
   * @sleep_us:	Maximum time to sleep between reads in us (0 tight-loops).
   * @timeout_ms:	Timeout in ms, 0 means never timeout
df9cf1cc0   Simon Glass   test: dm: regmap:...
248
249
   * @test_add_time: Used for sandbox testing - amount of time to add after
   *		starting the loop (0 if not testing)
d13801ef1   Neil Armstrong   regmap: add regma...
250
251
252
253
254
255
256
257
   *
   * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
   * error return value in case of a error read. In the two former cases,
   * the last read value at @addr is stored in @val. Must not be called
   * from atomic context if sleep_us or timeout_us are used.
   *
   * This is modelled after the regmap_read_poll_timeout macros in linux but
   * with millisecond timeout.
df9cf1cc0   Simon Glass   test: dm: regmap:...
258
259
260
   *
   * The _test version is for sandbox testing only. Do not use this in normal
   * code as it advances the timer.
d13801ef1   Neil Armstrong   regmap: add regma...
261
   */
df9cf1cc0   Simon Glass   test: dm: regmap:...
262
263
  #define regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \
  				      timeout_ms, test_add_time) \
d13801ef1   Neil Armstrong   regmap: add regma...
264
265
266
267
268
269
270
271
272
  ({ \
  	unsigned long __start = get_timer(0); \
  	int __ret; \
  	for (;;) { \
  		__ret = regmap_read((map), (addr), &(val)); \
  		if (__ret) \
  			break; \
  		if (cond) \
  			break; \
df9cf1cc0   Simon Glass   test: dm: regmap:...
273
274
  		if (IS_ENABLED(CONFIG_SANDBOX) && test_add_time) \
  			sandbox_timer_add_offset(test_add_time); \
d13801ef1   Neil Armstrong   regmap: add regma...
275
276
277
278
279
280
281
282
283
  		if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \
  			__ret = regmap_read((map), (addr), &(val)); \
  			break; \
  		} \
  		if ((sleep_us)) \
  			udelay((sleep_us)); \
  	} \
  	__ret ?: ((cond) ? 0 : -ETIMEDOUT); \
  })
df9cf1cc0   Simon Glass   test: dm: regmap:...
284
285
286
  #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_ms) \
  	regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \
  				      timeout_ms, 0) \
d13801ef1   Neil Armstrong   regmap: add regma...
287
  /**
285cbcf97   Neil Armstrong   regmap: add regma...
288
289
290
291
292
293
   * regmap_update_bits() - Perform a read/modify/write using a mask
   *
   * @map:	The map returned by regmap_init_mem*()
   * @offset:	Offset of the memory
   * @mask:	Mask to apply to the read value
   * @val:	Value to apply to the value to write
604b6696e   Mario Six   regmap: Fix docum...
294
   * Return: 0 if OK, -ve on error
285cbcf97   Neil Armstrong   regmap: add regma...
295
296
297
298
   */
  int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val);
  
  /**
6f98b7504   Simon Glass   dm: Add support f...
299
300
   * regmap_init_mem() - Set up a new register map that uses memory access
   *
d35812368   Masahiro Yamada   regmap: change re...
301
   * @node:	Device node that uses this map
6f98b7504   Simon Glass   dm: Add support f...
302
   * @mapp:	Returns allocated map
604b6696e   Mario Six   regmap: Fix docum...
303
304
305
   * Return: 0 if OK, -ve on error
   *
   * Use regmap_uninit() to free it.
6f98b7504   Simon Glass   dm: Add support f...
306
   */
d35812368   Masahiro Yamada   regmap: change re...
307
  int regmap_init_mem(ofnode node, struct regmap **mapp);
6f98b7504   Simon Glass   dm: Add support f...
308

1e6ca1a6a   Simon Glass   dm: core: Add an ...
309
  /**
604b6696e   Mario Six   regmap: Fix docum...
310
311
312
313
314
315
316
317
   * regmap_init_mem_platdata() - Set up a new memory register map for
   *				of-platdata
   *
   * @dev:	Device that uses this map
   * @reg:	List of address, size pairs
   * @count:	Number of pairs (e.g. 1 if the regmap has a single entry)
   * @mapp:	Returns allocated map
   * Return: 0 if OK, -ve on error
1e6ca1a6a   Simon Glass   dm: core: Add an ...
318
319
320
321
322
323
   *
   * This creates a new regmap with a list of regions passed in, rather than
   * using the device tree. It only supports 32-bit machines.
   *
   * Use regmap_uninit() to free it.
   *
1e6ca1a6a   Simon Glass   dm: core: Add an ...
324
   */
c20ee0ed0   Simon Glass   dtoc: Add support...
325
  int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
3b2a29e09   Simon Glass   dm: regmap: Add a...
326
  			     struct regmap **mapp);
6f98b7504   Simon Glass   dm: Add support f...
327
328
329
330
331
  /**
   * regmap_get_range() - Obtain the base memory address of a regmap range
   *
   * @map:	Regmap to query
   * @range_num:	Range to look up
604b6696e   Mario Six   regmap: Fix docum...
332
   * Return: Pointer to the range in question if OK, NULL on error
6f98b7504   Simon Glass   dm: Add support f...
333
334
335
336
337
   */
  void *regmap_get_range(struct regmap *map, unsigned int range_num);
  
  /**
   * regmap_uninit() - free a previously inited regmap
604b6696e   Mario Six   regmap: Fix docum...
338
339
340
   *
   * @map:	Regmap to free
   * Return: 0 if OK, -ve on error
6f98b7504   Simon Glass   dm: Add support f...
341
342
343
344
   */
  int regmap_uninit(struct regmap *map);
  
  #endif