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 | /** |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 26 | * struct regmap_range - a register map range |
| 27 | * |
| 28 | * @start: Start address |
| 29 | * @size: Size in bytes |
| 30 | */ |
| 31 | struct regmap_range { |
| 32 | ulong start; |
| 33 | ulong size; |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * struct regmap - a way of accessing hardware/bus registers |
| 38 | * |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 39 | * @range_count: Number of ranges available within the map |
| 40 | * @ranges: Array of ranges |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 41 | */ |
| 42 | struct regmap { |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 43 | int range_count; |
Masahiro Yamada | 8c1de5e | 2018-04-19 12:14:01 +0900 | [diff] [blame] | 44 | struct regmap_range ranges[0]; |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 45 | }; |
| 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 Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 51 | |
| 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 Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame^] | 59 | * 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 Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 63 | * Return: 0 if OK, -ve on error |
| 64 | */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 65 | int regmap_write(struct regmap *map, uint offset, uint val); |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 66 | |
| 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 Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame^] | 75 | * 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 Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 79 | * Return: 0 if OK, -ve on error |
| 80 | */ |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 81 | int regmap_read(struct regmap *map, uint offset, uint *valp); |
| 82 | |
Mario Six | 84ff8f6 | 2018-10-15 09:24:10 +0200 | [diff] [blame^] | 83 | /** |
| 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 | */ |
| 97 | int 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 | */ |
| 115 | int regmap_raw_read(struct regmap *map, uint offset, void *valp, |
| 116 | size_t val_len); |
| 117 | |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 118 | #define regmap_write32(map, ptr, member, val) \ |
| 119 | regmap_write(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), val) |
| 120 | |
| 121 | #define regmap_read32(map, ptr, member, valp) \ |
| 122 | regmap_read(map, (uint32_t *)(ptr)->member - (uint32_t *)(ptr), valp) |
| 123 | |
| 124 | /** |
Neil Armstrong | 285cbcf | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 125 | * regmap_update_bits() - Perform a read/modify/write using a mask |
| 126 | * |
| 127 | * @map: The map returned by regmap_init_mem*() |
| 128 | * @offset: Offset of the memory |
| 129 | * @mask: Mask to apply to the read value |
| 130 | * @val: Value to apply to the value to write |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 131 | * Return: 0 if OK, -ve on error |
Neil Armstrong | 285cbcf | 2018-04-27 11:56:14 +0200 | [diff] [blame] | 132 | */ |
| 133 | int regmap_update_bits(struct regmap *map, uint offset, uint mask, uint val); |
| 134 | |
| 135 | /** |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 136 | * regmap_init_mem() - Set up a new register map that uses memory access |
| 137 | * |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 138 | * @node: Device node that uses this map |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 139 | * @mapp: Returns allocated map |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 140 | * Return: 0 if OK, -ve on error |
| 141 | * |
| 142 | * Use regmap_uninit() to free it. |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 143 | */ |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 144 | int regmap_init_mem(ofnode node, struct regmap **mapp); |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 145 | |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 146 | /** |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 147 | * regmap_init_mem_platdata() - Set up a new memory register map for |
| 148 | * of-platdata |
| 149 | * |
| 150 | * @dev: Device that uses this map |
| 151 | * @reg: List of address, size pairs |
| 152 | * @count: Number of pairs (e.g. 1 if the regmap has a single entry) |
| 153 | * @mapp: Returns allocated map |
| 154 | * Return: 0 if OK, -ve on error |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 155 | * |
| 156 | * This creates a new regmap with a list of regions passed in, rather than |
| 157 | * using the device tree. It only supports 32-bit machines. |
| 158 | * |
| 159 | * Use regmap_uninit() to free it. |
| 160 | * |
Simon Glass | 1e6ca1a | 2016-07-04 11:58:22 -0600 | [diff] [blame] | 161 | */ |
Simon Glass | c20ee0e | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 162 | 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] | 163 | struct regmap **mapp); |
| 164 | |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 165 | /** |
| 166 | * regmap_get_range() - Obtain the base memory address of a regmap range |
| 167 | * |
| 168 | * @map: Regmap to query |
| 169 | * @range_num: Range to look up |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 170 | * Return: Pointer to the range in question if OK, NULL on error |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 171 | */ |
| 172 | void *regmap_get_range(struct regmap *map, unsigned int range_num); |
| 173 | |
| 174 | /** |
| 175 | * regmap_uninit() - free a previously inited regmap |
Mario Six | 604b669 | 2018-10-04 09:00:41 +0200 | [diff] [blame] | 176 | * |
| 177 | * @map: Regmap to free |
| 178 | * Return: 0 if OK, -ve on error |
Simon Glass | 6f98b75 | 2015-06-23 15:38:42 -0600 | [diff] [blame] | 179 | */ |
| 180 | int regmap_uninit(struct regmap *map); |
| 181 | |
| 182 | #endif |