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