regmap: introduce tables for readable/writeable/volatile/precious checks
[deliverable/linux.git] / include / linux / regmap.h
CommitLineData
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>
6863ca62 17#include <linux/rbtree.h>
b83a313b 18
de477254 19struct module;
313162d0 20struct device;
9943fa30 21struct i2c_client;
a676f083 22struct spi_device;
b83d2ff0 23struct regmap;
6863ca62 24struct regmap_range_cfg;
9943fa30 25
9fabe24e
DP
26/* An enum of all the supported cache types */
27enum regcache_type {
28 REGCACHE_NONE,
28644c80 29 REGCACHE_RBTREE,
50b776fc 30 REGCACHE_COMPRESSED
9fabe24e
DP
31};
32
bd20eb54
MB
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 */
41struct reg_default {
42 unsigned int reg;
43 unsigned int def;
44};
45
b83d2ff0
MB
46#ifdef CONFIG_REGMAP
47
141eba2e
SW
48enum 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
76aad392
DC
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 */
63struct 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 */
79struct 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
0d4529c5
DC
86typedef void (*regmap_lock)(void *);
87typedef void (*regmap_unlock)(void *);
88
dd898b20
MB
89/**
90 * Configuration for the register map of a device.
91 *
d3c242e1
SW
92 * @name: Optional name of the regmap. Useful when a device has multiple
93 * register regions.
94 *
dd898b20 95 * @reg_bits: Number of bits in a register address, mandatory.
f01ee60f
SW
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.
82159ba8 99 * @pad_bits: Number of bits of padding between register and value.
dd898b20 100 * @val_bits: Number of bits in a register value, mandatory.
2e2ae66d 101 *
3566cc9d 102 * @writeable_reg: Optional callback returning true if the register
76aad392
DC
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).
3566cc9d 107 * @readable_reg: Optional callback returning true if the register
76aad392
DC
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).
3566cc9d 112 * @volatile_reg: Optional callback returning true if the register
76aad392
DC
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).
3566cc9d 117 * @precious_reg: Optional callback returning true if the rgister
76aad392
DC
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).
bd20eb54
MB
129 *
130 * @max_register: Optional, specifies the maximum valid register index.
76aad392
DC
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.
bd20eb54
MB
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.
6f306441
LPC
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.
2e33caf1
AJ
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.
9fabe24e
DP
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.
141eba2e
SW
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.
6863ca62
KG
159 *
160 * @ranges: Array of configuration entries for virtual address ranges.
161 * @num_ranges: Number of range configuration entries.
dd898b20 162 */
b83a313b 163struct regmap_config {
d3c242e1
SW
164 const char *name;
165
b83a313b 166 int reg_bits;
f01ee60f 167 int reg_stride;
82159ba8 168 int pad_bits;
b83a313b 169 int val_bits;
2e2ae66d 170
2e2ae66d
MB
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);
18694886 174 bool (*precious_reg)(struct device *dev, unsigned int reg);
0d4529c5
DC
175 regmap_lock lock;
176 regmap_unlock unlock;
177 void *lock_arg;
bd20eb54
MB
178
179 unsigned int max_register;
76aad392
DC
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;
720e4616 184 const struct reg_default *reg_defaults;
9fabe24e
DP
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;
6f306441
LPC
189
190 u8 read_flag_mask;
191 u8 write_flag_mask;
2e33caf1
AJ
192
193 bool use_single_rw;
141eba2e
SW
194
195 enum regmap_endian reg_format_endian;
196 enum regmap_endian val_format_endian;
38e23194 197
6863ca62 198 const struct regmap_range_cfg *ranges;
e3549cd0 199 unsigned int num_ranges;
6863ca62
KG
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 *
d058bb49
MB
208 * @name: Descriptive name for diagnostics
209 *
6863ca62
KG
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 */
220struct regmap_range_cfg {
d058bb49
MB
221 const char *name;
222
6863ca62
KG
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;
b83a313b
MB
235};
236
0135bbcc 237typedef int (*regmap_hw_write)(void *context, const void *data,
b83a313b 238 size_t count);
0135bbcc 239typedef int (*regmap_hw_gather_write)(void *context,
b83a313b
MB
240 const void *reg, size_t reg_len,
241 const void *val, size_t val_len);
0135bbcc 242typedef int (*regmap_hw_read)(void *context,
b83a313b
MB
243 const void *reg_buf, size_t reg_size,
244 void *val_buf, size_t val_size);
0135bbcc 245typedef void (*regmap_hw_free_context)(void *context);
b83a313b
MB
246
247/**
248 * Description of a hardware bus for the register map infrastructure.
249 *
bacdbe07 250 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
0d4529c5
DC
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).
b83a313b
MB
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.
b83a313b
MB
259 * @read_flag_mask: Mask to be set in the top byte of the register when doing
260 * a read.
141eba2e
SW
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.
b83a313b
MB
267 */
268struct regmap_bus {
bacdbe07 269 bool fast_io;
b83a313b
MB
270 regmap_hw_write write;
271 regmap_hw_gather_write gather_write;
272 regmap_hw_read read;
0135bbcc 273 regmap_hw_free_context free_context;
b83a313b 274 u8 read_flag_mask;
141eba2e
SW
275 enum regmap_endian reg_format_endian_default;
276 enum regmap_endian val_format_endian_default;
b83a313b
MB
277};
278
279struct regmap *regmap_init(struct device *dev,
280 const struct regmap_bus *bus,
0135bbcc 281 void *bus_context,
b83a313b 282 const struct regmap_config *config);
9943fa30
MB
283struct regmap *regmap_init_i2c(struct i2c_client *i2c,
284 const struct regmap_config *config);
a676f083
MB
285struct regmap *regmap_init_spi(struct spi_device *dev,
286 const struct regmap_config *config);
45f5ff81
SW
287struct regmap *regmap_init_mmio(struct device *dev,
288 void __iomem *regs,
289 const struct regmap_config *config);
a676f083 290
c0eb4676
MB
291struct regmap *devm_regmap_init(struct device *dev,
292 const struct regmap_bus *bus,
0135bbcc 293 void *bus_context,
c0eb4676
MB
294 const struct regmap_config *config);
295struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
296 const struct regmap_config *config);
297struct regmap *devm_regmap_init_spi(struct spi_device *dev,
298 const struct regmap_config *config);
45f5ff81
SW
299struct regmap *devm_regmap_init_mmio(struct device *dev,
300 void __iomem *regs,
301 const struct regmap_config *config);
c0eb4676 302
b83a313b 303void regmap_exit(struct regmap *map);
bf315173
MB
304int regmap_reinit_cache(struct regmap *map,
305 const struct regmap_config *config);
72b39f6f 306struct regmap *dev_get_regmap(struct device *dev, const char *name);
b83a313b
MB
307int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
308int regmap_raw_write(struct regmap *map, unsigned int reg,
309 const void *val, size_t val_len);
8eaeb219
LD
310int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
311 size_t val_count);
b83a313b
MB
312int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
313int regmap_raw_read(struct regmap *map, unsigned int reg,
314 void *val, size_t val_len);
315int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
316 size_t val_count);
317int regmap_update_bits(struct regmap *map, unsigned int reg,
318 unsigned int mask, unsigned int val);
018690d3
MB
319int regmap_update_bits_check(struct regmap *map, unsigned int reg,
320 unsigned int mask, unsigned int val,
321 bool *change);
a6539c32 322int regmap_get_val_bytes(struct regmap *map);
b83a313b 323
39a58439 324int regcache_sync(struct regmap *map);
4d4cfd16
MB
325int regcache_sync_region(struct regmap *map, unsigned int min,
326 unsigned int max);
92afb286 327void regcache_cache_only(struct regmap *map, bool enable);
6eb0f5e0 328void regcache_cache_bypass(struct regmap *map, bool enable);
8ae0d7e8 329void regcache_mark_dirty(struct regmap *map);
92afb286 330
22f0d90a
MB
331int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
332 int num_regs);
333
76aad392
DC
334static 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
340bool regmap_reg_in_ranges(unsigned int reg,
341 const struct regmap_range *ranges,
342 unsigned int nranges);
343
f8beab2b
MB
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 */
350struct 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.
a43fd50d 365 * @wake_base: Base address for wake enables. If zero unsupported.
022f926a 366 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
0c00c50b 367 * @runtime_pm: Hold a runtime PM lock on the device when accessing it.
f8beab2b
MB
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 */
374struct regmap_irq_chip {
375 const char *name;
376
377 unsigned int status_base;
378 unsigned int mask_base;
379 unsigned int ack_base;
a43fd50d 380 unsigned int wake_base;
022f926a 381 unsigned int irq_reg_stride;
36ac914b 382 unsigned int mask_invert;
0c00c50b 383 bool runtime_pm;
f8beab2b
MB
384
385 int num_regs;
386
387 const struct regmap_irq *irqs;
388 int num_irqs;
389};
390
391struct regmap_irq_chip_data;
392
393int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
b026ddbb 394 int irq_base, const struct regmap_irq_chip *chip,
f8beab2b
MB
395 struct regmap_irq_chip_data **data);
396void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
209a6006 397int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
4af8be67 398int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
92afb286 399
9cde5fcd
MB
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
409static 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
416static 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
423static 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
430static 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
437static 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
444static 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
451static 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
458static 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
467static inline int regmap_get_val_bytes(struct regmap *map)
468{
469 WARN_ONCE(1, "regmap API is disabled");
470 return -EINVAL;
471}
472
473static inline int regcache_sync(struct regmap *map)
474{
475 WARN_ONCE(1, "regmap API is disabled");
476 return -EINVAL;
477}
478
a313f9f5
MB
479static 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
9cde5fcd
MB
486static inline void regcache_cache_only(struct regmap *map, bool enable)
487{
488 WARN_ONCE(1, "regmap API is disabled");
489}
490
491static inline void regcache_cache_bypass(struct regmap *map, bool enable)
492{
493 WARN_ONCE(1, "regmap API is disabled");
494}
495
496static inline void regcache_mark_dirty(struct regmap *map)
497{
498 WARN_ONCE(1, "regmap API is disabled");
499}
500
501static 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
72b39f6f
MB
509static inline struct regmap *dev_get_regmap(struct device *dev,
510 const char *name)
511{
72b39f6f
MB
512 return NULL;
513}
514
9cde5fcd
MB
515#endif
516
b83a313b 517#endif
This page took 0.147488 seconds and 5 git commands to generate.