regmap.h 4.7 KB

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