regmap.h 12 KB

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