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