regmap.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #ifndef __LINUX_REGMAP_H
  2. #define __LINUX_REGMAP_H
  3. /*
  4. * Register map access API
  5. *
  6. * Copyright 2011 Wolfson Microelectronics plc
  7. *
  8. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/list.h>
  15. struct module;
  16. struct device;
  17. struct i2c_client;
  18. struct spi_device;
  19. struct regmap;
  20. /* An enum of all the supported cache types */
  21. enum regcache_type {
  22. REGCACHE_NONE,
  23. REGCACHE_RBTREE,
  24. REGCACHE_COMPRESSED
  25. };
  26. /**
  27. * Default value for a register. We use an array of structs rather
  28. * than a simple array as many modern devices have very sparse
  29. * register maps.
  30. *
  31. * @reg: Register address.
  32. * @def: Register default value.
  33. */
  34. struct reg_default {
  35. unsigned int reg;
  36. unsigned int def;
  37. };
  38. #ifdef CONFIG_REGMAP
  39. /**
  40. * Configuration for the register map of a device.
  41. *
  42. * @reg_bits: Number of bits in a register address, mandatory.
  43. * @pad_bits: Number of bits of padding between register and value.
  44. * @val_bits: Number of bits in a register value, mandatory.
  45. *
  46. * @writeable_reg: Optional callback returning true if the register
  47. * can be written to.
  48. * @readable_reg: Optional callback returning true if the register
  49. * can be read from.
  50. * @volatile_reg: Optional callback returning true if the register
  51. * value can't be cached.
  52. * @precious_reg: Optional callback returning true if the rgister
  53. * should not be read outside of a call from the driver
  54. * (eg, a clear on read interrupt status register).
  55. *
  56. * @max_register: Optional, specifies the maximum valid register index.
  57. * @reg_defaults: Power on reset values for registers (for use with
  58. * register cache support).
  59. * @num_reg_defaults: Number of elements in reg_defaults.
  60. *
  61. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  62. * a read.
  63. * @write_flag_mask: Mask to be set in the top byte of the register when doing
  64. * a write. If both read_flag_mask and write_flag_mask are
  65. * empty the regmap_bus default masks are used.
  66. *
  67. * @cache_type: The actual cache type.
  68. * @reg_defaults_raw: Power on reset values for registers (for use with
  69. * register cache support).
  70. * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
  71. */
  72. struct regmap_config {
  73. int reg_bits;
  74. int pad_bits;
  75. int val_bits;
  76. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  77. bool (*readable_reg)(struct device *dev, unsigned int reg);
  78. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  79. bool (*precious_reg)(struct device *dev, unsigned int reg);
  80. unsigned int max_register;
  81. const struct reg_default *reg_defaults;
  82. unsigned int num_reg_defaults;
  83. enum regcache_type cache_type;
  84. const void *reg_defaults_raw;
  85. unsigned int num_reg_defaults_raw;
  86. u8 read_flag_mask;
  87. u8 write_flag_mask;
  88. };
  89. typedef int (*regmap_hw_write)(struct device *dev, const void *data,
  90. size_t count);
  91. typedef int (*regmap_hw_gather_write)(struct device *dev,
  92. const void *reg, size_t reg_len,
  93. const void *val, size_t val_len);
  94. typedef int (*regmap_hw_read)(struct device *dev,
  95. const void *reg_buf, size_t reg_size,
  96. void *val_buf, size_t val_size);
  97. /**
  98. * Description of a hardware bus for the register map infrastructure.
  99. *
  100. * @write: Write operation.
  101. * @gather_write: Write operation with split register/value, return -ENOTSUPP
  102. * if not implemented on a given device.
  103. * @read: Read operation. Data is returned in the buffer used to transmit
  104. * data.
  105. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  106. * a read.
  107. */
  108. struct regmap_bus {
  109. regmap_hw_write write;
  110. regmap_hw_gather_write gather_write;
  111. regmap_hw_read read;
  112. u8 read_flag_mask;
  113. };
  114. struct regmap *regmap_init(struct device *dev,
  115. const struct regmap_bus *bus,
  116. const struct regmap_config *config);
  117. struct regmap *regmap_init_i2c(struct i2c_client *i2c,
  118. const struct regmap_config *config);
  119. struct regmap *regmap_init_spi(struct spi_device *dev,
  120. const struct regmap_config *config);
  121. struct regmap *devm_regmap_init(struct device *dev,
  122. const struct regmap_bus *bus,
  123. const struct regmap_config *config);
  124. struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
  125. const struct regmap_config *config);
  126. struct regmap *devm_regmap_init_spi(struct spi_device *dev,
  127. const struct regmap_config *config);
  128. void regmap_exit(struct regmap *map);
  129. int regmap_reinit_cache(struct regmap *map,
  130. const struct regmap_config *config);
  131. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
  132. int regmap_raw_write(struct regmap *map, unsigned int reg,
  133. const void *val, size_t val_len);
  134. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  135. size_t val_count);
  136. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
  137. int regmap_raw_read(struct regmap *map, unsigned int reg,
  138. void *val, size_t val_len);
  139. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  140. size_t val_count);
  141. int regmap_update_bits(struct regmap *map, unsigned int reg,
  142. unsigned int mask, unsigned int val);
  143. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  144. unsigned int mask, unsigned int val,
  145. bool *change);
  146. int regmap_get_val_bytes(struct regmap *map);
  147. int regcache_sync(struct regmap *map);
  148. int regcache_sync_region(struct regmap *map, unsigned int min,
  149. unsigned int max);
  150. void regcache_cache_only(struct regmap *map, bool enable);
  151. void regcache_cache_bypass(struct regmap *map, bool enable);
  152. void regcache_mark_dirty(struct regmap *map);
  153. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  154. int num_regs);
  155. /**
  156. * Description of an IRQ for the generic regmap irq_chip.
  157. *
  158. * @reg_offset: Offset of the status/mask register within the bank
  159. * @mask: Mask used to flag/control the register.
  160. */
  161. struct regmap_irq {
  162. unsigned int reg_offset;
  163. unsigned int mask;
  164. };
  165. /**
  166. * Description of a generic regmap irq_chip. This is not intended to
  167. * handle every possible interrupt controller, but it should handle a
  168. * substantial proportion of those that are found in the wild.
  169. *
  170. * @name: Descriptive name for IRQ controller.
  171. *
  172. * @status_base: Base status register address.
  173. * @mask_base: Base mask register address.
  174. * @ack_base: Base ack address. If zero then the chip is clear on read.
  175. *
  176. * @num_regs: Number of registers in each control bank.
  177. * @irqs: Descriptors for individual IRQs. Interrupt numbers are
  178. * assigned based on the index in the array of the interrupt.
  179. * @num_irqs: Number of descriptors.
  180. */
  181. struct regmap_irq_chip {
  182. const char *name;
  183. unsigned int status_base;
  184. unsigned int mask_base;
  185. unsigned int ack_base;
  186. int num_regs;
  187. const struct regmap_irq *irqs;
  188. int num_irqs;
  189. };
  190. struct regmap_irq_chip_data;
  191. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  192. int irq_base, struct regmap_irq_chip *chip,
  193. struct regmap_irq_chip_data **data);
  194. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
  195. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
  196. #else
  197. /*
  198. * These stubs should only ever be called by generic code which has
  199. * regmap based facilities, if they ever get called at runtime
  200. * something is going wrong and something probably needs to select
  201. * REGMAP.
  202. */
  203. static inline int regmap_write(struct regmap *map, unsigned int reg,
  204. unsigned int val)
  205. {
  206. WARN_ONCE(1, "regmap API is disabled");
  207. return -EINVAL;
  208. }
  209. static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
  210. const void *val, size_t val_len)
  211. {
  212. WARN_ONCE(1, "regmap API is disabled");
  213. return -EINVAL;
  214. }
  215. static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
  216. const void *val, size_t val_count)
  217. {
  218. WARN_ONCE(1, "regmap API is disabled");
  219. return -EINVAL;
  220. }
  221. static inline int regmap_read(struct regmap *map, unsigned int reg,
  222. unsigned int *val)
  223. {
  224. WARN_ONCE(1, "regmap API is disabled");
  225. return -EINVAL;
  226. }
  227. static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
  228. void *val, size_t val_len)
  229. {
  230. WARN_ONCE(1, "regmap API is disabled");
  231. return -EINVAL;
  232. }
  233. static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
  234. void *val, size_t val_count)
  235. {
  236. WARN_ONCE(1, "regmap API is disabled");
  237. return -EINVAL;
  238. }
  239. static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
  240. unsigned int mask, unsigned int val)
  241. {
  242. WARN_ONCE(1, "regmap API is disabled");
  243. return -EINVAL;
  244. }
  245. static inline int regmap_update_bits_check(struct regmap *map,
  246. unsigned int reg,
  247. unsigned int mask, unsigned int val,
  248. bool *change)
  249. {
  250. WARN_ONCE(1, "regmap API is disabled");
  251. return -EINVAL;
  252. }
  253. static inline int regmap_get_val_bytes(struct regmap *map)
  254. {
  255. WARN_ONCE(1, "regmap API is disabled");
  256. return -EINVAL;
  257. }
  258. static inline int regcache_sync(struct regmap *map)
  259. {
  260. WARN_ONCE(1, "regmap API is disabled");
  261. return -EINVAL;
  262. }
  263. static inline int regcache_sync_region(struct regmap *map, unsigned int min,
  264. unsigned int max)
  265. {
  266. WARN_ONCE(1, "regmap API is disabled");
  267. return -EINVAL;
  268. }
  269. static inline void regcache_cache_only(struct regmap *map, bool enable)
  270. {
  271. WARN_ONCE(1, "regmap API is disabled");
  272. }
  273. static inline void regcache_cache_bypass(struct regmap *map, bool enable)
  274. {
  275. WARN_ONCE(1, "regmap API is disabled");
  276. }
  277. static inline void regcache_mark_dirty(struct regmap *map)
  278. {
  279. WARN_ONCE(1, "regmap API is disabled");
  280. }
  281. static inline int regmap_register_patch(struct regmap *map,
  282. const struct reg_default *regs,
  283. int num_regs)
  284. {
  285. WARN_ONCE(1, "regmap API is disabled");
  286. return -EINVAL;
  287. }
  288. #endif
  289. #endif