regmap.h 12 KB

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