]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/regmap.h
regmap: Fixup the kernel-doc comments on functions/structures
[linux.git] / include / linux / regmap.h
1 #ifndef __LINUX_REGMAP_H
2 #define __LINUX_REGMAP_H
3
4 /*
5  * Register map access API
6  *
7  * Copyright 2011 Wolfson Microelectronics plc
8  *
9  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15
16 #include <linux/list.h>
17 #include <linux/rbtree.h>
18 #include <linux/err.h>
19 #include <linux/bug.h>
20 #include <linux/lockdep.h>
21
22 struct module;
23 struct device;
24 struct i2c_client;
25 struct irq_domain;
26 struct spi_device;
27 struct spmi_device;
28 struct regmap;
29 struct regmap_range_cfg;
30 struct regmap_field;
31 struct snd_ac97;
32
33 /* An enum of all the supported cache types */
34 enum regcache_type {
35         REGCACHE_NONE,
36         REGCACHE_RBTREE,
37         REGCACHE_COMPRESSED,
38         REGCACHE_FLAT,
39 };
40
41 /**
42  * struct reg_default - Default value for a register.
43  *
44  * @reg: Register address.
45  * @def: Register default value.
46  *
47  * We use an array of structs rather than a simple array as many modern devices
48  * have very sparse register maps.
49  */
50 struct reg_default {
51         unsigned int reg;
52         unsigned int def;
53 };
54
55 /**
56  * struct reg_sequence - An individual write from a sequence of writes.
57  *
58  * @reg: Register address.
59  * @def: Register value.
60  * @delay_us: Delay to be applied after the register write in microseconds
61  *
62  * Register/value pairs for sequences of writes with an optional delay in
63  * microseconds to be applied after each write.
64  */
65 struct reg_sequence {
66         unsigned int reg;
67         unsigned int def;
68         unsigned int delay_us;
69 };
70
71 #define regmap_update_bits(map, reg, mask, val) \
72         regmap_update_bits_base(map, reg, mask, val, NULL, false, false)
73 #define regmap_update_bits_async(map, reg, mask, val)\
74         regmap_update_bits_base(map, reg, mask, val, NULL, true, false)
75 #define regmap_update_bits_check(map, reg, mask, val, change)\
76         regmap_update_bits_base(map, reg, mask, val, change, false, false)
77 #define regmap_update_bits_check_async(map, reg, mask, val, change)\
78         regmap_update_bits_base(map, reg, mask, val, change, true, false)
79
80 #define regmap_write_bits(map, reg, mask, val) \
81         regmap_update_bits_base(map, reg, mask, val, NULL, false, true)
82
83 #define regmap_field_write(field, val) \
84         regmap_field_update_bits_base(field, ~0, val, NULL, false, false)
85 #define regmap_field_force_write(field, val) \
86         regmap_field_update_bits_base(field, ~0, val, NULL, false, true)
87 #define regmap_field_update_bits(field, mask, val)\
88         regmap_field_update_bits_base(field, mask, val, NULL, false, false)
89 #define regmap_field_force_update_bits(field, mask, val) \
90         regmap_field_update_bits_base(field, mask, val, NULL, false, true)
91
92 #define regmap_fields_write(field, id, val) \
93         regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, false)
94 #define regmap_fields_force_write(field, id, val) \
95         regmap_fields_update_bits_base(field, id, ~0, val, NULL, false, true)
96 #define regmap_fields_update_bits(field, id, mask, val)\
97         regmap_fields_update_bits_base(field, id, mask, val, NULL, false, false)
98 #define regmap_fields_force_update_bits(field, id, mask, val) \
99         regmap_fields_update_bits_base(field, id, mask, val, NULL, false, true)
100
101 /**
102  * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs
103  *
104  * @map: Regmap to read from
105  * @addr: Address to poll
106  * @val: Unsigned integer variable to read the value into
107  * @cond: Break condition (usually involving @val)
108  * @sleep_us: Maximum time to sleep between reads in us (0
109  *            tight-loops).  Should be less than ~20ms since usleep_range
110  *            is used (see Documentation/timers/timers-howto.txt).
111  * @timeout_us: Timeout in us, 0 means never timeout
112  *
113  * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read
114  * error return value in case of a error read. In the two former cases,
115  * the last read value at @addr is stored in @val. Must not be called
116  * from atomic context if sleep_us or timeout_us are used.
117  *
118  * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
119  */
120 #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
121 ({ \
122         ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
123         int ret; \
124         might_sleep_if(sleep_us); \
125         for (;;) { \
126                 ret = regmap_read((map), (addr), &(val)); \
127                 if (ret) \
128                         break; \
129                 if (cond) \
130                         break; \
131                 if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
132                         ret = regmap_read((map), (addr), &(val)); \
133                         break; \
134                 } \
135                 if (sleep_us) \
136                         usleep_range((sleep_us >> 2) + 1, sleep_us); \
137         } \
138         ret ?: ((cond) ? 0 : -ETIMEDOUT); \
139 })
140
141 #ifdef CONFIG_REGMAP
142
143 enum regmap_endian {
144         /* Unspecified -> 0 -> Backwards compatible default */
145         REGMAP_ENDIAN_DEFAULT = 0,
146         REGMAP_ENDIAN_BIG,
147         REGMAP_ENDIAN_LITTLE,
148         REGMAP_ENDIAN_NATIVE,
149 };
150
151 /**
152  * struct regmap_range - A register range, used for access related checks
153  *                       (readable/writeable/volatile/precious checks)
154  *
155  * @range_min: address of first register
156  * @range_max: address of last register
157  */
158 struct regmap_range {
159         unsigned int range_min;
160         unsigned int range_max;
161 };
162
163 #define regmap_reg_range(low, high) { .range_min = low, .range_max = high, }
164
165 /**
166  * struct regmap_access_table - A table of register ranges for access checks
167  *
168  * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
169  * @n_yes_ranges: size of the above array
170  * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
171  * @n_no_ranges: size of the above array
172  *
173  * A table of ranges including some yes ranges and some no ranges.
174  * If a register belongs to a no_range, the corresponding check function
175  * will return false. If a register belongs to a yes range, the corresponding
176  * check function will return true. "no_ranges" are searched first.
177  */
178 struct regmap_access_table {
179         const struct regmap_range *yes_ranges;
180         unsigned int n_yes_ranges;
181         const struct regmap_range *no_ranges;
182         unsigned int n_no_ranges;
183 };
184
185 typedef void (*regmap_lock)(void *);
186 typedef void (*regmap_unlock)(void *);
187
188 /**
189  * struct regmap_config - Configuration for the register map of a device.
190  *
191  * @name: Optional name of the regmap. Useful when a device has multiple
192  *        register regions.
193  *
194  * @reg_bits: Number of bits in a register address, mandatory.
195  * @reg_stride: The register address stride. Valid register addresses are a
196  *              multiple of this value. If set to 0, a value of 1 will be
197  *              used.
198  * @pad_bits: Number of bits of padding between register and value.
199  * @val_bits: Number of bits in a register value, mandatory.
200  *
201  * @writeable_reg: Optional callback returning true if the register
202  *                 can be written to. If this field is NULL but wr_table
203  *                 (see below) is not, the check is performed on such table
204  *                 (a register is writeable if it belongs to one of the ranges
205  *                  specified by wr_table).
206  * @readable_reg: Optional callback returning true if the register
207  *                can be read from. If this field is NULL but rd_table
208  *                 (see below) is not, the check is performed on such table
209  *                 (a register is readable if it belongs to one of the ranges
210  *                  specified by rd_table).
211  * @volatile_reg: Optional callback returning true if the register
212  *                value can't be cached. If this field is NULL but
213  *                volatile_table (see below) is not, the check is performed on
214  *                such table (a register is volatile if it belongs to one of
215  *                the ranges specified by volatile_table).
216  * @precious_reg: Optional callback returning true if the register
217  *                should not be read outside of a call from the driver
218  *                (e.g., a clear on read interrupt status register). If this
219  *                field is NULL but precious_table (see below) is not, the
220  *                check is performed on such table (a register is precious if
221  *                it belongs to one of the ranges specified by precious_table).
222  * @lock:         Optional lock callback (overrides regmap's default lock
223  *                function, based on spinlock or mutex).
224  * @unlock:       As above for unlocking.
225  * @lock_arg:     this field is passed as the only argument of lock/unlock
226  *                functions (ignored in case regular lock/unlock functions
227  *                are not overridden).
228  * @reg_read:     Optional callback that if filled will be used to perform
229  *                all the reads from the registers. Should only be provided for
230  *                devices whose read operation cannot be represented as a simple
231  *                read operation on a bus such as SPI, I2C, etc. Most of the
232  *                devices do not need this.
233  * @reg_write:    Same as above for writing.
234  * @fast_io:      Register IO is fast. Use a spinlock instead of a mutex
235  *                to perform locking. This field is ignored if custom lock/unlock
236  *                functions are used (see fields lock/unlock of struct regmap_config).
237  *                This field is a duplicate of a similar file in
238  *                'struct regmap_bus' and serves exact same purpose.
239  *                 Use it only for "no-bus" cases.
240  * @max_register: Optional, specifies the maximum valid register address.
241  * @wr_table:     Optional, points to a struct regmap_access_table specifying
242  *                valid ranges for write access.
243  * @rd_table:     As above, for read access.
244  * @volatile_table: As above, for volatile registers.
245  * @precious_table: As above, for precious registers.
246  * @reg_defaults: Power on reset values for registers (for use with
247  *                register cache support).
248  * @num_reg_defaults: Number of elements in reg_defaults.
249  *
250  * @read_flag_mask: Mask to be set in the top bytes of the register when doing
251  *                  a read.
252  * @write_flag_mask: Mask to be set in the top bytes of the register when doing
253  *                   a write. If both read_flag_mask and write_flag_mask are
254  *                   empty the regmap_bus default masks are used.
255  * @use_single_rw: If set, converts the bulk read and write operations into
256  *                  a series of single read and write operations. This is useful
257  *                  for device that does not support bulk read and write.
258  * @can_multi_write: If set, the device supports the multi write mode of bulk
259  *                   write operations, if clear multi write requests will be
260  *                   split into individual write operations
261  *
262  * @cache_type: The actual cache type.
263  * @reg_defaults_raw: Power on reset values for registers (for use with
264  *                    register cache support).
265  * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
266  * @reg_format_endian: Endianness for formatted register addresses. If this is
267  *                     DEFAULT, the @reg_format_endian_default value from the
268  *                     regmap bus is used.
269  * @val_format_endian: Endianness for formatted register values. If this is
270  *                     DEFAULT, the @reg_format_endian_default value from the
271  *                     regmap bus is used.
272  *
273  * @ranges: Array of configuration entries for virtual address ranges.
274  * @num_ranges: Number of range configuration entries.
275  */
276 struct regmap_config {
277         const char *name;
278
279         int reg_bits;
280         int reg_stride;
281         int pad_bits;
282         int val_bits;
283
284         bool (*writeable_reg)(struct device *dev, unsigned int reg);
285         bool (*readable_reg)(struct device *dev, unsigned int reg);
286         bool (*volatile_reg)(struct device *dev, unsigned int reg);
287         bool (*precious_reg)(struct device *dev, unsigned int reg);
288         regmap_lock lock;
289         regmap_unlock unlock;
290         void *lock_arg;
291
292         int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
293         int (*reg_write)(void *context, unsigned int reg, unsigned int val);
294
295         bool fast_io;
296
297         unsigned int max_register;
298         const struct regmap_access_table *wr_table;
299         const struct regmap_access_table *rd_table;
300         const struct regmap_access_table *volatile_table;
301         const struct regmap_access_table *precious_table;
302         const struct reg_default *reg_defaults;
303         unsigned int num_reg_defaults;
304         enum regcache_type cache_type;
305         const void *reg_defaults_raw;
306         unsigned int num_reg_defaults_raw;
307
308         unsigned long read_flag_mask;
309         unsigned long write_flag_mask;
310
311         bool use_single_rw;
312         bool can_multi_write;
313
314         enum regmap_endian reg_format_endian;
315         enum regmap_endian val_format_endian;
316
317         const struct regmap_range_cfg *ranges;
318         unsigned int num_ranges;
319 };
320
321 /**
322  * struct regmap_range_cfg - Configuration for indirectly accessed or paged
323  *                           registers.
324  *
325  * @name: Descriptive name for diagnostics
326  *
327  * @range_min: Address of the lowest register address in virtual range.
328  * @range_max: Address of the highest register in virtual range.
329  *
330  * @selector_reg: Register with selector field.
331  * @selector_mask: Bit shift for selector value.
332  * @selector_shift: Bit mask for selector value.
333  *
334  * @window_start: Address of first (lowest) register in data window.
335  * @window_len: Number of registers in data window.
336  *
337  * Registers, mapped to this virtual range, are accessed in two steps:
338  *     1. page selector register update;
339  *     2. access through data window registers.
340  */
341 struct regmap_range_cfg {
342         const char *name;
343
344         /* Registers of virtual address range */
345         unsigned int range_min;
346         unsigned int range_max;
347
348         /* Page selector for indirect addressing */
349         unsigned int selector_reg;
350         unsigned int selector_mask;
351         int selector_shift;
352
353         /* Data window (per each page) */
354         unsigned int window_start;
355         unsigned int window_len;
356 };
357
358 struct regmap_async;
359
360 typedef int (*regmap_hw_write)(void *context, const void *data,
361                                size_t count);
362 typedef int (*regmap_hw_gather_write)(void *context,
363                                       const void *reg, size_t reg_len,
364                                       const void *val, size_t val_len);
365 typedef int (*regmap_hw_async_write)(void *context,
366                                      const void *reg, size_t reg_len,
367                                      const void *val, size_t val_len,
368                                      struct regmap_async *async);
369 typedef int (*regmap_hw_read)(void *context,
370                               const void *reg_buf, size_t reg_size,
371                               void *val_buf, size_t val_size);
372 typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg,
373                                   unsigned int *val);
374 typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg,
375                                    unsigned int val);
376 typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg,
377                                          unsigned int mask, unsigned int val);
378 typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
379 typedef void (*regmap_hw_free_context)(void *context);
380
381 /**
382  * struct regmap_bus - Description of a hardware bus for the register map
383  *                     infrastructure.
384  *
385  * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
386  *           to perform locking. This field is ignored if custom lock/unlock
387  *           functions are used (see fields lock/unlock of
388  *           struct regmap_config).
389  * @write: Write operation.
390  * @gather_write: Write operation with split register/value, return -ENOTSUPP
391  *                if not implemented  on a given device.
392  * @async_write: Write operation which completes asynchronously, optional and
393  *               must serialise with respect to non-async I/O.
394  * @reg_write: Write a single register value to the given register address. This
395  *             write operation has to complete when returning from the function.
396  * @reg_update_bits: Update bits operation to be used against volatile
397  *                   registers, intended for devices supporting some mechanism
398  *                   for setting clearing bits without having to
399  *                   read/modify/write.
400  * @read: Read operation.  Data is returned in the buffer used to transmit
401  *         data.
402  * @reg_read: Read a single register value from a given register address.
403  * @free_context: Free context.
404  * @async_alloc: Allocate a regmap_async() structure.
405  * @read_flag_mask: Mask to be set in the top byte of the register when doing
406  *                  a read.
407  * @reg_format_endian_default: Default endianness for formatted register
408  *     addresses. Used when the regmap_config specifies DEFAULT. If this is
409  *     DEFAULT, BIG is assumed.
410  * @val_format_endian_default: Default endianness for formatted register
411  *     values. Used when the regmap_config specifies DEFAULT. If this is
412  *     DEFAULT, BIG is assumed.
413  * @max_raw_read: Max raw read size that can be used on the bus.
414  * @max_raw_write: Max raw write size that can be used on the bus.
415  */
416 struct regmap_bus {
417         bool fast_io;
418         regmap_hw_write write;
419         regmap_hw_gather_write gather_write;
420         regmap_hw_async_write async_write;
421         regmap_hw_reg_write reg_write;
422         regmap_hw_reg_update_bits reg_update_bits;
423         regmap_hw_read read;
424         regmap_hw_reg_read reg_read;
425         regmap_hw_free_context free_context;
426         regmap_hw_async_alloc async_alloc;
427         u8 read_flag_mask;
428         enum regmap_endian reg_format_endian_default;
429         enum regmap_endian val_format_endian_default;
430         size_t max_raw_read;
431         size_t max_raw_write;
432 };
433
434 /*
435  * __regmap_init functions.
436  *
437  * These functions take a lock key and name parameter, and should not be called
438  * directly. Instead, use the regmap_init macros that generate a key and name
439  * for each call.
440  */
441 struct regmap *__regmap_init(struct device *dev,
442                              const struct regmap_bus *bus,
443                              void *bus_context,
444                              const struct regmap_config *config,
445                              struct lock_class_key *lock_key,
446                              const char *lock_name);
447 struct regmap *__regmap_init_i2c(struct i2c_client *i2c,
448                                  const struct regmap_config *config,
449                                  struct lock_class_key *lock_key,
450                                  const char *lock_name);
451 struct regmap *__regmap_init_spi(struct spi_device *dev,
452                                  const struct regmap_config *config,
453                                  struct lock_class_key *lock_key,
454                                  const char *lock_name);
455 struct regmap *__regmap_init_spmi_base(struct spmi_device *dev,
456                                        const struct regmap_config *config,
457                                        struct lock_class_key *lock_key,
458                                        const char *lock_name);
459 struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
460                                       const struct regmap_config *config,
461                                       struct lock_class_key *lock_key,
462                                       const char *lock_name);
463 struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
464                                       void __iomem *regs,
465                                       const struct regmap_config *config,
466                                       struct lock_class_key *lock_key,
467                                       const char *lock_name);
468 struct regmap *__regmap_init_ac97(struct snd_ac97 *ac97,
469                                   const struct regmap_config *config,
470                                   struct lock_class_key *lock_key,
471                                   const char *lock_name);
472
473 struct regmap *__devm_regmap_init(struct device *dev,
474                                   const struct regmap_bus *bus,
475                                   void *bus_context,
476                                   const struct regmap_config *config,
477                                   struct lock_class_key *lock_key,
478                                   const char *lock_name);
479 struct regmap *__devm_regmap_init_i2c(struct i2c_client *i2c,
480                                       const struct regmap_config *config,
481                                       struct lock_class_key *lock_key,
482                                       const char *lock_name);
483 struct regmap *__devm_regmap_init_spi(struct spi_device *dev,
484                                       const struct regmap_config *config,
485                                       struct lock_class_key *lock_key,
486                                       const char *lock_name);
487 struct regmap *__devm_regmap_init_spmi_base(struct spmi_device *dev,
488                                             const struct regmap_config *config,
489                                             struct lock_class_key *lock_key,
490                                             const char *lock_name);
491 struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
492                                            const struct regmap_config *config,
493                                            struct lock_class_key *lock_key,
494                                            const char *lock_name);
495 struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
496                                            const char *clk_id,
497                                            void __iomem *regs,
498                                            const struct regmap_config *config,
499                                            struct lock_class_key *lock_key,
500                                            const char *lock_name);
501 struct regmap *__devm_regmap_init_ac97(struct snd_ac97 *ac97,
502                                        const struct regmap_config *config,
503                                        struct lock_class_key *lock_key,
504                                        const char *lock_name);
505
506 /*
507  * Wrapper for regmap_init macros to include a unique lockdep key and name
508  * for each call. No-op if CONFIG_LOCKDEP is not set.
509  *
510  * @fn: Real function to call (in the form __[*_]regmap_init[_*])
511  * @name: Config variable name (#config in the calling macro)
512  **/
513 #ifdef CONFIG_LOCKDEP
514 #define __regmap_lockdep_wrapper(fn, name, ...)                         \
515 (                                                                       \
516         ({                                                              \
517                 static struct lock_class_key _key;                      \
518                 fn(__VA_ARGS__, &_key,                                  \
519                         KBUILD_BASENAME ":"                             \
520                         __stringify(__LINE__) ":"                       \
521                         "(" name ")->lock");                            \
522         })                                                              \
523 )
524 #else
525 #define __regmap_lockdep_wrapper(fn, name, ...) fn(__VA_ARGS__, NULL, NULL)
526 #endif
527
528 /**
529  * regmap_init() - Initialise register map
530  *
531  * @dev: Device that will be interacted with
532  * @bus: Bus-specific callbacks to use with device
533  * @bus_context: Data passed to bus-specific callbacks
534  * @config: Configuration for register map
535  *
536  * The return value will be an ERR_PTR() on error or a valid pointer to
537  * a struct regmap.  This function should generally not be called
538  * directly, it should be called by bus-specific init functions.
539  */
540 #define regmap_init(dev, bus, bus_context, config)                      \
541         __regmap_lockdep_wrapper(__regmap_init, #config,                \
542                                 dev, bus, bus_context, config)
543 int regmap_attach_dev(struct device *dev, struct regmap *map,
544                       const struct regmap_config *config);
545
546 /**
547  * regmap_init_i2c() - Initialise register map
548  *
549  * @i2c: Device that will be interacted with
550  * @config: Configuration for register map
551  *
552  * The return value will be an ERR_PTR() on error or a valid pointer to
553  * a struct regmap.
554  */
555 #define regmap_init_i2c(i2c, config)                                    \
556         __regmap_lockdep_wrapper(__regmap_init_i2c, #config,            \
557                                 i2c, config)
558
559 /**
560  * regmap_init_spi() - Initialise register map
561  *
562  * @dev: Device that will be interacted with
563  * @config: Configuration for register map
564  *
565  * The return value will be an ERR_PTR() on error or a valid pointer to
566  * a struct regmap.
567  */
568 #define regmap_init_spi(dev, config)                                    \
569         __regmap_lockdep_wrapper(__regmap_init_spi, #config,            \
570                                 dev, config)
571
572 /**
573  * regmap_init_spmi_base() - Create regmap for the Base register space
574  *
575  * @dev:        SPMI device that will be interacted with
576  * @config:     Configuration for register map
577  *
578  * The return value will be an ERR_PTR() on error or a valid pointer to
579  * a struct regmap.
580  */
581 #define regmap_init_spmi_base(dev, config)                              \
582         __regmap_lockdep_wrapper(__regmap_init_spmi_base, #config,      \
583                                 dev, config)
584
585 /**
586  * regmap_init_spmi_ext() - Create regmap for Ext register space
587  *
588  * @dev:        Device that will be interacted with
589  * @config:     Configuration for register map
590  *
591  * The return value will be an ERR_PTR() on error or a valid pointer to
592  * a struct regmap.
593  */
594 #define regmap_init_spmi_ext(dev, config)                               \
595         __regmap_lockdep_wrapper(__regmap_init_spmi_ext, #config,       \
596                                 dev, config)
597
598 /**
599  * regmap_init_mmio_clk() - Initialise register map with register clock
600  *
601  * @dev: Device that will be interacted with
602  * @clk_id: register clock consumer ID
603  * @regs: Pointer to memory-mapped IO region
604  * @config: Configuration for register map
605  *
606  * The return value will be an ERR_PTR() on error or a valid pointer to
607  * a struct regmap.
608  */
609 #define regmap_init_mmio_clk(dev, clk_id, regs, config)                 \
610         __regmap_lockdep_wrapper(__regmap_init_mmio_clk, #config,       \
611                                 dev, clk_id, regs, config)
612
613 /**
614  * regmap_init_mmio() - Initialise register map
615  *
616  * @dev: Device that will be interacted with
617  * @regs: Pointer to memory-mapped IO region
618  * @config: Configuration for register map
619  *
620  * The return value will be an ERR_PTR() on error or a valid pointer to
621  * a struct regmap.
622  */
623 #define regmap_init_mmio(dev, regs, config)             \
624         regmap_init_mmio_clk(dev, NULL, regs, config)
625
626 /**
627  * regmap_init_ac97() - Initialise AC'97 register map
628  *
629  * @ac97: Device that will be interacted with
630  * @config: Configuration for register map
631  *
632  * The return value will be an ERR_PTR() on error or a valid pointer to
633  * a struct regmap.
634  */
635 #define regmap_init_ac97(ac97, config)                                  \
636         __regmap_lockdep_wrapper(__regmap_init_ac97, #config,           \
637                                 ac97, config)
638 bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
639
640 /**
641  * devm_regmap_init() - Initialise managed register map
642  *
643  * @dev: Device that will be interacted with
644  * @bus: Bus-specific callbacks to use with device
645  * @bus_context: Data passed to bus-specific callbacks
646  * @config: Configuration for register map
647  *
648  * The return value will be an ERR_PTR() on error or a valid pointer
649  * to a struct regmap.  This function should generally not be called
650  * directly, it should be called by bus-specific init functions.  The
651  * map will be automatically freed by the device management code.
652  */
653 #define devm_regmap_init(dev, bus, bus_context, config)                 \
654         __regmap_lockdep_wrapper(__devm_regmap_init, #config,           \
655                                 dev, bus, bus_context, config)
656
657 /**
658  * devm_regmap_init_i2c() - Initialise managed register map
659  *
660  * @i2c: Device that will be interacted with
661  * @config: Configuration for register map
662  *
663  * The return value will be an ERR_PTR() on error or a valid pointer
664  * to a struct regmap.  The regmap will be automatically freed by the
665  * device management code.
666  */
667 #define devm_regmap_init_i2c(i2c, config)                               \
668         __regmap_lockdep_wrapper(__devm_regmap_init_i2c, #config,       \
669                                 i2c, config)
670
671 /**
672  * devm_regmap_init_spi() - Initialise register map
673  *
674  * @dev: Device that will be interacted with
675  * @config: Configuration for register map
676  *
677  * The return value will be an ERR_PTR() on error or a valid pointer
678  * to a struct regmap.  The map will be automatically freed by the
679  * device management code.
680  */
681 #define devm_regmap_init_spi(dev, config)                               \
682         __regmap_lockdep_wrapper(__devm_regmap_init_spi, #config,       \
683                                 dev, config)
684
685 /**
686  * devm_regmap_init_spmi_base() - Create managed regmap for Base register space
687  *
688  * @dev:        SPMI device that will be interacted with
689  * @config:     Configuration for register map
690  *
691  * The return value will be an ERR_PTR() on error or a valid pointer
692  * to a struct regmap.  The regmap will be automatically freed by the
693  * device management code.
694  */
695 #define devm_regmap_init_spmi_base(dev, config)                         \
696         __regmap_lockdep_wrapper(__devm_regmap_init_spmi_base, #config, \
697                                 dev, config)
698
699 /**
700  * devm_regmap_init_spmi_ext() - Create managed regmap for Ext register space
701  *
702  * @dev:        SPMI device that will be interacted with
703  * @config:     Configuration for register map
704  *
705  * The return value will be an ERR_PTR() on error or a valid pointer
706  * to a struct regmap.  The regmap will be automatically freed by the
707  * device management code.
708  */
709 #define devm_regmap_init_spmi_ext(dev, config)                          \
710         __regmap_lockdep_wrapper(__devm_regmap_init_spmi_ext, #config,  \
711                                 dev, config)
712
713 /**
714  * devm_regmap_init_mmio_clk() - Initialise managed register map with clock
715  *
716  * @dev: Device that will be interacted with
717  * @clk_id: register clock consumer ID
718  * @regs: Pointer to memory-mapped IO region
719  * @config: Configuration for register map
720  *
721  * The return value will be an ERR_PTR() on error or a valid pointer
722  * to a struct regmap.  The regmap will be automatically freed by the
723  * device management code.
724  */
725 #define devm_regmap_init_mmio_clk(dev, clk_id, regs, config)            \
726         __regmap_lockdep_wrapper(__devm_regmap_init_mmio_clk, #config,  \
727                                 dev, clk_id, regs, config)
728
729 /**
730  * devm_regmap_init_mmio() - Initialise managed register map
731  *
732  * @dev: Device that will be interacted with
733  * @regs: Pointer to memory-mapped IO region
734  * @config: Configuration for register map
735  *
736  * The return value will be an ERR_PTR() on error or a valid pointer
737  * to a struct regmap.  The regmap will be automatically freed by the
738  * device management code.
739  */
740 #define devm_regmap_init_mmio(dev, regs, config)                \
741         devm_regmap_init_mmio_clk(dev, NULL, regs, config)
742
743 /**
744  * devm_regmap_init_ac97() - Initialise AC'97 register map
745  *
746  * @ac97: Device that will be interacted with
747  * @config: Configuration for register map
748  *
749  * The return value will be an ERR_PTR() on error or a valid pointer
750  * to a struct regmap.  The regmap will be automatically freed by the
751  * device management code.
752  */
753 #define devm_regmap_init_ac97(ac97, config)                             \
754         __regmap_lockdep_wrapper(__devm_regmap_init_ac97, #config,      \
755                                 ac97, config)
756
757 void regmap_exit(struct regmap *map);
758 int regmap_reinit_cache(struct regmap *map,
759                         const struct regmap_config *config);
760 struct regmap *dev_get_regmap(struct device *dev, const char *name);
761 struct device *regmap_get_device(struct regmap *map);
762 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
763 int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val);
764 int regmap_raw_write(struct regmap *map, unsigned int reg,
765                      const void *val, size_t val_len);
766 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
767                         size_t val_count);
768 int regmap_multi_reg_write(struct regmap *map, const struct reg_sequence *regs,
769                         int num_regs);
770 int regmap_multi_reg_write_bypassed(struct regmap *map,
771                                     const struct reg_sequence *regs,
772                                     int num_regs);
773 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
774                            const void *val, size_t val_len);
775 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
776 int regmap_raw_read(struct regmap *map, unsigned int reg,
777                     void *val, size_t val_len);
778 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
779                      size_t val_count);
780 int regmap_update_bits_base(struct regmap *map, unsigned int reg,
781                             unsigned int mask, unsigned int val,
782                             bool *change, bool async, bool force);
783 int regmap_get_val_bytes(struct regmap *map);
784 int regmap_get_max_register(struct regmap *map);
785 int regmap_get_reg_stride(struct regmap *map);
786 int regmap_async_complete(struct regmap *map);
787 bool regmap_can_raw_write(struct regmap *map);
788 size_t regmap_get_raw_read_max(struct regmap *map);
789 size_t regmap_get_raw_write_max(struct regmap *map);
790
791 int regcache_sync(struct regmap *map);
792 int regcache_sync_region(struct regmap *map, unsigned int min,
793                          unsigned int max);
794 int regcache_drop_region(struct regmap *map, unsigned int min,
795                          unsigned int max);
796 void regcache_cache_only(struct regmap *map, bool enable);
797 void regcache_cache_bypass(struct regmap *map, bool enable);
798 void regcache_mark_dirty(struct regmap *map);
799
800 bool regmap_check_range_table(struct regmap *map, unsigned int reg,
801                               const struct regmap_access_table *table);
802
803 int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
804                           int num_regs);
805 int regmap_parse_val(struct regmap *map, const void *buf,
806                                 unsigned int *val);
807
808 static inline bool regmap_reg_in_range(unsigned int reg,
809                                        const struct regmap_range *range)
810 {
811         return reg >= range->range_min && reg <= range->range_max;
812 }
813
814 bool regmap_reg_in_ranges(unsigned int reg,
815                           const struct regmap_range *ranges,
816                           unsigned int nranges);
817
818 /**
819  * struct reg_field - Description of an register field
820  *
821  * @reg: Offset of the register within the regmap bank
822  * @lsb: lsb of the register field.
823  * @msb: msb of the register field.
824  * @id_size: port size if it has some ports
825  * @id_offset: address offset for each ports
826  */
827 struct reg_field {
828         unsigned int reg;
829         unsigned int lsb;
830         unsigned int msb;
831         unsigned int id_size;
832         unsigned int id_offset;
833 };
834
835 #define REG_FIELD(_reg, _lsb, _msb) {           \
836                                 .reg = _reg,    \
837                                 .lsb = _lsb,    \
838                                 .msb = _msb,    \
839                                 }
840
841 struct regmap_field *regmap_field_alloc(struct regmap *regmap,
842                 struct reg_field reg_field);
843 void regmap_field_free(struct regmap_field *field);
844
845 struct regmap_field *devm_regmap_field_alloc(struct device *dev,
846                 struct regmap *regmap, struct reg_field reg_field);
847 void devm_regmap_field_free(struct device *dev, struct regmap_field *field);
848
849 int regmap_field_read(struct regmap_field *field, unsigned int *val);
850 int regmap_field_update_bits_base(struct regmap_field *field,
851                                   unsigned int mask, unsigned int val,
852                                   bool *change, bool async, bool force);
853 int regmap_fields_read(struct regmap_field *field, unsigned int id,
854                        unsigned int *val);
855 int regmap_fields_update_bits_base(struct regmap_field *field,  unsigned int id,
856                                    unsigned int mask, unsigned int val,
857                                    bool *change, bool async, bool force);
858
859 /**
860  * struct regmap_irq - Description of an IRQ for the generic regmap irq_chip.
861  *
862  * @reg_offset: Offset of the status/mask register within the bank
863  * @mask:       Mask used to flag/control the register.
864  * @type_reg_offset: Offset register for the irq type setting.
865  * @type_rising_mask: Mask bit to configure RISING type irq.
866  * @type_falling_mask: Mask bit to configure FALLING type irq.
867  */
868 struct regmap_irq {
869         unsigned int reg_offset;
870         unsigned int mask;
871         unsigned int type_reg_offset;
872         unsigned int type_rising_mask;
873         unsigned int type_falling_mask;
874 };
875
876 #define REGMAP_IRQ_REG(_irq, _off, _mask)               \
877         [_irq] = { .reg_offset = (_off), .mask = (_mask) }
878
879 /**
880  * struct regmap_irq_chip - Description of a generic regmap irq_chip.
881  *
882  * @name:        Descriptive name for IRQ controller.
883  *
884  * @status_base: Base status register address.
885  * @mask_base:   Base mask register address.
886  * @unmask_base:  Base unmask register address. for chips who have
887  *                separate mask and unmask registers
888  * @ack_base:    Base ack address. If zero then the chip is clear on read.
889  *               Using zero value is possible with @use_ack bit.
890  * @wake_base:   Base address for wake enables.  If zero unsupported.
891  * @type_base:   Base address for irq type.  If zero unsupported.
892  * @irq_reg_stride:  Stride to use for chips where registers are not contiguous.
893  * @init_ack_masked: Ack all masked interrupts once during initalization.
894  * @mask_invert: Inverted mask register: cleared bits are masked out.
895  * @use_ack:     Use @ack register even if it is zero.
896  * @ack_invert:  Inverted ack register: cleared bits for ack.
897  * @wake_invert: Inverted wake register: cleared bits are wake enabled.
898  * @type_invert: Invert the type flags.
899  * @runtime_pm:  Hold a runtime PM lock on the device when accessing it.
900  *
901  * @num_regs:    Number of registers in each control bank.
902  * @irqs:        Descriptors for individual IRQs.  Interrupt numbers are
903  *               assigned based on the index in the array of the interrupt.
904  * @num_irqs:    Number of descriptors.
905  * @num_type_reg:    Number of type registers.
906  * @type_reg_stride: Stride to use for chips where type registers are not
907  *                      contiguous.
908  * @handle_pre_irq:  Driver specific callback to handle interrupt from device
909  *                   before regmap_irq_handler process the interrupts.
910  * @handle_post_irq: Driver specific callback to handle interrupt from device
911  *                   after handling the interrupts in regmap_irq_handler().
912  * @irq_drv_data:    Driver specific IRQ data which is passed as parameter when
913  *                   driver specific pre/post interrupt handler is called.
914  *
915  * This is not intended to handle every possible interrupt controller, but
916  * it should handle a substantial proportion of those that are found in the
917  * wild.
918  */
919 struct regmap_irq_chip {
920         const char *name;
921
922         unsigned int status_base;
923         unsigned int mask_base;
924         unsigned int unmask_base;
925         unsigned int ack_base;
926         unsigned int wake_base;
927         unsigned int type_base;
928         unsigned int irq_reg_stride;
929         bool init_ack_masked:1;
930         bool mask_invert:1;
931         bool use_ack:1;
932         bool ack_invert:1;
933         bool wake_invert:1;
934         bool runtime_pm:1;
935         bool type_invert:1;
936
937         int num_regs;
938
939         const struct regmap_irq *irqs;
940         int num_irqs;
941
942         int num_type_reg;
943         unsigned int type_reg_stride;
944
945         int (*handle_pre_irq)(void *irq_drv_data);
946         int (*handle_post_irq)(void *irq_drv_data);
947         void *irq_drv_data;
948 };
949
950 struct regmap_irq_chip_data;
951
952 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
953                         int irq_base, const struct regmap_irq_chip *chip,
954                         struct regmap_irq_chip_data **data);
955 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
956
957 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
958                              int irq_flags, int irq_base,
959                              const struct regmap_irq_chip *chip,
960                              struct regmap_irq_chip_data **data);
961 void devm_regmap_del_irq_chip(struct device *dev, int irq,
962                               struct regmap_irq_chip_data *data);
963
964 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
965 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
966 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
967
968 #else
969
970 /*
971  * These stubs should only ever be called by generic code which has
972  * regmap based facilities, if they ever get called at runtime
973  * something is going wrong and something probably needs to select
974  * REGMAP.
975  */
976
977 static inline int regmap_write(struct regmap *map, unsigned int reg,
978                                unsigned int val)
979 {
980         WARN_ONCE(1, "regmap API is disabled");
981         return -EINVAL;
982 }
983
984 static inline int regmap_write_async(struct regmap *map, unsigned int reg,
985                                      unsigned int val)
986 {
987         WARN_ONCE(1, "regmap API is disabled");
988         return -EINVAL;
989 }
990
991 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
992                                    const void *val, size_t val_len)
993 {
994         WARN_ONCE(1, "regmap API is disabled");
995         return -EINVAL;
996 }
997
998 static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
999                                          const void *val, size_t val_len)
1000 {
1001         WARN_ONCE(1, "regmap API is disabled");
1002         return -EINVAL;
1003 }
1004
1005 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
1006                                     const void *val, size_t val_count)
1007 {
1008         WARN_ONCE(1, "regmap API is disabled");
1009         return -EINVAL;
1010 }
1011
1012 static inline int regmap_read(struct regmap *map, unsigned int reg,
1013                               unsigned int *val)
1014 {
1015         WARN_ONCE(1, "regmap API is disabled");
1016         return -EINVAL;
1017 }
1018
1019 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
1020                                   void *val, size_t val_len)
1021 {
1022         WARN_ONCE(1, "regmap API is disabled");
1023         return -EINVAL;
1024 }
1025
1026 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
1027                                    void *val, size_t val_count)
1028 {
1029         WARN_ONCE(1, "regmap API is disabled");
1030         return -EINVAL;
1031 }
1032
1033 static inline int regmap_update_bits_base(struct regmap *map, unsigned int reg,
1034                                           unsigned int mask, unsigned int val,
1035                                           bool *change, bool async, bool force)
1036 {
1037         WARN_ONCE(1, "regmap API is disabled");
1038         return -EINVAL;
1039 }
1040
1041 static inline int regmap_field_update_bits_base(struct regmap_field *field,
1042                                         unsigned int mask, unsigned int val,
1043                                         bool *change, bool async, bool force)
1044 {
1045         WARN_ONCE(1, "regmap API is disabled");
1046         return -EINVAL;
1047 }
1048
1049 static inline int regmap_fields_update_bits_base(struct regmap_field *field,
1050                                    unsigned int id,
1051                                    unsigned int mask, unsigned int val,
1052                                    bool *change, bool async, bool force)
1053 {
1054         WARN_ONCE(1, "regmap API is disabled");
1055         return -EINVAL;
1056 }
1057
1058 static inline int regmap_get_val_bytes(struct regmap *map)
1059 {
1060         WARN_ONCE(1, "regmap API is disabled");
1061         return -EINVAL;
1062 }
1063
1064 static inline int regmap_get_max_register(struct regmap *map)
1065 {
1066         WARN_ONCE(1, "regmap API is disabled");
1067         return -EINVAL;
1068 }
1069
1070 static inline int regmap_get_reg_stride(struct regmap *map)
1071 {
1072         WARN_ONCE(1, "regmap API is disabled");
1073         return -EINVAL;
1074 }
1075
1076 static inline int regcache_sync(struct regmap *map)
1077 {
1078         WARN_ONCE(1, "regmap API is disabled");
1079         return -EINVAL;
1080 }
1081
1082 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
1083                                        unsigned int max)
1084 {
1085         WARN_ONCE(1, "regmap API is disabled");
1086         return -EINVAL;
1087 }
1088
1089 static inline int regcache_drop_region(struct regmap *map, unsigned int min,
1090                                        unsigned int max)
1091 {
1092         WARN_ONCE(1, "regmap API is disabled");
1093         return -EINVAL;
1094 }
1095
1096 static inline void regcache_cache_only(struct regmap *map, bool enable)
1097 {
1098         WARN_ONCE(1, "regmap API is disabled");
1099 }
1100
1101 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
1102 {
1103         WARN_ONCE(1, "regmap API is disabled");
1104 }
1105
1106 static inline void regcache_mark_dirty(struct regmap *map)
1107 {
1108         WARN_ONCE(1, "regmap API is disabled");
1109 }
1110
1111 static inline void regmap_async_complete(struct regmap *map)
1112 {
1113         WARN_ONCE(1, "regmap API is disabled");
1114 }
1115
1116 static inline int regmap_register_patch(struct regmap *map,
1117                                         const struct reg_sequence *regs,
1118                                         int num_regs)
1119 {
1120         WARN_ONCE(1, "regmap API is disabled");
1121         return -EINVAL;
1122 }
1123
1124 static inline int regmap_parse_val(struct regmap *map, const void *buf,
1125                                 unsigned int *val)
1126 {
1127         WARN_ONCE(1, "regmap API is disabled");
1128         return -EINVAL;
1129 }
1130
1131 static inline struct regmap *dev_get_regmap(struct device *dev,
1132                                             const char *name)
1133 {
1134         return NULL;
1135 }
1136
1137 static inline struct device *regmap_get_device(struct regmap *map)
1138 {
1139         WARN_ONCE(1, "regmap API is disabled");
1140         return NULL;
1141 }
1142
1143 #endif
1144
1145 #endif