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