internal.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Register map access API internal header
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef _REGMAP_INTERNAL_H
  13. #define _REGMAP_INTERNAL_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. struct regmap;
  20. struct regcache_ops;
  21. struct regmap_debugfs_off_cache {
  22. struct list_head list;
  23. off_t min;
  24. off_t max;
  25. unsigned int base_reg;
  26. unsigned int max_reg;
  27. };
  28. struct regmap_format {
  29. size_t buf_size;
  30. size_t reg_bytes;
  31. size_t pad_bytes;
  32. size_t val_bytes;
  33. void (*format_write)(struct regmap *map,
  34. unsigned int reg, unsigned int val);
  35. void (*format_reg)(void *buf, unsigned int reg, unsigned int shift);
  36. void (*format_val)(void *buf, unsigned int val, unsigned int shift);
  37. unsigned int (*parse_val)(const void *buf);
  38. void (*parse_inplace)(void *buf);
  39. };
  40. struct regmap_async {
  41. struct list_head list;
  42. struct regmap *map;
  43. void *work_buf;
  44. };
  45. struct regmap {
  46. union {
  47. struct mutex mutex;
  48. struct {
  49. spinlock_t spinlock;
  50. unsigned long spinlock_flags;
  51. };
  52. };
  53. regmap_lock lock;
  54. regmap_unlock unlock;
  55. void *lock_arg; /* This is passed to lock/unlock functions */
  56. gfp_t alloc_flags;
  57. struct device *dev; /* Device we do I/O on */
  58. void *work_buf; /* Scratch buffer used to format I/O */
  59. struct regmap_format format; /* Buffer format */
  60. const struct regmap_bus *bus;
  61. void *bus_context;
  62. const char *name;
  63. bool async;
  64. spinlock_t async_lock;
  65. wait_queue_head_t async_waitq;
  66. struct list_head async_list;
  67. struct list_head async_free;
  68. int async_ret;
  69. #ifdef CONFIG_DEBUG_FS
  70. bool debugfs_disable;
  71. struct dentry *debugfs;
  72. const char *debugfs_name;
  73. unsigned int debugfs_reg_len;
  74. unsigned int debugfs_val_len;
  75. unsigned int debugfs_tot_len;
  76. struct list_head debugfs_off_cache;
  77. struct mutex cache_lock;
  78. #endif
  79. unsigned int max_register;
  80. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  81. bool (*readable_reg)(struct device *dev, unsigned int reg);
  82. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  83. bool (*precious_reg)(struct device *dev, unsigned int reg);
  84. bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
  85. bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
  86. const struct regmap_access_table *wr_table;
  87. const struct regmap_access_table *rd_table;
  88. const struct regmap_access_table *volatile_table;
  89. const struct regmap_access_table *precious_table;
  90. const struct regmap_access_table *wr_noinc_table;
  91. const struct regmap_access_table *rd_noinc_table;
  92. int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
  93. int (*reg_write)(void *context, unsigned int reg, unsigned int val);
  94. int (*reg_update_bits)(void *context, unsigned int reg,
  95. unsigned int mask, unsigned int val);
  96. bool defer_caching;
  97. unsigned long read_flag_mask;
  98. unsigned long write_flag_mask;
  99. /* number of bits to (left) shift the reg value when formatting*/
  100. int reg_shift;
  101. int reg_stride;
  102. int reg_stride_order;
  103. /* regcache specific members */
  104. const struct regcache_ops *cache_ops;
  105. enum regcache_type cache_type;
  106. /* number of bytes in reg_defaults_raw */
  107. unsigned int cache_size_raw;
  108. /* number of bytes per word in reg_defaults_raw */
  109. unsigned int cache_word_size;
  110. /* number of entries in reg_defaults */
  111. unsigned int num_reg_defaults;
  112. /* number of entries in reg_defaults_raw */
  113. unsigned int num_reg_defaults_raw;
  114. /* if set, only the cache is modified not the HW */
  115. bool cache_only;
  116. /* if set, only the HW is modified not the cache */
  117. bool cache_bypass;
  118. /* if set, remember to free reg_defaults_raw */
  119. bool cache_free;
  120. struct reg_default *reg_defaults;
  121. const void *reg_defaults_raw;
  122. void *cache;
  123. /* if set, the cache contains newer data than the HW */
  124. bool cache_dirty;
  125. /* if set, the HW registers are known to match map->reg_defaults */
  126. bool no_sync_defaults;
  127. struct reg_sequence *patch;
  128. int patch_regs;
  129. /* if set, converts bulk read to single read */
  130. bool use_single_read;
  131. /* if set, converts bulk write to single write */
  132. bool use_single_write;
  133. /* if set, the device supports multi write mode */
  134. bool can_multi_write;
  135. /* if set, raw reads/writes are limited to this size */
  136. size_t max_raw_read;
  137. size_t max_raw_write;
  138. struct rb_root range_tree;
  139. void *selector_work_buf; /* Scratch buffer used for selector */
  140. struct hwspinlock *hwlock;
  141. };
  142. struct regcache_ops {
  143. const char *name;
  144. enum regcache_type type;
  145. int (*init)(struct regmap *map);
  146. int (*exit)(struct regmap *map);
  147. #ifdef CONFIG_DEBUG_FS
  148. void (*debugfs_init)(struct regmap *map);
  149. #endif
  150. int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
  151. int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
  152. int (*sync)(struct regmap *map, unsigned int min, unsigned int max);
  153. int (*drop)(struct regmap *map, unsigned int min, unsigned int max);
  154. };
  155. bool regmap_cached(struct regmap *map, unsigned int reg);
  156. bool regmap_writeable(struct regmap *map, unsigned int reg);
  157. bool regmap_readable(struct regmap *map, unsigned int reg);
  158. bool regmap_volatile(struct regmap *map, unsigned int reg);
  159. bool regmap_precious(struct regmap *map, unsigned int reg);
  160. bool regmap_writeable_noinc(struct regmap *map, unsigned int reg);
  161. bool regmap_readable_noinc(struct regmap *map, unsigned int reg);
  162. int _regmap_write(struct regmap *map, unsigned int reg,
  163. unsigned int val);
  164. struct regmap_range_node {
  165. struct rb_node node;
  166. const char *name;
  167. struct regmap *map;
  168. unsigned int range_min;
  169. unsigned int range_max;
  170. unsigned int selector_reg;
  171. unsigned int selector_mask;
  172. int selector_shift;
  173. unsigned int window_start;
  174. unsigned int window_len;
  175. };
  176. struct regmap_field {
  177. struct regmap *regmap;
  178. unsigned int mask;
  179. /* lsb */
  180. unsigned int shift;
  181. unsigned int reg;
  182. unsigned int id_size;
  183. unsigned int id_offset;
  184. };
  185. #ifdef CONFIG_DEBUG_FS
  186. extern void regmap_debugfs_initcall(void);
  187. extern void regmap_debugfs_init(struct regmap *map, const char *name);
  188. extern void regmap_debugfs_exit(struct regmap *map);
  189. static inline void regmap_debugfs_disable(struct regmap *map)
  190. {
  191. map->debugfs_disable = true;
  192. }
  193. #else
  194. static inline void regmap_debugfs_initcall(void) { }
  195. static inline void regmap_debugfs_init(struct regmap *map, const char *name) { }
  196. static inline void regmap_debugfs_exit(struct regmap *map) { }
  197. static inline void regmap_debugfs_disable(struct regmap *map) { }
  198. #endif
  199. /* regcache core declarations */
  200. int regcache_init(struct regmap *map, const struct regmap_config *config);
  201. void regcache_exit(struct regmap *map);
  202. int regcache_read(struct regmap *map,
  203. unsigned int reg, unsigned int *value);
  204. int regcache_write(struct regmap *map,
  205. unsigned int reg, unsigned int value);
  206. int regcache_sync(struct regmap *map);
  207. int regcache_sync_block(struct regmap *map, void *block,
  208. unsigned long *cache_present,
  209. unsigned int block_base, unsigned int start,
  210. unsigned int end);
  211. static inline const void *regcache_get_val_addr(struct regmap *map,
  212. const void *base,
  213. unsigned int idx)
  214. {
  215. return base + (map->cache_word_size * idx);
  216. }
  217. unsigned int regcache_get_val(struct regmap *map, const void *base,
  218. unsigned int idx);
  219. bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
  220. unsigned int val);
  221. int regcache_lookup_reg(struct regmap *map, unsigned int reg);
  222. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  223. const void *val, size_t val_len);
  224. void regmap_async_complete_cb(struct regmap_async *async, int ret);
  225. enum regmap_endian regmap_get_val_endian(struct device *dev,
  226. const struct regmap_bus *bus,
  227. const struct regmap_config *config);
  228. extern struct regcache_ops regcache_rbtree_ops;
  229. extern struct regcache_ops regcache_lzo_ops;
  230. extern struct regcache_ops regcache_flat_ops;
  231. static inline const char *regmap_name(const struct regmap *map)
  232. {
  233. if (map->dev)
  234. return dev_name(map->dev);
  235. return map->name;
  236. }
  237. static inline unsigned int regmap_get_offset(const struct regmap *map,
  238. unsigned int index)
  239. {
  240. if (map->reg_stride_order >= 0)
  241. return index << map->reg_stride_order;
  242. else
  243. return index * map->reg_stride;
  244. }
  245. static inline unsigned int regcache_get_index_by_order(const struct regmap *map,
  246. unsigned int reg)
  247. {
  248. return reg >> map->reg_stride_order;
  249. }
  250. #endif