regmap.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. enum regmap_endian {
  42. /* Unspecified -> 0 -> Backwards compatible default */
  43. REGMAP_ENDIAN_DEFAULT = 0,
  44. REGMAP_ENDIAN_BIG,
  45. REGMAP_ENDIAN_LITTLE,
  46. REGMAP_ENDIAN_NATIVE,
  47. };
  48. /**
  49. * A register range, used for access related checks
  50. * (readable/writeable/volatile/precious checks)
  51. *
  52. * @range_min: address of first register
  53. * @range_max: address of last register
  54. */
  55. struct regmap_range {
  56. unsigned int range_min;
  57. unsigned int range_max;
  58. };
  59. /*
  60. * A table of ranges including some yes ranges and some no ranges.
  61. * If a register belongs to a no_range, the corresponding check function
  62. * will return false. If a register belongs to a yes range, the corresponding
  63. * check function will return true. "no_ranges" are searched first.
  64. *
  65. * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
  66. * @n_yes_ranges: size of the above array
  67. * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
  68. * @n_no_ranges: size of the above array
  69. */
  70. struct regmap_access_table {
  71. const struct regmap_range *yes_ranges;
  72. unsigned int n_yes_ranges;
  73. const struct regmap_range *no_ranges;
  74. unsigned int n_no_ranges;
  75. };
  76. typedef void (*regmap_lock)(void *);
  77. typedef void (*regmap_unlock)(void *);
  78. /**
  79. * Configuration for the register map of a device.
  80. *
  81. * @name: Optional name of the regmap. Useful when a device has multiple
  82. * register regions.
  83. *
  84. * @reg_bits: Number of bits in a register address, mandatory.
  85. * @reg_stride: The register address stride. Valid register addresses are a
  86. * multiple of this value. If set to 0, a value of 1 will be
  87. * used.
  88. * @pad_bits: Number of bits of padding between register and value.
  89. * @val_bits: Number of bits in a register value, mandatory.
  90. *
  91. * @writeable_reg: Optional callback returning true if the register
  92. * can be written to. If this field is NULL but wr_table
  93. * (see below) is not, the check is performed on such table
  94. * (a register is writeable if it belongs to one of the ranges
  95. * specified by wr_table).
  96. * @readable_reg: Optional callback returning true if the register
  97. * can be read from. If this field is NULL but rd_table
  98. * (see below) is not, the check is performed on such table
  99. * (a register is readable if it belongs to one of the ranges
  100. * specified by rd_table).
  101. * @volatile_reg: Optional callback returning true if the register
  102. * value can't be cached. If this field is NULL but
  103. * volatile_table (see below) is not, the check is performed on
  104. * such table (a register is volatile if it belongs to one of
  105. * the ranges specified by volatile_table).
  106. * @precious_reg: Optional callback returning true if the rgister
  107. * should not be read outside of a call from the driver
  108. * (eg, a clear on read interrupt status register). If this
  109. * field is NULL but precious_table (see below) is not, the
  110. * check is performed on such table (a register is precious if
  111. * it belongs to one of the ranges specified by precious_table).
  112. * @lock: Optional lock callback (overrides regmap's default lock
  113. * function, based on spinlock or mutex).
  114. * @unlock: As above for unlocking.
  115. * @lock_arg: this field is passed as the only argument of lock/unlock
  116. * functions (ignored in case regular lock/unlock functions
  117. * are not overridden).
  118. *
  119. * @max_register: Optional, specifies the maximum valid register index.
  120. * @wr_table: Optional, points to a struct regmap_access_table specifying
  121. * valid ranges for write access.
  122. * @rd_table: As above, for read access.
  123. * @volatile_table: As above, for volatile registers.
  124. * @precious_table: As above, for precious registers.
  125. * @reg_defaults: Power on reset values for registers (for use with
  126. * register cache support).
  127. * @num_reg_defaults: Number of elements in reg_defaults.
  128. *
  129. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  130. * a read.
  131. * @write_flag_mask: Mask to be set in the top byte of the register when doing
  132. * a write. If both read_flag_mask and write_flag_mask are
  133. * empty the regmap_bus default masks are used.
  134. * @use_single_rw: If set, converts the bulk read and write operations into
  135. * a series of single read and write operations. This is useful
  136. * for device that does not support bulk read and write.
  137. *
  138. * @cache_type: The actual cache type.
  139. * @reg_defaults_raw: Power on reset values for registers (for use with
  140. * register cache support).
  141. * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
  142. * @reg_format_endian: Endianness for formatted register addresses. If this is
  143. * DEFAULT, the @reg_format_endian_default value from the
  144. * regmap bus is used.
  145. * @val_format_endian: Endianness for formatted register values. If this is
  146. * DEFAULT, the @reg_format_endian_default value from the
  147. * regmap bus is used.
  148. *
  149. * @ranges: Array of configuration entries for virtual address ranges.
  150. * @num_ranges: Number of range configuration entries.
  151. */
  152. struct regmap_config {
  153. const char *name;
  154. int reg_bits;
  155. int reg_stride;
  156. int pad_bits;
  157. int val_bits;
  158. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  159. bool (*readable_reg)(struct device *dev, unsigned int reg);
  160. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  161. bool (*precious_reg)(struct device *dev, unsigned int reg);
  162. regmap_lock lock;
  163. regmap_unlock unlock;
  164. void *lock_arg;
  165. unsigned int max_register;
  166. const struct regmap_access_table *wr_table;
  167. const struct regmap_access_table *rd_table;
  168. const struct regmap_access_table *volatile_table;
  169. const struct regmap_access_table *precious_table;
  170. const struct reg_default *reg_defaults;
  171. unsigned int num_reg_defaults;
  172. enum regcache_type cache_type;
  173. const void *reg_defaults_raw;
  174. unsigned int num_reg_defaults_raw;
  175. u8 read_flag_mask;
  176. u8 write_flag_mask;
  177. bool use_single_rw;
  178. enum regmap_endian reg_format_endian;
  179. enum regmap_endian val_format_endian;
  180. const struct regmap_range_cfg *ranges;
  181. unsigned int num_ranges;
  182. };
  183. /**
  184. * Configuration for indirectly accessed or paged registers.
  185. * Registers, mapped to this virtual range, are accessed in two steps:
  186. * 1. page selector register update;
  187. * 2. access through data window registers.
  188. *
  189. * @name: Descriptive name for diagnostics
  190. *
  191. * @range_min: Address of the lowest register address in virtual range.
  192. * @range_max: Address of the highest register in virtual range.
  193. *
  194. * @page_sel_reg: Register with selector field.
  195. * @page_sel_mask: Bit shift for selector value.
  196. * @page_sel_shift: Bit mask for selector value.
  197. *
  198. * @window_start: Address of first (lowest) register in data window.
  199. * @window_len: Number of registers in data window.
  200. */
  201. struct regmap_range_cfg {
  202. const char *name;
  203. /* Registers of virtual address range */
  204. unsigned int range_min;
  205. unsigned int range_max;
  206. /* Page selector for indirect addressing */
  207. unsigned int selector_reg;
  208. unsigned int selector_mask;
  209. int selector_shift;
  210. /* Data window (per each page) */
  211. unsigned int window_start;
  212. unsigned int window_len;
  213. };
  214. typedef int (*regmap_hw_write)(void *context, const void *data,
  215. size_t count);
  216. typedef int (*regmap_hw_gather_write)(void *context,
  217. const void *reg, size_t reg_len,
  218. const void *val, size_t val_len);
  219. typedef int (*regmap_hw_read)(void *context,
  220. const void *reg_buf, size_t reg_size,
  221. void *val_buf, size_t val_size);
  222. typedef void (*regmap_hw_free_context)(void *context);
  223. /**
  224. * Description of a hardware bus for the register map infrastructure.
  225. *
  226. * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
  227. * to perform locking. This field is ignored if custom lock/unlock
  228. * functions are used (see fields lock/unlock of
  229. * struct regmap_config).
  230. * @write: Write operation.
  231. * @gather_write: Write operation with split register/value, return -ENOTSUPP
  232. * if not implemented on a given device.
  233. * @read: Read operation. Data is returned in the buffer used to transmit
  234. * data.
  235. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  236. * a read.
  237. * @reg_format_endian_default: Default endianness for formatted register
  238. * addresses. Used when the regmap_config specifies DEFAULT. If this is
  239. * DEFAULT, BIG is assumed.
  240. * @val_format_endian_default: Default endianness for formatted register
  241. * values. Used when the regmap_config specifies DEFAULT. If this is
  242. * DEFAULT, BIG is assumed.
  243. */
  244. struct regmap_bus {
  245. bool fast_io;
  246. regmap_hw_write write;
  247. regmap_hw_gather_write gather_write;
  248. regmap_hw_read read;
  249. regmap_hw_free_context free_context;
  250. u8 read_flag_mask;
  251. enum regmap_endian reg_format_endian_default;
  252. enum regmap_endian val_format_endian_default;
  253. };
  254. struct regmap *regmap_init(struct device *dev,
  255. const struct regmap_bus *bus,
  256. void *bus_context,
  257. const struct regmap_config *config);
  258. struct regmap *regmap_init_i2c(struct i2c_client *i2c,
  259. const struct regmap_config *config);
  260. struct regmap *regmap_init_spi(struct spi_device *dev,
  261. const struct regmap_config *config);
  262. struct regmap *regmap_init_mmio(struct device *dev,
  263. void __iomem *regs,
  264. const struct regmap_config *config);
  265. struct regmap *devm_regmap_init(struct device *dev,
  266. const struct regmap_bus *bus,
  267. void *bus_context,
  268. const struct regmap_config *config);
  269. struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
  270. const struct regmap_config *config);
  271. struct regmap *devm_regmap_init_spi(struct spi_device *dev,
  272. const struct regmap_config *config);
  273. struct regmap *devm_regmap_init_mmio(struct device *dev,
  274. void __iomem *regs,
  275. const struct regmap_config *config);
  276. void regmap_exit(struct regmap *map);
  277. int regmap_reinit_cache(struct regmap *map,
  278. const struct regmap_config *config);
  279. struct regmap *dev_get_regmap(struct device *dev, const char *name);
  280. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
  281. int regmap_raw_write(struct regmap *map, unsigned int reg,
  282. const void *val, size_t val_len);
  283. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  284. size_t val_count);
  285. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
  286. int regmap_raw_read(struct regmap *map, unsigned int reg,
  287. void *val, size_t val_len);
  288. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  289. size_t val_count);
  290. int regmap_update_bits(struct regmap *map, unsigned int reg,
  291. unsigned int mask, unsigned int val);
  292. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  293. unsigned int mask, unsigned int val,
  294. bool *change);
  295. int regmap_get_val_bytes(struct regmap *map);
  296. int regcache_sync(struct regmap *map);
  297. int regcache_sync_region(struct regmap *map, unsigned int min,
  298. unsigned int max);
  299. void regcache_cache_only(struct regmap *map, bool enable);
  300. void regcache_cache_bypass(struct regmap *map, bool enable);
  301. void regcache_mark_dirty(struct regmap *map);
  302. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  303. int num_regs);
  304. static inline bool regmap_reg_in_range(unsigned int reg,
  305. const struct regmap_range *range)
  306. {
  307. return reg >= range->range_min && reg <= range->range_max;
  308. }
  309. bool regmap_reg_in_ranges(unsigned int reg,
  310. const struct regmap_range *ranges,
  311. unsigned int nranges);
  312. /**
  313. * Description of an IRQ for the generic regmap irq_chip.
  314. *
  315. * @reg_offset: Offset of the status/mask register within the bank
  316. * @mask: Mask used to flag/control the register.
  317. */
  318. struct regmap_irq {
  319. unsigned int reg_offset;
  320. unsigned int mask;
  321. };
  322. /**
  323. * Description of a generic regmap irq_chip. This is not intended to
  324. * handle every possible interrupt controller, but it should handle a
  325. * substantial proportion of those that are found in the wild.
  326. *
  327. * @name: Descriptive name for IRQ controller.
  328. *
  329. * @status_base: Base status register address.
  330. * @mask_base: Base mask register address.
  331. * @ack_base: Base ack address. If zero then the chip is clear on read.
  332. * @wake_base: Base address for wake enables. If zero unsupported.
  333. * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
  334. * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
  335. *
  336. * @num_regs: Number of registers in each control bank.
  337. * @irqs: Descriptors for individual IRQs. Interrupt numbers are
  338. * assigned based on the index in the array of the interrupt.
  339. * @num_irqs: Number of descriptors.
  340. */
  341. struct regmap_irq_chip {
  342. const char *name;
  343. unsigned int status_base;
  344. unsigned int mask_base;
  345. unsigned int ack_base;
  346. unsigned int wake_base;
  347. unsigned int irq_reg_stride;
  348. unsigned int mask_invert;
  349. bool runtime_pm;
  350. int num_regs;
  351. const struct regmap_irq *irqs;
  352. int num_irqs;
  353. };
  354. struct regmap_irq_chip_data;
  355. int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
  356. int irq_base, const struct regmap_irq_chip *chip,
  357. struct regmap_irq_chip_data **data);
  358. void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
  359. int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
  360. int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
  361. #else
  362. /*
  363. * These stubs should only ever be called by generic code which has
  364. * regmap based facilities, if they ever get called at runtime
  365. * something is going wrong and something probably needs to select
  366. * REGMAP.
  367. */
  368. static inline int regmap_write(struct regmap *map, unsigned int reg,
  369. unsigned int val)
  370. {
  371. WARN_ONCE(1, "regmap API is disabled");
  372. return -EINVAL;
  373. }
  374. static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
  375. const void *val, size_t val_len)
  376. {
  377. WARN_ONCE(1, "regmap API is disabled");
  378. return -EINVAL;
  379. }
  380. static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
  381. const void *val, size_t val_count)
  382. {
  383. WARN_ONCE(1, "regmap API is disabled");
  384. return -EINVAL;
  385. }
  386. static inline int regmap_read(struct regmap *map, unsigned int reg,
  387. unsigned int *val)
  388. {
  389. WARN_ONCE(1, "regmap API is disabled");
  390. return -EINVAL;
  391. }
  392. static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
  393. void *val, size_t val_len)
  394. {
  395. WARN_ONCE(1, "regmap API is disabled");
  396. return -EINVAL;
  397. }
  398. static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
  399. void *val, size_t val_count)
  400. {
  401. WARN_ONCE(1, "regmap API is disabled");
  402. return -EINVAL;
  403. }
  404. static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
  405. unsigned int mask, unsigned int val)
  406. {
  407. WARN_ONCE(1, "regmap API is disabled");
  408. return -EINVAL;
  409. }
  410. static inline int regmap_update_bits_check(struct regmap *map,
  411. unsigned int reg,
  412. unsigned int mask, unsigned int val,
  413. bool *change)
  414. {
  415. WARN_ONCE(1, "regmap API is disabled");
  416. return -EINVAL;
  417. }
  418. static inline int regmap_get_val_bytes(struct regmap *map)
  419. {
  420. WARN_ONCE(1, "regmap API is disabled");
  421. return -EINVAL;
  422. }
  423. static inline int regcache_sync(struct regmap *map)
  424. {
  425. WARN_ONCE(1, "regmap API is disabled");
  426. return -EINVAL;
  427. }
  428. static inline int regcache_sync_region(struct regmap *map, unsigned int min,
  429. unsigned int max)
  430. {
  431. WARN_ONCE(1, "regmap API is disabled");
  432. return -EINVAL;
  433. }
  434. static inline void regcache_cache_only(struct regmap *map, bool enable)
  435. {
  436. WARN_ONCE(1, "regmap API is disabled");
  437. }
  438. static inline void regcache_cache_bypass(struct regmap *map, bool enable)
  439. {
  440. WARN_ONCE(1, "regmap API is disabled");
  441. }
  442. static inline void regcache_mark_dirty(struct regmap *map)
  443. {
  444. WARN_ONCE(1, "regmap API is disabled");
  445. }
  446. static inline int regmap_register_patch(struct regmap *map,
  447. const struct reg_default *regs,
  448. int num_regs)
  449. {
  450. WARN_ONCE(1, "regmap API is disabled");
  451. return -EINVAL;
  452. }
  453. static inline struct regmap *dev_get_regmap(struct device *dev,
  454. const char *name)
  455. {
  456. return NULL;
  457. }
  458. #endif
  459. #endif