internal.h 8.2 KB

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