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