regmap.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/device.h>
  15. #include <linux/list.h>
  16. struct module;
  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_read(struct regmap *map, unsigned int reg, unsigned int *val);
  135. int regmap_raw_read(struct regmap *map, unsigned int reg,
  136. void *val, size_t val_len);
  137. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  138. size_t val_count);
  139. int regmap_update_bits(struct regmap *map, unsigned int reg,
  140. unsigned int mask, unsigned int val);
  141. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  142. unsigned int mask, unsigned int val,
  143. bool *change);
  144. int regcache_sync(struct regmap *map);
  145. int regcache_sync_region(struct regmap *map, unsigned int min,
  146. unsigned int max);
  147. void regcache_cache_only(struct regmap *map, bool enable);
  148. void regcache_cache_bypass(struct regmap *map, bool enable);
  149. void regcache_mark_dirty(struct regmap *map);
  150. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  151. int num_regs);
  152. /**
  153. * Description of an IRQ for the generic regmap irq_chip.
  154. *
  155. * @reg_offset: Offset of the status/mask register within the bank
  156. * @mask: Mask used to flag/control the register.
  157. */
  158. struct regmap_irq {
  159. unsigned int reg_offset;
  160. unsigned int mask;
  161. };
  162. /**
  163. * Description of a generic regmap irq_chip. This is not intended to
  164. * handle every possible interrupt controller, but it should handle a
  165. * substantial proportion of those that are found in the wild.
  166. *
  167. * @name: Descriptive name for IRQ controller.
  168. *
  169. * @status_base: Base status register address.
  170. * @mask_base: Base mask register address.
  171. * @ack_base: Base ack address. If zero then the chip is clear on read.
  172. *
  173. * @num_regs: Number of registers in each control bank.
  174. * @irqs: Descriptors for individual IRQs. Interrupt numbers are
  175. * assigned based on the index in the array of the interrupt.
  176. * @num_irqs: Number of descriptors.
  177. */
  178. struct regmap_irq_chip {
  179. const char *name;
  180. unsigned int status_base;
  181. unsigned int mask_base;
  182. unsigned int ack_base;
  183. int num_regs;
  184. const struct regmap_irq *irqs;
  185. int num_irqs;
  186. };
  187. struct regmap_irq_chip_data;
  188. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  189. int irq_base, struct regmap_irq_chip *chip,
  190. struct regmap_irq_chip_data **data);
  191. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
  192. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
  193. #else
  194. /*
  195. * These stubs should only ever be called by generic code which has
  196. * regmap based facilities, if they ever get called at runtime
  197. * something is going wrong and something probably needs to select
  198. * REGMAP.
  199. */
  200. static inline int regmap_write(struct regmap *map, unsigned int reg,
  201. unsigned int val)
  202. {
  203. WARN_ONCE(1, "regmap API is disabled");
  204. return -EINVAL;
  205. }
  206. static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
  207. const void *val, size_t val_len)
  208. {
  209. WARN_ONCE(1, "regmap API is disabled");
  210. return -EINVAL;
  211. }
  212. static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
  213. const void *val, size_t val_count)
  214. {
  215. WARN_ONCE(1, "regmap API is disabled");
  216. return -EINVAL;
  217. }
  218. static inline int regmap_read(struct regmap *map, unsigned int reg,
  219. unsigned int *val)
  220. {
  221. WARN_ONCE(1, "regmap API is disabled");
  222. return -EINVAL;
  223. }
  224. static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
  225. void *val, size_t val_len)
  226. {
  227. WARN_ONCE(1, "regmap API is disabled");
  228. return -EINVAL;
  229. }
  230. static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
  231. void *val, size_t val_count)
  232. {
  233. WARN_ONCE(1, "regmap API is disabled");
  234. return -EINVAL;
  235. }
  236. static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
  237. unsigned int mask, unsigned int val)
  238. {
  239. WARN_ONCE(1, "regmap API is disabled");
  240. return -EINVAL;
  241. }
  242. static inline int regmap_update_bits_check(struct regmap *map,
  243. unsigned int reg,
  244. unsigned int mask, unsigned int val,
  245. bool *change)
  246. {
  247. WARN_ONCE(1, "regmap API is disabled");
  248. return -EINVAL;
  249. }
  250. static inline int regmap_get_val_bytes(struct regmap *map)
  251. {
  252. WARN_ONCE(1, "regmap API is disabled");
  253. return -EINVAL;
  254. }
  255. static inline int regcache_sync(struct regmap *map)
  256. {
  257. WARN_ONCE(1, "regmap API is disabled");
  258. return -EINVAL;
  259. }
  260. static inline int regcache_sync_region(struct regmap *map, unsigned int min,
  261. unsigned int max)
  262. {
  263. WARN_ONCE(1, "regmap API is disabled");
  264. return -EINVAL;
  265. }
  266. static inline void regcache_cache_only(struct regmap *map, bool enable)
  267. {
  268. WARN_ONCE(1, "regmap API is disabled");
  269. }
  270. static inline void regcache_cache_bypass(struct regmap *map, bool enable)
  271. {
  272. WARN_ONCE(1, "regmap API is disabled");
  273. }
  274. static inline void regcache_mark_dirty(struct regmap *map)
  275. {
  276. WARN_ONCE(1, "regmap API is disabled");
  277. }
  278. static inline int regmap_register_patch(struct regmap *map,
  279. const struct reg_default *regs,
  280. int num_regs)
  281. {
  282. WARN_ONCE(1, "regmap API is disabled");
  283. return -EINVAL;
  284. }
  285. #endif
  286. #endif