Commit | Line | Data |
---|---|---|
b83a313b MB |
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 | ||
b83a313b | 16 | #include <linux/list.h> |
b83a313b | 17 | |
de477254 | 18 | struct module; |
313162d0 | 19 | struct device; |
9943fa30 | 20 | struct i2c_client; |
a676f083 | 21 | struct spi_device; |
b83d2ff0 | 22 | struct regmap; |
9943fa30 | 23 | |
9fabe24e DP |
24 | /* An enum of all the supported cache types */ |
25 | enum regcache_type { | |
26 | REGCACHE_NONE, | |
28644c80 | 27 | REGCACHE_RBTREE, |
50b776fc | 28 | REGCACHE_COMPRESSED |
9fabe24e DP |
29 | }; |
30 | ||
bd20eb54 MB |
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 | ||
b83d2ff0 MB |
44 | #ifdef CONFIG_REGMAP |
45 | ||
dd898b20 MB |
46 | /** |
47 | * Configuration for the register map of a device. | |
48 | * | |
d3c242e1 SW |
49 | * @name: Optional name of the regmap. Useful when a device has multiple |
50 | * register regions. | |
51 | * | |
dd898b20 | 52 | * @reg_bits: Number of bits in a register address, mandatory. |
f01ee60f SW |
53 | * @reg_stride: The register address stride. Valid register addresses are a |
54 | * multiple of this value. If set to 0, a value of 1 will be | |
55 | * used. | |
82159ba8 | 56 | * @pad_bits: Number of bits of padding between register and value. |
dd898b20 | 57 | * @val_bits: Number of bits in a register value, mandatory. |
2e2ae66d | 58 | * |
3566cc9d MB |
59 | * @writeable_reg: Optional callback returning true if the register |
60 | * can be written to. | |
61 | * @readable_reg: Optional callback returning true if the register | |
62 | * can be read from. | |
63 | * @volatile_reg: Optional callback returning true if the register | |
64 | * value can't be cached. | |
65 | * @precious_reg: Optional callback returning true if the rgister | |
66 | * should not be read outside of a call from the driver | |
67 | * (eg, a clear on read interrupt status register). | |
bd20eb54 MB |
68 | * |
69 | * @max_register: Optional, specifies the maximum valid register index. | |
70 | * @reg_defaults: Power on reset values for registers (for use with | |
71 | * register cache support). | |
72 | * @num_reg_defaults: Number of elements in reg_defaults. | |
6f306441 LPC |
73 | * |
74 | * @read_flag_mask: Mask to be set in the top byte of the register when doing | |
75 | * a read. | |
76 | * @write_flag_mask: Mask to be set in the top byte of the register when doing | |
77 | * a write. If both read_flag_mask and write_flag_mask are | |
78 | * empty the regmap_bus default masks are used. | |
9fabe24e DP |
79 | * |
80 | * @cache_type: The actual cache type. | |
81 | * @reg_defaults_raw: Power on reset values for registers (for use with | |
82 | * register cache support). | |
83 | * @num_reg_defaults_raw: Number of elements in reg_defaults_raw. | |
dd898b20 | 84 | */ |
b83a313b | 85 | struct regmap_config { |
d3c242e1 SW |
86 | const char *name; |
87 | ||
b83a313b | 88 | int reg_bits; |
f01ee60f | 89 | int reg_stride; |
82159ba8 | 90 | int pad_bits; |
b83a313b | 91 | int val_bits; |
2e2ae66d | 92 | |
2e2ae66d MB |
93 | bool (*writeable_reg)(struct device *dev, unsigned int reg); |
94 | bool (*readable_reg)(struct device *dev, unsigned int reg); | |
95 | bool (*volatile_reg)(struct device *dev, unsigned int reg); | |
18694886 | 96 | bool (*precious_reg)(struct device *dev, unsigned int reg); |
bd20eb54 MB |
97 | |
98 | unsigned int max_register; | |
720e4616 | 99 | const struct reg_default *reg_defaults; |
9fabe24e DP |
100 | unsigned int num_reg_defaults; |
101 | enum regcache_type cache_type; | |
102 | const void *reg_defaults_raw; | |
103 | unsigned int num_reg_defaults_raw; | |
6f306441 LPC |
104 | |
105 | u8 read_flag_mask; | |
106 | u8 write_flag_mask; | |
b83a313b MB |
107 | }; |
108 | ||
0135bbcc | 109 | typedef int (*regmap_hw_write)(void *context, const void *data, |
b83a313b | 110 | size_t count); |
0135bbcc | 111 | typedef int (*regmap_hw_gather_write)(void *context, |
b83a313b MB |
112 | const void *reg, size_t reg_len, |
113 | const void *val, size_t val_len); | |
0135bbcc | 114 | typedef int (*regmap_hw_read)(void *context, |
b83a313b MB |
115 | const void *reg_buf, size_t reg_size, |
116 | void *val_buf, size_t val_size); | |
0135bbcc | 117 | typedef void (*regmap_hw_free_context)(void *context); |
b83a313b MB |
118 | |
119 | /** | |
120 | * Description of a hardware bus for the register map infrastructure. | |
121 | * | |
bacdbe07 SW |
122 | * @fast_io: Register IO is fast. Use a spinlock instead of a mutex |
123 | * to perform locking. | |
b83a313b MB |
124 | * @write: Write operation. |
125 | * @gather_write: Write operation with split register/value, return -ENOTSUPP | |
126 | * if not implemented on a given device. | |
127 | * @read: Read operation. Data is returned in the buffer used to transmit | |
128 | * data. | |
b83a313b MB |
129 | * @read_flag_mask: Mask to be set in the top byte of the register when doing |
130 | * a read. | |
131 | */ | |
132 | struct regmap_bus { | |
bacdbe07 | 133 | bool fast_io; |
b83a313b MB |
134 | regmap_hw_write write; |
135 | regmap_hw_gather_write gather_write; | |
136 | regmap_hw_read read; | |
0135bbcc | 137 | regmap_hw_free_context free_context; |
b83a313b MB |
138 | u8 read_flag_mask; |
139 | }; | |
140 | ||
141 | struct regmap *regmap_init(struct device *dev, | |
142 | const struct regmap_bus *bus, | |
0135bbcc | 143 | void *bus_context, |
b83a313b | 144 | const struct regmap_config *config); |
9943fa30 MB |
145 | struct regmap *regmap_init_i2c(struct i2c_client *i2c, |
146 | const struct regmap_config *config); | |
a676f083 MB |
147 | struct regmap *regmap_init_spi(struct spi_device *dev, |
148 | const struct regmap_config *config); | |
45f5ff81 SW |
149 | struct regmap *regmap_init_mmio(struct device *dev, |
150 | void __iomem *regs, | |
151 | const struct regmap_config *config); | |
a676f083 | 152 | |
c0eb4676 MB |
153 | struct regmap *devm_regmap_init(struct device *dev, |
154 | const struct regmap_bus *bus, | |
0135bbcc | 155 | void *bus_context, |
c0eb4676 MB |
156 | const struct regmap_config *config); |
157 | struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c, | |
158 | const struct regmap_config *config); | |
159 | struct regmap *devm_regmap_init_spi(struct spi_device *dev, | |
160 | const struct regmap_config *config); | |
45f5ff81 SW |
161 | struct regmap *devm_regmap_init_mmio(struct device *dev, |
162 | void __iomem *regs, | |
163 | const struct regmap_config *config); | |
c0eb4676 | 164 | |
b83a313b | 165 | void regmap_exit(struct regmap *map); |
bf315173 MB |
166 | int regmap_reinit_cache(struct regmap *map, |
167 | const struct regmap_config *config); | |
b83a313b MB |
168 | int regmap_write(struct regmap *map, unsigned int reg, unsigned int val); |
169 | int regmap_raw_write(struct regmap *map, unsigned int reg, | |
170 | const void *val, size_t val_len); | |
8eaeb219 LD |
171 | int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val, |
172 | size_t val_count); | |
b83a313b MB |
173 | int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val); |
174 | int regmap_raw_read(struct regmap *map, unsigned int reg, | |
175 | void *val, size_t val_len); | |
176 | int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val, | |
177 | size_t val_count); | |
178 | int regmap_update_bits(struct regmap *map, unsigned int reg, | |
179 | unsigned int mask, unsigned int val); | |
018690d3 MB |
180 | int regmap_update_bits_check(struct regmap *map, unsigned int reg, |
181 | unsigned int mask, unsigned int val, | |
182 | bool *change); | |
a6539c32 | 183 | int regmap_get_val_bytes(struct regmap *map); |
b83a313b | 184 | |
39a58439 | 185 | int regcache_sync(struct regmap *map); |
4d4cfd16 MB |
186 | int regcache_sync_region(struct regmap *map, unsigned int min, |
187 | unsigned int max); | |
92afb286 | 188 | void regcache_cache_only(struct regmap *map, bool enable); |
6eb0f5e0 | 189 | void regcache_cache_bypass(struct regmap *map, bool enable); |
8ae0d7e8 | 190 | void regcache_mark_dirty(struct regmap *map); |
92afb286 | 191 | |
22f0d90a MB |
192 | int regmap_register_patch(struct regmap *map, const struct reg_default *regs, |
193 | int num_regs); | |
194 | ||
f8beab2b MB |
195 | /** |
196 | * Description of an IRQ for the generic regmap irq_chip. | |
197 | * | |
198 | * @reg_offset: Offset of the status/mask register within the bank | |
199 | * @mask: Mask used to flag/control the register. | |
200 | */ | |
201 | struct regmap_irq { | |
202 | unsigned int reg_offset; | |
203 | unsigned int mask; | |
204 | }; | |
205 | ||
206 | /** | |
207 | * Description of a generic regmap irq_chip. This is not intended to | |
208 | * handle every possible interrupt controller, but it should handle a | |
209 | * substantial proportion of those that are found in the wild. | |
210 | * | |
211 | * @name: Descriptive name for IRQ controller. | |
212 | * | |
213 | * @status_base: Base status register address. | |
214 | * @mask_base: Base mask register address. | |
215 | * @ack_base: Base ack address. If zero then the chip is clear on read. | |
216 | * | |
217 | * @num_regs: Number of registers in each control bank. | |
218 | * @irqs: Descriptors for individual IRQs. Interrupt numbers are | |
219 | * assigned based on the index in the array of the interrupt. | |
220 | * @num_irqs: Number of descriptors. | |
221 | */ | |
222 | struct regmap_irq_chip { | |
223 | const char *name; | |
224 | ||
225 | unsigned int status_base; | |
226 | unsigned int mask_base; | |
227 | unsigned int ack_base; | |
228 | ||
229 | int num_regs; | |
230 | ||
231 | const struct regmap_irq *irqs; | |
232 | int num_irqs; | |
233 | }; | |
234 | ||
235 | struct regmap_irq_chip_data; | |
236 | ||
237 | int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags, | |
238 | int irq_base, struct regmap_irq_chip *chip, | |
239 | struct regmap_irq_chip_data **data); | |
240 | void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data); | |
209a6006 | 241 | int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data); |
92afb286 | 242 | |
9cde5fcd MB |
243 | #else |
244 | ||
245 | /* | |
246 | * These stubs should only ever be called by generic code which has | |
247 | * regmap based facilities, if they ever get called at runtime | |
248 | * something is going wrong and something probably needs to select | |
249 | * REGMAP. | |
250 | */ | |
251 | ||
252 | static inline int regmap_write(struct regmap *map, unsigned int reg, | |
253 | unsigned int val) | |
254 | { | |
255 | WARN_ONCE(1, "regmap API is disabled"); | |
256 | return -EINVAL; | |
257 | } | |
258 | ||
259 | static inline int regmap_raw_write(struct regmap *map, unsigned int reg, | |
260 | const void *val, size_t val_len) | |
261 | { | |
262 | WARN_ONCE(1, "regmap API is disabled"); | |
263 | return -EINVAL; | |
264 | } | |
265 | ||
266 | static inline int regmap_bulk_write(struct regmap *map, unsigned int reg, | |
267 | const void *val, size_t val_count) | |
268 | { | |
269 | WARN_ONCE(1, "regmap API is disabled"); | |
270 | return -EINVAL; | |
271 | } | |
272 | ||
273 | static inline int regmap_read(struct regmap *map, unsigned int reg, | |
274 | unsigned int *val) | |
275 | { | |
276 | WARN_ONCE(1, "regmap API is disabled"); | |
277 | return -EINVAL; | |
278 | } | |
279 | ||
280 | static inline int regmap_raw_read(struct regmap *map, unsigned int reg, | |
281 | void *val, size_t val_len) | |
282 | { | |
283 | WARN_ONCE(1, "regmap API is disabled"); | |
284 | return -EINVAL; | |
285 | } | |
286 | ||
287 | static inline int regmap_bulk_read(struct regmap *map, unsigned int reg, | |
288 | void *val, size_t val_count) | |
289 | { | |
290 | WARN_ONCE(1, "regmap API is disabled"); | |
291 | return -EINVAL; | |
292 | } | |
293 | ||
294 | static inline int regmap_update_bits(struct regmap *map, unsigned int reg, | |
295 | unsigned int mask, unsigned int val) | |
296 | { | |
297 | WARN_ONCE(1, "regmap API is disabled"); | |
298 | return -EINVAL; | |
299 | } | |
300 | ||
301 | static inline int regmap_update_bits_check(struct regmap *map, | |
302 | unsigned int reg, | |
303 | unsigned int mask, unsigned int val, | |
304 | bool *change) | |
305 | { | |
306 | WARN_ONCE(1, "regmap API is disabled"); | |
307 | return -EINVAL; | |
308 | } | |
309 | ||
310 | static inline int regmap_get_val_bytes(struct regmap *map) | |
311 | { | |
312 | WARN_ONCE(1, "regmap API is disabled"); | |
313 | return -EINVAL; | |
314 | } | |
315 | ||
316 | static inline int regcache_sync(struct regmap *map) | |
317 | { | |
318 | WARN_ONCE(1, "regmap API is disabled"); | |
319 | return -EINVAL; | |
320 | } | |
321 | ||
a313f9f5 MB |
322 | static inline int regcache_sync_region(struct regmap *map, unsigned int min, |
323 | unsigned int max) | |
324 | { | |
325 | WARN_ONCE(1, "regmap API is disabled"); | |
326 | return -EINVAL; | |
327 | } | |
328 | ||
9cde5fcd MB |
329 | static inline void regcache_cache_only(struct regmap *map, bool enable) |
330 | { | |
331 | WARN_ONCE(1, "regmap API is disabled"); | |
332 | } | |
333 | ||
334 | static inline void regcache_cache_bypass(struct regmap *map, bool enable) | |
335 | { | |
336 | WARN_ONCE(1, "regmap API is disabled"); | |
337 | } | |
338 | ||
339 | static inline void regcache_mark_dirty(struct regmap *map) | |
340 | { | |
341 | WARN_ONCE(1, "regmap API is disabled"); | |
342 | } | |
343 | ||
344 | static inline int regmap_register_patch(struct regmap *map, | |
345 | const struct reg_default *regs, | |
346 | int num_regs) | |
347 | { | |
348 | WARN_ONCE(1, "regmap API is disabled"); | |
349 | return -EINVAL; | |
350 | } | |
351 | ||
352 | #endif | |
353 | ||
b83a313b | 354 | #endif |