blob: b01fe59fbfe8743ba0663a065f457a9d6e6ae069 [file] [log] [blame]
Mark Brown93de9122011-07-20 22:35:37 +01001/*
2 * Register map access API internal header
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef _REGMAP_INTERNAL_H
14#define _REGMAP_INTERNAL_H
15
16#include <linux/regmap.h>
Mark Brown31244e32011-07-20 22:56:53 +010017#include <linux/fs.h>
Mark Brown5166b7c2012-12-11 01:24:29 +090018#include <linux/list.h>
Mark Brown0d509f22013-01-27 22:07:38 +080019#include <linux/wait.h>
Mark Brown93de9122011-07-20 22:35:37 +010020
21struct regmap;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010022struct regcache_ops;
Mark Brown93de9122011-07-20 22:35:37 +010023
Mark Brown5166b7c2012-12-11 01:24:29 +090024struct regmap_debugfs_off_cache {
25 struct list_head list;
26 off_t min;
27 off_t max;
28 unsigned int base_reg;
Dimitris Papastamosc2c1ee62013-02-08 12:47:14 +000029 unsigned int max_reg;
Mark Brown5166b7c2012-12-11 01:24:29 +090030};
31
Mark Brown93de9122011-07-20 22:35:37 +010032struct regmap_format {
33 size_t buf_size;
34 size_t reg_bytes;
Mark Brown82159ba2012-01-18 10:52:25 +000035 size_t pad_bytes;
Mark Brown93de9122011-07-20 22:35:37 +010036 size_t val_bytes;
37 void (*format_write)(struct regmap *map,
38 unsigned int reg, unsigned int val);
Marc Reillyd939fb92012-03-16 12:11:43 +110039 void (*format_reg)(void *buf, unsigned int reg, unsigned int shift);
40 void (*format_val)(void *buf, unsigned int val, unsigned int shift);
Mark Brown8a819ff2013-03-04 09:04:51 +080041 unsigned int (*parse_val)(const void *buf);
42 void (*parse_inplace)(void *buf);
Mark Brown93de9122011-07-20 22:35:37 +010043};
44
Mark Brown0d509f22013-01-27 22:07:38 +080045struct regmap_async {
46 struct list_head list;
47 struct work_struct cleanup;
48 struct regmap *map;
49 void *work_buf;
50};
51
Mark Brown93de9122011-07-20 22:35:37 +010052struct regmap {
Stephen Warrenbacdbe02012-04-04 15:48:28 -060053 struct mutex mutex;
54 spinlock_t spinlock;
55 regmap_lock lock;
56 regmap_unlock unlock;
Davide Ciminaghi0d4529c2012-10-16 15:56:59 +020057 void *lock_arg; /* This is passed to lock/unlock functions */
Mark Brown93de9122011-07-20 22:35:37 +010058
59 struct device *dev; /* Device we do I/O on */
60 void *work_buf; /* Scratch buffer used to format I/O */
61 struct regmap_format format; /* Buffer format */
62 const struct regmap_bus *bus;
Stephen Warren0135bbc2012-04-04 15:48:30 -060063 void *bus_context;
Mark Brown72b39f62012-05-08 17:44:40 +010064 const char *name;
Mark Brown93de9122011-07-20 22:35:37 +010065
Mark Brown0d509f22013-01-27 22:07:38 +080066 spinlock_t async_lock;
67 wait_queue_head_t async_waitq;
68 struct list_head async_list;
69 int async_ret;
70
Mark Brown31244e32011-07-20 22:56:53 +010071#ifdef CONFIG_DEBUG_FS
72 struct dentry *debugfs;
Stephen Warrend3c242e2012-04-04 15:48:29 -060073 const char *debugfs_name;
Mark Browncbc19382012-12-06 13:29:05 +090074
75 unsigned int debugfs_reg_len;
76 unsigned int debugfs_val_len;
77 unsigned int debugfs_tot_len;
Mark Brown5166b7c2012-12-11 01:24:29 +090078
79 struct list_head debugfs_off_cache;
Mark Brown31244e32011-07-20 22:56:53 +010080#endif
81
Mark Brown93de9122011-07-20 22:35:37 +010082 unsigned int max_register;
83 bool (*writeable_reg)(struct device *dev, unsigned int reg);
84 bool (*readable_reg)(struct device *dev, unsigned int reg);
85 bool (*volatile_reg)(struct device *dev, unsigned int reg);
Mark Brown2efe1642011-08-08 15:41:46 +090086 bool (*precious_reg)(struct device *dev, unsigned int reg);
Davide Ciminaghi76aad392012-11-20 15:20:30 +010087 const struct regmap_access_table *wr_table;
88 const struct regmap_access_table *rd_table;
89 const struct regmap_access_table *volatile_table;
90 const struct regmap_access_table *precious_table;
Lars-Peter Clausen6f306442011-09-05 20:46:32 +020091
Andrey Smirnovad278402013-01-12 12:54:12 -080092 int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
Andrey Smirnov07c320d2013-01-12 12:54:13 -080093 int (*reg_write)(void *context, unsigned int reg, unsigned int val);
Andrey Smirnovad278402013-01-12 12:54:12 -080094
Andrey Smirnovd2a58842013-01-27 10:49:05 -080095 bool defer_caching;
96
Lars-Peter Clausen6f306442011-09-05 20:46:32 +020097 u8 read_flag_mask;
98 u8 write_flag_mask;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +010099
Marc Reillyd939fb92012-03-16 12:11:43 +1100100 /* number of bits to (left) shift the reg value when formatting*/
101 int reg_shift;
Stephen Warrenf01ee602012-04-09 13:40:24 -0600102 int reg_stride;
Marc Reillyd939fb92012-03-16 12:11:43 +1100103
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100104 /* regcache specific members */
105 const struct regcache_ops *cache_ops;
106 enum regcache_type cache_type;
107
108 /* number of bytes in reg_defaults_raw */
109 unsigned int cache_size_raw;
110 /* number of bytes per word in reg_defaults_raw */
111 unsigned int cache_word_size;
112 /* number of entries in reg_defaults */
113 unsigned int num_reg_defaults;
114 /* number of entries in reg_defaults_raw */
115 unsigned int num_reg_defaults_raw;
116
117 /* if set, only the cache is modified not the HW */
Mark Brown847fb6f2012-02-06 18:01:35 +0000118 u32 cache_only;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100119 /* if set, only the HW is modified not the cache */
Mark Brown847fb6f2012-02-06 18:01:35 +0000120 u32 cache_bypass;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100121 /* if set, remember to free reg_defaults_raw */
Mark Brown847fb6f2012-02-06 18:01:35 +0000122 bool cache_free;
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100123
124 struct reg_default *reg_defaults;
125 const void *reg_defaults_raw;
126 void *cache;
Mark Brown847fb6f2012-02-06 18:01:35 +0000127 u32 cache_dirty;
Mark Brown22f0d902012-01-21 12:01:14 +0000128
Mark Brown78493f22013-03-29 19:18:59 +0000129 unsigned long *cache_present;
130 unsigned int cache_present_nbits;
131
Mark Brown22f0d902012-01-21 12:01:14 +0000132 struct reg_default *patch;
133 int patch_regs;
Ashish Jangam2e33caf2012-04-30 23:23:40 +0100134
135 /* if set, converts bulk rw to single rw */
136 bool use_single_rw;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100137
138 struct rb_root range_tree;
139 void *selector_work_buf; /* Scratch buffer used for selector */
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100140};
141
142struct regcache_ops {
143 const char *name;
144 enum regcache_type type;
145 int (*init)(struct regmap *map);
146 int (*exit)(struct regmap *map);
147 int (*read)(struct regmap *map, unsigned int reg, unsigned int *value);
148 int (*write)(struct regmap *map, unsigned int reg, unsigned int value);
Mark Brownac8d91c2012-02-23 19:31:04 +0000149 int (*sync)(struct regmap *map, unsigned int min, unsigned int max);
Mark Brown93de9122011-07-20 22:35:37 +0100150};
151
Mark Brown8de2f082011-08-10 17:14:41 +0900152bool regmap_writeable(struct regmap *map, unsigned int reg);
153bool regmap_readable(struct regmap *map, unsigned int reg);
154bool regmap_volatile(struct regmap *map, unsigned int reg);
155bool regmap_precious(struct regmap *map, unsigned int reg);
156
Dimitris Papastamos4d2dc092011-09-29 10:39:07 +0100157int _regmap_write(struct regmap *map, unsigned int reg,
158 unsigned int val);
159
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100160struct regmap_range_node {
161 struct rb_node node;
Mark Brownd058bb42012-10-03 12:40:47 +0100162 const char *name;
Mark Brown4b020b32012-10-03 13:13:16 +0100163 struct regmap *map;
Krystian Garbaciak6863ca62012-06-15 11:23:56 +0100164
165 unsigned int range_min;
166 unsigned int range_max;
167
168 unsigned int selector_reg;
169 unsigned int selector_mask;
170 int selector_shift;
171
172 unsigned int window_start;
173 unsigned int window_len;
174};
175
Mark Brown31244e32011-07-20 22:56:53 +0100176#ifdef CONFIG_DEBUG_FS
177extern void regmap_debugfs_initcall(void);
Stephen Warrend3c242e2012-04-04 15:48:29 -0600178extern void regmap_debugfs_init(struct regmap *map, const char *name);
Mark Brown31244e32011-07-20 22:56:53 +0100179extern void regmap_debugfs_exit(struct regmap *map);
180#else
Lars-Peter Clausenbbcf61c2011-09-05 22:06:13 +0200181static inline void regmap_debugfs_initcall(void) { }
Stephen Warrenabec95a2012-04-05 23:09:20 -0600182static inline void regmap_debugfs_init(struct regmap *map, const char *name) { }
Lars-Peter Clausenbbcf61c2011-09-05 22:06:13 +0200183static inline void regmap_debugfs_exit(struct regmap *map) { }
Mark Brown31244e32011-07-20 22:56:53 +0100184#endif
185
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100186/* regcache core declarations */
Lars-Peter Clausene5e3b8a2011-11-16 16:28:16 +0100187int regcache_init(struct regmap *map, const struct regmap_config *config);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100188void regcache_exit(struct regmap *map);
189int regcache_read(struct regmap *map,
190 unsigned int reg, unsigned int *value);
191int regcache_write(struct regmap *map,
192 unsigned int reg, unsigned int value);
193int regcache_sync(struct regmap *map);
194
Mark Brown88177962013-03-13 19:29:36 +0000195static inline const void *regcache_get_val_addr(struct regmap *map,
196 const void *base,
197 unsigned int idx)
198{
199 return base + (map->cache_word_size * idx);
200}
201
Mark Brown879082c2013-02-21 18:03:13 +0000202unsigned int regcache_get_val(struct regmap *map, const void *base,
203 unsigned int idx);
204bool regcache_set_val(struct regmap *map, void *base, unsigned int idx,
205 unsigned int val);
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100206int regcache_lookup_reg(struct regmap *map, unsigned int reg);
Mark Brown78493f22013-03-29 19:18:59 +0000207int regcache_set_reg_present(struct regmap *map, unsigned int reg);
208
209static inline bool regcache_reg_present(struct regmap *map, unsigned int reg)
210{
211 if (!map->cache_present)
212 return true;
213 if (reg > map->cache_present_nbits)
214 return false;
215 return map->cache_present[BIT_WORD(reg)] & BIT_MASK(reg);
216}
Dimitris Papastamos9fabe242011-09-19 14:34:00 +0100217
Mark Brown584de322013-03-13 19:19:34 +0000218int _regmap_raw_write(struct regmap *map, unsigned int reg,
219 const void *val, size_t val_len, bool async);
220
Mark Brown0d509f22013-01-27 22:07:38 +0800221void regmap_async_complete_cb(struct regmap_async *async, int ret);
222
Dimitris Papastamos28644c802011-09-19 14:34:02 +0100223extern struct regcache_ops regcache_rbtree_ops;
Dimitris Papastamos2cbbb572011-09-19 14:34:03 +0100224extern struct regcache_ops regcache_lzo_ops;
Mark Brown2ac902c2012-12-19 14:51:55 +0000225extern struct regcache_ops regcache_flat_ops;
Dimitris Papastamos2cbbb572011-09-19 14:34:03 +0100226
Mark Brown93de9122011-07-20 22:35:37 +0100227#endif