regmap.h 11 KB

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