blob: 3b7eea5f49344dfa1f71098edc5e3ffb57aa8907 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass6f98b752015-06-23 15:38:42 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass6f98b752015-06-23 15:38:42 -06005 */
6
7#ifndef __REGMAP_H
8#define __REGMAP_H
9
10/**
Mario Six84ff8f62018-10-15 09:24:10 +020011 * enum regmap_size_t - Access sizes for regmap reads and writes
12 *
13 * @REGMAP_SIZE_8: 8-bit read/write access size
14 * @REGMAP_SIZE_16: 16-bit read/write access size
15 * @REGMAP_SIZE_32: 32-bit read/write access size
16 * @REGMAP_SIZE_64: 64-bit read/write access size
17 */
18enum regmap_size_t {
19 REGMAP_SIZE_8 = 1,
20 REGMAP_SIZE_16 = 2,
21 REGMAP_SIZE_32 = 4,
22 REGMAP_SIZE_64 = 8,
23};
24
25/**
Simon Glass6f98b752015-06-23 15:38:42 -060026 * struct regmap_range - a register map range
27 *
28 * @start: Start address
29 * @size: Size in bytes
30 */
31struct regmap_range {
32 ulong start;
33 ulong size;
34};
35
36/**
37 * struct regmap - a way of accessing hardware/bus registers
38 *
Mario Six604b6692018-10-04 09:00:41 +020039 * @range_count: Number of ranges available within the map
40 * @ranges: Array of ranges
Simon Glass6f98b752015-06-23 15:38:42 -060041 */
42struct regmap {
Simon Glass6f98b752015-06-23 15:38:42 -060043 int range_count;
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +090044 struct regmap_range ranges[0];
Simon Glass6f98b752015-06-23 15:38:42 -060045};
46
47/*
48 * Interface to provide access to registers either through a direct memory
49 * bus or through a peripheral bus like I2C, SPI.
50 */
Mario Six604b6692018-10-04 09:00:41 +020051
52/**
53 * regmap_write() - Write a 32-bit value to a regmap
54 *
55 * @map: Regmap to write to
56 * @offset: Offset in the regmap to write to
57 * @val: Data to write to the regmap at the specified offset
58 *
Mario Six84ff8f62018-10-15 09:24:10 +020059 * Note that this function will only write values of 32 bit width to the
60 * regmap; if the size of data to be read is different, the regmap_raw_write
61 * function can be used.
62 *
Mario Six604b6692018-10-04 09:00:41 +020063 * Return: 0 if OK, -ve on error
64 */
Simon Glass6f98b752015-06-23 15:38:42 -060065int regmap_write(struct regmap *map, uint offset, uint val);
Mario Six604b6692018-10-04 09:00:41 +020066
67/**
68 * regmap_read() - Read a 32-bit value from a regmap
69 *
70 * @map: Regmap to read from
71 * @offset: Offset in the regmap to read from
72 * @valp: Pointer to the buffer to receive the data read from the regmap
73 * at the specified offset
74 *
Mario Six84ff8f62018-10-15 09:24:10 +020075 * Note that this function will only read values of 32 bit width from the
76 * regmap; if the size of data to be read is different, the regmap_raw_read
77 * function can be used.
78 *
Mario Six604b6692018-10-04 09:00:41 +020079 * Return: 0 if OK, -ve on error
80 */
Simon Glass6f98b752015-06-23 15:38:42 -060081int regmap_read(struct regmap *map, uint offset, uint *valp);
82
Mario Six84ff8f62018-10-15 09:24:10 +020083/**
84 * regmap_raw_write() - Write a value of specified length to a regmap
85 *
86 * @map: Regmap to write to
87 * @offset: Offset in the regmap to write to
88 * @val: Value to write to the regmap at the specified offset
89 * @val_len: Length of the data to be written to the regmap
90 *
91 * Note that this function will, as opposed to regmap_write, write data of
92 * arbitrary length to the regmap, and not just 32-bit values, and is thus a
93 * generalized version of regmap_write.
94 *
95 * Return: 0 if OK, -ve on error
96 */
97int regmap_raw_write(struct regmap *map, uint offset, const void *val,
98 size_t val_len);
99
100/**
101 * regmap_raw_read() - Read a value of specified length from a regmap
102 *
103 * @map: Regmap to read from
104 * @offset: Offset in the regmap to read from
105 * @valp: Pointer to the buffer to receive the data read from the regmap
106 * at the specified offset
107 * @val_len: Length of the data to be read from the regmap
108 *
109 * Note that this function will, as opposed to regmap_read, read data of
110 * arbitrary length from the regmap, and not just 32-bit values, and is thus a
111 * generalized version of regmap_read.
112 *
113 * Return: 0 if OK, -ve on error
114 */
115int regmap_raw_read(struct regmap *map, uint offset, void *valp,
116 size_t val_len);
117
Mario Sixd5c7bd92018-10-15 09:24:11 +0200118/**
119 * regmap_raw_write_range() - Write a value of specified length to a range of a
120 * regmap
121 *
122 * @map: Regmap to write to
123 * @range_num: Number of the range in the regmap to write to
124 * @offset: Offset in the regmap to write to
125 * @val: Value to write to the regmap at the specified offset
126 * @val_len: Length of the data to be written to the regmap
127 *
128 * Return: 0 if OK, -ve on error
129 */
130int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset,
131 const void *val, size_t val_len);
132
133/**
134 * regmap_raw_read_range() - Read a value of specified length from a range of a
135 * regmap
136 *
137 * @map: Regmap to read from
138 * @range_num: Number of the range in the regmap to write to
139 * @offset: Offset in the regmap to read from
140 * @valp: Pointer to the buffer to receive the data read from the regmap
141 * at the specified offset
142 * @val_len: Length of the data to be read from the regmap
143 *
144 * Return: 0 if OK, -ve on error
145 */
146int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset,
147 void *valp, size_t val_len);
148
Mario Sixe9363972018-10-15 09:24:12 +0200149/**
150 * regmap_range_set() - Set a value in a regmap range described by a struct
151 * @map: Regmap in which a value should be set
152 * @range: Range of the regmap in which a value should be set
153 * @type: Structure type that describes the memory layout of the regmap range
154 * @member: Member of the describing structure that should be set in the regmap
155 * range
156 * @val: Value which should be written to the regmap range
157 */
158#define regmap_range_set(map, range, type, member, val) \
159 do { \
160 typeof(((type *)0)->member) __tmp = val; \
161 regmap_raw_write_range(map, range, offsetof(type, member), \
162 &__tmp, sizeof(((type *)0)->member)); \
163 } while (0)
Simon Glass6f98b752015-06-23 15:38:42 -0600164
Mario Sixe9363972018-10-15 09:24:12 +0200165/**
166 * regmap_set() - Set a value in a regmap described by a struct
167 * @map: Regmap in which a value should be set
168 * @type: Structure type that describes the memory layout of the regmap
169 * @member: Member of the describing structure that should be set in the regmap
170 * @val: Value which should be written to the regmap
171 */
172#define regmap_set(map, type, member, val) \
173 regmap_range_set(map, 0, type, member, val)
174
175/**
176 * regmap_range_get() - Get a value from a regmap range described by a struct
177 * @map: Regmap from which a value should be read
178 * @range: Range of the regmap from which a value should be read
179 * @type: Structure type that describes the memory layout of the regmap
180 * range
181 * @member: Member of the describing structure that should be read in the
182 * regmap range
183 * @valp: Variable that receives the value read from the regmap range
184 */
185#define regmap_range_get(map, range, type, member, valp) \
186 regmap_raw_read_range(map, range, offsetof(type, member), \
187 (void *)valp, sizeof(((type *)0)->member))
188
189/**
190 * regmap_get() - Get a value from a regmap described by a struct
191 * @map: Regmap from which a value should be read
192 * @type: Structure type that describes the memory layout of the regmap
193 * range
194 * @member: Member of the describing structure that should be read in the
195 * regmap
196 * @valp: Variable that receives the value read from the regmap
197 */
198#define regmap_get(map, type, member, valp) \
199 regmap_range_get(map, 0, type, member, valp)
Simon Glass6f98b752015-06-23 15:38:42 -0600200
201/**
Neil Armstrong285cbcf2018-04-27 11:56:14 +0200202 * regmap_update_bits() - Perform a read/modify/write using a mask
203 *
204 * @map: The map returned by regmap_init_mem*()
205 * @offset: Offset of the memory
206 * @mask: Mask to apply to the read value
207 * @val: Value to apply to the value to write
Mario Six604b6692018-10-04 09:00:41 +0200208 * Return: 0 if OK, -ve on error
Neil Armstrong285cbcf2018-04-27 11:56:14 +0200209 */
210int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val);
211
212/**
Simon Glass6f98b752015-06-23 15:38:42 -0600213 * regmap_init_mem() - Set up a new register map that uses memory access
214 *
Masahiro Yamadad3581232018-04-19 12:14:03 +0900215 * @node: Device node that uses this map
Simon Glass6f98b752015-06-23 15:38:42 -0600216 * @mapp: Returns allocated map
Mario Six604b6692018-10-04 09:00:41 +0200217 * Return: 0 if OK, -ve on error
218 *
219 * Use regmap_uninit() to free it.
Simon Glass6f98b752015-06-23 15:38:42 -0600220 */
Masahiro Yamadad3581232018-04-19 12:14:03 +0900221int regmap_init_mem(ofnode node, struct regmap **mapp);
Simon Glass6f98b752015-06-23 15:38:42 -0600222
Simon Glass1e6ca1a2016-07-04 11:58:22 -0600223/**
Mario Six604b6692018-10-04 09:00:41 +0200224 * regmap_init_mem_platdata() - Set up a new memory register map for
225 * of-platdata
226 *
227 * @dev: Device that uses this map
228 * @reg: List of address, size pairs
229 * @count: Number of pairs (e.g. 1 if the regmap has a single entry)
230 * @mapp: Returns allocated map
231 * Return: 0 if OK, -ve on error
Simon Glass1e6ca1a2016-07-04 11:58:22 -0600232 *
233 * This creates a new regmap with a list of regions passed in, rather than
234 * using the device tree. It only supports 32-bit machines.
235 *
236 * Use regmap_uninit() to free it.
237 *
Simon Glass1e6ca1a2016-07-04 11:58:22 -0600238 */
Simon Glassc20ee0e2017-08-29 14:15:50 -0600239int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
Simon Glass3b2a29e2016-07-04 11:57:59 -0600240 struct regmap **mapp);
241
Simon Glass6f98b752015-06-23 15:38:42 -0600242/**
243 * regmap_get_range() - Obtain the base memory address of a regmap range
244 *
245 * @map: Regmap to query
246 * @range_num: Range to look up
Mario Six604b6692018-10-04 09:00:41 +0200247 * Return: Pointer to the range in question if OK, NULL on error
Simon Glass6f98b752015-06-23 15:38:42 -0600248 */
249void *regmap_get_range(struct regmap *map, unsigned int range_num);
250
251/**
252 * regmap_uninit() - free a previously inited regmap
Mario Six604b6692018-10-04 09:00:41 +0200253 *
254 * @map: Regmap to free
255 * Return: 0 if OK, -ve on error
Simon Glass6f98b752015-06-23 15:38:42 -0600256 */
257int regmap_uninit(struct regmap *map);
258
259#endif