blob: 26136b57700995f72a689a49d91901fafbe7d0aa [file] [log] [blame]
Mark Brownb83a3132011-05-11 19:59:58 +02001#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
Mark Brownb83a3132011-05-11 19:59:58 +020016#include <linux/list.h>
Mark Brownb83a3132011-05-11 19:59:58 +020017
Paul Gortmakerde477252011-05-26 13:46:22 -040018struct module;
Paul Gortmaker313162d2012-01-30 11:46:54 -050019struct device;
Mark Brown9943fa32011-06-20 19:02:29 +010020struct i2c_client;
Mark Browna676f082011-05-12 11:42:10 +020021struct spi_device;
Mark Brownb83d2ff2012-03-11 11:49:17 +000022struct regmap;
Mark Brown9943fa32011-06-20 19:02:29 +010023
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010024/* An enum of all the supported cache types */
25enum regcache_type {
26 REGCACHE_NONE,
Dimitris Papastamos28644c802011-09-19 14:34:02 +010027 REGCACHE_RBTREE,
Mark Brown50b776f2011-11-02 15:00:03 +000028 REGCACHE_COMPRESSED
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010029};
30
Mark Browndd898b22011-07-20 22:28:58 +010031/**
Mark Brownbd20eb52011-08-19 18:09:38 +090032 * 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 */
39struct reg_default {
40 unsigned int reg;
41 unsigned int def;
42};
43
Mark Brownb83d2ff2012-03-11 11:49:17 +000044#ifdef CONFIG_REGMAP
45
Stephen Warren141eba22012-05-24 10:47:26 -060046enum 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
Mark Brownbd20eb52011-08-19 18:09:38 +090054/**
Mark Browndd898b22011-07-20 22:28:58 +010055 * Configuration for the register map of a device.
56 *
Stephen Warrend3c242e2012-04-04 15:48:29 -060057 * @name: Optional name of the regmap. Useful when a device has multiple
58 * register regions.
59 *
Mark Browndd898b22011-07-20 22:28:58 +010060 * @reg_bits: Number of bits in a register address, mandatory.
Stephen Warrenf01ee602012-04-09 13:40:24 -060061 * @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.
Mark Brown82159ba2012-01-18 10:52:25 +000064 * @pad_bits: Number of bits of padding between register and value.
Mark Browndd898b22011-07-20 22:28:58 +010065 * @val_bits: Number of bits in a register value, mandatory.
Mark Brown2e2ae662011-07-20 22:33:39 +010066 *
Mark Brown3566cc92011-08-09 10:23:22 +090067 * @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).
Mark Brownbd20eb52011-08-19 18:09:38 +090076 *
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.
Lars-Peter Clausen6f306442011-09-05 20:46:32 +020081 *
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.
Ashish Jangam2e33caf2012-04-30 23:23:40 +010087 * @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.
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010090 *
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.
Stephen Warren141eba22012-05-24 10:47:26 -060095 * @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.
Mark Browndd898b22011-07-20 22:28:58 +0100101 */
Mark Brownb83a3132011-05-11 19:59:58 +0200102struct regmap_config {
Stephen Warrend3c242e2012-04-04 15:48:29 -0600103 const char *name;
104
Mark Brownb83a3132011-05-11 19:59:58 +0200105 int reg_bits;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600106 int reg_stride;
Mark Brown82159ba2012-01-18 10:52:25 +0000107 int pad_bits;
Mark Brownb83a3132011-05-11 19:59:58 +0200108 int val_bits;
Mark Brown2e2ae662011-07-20 22:33:39 +0100109
Mark Brown2e2ae662011-07-20 22:33:39 +0100110 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);
Mark Brown18694882011-08-08 15:40:22 +0900113 bool (*precious_reg)(struct device *dev, unsigned int reg);
Mark Brownbd20eb52011-08-19 18:09:38 +0900114
115 unsigned int max_register;
Lars-Peter Clausen720e4612011-11-16 16:28:17 +0100116 const struct reg_default *reg_defaults;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100117 unsigned int num_reg_defaults;
118 enum regcache_type cache_type;
119 const void *reg_defaults_raw;
120 unsigned int num_reg_defaults_raw;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +0200121
122 u8 read_flag_mask;
123 u8 write_flag_mask;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100124
125 bool use_single_rw;
Stephen Warren141eba22012-05-24 10:47:26 -0600126
127 enum regmap_endian reg_format_endian;
128 enum regmap_endian val_format_endian;
Mark Brownb83a3132011-05-11 19:59:58 +0200129};
130
Stephen Warren0135bbc2012-04-04 15:48:30 -0600131typedef int (*regmap_hw_write)(void *context, const void *data,
Mark Brownb83a3132011-05-11 19:59:58 +0200132 size_t count);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600133typedef int (*regmap_hw_gather_write)(void *context,
Mark Brownb83a3132011-05-11 19:59:58 +0200134 const void *reg, size_t reg_len,
135 const void *val, size_t val_len);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600136typedef int (*regmap_hw_read)(void *context,
Mark Brownb83a3132011-05-11 19:59:58 +0200137 const void *reg_buf, size_t reg_size,
138 void *val_buf, size_t val_size);
Stephen Warren0135bbc2012-04-04 15:48:30 -0600139typedef void (*regmap_hw_free_context)(void *context);
Mark Brownb83a3132011-05-11 19:59:58 +0200140
141/**
142 * Description of a hardware bus for the register map infrastructure.
143 *
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600144 * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
145 * to perform locking.
Mark Brownb83a3132011-05-11 19:59:58 +0200146 * @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.
Mark Brownb83a3132011-05-11 19:59:58 +0200151 * @read_flag_mask: Mask to be set in the top byte of the register when doing
152 * a read.
Stephen Warren141eba22012-05-24 10:47:26 -0600153 * @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.
Mark Brownb83a3132011-05-11 19:59:58 +0200159 */
160struct regmap_bus {
Stephen Warrenbacdbe02012-04-04 15:48:28 -0600161 bool fast_io;
Mark Brownb83a3132011-05-11 19:59:58 +0200162 regmap_hw_write write;
163 regmap_hw_gather_write gather_write;
164 regmap_hw_read read;
Stephen Warren0135bbc2012-04-04 15:48:30 -0600165 regmap_hw_free_context free_context;
Mark Brownb83a3132011-05-11 19:59:58 +0200166 u8 read_flag_mask;
Stephen Warren141eba22012-05-24 10:47:26 -0600167 enum regmap_endian reg_format_endian_default;
168 enum regmap_endian val_format_endian_default;
Mark Brownb83a3132011-05-11 19:59:58 +0200169};
170
171struct regmap *regmap_init(struct device *dev,
172 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600173 void *bus_context,
Mark Brownb83a3132011-05-11 19:59:58 +0200174 const struct regmap_config *config);
Mark Brown9943fa32011-06-20 19:02:29 +0100175struct regmap *regmap_init_i2c(struct i2c_client *i2c,
176 const struct regmap_config *config);
Mark Browna676f082011-05-12 11:42:10 +0200177struct regmap *regmap_init_spi(struct spi_device *dev,
178 const struct regmap_config *config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600179struct regmap *regmap_init_mmio(struct device *dev,
180 void __iomem *regs,
181 const struct regmap_config *config);
Mark Browna676f082011-05-12 11:42:10 +0200182
Mark Brownc0eb4672012-01-30 19:56:52 +0000183struct regmap *devm_regmap_init(struct device *dev,
184 const struct regmap_bus *bus,
Stephen Warren0135bbc2012-04-04 15:48:30 -0600185 void *bus_context,
Mark Brownc0eb4672012-01-30 19:56:52 +0000186 const struct regmap_config *config);
187struct regmap *devm_regmap_init_i2c(struct i2c_client *i2c,
188 const struct regmap_config *config);
189struct regmap *devm_regmap_init_spi(struct spi_device *dev,
190 const struct regmap_config *config);
Stephen Warren45f5ff82012-04-04 15:48:31 -0600191struct regmap *devm_regmap_init_mmio(struct device *dev,
192 void __iomem *regs,
193 const struct regmap_config *config);
Mark Brownc0eb4672012-01-30 19:56:52 +0000194
Mark Brownb83a3132011-05-11 19:59:58 +0200195void regmap_exit(struct regmap *map);
Mark Brownbf315172011-12-03 17:06:20 +0000196int regmap_reinit_cache(struct regmap *map,
197 const struct regmap_config *config);
Mark Brown72b39f62012-05-08 17:44:40 +0100198struct regmap *dev_get_regmap(struct device *dev, const char *name);
Mark Brownb83a3132011-05-11 19:59:58 +0200199int regmap_write(struct regmap *map, unsigned int reg, unsigned int val);
200int regmap_raw_write(struct regmap *map, unsigned int reg,
201 const void *val, size_t val_len);
Laxman Dewangan8eaeb212012-02-12 19:49:43 +0530202int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
203 size_t val_count);
Mark Brownb83a3132011-05-11 19:59:58 +0200204int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val);
205int regmap_raw_read(struct regmap *map, unsigned int reg,
206 void *val, size_t val_len);
207int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
208 size_t val_count);
209int regmap_update_bits(struct regmap *map, unsigned int reg,
210 unsigned int mask, unsigned int val);
Mark Brown018690d2011-11-29 20:10:36 +0000211int regmap_update_bits_check(struct regmap *map, unsigned int reg,
212 unsigned int mask, unsigned int val,
213 bool *change);
Mark Browna6539c32012-02-17 14:20:14 -0800214int regmap_get_val_bytes(struct regmap *map);
Mark Brownb83a3132011-05-11 19:59:58 +0200215
Mark Brown39a58432011-09-19 18:21:49 +0100216int regcache_sync(struct regmap *map);
Mark Brown4d4cfd12012-02-23 20:53:37 +0000217int regcache_sync_region(struct regmap *map, unsigned int min,
218 unsigned int max);
Mark Brown92afb282011-09-19 18:22:14 +0100219void regcache_cache_only(struct regmap *map, bool enable);
Dimitris Papastamos6eb0f5e2011-09-29 14:36:27 +0100220void regcache_cache_bypass(struct regmap *map, bool enable);
Mark Brown8ae0d7e2011-10-26 10:34:22 +0200221void regcache_mark_dirty(struct regmap *map);
Mark Brown92afb282011-09-19 18:22:14 +0100222
Mark Brown22f0d902012-01-21 12:01:14 +0000223int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
224 int num_regs);
225
Mark Brownf8beab22011-10-28 23:50:49 +0200226/**
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 */
232struct 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.
Graeme Gregory022f926a2012-05-14 22:40:43 +0900247 * @irq_reg_stride: Stride to use for chips where registers are not contiguous.
Mark Brownf8beab22011-10-28 23:50:49 +0200248 *
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 */
254struct regmap_irq_chip {
255 const char *name;
256
257 unsigned int status_base;
258 unsigned int mask_base;
259 unsigned int ack_base;
Graeme Gregory022f926a2012-05-14 22:40:43 +0900260 unsigned int irq_reg_stride;
Mark Brownf8beab22011-10-28 23:50:49 +0200261
262 int num_regs;
263
264 const struct regmap_irq *irqs;
265 int num_irqs;
266};
267
268struct regmap_irq_chip_data;
269
270int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
271 int irq_base, struct regmap_irq_chip *chip,
272 struct regmap_irq_chip_data **data);
273void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *data);
Mark Brown209a6002011-12-05 16:10:15 +0000274int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data);
Mark Brown4af8be62012-05-13 10:59:56 +0100275int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq);
Mark Brownb83a3132011-05-11 19:59:58 +0200276
Mark Brown9cde5fc2012-02-17 14:49:51 -0800277#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
286static 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
293static 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
300static 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
307static 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
314static 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
321static 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
328static 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
335static 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
344static inline int regmap_get_val_bytes(struct regmap *map)
345{
346 WARN_ONCE(1, "regmap API is disabled");
347 return -EINVAL;
348}
349
350static inline int regcache_sync(struct regmap *map)
351{
352 WARN_ONCE(1, "regmap API is disabled");
353 return -EINVAL;
354}
355
Mark Browna313f9f2012-02-23 19:49:43 +0000356static 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
Mark Brown9cde5fc2012-02-17 14:49:51 -0800363static inline void regcache_cache_only(struct regmap *map, bool enable)
364{
365 WARN_ONCE(1, "regmap API is disabled");
366}
367
368static inline void regcache_cache_bypass(struct regmap *map, bool enable)
369{
370 WARN_ONCE(1, "regmap API is disabled");
371}
372
373static inline void regcache_mark_dirty(struct regmap *map)
374{
375 WARN_ONCE(1, "regmap API is disabled");
376}
377
378static 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
Mark Brown72b39f62012-05-08 17:44:40 +0100386static inline struct regmap *dev_get_regmap(struct device *dev,
387 const char *name)
388{
389 WARN_ONCE(1, "regmap API is disabled");
390 return NULL;
391}
392
Mark Brown9cde5fc2012-02-17 14:49:51 -0800393#endif
394
Mark Brownb83a3132011-05-11 19:59:58 +0200395#endif