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