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>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/bug.h>
21 #include <linux/lockdep.h>
30 struct regmap_range_cfg;
34 /* An enum of all the supported cache types */
43 * struct reg_default - Default value for a register.
45 * @reg: Register address.
46 * @def: Register default value.
48 * We use an array of structs rather than a simple array as many modern devices
49 * have very sparse register maps.
57 * struct reg_sequence - An individual write from a sequence of writes.
59 * @reg: Register address.
60 * @def: Register value.
61 * @delay_us: Delay to be applied after the register write in microseconds
63 * Register/value pairs for sequences of writes with an optional delay in
64 * microseconds to be applied after each write.
69 unsigned int delay_us;
72 #define regmap_update_bits(map, reg, mask, val) \
73 regmap_update_bits_base(map, reg, mask, val, NULL, false, false)
74 #define regmap_update_bits_async(map, reg, mask, val)\
75 regmap_update_bits_base(map, reg, mask, val, NULL, true, false)
76 #define regmap_update_bits_check(map, reg, mask, val, change)\
77 regmap_update_bits_base(map, reg, mask, val, change, false, false)
78 #define regmap_update_bits_check_async(map, reg, mask, val, change)\
79 regmap_update_bits_base(map, reg, mask, val, change, true, false)
81 #define regmap_write_bits(map, reg, mask, val) \
82 regmap_update_bits_base(map, reg, mask, val, NULL, false, true)
84 #define regmap_field_write(field, val) \
85 regmap_field_update_bits_base(field, ~0, val, NULL, false, false)
86 #define regmap_field_force_write(field, val) \
87 regmap_field_update_bits_base(field, ~0, val, NULL, false, true)
88 #define regmap_field_update_bits(field, mask, val)\
89 regmap_field_update_bits_base(field, mask, val, NULL, false, false)
90 #define regmap_field_force_update_bits(field, mask, val) \
91 regmap_field_update_bits_base(field, mask, val, NULL, false, true)
93 #define regmap_fields_write(field, id, val) \
94 regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, false)
95 #define regmap_fields_force_write(field, id, val) \
96 regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, true)
97 #define regmap_fields_update_bits(field, id, mask, val)\
98 regmap_fields_update_bits_base(field, id, mask, val, NULL, false, false)
99 #define regmap_fields_force_update_bits(field, id, mask, val) \
100 regmap_fields_update_bits_base(field, id, mask, val, NULL, false, true)
103 * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
105 * @map: Regmap to read from
106 * @addr: Address to poll
107 * @val: Unsigned integer variable to read the value into
108 * @cond: Break condition (usually involving @val)
109 * @sleep_us: Maximum time to sleep between reads in us (0
110 * tight-loops). Should be less than ~20ms since usleep_range
111 * is used (see Documentation/timers/timers-howto.txt).
112 * @timeout_us: Timeout in us, 0 means never timeout
114 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
115 * error return value in case of a error read. In the two former cases,
116 * the last read value at @addr is stored in @val. Must not be called
117 * from atomic context if sleep_us or timeout_us are used.
119 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
121 #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
123 ktime_t __timeout = ktime_add_us(ktime_get(), timeout_us); \
125 might_sleep_if(sleep_us); \
127 __ret = regmap_read((map), (addr), &(val)); \
132 if ((timeout_us) && \
133 ktime_compare(ktime_get(), __timeout) > 0) { \
134 __ret = regmap_read((map), (addr), &(val)); \
138 usleep_range(((sleep_us) >> 2) + 1, sleep_us); \
140 __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
144 * regmap_field_read_poll_timeout - Poll until a condition is met or timeout
146 * @field: Regmap field to read from
147 * @val: Unsigned integer variable to read the value into
148 * @cond: Break condition (usually involving @val)
149 * @sleep_us: Maximum time to sleep between reads in us (0
150 * tight-loops). Should be less than ~20ms since usleep_range
151 * is used (see Documentation/timers/timers-howto.txt).
152 * @timeout_us: Timeout in us, 0 means never timeout
154 * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_field_read
155 * error return value in case of a error read. In the two former cases,
156 * the last read value at @addr is stored in @val. Must not be called
157 * from atomic context if sleep_us or timeout_us are used.
159 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
161 #define regmap_field_read_poll_timeout(field, val, cond, sleep_us, timeout_us) \
163 ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
165 might_sleep_if(sleep_us); \
167 pollret = regmap_field_read((field), &(val)); \
172 if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
173 pollret = regmap_field_read((field), &(val)); \
177 usleep_range((sleep_us >> 2) + 1, sleep_us); \
179 pollret ?: ((cond) ? 0 : -ETIMEDOUT); \
185 /* Unspecified -> 0 -> Backwards compatible default */
186 REGMAP_ENDIAN_DEFAULT = 0,
188 REGMAP_ENDIAN_LITTLE,
189 REGMAP_ENDIAN_NATIVE,
193 * struct regmap_range - A register range, used for access related checks
194 * (readable/writeable/volatile/precious checks)
196 * @range_min: address of first register
197 * @range_max: address of last register
199 struct regmap_range {
200 unsigned int range_min;
201 unsigned int range_max;
204 #define regmap_reg_range(low, high) { .range_min = low, .range_max = high, }
207 * struct regmap_access_table - A table of register ranges for access checks
209 * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
210 * @n_yes_ranges: size of the above array
211 * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
212 * @n_no_ranges: size of the above array
214 * A table of ranges including some yes ranges and some no ranges.
215 * If a register belongs to a no_range, the corresponding check function
216 * will return false. If a register belongs to a yes range, the corresponding
217 * check function will return true. "no_ranges" are searched first.
219 struct regmap_access_table {
220 const struct regmap_range *yes_ranges;
221 unsigned int n_yes_ranges;
222 const struct regmap_range *no_ranges;
223 unsigned int n_no_ranges;
226 typedef void (*regmap_lock)(void *);
227 typedef void (*regmap_unlock)(void *);
230 * struct regmap_config - Configuration for the register map of a device.
232 * @name: Optional name of the regmap. Useful when a device has multiple
235 * @reg_bits: Number of bits in a register address, mandatory.
236 * @reg_stride: The register address stride. Valid register addresses are a
237 * multiple of this value. If set to 0, a value of 1 will be
239 * @pad_bits: Number of bits of padding between register and value.
240 * @val_bits: Number of bits in a register value, mandatory.
242 * @writeable_reg: Optional callback returning true if the register
243 * can be written to. If this field is NULL but wr_table
244 * (see below) is not, the check is performed on such table
245 * (a register is writeable if it belongs to one of the ranges
246 * specified by wr_table).
247 * @readable_reg: Optional callback returning true if the register
248 * can be read from. If this field is NULL but rd_table
249 * (see below) is not, the check is performed on such table
250 * (a register is readable if it belongs to one of the ranges
251 * specified by rd_table).
252 * @volatile_reg: Optional callback returning true if the register
253 * value can't be cached. If this field is NULL but
254 * volatile_table (see below) is not, the check is performed on
255 * such table (a register is volatile if it belongs to one of
256 * the ranges specified by volatile_table).
257 * @precious_reg: Optional callback returning true if the register
258 * should not be read outside of a call from the driver
259 * (e.g., a clear on read interrupt status register). If this
260 * field is NULL but precious_table (see below) is not, the
261 * check is performed on such table (a register is precious if
262 * it belongs to one of the ranges specified by precious_table).
263 * @lock: Optional lock callback (overrides regmap's default lock
264 * function, based on spinlock or mutex).
265 * @unlock: As above for unlocking.
266 * @lock_arg: this field is passed as the only argument of lock/unlock
267 * functions (ignored in case regular lock/unlock functions
268 * are not overridden).
269 * @reg_read: Optional callback that if filled will be used to perform
270 * all the reads from the registers. Should only be provided for
271 * devices whose read operation cannot be represented as a simple
272 * read operation on a bus such as SPI, I2C, etc. Most of the
273 * devices do not need this.
274 * @reg_write: Same as above for writing.
275 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
276 * to perform locking. This field is ignored if custom lock/unlock
277 * functions are used (see fields lock/unlock of struct regmap_config).
278 * This field is a duplicate of a similar file in
279 * 'struct regmap_bus' and serves exact same purpose.
280 * Use it only for "no-bus" cases.
281 * @max_register: Optional, specifies the maximum valid register address.
282 * @wr_table: Optional, points to a struct regmap_access_table specifying
283 * valid ranges for write access.
284 * @rd_table: As above, for read access.
285 * @volatile_table: As above, for volatile registers.
286 * @precious_table: As above, for precious registers.
287 * @reg_defaults: Power on reset values for registers (for use with
288 * register cache support).
289 * @num_reg_defaults: Number of elements in reg_defaults.
291 * @read_flag_mask: Mask to be set in the top bytes of the register when doing
293 * @write_flag_mask: Mask to be set in the top bytes of the register when doing
294 * a write. If both read_flag_mask and write_flag_mask are
295 * empty the regmap_bus default masks are used.
296 * @use_single_rw: If set, converts the bulk read and write operations into
297 * a series of single read and write operations. This is useful
298 * for device that does not support bulk read and write.
299 * @can_multi_write: If set, the device supports the multi write mode of bulk
300 * write operations, if clear multi write requests will be
301 * split into individual write operations
303 * @cache_type: The actual cache type.
304 * @reg_defaults_raw: Power on reset values for registers (for use with
305 * register cache support).
306 * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
307 * @reg_format_endian: Endianness for formatted register addresses. If this is
308 * DEFAULT, the @reg_format_endian_default value from the
309 * regmap bus is used.
310 * @val_format_endian: Endianness for formatted register values. If this is
311 * DEFAULT, the @reg_format_endian_default value from the
312 * regmap bus is used.
314 * @ranges: Array of configuration entries for virtual address ranges.
315 * @num_ranges: Number of range configuration entries.
317 struct regmap_config {
325 bool (*writeable_reg)(struct device *dev, unsigned int reg);
326 bool (*readable_reg)(struct device *dev, unsigned int reg);
327 bool (*volatile_reg)(struct device *dev, unsigned int reg);
328 bool (*precious_reg)(struct device *dev, unsigned int reg);
330 regmap_unlock unlock;
333 int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
334 int (*reg_write)(void *context, unsigned int reg, unsigned int val);
338 unsigned int max_register;
339 const struct regmap_access_table *wr_table;
340 const struct regmap_access_table *rd_table;
341 const struct regmap_access_table *volatile_table;
342 const struct regmap_access_table *precious_table;
343 const struct reg_default *reg_defaults;
344 unsigned int num_reg_defaults;
345 enum regcache_type cache_type;
346 const void *reg_defaults_raw;
347 unsigned int num_reg_defaults_raw;
349 unsigned long read_flag_mask;
350 unsigned long write_flag_mask;
353 bool can_multi_write;
355 enum regmap_endian reg_format_endian;
356 enum regmap_endian val_format_endian;
358 const struct regmap_range_cfg *ranges;
359 unsigned int num_ranges;
363 * struct regmap_range_cfg - Configuration for indirectly accessed or paged
366 * @name: Descriptive name for diagnostics
368 * @range_min: Address of the lowest register address in virtual range.
369 * @range_max: Address of the highest register in virtual range.
371 * @selector_reg: Register with selector field.
372 * @selector_mask: Bit shift for selector value.
373 * @selector_shift: Bit mask for selector value.
375 * @window_start: Address of first (lowest) register in data window.
376 * @window_len: Number of registers in data window.
378 * Registers, mapped to this virtual range, are accessed in two steps:
379 * 1. page selector register update;
380 * 2. access through data window registers.
382 struct regmap_range_cfg {
385 /* Registers of virtual address range */
386 unsigned int range_min;
387 unsigned int range_max;
389 /* Page selector for indirect addressing */
390 unsigned int selector_reg;
391 unsigned int selector_mask;
394 /* Data window (per each page) */
395 unsigned int window_start;
396 unsigned int window_len;
401 typedef int (*regmap_hw_write)(void *context, const void *data,
403 typedef int (*regmap_hw_gather_write)(void *context,
404 const void *reg, size_t reg_len,
405 const void *val, size_t val_len);
406 typedef int (*regmap_hw_async_write)(void *context,
407 const void *reg, size_t reg_len,
408 const void *val, size_t val_len,
409 struct regmap_async *async);
410 typedef int (*regmap_hw_read)(void *context,
411 const void *reg_buf, size_t reg_size,
412 void *val_buf, size_t val_size);
413 typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
415 typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
417 typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
418 unsigned int mask, unsigned int val);
419 typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
420 typedef void (*regmap_hw_free_context)(void *context);
423 * struct regmap_bus - Description of a hardware bus for the register map
426 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
427 * to perform locking. This field is ignored if custom lock/unlock
428 * functions are used (see fields lock/unlock of
429 * struct regmap_config).
430 * @write: Write operation.
431 * @gather_write: Write operation with split register/value, return -ENOTSUPP
432 * if not implemented on a given device.
433 * @async_write: Write operation which completes asynchronously, optional and
434 * must serialise with respect to non-async I/O.
435 * @reg_write: Write a single register value to the given register address. This
436 * write operation has to complete when returning from the function.
437 * @reg_update_bits: Update bits operation to be used against volatile
438 * registers, intended for devices supporting some mechanism
439 * for setting clearing bits without having to
441 * @read: Read operation. Data is returned in the buffer used to transmit
443 * @reg_read: Read a single register value from a given register address.
444 * @free_context: Free context.
445 * @async_alloc: Allocate a regmap_async() structure.
446 * @read_flag_mask: Mask to be set in the top byte of the register when doing
448 * @reg_format_endian_default: Default endianness for formatted register
449 * addresses. Used when the regmap_config specifies DEFAULT. If this is
450 * DEFAULT, BIG is assumed.
451 * @val_format_endian_default: Default endianness for formatted register
452 * values. Used when the regmap_config specifies DEFAULT. If this is
453 * DEFAULT, BIG is assumed.
454 * @max_raw_read: Max raw read size that can be used on the bus.
455 * @max_raw_write: Max raw write size that can be used on the bus.
459 regmap_hw_write write;
460 regmap_hw_gather_write gather_write;
461 regmap_hw_async_write async_write;
462 regmap_hw_reg_write reg_write;
463 regmap_hw_reg_update_bits reg_update_bits;
465 regmap_hw_reg_read reg_read;
466 regmap_hw_free_context free_context;
467 regmap_hw_async_alloc async_alloc;
469 enum regmap_endian reg_format_endian_default;
470 enum regmap_endian val_format_endian_default;
472 size_t max_raw_write;
476 * __regmap_init functions.
478 * These functions take a lock key and name parameter, and should not be called
479 * directly. Instead, use the regmap_init macros that generate a key and name
482 struct regmap *__regmap_init(struct device *dev,
483 const struct regmap_bus *bus,
485 const struct regmap_config *config,
486 struct lock_class_key *lock_key,
487 const char *lock_name);
488 struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
489 const struct regmap_config *config,
490 struct lock_class_key *lock_key,
491 const char *lock_name);
492 struct regmap *__regmap_init_spi(struct spi_device *dev,
493 const struct regmap_config *config,
494 struct lock_class_key *lock_key,
495 const char *lock_name);
496 struct regmap *__regmap_init_spmi_base(struct spmi_device *dev,
497 const struct regmap_config *config,
498 struct lock_class_key *lock_key,
499 const char *lock_name);
500 struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
501 const struct regmap_config *config,
502 struct lock_class_key *lock_key,
503 const char *lock_name);
504 struct regmap *__regmap_init_w1(struct device *w1_dev,
505 const struct regmap_config *config,
506 struct lock_class_key *lock_key,
507 const char *lock_name);
508 struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
510 const struct regmap_config *config,
511 struct lock_class_key *lock_key,
512 const char *lock_name);
513 struct regmap *__regmap_init_ac97(struct snd_ac97 *ac97,
514 const struct regmap_config *config,
515 struct lock_class_key *lock_key,
516 const char *lock_name);
518 struct regmap *__devm_regmap_init(struct device *dev,
519 const struct regmap_bus *bus,
521 const struct regmap_config *config,
522 struct lock_class_key *lock_key,
523 const char *lock_name);
524 struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
525 const struct regmap_config *config,
526 struct lock_class_key *lock_key,
527 const char *lock_name);
528 struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
529 const struct regmap_config *config,
530 struct lock_class_key *lock_key,
531 const char *lock_name);
532 struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *dev,
533 const struct regmap_config *config,
534 struct lock_class_key *lock_key,
535 const char *lock_name);
536 struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
537 const struct regmap_config *config,
538 struct lock_class_key *lock_key,
539 const char *lock_name);
540 struct regmap *__devm_regmap_init_w1(struct device *w1_dev,
541 const struct regmap_config *config,
542 struct lock_class_key *lock_key,
543 const char *lock_name);
544 struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
547 const struct regmap_config *config,
548 struct lock_class_key *lock_key,
549 const char *lock_name);
550 struct regmap *__devm_regmap_init_ac97(struct snd_ac97 *ac97,
551 const struct regmap_config *config,
552 struct lock_class_key *lock_key,
553 const char *lock_name);
556 * Wrapper for regmap_init macros to include a unique lockdep key and name
557 * for each call. No-op if CONFIG_LOCKDEP is not set.
559 * @fn: Real function to call (in the form __[*_]regmap_init[_*])
560 * @name: Config variable name (#config in the calling macro)
562 #ifdef CONFIG_LOCKDEP
563 #define __regmap_lockdep_wrapper(fn, name, ...) \
566 static struct lock_class_key _key; \
567 fn(__VA_ARGS__, &_key, \
568 KBUILD_BASENAME ":" \
569 __stringify(__LINE__) ":" \
570 "(" name ")->lock"); \
574 #define __regmap_lockdep_wrapper(fn, name, ...) fn(__VA_ARGS__, NULL, NULL)
578 * regmap_init() - Initialise register map
580 * @dev: Device that will be interacted with
581 * @bus: Bus-specific callbacks to use with device
582 * @bus_context: Data passed to bus-specific callbacks
583 * @config: Configuration for register map
585 * The return value will be an ERR_PTR() on error or a valid pointer to
586 * a struct regmap. This function should generally not be called
587 * directly, it should be called by bus-specific init functions.
589 #define regmap_init(dev, bus, bus_context, config) \
590 __regmap_lockdep_wrapper(__regmap_init, #config, \
591 dev, bus, bus_context, config)
592 int regmap_attach_dev(struct device *dev, struct regmap *map,
593 const struct regmap_config *config);
596 * regmap_init_i2c() - Initialise register map
598 * @i2c: Device that will be interacted with
599 * @config: Configuration for register map
601 * The return value will be an ERR_PTR() on error or a valid pointer to
604 #define regmap_init_i2c(i2c, config) \
605 __regmap_lockdep_wrapper(__regmap_init_i2c, #config, \
609 * regmap_init_spi() - Initialise register map
611 * @dev: Device that will be interacted with
612 * @config: Configuration for register map
614 * The return value will be an ERR_PTR() on error or a valid pointer to
617 #define regmap_init_spi(dev, config) \
618 __regmap_lockdep_wrapper(__regmap_init_spi, #config, \
622 * regmap_init_spmi_base() - Create regmap for the Base register space
624 * @dev: SPMI device that will be interacted with
625 * @config: Configuration for register map
627 * The return value will be an ERR_PTR() on error or a valid pointer to
630 #define regmap_init_spmi_base(dev, config) \
631 __regmap_lockdep_wrapper(__regmap_init_spmi_base, #config, \
635 * regmap_init_spmi_ext() - Create regmap for Ext register space
637 * @dev: Device that will be interacted with
638 * @config: Configuration for register map
640 * The return value will be an ERR_PTR() on error or a valid pointer to
643 #define regmap_init_spmi_ext(dev, config) \
644 __regmap_lockdep_wrapper(__regmap_init_spmi_ext, #config, \
648 * regmap_init_w1() - Initialise register map
650 * @w1_dev: Device that will be interacted with
651 * @config: Configuration for register map
653 * The return value will be an ERR_PTR() on error or a valid pointer to
656 #define regmap_init_w1(w1_dev, config) \
657 __regmap_lockdep_wrapper(__regmap_init_w1, #config, \
661 * regmap_init_mmio_clk() - Initialise register map with register clock
663 * @dev: Device that will be interacted with
664 * @clk_id: register clock consumer ID
665 * @regs: Pointer to memory-mapped IO region
666 * @config: Configuration for register map
668 * The return value will be an ERR_PTR() on error or a valid pointer to
671 #define regmap_init_mmio_clk(dev, clk_id, regs, config) \
672 __regmap_lockdep_wrapper(__regmap_init_mmio_clk, #config, \
673 dev, clk_id, regs, config)
676 * regmap_init_mmio() - Initialise register map
678 * @dev: Device that will be interacted with
679 * @regs: Pointer to memory-mapped IO region
680 * @config: Configuration for register map
682 * The return value will be an ERR_PTR() on error or a valid pointer to
685 #define regmap_init_mmio(dev, regs, config) \
686 regmap_init_mmio_clk(dev, NULL, regs, config)
689 * regmap_init_ac97() - Initialise AC'97 register map
691 * @ac97: Device that will be interacted with
692 * @config: Configuration for register map
694 * The return value will be an ERR_PTR() on error or a valid pointer to
697 #define regmap_init_ac97(ac97, config) \
698 __regmap_lockdep_wrapper(__regmap_init_ac97, #config, \
700 bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
703 * devm_regmap_init() - Initialise managed register map
705 * @dev: Device that will be interacted with
706 * @bus: Bus-specific callbacks to use with device
707 * @bus_context: Data passed to bus-specific callbacks
708 * @config: Configuration for register map
710 * The return value will be an ERR_PTR() on error or a valid pointer
711 * to a struct regmap. This function should generally not be called
712 * directly, it should be called by bus-specific init functions. The
713 * map will be automatically freed by the device management code.
715 #define devm_regmap_init(dev, bus, bus_context, config) \
716 __regmap_lockdep_wrapper(__devm_regmap_init, #config, \
717 dev, bus, bus_context, config)
720 * devm_regmap_init_i2c() - Initialise managed register map
722 * @i2c: Device that will be interacted with
723 * @config: Configuration for register map
725 * The return value will be an ERR_PTR() on error or a valid pointer
726 * to a struct regmap. The regmap will be automatically freed by the
727 * device management code.
729 #define devm_regmap_init_i2c(i2c, config) \
730 __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config, \
734 * devm_regmap_init_spi() - Initialise register map
736 * @dev: Device that will be interacted with
737 * @config: Configuration for register map
739 * The return value will be an ERR_PTR() on error or a valid pointer
740 * to a struct regmap. The map will be automatically freed by the
741 * device management code.
743 #define devm_regmap_init_spi(dev, config) \
744 __regmap_lockdep_wrapper(__devm_regmap_init_spi, #config, \
748 * devm_regmap_init_spmi_base() - Create managed regmap for Base register space
750 * @dev: SPMI device that will be interacted with
751 * @config: Configuration for register map
753 * The return value will be an ERR_PTR() on error or a valid pointer
754 * to a struct regmap. The regmap will be automatically freed by the
755 * device management code.
757 #define devm_regmap_init_spmi_base(dev, config) \
758 __regmap_lockdep_wrapper(__devm_regmap_init_spmi_base, #config, \
762 * devm_regmap_init_spmi_ext() - Create managed regmap for Ext register space
764 * @dev: SPMI device that will be interacted with
765 * @config: Configuration for register map
767 * The return value will be an ERR_PTR() on error or a valid pointer
768 * to a struct regmap. The regmap will be automatically freed by the
769 * device management code.
771 #define devm_regmap_init_spmi_ext(dev, config) \
772 __regmap_lockdep_wrapper(__devm_regmap_init_spmi_ext, #config, \
776 * devm_regmap_init_w1() - Initialise managed register map
778 * @w1_dev: Device that will be interacted with
779 * @config: Configuration for register map
781 * The return value will be an ERR_PTR() on error or a valid pointer
782 * to a struct regmap. The regmap will be automatically freed by the
783 * device management code.
785 #define devm_regmap_init_w1(w1_dev, config) \
786 __regmap_lockdep_wrapper(__devm_regmap_init_w1, #config, \
789 * devm_regmap_init_mmio_clk() - Initialise managed register map with clock
791 * @dev: Device that will be interacted with
792 * @clk_id: register clock consumer ID
793 * @regs: Pointer to memory-mapped IO region
794 * @config: Configuration for register map
796 * The return value will be an ERR_PTR() on error or a valid pointer
797 * to a struct regmap. The regmap will be automatically freed by the
798 * device management code.
800 #define devm_regmap_init_mmio_clk(dev, clk_id, regs, config) \
801 __regmap_lockdep_wrapper(__devm_regmap_init_mmio_clk, #config, \
802 dev, clk_id, regs, config)
805 * devm_regmap_init_mmio() - Initialise managed register map
807 * @dev: Device that will be interacted with
808 * @regs: Pointer to memory-mapped IO region
809 * @config: Configuration for register map
811 * The return value will be an ERR_PTR() on error or a valid pointer
812 * to a struct regmap. The regmap will be automatically freed by the
813 * device management code.
815 #define devm_regmap_init_mmio(dev, regs, config) \
816 devm_regmap_init_mmio_clk(dev, NULL, regs, config)
819 * devm_regmap_init_ac97() - Initialise AC'97 register map
821 * @ac97: Device that will be interacted with
822 * @config: Configuration for register map
824 * The return value will be an ERR_PTR() on error or a valid pointer
825 * to a struct regmap. The regmap will be automatically freed by the
826 * device management code.
828 #define devm_regmap_init_ac97(ac97, config) \
829 __regmap_lockdep_wrapper(__devm_regmap_init_ac97, #config, \
832 void regmap_exit(struct regmap *map);
833 int regmap_reinit_cache(struct regmap *map,
834 const struct regmap_config *config);
835 struct regmap *dev_get_regmap(struct device *dev, const char *name);
836 struct device *regmap_get_device(struct regmap *map);
837 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
838 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
839 int regmap_raw_write(struct regmap *map, unsigned int reg,
840 const void *val, size_t val_len);
841 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
843 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
845 int regmap_multi_reg_write_bypassed(struct regmap *map,
846 const struct reg_sequence *regs,
848 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
849 const void *val, size_t val_len);
850 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
851 int regmap_raw_read(struct regmap *map, unsigned int reg,
852 void *val, size_t val_len);
853 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
855 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
856 unsigned int mask, unsigned int val,
857 bool *change, bool async, bool force);
858 int regmap_get_val_bytes(struct regmap *map);
859 int regmap_get_max_register(struct regmap *map);
860 int regmap_get_reg_stride(struct regmap *map);
861 int regmap_async_complete(struct regmap *map);
862 bool regmap_can_raw_write(struct regmap *map);
863 size_t regmap_get_raw_read_max(struct regmap *map);
864 size_t regmap_get_raw_write_max(struct regmap *map);
866 int regcache_sync(struct regmap *map);
867 int regcache_sync_region(struct regmap *map, unsigned int min,
869 int regcache_drop_region(struct regmap *map, unsigned int min,
871 void regcache_cache_only(struct regmap *map, bool enable);
872 void regcache_cache_bypass(struct regmap *map, bool enable);
873 void regcache_mark_dirty(struct regmap *map);
875 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
876 const struct regmap_access_table *table);
878 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
880 int regmap_parse_val(struct regmap *map, const void *buf,
883 static inline bool regmap_reg_in_range(unsigned int reg,
884 const struct regmap_range *range)
886 return reg >= range->range_min && reg <= range->range_max;
889 bool regmap_reg_in_ranges(unsigned int reg,
890 const struct regmap_range *ranges,
891 unsigned int nranges);
894 * struct reg_field - Description of an register field
896 * @reg: Offset of the register within the regmap bank
897 * @lsb: lsb of the register field.
898 * @msb: msb of the register field.
899 * @id_size: port size if it has some ports
900 * @id_offset: address offset for each ports
906 unsigned int id_size;
907 unsigned int id_offset;
910 #define REG_FIELD(_reg, _lsb, _msb) { \
916 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
917 struct reg_field reg_field);
918 void regmap_field_free(struct regmap_field *field);
920 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
921 struct regmap *regmap, struct reg_field reg_field);
922 void devm_regmap_field_free(struct device *dev, struct regmap_field *field);
924 int regmap_field_read(struct regmap_field *field, unsigned int *val);
925 int regmap_field_update_bits_base(struct regmap_field *field,
926 unsigned int mask, unsigned int val,
927 bool *change, bool async, bool force);
928 int regmap_fields_read(struct regmap_field *field, unsigned int id,
930 int regmap_fields_update_bits_base(struct regmap_field *field, unsigned int id,
931 unsigned int mask, unsigned int val,
932 bool *change, bool async, bool force);
935 * struct regmap_irq - Description of an IRQ for the generic regmap irq_chip.
937 * @reg_offset: Offset of the status/mask register within the bank
938 * @mask: Mask used to flag/control the register.
939 * @type_reg_offset: Offset register for the irq type setting.
940 * @type_rising_mask: Mask bit to configure RISING type irq.
941 * @type_falling_mask: Mask bit to configure FALLING type irq.
944 unsigned int reg_offset;
946 unsigned int type_reg_offset;
947 unsigned int type_rising_mask;
948 unsigned int type_falling_mask;
951 #define REGMAP_IRQ_REG(_irq, _off, _mask) \
952 [_irq] = { .reg_offset = (_off), .mask = (_mask) }
955 * struct regmap_irq_chip - Description of a generic regmap irq_chip.
957 * @name: Descriptive name for IRQ controller.
959 * @status_base: Base status register address.
960 * @mask_base: Base mask register address.
961 * @mask_writeonly: Base mask register is write only.
962 * @unmask_base: Base unmask register address. for chips who have
963 * separate mask and unmask registers
964 * @ack_base: Base ack address. If zero then the chip is clear on read.
965 * Using zero value is possible with @use_ack bit.
966 * @wake_base: Base address for wake enables. If zero unsupported.
967 * @type_base: Base address for irq type. If zero unsupported.
968 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
969 * @init_ack_masked: Ack all masked interrupts once during initalization.
970 * @mask_invert: Inverted mask register: cleared bits are masked out.
971 * @use_ack: Use @ack register even if it is zero.
972 * @ack_invert: Inverted ack register: cleared bits for ack.
973 * @wake_invert: Inverted wake register: cleared bits are wake enabled.
974 * @type_invert: Invert the type flags.
975 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
977 * @num_regs: Number of registers in each control bank.
978 * @irqs: Descriptors for individual IRQs. Interrupt numbers are
979 * assigned based on the index in the array of the interrupt.
980 * @num_irqs: Number of descriptors.
981 * @num_type_reg: Number of type registers.
982 * @type_reg_stride: Stride to use for chips where type registers are not
984 * @handle_pre_irq: Driver specific callback to handle interrupt from device
985 * before regmap_irq_handler process the interrupts.
986 * @handle_post_irq: Driver specific callback to handle interrupt from device
987 * after handling the interrupts in regmap_irq_handler().
988 * @irq_drv_data: Driver specific IRQ data which is passed as parameter when
989 * driver specific pre/post interrupt handler is called.
991 * This is not intended to handle every possible interrupt controller, but
992 * it should handle a substantial proportion of those that are found in the
995 struct regmap_irq_chip {
998 unsigned int status_base;
999 unsigned int mask_base;
1000 unsigned int unmask_base;
1001 unsigned int ack_base;
1002 unsigned int wake_base;
1003 unsigned int type_base;
1004 unsigned int irq_reg_stride;
1005 bool mask_writeonly:1;
1006 bool init_ack_masked:1;
1016 const struct regmap_irq *irqs;
1020 unsigned int type_reg_stride;
1022 int (*handle_pre_irq)(void *irq_drv_data);
1023 int (*handle_post_irq)(void *irq_drv_data);
1027 struct regmap_irq_chip_data;
1029 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
1030 int irq_base, const struct regmap_irq_chip *chip,
1031 struct regmap_irq_chip_data **data);
1032 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
1034 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
1035 int irq_flags, int irq_base,
1036 const struct regmap_irq_chip *chip,
1037 struct regmap_irq_chip_data **data);
1038 void devm_regmap_del_irq_chip(struct device *dev, int irq,
1039 struct regmap_irq_chip_data *data);
1041 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
1042 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
1043 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
1048 * These stubs should only ever be called by generic code which has
1049 * regmap based facilities, if they ever get called at runtime
1050 * something is going wrong and something probably needs to select
1054 static inline int regmap_write(struct regmap *map, unsigned int reg,
1057 WARN_ONCE(1, "regmap API is disabled");
1061 static inline int regmap_write_async(struct regmap *map, unsigned int reg,
1064 WARN_ONCE(1, "regmap API is disabled");
1068 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
1069 const void *val, size_t val_len)
1071 WARN_ONCE(1, "regmap API is disabled");
1075 static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1076 const void *val, size_t val_len)
1078 WARN_ONCE(1, "regmap API is disabled");
1082 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
1083 const void *val, size_t val_count)
1085 WARN_ONCE(1, "regmap API is disabled");
1089 static inline int regmap_read(struct regmap *map, unsigned int reg,
1092 WARN_ONCE(1, "regmap API is disabled");
1096 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
1097 void *val, size_t val_len)
1099 WARN_ONCE(1, "regmap API is disabled");
1103 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
1104 void *val, size_t val_count)
1106 WARN_ONCE(1, "regmap API is disabled");
1110 static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1111 unsigned int mask, unsigned int val,
1112 bool *change, bool async, bool force)
1114 WARN_ONCE(1, "regmap API is disabled");
1118 static inline int regmap_field_update_bits_base(struct regmap_field *field,
1119 unsigned int mask, unsigned int val,
1120 bool *change, bool async, bool force)
1122 WARN_ONCE(1, "regmap API is disabled");
1126 static inline int regmap_fields_update_bits_base(struct regmap_field *field,
1128 unsigned int mask, unsigned int val,
1129 bool *change, bool async, bool force)
1131 WARN_ONCE(1, "regmap API is disabled");
1135 static inline int regmap_get_val_bytes(struct regmap *map)
1137 WARN_ONCE(1, "regmap API is disabled");
1141 static inline int regmap_get_max_register(struct regmap *map)
1143 WARN_ONCE(1, "regmap API is disabled");
1147 static inline int regmap_get_reg_stride(struct regmap *map)
1149 WARN_ONCE(1, "regmap API is disabled");
1153 static inline int regcache_sync(struct regmap *map)
1155 WARN_ONCE(1, "regmap API is disabled");
1159 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
1162 WARN_ONCE(1, "regmap API is disabled");
1166 static inline int regcache_drop_region(struct regmap *map, unsigned int min,
1169 WARN_ONCE(1, "regmap API is disabled");
1173 static inline void regcache_cache_only(struct regmap *map, bool enable)
1175 WARN_ONCE(1, "regmap API is disabled");
1178 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
1180 WARN_ONCE(1, "regmap API is disabled");
1183 static inline void regcache_mark_dirty(struct regmap *map)
1185 WARN_ONCE(1, "regmap API is disabled");
1188 static inline void regmap_async_complete(struct regmap *map)
1190 WARN_ONCE(1, "regmap API is disabled");
1193 static inline int regmap_register_patch(struct regmap *map,
1194 const struct reg_sequence *regs,
1197 WARN_ONCE(1, "regmap API is disabled");
1201 static inline int regmap_parse_val(struct regmap *map, const void *buf,
1204 WARN_ONCE(1, "regmap API is disabled");
1208 static inline struct regmap *dev_get_regmap(struct device *dev,
1214 static inline struct device *regmap_get_device(struct regmap *map)
1216 WARN_ONCE(1, "regmap API is disabled");