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