regmap: Add devm_regmap_init()
[platform/kernel/u-boot.git] / include / regmap.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #ifndef __REGMAP_H
8 #define __REGMAP_H
9
10 #include <linux/delay.h>
11
12 /**
13  * DOC: Overview
14  *
15  * Regmaps are an abstraction mechanism that allows device drivers to access
16  * register maps irrespective of the underlying bus architecture. This entails
17  * that for devices that support multiple busses (e.g. I2C and SPI for a GPIO
18  * expander chip) only one driver has to be written. This driver will
19  * instantiate a regmap with a backend depending on the bus the device is
20  * attached to, and use the regmap API to access the register map through that
21  * bus transparently.
22  *
23  * Read and write functions are supplied, which can read/write data of
24  * arbitrary length from/to the regmap.
25  *
26  * The endianness of regmap accesses is selectable for each map through device
27  * tree settings via the boolean "little-endian", "big-endian", and
28  * "native-endian" properties.
29  *
30  * Furthermore, the register map described by a regmap can be split into
31  * multiple disjoint areas called ranges. In this way, register maps with
32  * "holes", i.e. areas of addressable memory that are not part of the register
33  * map, can be accessed in a concise manner.
34  *
35  * Currently, only a bare "mem" backend for regmaps is supported, which
36  * accesses the register map as regular IO-mapped memory.
37  */
38
39 /**
40  * enum regmap_size_t - Access sizes for regmap reads and writes
41  *
42  * @REGMAP_SIZE_8: 8-bit read/write access size
43  * @REGMAP_SIZE_16: 16-bit read/write access size
44  * @REGMAP_SIZE_32: 32-bit read/write access size
45  * @REGMAP_SIZE_64: 64-bit read/write access size
46  */
47 enum regmap_size_t {
48         REGMAP_SIZE_8 = 1,
49         REGMAP_SIZE_16 = 2,
50         REGMAP_SIZE_32 = 4,
51         REGMAP_SIZE_64 = 8,
52 };
53
54 /**
55  * enum regmap_endianness_t - Endianness for regmap reads and writes
56  *
57  * @REGMAP_NATIVE_ENDIAN: Native endian read/write accesses
58  * @REGMAP_LITTLE_ENDIAN: Little endian read/write accesses
59  * @REGMAP_BIG_ENDIAN: Big endian read/write accesses
60  */
61 enum regmap_endianness_t {
62         REGMAP_NATIVE_ENDIAN,
63         REGMAP_LITTLE_ENDIAN,
64         REGMAP_BIG_ENDIAN,
65 };
66
67 /**
68  * struct regmap_range - a register map range
69  *
70  * @start:      Start address
71  * @size:       Size in bytes
72  */
73 struct regmap_range {
74         ulong start;
75         ulong size;
76 };
77
78 struct regmap_bus;
79 struct regmap_config;
80
81 /**
82  * struct regmap - a way of accessing hardware/bus registers
83  *
84  * @range_count:        Number of ranges available within the map
85  * @ranges:             Array of ranges
86  */
87 struct regmap {
88         enum regmap_endianness_t endianness;
89         int range_count;
90         struct regmap_range ranges[0];
91 };
92
93 /*
94  * Interface to provide access to registers either through a direct memory
95  * bus or through a peripheral bus like I2C, SPI.
96  */
97
98 /**
99  * regmap_write() - Write a 32-bit value to a regmap
100  *
101  * @map:        Regmap to write to
102  * @offset:     Offset in the regmap to write to
103  * @val:        Data to write to the regmap at the specified offset
104  *
105  * Note that this function will only write values of 32 bit width to the
106  * regmap; if the size of data to be read is different, the regmap_raw_write
107  * function can be used.
108  *
109  * Return: 0 if OK, -ve on error
110  */
111 int regmap_write(struct regmap *map, uint offset, uint val);
112
113 /**
114  * regmap_read() - Read a 32-bit value from a regmap
115  *
116  * @map:        Regmap to read from
117  * @offset:     Offset in the regmap to read from
118  * @valp:       Pointer to the buffer to receive the data read from the regmap
119  *              at the specified offset
120  *
121  * Note that this function will only read values of 32 bit width from the
122  * regmap; if the size of data to be read is different, the regmap_raw_read
123  * function can be used.
124  *
125  * Return: 0 if OK, -ve on error
126  */
127 int regmap_read(struct regmap *map, uint offset, uint *valp);
128
129 /**
130  * regmap_raw_write() - Write a value of specified length to a regmap
131  *
132  * @map:        Regmap to write to
133  * @offset:     Offset in the regmap to write to
134  * @val:        Value to write to the regmap at the specified offset
135  * @val_len:    Length of the data to be written to the regmap
136  *
137  * Note that this function will, as opposed to regmap_write, write data of
138  * arbitrary length to the regmap, and not just 32-bit values, and is thus a
139  * generalized version of regmap_write.
140  *
141  * Return: 0 if OK, -ve on error
142  */
143 int regmap_raw_write(struct regmap *map, uint offset, const void *val,
144                      size_t val_len);
145
146 /**
147  * regmap_raw_read() - Read a value of specified length from a regmap
148  *
149  * @map:        Regmap to read from
150  * @offset:     Offset in the regmap to read from
151  * @valp:       Pointer to the buffer to receive the data read from the regmap
152  *              at the specified offset
153  * @val_len:    Length of the data to be read from the regmap
154  *
155  * Note that this function will, as opposed to regmap_read, read data of
156  * arbitrary length from the regmap, and not just 32-bit values, and is thus a
157  * generalized version of regmap_read.
158  *
159  * Return: 0 if OK, -ve on error
160  */
161 int regmap_raw_read(struct regmap *map, uint offset, void *valp,
162                     size_t val_len);
163
164 /**
165  * regmap_raw_write_range() - Write a value of specified length to a range of a
166  *                            regmap
167  *
168  * @map:        Regmap to write to
169  * @range_num:  Number of the range in the regmap to write to
170  * @offset:     Offset in the regmap to write to
171  * @val:        Value to write to the regmap at the specified offset
172  * @val_len:    Length of the data to be written to the regmap
173  *
174  * Return: 0 if OK, -ve on error
175  */
176 int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
177                            const void *val, size_t val_len);
178
179 /**
180  * regmap_raw_read_range() - Read a value of specified length from a range of a
181  *                           regmap
182  *
183  * @map:        Regmap to read from
184  * @range_num:  Number of the range in the regmap to write to
185  * @offset:     Offset in the regmap to read from
186  * @valp:       Pointer to the buffer to receive the data read from the regmap
187  *              at the specified offset
188  * @val_len:    Length of the data to be read from the regmap
189  *
190  * Return: 0 if OK, -ve on error
191  */
192 int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
193                           void *valp, size_t val_len);
194
195 /**
196  * regmap_range_set() - Set a value in a regmap range described by a struct
197  * @map:    Regmap in which a value should be set
198  * @range:  Range of the regmap in which a value should be set
199  * @type:   Structure type that describes the memory layout of the regmap range
200  * @member: Member of the describing structure that should be set in the regmap
201  *          range
202  * @val:    Value which should be written to the regmap range
203  */
204 #define regmap_range_set(map, range, type, member, val) \
205         do { \
206                 typeof(((type *)0)->member) __tmp = val; \
207                 regmap_raw_write_range(map, range, offsetof(type, member), \
208                                        &__tmp, sizeof(((type *)0)->member)); \
209         } while (0)
210
211 /**
212  * regmap_set() - Set a value in a regmap described by a struct
213  * @map:    Regmap in which a value should be set
214  * @type:   Structure type that describes the memory layout of the regmap
215  * @member: Member of the describing structure that should be set in the regmap
216  * @val:    Value which should be written to the regmap
217  */
218 #define regmap_set(map, type, member, val) \
219         regmap_range_set(map, 0, type, member, val)
220
221 /**
222  * regmap_range_get() - Get a value from a regmap range described by a struct
223  * @map:    Regmap from which a value should be read
224  * @range:  Range of the regmap from which a value should be read
225  * @type:   Structure type that describes the memory layout of the regmap
226  *          range
227  * @member: Member of the describing structure that should be read in the
228  *          regmap range
229  * @valp:   Variable that receives the value read from the regmap range
230  */
231 #define regmap_range_get(map, range, type, member, valp) \
232         regmap_raw_read_range(map, range, offsetof(type, member), \
233                               (void *)valp, sizeof(((type *)0)->member))
234
235 /**
236  * regmap_get() - Get a value from a regmap described by a struct
237  * @map:    Regmap from which a value should be read
238  * @type:   Structure type that describes the memory layout of the regmap
239  *          range
240  * @member: Member of the describing structure that should be read in the
241  *          regmap
242  * @valp:   Variable that receives the value read from the regmap
243  */
244 #define regmap_get(map, type, member, valp) \
245         regmap_range_get(map, 0, type, member, valp)
246
247 /**
248  * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
249  *
250  * @map:        Regmap to read from
251  * @addr:       Offset to poll
252  * @val:        Unsigned integer variable to read the value into
253  * @cond:       Break condition (usually involving @val)
254  * @sleep_us:   Maximum time to sleep between reads in us (0 tight-loops).
255  * @timeout_ms: Timeout in ms, 0 means never timeout
256  * @test_add_time: Used for sandbox testing - amount of time to add after
257  *              starting the loop (0 if not testing)
258  *
259  * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
260  * error return value in case of a error read. In the two former cases,
261  * the last read value at @addr is stored in @val. Must not be called
262  * from atomic context if sleep_us or timeout_us are used.
263  *
264  * This is modelled after the regmap_read_poll_timeout macros in linux but
265  * with millisecond timeout.
266  *
267  * The _test version is for sandbox testing only. Do not use this in normal
268  * code as it advances the timer.
269  */
270 #define regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \
271                                       timeout_ms, test_add_time) \
272 ({ \
273         unsigned long __start = get_timer(0); \
274         int __ret; \
275         for (;;) { \
276                 __ret = regmap_read((map), (addr), &(val)); \
277                 if (__ret) \
278                         break; \
279                 if (cond) \
280                         break; \
281                 if (IS_ENABLED(CONFIG_SANDBOX) && test_add_time) \
282                         timer_test_add_offset(test_add_time); \
283                 if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \
284                         __ret = regmap_read((map), (addr), &(val)); \
285                         break; \
286                 } \
287                 if ((sleep_us)) \
288                         udelay((sleep_us)); \
289         } \
290         __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
291 })
292
293 #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_ms) \
294         regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \
295                                       timeout_ms, 0) \
296
297 /**
298  * regmap_update_bits() - Perform a read/modify/write using a mask
299  *
300  * @map:        The map returned by regmap_init_mem*()
301  * @offset:     Offset of the memory
302  * @mask:       Mask to apply to the read value
303  * @val:        Value to OR with the read value after masking. Note that any
304  *      bits set in @val which are not set in @mask are ignored
305  * Return: 0 if OK, -ve on error
306  */
307 int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val);
308
309 /**
310  * regmap_init_mem() - Set up a new register map that uses memory access
311  *
312  * @node:       Device node that uses this map
313  * @mapp:       Returns allocated map
314  * Return: 0 if OK, -ve on error
315  *
316  * Use regmap_uninit() to free it.
317  */
318 int regmap_init_mem(ofnode node, struct regmap **mapp);
319
320 /**
321  * regmap_init_mem_platdata() - Set up a new memory register map for
322  *                              of-platdata
323  *
324  * @dev:        Device that uses this map
325  * @reg:        List of address, size pairs
326  * @count:      Number of pairs (e.g. 1 if the regmap has a single entry)
327  * @mapp:       Returns allocated map
328  * Return: 0 if OK, -ve on error
329  *
330  * This creates a new regmap with a list of regions passed in, rather than
331  * using the device tree. It only supports 32-bit machines.
332  *
333  * Use regmap_uninit() to free it.
334  *
335  */
336 int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
337                              struct regmap **mapp);
338
339 int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index);
340
341 /**
342  * devm_regmap_init() - Initialise register map (device managed)
343  *
344  * @dev: Device that will be interacted with
345  * @bus: Bus-specific callbacks to use with device (IGNORED)
346  * @bus_context: Data passed to bus-specific callbacks (IGNORED)
347  * @config: Configuration for register map (IGNORED)
348  *
349  * @Return a valid pointer to a struct regmap or a ERR_PTR() on error.
350  * The structure is automatically freed when the device is unbound
351  */
352 struct regmap *devm_regmap_init(struct udevice *dev,
353                                 const struct regmap_bus *bus,
354                                 void *bus_context,
355                                 const struct regmap_config *config);
356 /**
357  * regmap_get_range() - Obtain the base memory address of a regmap range
358  *
359  * @map:        Regmap to query
360  * @range_num:  Range to look up
361  * Return: Pointer to the range in question if OK, NULL on error
362  */
363 void *regmap_get_range(struct regmap *map, unsigned int range_num);
364
365 /**
366  * regmap_uninit() - free a previously inited regmap
367  *
368  * @map:        Regmap to free
369  * Return: 0 if OK, -ve on error
370  */
371 int regmap_uninit(struct regmap *map);
372
373 #endif