internal.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. spinlock_t async_lock;
  68. wait_queue_head_t async_waitq;
  69. struct list_head async_list;
  70. struct list_head async_free;
  71. int async_ret;
  72. bool async;
  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. int (*reg_default_cb)(struct device *dev, unsigned int reg,
  106. unsigned int *val);
  107. unsigned long read_flag_mask;
  108. unsigned long write_flag_mask;
  109. /* number of bits to (left) shift the reg value when formatting*/
  110. int reg_shift;
  111. int reg_stride;
  112. int reg_stride_order;
  113. bool defer_caching;
  114. /* If set, will always write field to HW. */
  115. bool force_write_field;
  116. /* regcache specific members */
  117. const struct regcache_ops *cache_ops;
  118. enum regcache_type cache_type;
  119. /* number of bytes in reg_defaults_raw */
  120. unsigned int cache_size_raw;
  121. /* number of bytes per word in reg_defaults_raw */
  122. unsigned int cache_word_size;
  123. /* number of entries in reg_defaults */
  124. unsigned int num_reg_defaults;
  125. /* number of entries in reg_defaults_raw */
  126. unsigned int num_reg_defaults_raw;
  127. /* if set, only the cache is modified not the HW */
  128. bool cache_only;
  129. /* if set, only the HW is modified not the cache */
  130. bool cache_bypass;
  131. /* if set, remember to free reg_defaults_raw */
  132. bool cache_free;
  133. struct reg_default *reg_defaults;
  134. const void *reg_defaults_raw;
  135. void *cache;
  136. /* if set, the cache contains newer data than the HW */
  137. bool cache_dirty;
  138. /* if set, the HW registers are known to match map->reg_defaults */
  139. bool no_sync_defaults;
  140. struct reg_sequence *patch;
  141. int patch_regs;
  142. /* if set, the regmap core can sleep */
  143. bool can_sleep;
  144. /* if set, converts bulk read to single read */
  145. bool use_single_read;
  146. /* if set, converts bulk write to single write */
  147. bool use_single_write;
  148. /* if set, the device supports multi write mode */
  149. bool can_multi_write;
  150. /* if set, raw reads/writes are limited to this size */
  151. size_t max_raw_read;
  152. size_t max_raw_write;
  153. struct rb_root range_tree;
  154. void *selector_work_buf; /* Scratch buffer used for selector */
  155. struct hwspinlock *hwlock;
  156. };
  157. struct regcache_ops {
  158. const char *name;
  159. enum regcache_type type;
  160. int (*init)(struct regmap *map);
  161. int (*exit)(struct regmap *map);
  162. int (*populate)(struct regmap *map);
  163. #ifdef CONFIG_DEBUG_FS
  164. void (*debugfs_init)(struct regmap *map);
  165. #endif
  166. int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
  167. int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
  168. int (*sync)(struct regmap *map, unsigned int min, unsigned int max);
  169. int (*drop)(struct regmap *map, unsigned int min, unsigned int max);
  170. };
  171. bool regmap_cached(struct regmap *map, unsigned int reg);
  172. bool regmap_writeable(struct regmap *map, unsigned int reg);
  173. bool regmap_readable(struct regmap *map, unsigned int reg);
  174. bool regmap_volatile(struct regmap *map, unsigned int reg);
  175. bool regmap_precious(struct regmap *map, unsigned int reg);
  176. bool regmap_writeable_noinc(struct regmap *map, unsigned int reg);
  177. bool regmap_readable_noinc(struct regmap *map, unsigned int reg);
  178. int _regmap_write(struct regmap *map, unsigned int reg,
  179. unsigned int val);
  180. struct regmap_range_node {
  181. struct rb_node node;
  182. const char *name;
  183. struct regmap *map;
  184. unsigned int range_min;
  185. unsigned int range_max;
  186. unsigned int selector_reg;
  187. unsigned int selector_mask;
  188. int selector_shift;
  189. unsigned int window_start;
  190. unsigned int window_len;
  191. };
  192. struct regmap_field {
  193. struct regmap *regmap;
  194. unsigned int mask;
  195. /* lsb */
  196. unsigned int shift;
  197. unsigned int reg;
  198. unsigned int id_size;
  199. unsigned int id_offset;
  200. };
  201. #ifdef CONFIG_DEBUG_FS
  202. extern void regmap_debugfs_initcall(void);
  203. extern void regmap_debugfs_init(struct regmap *map);
  204. extern void regmap_debugfs_exit(struct regmap *map);
  205. static inline void regmap_debugfs_disable(struct regmap *map)
  206. {
  207. map->debugfs_disable = true;
  208. }
  209. #else
  210. static inline void regmap_debugfs_initcall(void) { }
  211. static inline void regmap_debugfs_init(struct regmap *map) { }
  212. static inline void regmap_debugfs_exit(struct regmap *map) { }
  213. static inline void regmap_debugfs_disable(struct regmap *map) { }
  214. #endif
  215. /* regcache core declarations */
  216. int regcache_init(struct regmap *map, const struct regmap_config *config);
  217. void regcache_exit(struct regmap *map);
  218. int regcache_read(struct regmap *map,
  219. unsigned int reg, unsigned int *value);
  220. int regcache_write(struct regmap *map,
  221. unsigned int reg, unsigned int value);
  222. int regcache_sync(struct regmap *map);
  223. int regcache_sync_block(struct regmap *map, void *block,
  224. unsigned long *cache_present,
  225. unsigned int block_base, unsigned int start,
  226. unsigned int end);
  227. bool regcache_reg_needs_sync(struct regmap *map, unsigned int reg,
  228. unsigned int val);
  229. static inline const void *regcache_get_val_addr(struct regmap *map,
  230. const void *base,
  231. unsigned int idx)
  232. {
  233. return base + (map->cache_word_size * idx);
  234. }
  235. unsigned int regcache_get_val(struct regmap *map, const void *base,
  236. unsigned int idx);
  237. void regcache_set_val(struct regmap *map, void *base, unsigned int idx,
  238. unsigned int val);
  239. int regcache_lookup_reg(struct regmap *map, unsigned int reg);
  240. int regcache_sync_val(struct regmap *map, unsigned int reg, unsigned int val);
  241. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  242. const void *val, size_t val_len, bool noinc);
  243. void regmap_async_complete_cb(struct regmap_async *async, int ret);
  244. enum regmap_endian regmap_get_val_endian(struct device *dev,
  245. const struct regmap_bus *bus,
  246. const struct regmap_config *config);
  247. extern struct regcache_ops regcache_flat_sparse_ops;
  248. extern struct regcache_ops regcache_rbtree_ops;
  249. extern struct regcache_ops regcache_maple_ops;
  250. extern struct regcache_ops regcache_flat_ops;
  251. static inline const char *regmap_name(const struct regmap *map)
  252. {
  253. if (map->dev)
  254. return dev_name(map->dev);
  255. return map->name;
  256. }
  257. static inline unsigned int regmap_get_offset(const struct regmap *map,
  258. unsigned int index)
  259. {
  260. if (map->reg_stride_order >= 0)
  261. return index << map->reg_stride_order;
  262. else
  263. return index * map->reg_stride;
  264. }
  265. static inline unsigned int regcache_get_index_by_order(const struct regmap *map,
  266. unsigned int reg)
  267. {
  268. return reg >> map->reg_stride_order;
  269. }
  270. struct regmap_ram_data {
  271. unsigned int *vals; /* Allocatd by caller */
  272. bool *read;
  273. bool *written;
  274. enum regmap_endian reg_endian;
  275. bool (*noinc_reg)(struct regmap_ram_data *data, unsigned int reg);
  276. };
  277. /*
  278. * Create a test register map with data stored in RAM, not intended
  279. * for practical use.
  280. */
  281. struct regmap *__regmap_init_ram(struct device *dev,
  282. const struct regmap_config *config,
  283. struct regmap_ram_data *data,
  284. struct lock_class_key *lock_key,
  285. const char *lock_name);
  286. #define regmap_init_ram(dev, config, data) \
  287. __regmap_lockdep_wrapper(__regmap_init_ram, #dev, dev, config, data)
  288. struct regmap *__regmap_init_raw_ram(struct device *dev,
  289. const struct regmap_config *config,
  290. struct regmap_ram_data *data,
  291. struct lock_class_key *lock_key,
  292. const char *lock_name);
  293. #define regmap_init_raw_ram(dev, config, data) \
  294. __regmap_lockdep_wrapper(__regmap_init_raw_ram, #dev, dev, config, data)
  295. #endif