Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | ad8f8ab | 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 | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __REGMAP_H |
| 8 | #define __REGMAP_H |
| 9 | |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 10 | #include <linux/delay.h> |
| 11 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 12 | /** |
Mario Six | 318937e | 2018-10-15 09:24:15 +0200 | [diff] [blame] | 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 | /** |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 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 | /** |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 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 | /** |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 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 | |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 78 | struct regmap_bus; |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 79 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 80 | /** |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 81 | * struct regmap_config - Configure the behaviour of a regmap |
| 82 | * |
| 83 | * @width: Width of the read/write operations. Defaults to |
| 84 | * REGMAP_SIZE_32 if set to 0. |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 85 | * @reg_offset_shift Left shift the register offset by this value before |
| 86 | * performing read or write. |
Pratyush Yadav | 3e4e50a | 2020-09-24 10:04:15 +0530 | [diff] [blame] | 87 | * @r_start: If specified, the regmap is created with one range |
| 88 | * which starts at this address, instead of finding the |
| 89 | * start from device tree. |
| 90 | * @r_size: Same as above for the range size |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 91 | */ |
| 92 | struct regmap_config { |
| 93 | enum regmap_size_t width; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 94 | u32 reg_offset_shift; |
Pratyush Yadav | 3e4e50a | 2020-09-24 10:04:15 +0530 | [diff] [blame] | 95 | ulong r_start; |
| 96 | ulong r_size; |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /** |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 100 | * struct regmap - a way of accessing hardware/bus registers |
| 101 | * |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 102 | * @width: Width of the read/write operations. Defaults to |
| 103 | * REGMAP_SIZE_32 if set to 0. |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 104 | * @reg_offset_shift Left shift the register offset by this value before |
| 105 | * performing read or write. |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 106 | * @range_count: Number of ranges available within the map |
| 107 | * @ranges: Array of ranges |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 108 | */ |
| 109 | struct regmap { |
Mario Six | e5a3d5b | 2018-10-15 09:24:14 +0200 | [diff] [blame] | 110 | enum regmap_endianness_t endianness; |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 111 | enum regmap_size_t width; |
Pratyush Yadav | 3b94e5d | 2020-09-24 10:04:13 +0530 | [diff] [blame] | 112 | u32 reg_offset_shift; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 113 | int range_count; |
Masahiro Yamada | 54c5ecb | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 114 | struct regmap_range ranges[0]; |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* |
| 118 | * Interface to provide access to registers either through a direct memory |
| 119 | * bus or through a peripheral bus like I2C, SPI. |
| 120 | */ |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 121 | |
| 122 | /** |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 123 | * regmap_write() - Write a value to a regmap |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 124 | * |
| 125 | * @map: Regmap to write to |
| 126 | * @offset: Offset in the regmap to write to |
| 127 | * @val: Data to write to the regmap at the specified offset |
| 128 | * |
| 129 | * Return: 0 if OK, -ve on error |
| 130 | */ |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 131 | int regmap_write(struct regmap *map, uint offset, uint val); |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 134 | * regmap_read() - Read a value from a regmap |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 135 | * |
| 136 | * @map: Regmap to read from |
| 137 | * @offset: Offset in the regmap to read from |
| 138 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 139 | * at the specified offset |
| 140 | * |
| 141 | * Return: 0 if OK, -ve on error |
| 142 | */ |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 143 | int regmap_read(struct regmap *map, uint offset, uint *valp); |
| 144 | |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 145 | /** |
| 146 | * regmap_raw_write() - Write a value of specified length to a regmap |
| 147 | * |
| 148 | * @map: Regmap to write to |
| 149 | * @offset: Offset in the regmap to write to |
| 150 | * @val: Value to write to the regmap at the specified offset |
| 151 | * @val_len: Length of the data to be written to the regmap |
| 152 | * |
| 153 | * Note that this function will, as opposed to regmap_write, write data of |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 154 | * arbitrary length to the regmap, and not just the size configured in the |
| 155 | * regmap (defaults to 32-bit) and is thus a generalized version of |
| 156 | * regmap_write. |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 157 | * |
| 158 | * Return: 0 if OK, -ve on error |
| 159 | */ |
| 160 | int regmap_raw_write(struct regmap *map, uint offset, const void *val, |
| 161 | size_t val_len); |
| 162 | |
| 163 | /** |
| 164 | * regmap_raw_read() - Read a value of specified length from a regmap |
| 165 | * |
| 166 | * @map: Regmap to read from |
| 167 | * @offset: Offset in the regmap to read from |
| 168 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 169 | * at the specified offset |
| 170 | * @val_len: Length of the data to be read from the regmap |
| 171 | * |
| 172 | * Note that this function will, as opposed to regmap_read, read data of |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 173 | * arbitrary length from the regmap, and not just the size configured in the |
| 174 | * regmap (defaults to 32-bit) and is thus a generalized version of |
| 175 | * regmap_read. |
Mario Six | a4fd59e | 2018-10-15 09:24:10 +0200 | [diff] [blame] | 176 | * |
| 177 | * Return: 0 if OK, -ve on error |
| 178 | */ |
| 179 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, |
| 180 | size_t val_len); |
| 181 | |
Mario Six | 2f00997 | 2018-10-15 09:24:11 +0200 | [diff] [blame] | 182 | /** |
| 183 | * regmap_raw_write_range() - Write a value of specified length to a range of a |
| 184 | * regmap |
| 185 | * |
| 186 | * @map: Regmap to write to |
| 187 | * @range_num: Number of the range in the regmap to write to |
| 188 | * @offset: Offset in the regmap to write to |
| 189 | * @val: Value to write to the regmap at the specified offset |
| 190 | * @val_len: Length of the data to be written to the regmap |
| 191 | * |
| 192 | * Return: 0 if OK, -ve on error |
| 193 | */ |
| 194 | int regmap_raw_write_range(struct regmap *map, uint range_num, uint offset, |
| 195 | const void *val, size_t val_len); |
| 196 | |
| 197 | /** |
| 198 | * regmap_raw_read_range() - Read a value of specified length from a range of a |
| 199 | * regmap |
| 200 | * |
| 201 | * @map: Regmap to read from |
| 202 | * @range_num: Number of the range in the regmap to write to |
| 203 | * @offset: Offset in the regmap to read from |
| 204 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 205 | * at the specified offset |
| 206 | * @val_len: Length of the data to be read from the regmap |
| 207 | * |
| 208 | * Return: 0 if OK, -ve on error |
| 209 | */ |
| 210 | int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, |
| 211 | void *valp, size_t val_len); |
| 212 | |
Mario Six | 83ceba2 | 2018-10-15 09:24:12 +0200 | [diff] [blame] | 213 | /** |
| 214 | * regmap_range_set() - Set a value in a regmap range described by a struct |
| 215 | * @map: Regmap in which a value should be set |
| 216 | * @range: Range of the regmap in which a value should be set |
| 217 | * @type: Structure type that describes the memory layout of the regmap range |
| 218 | * @member: Member of the describing structure that should be set in the regmap |
| 219 | * range |
| 220 | * @val: Value which should be written to the regmap range |
| 221 | */ |
| 222 | #define regmap_range_set(map, range, type, member, val) \ |
| 223 | do { \ |
| 224 | typeof(((type *)0)->member) __tmp = val; \ |
| 225 | regmap_raw_write_range(map, range, offsetof(type, member), \ |
| 226 | &__tmp, sizeof(((type *)0)->member)); \ |
| 227 | } while (0) |
| 228 | |
| 229 | /** |
| 230 | * regmap_set() - Set a value in a regmap described by a struct |
| 231 | * @map: Regmap in which a value should be set |
| 232 | * @type: Structure type that describes the memory layout of the regmap |
| 233 | * @member: Member of the describing structure that should be set in the regmap |
| 234 | * @val: Value which should be written to the regmap |
| 235 | */ |
| 236 | #define regmap_set(map, type, member, val) \ |
| 237 | regmap_range_set(map, 0, type, member, val) |
| 238 | |
| 239 | /** |
| 240 | * regmap_range_get() - Get a value from a regmap range described by a struct |
| 241 | * @map: Regmap from which a value should be read |
| 242 | * @range: Range of the regmap from which a value should be read |
| 243 | * @type: Structure type that describes the memory layout of the regmap |
| 244 | * range |
| 245 | * @member: Member of the describing structure that should be read in the |
| 246 | * regmap range |
| 247 | * @valp: Variable that receives the value read from the regmap range |
| 248 | */ |
| 249 | #define regmap_range_get(map, range, type, member, valp) \ |
| 250 | regmap_raw_read_range(map, range, offsetof(type, member), \ |
| 251 | (void *)valp, sizeof(((type *)0)->member)) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 252 | |
Mario Six | 83ceba2 | 2018-10-15 09:24:12 +0200 | [diff] [blame] | 253 | /** |
| 254 | * regmap_get() - Get a value from a regmap described by a struct |
| 255 | * @map: Regmap from which a value should be read |
| 256 | * @type: Structure type that describes the memory layout of the regmap |
| 257 | * range |
| 258 | * @member: Member of the describing structure that should be read in the |
| 259 | * regmap |
| 260 | * @valp: Variable that receives the value read from the regmap |
| 261 | */ |
| 262 | #define regmap_get(map, type, member, valp) \ |
| 263 | regmap_range_get(map, 0, type, member, valp) |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 264 | |
| 265 | /** |
Neil Armstrong | 66cce82 | 2018-11-22 11:01:03 +0100 | [diff] [blame] | 266 | * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs |
| 267 | * |
| 268 | * @map: Regmap to read from |
| 269 | * @addr: Offset to poll |
| 270 | * @val: Unsigned integer variable to read the value into |
| 271 | * @cond: Break condition (usually involving @val) |
| 272 | * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). |
| 273 | * @timeout_ms: Timeout in ms, 0 means never timeout |
Simon Glass | c2a7c49 | 2018-12-09 17:11:10 -0700 | [diff] [blame] | 274 | * @test_add_time: Used for sandbox testing - amount of time to add after |
| 275 | * starting the loop (0 if not testing) |
Neil Armstrong | 66cce82 | 2018-11-22 11:01:03 +0100 | [diff] [blame] | 276 | * |
| 277 | * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read |
| 278 | * error return value in case of a error read. In the two former cases, |
| 279 | * the last read value at @addr is stored in @val. Must not be called |
| 280 | * from atomic context if sleep_us or timeout_us are used. |
| 281 | * |
| 282 | * This is modelled after the regmap_read_poll_timeout macros in linux but |
| 283 | * with millisecond timeout. |
Simon Glass | c2a7c49 | 2018-12-09 17:11:10 -0700 | [diff] [blame] | 284 | * |
| 285 | * The _test version is for sandbox testing only. Do not use this in normal |
| 286 | * code as it advances the timer. |
Neil Armstrong | 66cce82 | 2018-11-22 11:01:03 +0100 | [diff] [blame] | 287 | */ |
Simon Glass | c2a7c49 | 2018-12-09 17:11:10 -0700 | [diff] [blame] | 288 | #define regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \ |
| 289 | timeout_ms, test_add_time) \ |
Neil Armstrong | 66cce82 | 2018-11-22 11:01:03 +0100 | [diff] [blame] | 290 | ({ \ |
| 291 | unsigned long __start = get_timer(0); \ |
| 292 | int __ret; \ |
| 293 | for (;;) { \ |
| 294 | __ret = regmap_read((map), (addr), &(val)); \ |
| 295 | if (__ret) \ |
| 296 | break; \ |
| 297 | if (cond) \ |
| 298 | break; \ |
Simon Glass | c2a7c49 | 2018-12-09 17:11:10 -0700 | [diff] [blame] | 299 | if (IS_ENABLED(CONFIG_SANDBOX) && test_add_time) \ |
Neil Armstrong | 77c0dbc | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 300 | timer_test_add_offset(test_add_time); \ |
Neil Armstrong | 66cce82 | 2018-11-22 11:01:03 +0100 | [diff] [blame] | 301 | if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \ |
| 302 | __ret = regmap_read((map), (addr), &(val)); \ |
| 303 | break; \ |
| 304 | } \ |
| 305 | if ((sleep_us)) \ |
| 306 | udelay((sleep_us)); \ |
| 307 | } \ |
| 308 | __ret ?: ((cond) ? 0 : -ETIMEDOUT); \ |
| 309 | }) |
| 310 | |
Simon Glass | c2a7c49 | 2018-12-09 17:11:10 -0700 | [diff] [blame] | 311 | #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_ms) \ |
| 312 | regmap_read_poll_timeout_test(map, addr, val, cond, sleep_us, \ |
| 313 | timeout_ms, 0) \ |
| 314 | |
Neil Armstrong | 66cce82 | 2018-11-22 11:01:03 +0100 | [diff] [blame] | 315 | /** |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 316 | * regmap_field_read_poll_timeout - Poll until a condition is met or a timeout |
| 317 | * occurs |
| 318 | * |
| 319 | * @field: Regmap field to read from |
| 320 | * @val: Unsigned integer variable to read the value into |
| 321 | * @cond: Break condition (usually involving @val) |
| 322 | * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). |
| 323 | * @timeout_ms: Timeout in ms, 0 means never timeout |
| 324 | * |
| 325 | * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_field_read |
| 326 | * error return value in case of a error read. In the two former cases, |
| 327 | * the last read value at @addr is stored in @val. |
| 328 | * |
| 329 | * This is modelled after the regmap_read_poll_timeout macros in linux but |
| 330 | * with millisecond timeout. |
| 331 | */ |
| 332 | #define regmap_field_read_poll_timeout(field, val, cond, sleep_us, timeout_ms) \ |
| 333 | ({ \ |
| 334 | unsigned long __start = get_timer(0); \ |
| 335 | int __ret; \ |
| 336 | for (;;) { \ |
| 337 | __ret = regmap_field_read((field), &(val)); \ |
| 338 | if (__ret) \ |
| 339 | break; \ |
| 340 | if (cond) \ |
| 341 | break; \ |
| 342 | if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \ |
| 343 | __ret = regmap_field_read((field), &(val)); \ |
| 344 | break; \ |
| 345 | } \ |
| 346 | if ((sleep_us)) \ |
| 347 | udelay((sleep_us)); \ |
| 348 | } \ |
| 349 | __ret ?: ((cond) ? 0 : -ETIMEDOUT); \ |
| 350 | }) |
| 351 | |
| 352 | /** |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 353 | * regmap_update_bits() - Perform a read/modify/write using a mask |
| 354 | * |
| 355 | * @map: The map returned by regmap_init_mem*() |
| 356 | * @offset: Offset of the memory |
| 357 | * @mask: Mask to apply to the read value |
Simon Glass | 56bed2a | 2019-10-11 16:16:49 -0600 | [diff] [blame] | 358 | * @val: Value to OR with the read value after masking. Note that any |
| 359 | * bits set in @val which are not set in @mask are ignored |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 360 | * Return: 0 if OK, -ve on error |
Neil Armstrong | 5444ec6 | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 361 | */ |
| 362 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val); |
| 363 | |
| 364 | /** |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 365 | * regmap_init_mem() - Set up a new register map that uses memory access |
| 366 | * |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 367 | * @node: Device node that uses this map |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 368 | * @mapp: Returns allocated map |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 369 | * Return: 0 if OK, -ve on error |
| 370 | * |
| 371 | * Use regmap_uninit() to free it. |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 372 | */ |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 373 | int regmap_init_mem(ofnode node, struct regmap **mapp); |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 374 | |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 375 | /** |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 376 | * regmap_init_mem_plat() - Set up a new memory register map for |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 377 | * of-platdata |
| 378 | * |
| 379 | * @dev: Device that uses this map |
| 380 | * @reg: List of address, size pairs |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 381 | * @size: Size of one reg array item |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 382 | * @count: Number of pairs (e.g. 1 if the regmap has a single entry) |
| 383 | * @mapp: Returns allocated map |
| 384 | * Return: 0 if OK, -ve on error |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 385 | * |
| 386 | * This creates a new regmap with a list of regions passed in, rather than |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 387 | * using the device tree. |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 388 | * |
| 389 | * Use regmap_uninit() to free it. |
| 390 | * |
Simon Glass | b611433 | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 391 | */ |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 392 | int regmap_init_mem_plat(struct udevice *dev, void *reg, int size, int count, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 393 | struct regmap **mapp); |
Simon Glass | b944345 | 2016-07-04 11:57:59 -0600 | [diff] [blame] | 394 | |
Faiz Abbas | 52742f4 | 2019-06-11 00:43:33 +0530 | [diff] [blame] | 395 | int regmap_init_mem_index(ofnode node, struct regmap **mapp, int index); |
| 396 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 397 | /** |
Pratyush Yadav | 7eb2476 | 2020-09-24 10:04:14 +0530 | [diff] [blame] | 398 | * regmap_init_mem_range() - Set up a new memory region for ofnode with the |
| 399 | * specified range. |
| 400 | * |
| 401 | * @node: The ofnode for the map. |
| 402 | * @r_start: Start of the range. |
| 403 | * @r_size: Size of the range. |
| 404 | * @mapp: Returns allocated map. |
| 405 | * |
| 406 | * Return: 0 in success, -errno otherwise |
| 407 | * |
| 408 | * This creates a regmap with one range where instead of extracting the range |
| 409 | * from 'node', it is created based on the parameters specified. This is |
| 410 | * useful when a driver needs to calculate the base of the regmap at runtime, |
| 411 | * and can't specify it in device tree. |
| 412 | */ |
| 413 | int regmap_init_mem_range(ofnode node, ulong r_start, ulong r_size, |
| 414 | struct regmap **mapp); |
| 415 | |
| 416 | /** |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 417 | * devm_regmap_init() - Initialise register map (device managed) |
| 418 | * |
| 419 | * @dev: Device that will be interacted with |
| 420 | * @bus: Bus-specific callbacks to use with device (IGNORED) |
| 421 | * @bus_context: Data passed to bus-specific callbacks (IGNORED) |
Pratyush Yadav | 1c9867c | 2020-09-24 10:04:12 +0530 | [diff] [blame] | 422 | * @config: Configuration for register map |
Jean-Jacques Hiblot | de03d12 | 2020-09-24 10:04:10 +0530 | [diff] [blame] | 423 | * |
| 424 | * @Return a valid pointer to a struct regmap or a ERR_PTR() on error. |
| 425 | * The structure is automatically freed when the device is unbound |
| 426 | */ |
| 427 | struct regmap *devm_regmap_init(struct udevice *dev, |
| 428 | const struct regmap_bus *bus, |
| 429 | void *bus_context, |
| 430 | const struct regmap_config *config); |
| 431 | /** |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 432 | * regmap_get_range() - Obtain the base memory address of a regmap range |
| 433 | * |
| 434 | * @map: Regmap to query |
| 435 | * @range_num: Range to look up |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 436 | * Return: Pointer to the range in question if OK, NULL on error |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 437 | */ |
| 438 | void *regmap_get_range(struct regmap *map, unsigned int range_num); |
| 439 | |
| 440 | /** |
| 441 | * regmap_uninit() - free a previously inited regmap |
Mario Six | 1d67cfb | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 442 | * |
| 443 | * @map: Regmap to free |
| 444 | * Return: 0 if OK, -ve on error |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 445 | */ |
| 446 | int regmap_uninit(struct regmap *map); |
| 447 | |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 448 | /** |
| 449 | * struct reg_field - Description of an register field |
| 450 | * |
| 451 | * @reg: Offset of the register within the regmap bank |
| 452 | * @lsb: lsb of the register field. |
| 453 | * @msb: msb of the register field. |
| 454 | */ |
| 455 | struct reg_field { |
| 456 | unsigned int reg; |
| 457 | unsigned int lsb; |
| 458 | unsigned int msb; |
| 459 | }; |
| 460 | |
| 461 | struct regmap_field; |
| 462 | |
| 463 | /** |
Ralph Siemsen | 5c202c1 | 2022-06-24 11:19:15 -0400 | [diff] [blame] | 464 | * REG_FIELD() - A convenient way to initialize a 'struct reg_field'. |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 465 | * |
| 466 | * @_reg: Offset of the register within the regmap bank |
| 467 | * @_lsb: lsb of the register field. |
| 468 | * @_msb: msb of the register field. |
| 469 | * |
| 470 | * Register fields are often described in terms of 3 things: the register it |
| 471 | * belongs to, its LSB, and its MSB. This macro can be used by drivers to |
| 472 | * clearly and easily initialize a 'struct regmap_field'. |
| 473 | * |
| 474 | * For example, say a device has a register at offset DEV_REG1 (0x100) and a |
| 475 | * field of DEV_REG1 is on bits [7:3]. So a driver can initialize a regmap |
| 476 | * field for this by doing: |
| 477 | * struct reg_field field = REG_FIELD(DEV_REG1, 3, 7); |
| 478 | */ |
| 479 | #define REG_FIELD(_reg, _lsb, _msb) { \ |
| 480 | .reg = _reg, \ |
| 481 | .lsb = _lsb, \ |
| 482 | .msb = _msb, \ |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * devm_regmap_field_alloc() - Allocate and initialise a register field. |
| 487 | * |
| 488 | * @dev: Device that will be interacted with |
| 489 | * @regmap: regmap bank in which this register field is located. |
| 490 | * @reg_field: Register field with in the bank. |
| 491 | * |
| 492 | * The return value will be an ERR_PTR() on error or a valid pointer |
| 493 | * to a struct regmap_field. The regmap_field will be automatically freed |
| 494 | * by the device management code. |
| 495 | */ |
| 496 | struct regmap_field *devm_regmap_field_alloc(struct udevice *dev, |
| 497 | struct regmap *regmap, |
| 498 | struct reg_field reg_field); |
| 499 | /** |
| 500 | * devm_regmap_field_free() - Free a register field allocated using |
| 501 | * devm_regmap_field_alloc. |
| 502 | * |
| 503 | * @dev: Device that will be interacted with |
| 504 | * @field: regmap field which should be freed. |
| 505 | * |
| 506 | * Free register field allocated using devm_regmap_field_alloc(). Usually |
| 507 | * drivers need not call this function, as the memory allocated via devm |
| 508 | * will be freed as per device-driver life-cyle. |
| 509 | */ |
| 510 | void devm_regmap_field_free(struct udevice *dev, struct regmap_field *field); |
| 511 | |
| 512 | /** |
| 513 | * regmap_field_write() - Write a value to a regmap field |
| 514 | * |
| 515 | * @field: Regmap field to write to |
| 516 | * @val: Data to write to the regmap at the specified offset |
| 517 | * |
| 518 | * Return: 0 if OK, -ve on error |
| 519 | */ |
| 520 | int regmap_field_write(struct regmap_field *field, unsigned int val); |
| 521 | |
| 522 | /** |
Ralph Siemsen | 5c202c1 | 2022-06-24 11:19:15 -0400 | [diff] [blame] | 523 | * regmap_field_read() - Read a 32-bit value from a regmap |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 524 | * |
Ralph Siemsen | 5c202c1 | 2022-06-24 11:19:15 -0400 | [diff] [blame] | 525 | * @field: Regmap field to read from |
Jean-Jacques Hiblot | b3bbbcf | 2020-09-24 10:04:16 +0530 | [diff] [blame] | 526 | * @valp: Pointer to the buffer to receive the data read from the regmap |
| 527 | * field |
| 528 | * |
| 529 | * Return: 0 if OK, -ve on error |
| 530 | */ |
| 531 | int regmap_field_read(struct regmap_field *field, unsigned int *val); |
| 532 | |
Simon Glass | ad8f8ab | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 533 | #endif |