regmap.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/device.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. struct i2c_client;
  18. struct spi_device;
  19. /* An enum of all the supported cache types */
  20. enum regcache_type {
  21. REGCACHE_NONE,
  22. REGCACHE_INDEXED,
  23. REGCACHE_RBTREE,
  24. REGCACHE_LZO
  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. /**
  39. * Configuration for the register map of a device.
  40. *
  41. * @reg_bits: Number of bits in a register address, mandatory.
  42. * @val_bits: Number of bits in a register value, mandatory.
  43. *
  44. * @writeable_reg: Optional callback returning true if the register
  45. * can be written to.
  46. * @readable_reg: Optional callback returning true if the register
  47. * can be read from.
  48. * @volatile_reg: Optional callback returning true if the register
  49. * value can't be cached.
  50. * @precious_reg: Optional callback returning true if the rgister
  51. * should not be read outside of a call from the driver
  52. * (eg, a clear on read interrupt status register).
  53. *
  54. * @max_register: Optional, specifies the maximum valid register index.
  55. * @reg_defaults: Power on reset values for registers (for use with
  56. * register cache support).
  57. * @num_reg_defaults: Number of elements in reg_defaults.
  58. *
  59. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  60. * a read.
  61. * @write_flag_mask: Mask to be set in the top byte of the register when doing
  62. * a write. If both read_flag_mask and write_flag_mask are
  63. * empty the regmap_bus default masks are used.
  64. *
  65. * @cache_type: The actual cache type.
  66. * @reg_defaults_raw: Power on reset values for registers (for use with
  67. * register cache support).
  68. * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
  69. */
  70. struct regmap_config {
  71. int reg_bits;
  72. int val_bits;
  73. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  74. bool (*readable_reg)(struct device *dev, unsigned int reg);
  75. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  76. bool (*precious_reg)(struct device *dev, unsigned int reg);
  77. unsigned int max_register;
  78. struct reg_default *reg_defaults;
  79. unsigned int num_reg_defaults;
  80. enum regcache_type cache_type;
  81. const void *reg_defaults_raw;
  82. unsigned int num_reg_defaults_raw;
  83. u8 read_flag_mask;
  84. u8 write_flag_mask;
  85. };
  86. typedef int (*regmap_hw_write)(struct device *dev, const void *data,
  87. size_t count);
  88. typedef int (*regmap_hw_gather_write)(struct device *dev,
  89. const void *reg, size_t reg_len,
  90. const void *val, size_t val_len);
  91. typedef int (*regmap_hw_read)(struct device *dev,
  92. const void *reg_buf, size_t reg_size,
  93. void *val_buf, size_t val_size);
  94. /**
  95. * Description of a hardware bus for the register map infrastructure.
  96. *
  97. * @write: Write operation.
  98. * @gather_write: Write operation with split register/value, return -ENOTSUPP
  99. * if not implemented on a given device.
  100. * @read: Read operation. Data is returned in the buffer used to transmit
  101. * data.
  102. * @read_flag_mask: Mask to be set in the top byte of the register when doing
  103. * a read.
  104. */
  105. struct regmap_bus {
  106. regmap_hw_write write;
  107. regmap_hw_gather_write gather_write;
  108. regmap_hw_read read;
  109. u8 read_flag_mask;
  110. };
  111. struct regmap *regmap_init(struct device *dev,
  112. const struct regmap_bus *bus,
  113. const struct regmap_config *config);
  114. struct regmap *regmap_init_i2c(struct i2c_client *i2c,
  115. const struct regmap_config *config);
  116. struct regmap *regmap_init_spi(struct spi_device *dev,
  117. const struct regmap_config *config);
  118. void regmap_exit(struct regmap *map);
  119. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
  120. int regmap_raw_write(struct regmap *map, unsigned int reg,
  121. const void *val, size_t val_len);
  122. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
  123. int regmap_raw_read(struct regmap *map, unsigned int reg,
  124. void *val, size_t val_len);
  125. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  126. size_t val_count);
  127. int regmap_update_bits(struct regmap *map, unsigned int reg,
  128. unsigned int mask, unsigned int val);
  129. int regcache_sync(struct regmap *map);
  130. void regcache_cache_only(struct regmap *map, bool enable);
  131. #endif