regmap.h 11 KB

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