2 * Register map access API - MMIO support
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/clk.h>
20 #include <linux/err.h>
22 #include <linux/module.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
26 struct regmap_mmio_context {
32 static int regmap_mmio_gather_write(void *context,
33 const void *reg, size_t reg_size,
34 const void *val, size_t val_size)
36 struct regmap_mmio_context *ctx = context;
40 BUG_ON(reg_size != 4);
42 if (!IS_ERR(ctx->clk)) {
43 ret = clk_enable(ctx->clk);
51 switch (ctx->val_bytes) {
53 writeb(*(u8 *)val, ctx->regs + offset);
56 writew(*(u16 *)val, ctx->regs + offset);
59 writel(*(u32 *)val, ctx->regs + offset);
63 writeq(*(u64 *)val, ctx->regs + offset);
67 /* Should be caught by regmap_mmio_check_config */
70 val_size -= ctx->val_bytes;
71 val += ctx->val_bytes;
72 offset += ctx->val_bytes;
75 if (!IS_ERR(ctx->clk))
76 clk_disable(ctx->clk);
81 static int regmap_mmio_write(void *context, const void *data, size_t count)
85 return regmap_mmio_gather_write(context, data, 4, data + 4, count - 4);
88 static int regmap_mmio_read(void *context,
89 const void *reg, size_t reg_size,
90 void *val, size_t val_size)
92 struct regmap_mmio_context *ctx = context;
96 BUG_ON(reg_size != 4);
98 if (!IS_ERR(ctx->clk)) {
99 ret = clk_enable(ctx->clk);
104 offset = *(u32 *)reg;
107 switch (ctx->val_bytes) {
109 *(u8 *)val = readb(ctx->regs + offset);
112 *(u16 *)val = readw(ctx->regs + offset);
115 *(u32 *)val = readl(ctx->regs + offset);
119 *(u64 *)val = readq(ctx->regs + offset);
123 /* Should be caught by regmap_mmio_check_config */
126 val_size -= ctx->val_bytes;
127 val += ctx->val_bytes;
128 offset += ctx->val_bytes;
131 if (!IS_ERR(ctx->clk))
132 clk_disable(ctx->clk);
137 static void regmap_mmio_free_context(void *context)
139 struct regmap_mmio_context *ctx = context;
141 if (!IS_ERR(ctx->clk)) {
142 clk_unprepare(ctx->clk);
148 static struct regmap_bus regmap_mmio = {
150 .write = regmap_mmio_write,
151 .gather_write = regmap_mmio_gather_write,
152 .read = regmap_mmio_read,
153 .free_context = regmap_mmio_free_context,
154 .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
155 .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
158 static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
161 const struct regmap_config *config)
163 struct regmap_mmio_context *ctx;
167 if (config->reg_bits != 32)
168 return ERR_PTR(-EINVAL);
170 if (config->pad_bits)
171 return ERR_PTR(-EINVAL);
173 switch (config->val_bits) {
175 /* The core treats 0 as 1 */
191 return ERR_PTR(-EINVAL);
194 if (config->reg_stride < min_stride)
195 return ERR_PTR(-EINVAL);
197 switch (config->reg_format_endian) {
198 case REGMAP_ENDIAN_DEFAULT:
199 case REGMAP_ENDIAN_NATIVE:
202 return ERR_PTR(-EINVAL);
205 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
207 return ERR_PTR(-ENOMEM);
210 ctx->val_bytes = config->val_bits / 8;
211 ctx->clk = ERR_PTR(-ENODEV);
213 ctx->clk = clk_get(dev, clk_id);
214 if (!IS_ERR(ctx->clk)) {
215 ret = clk_prepare(ctx->clk);
233 * regmap_init_mmio_clk(): Initialise register map with register clock
235 * @dev: Device that will be interacted with
236 * @clk_id: register clock consumer ID
237 * @regs: Pointer to memory-mapped IO region
238 * @config: Configuration for register map
240 * The return value will be an ERR_PTR() on error or a valid pointer to
243 struct regmap *regmap_init_mmio_clk(struct device *dev, const char *clk_id,
245 const struct regmap_config *config)
247 struct regmap_mmio_context *ctx;
249 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
251 return ERR_CAST(ctx);
253 return regmap_init(dev, ®map_mmio, ctx, config);
255 EXPORT_SYMBOL_GPL(regmap_init_mmio_clk);
258 * devm_regmap_init_mmio_clk(): Initialise managed register map with clock
260 * @dev: Device that will be interacted with
261 * @clk_id: register clock consumer ID
262 * @regs: Pointer to memory-mapped IO region
263 * @config: Configuration for register map
265 * The return value will be an ERR_PTR() on error or a valid pointer
266 * to a struct regmap. The regmap will be automatically freed by the
267 * device management code.
269 struct regmap *devm_regmap_init_mmio_clk(struct device *dev, const char *clk_id,
271 const struct regmap_config *config)
273 struct regmap_mmio_context *ctx;
275 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
277 return ERR_CAST(ctx);
279 return devm_regmap_init(dev, ®map_mmio, ctx, config);
281 EXPORT_SYMBOL_GPL(devm_regmap_init_mmio_clk);
283 MODULE_LICENSE("GPL v2");