regmap.h 18 KB

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