regmap.h 20 KB

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