1 #ifndef __LINUX_REGMAP_H
2 #define __LINUX_REGMAP_H
5 * Register map access API
7 * Copyright 2011 Wolfson Microelectronics plc
9 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/list.h>
24 /* An enum of all the supported cache types */
32 * Default value for a register. We use an array of structs rather
33 * than a simple array as many modern devices have very sparse
36 * @reg: Register address.
37 * @def: Register default value.
47 /* Unspecified -> 0 -> Backwards compatible default */
48 REGMAP_ENDIAN_DEFAULT = 0,
55 * Configuration for the register map of a device.
57 * @name: Optional name of the regmap. Useful when a device has multiple
60 * @reg_bits: Number of bits in a register address, mandatory.
61 * @reg_stride: The register address stride. Valid register addresses are a
62 * multiple of this value. If set to 0, a value of 1 will be
64 * @pad_bits: Number of bits of padding between register and value.
65 * @val_bits: Number of bits in a register value, mandatory.
67 * @writeable_reg: Optional callback returning true if the register
69 * @readable_reg: Optional callback returning true if the register
71 * @volatile_reg: Optional callback returning true if the register
72 * value can't be cached.
73 * @precious_reg: Optional callback returning true if the rgister
74 * should not be read outside of a call from the driver
75 * (eg, a clear on read interrupt status register).
77 * @max_register: Optional, specifies the maximum valid register index.
78 * @reg_defaults: Power on reset values for registers (for use with
79 * register cache support).
80 * @num_reg_defaults: Number of elements in reg_defaults.
82 * @read_flag_mask: Mask to be set in the top byte of the register when doing
84 * @write_flag_mask: Mask to be set in the top byte of the register when doing
85 * a write. If both read_flag_mask and write_flag_mask are
86 * empty the regmap_bus default masks are used.
87 * @use_single_rw: If set, converts the bulk read and write operations into
88 * a series of single read and write operations. This is useful
89 * for device that does not support bulk read and write.
91 * @cache_type: The actual cache type.
92 * @reg_defaults_raw: Power on reset values for registers (for use with
93 * register cache support).
94 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
95 * @reg_format_endian: Endianness for formatted register addresses. If this is
96 * DEFAULT, the @reg_format_endian_default value from the
98 * @val_format_endian: Endianness for formatted register values. If this is
99 * DEFAULT, the @reg_format_endian_default value from the
100 * regmap bus is used.
102 struct regmap_config {
110 bool (*writeable_reg)(struct device *dev, unsigned int reg);
111 bool (*readable_reg)(struct device *dev, unsigned int reg);
112 bool (*volatile_reg)(struct device *dev, unsigned int reg);
113 bool (*precious_reg)(struct device *dev, unsigned int reg);
115 unsigned int max_register;
116 const struct reg_default *reg_defaults;
117 unsigned int num_reg_defaults;
118 enum regcache_type cache_type;
119 const void *reg_defaults_raw;
120 unsigned int num_reg_defaults_raw;
127 enum regmap_endian reg_format_endian;
128 enum regmap_endian val_format_endian;
131 typedef int (*regmap_hw_write)(void *context, const void *data,
133 typedef int (*regmap_hw_gather_write)(void *context,
134 const void *reg, size_t reg_len,
135 const void *val, size_t val_len);
136 typedef int (*regmap_hw_read)(void *context,
137 const void *reg_buf, size_t reg_size,
138 void *val_buf, size_t val_size);
139 typedef void (*regmap_hw_free_context)(void *context);
142 * Description of a hardware bus for the register map infrastructure.
144 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
145 * to perform locking.
146 * @write: Write operation.
147 * @gather_write: Write operation with split register/value, return -ENOTSUPP
148 * if not implemented on a given device.
149 * @read: Read operation. Data is returned in the buffer used to transmit
151 * @read_flag_mask: Mask to be set in the top byte of the register when doing
153 * @reg_format_endian_default: Default endianness for formatted register
154 * addresses. Used when the regmap_config specifies DEFAULT. If this is
155 * DEFAULT, BIG is assumed.
156 * @val_format_endian_default: Default endianness for formatted register
157 * values. Used when the regmap_config specifies DEFAULT. If this is
158 * DEFAULT, BIG is assumed.
162 regmap_hw_write write;
163 regmap_hw_gather_write gather_write;
165 regmap_hw_free_context free_context;
167 enum regmap_endian reg_format_endian_default;
168 enum regmap_endian val_format_endian_default;
171 struct regmap *regmap_init(struct device *dev,
172 const struct regmap_bus *bus,
174 const struct regmap_config *config);
175 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
176 const struct regmap_config *config);
177 struct regmap *regmap_init_spi(struct spi_device *dev,
178 const struct regmap_config *config);
179 struct regmap *regmap_init_mmio(struct device *dev,
181 const struct regmap_config *config);
183 struct regmap *devm_regmap_init(struct device *dev,
184 const struct regmap_bus *bus,
186 const struct regmap_config *config);
187 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
188 const struct regmap_config *config);
189 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
190 const struct regmap_config *config);
191 struct regmap *devm_regmap_init_mmio(struct device *dev,
193 const struct regmap_config *config);
195 void regmap_exit(struct regmap *map);
196 int regmap_reinit_cache(struct regmap *map,
197 const struct regmap_config *config);
198 struct regmap *dev_get_regmap(struct device *dev, const char *name);
199 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
200 int regmap_raw_write(struct regmap *map, unsigned int reg,
201 const void *val, size_t val_len);
202 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
204 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
205 int regmap_raw_read(struct regmap *map, unsigned int reg,
206 void *val, size_t val_len);
207 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
209 int regmap_update_bits(struct regmap *map, unsigned int reg,
210 unsigned int mask, unsigned int val);
211 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
212 unsigned int mask, unsigned int val,
214 int regmap_get_val_bytes(struct regmap *map);
216 int regcache_sync(struct regmap *map);
217 int regcache_sync_region(struct regmap *map, unsigned int min,
219 void regcache_cache_only(struct regmap *map, bool enable);
220 void regcache_cache_bypass(struct regmap *map, bool enable);
221 void regcache_mark_dirty(struct regmap *map);
223 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
227 * Description of an IRQ for the generic regmap irq_chip.
229 * @reg_offset: Offset of the status/mask register within the bank
230 * @mask: Mask used to flag/control the register.
233 unsigned int reg_offset;
238 * Description of a generic regmap irq_chip. This is not intended to
239 * handle every possible interrupt controller, but it should handle a
240 * substantial proportion of those that are found in the wild.
242 * @name: Descriptive name for IRQ controller.
244 * @status_base: Base status register address.
245 * @mask_base: Base mask register address.
246 * @ack_base: Base ack address. If zero then the chip is clear on read.
247 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
249 * @num_regs: Number of registers in each control bank.
250 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
251 * assigned based on the index in the array of the interrupt.
252 * @num_irqs: Number of descriptors.
254 struct regmap_irq_chip {
257 unsigned int status_base;
258 unsigned int mask_base;
259 unsigned int ack_base;
260 unsigned int irq_reg_stride;
264 const struct regmap_irq *irqs;
268 struct regmap_irq_chip_data;
270 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
271 int irq_base, const struct regmap_irq_chip *chip,
272 struct regmap_irq_chip_data **data);
273 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
274 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
275 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
280 * These stubs should only ever be called by generic code which has
281 * regmap based facilities, if they ever get called at runtime
282 * something is going wrong and something probably needs to select
286 static inline int regmap_write(struct regmap *map, unsigned int reg,
289 WARN_ONCE(1, "regmap API is disabled");
293 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
294 const void *val, size_t val_len)
296 WARN_ONCE(1, "regmap API is disabled");
300 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
301 const void *val, size_t val_count)
303 WARN_ONCE(1, "regmap API is disabled");
307 static inline int regmap_read(struct regmap *map, unsigned int reg,
310 WARN_ONCE(1, "regmap API is disabled");
314 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
315 void *val, size_t val_len)
317 WARN_ONCE(1, "regmap API is disabled");
321 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
322 void *val, size_t val_count)
324 WARN_ONCE(1, "regmap API is disabled");
328 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
329 unsigned int mask, unsigned int val)
331 WARN_ONCE(1, "regmap API is disabled");
335 static inline int regmap_update_bits_check(struct regmap *map,
337 unsigned int mask, unsigned int val,
340 WARN_ONCE(1, "regmap API is disabled");
344 static inline int regmap_get_val_bytes(struct regmap *map)
346 WARN_ONCE(1, "regmap API is disabled");
350 static inline int regcache_sync(struct regmap *map)
352 WARN_ONCE(1, "regmap API is disabled");
356 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
359 WARN_ONCE(1, "regmap API is disabled");
363 static inline void regcache_cache_only(struct regmap *map, bool enable)
365 WARN_ONCE(1, "regmap API is disabled");
368 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
370 WARN_ONCE(1, "regmap API is disabled");
373 static inline void regcache_mark_dirty(struct regmap *map)
375 WARN_ONCE(1, "regmap API is disabled");
378 static inline int regmap_register_patch(struct regmap *map,
379 const struct reg_default *regs,
382 WARN_ONCE(1, "regmap API is disabled");
386 static inline struct regmap *dev_get_regmap(struct device *dev,