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