regmap.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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)(void *context, const void *data,
  90. size_t count);
  91. typedef int (*regmap_hw_gather_write)(void *context,
  92. const void *reg, size_t reg_len,
  93. const void *val, size_t val_len);
  94. typedef int (*regmap_hw_read)(void *context,
  95. const void *reg_buf, size_t reg_size,
  96. void *val_buf, size_t val_size);
  97. typedef void (*regmap_hw_free_context)(void *context);
  98. /**
  99. * Description of a hardware bus for the register map infrastructure.
  100. *
  101. * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
  102. * to perform locking.
  103. * @write: Write operation.
  104. * @gather_write: Write operation with split register/value, return -ENOTSUPP
  105. * if not implemented on a given device.
  106. * @read: Read operation. Data is returned in the buffer used to transmit
  107. * data.
  108. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  109. * a read.
  110. */
  111. struct regmap_bus {
  112. bool fast_io;
  113. regmap_hw_write write;
  114. regmap_hw_gather_write gather_write;
  115. regmap_hw_read read;
  116. regmap_hw_free_context free_context;
  117. u8 read_flag_mask;
  118. };
  119. struct regmap *regmap_init(struct device *dev,
  120. const struct regmap_bus *bus,
  121. void *bus_context,
  122. const struct regmap_config *config);
  123. struct regmap *regmap_init_i2c(struct i2c_client *i2c,
  124. const struct regmap_config *config);
  125. struct regmap *regmap_init_spi(struct spi_device *dev,
  126. const struct regmap_config *config);
  127. struct regmap *regmap_init_mmio(struct device *dev,
  128. void __iomem *regs,
  129. const struct regmap_config *config);
  130. struct regmap *devm_regmap_init(struct device *dev,
  131. const struct regmap_bus *bus,
  132. void *bus_context,
  133. const struct regmap_config *config);
  134. struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
  135. const struct regmap_config *config);
  136. struct regmap *devm_regmap_init_spi(struct spi_device *dev,
  137. const struct regmap_config *config);
  138. struct regmap *devm_regmap_init_mmio(struct device *dev,
  139. void __iomem *regs,
  140. const struct regmap_config *config);
  141. void regmap_exit(struct regmap *map);
  142. int regmap_reinit_cache(struct regmap *map,
  143. const struct regmap_config *config);
  144. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
  145. int regmap_raw_write(struct regmap *map, unsigned int reg,
  146. const void *val, size_t val_len);
  147. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  148. size_t val_count);
  149. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
  150. int regmap_raw_read(struct regmap *map, unsigned int reg,
  151. void *val, size_t val_len);
  152. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  153. size_t val_count);
  154. int regmap_update_bits(struct regmap *map, unsigned int reg,
  155. unsigned int mask, unsigned int val);
  156. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  157. unsigned int mask, unsigned int val,
  158. bool *change);
  159. int regmap_get_val_bytes(struct regmap *map);
  160. int regcache_sync(struct regmap *map);
  161. int regcache_sync_region(struct regmap *map, unsigned int min,
  162. unsigned int max);
  163. void regcache_cache_only(struct regmap *map, bool enable);
  164. void regcache_cache_bypass(struct regmap *map, bool enable);
  165. void regcache_mark_dirty(struct regmap *map);
  166. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  167. int num_regs);
  168. /**
  169. * Description of an IRQ for the generic regmap irq_chip.
  170. *
  171. * @reg_offset: Offset of the status/mask register within the bank
  172. * @mask: Mask used to flag/control the register.
  173. */
  174. struct regmap_irq {
  175. unsigned int reg_offset;
  176. unsigned int mask;
  177. };
  178. /**
  179. * Description of a generic regmap irq_chip. This is not intended to
  180. * handle every possible interrupt controller, but it should handle a
  181. * substantial proportion of those that are found in the wild.
  182. *
  183. * @name: Descriptive name for IRQ controller.
  184. *
  185. * @status_base: Base status register address.
  186. * @mask_base: Base mask register address.
  187. * @ack_base: Base ack address. If zero then the chip is clear on read.
  188. *
  189. * @num_regs: Number of registers in each control bank.
  190. * @irqs: Descriptors for individual IRQs. Interrupt numbers are
  191. * assigned based on the index in the array of the interrupt.
  192. * @num_irqs: Number of descriptors.
  193. */
  194. struct regmap_irq_chip {
  195. const char *name;
  196. unsigned int status_base;
  197. unsigned int mask_base;
  198. unsigned int ack_base;
  199. int num_regs;
  200. const struct regmap_irq *irqs;
  201. int num_irqs;
  202. };
  203. struct regmap_irq_chip_data;
  204. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  205. int irq_base, struct regmap_irq_chip *chip,
  206. struct regmap_irq_chip_data **data);
  207. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
  208. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
  209. #else
  210. /*
  211. * These stubs should only ever be called by generic code which has
  212. * regmap based facilities, if they ever get called at runtime
  213. * something is going wrong and something probably needs to select
  214. * REGMAP.
  215. */
  216. static inline int regmap_write(struct regmap *map, unsigned int reg,
  217. unsigned int val)
  218. {
  219. WARN_ONCE(1, "regmap API is disabled");
  220. return -EINVAL;
  221. }
  222. static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
  223. const void *val, size_t val_len)
  224. {
  225. WARN_ONCE(1, "regmap API is disabled");
  226. return -EINVAL;
  227. }
  228. static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
  229. const void *val, size_t val_count)
  230. {
  231. WARN_ONCE(1, "regmap API is disabled");
  232. return -EINVAL;
  233. }
  234. static inline int regmap_read(struct regmap *map, unsigned int reg,
  235. unsigned int *val)
  236. {
  237. WARN_ONCE(1, "regmap API is disabled");
  238. return -EINVAL;
  239. }
  240. static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
  241. void *val, size_t val_len)
  242. {
  243. WARN_ONCE(1, "regmap API is disabled");
  244. return -EINVAL;
  245. }
  246. static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
  247. void *val, size_t val_count)
  248. {
  249. WARN_ONCE(1, "regmap API is disabled");
  250. return -EINVAL;
  251. }
  252. static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
  253. unsigned int mask, unsigned int val)
  254. {
  255. WARN_ONCE(1, "regmap API is disabled");
  256. return -EINVAL;
  257. }
  258. static inline int regmap_update_bits_check(struct regmap *map,
  259. unsigned int reg,
  260. unsigned int mask, unsigned int val,
  261. bool *change)
  262. {
  263. WARN_ONCE(1, "regmap API is disabled");
  264. return -EINVAL;
  265. }
  266. static inline int regmap_get_val_bytes(struct regmap *map)
  267. {
  268. WARN_ONCE(1, "regmap API is disabled");
  269. return -EINVAL;
  270. }
  271. static inline int regcache_sync(struct regmap *map)
  272. {
  273. WARN_ONCE(1, "regmap API is disabled");
  274. return -EINVAL;
  275. }
  276. static inline int regcache_sync_region(struct regmap *map, unsigned int min,
  277. unsigned int max)
  278. {
  279. WARN_ONCE(1, "regmap API is disabled");
  280. return -EINVAL;
  281. }
  282. static inline void regcache_cache_only(struct regmap *map, bool enable)
  283. {
  284. WARN_ONCE(1, "regmap API is disabled");
  285. }
  286. static inline void regcache_cache_bypass(struct regmap *map, bool enable)
  287. {
  288. WARN_ONCE(1, "regmap API is disabled");
  289. }
  290. static inline void regcache_mark_dirty(struct regmap *map)
  291. {
  292. WARN_ONCE(1, "regmap API is disabled");
  293. }
  294. static inline int regmap_register_patch(struct regmap *map,
  295. const struct reg_default *regs,
  296. int num_regs)
  297. {
  298. WARN_ONCE(1, "regmap API is disabled");
  299. return -EINVAL;
  300. }
  301. #endif
  302. #endif