internal.h 9.8 KB

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