regmap.h 9.8 KB

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