1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Register map access API internal header 4 * 5 * Copyright 2011 Wolfson Microelectronics plc 6 * 7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 8 */ 9 10 #ifndef _REGMAP_INTERNAL_H 11 #define _REGMAP_INTERNAL_H 12 13 #include <linux/cleanup.h> 14 #include <linux/device.h> 15 #include <linux/regmap.h> 16 #include <linux/fs.h> 17 #include <linux/list.h> 18 #include <linux/wait.h> 19 20 struct regmap; 21 struct regcache_ops; 22 23 struct regmap_debugfs_off_cache { 24 struct list_head list; 25 off_t min; 26 off_t max; 27 unsigned int base_reg; 28 unsigned int max_reg; 29 }; 30 31 struct regmap_format { 32 size_t buf_size; 33 size_t reg_bytes; 34 size_t pad_bytes; 35 size_t val_bytes; 36 s8 reg_shift; 37 void (*format_write)(struct regmap *map, 38 unsigned int reg, unsigned int val); 39 void (*format_reg)(void *buf, unsigned int reg, unsigned int shift); 40 void (*format_val)(void *buf, unsigned int val, unsigned int shift); 41 unsigned int (*parse_val)(const void *buf); 42 void (*parse_inplace)(void *buf); 43 }; 44 45 struct regmap_async { 46 struct list_head list; 47 struct regmap *map; 48 void *work_buf; 49 }; 50 51 struct regmap { 52 union { 53 struct mutex mutex; 54 struct { 55 spinlock_t spinlock; 56 unsigned long spinlock_flags; 57 }; 58 struct { 59 raw_spinlock_t raw_spinlock; 60 unsigned long raw_spinlock_flags; 61 }; 62 }; 63 struct lock_class_key *lock_key; 64 regmap_lock lock; 65 regmap_unlock unlock; 66 void *lock_arg; /* This is passed to lock/unlock functions */ 67 gfp_t alloc_flags; 68 unsigned int reg_base; 69 70 struct device *dev; /* Device we do I/O on */ 71 void *work_buf; /* Scratch buffer used to format I/O */ 72 struct regmap_format format; /* Buffer format */ 73 const struct regmap_bus *bus; 74 void *bus_context; 75 const char *name; 76 77 spinlock_t async_lock; 78 wait_queue_head_t async_waitq; 79 struct list_head async_list; 80 struct list_head async_free; 81 int async_ret; 82 bool async; 83 84 #ifdef CONFIG_DEBUG_FS 85 bool debugfs_disable; 86 struct dentry *debugfs; 87 const char *debugfs_name; 88 int debugfs_dummy_id; 89 90 unsigned int debugfs_reg_len; 91 unsigned int debugfs_val_len; 92 unsigned int debugfs_tot_len; 93 94 struct list_head debugfs_off_cache; 95 struct mutex cache_lock; 96 #endif 97 98 unsigned int max_register; 99 bool max_register_is_set; 100 bool (*writeable_reg)(struct device *dev, unsigned int reg); 101 bool (*readable_reg)(struct device *dev, unsigned int reg); 102 bool (*volatile_reg)(struct device *dev, unsigned int reg); 103 bool (*precious_reg)(struct device *dev, unsigned int reg); 104 bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg); 105 bool (*readable_noinc_reg)(struct device *dev, unsigned int reg); 106 const struct regmap_access_table *wr_table; 107 const struct regmap_access_table *rd_table; 108 const struct regmap_access_table *volatile_table; 109 const struct regmap_access_table *precious_table; 110 const struct regmap_access_table *wr_noinc_table; 111 const struct regmap_access_table *rd_noinc_table; 112 113 int (*reg_read)(void *context, unsigned int reg, unsigned int *val); 114 int (*reg_write)(void *context, unsigned int reg, unsigned int val); 115 int (*reg_update_bits)(void *context, unsigned int reg, 116 unsigned int mask, unsigned int val); 117 /* Bulk read/write */ 118 int (*read)(void *context, const void *reg_buf, size_t reg_size, 119 void *val_buf, size_t val_size); 120 int (*write)(void *context, const void *data, size_t count); 121 122 int (*reg_default_cb)(struct device *dev, unsigned int reg, 123 unsigned int *val); 124 125 unsigned long read_flag_mask; 126 unsigned long write_flag_mask; 127 128 /* number of bits to (left) shift the reg value when formatting*/ 129 int reg_shift; 130 int reg_stride; 131 int reg_stride_order; 132 133 bool defer_caching; 134 135 /* If set, will always write field to HW. */ 136 bool force_write_field; 137 138 /* regcache specific members */ 139 const struct regcache_ops *cache_ops; 140 enum regcache_type cache_type; 141 142 /* number of bytes in reg_defaults_raw */ 143 unsigned int cache_size_raw; 144 /* number of bytes per word in reg_defaults_raw */ 145 unsigned int cache_word_size; 146 /* number of entries in reg_defaults */ 147 unsigned int num_reg_defaults; 148 /* number of entries in reg_defaults_raw */ 149 unsigned int num_reg_defaults_raw; 150 151 /* if set, only the cache is modified not the HW */ 152 bool cache_only; 153 /* if set, only the HW is modified not the cache */ 154 bool cache_bypass; 155 /* if set, remember to free reg_defaults_raw */ 156 bool cache_free; 157 158 struct reg_default *reg_defaults; 159 const void *reg_defaults_raw; 160 void *cache; 161 /* if set, the cache contains newer data than the HW */ 162 bool cache_dirty; 163 /* if set, the HW registers are known to match map->reg_defaults */ 164 bool no_sync_defaults; 165 166 struct reg_sequence *patch; 167 unsigned int patch_regs; 168 169 /* if set, the regmap core can sleep */ 170 bool can_sleep; 171 172 /* if set, converts bulk read to single read */ 173 bool use_single_read; 174 /* if set, converts bulk write to single write */ 175 bool use_single_write; 176 /* if set, the device supports multi write mode */ 177 bool can_multi_write; 178 179 /* if set, raw reads/writes are limited to this size */ 180 size_t max_raw_read; 181 size_t max_raw_write; 182 183 struct rb_root range_tree; 184 void *selector_work_buf; /* Scratch buffer used for selector */ 185 186 struct hwspinlock *hwlock; 187 }; 188 189 /* 190 * Scoped guard for the regmap lock. The lock/unlock callbacks are selected 191 * at init time (mutex, spinlock, raw spinlock, hwspinlock or none) and never 192 * fail, so an unconditional guard is sufficient. Use with guard(regmap)(map) 193 * or scoped_guard(regmap, map) { ... }. 194 */ 195 DEFINE_GUARD(regmap, struct regmap *, 196 _T->lock(_T->lock_arg), 197 _T->unlock(_T->lock_arg)) 198 199 struct regcache_ops { 200 const char *name; 201 enum regcache_type type; 202 int (*init)(struct regmap *map); 203 void (*exit)(struct regmap *map); 204 int (*populate)(struct regmap *map); 205 #ifdef CONFIG_DEBUG_FS 206 void (*debugfs_init)(struct regmap *map); 207 #endif 208 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value); 209 int (*write)(struct regmap *map, unsigned int reg, unsigned int value); 210 int (*sync)(struct regmap *map, unsigned int min, unsigned int max); 211 int (*drop)(struct regmap *map, unsigned int min, unsigned int max); 212 }; 213 214 bool regmap_cached(struct regmap *map, unsigned int reg); 215 bool regmap_writeable(struct regmap *map, unsigned int reg); 216 bool regmap_readable(struct regmap *map, unsigned int reg); 217 bool regmap_volatile(struct regmap *map, unsigned int reg); 218 bool regmap_precious(struct regmap *map, unsigned int reg); 219 bool regmap_writeable_noinc(struct regmap *map, unsigned int reg); 220 bool regmap_readable_noinc(struct regmap *map, unsigned int reg); 221 222 int _regmap_write(struct regmap *map, unsigned int reg, 223 unsigned int val); 224 225 struct regmap_range_node { 226 struct rb_node node; 227 const char *name; 228 struct regmap *map; 229 230 unsigned int range_min; 231 unsigned int range_max; 232 233 unsigned int selector_reg; 234 unsigned int selector_mask; 235 int selector_shift; 236 237 unsigned int window_start; 238 unsigned int window_len; 239 }; 240 241 struct regmap_field { 242 struct regmap *regmap; 243 unsigned int mask; 244 /* lsb */ 245 unsigned int shift; 246 unsigned int reg; 247 248 unsigned int id_size; 249 unsigned int id_offset; 250 }; 251 252 #ifdef CONFIG_DEBUG_FS 253 extern void regmap_debugfs_initcall(void); 254 extern void regmap_debugfs_init(struct regmap *map); 255 extern void regmap_debugfs_exit(struct regmap *map); 256 257 static inline void regmap_debugfs_disable(struct regmap *map) 258 { 259 map->debugfs_disable = true; 260 } 261 262 #else 263 static inline void regmap_debugfs_initcall(void) { } 264 static inline void regmap_debugfs_init(struct regmap *map) { } 265 static inline void regmap_debugfs_exit(struct regmap *map) { } 266 static inline void regmap_debugfs_disable(struct regmap *map) { } 267 #endif 268 269 /* regcache core declarations */ 270 int regcache_init(struct regmap *map, const struct regmap_config *config); 271 void regcache_exit(struct regmap *map); 272 int regcache_read(struct regmap *map, 273 unsigned int reg, unsigned int *value); 274 int regcache_write(struct regmap *map, 275 unsigned int reg, unsigned int value); 276 int regcache_sync(struct regmap *map); 277 int regcache_sync_block(struct regmap *map, void *block, 278 unsigned long *cache_present, 279 unsigned int block_base, unsigned int start, 280 unsigned int end); 281 bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg, 282 unsigned int val); 283 284 static inline const void *regcache_get_val_addr(struct regmap *map, 285 const void *base, 286 unsigned int idx) 287 { 288 return base + (map->cache_word_size * idx); 289 } 290 291 unsigned int regcache_get_val(struct regmap *map, const void *base, 292 unsigned int idx); 293 void regcache_set_val(struct regmap *map, void *base, unsigned int idx, 294 unsigned int val); 295 int regcache_lookup_reg(struct regmap *map, unsigned int reg); 296 int regcache_sync_val(struct regmap *map, unsigned int reg, unsigned int val); 297 298 int _regmap_raw_write(struct regmap *map, unsigned int reg, 299 const void *val, size_t val_len, bool noinc); 300 301 void regmap_async_complete_cb(struct regmap_async *async, int ret); 302 303 enum regmap_endian regmap_get_val_endian(struct device *dev, 304 const struct regmap_bus *bus, 305 const struct regmap_config *config); 306 307 extern struct regcache_ops regcache_flat_sparse_ops; 308 extern struct regcache_ops regcache_rbtree_ops; 309 extern struct regcache_ops regcache_maple_ops; 310 extern struct regcache_ops regcache_flat_ops; 311 312 static inline const char *regmap_name(const struct regmap *map) 313 { 314 if (map->dev) 315 return dev_name(map->dev); 316 317 return map->name; 318 } 319 320 static inline unsigned int regmap_get_offset(const struct regmap *map, 321 unsigned int index) 322 { 323 if (map->reg_stride_order >= 0) 324 return index << map->reg_stride_order; 325 else 326 return index * map->reg_stride; 327 } 328 329 static inline unsigned int regcache_get_index_by_order(const struct regmap *map, 330 unsigned int reg) 331 { 332 return reg >> map->reg_stride_order; 333 } 334 335 struct regmap_ram_data { 336 unsigned int base_reg; 337 unsigned int *vals; /* Allocatd by caller */ 338 bool *read; 339 bool *written; 340 enum regmap_endian reg_endian; 341 bool (*noinc_reg)(struct regmap_ram_data *data, unsigned int reg); 342 }; 343 344 /* 345 * Create a test register map with data stored in RAM, not intended 346 * for practical use. 347 */ 348 struct regmap *__regmap_init_ram(struct device *dev, 349 const struct regmap_config *config, 350 struct regmap_ram_data *data, 351 struct lock_class_key *lock_key, 352 const char *lock_name); 353 354 #define regmap_init_ram(dev, config, data) \ 355 __regmap_lockdep_wrapper(__regmap_init_ram, #dev, dev, config, data) 356 357 struct regmap *__regmap_init_raw_ram(struct device *dev, 358 const struct regmap_config *config, 359 struct regmap_ram_data *data, 360 struct lock_class_key *lock_key, 361 const char *lock_name); 362 363 #define regmap_init_raw_ram(dev, config, data) \ 364 __regmap_lockdep_wrapper(__regmap_init_raw_ram, #dev, dev, config, data) 365 366 #endif 367