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