]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - include/linux/regmap.h
Merge remote-tracking branch 'regmap/topic/irq' into regmap-next
[karo-tx-linux.git] / include / linux / regmap.h
1 #ifndef __LINUX_REGMAP_H
2 #define __LINUX_REGMAP_H
3
4 /*
5  * Register map access API
6  *
7  * Copyright 2011 Wolfson Microelectronics plc
8  *
9  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15
16 #include <linux/list.h>
17 #include <linux/rbtree.h>
18
19 struct module;
20 struct device;
21 struct i2c_client;
22 struct irq_domain;
23 struct spi_device;
24 struct regmap;
25 struct regmap_range_cfg;
26
27 /* An enum of all the supported cache types */
28 enum regcache_type {
29         REGCACHE_NONE,
30         REGCACHE_RBTREE,
31         REGCACHE_COMPRESSED,
32         REGCACHE_FLAT,
33 };
34
35 /**
36  * Default value for a register.  We use an array of structs rather
37  * than a simple array as many modern devices have very sparse
38  * register maps.
39  *
40  * @reg: Register address.
41  * @def: Register default value.
42  */
43 struct reg_default {
44         unsigned int reg;
45         unsigned int def;
46 };
47
48 #ifdef CONFIG_REGMAP
49
50 enum regmap_endian {
51         /* Unspecified -> 0 -> Backwards compatible default */
52         REGMAP_ENDIAN_DEFAULT = 0,
53         REGMAP_ENDIAN_BIG,
54         REGMAP_ENDIAN_LITTLE,
55         REGMAP_ENDIAN_NATIVE,
56 };
57
58 /**
59  * A register range, used for access related checks
60  * (readable/writeable/volatile/precious checks)
61  *
62  * @range_min: address of first register
63  * @range_max: address of last register
64  */
65 struct regmap_range {
66         unsigned int range_min;
67         unsigned int range_max;
68 };
69
70 /*
71  * A table of ranges including some yes ranges and some no ranges.
72  * If a register belongs to a no_range, the corresponding check function
73  * will return false. If a register belongs to a yes range, the corresponding
74  * check function will return true. "no_ranges" are searched first.
75  *
76  * @yes_ranges : pointer to an array of regmap ranges used as "yes ranges"
77  * @n_yes_ranges: size of the above array
78  * @no_ranges: pointer to an array of regmap ranges used as "no ranges"
79  * @n_no_ranges: size of the above array
80  */
81 struct regmap_access_table {
82         const struct regmap_range *yes_ranges;
83         unsigned int n_yes_ranges;
84         const struct regmap_range *no_ranges;
85         unsigned int n_no_ranges;
86 };
87
88 typedef void (*regmap_lock)(void *);
89 typedef void (*regmap_unlock)(void *);
90
91 /**
92  * Configuration for the register map of a device.
93  *
94  * @name: Optional name of the regmap. Useful when a device has multiple
95  *        register regions.
96  *
97  * @reg_bits: Number of bits in a register address, mandatory.
98  * @reg_stride: The register address stride. Valid register addresses are a
99  *              multiple of this value. If set to 0, a value of 1 will be
100  *              used.
101  * @pad_bits: Number of bits of padding between register and value.
102  * @val_bits: Number of bits in a register value, mandatory.
103  *
104  * @writeable_reg: Optional callback returning true if the register
105  *                 can be written to. If this field is NULL but wr_table
106  *                 (see below) is not, the check is performed on such table
107  *                 (a register is writeable if it belongs to one of the ranges
108  *                  specified by wr_table).
109  * @readable_reg: Optional callback returning true if the register
110  *                can be read from. If this field is NULL but rd_table
111  *                 (see below) is not, the check is performed on such table
112  *                 (a register is readable if it belongs to one of the ranges
113  *                  specified by rd_table).
114  * @volatile_reg: Optional callback returning true if the register
115  *                value can't be cached. If this field is NULL but
116  *                volatile_table (see below) is not, the check is performed on
117  *                such table (a register is volatile if it belongs to one of
118  *                the ranges specified by volatile_table).
119  * @precious_reg: Optional callback returning true if the rgister
120  *                should not be read outside of a call from the driver
121  *                (eg, a clear on read interrupt status register). If this
122  *                field is NULL but precious_table (see below) is not, the
123  *                check is performed on such table (a register is precious if
124  *                it belongs to one of the ranges specified by precious_table).
125  * @lock:         Optional lock callback (overrides regmap's default lock
126  *                function, based on spinlock or mutex).
127  * @unlock:       As above for unlocking.
128  * @lock_arg:     this field is passed as the only argument of lock/unlock
129  *                functions (ignored in case regular lock/unlock functions
130  *                are not overridden).
131  *
132  * @max_register: Optional, specifies the maximum valid register index.
133  * @wr_table:     Optional, points to a struct regmap_access_table specifying
134  *                valid ranges for write access.
135  * @rd_table:     As above, for read access.
136  * @volatile_table: As above, for volatile registers.
137  * @precious_table: As above, for precious registers.
138  * @reg_defaults: Power on reset values for registers (for use with
139  *                register cache support).
140  * @num_reg_defaults: Number of elements in reg_defaults.
141  *
142  * @read_flag_mask: Mask to be set in the top byte of the register when doing
143  *                  a read.
144  * @write_flag_mask: Mask to be set in the top byte of the register when doing
145  *                   a write. If both read_flag_mask and write_flag_mask are
146  *                   empty the regmap_bus default masks are used.
147  * @use_single_rw: If set, converts the bulk read and write operations into
148  *                  a series of single read and write operations. This is useful
149  *                  for device that does not support bulk read and write.
150  *
151  * @cache_type: The actual cache type.
152  * @reg_defaults_raw: Power on reset values for registers (for use with
153  *                    register cache support).
154  * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
155  * @reg_format_endian: Endianness for formatted register addresses. If this is
156  *                     DEFAULT, the @reg_format_endian_default value from the
157  *                     regmap bus is used.
158  * @val_format_endian: Endianness for formatted register values. If this is
159  *                     DEFAULT, the @reg_format_endian_default value from the
160  *                     regmap bus is used.
161  *
162  * @ranges: Array of configuration entries for virtual address ranges.
163  * @num_ranges: Number of range configuration entries.
164  */
165 struct regmap_config {
166         const char *name;
167
168         int reg_bits;
169         int reg_stride;
170         int pad_bits;
171         int val_bits;
172
173         bool (*writeable_reg)(struct device *dev, unsigned int reg);
174         bool (*readable_reg)(struct device *dev, unsigned int reg);
175         bool (*volatile_reg)(struct device *dev, unsigned int reg);
176         bool (*precious_reg)(struct device *dev, unsigned int reg);
177         regmap_lock lock;
178         regmap_unlock unlock;
179         void *lock_arg;
180
181         unsigned int max_register;
182         const struct regmap_access_table *wr_table;
183         const struct regmap_access_table *rd_table;
184         const struct regmap_access_table *volatile_table;
185         const struct regmap_access_table *precious_table;
186         const struct reg_default *reg_defaults;
187         unsigned int num_reg_defaults;
188         enum regcache_type cache_type;
189         const void *reg_defaults_raw;
190         unsigned int num_reg_defaults_raw;
191
192         u8 read_flag_mask;
193         u8 write_flag_mask;
194
195         bool use_single_rw;
196
197         enum regmap_endian reg_format_endian;
198         enum regmap_endian val_format_endian;
199
200         const struct regmap_range_cfg *ranges;
201         unsigned int num_ranges;
202 };
203
204 /**
205  * Configuration for indirectly accessed or paged registers.
206  * Registers, mapped to this virtual range, are accessed in two steps:
207  *     1. page selector register update;
208  *     2. access through data window registers.
209  *
210  * @name: Descriptive name for diagnostics
211  *
212  * @range_min: Address of the lowest register address in virtual range.
213  * @range_max: Address of the highest register in virtual range.
214  *
215  * @page_sel_reg: Register with selector field.
216  * @page_sel_mask: Bit shift for selector value.
217  * @page_sel_shift: Bit mask for selector value.
218  *
219  * @window_start: Address of first (lowest) register in data window.
220  * @window_len: Number of registers in data window.
221  */
222 struct regmap_range_cfg {
223         const char *name;
224
225         /* Registers of virtual address range */
226         unsigned int range_min;
227         unsigned int range_max;
228
229         /* Page selector for indirect addressing */
230         unsigned int selector_reg;
231         unsigned int selector_mask;
232         int selector_shift;
233
234         /* Data window (per each page) */
235         unsigned int window_start;
236         unsigned int window_len;
237 };
238
239 struct regmap_async;
240
241 typedef int (*regmap_hw_write)(void *context, const void *data,
242                                size_t count);
243 typedef int (*regmap_hw_gather_write)(void *context,
244                                       const void *reg, size_t reg_len,
245                                       const void *val, size_t val_len);
246 typedef int (*regmap_hw_async_write)(void *context,
247                                      const void *reg, size_t reg_len,
248                                      const void *val, size_t val_len,
249                                      struct regmap_async *async);
250 typedef int (*regmap_hw_read)(void *context,
251                               const void *reg_buf, size_t reg_size,
252                               void *val_buf, size_t val_size);
253 typedef struct regmap_async *(*regmap_hw_async_alloc)(void);
254 typedef void (*regmap_hw_free_context)(void *context);
255
256 /**
257  * Description of a hardware bus for the register map infrastructure.
258  *
259  * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
260  *           to perform locking. This field is ignored if custom lock/unlock
261  *           functions are used (see fields lock/unlock of
262  *           struct regmap_config).
263  * @write: Write operation.
264  * @gather_write: Write operation with split register/value, return -ENOTSUPP
265  *                if not implemented  on a given device.
266  * @async_write: Write operation which completes asynchronously, optional and
267  *               must serialise with respect to non-async I/O.
268  * @read: Read operation.  Data is returned in the buffer used to transmit
269  *         data.
270  * @async_alloc: Allocate a regmap_async() structure.
271  * @read_flag_mask: Mask to be set in the top byte of the register when doing
272  *                  a read.
273  * @reg_format_endian_default: Default endianness for formatted register
274  *     addresses. Used when the regmap_config specifies DEFAULT. If this is
275  *     DEFAULT, BIG is assumed.
276  * @val_format_endian_default: Default endianness for formatted register
277  *     values. Used when the regmap_config specifies DEFAULT. If this is
278  *     DEFAULT, BIG is assumed.
279  * @async_size: Size of struct used for async work.
280  */
281 struct regmap_bus {
282         bool fast_io;
283         regmap_hw_write write;
284         regmap_hw_gather_write gather_write;
285         regmap_hw_async_write async_write;
286         regmap_hw_read read;
287         regmap_hw_free_context free_context;
288         regmap_hw_async_alloc async_alloc;
289         u8 read_flag_mask;
290         enum regmap_endian reg_format_endian_default;
291         enum regmap_endian val_format_endian_default;
292 };
293
294 struct regmap *regmap_init(struct device *dev,
295                            const struct regmap_bus *bus,
296                            void *bus_context,
297                            const struct regmap_config *config);
298 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
299                                const struct regmap_config *config);
300 struct regmap *regmap_init_spi(struct spi_device *dev,
301                                const struct regmap_config *config);
302 struct regmap *regmap_init_mmio(struct device *dev,
303                                 void __iomem *regs,
304                                 const struct regmap_config *config);
305
306 struct regmap *devm_regmap_init(struct device *dev,
307                                 const struct regmap_bus *bus,
308                                 void *bus_context,
309                                 const struct regmap_config *config);
310 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
311                                     const struct regmap_config *config);
312 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
313                                     const struct regmap_config *config);
314 struct regmap *devm_regmap_init_mmio(struct device *dev,
315                                      void __iomem *regs,
316                                      const struct regmap_config *config);
317
318 void regmap_exit(struct regmap *map);
319 int regmap_reinit_cache(struct regmap *map,
320                         const struct regmap_config *config);
321 struct regmap *dev_get_regmap(struct device *dev, const char *name);
322 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
323 int regmap_raw_write(struct regmap *map, unsigned int reg,
324                      const void *val, size_t val_len);
325 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
326                         size_t val_count);
327 int regmap_raw_write_async(struct regmap *map, unsigned int reg,
328                            const void *val, size_t val_len);
329 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
330 int regmap_raw_read(struct regmap *map, unsigned int reg,
331                     void *val, size_t val_len);
332 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
333                      size_t val_count);
334 int regmap_update_bits(struct regmap *map, unsigned int reg,
335                        unsigned int mask, unsigned int val);
336 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
337                              unsigned int mask, unsigned int val,
338                              bool *change);
339 int regmap_get_val_bytes(struct regmap *map);
340 int regmap_async_complete(struct regmap *map);
341
342 int regcache_sync(struct regmap *map);
343 int regcache_sync_region(struct regmap *map, unsigned int min,
344                          unsigned int max);
345 void regcache_cache_only(struct regmap *map, bool enable);
346 void regcache_cache_bypass(struct regmap *map, bool enable);
347 void regcache_mark_dirty(struct regmap *map);
348
349 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
350                           int num_regs);
351
352 static inline bool regmap_reg_in_range(unsigned int reg,
353                                        const struct regmap_range *range)
354 {
355         return reg >= range->range_min && reg <= range->range_max;
356 }
357
358 bool regmap_reg_in_ranges(unsigned int reg,
359                           const struct regmap_range *ranges,
360                           unsigned int nranges);
361
362 /**
363  * Description of an IRQ for the generic regmap irq_chip.
364  *
365  * @reg_offset: Offset of the status/mask register within the bank
366  * @mask:       Mask used to flag/control the register.
367  */
368 struct regmap_irq {
369         unsigned int reg_offset;
370         unsigned int mask;
371 };
372
373 /**
374  * Description of a generic regmap irq_chip.  This is not intended to
375  * handle every possible interrupt controller, but it should handle a
376  * substantial proportion of those that are found in the wild.
377  *
378  * @name:        Descriptive name for IRQ controller.
379  *
380  * @status_base: Base status register address.
381  * @mask_base:   Base mask register address.
382  * @ack_base:    Base ack address.  If zero then the chip is clear on read.
383  * @wake_base:   Base address for wake enables.  If zero unsupported.
384  * @irq_reg_stride:  Stride to use for chips where registers are not contiguous.
385  * @runtime_pm:  Hold a runtime PM lock on the device when accessing it.
386  *
387  * @num_regs:    Number of registers in each control bank.
388  * @irqs:        Descriptors for individual IRQs.  Interrupt numbers are
389  *               assigned based on the index in the array of the interrupt.
390  * @num_irqs:    Number of descriptors.
391  */
392 struct regmap_irq_chip {
393         const char *name;
394
395         unsigned int status_base;
396         unsigned int mask_base;
397         unsigned int ack_base;
398         unsigned int wake_base;
399         unsigned int irq_reg_stride;
400         unsigned int mask_invert;
401         unsigned int wake_invert;
402         bool runtime_pm;
403
404         int num_regs;
405
406         const struct regmap_irq *irqs;
407         int num_irqs;
408 };
409
410 struct regmap_irq_chip_data;
411
412 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
413                         int irq_base, const struct regmap_irq_chip *chip,
414                         struct regmap_irq_chip_data **data);
415 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
416 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
417 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
418 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data);
419
420 #else
421
422 /*
423  * These stubs should only ever be called by generic code which has
424  * regmap based facilities, if they ever get called at runtime
425  * something is going wrong and something probably needs to select
426  * REGMAP.
427  */
428
429 static inline int regmap_write(struct regmap *map, unsigned int reg,
430                                unsigned int val)
431 {
432         WARN_ONCE(1, "regmap API is disabled");
433         return -EINVAL;
434 }
435
436 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
437                                    const void *val, size_t val_len)
438 {
439         WARN_ONCE(1, "regmap API is disabled");
440         return -EINVAL;
441 }
442
443 static inline int regmap_raw_write_async(struct regmap *map, unsigned int reg,
444                                          const void *val, size_t val_len)
445 {
446         WARN_ONCE(1, "regmap API is disabled");
447         return -EINVAL;
448 }
449
450 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
451                                     const void *val, size_t val_count)
452 {
453         WARN_ONCE(1, "regmap API is disabled");
454         return -EINVAL;
455 }
456
457 static inline int regmap_read(struct regmap *map, unsigned int reg,
458                               unsigned int *val)
459 {
460         WARN_ONCE(1, "regmap API is disabled");
461         return -EINVAL;
462 }
463
464 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
465                                   void *val, size_t val_len)
466 {
467         WARN_ONCE(1, "regmap API is disabled");
468         return -EINVAL;
469 }
470
471 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
472                                    void *val, size_t val_count)
473 {
474         WARN_ONCE(1, "regmap API is disabled");
475         return -EINVAL;
476 }
477
478 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
479                                      unsigned int mask, unsigned int val)
480 {
481         WARN_ONCE(1, "regmap API is disabled");
482         return -EINVAL;
483 }
484
485 static inline int regmap_update_bits_check(struct regmap *map,
486                                            unsigned int reg,
487                                            unsigned int mask, unsigned int val,
488                                            bool *change)
489 {
490         WARN_ONCE(1, "regmap API is disabled");
491         return -EINVAL;
492 }
493
494 static inline int regmap_get_val_bytes(struct regmap *map)
495 {
496         WARN_ONCE(1, "regmap API is disabled");
497         return -EINVAL;
498 }
499
500 static inline int regcache_sync(struct regmap *map)
501 {
502         WARN_ONCE(1, "regmap API is disabled");
503         return -EINVAL;
504 }
505
506 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
507                                        unsigned int max)
508 {
509         WARN_ONCE(1, "regmap API is disabled");
510         return -EINVAL;
511 }
512
513 static inline void regcache_cache_only(struct regmap *map, bool enable)
514 {
515         WARN_ONCE(1, "regmap API is disabled");
516 }
517
518 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
519 {
520         WARN_ONCE(1, "regmap API is disabled");
521 }
522
523 static inline void regcache_mark_dirty(struct regmap *map)
524 {
525         WARN_ONCE(1, "regmap API is disabled");
526 }
527
528 static inline void regmap_async_complete(struct regmap *map)
529 {
530         WARN_ONCE(1, "regmap API is disabled");
531 }
532
533 static inline int regmap_register_patch(struct regmap *map,
534                                         const struct reg_default *regs,
535                                         int num_regs)
536 {
537         WARN_ONCE(1, "regmap API is disabled");
538         return -EINVAL;
539 }
540
541 static inline struct regmap *dev_get_regmap(struct device *dev,
542                                             const char *name)
543 {
544         return NULL;
545 }
546
547 #endif
548
549 #endif