regmap: Remove warning on stubbed dev_get_regmap()
[linux-block.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
18 struct module;
19 struct device;
20 struct i2c_client;
21 struct spi_device;
22 struct regmap;
23
24 /* An enum of all the supported cache types */
25 enum regcache_type {
26         REGCACHE_NONE,
27         REGCACHE_RBTREE,
28         REGCACHE_COMPRESSED
29 };
30
31 /**
32  * Default value for a register.  We use an array of structs rather
33  * than a simple array as many modern devices have very sparse
34  * register maps.
35  *
36  * @reg: Register address.
37  * @def: Register default value.
38  */
39 struct reg_default {
40         unsigned int reg;
41         unsigned int def;
42 };
43
44 #ifdef CONFIG_REGMAP
45
46 enum regmap_endian {
47         /* Unspecified -> 0 -> Backwards compatible default */
48         REGMAP_ENDIAN_DEFAULT = 0,
49         REGMAP_ENDIAN_BIG,
50         REGMAP_ENDIAN_LITTLE,
51         REGMAP_ENDIAN_NATIVE,
52 };
53
54 /**
55  * Configuration for the register map of a device.
56  *
57  * @name: Optional name of the regmap. Useful when a device has multiple
58  *        register regions.
59  *
60  * @reg_bits: Number of bits in a register address, mandatory.
61  * @reg_stride: The register address stride. Valid register addresses are a
62  *              multiple of this value. If set to 0, a value of 1 will be
63  *              used.
64  * @pad_bits: Number of bits of padding between register and value.
65  * @val_bits: Number of bits in a register value, mandatory.
66  *
67  * @writeable_reg: Optional callback returning true if the register
68  *                 can be written to.
69  * @readable_reg: Optional callback returning true if the register
70  *                can be read from.
71  * @volatile_reg: Optional callback returning true if the register
72  *                value can't be cached.
73  * @precious_reg: Optional callback returning true if the rgister
74  *                should not be read outside of a call from the driver
75  *                (eg, a clear on read interrupt status register).
76  *
77  * @max_register: Optional, specifies the maximum valid register index.
78  * @reg_defaults: Power on reset values for registers (for use with
79  *                register cache support).
80  * @num_reg_defaults: Number of elements in reg_defaults.
81  *
82  * @read_flag_mask: Mask to be set in the top byte of the register when doing
83  *                  a read.
84  * @write_flag_mask: Mask to be set in the top byte of the register when doing
85  *                   a write. If both read_flag_mask and write_flag_mask are
86  *                   empty the regmap_bus default masks are used.
87  * @use_single_rw: If set, converts the bulk read and write operations into
88  *                  a series of single read and write operations. This is useful
89  *                  for device that does not support bulk read and write.
90  *
91  * @cache_type: The actual cache type.
92  * @reg_defaults_raw: Power on reset values for registers (for use with
93  *                    register cache support).
94  * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
95  * @reg_format_endian: Endianness for formatted register addresses. If this is
96  *                     DEFAULT, the @reg_format_endian_default value from the
97  *                     regmap bus is used.
98  * @val_format_endian: Endianness for formatted register values. If this is
99  *                     DEFAULT, the @reg_format_endian_default value from the
100  *                     regmap bus is used.
101  */
102 struct regmap_config {
103         const char *name;
104
105         int reg_bits;
106         int reg_stride;
107         int pad_bits;
108         int val_bits;
109
110         bool (*writeable_reg)(struct device *dev, unsigned int reg);
111         bool (*readable_reg)(struct device *dev, unsigned int reg);
112         bool (*volatile_reg)(struct device *dev, unsigned int reg);
113         bool (*precious_reg)(struct device *dev, unsigned int reg);
114
115         unsigned int max_register;
116         const struct reg_default *reg_defaults;
117         unsigned int num_reg_defaults;
118         enum regcache_type cache_type;
119         const void *reg_defaults_raw;
120         unsigned int num_reg_defaults_raw;
121
122         u8 read_flag_mask;
123         u8 write_flag_mask;
124
125         bool use_single_rw;
126
127         enum regmap_endian reg_format_endian;
128         enum regmap_endian val_format_endian;
129 };
130
131 typedef int (*regmap_hw_write)(void *context, const void *data,
132                                size_t count);
133 typedef int (*regmap_hw_gather_write)(void *context,
134                                       const void *reg, size_t reg_len,
135                                       const void *val, size_t val_len);
136 typedef int (*regmap_hw_read)(void *context,
137                               const void *reg_buf, size_t reg_size,
138                               void *val_buf, size_t val_size);
139 typedef void (*regmap_hw_free_context)(void *context);
140
141 /**
142  * Description of a hardware bus for the register map infrastructure.
143  *
144  * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
145  *           to perform locking.
146  * @write: Write operation.
147  * @gather_write: Write operation with split register/value, return -ENOTSUPP
148  *                if not implemented  on a given device.
149  * @read: Read operation.  Data is returned in the buffer used to transmit
150  *         data.
151  * @read_flag_mask: Mask to be set in the top byte of the register when doing
152  *                  a read.
153  * @reg_format_endian_default: Default endianness for formatted register
154  *     addresses. Used when the regmap_config specifies DEFAULT. If this is
155  *     DEFAULT, BIG is assumed.
156  * @val_format_endian_default: Default endianness for formatted register
157  *     values. Used when the regmap_config specifies DEFAULT. If this is
158  *     DEFAULT, BIG is assumed.
159  */
160 struct regmap_bus {
161         bool fast_io;
162         regmap_hw_write write;
163         regmap_hw_gather_write gather_write;
164         regmap_hw_read read;
165         regmap_hw_free_context free_context;
166         u8 read_flag_mask;
167         enum regmap_endian reg_format_endian_default;
168         enum regmap_endian val_format_endian_default;
169 };
170
171 struct regmap *regmap_init(struct device *dev,
172                            const struct regmap_bus *bus,
173                            void *bus_context,
174                            const struct regmap_config *config);
175 struct regmap *regmap_init_i2c(struct i2c_client *i2c,
176                                const struct regmap_config *config);
177 struct regmap *regmap_init_spi(struct spi_device *dev,
178                                const struct regmap_config *config);
179 struct regmap *regmap_init_mmio(struct device *dev,
180                                 void __iomem *regs,
181                                 const struct regmap_config *config);
182
183 struct regmap *devm_regmap_init(struct device *dev,
184                                 const struct regmap_bus *bus,
185                                 void *bus_context,
186                                 const struct regmap_config *config);
187 struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
188                                     const struct regmap_config *config);
189 struct regmap *devm_regmap_init_spi(struct spi_device *dev,
190                                     const struct regmap_config *config);
191 struct regmap *devm_regmap_init_mmio(struct device *dev,
192                                      void __iomem *regs,
193                                      const struct regmap_config *config);
194
195 void regmap_exit(struct regmap *map);
196 int regmap_reinit_cache(struct regmap *map,
197                         const struct regmap_config *config);
198 struct regmap *dev_get_regmap(struct device *dev, const char *name);
199 int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
200 int regmap_raw_write(struct regmap *map, unsigned int reg,
201                      const void *val, size_t val_len);
202 int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
203                         size_t val_count);
204 int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
205 int regmap_raw_read(struct regmap *map, unsigned int reg,
206                     void *val, size_t val_len);
207 int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
208                      size_t val_count);
209 int regmap_update_bits(struct regmap *map, unsigned int reg,
210                        unsigned int mask, unsigned int val);
211 int regmap_update_bits_check(struct regmap *map, unsigned int reg,
212                              unsigned int mask, unsigned int val,
213                              bool *change);
214 int regmap_get_val_bytes(struct regmap *map);
215
216 int regcache_sync(struct regmap *map);
217 int regcache_sync_region(struct regmap *map, unsigned int min,
218                          unsigned int max);
219 void regcache_cache_only(struct regmap *map, bool enable);
220 void regcache_cache_bypass(struct regmap *map, bool enable);
221 void regcache_mark_dirty(struct regmap *map);
222
223 int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
224                           int num_regs);
225
226 /**
227  * Description of an IRQ for the generic regmap irq_chip.
228  *
229  * @reg_offset: Offset of the status/mask register within the bank
230  * @mask:       Mask used to flag/control the register.
231  */
232 struct regmap_irq {
233         unsigned int reg_offset;
234         unsigned int mask;
235 };
236
237 /**
238  * Description of a generic regmap irq_chip.  This is not intended to
239  * handle every possible interrupt controller, but it should handle a
240  * substantial proportion of those that are found in the wild.
241  *
242  * @name:        Descriptive name for IRQ controller.
243  *
244  * @status_base: Base status register address.
245  * @mask_base:   Base mask register address.
246  * @ack_base:    Base ack address.  If zero then the chip is clear on read.
247  * @irq_reg_stride:  Stride to use for chips where registers are not contiguous.
248  *
249  * @num_regs:    Number of registers in each control bank.
250  * @irqs:        Descriptors for individual IRQs.  Interrupt numbers are
251  *               assigned based on the index in the array of the interrupt.
252  * @num_irqs:    Number of descriptors.
253  */
254 struct regmap_irq_chip {
255         const char *name;
256
257         unsigned int status_base;
258         unsigned int mask_base;
259         unsigned int ack_base;
260         unsigned int irq_reg_stride;
261
262         int num_regs;
263
264         const struct regmap_irq *irqs;
265         int num_irqs;
266 };
267
268 struct regmap_irq_chip_data;
269
270 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
271                         int irq_base, const struct regmap_irq_chip *chip,
272                         struct regmap_irq_chip_data **data);
273 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
274 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
275 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
276
277 #else
278
279 /*
280  * These stubs should only ever be called by generic code which has
281  * regmap based facilities, if they ever get called at runtime
282  * something is going wrong and something probably needs to select
283  * REGMAP.
284  */
285
286 static inline int regmap_write(struct regmap *map, unsigned int reg,
287                                unsigned int val)
288 {
289         WARN_ONCE(1, "regmap API is disabled");
290         return -EINVAL;
291 }
292
293 static inline int regmap_raw_write(struct regmap *map, unsigned int reg,
294                                    const void *val, size_t val_len)
295 {
296         WARN_ONCE(1, "regmap API is disabled");
297         return -EINVAL;
298 }
299
300 static inline int regmap_bulk_write(struct regmap *map, unsigned int reg,
301                                     const void *val, size_t val_count)
302 {
303         WARN_ONCE(1, "regmap API is disabled");
304         return -EINVAL;
305 }
306
307 static inline int regmap_read(struct regmap *map, unsigned int reg,
308                               unsigned int *val)
309 {
310         WARN_ONCE(1, "regmap API is disabled");
311         return -EINVAL;
312 }
313
314 static inline int regmap_raw_read(struct regmap *map, unsigned int reg,
315                                   void *val, size_t val_len)
316 {
317         WARN_ONCE(1, "regmap API is disabled");
318         return -EINVAL;
319 }
320
321 static inline int regmap_bulk_read(struct regmap *map, unsigned int reg,
322                                    void *val, size_t val_count)
323 {
324         WARN_ONCE(1, "regmap API is disabled");
325         return -EINVAL;
326 }
327
328 static inline int regmap_update_bits(struct regmap *map, unsigned int reg,
329                                      unsigned int mask, unsigned int val)
330 {
331         WARN_ONCE(1, "regmap API is disabled");
332         return -EINVAL;
333 }
334
335 static inline int regmap_update_bits_check(struct regmap *map,
336                                            unsigned int reg,
337                                            unsigned int mask, unsigned int val,
338                                            bool *change)
339 {
340         WARN_ONCE(1, "regmap API is disabled");
341         return -EINVAL;
342 }
343
344 static inline int regmap_get_val_bytes(struct regmap *map)
345 {
346         WARN_ONCE(1, "regmap API is disabled");
347         return -EINVAL;
348 }
349
350 static inline int regcache_sync(struct regmap *map)
351 {
352         WARN_ONCE(1, "regmap API is disabled");
353         return -EINVAL;
354 }
355
356 static inline int regcache_sync_region(struct regmap *map, unsigned int min,
357                                        unsigned int max)
358 {
359         WARN_ONCE(1, "regmap API is disabled");
360         return -EINVAL;
361 }
362
363 static inline void regcache_cache_only(struct regmap *map, bool enable)
364 {
365         WARN_ONCE(1, "regmap API is disabled");
366 }
367
368 static inline void regcache_cache_bypass(struct regmap *map, bool enable)
369 {
370         WARN_ONCE(1, "regmap API is disabled");
371 }
372
373 static inline void regcache_mark_dirty(struct regmap *map)
374 {
375         WARN_ONCE(1, "regmap API is disabled");
376 }
377
378 static inline int regmap_register_patch(struct regmap *map,
379                                         const struct reg_default *regs,
380                                         int num_regs)
381 {
382         WARN_ONCE(1, "regmap API is disabled");
383         return -EINVAL;
384 }
385
386 static inline struct regmap *dev_get_regmap(struct device *dev,
387                                             const char *name)
388 {
389         return NULL;
390 }
391
392 #endif
393
394 #endif