regmap: teach regmap to use raw spinlocks if requested in the config
[platform/kernel/linux-rpi.git] / include / linux / regmap.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef __LINUX_REGMAP_H
3 #define __LINUX_REGMAP_H
4
5 /*
6  * Register map access API
7  *
8  * Copyright 2011 Wolfson Microelectronics plc
9  *
10  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11  */
12
13 #include <linux/list.h>
14 #include <linux/rbtree.h>
15 #include <linux/ktime.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/bug.h>
19 #include <linux/lockdep.h>
20 #include <linux/iopoll.h>
21 #include <linux/fwnode.h>
22
23 struct module;
24 struct clk;
25 struct device;
26 struct device_node;
27 struct i2c_client;
28 struct i3c_device;
29 struct irq_domain;
30 struct slim_device;
31 struct spi_device;
32 struct spmi_device;
33 struct regmap;
34 struct regmap_range_cfg;
35 struct regmap_field;
36 struct snd_ac97;
37 struct sdw_slave;
38
39 /* An enum of all the supported cache types */
40 enum regcache_type {
41         REGCACHE_NONE,
42         REGCACHE_RBTREE,
43         REGCACHE_COMPRESSED,
44         REGCACHE_FLAT,
45 };
46
47 /**
48  * struct reg_default - Default value for a register.
49  *
50  * @reg: Register address.
51  * @def: Register default value.
52  *
53  * We use an array of structs rather than a simple array as many modern devices
54  * have very sparse register maps.
55  */
56 struct reg_default {
57         unsigned int reg;
58         unsigned int def;
59 };
60
61 /**
62  * struct reg_sequence - An individual write from a sequence of writes.
63  *
64  * @reg: Register address.
65  * @def: Register value.
66  * @delay_us: Delay to be applied after the register write in microseconds
67  *
68  * Register/value pairs for sequences of writes with an optional delay in
69  * microseconds to be applied after each write.
70  */
71 struct reg_sequence {
72         unsigned int reg;
73         unsigned int def;
74         unsigned int delay_us;
75 };
76
77 #define REG_SEQ(_reg, _def, _delay_us) {                \
78                                 .reg = _reg,            \
79                                 .def = _def,            \
80                                 .delay_us = _delay_us,  \
81                                 }
82 #define REG_SEQ0(_reg, _def)    REG_SEQ(_reg, _def, 0)
83
84 /**
85  * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
86  *
87  * @map: Regmap to read from
88  * @addr: Address to poll
89  * @val: Unsigned integer variable to read the value into
90  * @cond: Break condition (usually involving @val)
91  * @sleep_us: Maximum time to sleep between reads in us (0
92  *            tight-loops).  Should be less than ~20ms since usleep_range
93  *            is used (see Documentation/timers/timers-howto.rst).
94  * @timeout_us: Timeout in us, 0 means never timeout
95  *
96  * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
97  * error return value in case of a error read. In the two former cases,
98  * the last read value at @addr is stored in @val. Must not be called
99  * from atomic context if sleep_us or timeout_us are used.
100  *
101  * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
102  */
103 #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
104 ({ \
105         int __ret, __tmp; \
106         __tmp = read_poll_timeout(regmap_read, __ret, __ret || (cond), \
107                         sleep_us, timeout_us, false, (map), (addr), &(val)); \
108         __ret ?: __tmp; \
109 })
110
111 /**
112  * regmap_read_poll_timeout_atomic - Poll until a condition is met or a timeout occurs
113  *
114  * @map: Regmap to read from
115  * @addr: Address to poll
116  * @val: Unsigned integer variable to read the value into
117  * @cond: Break condition (usually involving @val)
118  * @delay_us: Time to udelay between reads in us (0 tight-loops).
119  *            Should be less than ~10us since udelay is used
120  *            (see Documentation/timers/timers-howto.rst).
121  * @timeout_us: Timeout in us, 0 means never timeout
122  *
123  * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
124  * error return value in case of a error read. In the two former cases,
125  * the last read value at @addr is stored in @val.
126  *
127  * This is modelled after the readx_poll_timeout_atomic macros in linux/iopoll.h.
128  *
129  * Note: In general regmap cannot be used in atomic context. If you want to use
130  * this macro then first setup your regmap for atomic use (flat or no cache
131  * and MMIO regmap).
132  */
133 #define regmap_read_poll_timeout_atomic(map, addr, val, cond, delay_us, timeout_us) \
134 ({ \
135         u64 __timeout_us = (timeout_us); \
136         unsigned long __delay_us = (delay_us); \
137         ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
138         int __ret; \
139         for (;;) { \
140                 __ret = regmap_read((map), (addr), &(val)); \
141                 if (__ret) \
142                         break; \
143                 if (cond) \
144                         break; \
145                 if ((__timeout_us) && \
146                     ktime_compare(ktime_get(), __timeout) > 0) { \
147                         __ret = regmap_read((map), (addr), &(val)); \
148                         break; \
149                 } \
150                 if (__delay_us) \
151                         udelay(__delay_us); \
152         } \
153         __ret ?: ((cond) ? 0 : -ETIMEDOUT); \
154 })
155
156 /**
157  * regmap_field_read_poll_timeout - Poll until a condition is met or timeout
158  *
159  * @field: Regmap field to read from
160  * @val: Unsigned integer variable to read the value into
161  * @cond: Break condition (usually involving @val)
162  * @sleep_us: Maximum time to sleep between reads in us (0
163  *            tight-loops).  Should be less than ~20ms since usleep_range
164  *            is used (see Documentation/timers/timers-howto.rst).
165  * @timeout_us: Timeout in us, 0 means never timeout
166  *
167  * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_field_read
168  * error return value in case of a error read. In the two former cases,
169  * the last read value at @addr is stored in @val. Must not be called
170  * from atomic context if sleep_us or timeout_us are used.
171  *
172  * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
173  */
174 #define regmap_field_read_poll_timeout(field, val, cond, sleep_us, timeout_us) \
175 ({ \
176         int __ret, __tmp; \
177         __tmp = read_poll_timeout(regmap_field_read, __ret, __ret || (cond), \
178                         sleep_us, timeout_us, false, (field), &(val)); \
179         __ret ?: __tmp; \
180 })
181
182 #ifdef CONFIG_REGMAP
183
184 enum regmap_endian {
185         /* Unspecified -> 0 -> Backwards compatible default */
186         REGMAP_ENDIAN_DEFAULT = 0,
187         REGMAP_ENDIAN_BIG,
188         REGMAP_ENDIAN_LITTLE,
189         REGMAP_ENDIAN_NATIVE,
190 };
191
192 /**
193  * struct regmap_range - A register range, used for access related checks
194  *                       (readable/writeable/volatile/precious checks)
195  *
196  * @range_min: address of first register
197  * @range_max: address of last register
198  */
199 struct regmap_range {
200         unsigned int range_min;
201         unsigned int range_max;
202 };
203
204 #define regmap_reg_range(low, high) { .range_min = low, .range_max = high, }
205
206 /**
207  * struct regmap_access_table - A table of register ranges for access checks
208  *
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
213  *
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.
218  */
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;
224 };
225
226 typedef void (*regmap_lock)(void *);
227 typedef void (*regmap_unlock)(void *);
228
229 /**
230  * struct regmap_config - Configuration for the register map of a device.
231  *
232  * @name: Optional name of the regmap. Useful when a device has multiple
233  *        register regions.
234  *
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
238  *              used.
239  * @pad_bits: Number of bits of padding between register and value.
240  * @val_bits: Number of bits in a register value, mandatory.
241  *
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  * @writeable_noinc_reg: Optional callback returning true if the register
264  *                      supports multiple write operations without incrementing
265  *                      the register number. If this field is NULL but
266  *                      wr_noinc_table (see below) is not, the check is
267  *                      performed on such table (a register is no increment
268  *                      writeable if it belongs to one of the ranges specified
269  *                      by wr_noinc_table).
270  * @readable_noinc_reg: Optional callback returning true if the register
271  *                      supports multiple read operations without incrementing
272  *                      the register number. If this field is NULL but
273  *                      rd_noinc_table (see below) is not, the check is
274  *                      performed on such table (a register is no increment
275  *                      readable if it belongs to one of the ranges specified
276  *                      by rd_noinc_table).
277  * @disable_locking: This regmap is either protected by external means or
278  *                   is guaranteed not to be accessed from multiple threads.
279  *                   Don't use any locking mechanisms.
280  * @lock:         Optional lock callback (overrides regmap's default lock
281  *                function, based on spinlock or mutex).
282  * @unlock:       As above for unlocking.
283  * @lock_arg:     this field is passed as the only argument of lock/unlock
284  *                functions (ignored in case regular lock/unlock functions
285  *                are not overridden).
286  * @reg_read:     Optional callback that if filled will be used to perform
287  *                all the reads from the registers. Should only be provided for
288  *                devices whose read operation cannot be represented as a simple
289  *                read operation on a bus such as SPI, I2C, etc. Most of the
290  *                devices do not need this.
291  * @reg_write:    Same as above for writing.
292  * @fast_io:      Register IO is fast. Use a spinlock instead of a mutex
293  *                to perform locking. This field is ignored if custom lock/unlock
294  *                functions are used (see fields lock/unlock of struct regmap_config).
295  *                This field is a duplicate of a similar file in
296  *                'struct regmap_bus' and serves exact same purpose.
297  *                 Use it only for "no-bus" cases.
298  * @max_register: Optional, specifies the maximum valid register address.
299  * @wr_table:     Optional, points to a struct regmap_access_table specifying
300  *                valid ranges for write access.
301  * @rd_table:     As above, for read access.
302  * @volatile_table: As above, for volatile registers.
303  * @precious_table: As above, for precious registers.
304  * @wr_noinc_table: As above, for no increment writeable registers.
305  * @rd_noinc_table: As above, for no increment readable registers.
306  * @reg_defaults: Power on reset values for registers (for use with
307  *                register cache support).
308  * @num_reg_defaults: Number of elements in reg_defaults.
309  *
310  * @read_flag_mask: Mask to be set in the top bytes of the register when doing
311  *                  a read.
312  * @write_flag_mask: Mask to be set in the top bytes of the register when doing
313  *                   a write. If both read_flag_mask and write_flag_mask are
314  *                   empty and zero_flag_mask is not set the regmap_bus default
315  *                   masks are used.
316  * @zero_flag_mask: If set, read_flag_mask and write_flag_mask are used even
317  *                   if they are both empty.
318  * @use_relaxed_mmio: If set, MMIO R/W operations will not use memory barriers.
319  *                    This can avoid load on devices which don't require strict
320  *                    orderings, but drivers should carefully add any explicit
321  *                    memory barriers when they may require them.
322  * @use_single_read: If set, converts the bulk read operation into a series of
323  *                   single read operations. This is useful for a device that
324  *                   does not support  bulk read.
325  * @use_single_write: If set, converts the bulk write operation into a series of
326  *                    single write operations. This is useful for a device that
327  *                    does not support bulk write.
328  * @can_multi_write: If set, the device supports the multi write mode of bulk
329  *                   write operations, if clear multi write requests will be
330  *                   split into individual write operations
331  *
332  * @cache_type: The actual cache type.
333  * @reg_defaults_raw: Power on reset values for registers (for use with
334  *                    register cache support).
335  * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
336  * @reg_format_endian: Endianness for formatted register addresses. If this is
337  *                     DEFAULT, the @reg_format_endian_default value from the
338  *                     regmap bus is used.
339  * @val_format_endian: Endianness for formatted register values. If this is
340  *                     DEFAULT, the @reg_format_endian_default value from the
341  *                     regmap bus is used.
342  *
343  * @ranges: Array of configuration entries for virtual address ranges.
344  * @num_ranges: Number of range configuration entries.
345  * @use_hwlock: Indicate if a hardware spinlock should be used.
346  * @use_raw_spinlock: Indicate if a raw spinlock should be used.
347  * @hwlock_id: Specify the hardware spinlock id.
348  * @hwlock_mode: The hardware spinlock mode, should be HWLOCK_IRQSTATE,
349  *               HWLOCK_IRQ or 0.
350  * @can_sleep: Optional, specifies whether regmap operations can sleep.
351  */
352 struct regmap_config {
353         const char *name;
354
355         int reg_bits;
356         int reg_stride;
357         int pad_bits;
358         int val_bits;
359
360         bool (*writeable_reg)(struct device *dev, unsigned int reg);
361         bool (*readable_reg)(struct device *dev, unsigned int reg);
362         bool (*volatile_reg)(struct device *dev, unsigned int reg);
363         bool (*precious_reg)(struct device *dev, unsigned int reg);
364         bool (*writeable_noinc_reg)(struct device *dev, unsigned int reg);
365         bool (*readable_noinc_reg)(struct device *dev, unsigned int reg);
366
367         bool disable_locking;
368         regmap_lock lock;
369         regmap_unlock unlock;
370         void *lock_arg;
371
372         int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
373         int (*reg_write)(void *context, unsigned int reg, unsigned int val);
374
375         bool fast_io;
376
377         unsigned int max_register;
378         const struct regmap_access_table *wr_table;
379         const struct regmap_access_table *rd_table;
380         const struct regmap_access_table *volatile_table;
381         const struct regmap_access_table *precious_table;
382         const struct regmap_access_table *wr_noinc_table;
383         const struct regmap_access_table *rd_noinc_table;
384         const struct reg_default *reg_defaults;
385         unsigned int num_reg_defaults;
386         enum regcache_type cache_type;
387         const void *reg_defaults_raw;
388         unsigned int num_reg_defaults_raw;
389
390         unsigned long read_flag_mask;
391         unsigned long write_flag_mask;
392         bool zero_flag_mask;
393
394         bool use_single_read;
395         bool use_single_write;
396         bool use_relaxed_mmio;
397         bool can_multi_write;
398
399         enum regmap_endian reg_format_endian;
400         enum regmap_endian val_format_endian;
401
402         const struct regmap_range_cfg *ranges;
403         unsigned int num_ranges;
404
405         bool use_hwlock;
406         bool use_raw_spinlock;
407         unsigned int hwlock_id;
408         unsigned int hwlock_mode;
409
410         bool can_sleep;
411 };
412
413 /**
414  * struct regmap_range_cfg - Configuration for indirectly accessed or paged
415  *                           registers.
416  *
417  * @name: Descriptive name for diagnostics
418  *
419  * @range_min: Address of the lowest register address in virtual range.
420  * @range_max: Address of the highest register in virtual range.
421  *
422  * @selector_reg: Register with selector field.
423  * @selector_mask: Bit mask for selector value.
424  * @selector_shift: Bit shift for selector value.
425  *
426  * @window_start: Address of first (lowest) register in data window.
427  * @window_len: Number of registers in data window.
428  *
429  * Registers, mapped to this virtual range, are accessed in two steps:
430  *     1. page selector register update;
431  *     2. access through data window registers.
432  */
433 struct regmap_range_cfg {
434         const char *name;
435
436         /* Registers of virtual address range */
437         unsigned int range_min;
438         unsigned int range_max;
439
440         /* Page selector for indirect addressing */
441         unsigned int selector_reg;
442         unsigned int selector_mask;
443         int selector_shift;
444
445         /* Data window (per each page) */
446         unsigned int window_start;
447         unsigned int window_len;
448 };
449
450 struct regmap_async;
451
452 typedef int (*regmap_hw_write)(void *context, const void *data,
453                                size_t count);
454 typedef int (*regmap_hw_gather_write)(void *context,
455                                       const void *reg, size_t reg_len,
456                                       const void *val, size_t val_len);
457 typedef int (*regmap_hw_async_write)(void *context,
458                                      const void *reg, size_t reg_len,
459                                      const void *val, size_t val_len,
460                                      struct regmap_async *async);
461 typedef int (*regmap_hw_read)(void *context,
462                               const void *reg_buf, size_t reg_size,
463                               void *val_buf, size_t val_size);
464 typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
465                                   unsigned int *val);
466 typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
467                                    unsigned int val);
468 typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
469                                          unsigned int mask, unsigned int val);
470 typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
471 typedef void (*regmap_hw_free_context)(void *context);
472
473 /**
474  * struct regmap_bus - Description of a hardware bus for the register map
475  *                     infrastructure.
476  *
477  * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
478  *           to perform locking. This field is ignored if custom lock/unlock
479  *           functions are used (see fields lock/unlock of
480  *           struct regmap_config).
481  * @write: Write operation.
482  * @gather_write: Write operation with split register/value, return -ENOTSUPP
483  *                if not implemented  on a given device.
484  * @async_write: Write operation which completes asynchronously, optional and
485  *               must serialise with respect to non-async I/O.
486  * @reg_write: Write a single register value to the given register address. This
487  *             write operation has to complete when returning from the function.
488  * @reg_update_bits: Update bits operation to be used against volatile
489  *                   registers, intended for devices supporting some mechanism
490  *                   for setting clearing bits without having to
491  *                   read/modify/write.
492  * @read: Read operation.  Data is returned in the buffer used to transmit
493  *         data.
494  * @reg_read: Read a single register value from a given register address.
495  * @free_context: Free context.
496  * @async_alloc: Allocate a regmap_async() structure.
497  * @read_flag_mask: Mask to be set in the top byte of the register when doing
498  *                  a read.
499  * @reg_format_endian_default: Default endianness for formatted register
500  *     addresses. Used when the regmap_config specifies DEFAULT. If this is
501  *     DEFAULT, BIG is assumed.
502  * @val_format_endian_default: Default endianness for formatted register
503  *     values. Used when the regmap_config specifies DEFAULT. If this is
504  *     DEFAULT, BIG is assumed.
505  * @max_raw_read: Max raw read size that can be used on the bus.
506  * @max_raw_write: Max raw write size that can be used on the bus.
507  */
508 struct regmap_bus {
509         bool fast_io;
510         regmap_hw_write write;
511         regmap_hw_gather_write gather_write;
512         regmap_hw_async_write async_write;
513         regmap_hw_reg_write reg_write;
514         regmap_hw_reg_update_bits reg_update_bits;
515         regmap_hw_read read;
516         regmap_hw_reg_read reg_read;
517         regmap_hw_free_context free_context;
518         regmap_hw_async_alloc async_alloc;
519         u8 read_flag_mask;
520         enum regmap_endian reg_format_endian_default;
521         enum regmap_endian val_format_endian_default;
522         size_t max_raw_read;
523         size_t max_raw_write;
524 };
525
526 /*
527  * __regmap_init functions.
528  *
529  * These functions take a lock key and name parameter, and should not be called
530  * directly. Instead, use the regmap_init macros that generate a key and name
531  * for each call.
532  */
533 struct regmap *__regmap_init(struct device *dev,
534                              const struct regmap_bus *bus,
535                              void *bus_context,
536                              const struct regmap_config *config,
537                              struct lock_class_key *lock_key,
538                              const char *lock_name);
539 struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
540                                  const struct regmap_config *config,
541                                  struct lock_class_key *lock_key,
542                                  const char *lock_name);
543 struct regmap *__regmap_init_sccb(struct i2c_client *i2c,
544                                   const struct regmap_config *config,
545                                   struct lock_class_key *lock_key,
546                                   const char *lock_name);
547 struct regmap *__regmap_init_slimbus(struct slim_device *slimbus,
548                                  const struct regmap_config *config,
549                                  struct lock_class_key *lock_key,
550                                  const char *lock_name);
551 struct regmap *__regmap_init_spi(struct spi_device *dev,
552                                  const struct regmap_config *config,
553                                  struct lock_class_key *lock_key,
554                                  const char *lock_name);
555 struct regmap *__regmap_init_spmi_base(struct spmi_device *dev,
556                                        const struct regmap_config *config,
557                                        struct lock_class_key *lock_key,
558                                        const char *lock_name);
559 struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
560                                       const struct regmap_config *config,
561                                       struct lock_class_key *lock_key,
562                                       const char *lock_name);
563 struct regmap *__regmap_init_w1(struct device *w1_dev,
564                                  const struct regmap_config *config,
565                                  struct lock_class_key *lock_key,
566                                  const char *lock_name);
567 struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
568                                       void __iomem *regs,
569                                       const struct regmap_config *config,
570                                       struct lock_class_key *lock_key,
571                                       const char *lock_name);
572 struct regmap *__regmap_init_ac97(struct snd_ac97 *ac97,
573                                   const struct regmap_config *config,
574                                   struct lock_class_key *lock_key,
575                                   const char *lock_name);
576 struct regmap *__regmap_init_sdw(struct sdw_slave *sdw,
577                                  const struct regmap_config *config,
578                                  struct lock_class_key *lock_key,
579                                  const char *lock_name);
580 struct regmap *__regmap_init_sdw_mbq(struct sdw_slave *sdw,
581                                      const struct regmap_config *config,
582                                      struct lock_class_key *lock_key,
583                                      const char *lock_name);
584 struct regmap *__regmap_init_spi_avmm(struct spi_device *spi,
585                                       const struct regmap_config *config,
586                                       struct lock_class_key *lock_key,
587                                       const char *lock_name);
588
589 struct regmap *__devm_regmap_init(struct device *dev,
590                                   const struct regmap_bus *bus,
591                                   void *bus_context,
592                                   const struct regmap_config *config,
593                                   struct lock_class_key *lock_key,
594                                   const char *lock_name);
595 struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
596                                       const struct regmap_config *config,
597                                       struct lock_class_key *lock_key,
598                                       const char *lock_name);
599 struct regmap *__devm_regmap_init_sccb(struct i2c_client *i2c,
600                                        const struct regmap_config *config,
601                                        struct lock_class_key *lock_key,
602                                        const char *lock_name);
603 struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
604                                       const struct regmap_config *config,
605                                       struct lock_class_key *lock_key,
606                                       const char *lock_name);
607 struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *dev,
608                                             const struct regmap_config *config,
609                                             struct lock_class_key *lock_key,
610                                             const char *lock_name);
611 struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
612                                            const struct regmap_config *config,
613                                            struct lock_class_key *lock_key,
614                                            const char *lock_name);
615 struct regmap *__devm_regmap_init_w1(struct device *w1_dev,
616                                       const struct regmap_config *config,
617                                       struct lock_class_key *lock_key,
618                                       const char *lock_name);
619 struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
620                                            const char *clk_id,
621                                            void __iomem *regs,
622                                            const struct regmap_config *config,
623                                            struct lock_class_key *lock_key,
624                                            const char *lock_name);
625 struct regmap *__devm_regmap_init_ac97(struct snd_ac97 *ac97,
626                                        const struct regmap_config *config,
627                                        struct lock_class_key *lock_key,
628                                        const char *lock_name);
629 struct regmap *__devm_regmap_init_sdw(struct sdw_slave *sdw,
630                                  const struct regmap_config *config,
631                                  struct lock_class_key *lock_key,
632                                  const char *lock_name);
633 struct regmap *__devm_regmap_init_sdw_mbq(struct sdw_slave *sdw,
634                                           const struct regmap_config *config,
635                                           struct lock_class_key *lock_key,
636                                           const char *lock_name);
637 struct regmap *__devm_regmap_init_slimbus(struct slim_device *slimbus,
638                                  const struct regmap_config *config,
639                                  struct lock_class_key *lock_key,
640                                  const char *lock_name);
641 struct regmap *__devm_regmap_init_i3c(struct i3c_device *i3c,
642                                  const struct regmap_config *config,
643                                  struct lock_class_key *lock_key,
644                                  const char *lock_name);
645 struct regmap *__devm_regmap_init_spi_avmm(struct spi_device *spi,
646                                            const struct regmap_config *config,
647                                            struct lock_class_key *lock_key,
648                                            const char *lock_name);
649 /*
650  * Wrapper for regmap_init macros to include a unique lockdep key and name
651  * for each call. No-op if CONFIG_LOCKDEP is not set.
652  *
653  * @fn: Real function to call (in the form __[*_]regmap_init[_*])
654  * @name: Config variable name (#config in the calling macro)
655  **/
656 #ifdef CONFIG_LOCKDEP
657 #define __regmap_lockdep_wrapper(fn, name, ...)                         \
658 (                                                                       \
659         ({                                                              \
660                 static struct lock_class_key _key;                      \
661                 fn(__VA_ARGS__, &_key,                                  \
662                         KBUILD_BASENAME ":"                             \
663                         __stringify(__LINE__) ":"                       \
664                         "(" name ")->lock");                            \
665         })                                                              \
666 )
667 #else
668 #define __regmap_lockdep_wrapper(fn, name, ...) fn(__VA_ARGS__, NULL, NULL)
669 #endif
670
671 /**
672  * regmap_init() - Initialise register map
673  *
674  * @dev: Device that will be interacted with
675  * @bus: Bus-specific callbacks to use with device
676  * @bus_context: Data passed to bus-specific callbacks
677  * @config: Configuration for register map
678  *
679  * The return value will be an ERR_PTR() on error or a valid pointer to
680  * a struct regmap.  This function should generally not be called
681  * directly, it should be called by bus-specific init functions.
682  */
683 #define regmap_init(dev, bus, bus_context, config)                      \
684         __regmap_lockdep_wrapper(__regmap_init, #config,                \
685                                 dev, bus, bus_context, config)
686 int regmap_attach_dev(struct device *dev, struct regmap *map,
687                       const struct regmap_config *config);
688
689 /**
690  * regmap_init_i2c() - Initialise register map
691  *
692  * @i2c: Device that will be interacted with
693  * @config: Configuration for register map
694  *
695  * The return value will be an ERR_PTR() on error or a valid pointer to
696  * a struct regmap.
697  */
698 #define regmap_init_i2c(i2c, config)                                    \
699         __regmap_lockdep_wrapper(__regmap_init_i2c, #config,            \
700                                 i2c, config)
701
702 /**
703  * regmap_init_sccb() - Initialise register map
704  *
705  * @i2c: Device that will be interacted with
706  * @config: Configuration for register map
707  *
708  * The return value will be an ERR_PTR() on error or a valid pointer to
709  * a struct regmap.
710  */
711 #define regmap_init_sccb(i2c, config)                                   \
712         __regmap_lockdep_wrapper(__regmap_init_sccb, #config,           \
713                                 i2c, config)
714
715 /**
716  * regmap_init_slimbus() - Initialise register map
717  *
718  * @slimbus: Device that will be interacted with
719  * @config: Configuration for register map
720  *
721  * The return value will be an ERR_PTR() on error or a valid pointer to
722  * a struct regmap.
723  */
724 #define regmap_init_slimbus(slimbus, config)                            \
725         __regmap_lockdep_wrapper(__regmap_init_slimbus, #config,        \
726                                 slimbus, config)
727
728 /**
729  * regmap_init_spi() - Initialise register map
730  *
731  * @dev: Device that will be interacted with
732  * @config: Configuration for register map
733  *
734  * The return value will be an ERR_PTR() on error or a valid pointer to
735  * a struct regmap.
736  */
737 #define regmap_init_spi(dev, config)                                    \
738         __regmap_lockdep_wrapper(__regmap_init_spi, #config,            \
739                                 dev, config)
740
741 /**
742  * regmap_init_spmi_base() - Create regmap for the Base register space
743  *
744  * @dev:        SPMI device that will be interacted with
745  * @config:     Configuration for register map
746  *
747  * The return value will be an ERR_PTR() on error or a valid pointer to
748  * a struct regmap.
749  */
750 #define regmap_init_spmi_base(dev, config)                              \
751         __regmap_lockdep_wrapper(__regmap_init_spmi_base, #config,      \
752                                 dev, config)
753
754 /**
755  * regmap_init_spmi_ext() - Create regmap for Ext register space
756  *
757  * @dev:        Device that will be interacted with
758  * @config:     Configuration for register map
759  *
760  * The return value will be an ERR_PTR() on error or a valid pointer to
761  * a struct regmap.
762  */
763 #define regmap_init_spmi_ext(dev, config)                               \
764         __regmap_lockdep_wrapper(__regmap_init_spmi_ext, #config,       \
765                                 dev, config)
766
767 /**
768  * regmap_init_w1() - Initialise register map
769  *
770  * @w1_dev: Device that will be interacted with
771  * @config: Configuration for register map
772  *
773  * The return value will be an ERR_PTR() on error or a valid pointer to
774  * a struct regmap.
775  */
776 #define regmap_init_w1(w1_dev, config)                                  \
777         __regmap_lockdep_wrapper(__regmap_init_w1, #config,             \
778                                 w1_dev, config)
779
780 /**
781  * regmap_init_mmio_clk() - Initialise register map with register clock
782  *
783  * @dev: Device that will be interacted with
784  * @clk_id: register clock consumer ID
785  * @regs: Pointer to memory-mapped IO region
786  * @config: Configuration for register map
787  *
788  * The return value will be an ERR_PTR() on error or a valid pointer to
789  * a struct regmap.
790  */
791 #define regmap_init_mmio_clk(dev, clk_id, regs, config)                 \
792         __regmap_lockdep_wrapper(__regmap_init_mmio_clk, #config,       \
793                                 dev, clk_id, regs, config)
794
795 /**
796  * regmap_init_mmio() - Initialise register map
797  *
798  * @dev: Device that will be interacted with
799  * @regs: Pointer to memory-mapped IO region
800  * @config: Configuration for register map
801  *
802  * The return value will be an ERR_PTR() on error or a valid pointer to
803  * a struct regmap.
804  */
805 #define regmap_init_mmio(dev, regs, config)             \
806         regmap_init_mmio_clk(dev, NULL, regs, config)
807
808 /**
809  * regmap_init_ac97() - Initialise AC'97 register map
810  *
811  * @ac97: Device that will be interacted with
812  * @config: Configuration for register map
813  *
814  * The return value will be an ERR_PTR() on error or a valid pointer to
815  * a struct regmap.
816  */
817 #define regmap_init_ac97(ac97, config)                                  \
818         __regmap_lockdep_wrapper(__regmap_init_ac97, #config,           \
819                                 ac97, config)
820 bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
821
822 /**
823  * regmap_init_sdw() - Initialise register map
824  *
825  * @sdw: Device that will be interacted with
826  * @config: Configuration for register map
827  *
828  * The return value will be an ERR_PTR() on error or a valid pointer to
829  * a struct regmap.
830  */
831 #define regmap_init_sdw(sdw, config)                                    \
832         __regmap_lockdep_wrapper(__regmap_init_sdw, #config,            \
833                                 sdw, config)
834
835 /**
836  * regmap_init_sdw_mbq() - Initialise register map
837  *
838  * @sdw: Device that will be interacted with
839  * @config: Configuration for register map
840  *
841  * The return value will be an ERR_PTR() on error or a valid pointer to
842  * a struct regmap.
843  */
844 #define regmap_init_sdw_mbq(sdw, config)                                        \
845         __regmap_lockdep_wrapper(__regmap_init_sdw_mbq, #config,                \
846                                 sdw, config)
847
848 /**
849  * regmap_init_spi_avmm() - Initialize register map for Intel SPI Slave
850  * to AVMM Bus Bridge
851  *
852  * @spi: Device that will be interacted with
853  * @config: Configuration for register map
854  *
855  * The return value will be an ERR_PTR() on error or a valid pointer
856  * to a struct regmap.
857  */
858 #define regmap_init_spi_avmm(spi, config)                                       \
859         __regmap_lockdep_wrapper(__regmap_init_spi_avmm, #config,               \
860                                  spi, config)
861
862 /**
863  * devm_regmap_init() - Initialise managed register map
864  *
865  * @dev: Device that will be interacted with
866  * @bus: Bus-specific callbacks to use with device
867  * @bus_context: Data passed to bus-specific callbacks
868  * @config: Configuration for register map
869  *
870  * The return value will be an ERR_PTR() on error or a valid pointer
871  * to a struct regmap.  This function should generally not be called
872  * directly, it should be called by bus-specific init functions.  The
873  * map will be automatically freed by the device management code.
874  */
875 #define devm_regmap_init(dev, bus, bus_context, config)                 \
876         __regmap_lockdep_wrapper(__devm_regmap_init, #config,           \
877                                 dev, bus, bus_context, config)
878
879 /**
880  * devm_regmap_init_i2c() - Initialise managed register map
881  *
882  * @i2c: Device that will be interacted with
883  * @config: Configuration for register map
884  *
885  * The return value will be an ERR_PTR() on error or a valid pointer
886  * to a struct regmap.  The regmap will be automatically freed by the
887  * device management code.
888  */
889 #define devm_regmap_init_i2c(i2c, config)                               \
890         __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config,       \
891                                 i2c, config)
892
893 /**
894  * devm_regmap_init_sccb() - Initialise managed register map
895  *
896  * @i2c: Device that will be interacted with
897  * @config: Configuration for register map
898  *
899  * The return value will be an ERR_PTR() on error or a valid pointer
900  * to a struct regmap.  The regmap will be automatically freed by the
901  * device management code.
902  */
903 #define devm_regmap_init_sccb(i2c, config)                              \
904         __regmap_lockdep_wrapper(__devm_regmap_init_sccb, #config,      \
905                                 i2c, config)
906
907 /**
908  * devm_regmap_init_spi() - Initialise register map
909  *
910  * @dev: Device that will be interacted with
911  * @config: Configuration for register map
912  *
913  * The return value will be an ERR_PTR() on error or a valid pointer
914  * to a struct regmap.  The map will be automatically freed by the
915  * device management code.
916  */
917 #define devm_regmap_init_spi(dev, config)                               \
918         __regmap_lockdep_wrapper(__devm_regmap_init_spi, #config,       \
919                                 dev, config)
920
921 /**
922  * devm_regmap_init_spmi_base() - Create managed regmap for Base register space
923  *
924  * @dev:        SPMI device that will be interacted with
925  * @config:     Configuration for register map
926  *
927  * The return value will be an ERR_PTR() on error or a valid pointer
928  * to a struct regmap.  The regmap will be automatically freed by the
929  * device management code.
930  */
931 #define devm_regmap_init_spmi_base(dev, config)                         \
932         __regmap_lockdep_wrapper(__devm_regmap_init_spmi_base, #config, \
933                                 dev, config)
934
935 /**
936  * devm_regmap_init_spmi_ext() - Create managed regmap for Ext register space
937  *
938  * @dev:        SPMI device that will be interacted with
939  * @config:     Configuration for register map
940  *
941  * The return value will be an ERR_PTR() on error or a valid pointer
942  * to a struct regmap.  The regmap will be automatically freed by the
943  * device management code.
944  */
945 #define devm_regmap_init_spmi_ext(dev, config)                          \
946         __regmap_lockdep_wrapper(__devm_regmap_init_spmi_ext, #config,  \
947                                 dev, config)
948
949 /**
950  * devm_regmap_init_w1() - Initialise managed register map
951  *
952  * @w1_dev: Device that will be interacted with
953  * @config: Configuration for register map
954  *
955  * The return value will be an ERR_PTR() on error or a valid pointer
956  * to a struct regmap.  The regmap will be automatically freed by the
957  * device management code.
958  */
959 #define devm_regmap_init_w1(w1_dev, config)                             \
960         __regmap_lockdep_wrapper(__devm_regmap_init_w1, #config,        \
961                                 w1_dev, config)
962 /**
963  * devm_regmap_init_mmio_clk() - Initialise managed register map with clock
964  *
965  * @dev: Device that will be interacted with
966  * @clk_id: register clock consumer ID
967  * @regs: Pointer to memory-mapped IO region
968  * @config: Configuration for register map
969  *
970  * The return value will be an ERR_PTR() on error or a valid pointer
971  * to a struct regmap.  The regmap will be automatically freed by the
972  * device management code.
973  */
974 #define devm_regmap_init_mmio_clk(dev, clk_id, regs, config)            \
975         __regmap_lockdep_wrapper(__devm_regmap_init_mmio_clk, #config,  \
976                                 dev, clk_id, regs, config)
977
978 /**
979  * devm_regmap_init_mmio() - Initialise managed register map
980  *
981  * @dev: Device that will be interacted with
982  * @regs: Pointer to memory-mapped IO region
983  * @config: Configuration for register map
984  *
985  * The return value will be an ERR_PTR() on error or a valid pointer
986  * to a struct regmap.  The regmap will be automatically freed by the
987  * device management code.
988  */
989 #define devm_regmap_init_mmio(dev, regs, config)                \
990         devm_regmap_init_mmio_clk(dev, NULL, regs, config)
991
992 /**
993  * devm_regmap_init_ac97() - Initialise AC'97 register map
994  *
995  * @ac97: Device that will be interacted with
996  * @config: Configuration for register map
997  *
998  * The return value will be an ERR_PTR() on error or a valid pointer
999  * to a struct regmap.  The regmap will be automatically freed by the
1000  * device management code.
1001  */
1002 #define devm_regmap_init_ac97(ac97, config)                             \
1003         __regmap_lockdep_wrapper(__devm_regmap_init_ac97, #config,      \
1004                                 ac97, config)
1005
1006 /**
1007  * devm_regmap_init_sdw() - Initialise managed register map
1008  *
1009  * @sdw: Device that will be interacted with
1010  * @config: Configuration for register map
1011  *
1012  * The return value will be an ERR_PTR() on error or a valid pointer
1013  * to a struct regmap. The regmap will be automatically freed by the
1014  * device management code.
1015  */
1016 #define devm_regmap_init_sdw(sdw, config)                               \
1017         __regmap_lockdep_wrapper(__devm_regmap_init_sdw, #config,       \
1018                                 sdw, config)
1019
1020 /**
1021  * devm_regmap_init_sdw_mbq() - Initialise managed register map
1022  *
1023  * @sdw: Device that will be interacted with
1024  * @config: Configuration for register map
1025  *
1026  * The return value will be an ERR_PTR() on error or a valid pointer
1027  * to a struct regmap. The regmap will be automatically freed by the
1028  * device management code.
1029  */
1030 #define devm_regmap_init_sdw_mbq(sdw, config)                   \
1031         __regmap_lockdep_wrapper(__devm_regmap_init_sdw_mbq, #config,   \
1032                                 sdw, config)
1033
1034 /**
1035  * devm_regmap_init_slimbus() - Initialise managed register map
1036  *
1037  * @slimbus: Device that will be interacted with
1038  * @config: Configuration for register map
1039  *
1040  * The return value will be an ERR_PTR() on error or a valid pointer
1041  * to a struct regmap. The regmap will be automatically freed by the
1042  * device management code.
1043  */
1044 #define devm_regmap_init_slimbus(slimbus, config)                       \
1045         __regmap_lockdep_wrapper(__devm_regmap_init_slimbus, #config,   \
1046                                 slimbus, config)
1047
1048 /**
1049  * devm_regmap_init_i3c() - Initialise managed register map
1050  *
1051  * @i3c: Device that will be interacted with
1052  * @config: Configuration for register map
1053  *
1054  * The return value will be an ERR_PTR() on error or a valid pointer
1055  * to a struct regmap.  The regmap will be automatically freed by the
1056  * device management code.
1057  */
1058 #define devm_regmap_init_i3c(i3c, config)                               \
1059         __regmap_lockdep_wrapper(__devm_regmap_init_i3c, #config,       \
1060                                 i3c, config)
1061
1062 /**
1063  * devm_regmap_init_spi_avmm() - Initialize register map for Intel SPI Slave
1064  * to AVMM Bus Bridge
1065  *
1066  * @spi: Device that will be interacted with
1067  * @config: Configuration for register map
1068  *
1069  * The return value will be an ERR_PTR() on error or a valid pointer
1070  * to a struct regmap.  The map will be automatically freed by the
1071  * device management code.
1072  */
1073 #define devm_regmap_init_spi_avmm(spi, config)                          \
1074         __regmap_lockdep_wrapper(__devm_regmap_init_spi_avmm, #config,  \
1075                                  spi, config)
1076
1077 int regmap_mmio_attach_clk(struct regmap *map, struct clk *clk);
1078 void regmap_mmio_detach_clk(struct regmap *map);
1079 void regmap_exit(struct regmap *map);
1080 int regmap_reinit_cache(struct regmap *map,
1081                         const struct regmap_config *config);
1082 struct regmap *dev_get_regmap(struct device *dev, const char *name);
1083 struct device *regmap_get_device(struct regmap *map);
1084 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
1085 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
1086 int regmap_raw_write(struct regmap *map, unsigned int reg,
1087                      const void *val, size_t val_len);
1088 int regmap_noinc_write(struct regmap *map, unsigned int reg,
1089                      const void *val, size_t val_len);
1090 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1091                         size_t val_count);
1092 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
1093                         int num_regs);
1094 int regmap_multi_reg_write_bypassed(struct regmap *map,
1095                                     const struct reg_sequence *regs,
1096                                     int num_regs);
1097 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1098                            const void *val, size_t val_len);
1099 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
1100 int regmap_raw_read(struct regmap *map, unsigned int reg,
1101                     void *val, size_t val_len);
1102 int regmap_noinc_read(struct regmap *map, unsigned int reg,
1103                       void *val, size_t val_len);
1104 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
1105                      size_t val_count);
1106 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1107                             unsigned int mask, unsigned int val,
1108                             bool *change, bool async, bool force);
1109
1110 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
1111                                      unsigned int mask, unsigned int val)
1112 {
1113         return regmap_update_bits_base(map, reg, mask, val, NULL, false, false);
1114 }
1115
1116 static inline int regmap_update_bits_async(struct regmap *map, unsigned int reg,
1117                                            unsigned int mask, unsigned int val)
1118 {
1119         return regmap_update_bits_base(map, reg, mask, val, NULL, true, false);
1120 }
1121
1122 static inline int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1123                                            unsigned int mask, unsigned int val,
1124                                            bool *change)
1125 {
1126         return regmap_update_bits_base(map, reg, mask, val,
1127                                        change, false, false);
1128 }
1129
1130 static inline int
1131 regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
1132                                unsigned int mask, unsigned int val,
1133                                bool *change)
1134 {
1135         return regmap_update_bits_base(map, reg, mask, val,
1136                                        change, true, false);
1137 }
1138
1139 static inline int regmap_write_bits(struct regmap *map, unsigned int reg,
1140                                     unsigned int mask, unsigned int val)
1141 {
1142         return regmap_update_bits_base(map, reg, mask, val, NULL, false, true);
1143 }
1144
1145 int regmap_get_val_bytes(struct regmap *map);
1146 int regmap_get_max_register(struct regmap *map);
1147 int regmap_get_reg_stride(struct regmap *map);
1148 int regmap_async_complete(struct regmap *map);
1149 bool regmap_can_raw_write(struct regmap *map);
1150 size_t regmap_get_raw_read_max(struct regmap *map);
1151 size_t regmap_get_raw_write_max(struct regmap *map);
1152
1153 int regcache_sync(struct regmap *map);
1154 int regcache_sync_region(struct regmap *map, unsigned int min,
1155                          unsigned int max);
1156 int regcache_drop_region(struct regmap *map, unsigned int min,
1157                          unsigned int max);
1158 void regcache_cache_only(struct regmap *map, bool enable);
1159 void regcache_cache_bypass(struct regmap *map, bool enable);
1160 void regcache_mark_dirty(struct regmap *map);
1161
1162 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
1163                               const struct regmap_access_table *table);
1164
1165 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
1166                           int num_regs);
1167 int regmap_parse_val(struct regmap *map, const void *buf,
1168                                 unsigned int *val);
1169
1170 static inline bool regmap_reg_in_range(unsigned int reg,
1171                                        const struct regmap_range *range)
1172 {
1173         return reg >= range->range_min && reg <= range->range_max;
1174 }
1175
1176 bool regmap_reg_in_ranges(unsigned int reg,
1177                           const struct regmap_range *ranges,
1178                           unsigned int nranges);
1179
1180 static inline int regmap_set_bits(struct regmap *map,
1181                                   unsigned int reg, unsigned int bits)
1182 {
1183         return regmap_update_bits_base(map, reg, bits, bits,
1184                                        NULL, false, false);
1185 }
1186
1187 static inline int regmap_clear_bits(struct regmap *map,
1188                                     unsigned int reg, unsigned int bits)
1189 {
1190         return regmap_update_bits_base(map, reg, bits, 0, NULL, false, false);
1191 }
1192
1193 int regmap_test_bits(struct regmap *map, unsigned int reg, unsigned int bits);
1194
1195 /**
1196  * struct reg_field - Description of an register field
1197  *
1198  * @reg: Offset of the register within the regmap bank
1199  * @lsb: lsb of the register field.
1200  * @msb: msb of the register field.
1201  * @id_size: port size if it has some ports
1202  * @id_offset: address offset for each ports
1203  */
1204 struct reg_field {
1205         unsigned int reg;
1206         unsigned int lsb;
1207         unsigned int msb;
1208         unsigned int id_size;
1209         unsigned int id_offset;
1210 };
1211
1212 #define REG_FIELD(_reg, _lsb, _msb) {           \
1213                                 .reg = _reg,    \
1214                                 .lsb = _lsb,    \
1215                                 .msb = _msb,    \
1216                                 }
1217
1218 #define REG_FIELD_ID(_reg, _lsb, _msb, _size, _offset) {        \
1219                                 .reg = _reg,                    \
1220                                 .lsb = _lsb,                    \
1221                                 .msb = _msb,                    \
1222                                 .id_size = _size,               \
1223                                 .id_offset = _offset,           \
1224                                 }
1225
1226 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
1227                 struct reg_field reg_field);
1228 void regmap_field_free(struct regmap_field *field);
1229
1230 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
1231                 struct regmap *regmap, struct reg_field reg_field);
1232 void devm_regmap_field_free(struct device *dev, struct regmap_field *field);
1233
1234 int regmap_field_bulk_alloc(struct regmap *regmap,
1235                              struct regmap_field **rm_field,
1236                              struct reg_field *reg_field,
1237                              int num_fields);
1238 void regmap_field_bulk_free(struct regmap_field *field);
1239 int devm_regmap_field_bulk_alloc(struct device *dev, struct regmap *regmap,
1240                                  struct regmap_field **field,
1241                                  struct reg_field *reg_field, int num_fields);
1242 void devm_regmap_field_bulk_free(struct device *dev,
1243                                  struct regmap_field *field);
1244
1245 int regmap_field_read(struct regmap_field *field, unsigned int *val);
1246 int regmap_field_update_bits_base(struct regmap_field *field,
1247                                   unsigned int mask, unsigned int val,
1248                                   bool *change, bool async, bool force);
1249 int regmap_fields_read(struct regmap_field *field, unsigned int id,
1250                        unsigned int *val);
1251 int regmap_fields_update_bits_base(struct regmap_field *field,  unsigned int id,
1252                                    unsigned int mask, unsigned int val,
1253                                    bool *change, bool async, bool force);
1254
1255 static inline int regmap_field_write(struct regmap_field *field,
1256                                      unsigned int val)
1257 {
1258         return regmap_field_update_bits_base(field, ~0, val,
1259                                              NULL, false, false);
1260 }
1261
1262 static inline int regmap_field_force_write(struct regmap_field *field,
1263                                            unsigned int val)
1264 {
1265         return regmap_field_update_bits_base(field, ~0, val, NULL, false, true);
1266 }
1267
1268 static inline int regmap_field_update_bits(struct regmap_field *field,
1269                                            unsigned int mask, unsigned int val)
1270 {
1271         return regmap_field_update_bits_base(field, mask, val,
1272                                              NULL, false, false);
1273 }
1274
1275 static inline int
1276 regmap_field_force_update_bits(struct regmap_field *field,
1277                                unsigned int mask, unsigned int val)
1278 {
1279         return regmap_field_update_bits_base(field, mask, val,
1280                                              NULL, false, true);
1281 }
1282
1283 static inline int regmap_fields_write(struct regmap_field *field,
1284                                       unsigned int id, unsigned int val)
1285 {
1286         return regmap_fields_update_bits_base(field, id, ~0, val,
1287                                               NULL, false, false);
1288 }
1289
1290 static inline int regmap_fields_force_write(struct regmap_field *field,
1291                                             unsigned int id, unsigned int val)
1292 {
1293         return regmap_fields_update_bits_base(field, id, ~0, val,
1294                                               NULL, false, true);
1295 }
1296
1297 static inline int
1298 regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1299                           unsigned int mask, unsigned int val)
1300 {
1301         return regmap_fields_update_bits_base(field, id, mask, val,
1302                                               NULL, false, false);
1303 }
1304
1305 static inline int
1306 regmap_fields_force_update_bits(struct regmap_field *field, unsigned int id,
1307                                 unsigned int mask, unsigned int val)
1308 {
1309         return regmap_fields_update_bits_base(field, id, mask, val,
1310                                               NULL, false, true);
1311 }
1312
1313 /**
1314  * struct regmap_irq_type - IRQ type definitions.
1315  *
1316  * @type_reg_offset: Offset register for the irq type setting.
1317  * @type_rising_val: Register value to configure RISING type irq.
1318  * @type_falling_val: Register value to configure FALLING type irq.
1319  * @type_level_low_val: Register value to configure LEVEL_LOW type irq.
1320  * @type_level_high_val: Register value to configure LEVEL_HIGH type irq.
1321  * @types_supported: logical OR of IRQ_TYPE_* flags indicating supported types.
1322  */
1323 struct regmap_irq_type {
1324         unsigned int type_reg_offset;
1325         unsigned int type_reg_mask;
1326         unsigned int type_rising_val;
1327         unsigned int type_falling_val;
1328         unsigned int type_level_low_val;
1329         unsigned int type_level_high_val;
1330         unsigned int types_supported;
1331 };
1332
1333 /**
1334  * struct regmap_irq - Description of an IRQ for the generic regmap irq_chip.
1335  *
1336  * @reg_offset: Offset of the status/mask register within the bank
1337  * @mask:       Mask used to flag/control the register.
1338  * @type:       IRQ trigger type setting details if supported.
1339  */
1340 struct regmap_irq {
1341         unsigned int reg_offset;
1342         unsigned int mask;
1343         struct regmap_irq_type type;
1344 };
1345
1346 #define REGMAP_IRQ_REG(_irq, _off, _mask)               \
1347         [_irq] = { .reg_offset = (_off), .mask = (_mask) }
1348
1349 #define REGMAP_IRQ_REG_LINE(_id, _reg_bits) \
1350         [_id] = {                               \
1351                 .mask = BIT((_id) % (_reg_bits)),       \
1352                 .reg_offset = (_id) / (_reg_bits),      \
1353         }
1354
1355 #define REGMAP_IRQ_MAIN_REG_OFFSET(arr)                         \
1356         { .num_regs = ARRAY_SIZE((arr)), .offset = &(arr)[0] }
1357
1358 struct regmap_irq_sub_irq_map {
1359         unsigned int num_regs;
1360         unsigned int *offset;
1361 };
1362
1363 /**
1364  * struct regmap_irq_chip - Description of a generic regmap irq_chip.
1365  *
1366  * @name:        Descriptive name for IRQ controller.
1367  *
1368  * @main_status: Base main status register address. For chips which have
1369  *               interrupts arranged in separate sub-irq blocks with own IRQ
1370  *               registers and which have a main IRQ registers indicating
1371  *               sub-irq blocks with unhandled interrupts. For such chips fill
1372  *               sub-irq register information in status_base, mask_base and
1373  *               ack_base.
1374  * @num_main_status_bits: Should be given to chips where number of meaningfull
1375  *                        main status bits differs from num_regs.
1376  * @sub_reg_offsets: arrays of mappings from main register bits to sub irq
1377  *                   registers. First item in array describes the registers
1378  *                   for first main status bit. Second array for second bit etc.
1379  *                   Offset is given as sub register status offset to
1380  *                   status_base. Should contain num_regs arrays.
1381  *                   Can be provided for chips with more complex mapping than
1382  *                   1.st bit to 1.st sub-reg, 2.nd bit to 2.nd sub-reg, ...
1383  *                   When used with not_fixed_stride, each one-element array
1384  *                   member contains offset calculated as address from each
1385  *                   peripheral to first peripheral.
1386  * @num_main_regs: Number of 'main status' irq registers for chips which have
1387  *                 main_status set.
1388  *
1389  * @status_base: Base status register address.
1390  * @mask_base:   Base mask register address.
1391  * @mask_writeonly: Base mask register is write only.
1392  * @unmask_base:  Base unmask register address. for chips who have
1393  *                separate mask and unmask registers
1394  * @ack_base:    Base ack address. If zero then the chip is clear on read.
1395  *               Using zero value is possible with @use_ack bit.
1396  * @wake_base:   Base address for wake enables.  If zero unsupported.
1397  * @type_base:   Base address for irq type.  If zero unsupported.
1398  * @virt_reg_base:   Base addresses for extra config regs.
1399  * @irq_reg_stride:  Stride to use for chips where registers are not contiguous.
1400  * @init_ack_masked: Ack all masked interrupts once during initalization.
1401  * @mask_invert: Inverted mask register: cleared bits are masked out.
1402  * @use_ack:     Use @ack register even if it is zero.
1403  * @ack_invert:  Inverted ack register: cleared bits for ack.
1404  * @clear_ack:  Use this to set 1 and 0 or vice-versa to clear interrupts.
1405  * @wake_invert: Inverted wake register: cleared bits are wake enabled.
1406  * @type_invert: Invert the type flags.
1407  * @type_in_mask: Use the mask registers for controlling irq type. For
1408  *                interrupts defining type_rising/falling_mask use mask_base
1409  *                for edge configuration and never update bits in type_base.
1410  * @clear_on_unmask: For chips with interrupts cleared on read: read the status
1411  *                   registers before unmasking interrupts to clear any bits
1412  *                   set when they were masked.
1413  * @not_fixed_stride: Used when chip peripherals are not laid out with fixed
1414  *                    stride. Must be used with sub_reg_offsets containing the
1415  *                    offsets to each peripheral.
1416  * @runtime_pm:  Hold a runtime PM lock on the device when accessing it.
1417  *
1418  * @num_regs:    Number of registers in each control bank.
1419  * @irqs:        Descriptors for individual IRQs.  Interrupt numbers are
1420  *               assigned based on the index in the array of the interrupt.
1421  * @num_irqs:    Number of descriptors.
1422  * @num_type_reg:    Number of type registers.
1423  * @num_virt_regs:   Number of non-standard irq configuration registers.
1424  *                   If zero unsupported.
1425  * @type_reg_stride: Stride to use for chips where type registers are not
1426  *                      contiguous.
1427  * @handle_pre_irq:  Driver specific callback to handle interrupt from device
1428  *                   before regmap_irq_handler process the interrupts.
1429  * @handle_post_irq: Driver specific callback to handle interrupt from device
1430  *                   after handling the interrupts in regmap_irq_handler().
1431  * @set_type_virt:   Driver specific callback to extend regmap_irq_set_type()
1432  *                   and configure virt regs.
1433  * @irq_drv_data:    Driver specific IRQ data which is passed as parameter when
1434  *                   driver specific pre/post interrupt handler is called.
1435  *
1436  * This is not intended to handle every possible interrupt controller, but
1437  * it should handle a substantial proportion of those that are found in the
1438  * wild.
1439  */
1440 struct regmap_irq_chip {
1441         const char *name;
1442
1443         unsigned int main_status;
1444         unsigned int num_main_status_bits;
1445         struct regmap_irq_sub_irq_map *sub_reg_offsets;
1446         int num_main_regs;
1447
1448         unsigned int status_base;
1449         unsigned int mask_base;
1450         unsigned int unmask_base;
1451         unsigned int ack_base;
1452         unsigned int wake_base;
1453         unsigned int type_base;
1454         unsigned int *virt_reg_base;
1455         unsigned int irq_reg_stride;
1456         bool mask_writeonly:1;
1457         bool init_ack_masked:1;
1458         bool mask_invert:1;
1459         bool use_ack:1;
1460         bool ack_invert:1;
1461         bool clear_ack:1;
1462         bool wake_invert:1;
1463         bool runtime_pm:1;
1464         bool type_invert:1;
1465         bool type_in_mask:1;
1466         bool clear_on_unmask:1;
1467         bool not_fixed_stride:1;
1468
1469         int num_regs;
1470
1471         const struct regmap_irq *irqs;
1472         int num_irqs;
1473
1474         int num_type_reg;
1475         int num_virt_regs;
1476         unsigned int type_reg_stride;
1477
1478         int (*handle_pre_irq)(void *irq_drv_data);
1479         int (*handle_post_irq)(void *irq_drv_data);
1480         int (*set_type_virt)(unsigned int **buf, unsigned int type,
1481                              unsigned long hwirq, int reg);
1482         void *irq_drv_data;
1483 };
1484
1485 struct regmap_irq_chip_data;
1486
1487 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
1488                         int irq_base, const struct regmap_irq_chip *chip,
1489                         struct regmap_irq_chip_data **data);
1490 int regmap_add_irq_chip_fwnode(struct fwnode_handle *fwnode,
1491                                struct regmap *map, int irq,
1492                                int irq_flags, int irq_base,
1493                                const struct regmap_irq_chip *chip,
1494                                struct regmap_irq_chip_data **data);
1495 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
1496
1497 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
1498                              int irq_flags, int irq_base,
1499                              const struct regmap_irq_chip *chip,
1500                              struct regmap_irq_chip_data **data);
1501 int devm_regmap_add_irq_chip_fwnode(struct device *dev,
1502                                     struct fwnode_handle *fwnode,
1503                                     struct regmap *map, int irq,
1504                                     int irq_flags, int irq_base,
1505                                     const struct regmap_irq_chip *chip,
1506                                     struct regmap_irq_chip_data **data);
1507 void devm_regmap_del_irq_chip(struct device *dev, int irq,
1508                               struct regmap_irq_chip_data *data);
1509
1510 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
1511 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
1512 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
1513
1514 #else
1515
1516 /*
1517  * These stubs should only ever be called by generic code which has
1518  * regmap based facilities, if they ever get called at runtime
1519  * something is going wrong and something probably needs to select
1520  * REGMAP.
1521  */
1522
1523 static inline int regmap_write(struct regmap *map, unsigned int reg,
1524                                unsigned int val)
1525 {
1526         WARN_ONCE(1, "regmap API is disabled");
1527         return -EINVAL;
1528 }
1529
1530 static inline int regmap_write_async(struct regmap *map, unsigned int reg,
1531                                      unsigned int val)
1532 {
1533         WARN_ONCE(1, "regmap API is disabled");
1534         return -EINVAL;
1535 }
1536
1537 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
1538                                    const void *val, size_t val_len)
1539 {
1540         WARN_ONCE(1, "regmap API is disabled");
1541         return -EINVAL;
1542 }
1543
1544 static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1545                                          const void *val, size_t val_len)
1546 {
1547         WARN_ONCE(1, "regmap API is disabled");
1548         return -EINVAL;
1549 }
1550
1551 static inline int regmap_noinc_write(struct regmap *map, unsigned int reg,
1552                                     const void *val, size_t val_len)
1553 {
1554         WARN_ONCE(1, "regmap API is disabled");
1555         return -EINVAL;
1556 }
1557
1558 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
1559                                     const void *val, size_t val_count)
1560 {
1561         WARN_ONCE(1, "regmap API is disabled");
1562         return -EINVAL;
1563 }
1564
1565 static inline int regmap_read(struct regmap *map, unsigned int reg,
1566                               unsigned int *val)
1567 {
1568         WARN_ONCE(1, "regmap API is disabled");
1569         return -EINVAL;
1570 }
1571
1572 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
1573                                   void *val, size_t val_len)
1574 {
1575         WARN_ONCE(1, "regmap API is disabled");
1576         return -EINVAL;
1577 }
1578
1579 static inline int regmap_noinc_read(struct regmap *map, unsigned int reg,
1580                                     void *val, size_t val_len)
1581 {
1582         WARN_ONCE(1, "regmap API is disabled");
1583         return -EINVAL;
1584 }
1585
1586 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
1587                                    void *val, size_t val_count)
1588 {
1589         WARN_ONCE(1, "regmap API is disabled");
1590         return -EINVAL;
1591 }
1592
1593 static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1594                                           unsigned int mask, unsigned int val,
1595                                           bool *change, bool async, bool force)
1596 {
1597         WARN_ONCE(1, "regmap API is disabled");
1598         return -EINVAL;
1599 }
1600
1601 static inline int regmap_set_bits(struct regmap *map,
1602                                   unsigned int reg, unsigned int bits)
1603 {
1604         WARN_ONCE(1, "regmap API is disabled");
1605         return -EINVAL;
1606 }
1607
1608 static inline int regmap_clear_bits(struct regmap *map,
1609                                     unsigned int reg, unsigned int bits)
1610 {
1611         WARN_ONCE(1, "regmap API is disabled");
1612         return -EINVAL;
1613 }
1614
1615 static inline int regmap_test_bits(struct regmap *map,
1616                                    unsigned int reg, unsigned int bits)
1617 {
1618         WARN_ONCE(1, "regmap API is disabled");
1619         return -EINVAL;
1620 }
1621
1622 static inline int regmap_field_update_bits_base(struct regmap_field *field,
1623                                         unsigned int mask, unsigned int val,
1624                                         bool *change, bool async, bool force)
1625 {
1626         WARN_ONCE(1, "regmap API is disabled");
1627         return -EINVAL;
1628 }
1629
1630 static inline int regmap_fields_update_bits_base(struct regmap_field *field,
1631                                    unsigned int id,
1632                                    unsigned int mask, unsigned int val,
1633                                    bool *change, bool async, bool force)
1634 {
1635         WARN_ONCE(1, "regmap API is disabled");
1636         return -EINVAL;
1637 }
1638
1639 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
1640                                      unsigned int mask, unsigned int val)
1641 {
1642         WARN_ONCE(1, "regmap API is disabled");
1643         return -EINVAL;
1644 }
1645
1646 static inline int regmap_update_bits_async(struct regmap *map, unsigned int reg,
1647                                            unsigned int mask, unsigned int val)
1648 {
1649         WARN_ONCE(1, "regmap API is disabled");
1650         return -EINVAL;
1651 }
1652
1653 static inline int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1654                                            unsigned int mask, unsigned int val,
1655                                            bool *change)
1656 {
1657         WARN_ONCE(1, "regmap API is disabled");
1658         return -EINVAL;
1659 }
1660
1661 static inline int
1662 regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
1663                                unsigned int mask, unsigned int val,
1664                                bool *change)
1665 {
1666         WARN_ONCE(1, "regmap API is disabled");
1667         return -EINVAL;
1668 }
1669
1670 static inline int regmap_write_bits(struct regmap *map, unsigned int reg,
1671                                     unsigned int mask, unsigned int val)
1672 {
1673         WARN_ONCE(1, "regmap API is disabled");
1674         return -EINVAL;
1675 }
1676
1677 static inline int regmap_field_write(struct regmap_field *field,
1678                                      unsigned int val)
1679 {
1680         WARN_ONCE(1, "regmap API is disabled");
1681         return -EINVAL;
1682 }
1683
1684 static inline int regmap_field_force_write(struct regmap_field *field,
1685                                            unsigned int val)
1686 {
1687         WARN_ONCE(1, "regmap API is disabled");
1688         return -EINVAL;
1689 }
1690
1691 static inline int regmap_field_update_bits(struct regmap_field *field,
1692                                            unsigned int mask, unsigned int val)
1693 {
1694         WARN_ONCE(1, "regmap API is disabled");
1695         return -EINVAL;
1696 }
1697
1698 static inline int
1699 regmap_field_force_update_bits(struct regmap_field *field,
1700                                unsigned int mask, unsigned int val)
1701 {
1702         WARN_ONCE(1, "regmap API is disabled");
1703         return -EINVAL;
1704 }
1705
1706 static inline int regmap_fields_write(struct regmap_field *field,
1707                                       unsigned int id, unsigned int val)
1708 {
1709         WARN_ONCE(1, "regmap API is disabled");
1710         return -EINVAL;
1711 }
1712
1713 static inline int regmap_fields_force_write(struct regmap_field *field,
1714                                             unsigned int id, unsigned int val)
1715 {
1716         WARN_ONCE(1, "regmap API is disabled");
1717         return -EINVAL;
1718 }
1719
1720 static inline int
1721 regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1722                           unsigned int mask, unsigned int val)
1723 {
1724         WARN_ONCE(1, "regmap API is disabled");
1725         return -EINVAL;
1726 }
1727
1728 static inline int
1729 regmap_fields_force_update_bits(struct regmap_field *field, unsigned int id,
1730                                 unsigned int mask, unsigned int val)
1731 {
1732         WARN_ONCE(1, "regmap API is disabled");
1733         return -EINVAL;
1734 }
1735
1736 static inline int regmap_get_val_bytes(struct regmap *map)
1737 {
1738         WARN_ONCE(1, "regmap API is disabled");
1739         return -EINVAL;
1740 }
1741
1742 static inline int regmap_get_max_register(struct regmap *map)
1743 {
1744         WARN_ONCE(1, "regmap API is disabled");
1745         return -EINVAL;
1746 }
1747
1748 static inline int regmap_get_reg_stride(struct regmap *map)
1749 {
1750         WARN_ONCE(1, "regmap API is disabled");
1751         return -EINVAL;
1752 }
1753
1754 static inline int regcache_sync(struct regmap *map)
1755 {
1756         WARN_ONCE(1, "regmap API is disabled");
1757         return -EINVAL;
1758 }
1759
1760 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
1761                                        unsigned int max)
1762 {
1763         WARN_ONCE(1, "regmap API is disabled");
1764         return -EINVAL;
1765 }
1766
1767 static inline int regcache_drop_region(struct regmap *map, unsigned int min,
1768                                        unsigned int max)
1769 {
1770         WARN_ONCE(1, "regmap API is disabled");
1771         return -EINVAL;
1772 }
1773
1774 static inline void regcache_cache_only(struct regmap *map, bool enable)
1775 {
1776         WARN_ONCE(1, "regmap API is disabled");
1777 }
1778
1779 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
1780 {
1781         WARN_ONCE(1, "regmap API is disabled");
1782 }
1783
1784 static inline void regcache_mark_dirty(struct regmap *map)
1785 {
1786         WARN_ONCE(1, "regmap API is disabled");
1787 }
1788
1789 static inline void regmap_async_complete(struct regmap *map)
1790 {
1791         WARN_ONCE(1, "regmap API is disabled");
1792 }
1793
1794 static inline int regmap_register_patch(struct regmap *map,
1795                                         const struct reg_sequence *regs,
1796                                         int num_regs)
1797 {
1798         WARN_ONCE(1, "regmap API is disabled");
1799         return -EINVAL;
1800 }
1801
1802 static inline int regmap_parse_val(struct regmap *map, const void *buf,
1803                                 unsigned int *val)
1804 {
1805         WARN_ONCE(1, "regmap API is disabled");
1806         return -EINVAL;
1807 }
1808
1809 static inline struct regmap *dev_get_regmap(struct device *dev,
1810                                             const char *name)
1811 {
1812         return NULL;
1813 }
1814
1815 static inline struct device *regmap_get_device(struct regmap *map)
1816 {
1817         WARN_ONCE(1, "regmap API is disabled");
1818         return NULL;
1819 }
1820
1821 #endif
1822
1823 #endif