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>
17 #include <linux/rbtree.h>
25 struct regmap_range_cfg;
27 /* An enum of all the supported cache types */
35 * Default value for a register. We use an array of structs rather
36 * than a simple array as many modern devices have very sparse
39 * @reg: Register address.
40 * @def: Register default value.
50 /* Unspecified -> 0 -> Backwards compatible default */
51 REGMAP_ENDIAN_DEFAULT = 0,
58 * Configuration for the register map of a device.
60 * @name: Optional name of the regmap. Useful when a device has multiple
63 * @reg_bits: Number of bits in a register address, mandatory.
64 * @reg_stride: The register address stride. Valid register addresses are a
65 * multiple of this value. If set to 0, a value of 1 will be
67 * @pad_bits: Number of bits of padding between register and value.
68 * @val_bits: Number of bits in a register value, mandatory.
70 * @writeable_reg: Optional callback returning true if the register
72 * @readable_reg: Optional callback returning true if the register
74 * @volatile_reg: Optional callback returning true if the register
75 * value can't be cached.
76 * @precious_reg: Optional callback returning true if the rgister
77 * should not be read outside of a call from the driver
78 * (eg, a clear on read interrupt status register).
80 * @max_register: Optional, specifies the maximum valid register index.
81 * @reg_defaults: Power on reset values for registers (for use with
82 * register cache support).
83 * @num_reg_defaults: Number of elements in reg_defaults.
85 * @read_flag_mask: Mask to be set in the top byte of the register when doing
87 * @write_flag_mask: Mask to be set in the top byte of the register when doing
88 * a write. If both read_flag_mask and write_flag_mask are
89 * empty the regmap_bus default masks are used.
90 * @use_single_rw: If set, converts the bulk read and write operations into
91 * a series of single read and write operations. This is useful
92 * for device that does not support bulk read and write.
94 * @cache_type: The actual cache type.
95 * @reg_defaults_raw: Power on reset values for registers (for use with
96 * register cache support).
97 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
98 * @reg_format_endian: Endianness for formatted register addresses. If this is
99 * DEFAULT, the @reg_format_endian_default value from the
100 * regmap bus is used.
101 * @val_format_endian: Endianness for formatted register values. If this is
102 * DEFAULT, the @reg_format_endian_default value from the
103 * regmap bus is used.
105 * @ranges: Array of configuration entries for virtual address ranges.
106 * @num_ranges: Number of range configuration entries.
108 struct regmap_config {
116 bool (*writeable_reg)(struct device *dev, unsigned int reg);
117 bool (*readable_reg)(struct device *dev, unsigned int reg);
118 bool (*volatile_reg)(struct device *dev, unsigned int reg);
119 bool (*precious_reg)(struct device *dev, unsigned int reg);
121 unsigned int max_register;
122 const struct reg_default *reg_defaults;
123 unsigned int num_reg_defaults;
124 enum regcache_type cache_type;
125 const void *reg_defaults_raw;
126 unsigned int num_reg_defaults_raw;
133 enum regmap_endian reg_format_endian;
134 enum regmap_endian val_format_endian;
136 const struct regmap_range_cfg *ranges;
137 unsigned int num_ranges;
141 * Configuration for indirectly accessed or paged registers.
142 * Registers, mapped to this virtual range, are accessed in two steps:
143 * 1. page selector register update;
144 * 2. access through data window registers.
146 * @name: Descriptive name for diagnostics
148 * @range_min: Address of the lowest register address in virtual range.
149 * @range_max: Address of the highest register in virtual range.
151 * @page_sel_reg: Register with selector field.
152 * @page_sel_mask: Bit shift for selector value.
153 * @page_sel_shift: Bit mask for selector value.
155 * @window_start: Address of first (lowest) register in data window.
156 * @window_len: Number of registers in data window.
158 struct regmap_range_cfg {
161 /* Registers of virtual address range */
162 unsigned int range_min;
163 unsigned int range_max;
165 /* Page selector for indirect addressing */
166 unsigned int selector_reg;
167 unsigned int selector_mask;
170 /* Data window (per each page) */
171 unsigned int window_start;
172 unsigned int window_len;
175 typedef int (*regmap_hw_write)(void *context, const void *data,
177 typedef int (*regmap_hw_gather_write)(void *context,
178 const void *reg, size_t reg_len,
179 const void *val, size_t val_len);
180 typedef int (*regmap_hw_read)(void *context,
181 const void *reg_buf, size_t reg_size,
182 void *val_buf, size_t val_size);
183 typedef void (*regmap_hw_free_context)(void *context);
186 * Description of a hardware bus for the register map infrastructure.
188 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
189 * to perform locking.
190 * @write: Write operation.
191 * @gather_write: Write operation with split register/value, return -ENOTSUPP
192 * if not implemented on a given device.
193 * @read: Read operation. Data is returned in the buffer used to transmit
195 * @read_flag_mask: Mask to be set in the top byte of the register when doing
197 * @reg_format_endian_default: Default endianness for formatted register
198 * addresses. Used when the regmap_config specifies DEFAULT. If this is
199 * DEFAULT, BIG is assumed.
200 * @val_format_endian_default: Default endianness for formatted register
201 * values. Used when the regmap_config specifies DEFAULT. If this is
202 * DEFAULT, BIG is assumed.
206 regmap_hw_write write;
207 regmap_hw_gather_write gather_write;
209 regmap_hw_free_context free_context;
211 enum regmap_endian reg_format_endian_default;
212 enum regmap_endian val_format_endian_default;
215 struct regmap *regmap_init(struct device *dev,
216 const struct regmap_bus *bus,
218 const struct regmap_config *config);
219 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
220 const struct regmap_config *config);
221 struct regmap *regmap_init_spi(struct spi_device *dev,
222 const struct regmap_config *config);
223 struct regmap *regmap_init_mmio(struct device *dev,
225 const struct regmap_config *config);
227 struct regmap *devm_regmap_init(struct device *dev,
228 const struct regmap_bus *bus,
230 const struct regmap_config *config);
231 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
232 const struct regmap_config *config);
233 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
234 const struct regmap_config *config);
235 struct regmap *devm_regmap_init_mmio(struct device *dev,
237 const struct regmap_config *config);
239 void regmap_exit(struct regmap *map);
240 int regmap_reinit_cache(struct regmap *map,
241 const struct regmap_config *config);
242 struct regmap *dev_get_regmap(struct device *dev, const char *name);
243 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
244 int regmap_raw_write(struct regmap *map, unsigned int reg,
245 const void *val, size_t val_len);
246 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
248 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
249 int regmap_raw_read(struct regmap *map, unsigned int reg,
250 void *val, size_t val_len);
251 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
253 int regmap_update_bits(struct regmap *map, unsigned int reg,
254 unsigned int mask, unsigned int val);
255 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
256 unsigned int mask, unsigned int val,
258 int regmap_get_val_bytes(struct regmap *map);
260 int regcache_sync(struct regmap *map);
261 int regcache_sync_region(struct regmap *map, unsigned int min,
263 void regcache_cache_only(struct regmap *map, bool enable);
264 void regcache_cache_bypass(struct regmap *map, bool enable);
265 void regcache_mark_dirty(struct regmap *map);
267 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
271 * Description of an IRQ for the generic regmap irq_chip.
273 * @reg_offset: Offset of the status/mask register within the bank
274 * @mask: Mask used to flag/control the register.
277 unsigned int reg_offset;
282 * Description of a generic regmap irq_chip. This is not intended to
283 * handle every possible interrupt controller, but it should handle a
284 * substantial proportion of those that are found in the wild.
286 * @name: Descriptive name for IRQ controller.
288 * @status_base: Base status register address.
289 * @mask_base: Base mask register address.
290 * @ack_base: Base ack address. If zero then the chip is clear on read.
291 * @wake_base: Base address for wake enables. If zero unsupported.
292 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
293 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
295 * @num_regs: Number of registers in each control bank.
296 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
297 * assigned based on the index in the array of the interrupt.
298 * @num_irqs: Number of descriptors.
300 struct regmap_irq_chip {
303 unsigned int status_base;
304 unsigned int mask_base;
305 unsigned int ack_base;
306 unsigned int wake_base;
307 unsigned int irq_reg_stride;
308 unsigned int mask_invert;
313 const struct regmap_irq *irqs;
317 struct regmap_irq_chip_data;
319 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
320 int irq_base, const struct regmap_irq_chip *chip,
321 struct regmap_irq_chip_data **data);
322 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
323 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
324 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
325 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
330 * These stubs should only ever be called by generic code which has
331 * regmap based facilities, if they ever get called at runtime
332 * something is going wrong and something probably needs to select
336 static inline int regmap_write(struct regmap *map, unsigned int reg,
339 WARN_ONCE(1, "regmap API is disabled");
343 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
344 const void *val, size_t val_len)
346 WARN_ONCE(1, "regmap API is disabled");
350 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
351 const void *val, size_t val_count)
353 WARN_ONCE(1, "regmap API is disabled");
357 static inline int regmap_read(struct regmap *map, unsigned int reg,
360 WARN_ONCE(1, "regmap API is disabled");
364 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
365 void *val, size_t val_len)
367 WARN_ONCE(1, "regmap API is disabled");
371 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
372 void *val, size_t val_count)
374 WARN_ONCE(1, "regmap API is disabled");
378 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
379 unsigned int mask, unsigned int val)
381 WARN_ONCE(1, "regmap API is disabled");
385 static inline int regmap_update_bits_check(struct regmap *map,
387 unsigned int mask, unsigned int val,
390 WARN_ONCE(1, "regmap API is disabled");
394 static inline int regmap_get_val_bytes(struct regmap *map)
396 WARN_ONCE(1, "regmap API is disabled");
400 static inline int regcache_sync(struct regmap *map)
402 WARN_ONCE(1, "regmap API is disabled");
406 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
409 WARN_ONCE(1, "regmap API is disabled");
413 static inline void regcache_cache_only(struct regmap *map, bool enable)
415 WARN_ONCE(1, "regmap API is disabled");
418 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
420 WARN_ONCE(1, "regmap API is disabled");
423 static inline void regcache_mark_dirty(struct regmap *map)
425 WARN_ONCE(1, "regmap API is disabled");
428 static inline int regmap_register_patch(struct regmap *map,
429 const struct reg_default *regs,
432 WARN_ONCE(1, "regmap API is disabled");
436 static inline struct regmap *dev_get_regmap(struct device *dev,