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 */
36 * Default value for a register. We use an array of structs rather
37 * than a simple array as many modern devices have very sparse
40 * @reg: Register address.
41 * @def: Register default value.
51 /* Unspecified -> 0 -> Backwards compatible default */
52 REGMAP_ENDIAN_DEFAULT = 0,
59 * A register range, used for access related checks
60 * (readable/writeable/volatile/precious checks)
62 * @range_min: address of first register
63 * @range_max: address of last register
66 unsigned int range_min;
67 unsigned int range_max;
71 * A table of ranges including some yes ranges and some no ranges.
72 * If a register belongs to a no_range, the corresponding check function
73 * will return false. If a register belongs to a yes range, the corresponding
74 * check function will return true. "no_ranges" are searched first.
76 * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
77 * @n_yes_ranges: size of the above array
78 * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
79 * @n_no_ranges: size of the above array
81 struct regmap_access_table {
82 const struct regmap_range *yes_ranges;
83 unsigned int n_yes_ranges;
84 const struct regmap_range *no_ranges;
85 unsigned int n_no_ranges;
88 typedef void (*regmap_lock)(void *);
89 typedef void (*regmap_unlock)(void *);
92 * Configuration for the register map of a device.
94 * @name: Optional name of the regmap. Useful when a device has multiple
97 * @reg_bits: Number of bits in a register address, mandatory.
98 * @reg_stride: The register address stride. Valid register addresses are a
99 * multiple of this value. If set to 0, a value of 1 will be
101 * @pad_bits: Number of bits of padding between register and value.
102 * @val_bits: Number of bits in a register value, mandatory.
104 * @writeable_reg: Optional callback returning true if the register
105 * can be written to. If this field is NULL but wr_table
106 * (see below) is not, the check is performed on such table
107 * (a register is writeable if it belongs to one of the ranges
108 * specified by wr_table).
109 * @readable_reg: Optional callback returning true if the register
110 * can be read from. If this field is NULL but rd_table
111 * (see below) is not, the check is performed on such table
112 * (a register is readable if it belongs to one of the ranges
113 * specified by rd_table).
114 * @volatile_reg: Optional callback returning true if the register
115 * value can't be cached. If this field is NULL but
116 * volatile_table (see below) is not, the check is performed on
117 * such table (a register is volatile if it belongs to one of
118 * the ranges specified by volatile_table).
119 * @precious_reg: Optional callback returning true if the rgister
120 * should not be read outside of a call from the driver
121 * (eg, a clear on read interrupt status register). If this
122 * field is NULL but precious_table (see below) is not, the
123 * check is performed on such table (a register is precious if
124 * it belongs to one of the ranges specified by precious_table).
125 * @lock: Optional lock callback (overrides regmap's default lock
126 * function, based on spinlock or mutex).
127 * @unlock: As above for unlocking.
128 * @lock_arg: this field is passed as the only argument of lock/unlock
129 * functions (ignored in case regular lock/unlock functions
130 * are not overridden).
132 * @max_register: Optional, specifies the maximum valid register index.
133 * @wr_table: Optional, points to a struct regmap_access_table specifying
134 * valid ranges for write access.
135 * @rd_table: As above, for read access.
136 * @volatile_table: As above, for volatile registers.
137 * @precious_table: As above, for precious registers.
138 * @reg_defaults: Power on reset values for registers (for use with
139 * register cache support).
140 * @num_reg_defaults: Number of elements in reg_defaults.
142 * @read_flag_mask: Mask to be set in the top byte of the register when doing
144 * @write_flag_mask: Mask to be set in the top byte of the register when doing
145 * a write. If both read_flag_mask and write_flag_mask are
146 * empty the regmap_bus default masks are used.
147 * @use_single_rw: If set, converts the bulk read and write operations into
148 * a series of single read and write operations. This is useful
149 * for device that does not support bulk read and write.
151 * @cache_type: The actual cache type.
152 * @reg_defaults_raw: Power on reset values for registers (for use with
153 * register cache support).
154 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
155 * @reg_format_endian: Endianness for formatted register addresses. If this is
156 * DEFAULT, the @reg_format_endian_default value from the
157 * regmap bus is used.
158 * @val_format_endian: Endianness for formatted register values. If this is
159 * DEFAULT, the @reg_format_endian_default value from the
160 * regmap bus is used.
162 * @ranges: Array of configuration entries for virtual address ranges.
163 * @num_ranges: Number of range configuration entries.
165 struct regmap_config {
173 bool (*writeable_reg)(struct device *dev, unsigned int reg);
174 bool (*readable_reg)(struct device *dev, unsigned int reg);
175 bool (*volatile_reg)(struct device *dev, unsigned int reg);
176 bool (*precious_reg)(struct device *dev, unsigned int reg);
178 regmap_unlock unlock;
181 unsigned int max_register;
182 const struct regmap_access_table *wr_table;
183 const struct regmap_access_table *rd_table;
184 const struct regmap_access_table *volatile_table;
185 const struct regmap_access_table *precious_table;
186 const struct reg_default *reg_defaults;
187 unsigned int num_reg_defaults;
188 enum regcache_type cache_type;
189 const void *reg_defaults_raw;
190 unsigned int num_reg_defaults_raw;
197 enum regmap_endian reg_format_endian;
198 enum regmap_endian val_format_endian;
200 const struct regmap_range_cfg *ranges;
201 unsigned int num_ranges;
205 * Configuration for indirectly accessed or paged registers.
206 * Registers, mapped to this virtual range, are accessed in two steps:
207 * 1. page selector register update;
208 * 2. access through data window registers.
210 * @name: Descriptive name for diagnostics
212 * @range_min: Address of the lowest register address in virtual range.
213 * @range_max: Address of the highest register in virtual range.
215 * @page_sel_reg: Register with selector field.
216 * @page_sel_mask: Bit shift for selector value.
217 * @page_sel_shift: Bit mask for selector value.
219 * @window_start: Address of first (lowest) register in data window.
220 * @window_len: Number of registers in data window.
222 struct regmap_range_cfg {
225 /* Registers of virtual address range */
226 unsigned int range_min;
227 unsigned int range_max;
229 /* Page selector for indirect addressing */
230 unsigned int selector_reg;
231 unsigned int selector_mask;
234 /* Data window (per each page) */
235 unsigned int window_start;
236 unsigned int window_len;
241 typedef int (*regmap_hw_write)(void *context, const void *data,
243 typedef int (*regmap_hw_gather_write)(void *context,
244 const void *reg, size_t reg_len,
245 const void *val, size_t val_len);
246 typedef int (*regmap_hw_async_write)(void *context,
247 const void *reg, size_t reg_len,
248 const void *val, size_t val_len,
249 struct regmap_async *async);
250 typedef int (*regmap_hw_read)(void *context,
251 const void *reg_buf, size_t reg_size,
252 void *val_buf, size_t val_size);
253 typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
254 typedef void (*regmap_hw_free_context)(void *context);
257 * Description of a hardware bus for the register map infrastructure.
259 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
260 * to perform locking. This field is ignored if custom lock/unlock
261 * functions are used (see fields lock/unlock of
262 * struct regmap_config).
263 * @write: Write operation.
264 * @gather_write: Write operation with split register/value, return -ENOTSUPP
265 * if not implemented on a given device.
266 * @async_write: Write operation which completes asynchronously, optional and
267 * must serialise with respect to non-async I/O.
268 * @read: Read operation. Data is returned in the buffer used to transmit
270 * @async_alloc: Allocate a regmap_async() structure.
271 * @read_flag_mask: Mask to be set in the top byte of the register when doing
273 * @reg_format_endian_default: Default endianness for formatted register
274 * addresses. Used when the regmap_config specifies DEFAULT. If this is
275 * DEFAULT, BIG is assumed.
276 * @val_format_endian_default: Default endianness for formatted register
277 * values. Used when the regmap_config specifies DEFAULT. If this is
278 * DEFAULT, BIG is assumed.
279 * @async_size: Size of struct used for async work.
283 regmap_hw_write write;
284 regmap_hw_gather_write gather_write;
285 regmap_hw_async_write async_write;
287 regmap_hw_free_context free_context;
288 regmap_hw_async_alloc async_alloc;
290 enum regmap_endian reg_format_endian_default;
291 enum regmap_endian val_format_endian_default;
294 struct regmap *regmap_init(struct device *dev,
295 const struct regmap_bus *bus,
297 const struct regmap_config *config);
298 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
299 const struct regmap_config *config);
300 struct regmap *regmap_init_spi(struct spi_device *dev,
301 const struct regmap_config *config);
302 struct regmap *regmap_init_mmio(struct device *dev,
304 const struct regmap_config *config);
306 struct regmap *devm_regmap_init(struct device *dev,
307 const struct regmap_bus *bus,
309 const struct regmap_config *config);
310 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
311 const struct regmap_config *config);
312 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
313 const struct regmap_config *config);
314 struct regmap *devm_regmap_init_mmio(struct device *dev,
316 const struct regmap_config *config);
318 void regmap_exit(struct regmap *map);
319 int regmap_reinit_cache(struct regmap *map,
320 const struct regmap_config *config);
321 struct regmap *dev_get_regmap(struct device *dev, const char *name);
322 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
323 int regmap_raw_write(struct regmap *map, unsigned int reg,
324 const void *val, size_t val_len);
325 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
327 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
328 const void *val, size_t val_len);
329 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
330 int regmap_raw_read(struct regmap *map, unsigned int reg,
331 void *val, size_t val_len);
332 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
334 int regmap_update_bits(struct regmap *map, unsigned int reg,
335 unsigned int mask, unsigned int val);
336 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
337 unsigned int mask, unsigned int val,
339 int regmap_get_val_bytes(struct regmap *map);
340 int regmap_async_complete(struct regmap *map);
342 int regcache_sync(struct regmap *map);
343 int regcache_sync_region(struct regmap *map, unsigned int min,
345 void regcache_cache_only(struct regmap *map, bool enable);
346 void regcache_cache_bypass(struct regmap *map, bool enable);
347 void regcache_mark_dirty(struct regmap *map);
349 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
352 static inline bool regmap_reg_in_range(unsigned int reg,
353 const struct regmap_range *range)
355 return reg >= range->range_min && reg <= range->range_max;
358 bool regmap_reg_in_ranges(unsigned int reg,
359 const struct regmap_range *ranges,
360 unsigned int nranges);
363 * Description of an IRQ for the generic regmap irq_chip.
365 * @reg_offset: Offset of the status/mask register within the bank
366 * @mask: Mask used to flag/control the register.
369 unsigned int reg_offset;
374 * Description of a generic regmap irq_chip. This is not intended to
375 * handle every possible interrupt controller, but it should handle a
376 * substantial proportion of those that are found in the wild.
378 * @name: Descriptive name for IRQ controller.
380 * @status_base: Base status register address.
381 * @mask_base: Base mask register address.
382 * @ack_base: Base ack address. If zero then the chip is clear on read.
383 * @wake_base: Base address for wake enables. If zero unsupported.
384 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
385 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
387 * @num_regs: Number of registers in each control bank.
388 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
389 * assigned based on the index in the array of the interrupt.
390 * @num_irqs: Number of descriptors.
392 struct regmap_irq_chip {
395 unsigned int status_base;
396 unsigned int mask_base;
397 unsigned int ack_base;
398 unsigned int wake_base;
399 unsigned int irq_reg_stride;
400 unsigned int mask_invert;
405 const struct regmap_irq *irqs;
409 struct regmap_irq_chip_data;
411 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
412 int irq_base, const struct regmap_irq_chip *chip,
413 struct regmap_irq_chip_data **data);
414 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
415 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
416 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
417 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
422 * These stubs should only ever be called by generic code which has
423 * regmap based facilities, if they ever get called at runtime
424 * something is going wrong and something probably needs to select
428 static inline int regmap_write(struct regmap *map, unsigned int reg,
431 WARN_ONCE(1, "regmap API is disabled");
435 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
436 const void *val, size_t val_len)
438 WARN_ONCE(1, "regmap API is disabled");
442 static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
443 const void *val, size_t val_len)
445 WARN_ONCE(1, "regmap API is disabled");
449 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
450 const void *val, size_t val_count)
452 WARN_ONCE(1, "regmap API is disabled");
456 static inline int regmap_read(struct regmap *map, unsigned int reg,
459 WARN_ONCE(1, "regmap API is disabled");
463 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
464 void *val, size_t val_len)
466 WARN_ONCE(1, "regmap API is disabled");
470 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
471 void *val, size_t val_count)
473 WARN_ONCE(1, "regmap API is disabled");
477 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
478 unsigned int mask, unsigned int val)
480 WARN_ONCE(1, "regmap API is disabled");
484 static inline int regmap_update_bits_check(struct regmap *map,
486 unsigned int mask, unsigned int val,
489 WARN_ONCE(1, "regmap API is disabled");
493 static inline int regmap_get_val_bytes(struct regmap *map)
495 WARN_ONCE(1, "regmap API is disabled");
499 static inline int regcache_sync(struct regmap *map)
501 WARN_ONCE(1, "regmap API is disabled");
505 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
508 WARN_ONCE(1, "regmap API is disabled");
512 static inline void regcache_cache_only(struct regmap *map, bool enable)
514 WARN_ONCE(1, "regmap API is disabled");
517 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
519 WARN_ONCE(1, "regmap API is disabled");
522 static inline void regcache_mark_dirty(struct regmap *map)
524 WARN_ONCE(1, "regmap API is disabled");
527 static inline void regmap_async_complete(struct regmap *map)
529 WARN_ONCE(1, "regmap API is disabled");
532 static inline int regmap_register_patch(struct regmap *map,
533 const struct reg_default *regs,
536 WARN_ONCE(1, "regmap API is disabled");
540 static inline struct regmap *dev_get_regmap(struct device *dev,