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