internal.h 7.8 KB

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