internal.h 8.0 KB

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