Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __REGMAP_H |
| 8 | #define __REGMAP_H |
| 9 | |
| 10 | /** |
Mario Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 11 | * 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 | */ |
| 18 | enum 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 | /** |
Mario Six | 9b77fe3 | 2018-10-15 09:24:14 +0200 | [diff] [blame^] | 26 | * enum regmap_endianness_t - Endianness for regmap reads and writes |
| 27 | * |
| 28 | * @REGMAP_NATIVE_ENDIAN: Native endian read/write accesses |
| 29 | * @REGMAP_LITTLE_ENDIAN: Little endian read/write accesses |
| 30 | * @REGMAP_BIG_ENDIAN: Big endian read/write accesses |
| 31 | */ |
| 32 | enum regmap_endianness_t { |
| 33 | REGMAP_NATIVE_ENDIAN, |
| 34 | REGMAP_LITTLE_ENDIAN, |
| 35 | REGMAP_BIG_ENDIAN, |
| 36 | }; |
| 37 | |
| 38 | /** |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 39 | * struct regmap_range - a register map range |
| 40 | * |
| 41 | * @start: Start address |
| 42 | * @size: Size in bytes |
| 43 | */ |
| 44 | struct regmap_range { |
| 45 | ulong start; |
| 46 | ulong size; |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * struct regmap - a way of accessing hardware/bus registers |
| 51 | * |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 52 | * @range_count: Number of ranges available within the map |
| 53 | * @ranges: Array of ranges |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 54 | */ |
| 55 | struct regmap { |
Mario Six | 9b77fe3 | 2018-10-15 09:24:14 +0200 | [diff] [blame^] | 56 | enum regmap_endianness_t endianness; |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 57 | int range_count; |
Masahiro Yamada | 8c1de5e | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 58 | struct regmap_range ranges[0]; |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * Interface to provide access to registers either through a direct memory |
| 63 | * bus or through a peripheral bus like I2C, SPI. |
| 64 | */ |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * regmap_write() - Write a 32-bit value to a regmap |
| 68 | * |
| 69 | * @map: Regmap to write to |
| 70 | * @offset: Offset in the regmap to write to |
| 71 | * @val: Data to write to the regmap at the specified offset |
| 72 | * |
Mario Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 73 | * Note that this function will only write values of 32 bit width to the |
| 74 | * regmap; if the size of data to be read is different, the regmap_raw_write |
| 75 | * function can be used. |
| 76 | * |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 77 | * Return: 0 if OK, -ve on error |
| 78 | */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 79 | int regmap_write(struct regmap *map, uint offset, uint val); |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * regmap_read() - Read a 32-bit value from a regmap |
| 83 | * |
| 84 | * @map: Regmap to read from |
| 85 | * @offset: Offset in the regmap to read from |
| 86 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 87 | * at the specified offset |
| 88 | * |
Mario Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 89 | * Note that this function will only read values of 32 bit width from the |
| 90 | * regmap; if the size of data to be read is different, the regmap_raw_read |
| 91 | * function can be used. |
| 92 | * |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 93 | * Return: 0 if OK, -ve on error |
| 94 | */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 95 | int regmap_read(struct regmap *map, uint offset, uint *valp); |
| 96 | |
Mario Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 97 | /** |
| 98 | * regmap_raw_write() - Write a value of specified length to a regmap |
| 99 | * |
| 100 | * @map: Regmap to write to |
| 101 | * @offset: Offset in the regmap to write to |
| 102 | * @val: Value to write to the regmap at the specified offset |
| 103 | * @val_len: Length of the data to be written to the regmap |
| 104 | * |
| 105 | * Note that this function will, as opposed to regmap_write, write data of |
| 106 | * arbitrary length to the regmap, and not just 32-bit values, and is thus a |
| 107 | * generalized version of regmap_write. |
| 108 | * |
| 109 | * Return: 0 if OK, -ve on error |
| 110 | */ |
| 111 | int regmap_raw_write(struct regmap *map, uint offset, const void *val, |
| 112 | size_t val_len); |
| 113 | |
| 114 | /** |
| 115 | * regmap_raw_read() - Read a value of specified length from a regmap |
| 116 | * |
| 117 | * @map: Regmap to read from |
| 118 | * @offset: Offset in the regmap to read from |
| 119 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 120 | * at the specified offset |
| 121 | * @val_len: Length of the data to be read from the regmap |
| 122 | * |
| 123 | * Note that this function will, as opposed to regmap_read, read data of |
| 124 | * arbitrary length from the regmap, and not just 32-bit values, and is thus a |
| 125 | * generalized version of regmap_read. |
| 126 | * |
| 127 | * Return: 0 if OK, -ve on error |
| 128 | */ |
| 129 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, |
| 130 | size_t val_len); |
| 131 | |
Mario Six | d5c7bd9 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 132 | /** |
| 133 | * regmap_raw_write_range() - Write a value of specified length to a range of a |
| 134 | * regmap |
| 135 | * |
| 136 | * @map: Regmap to write to |
| 137 | * @range_num: Number of the range in the regmap to write to |
| 138 | * @offset: Offset in the regmap to write to |
| 139 | * @val: Value to write to the regmap at the specified offset |
| 140 | * @val_len: Length of the data to be written to the regmap |
| 141 | * |
| 142 | * Return: 0 if OK, -ve on error |
| 143 | */ |
| 144 | int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, |
| 145 | const void *val, size_t val_len); |
| 146 | |
| 147 | /** |
| 148 | * regmap_raw_read_range() - Read a value of specified length from a range of a |
| 149 | * regmap |
| 150 | * |
| 151 | * @map: Regmap to read from |
| 152 | * @range_num: Number of the range in the regmap to write to |
| 153 | * @offset: Offset in the regmap to read from |
| 154 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 155 | * at the specified offset |
| 156 | * @val_len: Length of the data to be read from the regmap |
| 157 | * |
| 158 | * Return: 0 if OK, -ve on error |
| 159 | */ |
| 160 | int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, |
| 161 | void *valp, size_t val_len); |
| 162 | |
Mario Six | e936397 | 2018-10-15 09:24:12 +0200 | [diff] [blame] | 163 | /** |
| 164 | * regmap_range_set() - Set a value in a regmap range described by a struct |
| 165 | * @map: Regmap in which a value should be set |
| 166 | * @range: Range of the regmap in which a value should be set |
| 167 | * @type: Structure type that describes the memory layout of the regmap range |
| 168 | * @member: Member of the describing structure that should be set in the regmap |
| 169 | * range |
| 170 | * @val: Value which should be written to the regmap range |
| 171 | */ |
| 172 | #define regmap_range_set(map, range, type, member, val) \ |
| 173 | do { \ |
| 174 | typeof(((type *)0)->member) __tmp = val; \ |
| 175 | regmap_raw_write_range(map, range, offsetof(type, member), \ |
| 176 | &__tmp, sizeof(((type *)0)->member)); \ |
| 177 | } while (0) |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 178 | |
Mario Six | e936397 | 2018-10-15 09:24:12 +0200 | [diff] [blame] | 179 | /** |
| 180 | * regmap_set() - Set a value in a regmap described by a struct |
| 181 | * @map: Regmap in which a value should be set |
| 182 | * @type: Structure type that describes the memory layout of the regmap |
| 183 | * @member: Member of the describing structure that should be set in the regmap |
| 184 | * @val: Value which should be written to the regmap |
| 185 | */ |
| 186 | #define regmap_set(map, type, member, val) \ |
| 187 | regmap_range_set(map, 0, type, member, val) |
| 188 | |
| 189 | /** |
| 190 | * regmap_range_get() - Get a value from a regmap range described by a struct |
| 191 | * @map: Regmap from which a value should be read |
| 192 | * @range: Range of the regmap from which a value should be read |
| 193 | * @type: Structure type that describes the memory layout of the regmap |
| 194 | * range |
| 195 | * @member: Member of the describing structure that should be read in the |
| 196 | * regmap range |
| 197 | * @valp: Variable that receives the value read from the regmap range |
| 198 | */ |
| 199 | #define regmap_range_get(map, range, type, member, valp) \ |
| 200 | regmap_raw_read_range(map, range, offsetof(type, member), \ |
| 201 | (void *)valp, sizeof(((type *)0)->member)) |
| 202 | |
| 203 | /** |
| 204 | * regmap_get() - Get a value from a regmap described by a struct |
| 205 | * @map: Regmap from which a value should be read |
| 206 | * @type: Structure type that describes the memory layout of the regmap |
| 207 | * range |
| 208 | * @member: Member of the describing structure that should be read in the |
| 209 | * regmap |
| 210 | * @valp: Variable that receives the value read from the regmap |
| 211 | */ |
| 212 | #define regmap_get(map, type, member, valp) \ |
| 213 | regmap_range_get(map, 0, type, member, valp) |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 214 | |
| 215 | /** |
Neil Armstrong | 285cbcf | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 216 | * regmap_update_bits() - Perform a read/modify/write using a mask |
| 217 | * |
| 218 | * @map: The map returned by regmap_init_mem*() |
| 219 | * @offset: Offset of the memory |
| 220 | * @mask: Mask to apply to the read value |
| 221 | * @val: Value to apply to the value to write |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 222 | * Return: 0 if OK, -ve on error |
Neil Armstrong | 285cbcf | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 223 | */ |
| 224 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val); |
| 225 | |
| 226 | /** |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 227 | * regmap_init_mem() - Set up a new register map that uses memory access |
| 228 | * |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 229 | * @node: Device node that uses this map |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 230 | * @mapp: Returns allocated map |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 231 | * Return: 0 if OK, -ve on error |
| 232 | * |
| 233 | * Use regmap_uninit() to free it. |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 234 | */ |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 235 | int regmap_init_mem(ofnode node, struct regmap **mapp); |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 236 | |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 237 | /** |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 238 | * regmap_init_mem_platdata() - Set up a new memory register map for |
| 239 | * of-platdata |
| 240 | * |
| 241 | * @dev: Device that uses this map |
| 242 | * @reg: List of address, size pairs |
| 243 | * @count: Number of pairs (e.g. 1 if the regmap has a single entry) |
| 244 | * @mapp: Returns allocated map |
| 245 | * Return: 0 if OK, -ve on error |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 246 | * |
| 247 | * This creates a new regmap with a list of regions passed in, rather than |
| 248 | * using the device tree. It only supports 32-bit machines. |
| 249 | * |
| 250 | * Use regmap_uninit() to free it. |
| 251 | * |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 252 | */ |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 253 | int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count, |
Simon Glass | 3b2a29e | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 254 | struct regmap **mapp); |
| 255 | |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 256 | /** |
| 257 | * regmap_get_range() - Obtain the base memory address of a regmap range |
| 258 | * |
| 259 | * @map: Regmap to query |
| 260 | * @range_num: Range to look up |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 261 | * Return: Pointer to the range in question if OK, NULL on error |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 262 | */ |
| 263 | void *regmap_get_range(struct regmap *map, unsigned int range_num); |
| 264 | |
| 265 | /** |
| 266 | * regmap_uninit() - free a previously inited regmap |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 267 | * |
| 268 | * @map: Regmap to free |
| 269 | * Return: 0 if OK, -ve on error |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 270 | */ |
| 271 | int regmap_uninit(struct regmap *map); |
| 272 | |
| 273 | #endif |