Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / base / regmap / regmap-mmio.c
blob7526906ca080f81dcff1499b7e57c2ff0b79569e
1 /*
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
13 * more details.
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>
21 #include <linux/io.h>
22 #include <linux/module.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
26 struct regmap_mmio_context {
27 void __iomem *regs;
28 unsigned val_bytes;
29 struct clk *clk;
31 void (*reg_write)(struct regmap_mmio_context *ctx,
32 unsigned int reg, unsigned int val);
33 unsigned int (*reg_read)(struct regmap_mmio_context *ctx,
34 unsigned int reg);
37 static int regmap_mmio_regbits_check(size_t reg_bits)
39 switch (reg_bits) {
40 case 8:
41 case 16:
42 case 32:
43 #ifdef CONFIG_64BIT
44 case 64:
45 #endif
46 return 0;
47 default:
48 return -EINVAL;
52 static int regmap_mmio_get_min_stride(size_t val_bits)
54 int min_stride;
56 switch (val_bits) {
57 case 8:
58 /* The core treats 0 as 1 */
59 min_stride = 0;
60 return 0;
61 case 16:
62 min_stride = 2;
63 break;
64 case 32:
65 min_stride = 4;
66 break;
67 #ifdef CONFIG_64BIT
68 case 64:
69 min_stride = 8;
70 break;
71 #endif
72 default:
73 return -EINVAL;
76 return min_stride;
79 static void regmap_mmio_write8(struct regmap_mmio_context *ctx,
80 unsigned int reg,
81 unsigned int val)
83 writeb(val, ctx->regs + reg);
86 static void regmap_mmio_write16le(struct regmap_mmio_context *ctx,
87 unsigned int reg,
88 unsigned int val)
90 writew(val, ctx->regs + reg);
93 static void regmap_mmio_write16be(struct regmap_mmio_context *ctx,
94 unsigned int reg,
95 unsigned int val)
97 iowrite16be(val, ctx->regs + reg);
100 static void regmap_mmio_write32le(struct regmap_mmio_context *ctx,
101 unsigned int reg,
102 unsigned int val)
104 writel(val, ctx->regs + reg);
107 static void regmap_mmio_write32be(struct regmap_mmio_context *ctx,
108 unsigned int reg,
109 unsigned int val)
111 iowrite32be(val, ctx->regs + reg);
114 #ifdef CONFIG_64BIT
115 static void regmap_mmio_write64le(struct regmap_mmio_context *ctx,
116 unsigned int reg,
117 unsigned int val)
119 writeq(val, ctx->regs + reg);
121 #endif
123 static int regmap_mmio_write(void *context, unsigned int reg, unsigned int val)
125 struct regmap_mmio_context *ctx = context;
126 int ret;
128 if (!IS_ERR(ctx->clk)) {
129 ret = clk_enable(ctx->clk);
130 if (ret < 0)
131 return ret;
134 ctx->reg_write(ctx, reg, val);
136 if (!IS_ERR(ctx->clk))
137 clk_disable(ctx->clk);
139 return 0;
142 static unsigned int regmap_mmio_read8(struct regmap_mmio_context *ctx,
143 unsigned int reg)
145 return readb(ctx->regs + reg);
148 static unsigned int regmap_mmio_read16le(struct regmap_mmio_context *ctx,
149 unsigned int reg)
151 return readw(ctx->regs + reg);
154 static unsigned int regmap_mmio_read16be(struct regmap_mmio_context *ctx,
155 unsigned int reg)
157 return ioread16be(ctx->regs + reg);
160 static unsigned int regmap_mmio_read32le(struct regmap_mmio_context *ctx,
161 unsigned int reg)
163 return readl(ctx->regs + reg);
166 static unsigned int regmap_mmio_read32be(struct regmap_mmio_context *ctx,
167 unsigned int reg)
169 return ioread32be(ctx->regs + reg);
172 #ifdef CONFIG_64BIT
173 static unsigned int regmap_mmio_read64le(struct regmap_mmio_context *ctx,
174 unsigned int reg)
176 return readq(ctx->regs + reg);
178 #endif
180 static int regmap_mmio_read(void *context, unsigned int reg, unsigned int *val)
182 struct regmap_mmio_context *ctx = context;
183 int ret;
185 if (!IS_ERR(ctx->clk)) {
186 ret = clk_enable(ctx->clk);
187 if (ret < 0)
188 return ret;
191 *val = ctx->reg_read(ctx, reg);
193 if (!IS_ERR(ctx->clk))
194 clk_disable(ctx->clk);
196 return 0;
199 static void regmap_mmio_free_context(void *context)
201 struct regmap_mmio_context *ctx = context;
203 if (!IS_ERR(ctx->clk)) {
204 clk_unprepare(ctx->clk);
205 clk_put(ctx->clk);
207 kfree(context);
210 static const struct regmap_bus regmap_mmio = {
211 .fast_io = true,
212 .reg_write = regmap_mmio_write,
213 .reg_read = regmap_mmio_read,
214 .free_context = regmap_mmio_free_context,
217 static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
218 const char *clk_id,
219 void __iomem *regs,
220 const struct regmap_config *config)
222 struct regmap_mmio_context *ctx;
223 int min_stride;
224 int ret;
226 ret = regmap_mmio_regbits_check(config->reg_bits);
227 if (ret)
228 return ERR_PTR(ret);
230 if (config->pad_bits)
231 return ERR_PTR(-EINVAL);
233 min_stride = regmap_mmio_get_min_stride(config->val_bits);
234 if (min_stride < 0)
235 return ERR_PTR(min_stride);
237 if (config->reg_stride < min_stride)
238 return ERR_PTR(-EINVAL);
240 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
241 if (!ctx)
242 return ERR_PTR(-ENOMEM);
244 ctx->regs = regs;
245 ctx->val_bytes = config->val_bits / 8;
246 ctx->clk = ERR_PTR(-ENODEV);
248 switch (config->reg_format_endian) {
249 case REGMAP_ENDIAN_DEFAULT:
250 case REGMAP_ENDIAN_LITTLE:
251 #ifdef __LITTLE_ENDIAN
252 case REGMAP_ENDIAN_NATIVE:
253 #endif
254 switch (config->val_bits) {
255 case 8:
256 ctx->reg_read = regmap_mmio_read8;
257 ctx->reg_write = regmap_mmio_write8;
258 break;
259 case 16:
260 ctx->reg_read = regmap_mmio_read16le;
261 ctx->reg_write = regmap_mmio_write16le;
262 break;
263 case 32:
264 ctx->reg_read = regmap_mmio_read32le;
265 ctx->reg_write = regmap_mmio_write32le;
266 break;
267 #ifdef CONFIG_64BIT
268 case 64:
269 ctx->reg_read = regmap_mmio_read64le;
270 ctx->reg_write = regmap_mmio_write64le;
271 break;
272 #endif
273 default:
274 ret = -EINVAL;
275 goto err_free;
277 break;
278 case REGMAP_ENDIAN_BIG:
279 #ifdef __BIG_ENDIAN
280 case REGMAP_ENDIAN_NATIVE:
281 #endif
282 switch (config->val_bits) {
283 case 8:
284 ctx->reg_read = regmap_mmio_read8;
285 ctx->reg_write = regmap_mmio_write8;
286 break;
287 case 16:
288 ctx->reg_read = regmap_mmio_read16be;
289 ctx->reg_write = regmap_mmio_write16be;
290 break;
291 case 32:
292 ctx->reg_read = regmap_mmio_read32be;
293 ctx->reg_write = regmap_mmio_write32be;
294 break;
295 default:
296 ret = -EINVAL;
297 goto err_free;
299 break;
300 default:
301 ret = -EINVAL;
302 goto err_free;
305 if (clk_id == NULL)
306 return ctx;
308 ctx->clk = clk_get(dev, clk_id);
309 if (IS_ERR(ctx->clk)) {
310 ret = PTR_ERR(ctx->clk);
311 goto err_free;
314 ret = clk_prepare(ctx->clk);
315 if (ret < 0) {
316 clk_put(ctx->clk);
317 goto err_free;
320 return ctx;
322 err_free:
323 kfree(ctx);
325 return ERR_PTR(ret);
328 struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
329 void __iomem *regs,
330 const struct regmap_config *config,
331 struct lock_class_key *lock_key,
332 const char *lock_name)
334 struct regmap_mmio_context *ctx;
336 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
337 if (IS_ERR(ctx))
338 return ERR_CAST(ctx);
340 return __regmap_init(dev, &regmap_mmio, ctx, config,
341 lock_key, lock_name);
343 EXPORT_SYMBOL_GPL(__regmap_init_mmio_clk);
345 struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
346 const char *clk_id,
347 void __iomem *regs,
348 const struct regmap_config *config,
349 struct lock_class_key *lock_key,
350 const char *lock_name)
352 struct regmap_mmio_context *ctx;
354 ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
355 if (IS_ERR(ctx))
356 return ERR_CAST(ctx);
358 return __devm_regmap_init(dev, &regmap_mmio, ctx, config,
359 lock_key, lock_name);
361 EXPORT_SYMBOL_GPL(__devm_regmap_init_mmio_clk);
363 MODULE_LICENSE("GPL v2");