regmap.h 10.0 KB

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