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>
24 struct regmap_range_cfg;
26 /* An enum of all the supported cache types */
34 * Default value for a register. We use an array of structs rather
35 * than a simple array as many modern devices have very sparse
38 * @reg: Register address.
39 * @def: Register default value.
49 /* Unspecified -> 0 -> Backwards compatible default */
50 REGMAP_ENDIAN_DEFAULT = 0,
57 * A register range, used for access related checks
58 * (readable/writeable/volatile/precious checks)
60 * @range_min: address of first register
61 * @range_max: address of last register
64 unsigned int range_min;
65 unsigned int range_max;
69 * A table of ranges including some yes ranges and some no ranges.
70 * If a register belongs to a no_range, the corresponding check function
71 * will return false. If a register belongs to a yes range, the corresponding
72 * check function will return true. "no_ranges" are searched first.
74 * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
75 * @n_yes_ranges: size of the above array
76 * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
77 * @n_no_ranges: size of the above array
79 struct regmap_access_table {
80 const struct regmap_range *yes_ranges;
81 unsigned int n_yes_ranges;
82 const struct regmap_range *no_ranges;
83 unsigned int n_no_ranges;
86 typedef void (*regmap_lock)(void *);
87 typedef void (*regmap_unlock)(void *);
90 * Configuration for the register map of a device.
92 * @name: Optional name of the regmap. Useful when a device has multiple
95 * @reg_bits: Number of bits in a register address, mandatory.
96 * @reg_stride: The register address stride. Valid register addresses are a
97 * multiple of this value. If set to 0, a value of 1 will be
99 * @pad_bits: Number of bits of padding between register and value.
100 * @val_bits: Number of bits in a register value, mandatory.
102 * @writeable_reg: Optional callback returning true if the register
103 * can be written to. If this field is NULL but wr_table
104 * (see below) is not, the check is performed on such table
105 * (a register is writeable if it belongs to one of the ranges
106 * specified by wr_table).
107 * @readable_reg: Optional callback returning true if the register
108 * can be read from. If this field is NULL but rd_table
109 * (see below) is not, the check is performed on such table
110 * (a register is readable if it belongs to one of the ranges
111 * specified by rd_table).
112 * @volatile_reg: Optional callback returning true if the register
113 * value can't be cached. If this field is NULL but
114 * volatile_table (see below) is not, the check is performed on
115 * such table (a register is volatile if it belongs to one of
116 * the ranges specified by volatile_table).
117 * @precious_reg: Optional callback returning true if the rgister
118 * should not be read outside of a call from the driver
119 * (eg, a clear on read interrupt status register). If this
120 * field is NULL but precious_table (see below) is not, the
121 * check is performed on such table (a register is precious if
122 * it belongs to one of the ranges specified by precious_table).
123 * @lock: Optional lock callback (overrides regmap's default lock
124 * function, based on spinlock or mutex).
125 * @unlock: As above for unlocking.
126 * @lock_arg: this field is passed as the only argument of lock/unlock
127 * functions (ignored in case regular lock/unlock functions
128 * are not overridden).
130 * @max_register: Optional, specifies the maximum valid register index.
131 * @wr_table: Optional, points to a struct regmap_access_table specifying
132 * valid ranges for write access.
133 * @rd_table: As above, for read access.
134 * @volatile_table: As above, for volatile registers.
135 * @precious_table: As above, for precious registers.
136 * @reg_defaults: Power on reset values for registers (for use with
137 * register cache support).
138 * @num_reg_defaults: Number of elements in reg_defaults.
140 * @read_flag_mask: Mask to be set in the top byte of the register when doing
142 * @write_flag_mask: Mask to be set in the top byte of the register when doing
143 * a write. If both read_flag_mask and write_flag_mask are
144 * empty the regmap_bus default masks are used.
145 * @use_single_rw: If set, converts the bulk read and write operations into
146 * a series of single read and write operations. This is useful
147 * for device that does not support bulk read and write.
149 * @cache_type: The actual cache type.
150 * @reg_defaults_raw: Power on reset values for registers (for use with
151 * register cache support).
152 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
153 * @reg_format_endian: Endianness for formatted register addresses. If this is
154 * DEFAULT, the @reg_format_endian_default value from the
155 * regmap bus is used.
156 * @val_format_endian: Endianness for formatted register values. If this is
157 * DEFAULT, the @reg_format_endian_default value from the
158 * regmap bus is used.
160 * @ranges: Array of configuration entries for virtual address ranges.
161 * @num_ranges: Number of range configuration entries.
163 struct regmap_config {
171 bool (*writeable_reg)(struct device *dev, unsigned int reg);
172 bool (*readable_reg)(struct device *dev, unsigned int reg);
173 bool (*volatile_reg)(struct device *dev, unsigned int reg);
174 bool (*precious_reg)(struct device *dev, unsigned int reg);
176 regmap_unlock unlock;
179 unsigned int max_register;
180 const struct regmap_access_table *wr_table;
181 const struct regmap_access_table *rd_table;
182 const struct regmap_access_table *volatile_table;
183 const struct regmap_access_table *precious_table;
184 const struct reg_default *reg_defaults;
185 unsigned int num_reg_defaults;
186 enum regcache_type cache_type;
187 const void *reg_defaults_raw;
188 unsigned int num_reg_defaults_raw;
195 enum regmap_endian reg_format_endian;
196 enum regmap_endian val_format_endian;
198 const struct regmap_range_cfg *ranges;
199 unsigned int num_ranges;
203 * Configuration for indirectly accessed or paged registers.
204 * Registers, mapped to this virtual range, are accessed in two steps:
205 * 1. page selector register update;
206 * 2. access through data window registers.
208 * @name: Descriptive name for diagnostics
210 * @range_min: Address of the lowest register address in virtual range.
211 * @range_max: Address of the highest register in virtual range.
213 * @page_sel_reg: Register with selector field.
214 * @page_sel_mask: Bit shift for selector value.
215 * @page_sel_shift: Bit mask for selector value.
217 * @window_start: Address of first (lowest) register in data window.
218 * @window_len: Number of registers in data window.
220 struct regmap_range_cfg {
223 /* Registers of virtual address range */
224 unsigned int range_min;
225 unsigned int range_max;
227 /* Page selector for indirect addressing */
228 unsigned int selector_reg;
229 unsigned int selector_mask;
232 /* Data window (per each page) */
233 unsigned int window_start;
234 unsigned int window_len;
237 typedef int (*regmap_hw_write)(void *context, const void *data,
239 typedef int (*regmap_hw_gather_write)(void *context,
240 const void *reg, size_t reg_len,
241 const void *val, size_t val_len);
242 typedef int (*regmap_hw_read)(void *context,
243 const void *reg_buf, size_t reg_size,
244 void *val_buf, size_t val_size);
245 typedef void (*regmap_hw_free_context)(void *context);
248 * Description of a hardware bus for the register map infrastructure.
250 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
251 * to perform locking. This field is ignored if custom lock/unlock
252 * functions are used (see fields lock/unlock of
253 * struct regmap_config).
254 * @write: Write operation.
255 * @gather_write: Write operation with split register/value, return -ENOTSUPP
256 * if not implemented on a given device.
257 * @read: Read operation. Data is returned in the buffer used to transmit
259 * @read_flag_mask: Mask to be set in the top byte of the register when doing
261 * @reg_format_endian_default: Default endianness for formatted register
262 * addresses. Used when the regmap_config specifies DEFAULT. If this is
263 * DEFAULT, BIG is assumed.
264 * @val_format_endian_default: Default endianness for formatted register
265 * values. Used when the regmap_config specifies DEFAULT. If this is
266 * DEFAULT, BIG is assumed.
270 regmap_hw_write write;
271 regmap_hw_gather_write gather_write;
273 regmap_hw_free_context free_context;
275 enum regmap_endian reg_format_endian_default;
276 enum regmap_endian val_format_endian_default;
279 struct regmap *regmap_init(struct device *dev,
280 const struct regmap_bus *bus,
282 const struct regmap_config *config);
283 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
284 const struct regmap_config *config);
285 struct regmap *regmap_init_spi(struct spi_device *dev,
286 const struct regmap_config *config);
287 struct regmap *regmap_init_mmio(struct device *dev,
289 const struct regmap_config *config);
291 struct regmap *devm_regmap_init(struct device *dev,
292 const struct regmap_bus *bus,
294 const struct regmap_config *config);
295 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
296 const struct regmap_config *config);
297 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
298 const struct regmap_config *config);
299 struct regmap *devm_regmap_init_mmio(struct device *dev,
301 const struct regmap_config *config);
303 void regmap_exit(struct regmap *map);
304 int regmap_reinit_cache(struct regmap *map,
305 const struct regmap_config *config);
306 struct regmap *dev_get_regmap(struct device *dev, const char *name);
307 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
308 int regmap_raw_write(struct regmap *map, unsigned int reg,
309 const void *val, size_t val_len);
310 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
312 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
313 int regmap_raw_read(struct regmap *map, unsigned int reg,
314 void *val, size_t val_len);
315 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
317 int regmap_update_bits(struct regmap *map, unsigned int reg,
318 unsigned int mask, unsigned int val);
319 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
320 unsigned int mask, unsigned int val,
322 int regmap_get_val_bytes(struct regmap *map);
324 int regcache_sync(struct regmap *map);
325 int regcache_sync_region(struct regmap *map, unsigned int min,
327 void regcache_cache_only(struct regmap *map, bool enable);
328 void regcache_cache_bypass(struct regmap *map, bool enable);
329 void regcache_mark_dirty(struct regmap *map);
331 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
334 static inline bool regmap_reg_in_range(unsigned int reg,
335 const struct regmap_range *range)
337 return reg >= range->range_min && reg <= range->range_max;
340 bool regmap_reg_in_ranges(unsigned int reg,
341 const struct regmap_range *ranges,
342 unsigned int nranges);
345 * Description of an IRQ for the generic regmap irq_chip.
347 * @reg_offset: Offset of the status/mask register within the bank
348 * @mask: Mask used to flag/control the register.
351 unsigned int reg_offset;
356 * Description of a generic regmap irq_chip. This is not intended to
357 * handle every possible interrupt controller, but it should handle a
358 * substantial proportion of those that are found in the wild.
360 * @name: Descriptive name for IRQ controller.
362 * @status_base: Base status register address.
363 * @mask_base: Base mask register address.
364 * @ack_base: Base ack address. If zero then the chip is clear on read.
365 * @wake_base: Base address for wake enables. If zero unsupported.
366 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
367 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
369 * @num_regs: Number of registers in each control bank.
370 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
371 * assigned based on the index in the array of the interrupt.
372 * @num_irqs: Number of descriptors.
374 struct regmap_irq_chip {
377 unsigned int status_base;
378 unsigned int mask_base;
379 unsigned int ack_base;
380 unsigned int wake_base;
381 unsigned int irq_reg_stride;
382 unsigned int mask_invert;
387 const struct regmap_irq *irqs;
391 struct regmap_irq_chip_data;
393 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
394 int irq_base, const struct regmap_irq_chip *chip,
395 struct regmap_irq_chip_data **data);
396 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
397 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
398 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
403 * These stubs should only ever be called by generic code which has
404 * regmap based facilities, if they ever get called at runtime
405 * something is going wrong and something probably needs to select
409 static inline int regmap_write(struct regmap *map, unsigned int reg,
412 WARN_ONCE(1, "regmap API is disabled");
416 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
417 const void *val, size_t val_len)
419 WARN_ONCE(1, "regmap API is disabled");
423 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
424 const void *val, size_t val_count)
426 WARN_ONCE(1, "regmap API is disabled");
430 static inline int regmap_read(struct regmap *map, unsigned int reg,
433 WARN_ONCE(1, "regmap API is disabled");
437 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
438 void *val, size_t val_len)
440 WARN_ONCE(1, "regmap API is disabled");
444 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
445 void *val, size_t val_count)
447 WARN_ONCE(1, "regmap API is disabled");
451 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
452 unsigned int mask, unsigned int val)
454 WARN_ONCE(1, "regmap API is disabled");
458 static inline int regmap_update_bits_check(struct regmap *map,
460 unsigned int mask, unsigned int val,
463 WARN_ONCE(1, "regmap API is disabled");
467 static inline int regmap_get_val_bytes(struct regmap *map)
469 WARN_ONCE(1, "regmap API is disabled");
473 static inline int regcache_sync(struct regmap *map)
475 WARN_ONCE(1, "regmap API is disabled");
479 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
482 WARN_ONCE(1, "regmap API is disabled");
486 static inline void regcache_cache_only(struct regmap *map, bool enable)
488 WARN_ONCE(1, "regmap API is disabled");
491 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
493 WARN_ONCE(1, "regmap API is disabled");
496 static inline void regcache_mark_dirty(struct regmap *map)
498 WARN_ONCE(1, "regmap API is disabled");
501 static inline int regmap_register_patch(struct regmap *map,
502 const struct reg_default *regs,
505 WARN_ONCE(1, "regmap API is disabled");
509 static inline struct regmap *dev_get_regmap(struct device *dev,