internal.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <[email protected]>
  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 reg_downshift;
  30. size_t val_bytes;
  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. regmap_lock lock;
  56. regmap_unlock unlock;
  57. void *lock_arg; /* This is passed to lock/unlock functions */
  58. gfp_t alloc_flags;
  59. unsigned int reg_base;
  60. struct device *dev; /* Device we do I/O on */
  61. void *work_buf; /* Scratch buffer used to format I/O */
  62. struct regmap_format format; /* Buffer format */
  63. const struct regmap_bus *bus;
  64. void *bus_context;
  65. const char *name;
  66. bool async;
  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. #ifdef CONFIG_DEBUG_FS
  73. bool debugfs_disable;
  74. struct dentry *debugfs;
  75. const char *debugfs_name;
  76. unsigned int debugfs_reg_len;
  77. unsigned int debugfs_val_len;
  78. unsigned int debugfs_tot_len;
  79. struct list_head debugfs_off_cache;
  80. struct mutex cache_lock;
  81. #endif
  82. unsigned int max_register;
  83. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  84. bool (*readable_reg)(struct device *dev, unsigned int reg);
  85. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  86. bool (*precious_reg)(struct device *dev, unsigned int reg);
  87. bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
  88. bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
  89. const struct regmap_access_table *wr_table;
  90. const struct regmap_access_table *rd_table;
  91. const struct regmap_access_table *volatile_table;
  92. const struct regmap_access_table *precious_table;
  93. const struct regmap_access_table *wr_noinc_table;
  94. const struct regmap_access_table *rd_noinc_table;
  95. int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
  96. int (*reg_write)(void *context, unsigned int reg, unsigned int val);
  97. int (*reg_update_bits)(void *context, unsigned int reg,
  98. unsigned int mask, unsigned int val);
  99. /* Bulk read/write */
  100. int (*read)(void *context, const void *reg_buf, size_t reg_size,
  101. void *val_buf, size_t val_size);
  102. int (*write)(void *context, const void *data, size_t count);
  103. bool defer_caching;
  104. unsigned long read_flag_mask;
  105. unsigned long write_flag_mask;
  106. /* number of bits to (left) shift the reg value when formatting*/
  107. int reg_shift;
  108. int reg_stride;
  109. int reg_stride_order;
  110. /* regcache specific members */
  111. const struct regcache_ops *cache_ops;
  112. enum regcache_type cache_type;
  113. /* number of bytes in reg_defaults_raw */
  114. unsigned int cache_size_raw;
  115. /* number of bytes per word in reg_defaults_raw */
  116. unsigned int cache_word_size;
  117. /* number of entries in reg_defaults */
  118. unsigned int num_reg_defaults;
  119. /* number of entries in reg_defaults_raw */
  120. unsigned int num_reg_defaults_raw;
  121. /* if set, only the cache is modified not the HW */
  122. bool cache_only;
  123. /* if set, only the HW is modified not the cache */
  124. bool cache_bypass;
  125. /* if set, remember to free reg_defaults_raw */
  126. bool cache_free;
  127. struct reg_default *reg_defaults;
  128. const void *reg_defaults_raw;
  129. void *cache;
  130. /* if set, the cache contains newer data than the HW */
  131. bool cache_dirty;
  132. /* if set, the HW registers are known to match map->reg_defaults */
  133. bool no_sync_defaults;
  134. struct reg_sequence *patch;
  135. int patch_regs;
  136. /* if set, converts bulk read to single read */
  137. bool use_single_read;
  138. /* if set, converts bulk write to single write */
  139. bool use_single_write;
  140. /* if set, the device supports multi write mode */
  141. bool can_multi_write;
  142. /* if set, raw reads/writes are limited to this size */
  143. size_t max_raw_read;
  144. size_t max_raw_write;
  145. struct rb_root range_tree;
  146. void *selector_work_buf; /* Scratch buffer used for selector */
  147. struct hwspinlock *hwlock;
  148. /* if set, the regmap core can sleep */
  149. bool can_sleep;
  150. };
  151. struct regcache_ops {
  152. const char *name;
  153. enum regcache_type type;
  154. int (*init)(struct regmap *map);
  155. int (*exit)(struct regmap *map);
  156. #ifdef CONFIG_DEBUG_FS
  157. void (*debugfs_init)(struct regmap *map);
  158. #endif
  159. int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
  160. int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
  161. int (*sync)(struct regmap *map, unsigned int min, unsigned int max);
  162. int (*drop)(struct regmap *map, unsigned int min, unsigned int max);
  163. };
  164. bool regmap_cached(struct regmap *map, unsigned int reg);
  165. bool regmap_writeable(struct regmap *map, unsigned int reg);
  166. bool regmap_readable(struct regmap *map, unsigned int reg);
  167. bool regmap_volatile(struct regmap *map, unsigned int reg);
  168. bool regmap_precious(struct regmap *map, unsigned int reg);
  169. bool regmap_writeable_noinc(struct regmap *map, unsigned int reg);
  170. bool regmap_readable_noinc(struct regmap *map, unsigned int reg);
  171. int _regmap_write(struct regmap *map, unsigned int reg,
  172. unsigned int val);
  173. struct regmap_range_node {
  174. struct rb_node node;
  175. const char *name;
  176. struct regmap *map;
  177. unsigned int range_min;
  178. unsigned int range_max;
  179. unsigned int selector_reg;
  180. unsigned int selector_mask;
  181. int selector_shift;
  182. unsigned int window_start;
  183. unsigned int window_len;
  184. };
  185. struct regmap_field {
  186. struct regmap *regmap;
  187. unsigned int mask;
  188. /* lsb */
  189. unsigned int shift;
  190. unsigned int reg;
  191. unsigned int id_size;
  192. unsigned int id_offset;
  193. };
  194. #ifdef CONFIG_DEBUG_FS
  195. extern void regmap_debugfs_initcall(void);
  196. extern void regmap_debugfs_init(struct regmap *map);
  197. extern void regmap_debugfs_exit(struct regmap *map);
  198. static inline void regmap_debugfs_disable(struct regmap *map)
  199. {
  200. map->debugfs_disable = true;
  201. }
  202. #else
  203. static inline void regmap_debugfs_initcall(void) { }
  204. static inline void regmap_debugfs_init(struct regmap *map) { }
  205. static inline void regmap_debugfs_exit(struct regmap *map) { }
  206. static inline void regmap_debugfs_disable(struct regmap *map) { }
  207. #endif
  208. /* regcache core declarations */
  209. int regcache_init(struct regmap *map, const struct regmap_config *config);
  210. void regcache_exit(struct regmap *map);
  211. int regcache_read(struct regmap *map,
  212. unsigned int reg, unsigned int *value);
  213. int regcache_write(struct regmap *map,
  214. unsigned int reg, unsigned int value);
  215. int regcache_sync(struct regmap *map);
  216. int regcache_sync_block(struct regmap *map, void *block,
  217. unsigned long *cache_present,
  218. unsigned int block_base, unsigned int start,
  219. unsigned int end);
  220. static inline const void *regcache_get_val_addr(struct regmap *map,
  221. const void *base,
  222. unsigned int idx)
  223. {
  224. return base + (map->cache_word_size * idx);
  225. }
  226. unsigned int regcache_get_val(struct regmap *map, const void *base,
  227. unsigned int idx);
  228. bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
  229. unsigned int val);
  230. int regcache_lookup_reg(struct regmap *map, unsigned int reg);
  231. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  232. const void *val, size_t val_len, bool noinc);
  233. void regmap_async_complete_cb(struct regmap_async *async, int ret);
  234. enum regmap_endian regmap_get_val_endian(struct device *dev,
  235. const struct regmap_bus *bus,
  236. const struct regmap_config *config);
  237. extern struct regcache_ops regcache_rbtree_ops;
  238. extern struct regcache_ops regcache_lzo_ops;
  239. extern struct regcache_ops regcache_flat_ops;
  240. static inline const char *regmap_name(const struct regmap *map)
  241. {
  242. if (map->dev)
  243. return dev_name(map->dev);
  244. return map->name;
  245. }
  246. static inline unsigned int regmap_get_offset(const struct regmap *map,
  247. unsigned int index)
  248. {
  249. if (map->reg_stride_order >= 0)
  250. return index << map->reg_stride_order;
  251. else
  252. return index * map->reg_stride;
  253. }
  254. static inline unsigned int regcache_get_index_by_order(const struct regmap *map,
  255. unsigned int reg)
  256. {
  257. return reg >> map->reg_stride_order;
  258. }
  259. #endif