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,
57 typedef void (*regmap_lock)(void *);
58 typedef void (*regmap_unlock)(void *);
61 * Configuration for the register map of a device.
63 * @name: Optional name of the regmap. Useful when a device has multiple
66 * @reg_bits: Number of bits in a register address, mandatory.
67 * @reg_stride: The register address stride. Valid register addresses are a
68 * multiple of this value. If set to 0, a value of 1 will be
70 * @pad_bits: Number of bits of padding between register and value.
71 * @val_bits: Number of bits in a register value, mandatory.
73 * @writeable_reg: Optional callback returning true if the register
75 * @readable_reg: Optional callback returning true if the register
77 * @volatile_reg: Optional callback returning true if the register
78 * value can't be cached.
79 * @precious_reg: Optional callback returning true if the rgister
80 * should not be read outside of a call from the driver
81 * (eg, a clear on read interrupt status register).
82 * @lock: Optional lock callback (overrides regmap's default lock
83 * function, based on spinlock or mutex).
84 * @unlock: As above for unlocking.
85 * @lock_arg: this field is passed as the only argument of lock/unlock
86 * functions (ignored in case regular lock/unlock functions
87 * are not overridden).
89 * @max_register: Optional, specifies the maximum valid register index.
90 * @reg_defaults: Power on reset values for registers (for use with
91 * register cache support).
92 * @num_reg_defaults: Number of elements in reg_defaults.
94 * @read_flag_mask: Mask to be set in the top byte of the register when doing
96 * @write_flag_mask: Mask to be set in the top byte of the register when doing
97 * a write. If both read_flag_mask and write_flag_mask are
98 * empty the regmap_bus default masks are used.
99 * @use_single_rw: If set, converts the bulk read and write operations into
100 * a series of single read and write operations. This is useful
101 * for device that does not support bulk read and write.
103 * @cache_type: The actual cache type.
104 * @reg_defaults_raw: Power on reset values for registers (for use with
105 * register cache support).
106 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
107 * @reg_format_endian: Endianness for formatted register addresses. If this is
108 * DEFAULT, the @reg_format_endian_default value from the
109 * regmap bus is used.
110 * @val_format_endian: Endianness for formatted register values. If this is
111 * DEFAULT, the @reg_format_endian_default value from the
112 * regmap bus is used.
114 * @ranges: Array of configuration entries for virtual address ranges.
115 * @num_ranges: Number of range configuration entries.
117 struct regmap_config {
125 bool (*writeable_reg)(struct device *dev, unsigned int reg);
126 bool (*readable_reg)(struct device *dev, unsigned int reg);
127 bool (*volatile_reg)(struct device *dev, unsigned int reg);
128 bool (*precious_reg)(struct device *dev, unsigned int reg);
130 regmap_unlock unlock;
133 unsigned int max_register;
134 const struct reg_default *reg_defaults;
135 unsigned int num_reg_defaults;
136 enum regcache_type cache_type;
137 const void *reg_defaults_raw;
138 unsigned int num_reg_defaults_raw;
145 enum regmap_endian reg_format_endian;
146 enum regmap_endian val_format_endian;
148 const struct regmap_range_cfg *ranges;
149 unsigned int num_ranges;
153 * Configuration for indirectly accessed or paged registers.
154 * Registers, mapped to this virtual range, are accessed in two steps:
155 * 1. page selector register update;
156 * 2. access through data window registers.
158 * @name: Descriptive name for diagnostics
160 * @range_min: Address of the lowest register address in virtual range.
161 * @range_max: Address of the highest register in virtual range.
163 * @page_sel_reg: Register with selector field.
164 * @page_sel_mask: Bit shift for selector value.
165 * @page_sel_shift: Bit mask for selector value.
167 * @window_start: Address of first (lowest) register in data window.
168 * @window_len: Number of registers in data window.
170 struct regmap_range_cfg {
173 /* Registers of virtual address range */
174 unsigned int range_min;
175 unsigned int range_max;
177 /* Page selector for indirect addressing */
178 unsigned int selector_reg;
179 unsigned int selector_mask;
182 /* Data window (per each page) */
183 unsigned int window_start;
184 unsigned int window_len;
187 typedef int (*regmap_hw_write)(void *context, const void *data,
189 typedef int (*regmap_hw_gather_write)(void *context,
190 const void *reg, size_t reg_len,
191 const void *val, size_t val_len);
192 typedef int (*regmap_hw_read)(void *context,
193 const void *reg_buf, size_t reg_size,
194 void *val_buf, size_t val_size);
195 typedef void (*regmap_hw_free_context)(void *context);
198 * Description of a hardware bus for the register map infrastructure.
200 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
201 * to perform locking. This field is ignored if custom lock/unlock
202 * functions are used (see fields lock/unlock of
203 * struct regmap_config).
204 * @write: Write operation.
205 * @gather_write: Write operation with split register/value, return -ENOTSUPP
206 * if not implemented on a given device.
207 * @read: Read operation. Data is returned in the buffer used to transmit
209 * @read_flag_mask: Mask to be set in the top byte of the register when doing
211 * @reg_format_endian_default: Default endianness for formatted register
212 * addresses. Used when the regmap_config specifies DEFAULT. If this is
213 * DEFAULT, BIG is assumed.
214 * @val_format_endian_default: Default endianness for formatted register
215 * values. Used when the regmap_config specifies DEFAULT. If this is
216 * DEFAULT, BIG is assumed.
220 regmap_hw_write write;
221 regmap_hw_gather_write gather_write;
223 regmap_hw_free_context free_context;
225 enum regmap_endian reg_format_endian_default;
226 enum regmap_endian val_format_endian_default;
229 struct regmap *regmap_init(struct device *dev,
230 const struct regmap_bus *bus,
232 const struct regmap_config *config);
233 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
234 const struct regmap_config *config);
235 struct regmap *regmap_init_spi(struct spi_device *dev,
236 const struct regmap_config *config);
237 struct regmap *regmap_init_mmio(struct device *dev,
239 const struct regmap_config *config);
241 struct regmap *devm_regmap_init(struct device *dev,
242 const struct regmap_bus *bus,
244 const struct regmap_config *config);
245 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
246 const struct regmap_config *config);
247 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
248 const struct regmap_config *config);
249 struct regmap *devm_regmap_init_mmio(struct device *dev,
251 const struct regmap_config *config);
253 void regmap_exit(struct regmap *map);
254 int regmap_reinit_cache(struct regmap *map,
255 const struct regmap_config *config);
256 struct regmap *dev_get_regmap(struct device *dev, const char *name);
257 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
258 int regmap_raw_write(struct regmap *map, unsigned int reg,
259 const void *val, size_t val_len);
260 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
262 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
263 int regmap_raw_read(struct regmap *map, unsigned int reg,
264 void *val, size_t val_len);
265 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
267 int regmap_update_bits(struct regmap *map, unsigned int reg,
268 unsigned int mask, unsigned int val);
269 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
270 unsigned int mask, unsigned int val,
272 int regmap_get_val_bytes(struct regmap *map);
274 int regcache_sync(struct regmap *map);
275 int regcache_sync_region(struct regmap *map, unsigned int min,
277 void regcache_cache_only(struct regmap *map, bool enable);
278 void regcache_cache_bypass(struct regmap *map, bool enable);
279 void regcache_mark_dirty(struct regmap *map);
281 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
285 * Description of an IRQ for the generic regmap irq_chip.
287 * @reg_offset: Offset of the status/mask register within the bank
288 * @mask: Mask used to flag/control the register.
291 unsigned int reg_offset;
296 * Description of a generic regmap irq_chip. This is not intended to
297 * handle every possible interrupt controller, but it should handle a
298 * substantial proportion of those that are found in the wild.
300 * @name: Descriptive name for IRQ controller.
302 * @status_base: Base status register address.
303 * @mask_base: Base mask register address.
304 * @ack_base: Base ack address. If zero then the chip is clear on read.
305 * @wake_base: Base address for wake enables. If zero unsupported.
306 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
307 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
309 * @num_regs: Number of registers in each control bank.
310 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
311 * assigned based on the index in the array of the interrupt.
312 * @num_irqs: Number of descriptors.
314 struct regmap_irq_chip {
317 unsigned int status_base;
318 unsigned int mask_base;
319 unsigned int ack_base;
320 unsigned int wake_base;
321 unsigned int irq_reg_stride;
322 unsigned int mask_invert;
327 const struct regmap_irq *irqs;
331 struct regmap_irq_chip_data;
333 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
334 int irq_base, const struct regmap_irq_chip *chip,
335 struct regmap_irq_chip_data **data);
336 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
337 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
338 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
339 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
344 * These stubs should only ever be called by generic code which has
345 * regmap based facilities, if they ever get called at runtime
346 * something is going wrong and something probably needs to select
350 static inline int regmap_write(struct regmap *map, unsigned int reg,
353 WARN_ONCE(1, "regmap API is disabled");
357 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
358 const void *val, size_t val_len)
360 WARN_ONCE(1, "regmap API is disabled");
364 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
365 const void *val, size_t val_count)
367 WARN_ONCE(1, "regmap API is disabled");
371 static inline int regmap_read(struct regmap *map, unsigned int reg,
374 WARN_ONCE(1, "regmap API is disabled");
378 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
379 void *val, size_t val_len)
381 WARN_ONCE(1, "regmap API is disabled");
385 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
386 void *val, size_t val_count)
388 WARN_ONCE(1, "regmap API is disabled");
392 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
393 unsigned int mask, unsigned int val)
395 WARN_ONCE(1, "regmap API is disabled");
399 static inline int regmap_update_bits_check(struct regmap *map,
401 unsigned int mask, unsigned int val,
404 WARN_ONCE(1, "regmap API is disabled");
408 static inline int regmap_get_val_bytes(struct regmap *map)
410 WARN_ONCE(1, "regmap API is disabled");
414 static inline int regcache_sync(struct regmap *map)
416 WARN_ONCE(1, "regmap API is disabled");
420 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
423 WARN_ONCE(1, "regmap API is disabled");
427 static inline void regcache_cache_only(struct regmap *map, bool enable)
429 WARN_ONCE(1, "regmap API is disabled");
432 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
434 WARN_ONCE(1, "regmap API is disabled");
437 static inline void regcache_mark_dirty(struct regmap *map)
439 WARN_ONCE(1, "regmap API is disabled");
442 static inline int regmap_register_patch(struct regmap *map,
443 const struct reg_default *regs,
446 WARN_ONCE(1, "regmap API is disabled");
450 static inline struct regmap *dev_get_regmap(struct device *dev,