]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/regmap/regmap-mmio.c
drivers/base: delete non-required instances of include <linux/init.h>
[karo-tx-linux.git] / drivers / base / regmap / regmap-mmio.c
1 /*
2  * Register map access API - MMIO support
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5  *
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.
9  *
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.
14  *
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/>.
17  */
18
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>
25
26 struct regmap_mmio_context {
27         void __iomem *regs;
28         unsigned val_bytes;
29         struct clk *clk;
30 };
31
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)
35 {
36         struct regmap_mmio_context *ctx = context;
37         u32 offset;
38         int ret;
39
40         BUG_ON(reg_size != 4);
41
42         if (!IS_ERR(ctx->clk)) {
43                 ret = clk_enable(ctx->clk);
44                 if (ret < 0)
45                         return ret;
46         }
47
48         offset = *(u32 *)reg;
49
50         while (val_size) {
51                 switch (ctx->val_bytes) {
52                 case 1:
53                         writeb(*(u8 *)val, ctx->regs + offset);
54                         break;
55                 case 2:
56                         writew(*(u16 *)val, ctx->regs + offset);
57                         break;
58                 case 4:
59                         writel(*(u32 *)val, ctx->regs + offset);
60                         break;
61 #ifdef CONFIG_64BIT
62                 case 8:
63                         writeq(*(u64 *)val, ctx->regs + offset);
64                         break;
65 #endif
66                 default:
67                         /* Should be caught by regmap_mmio_check_config */
68                         BUG();
69                 }
70                 val_size -= ctx->val_bytes;
71                 val += ctx->val_bytes;
72                 offset += ctx->val_bytes;
73         }
74
75         if (!IS_ERR(ctx->clk))
76                 clk_disable(ctx->clk);
77
78         return 0;
79 }
80
81 static int regmap_mmio_write(void *context, const void *data, size_t count)
82 {
83         BUG_ON(count < 4);
84
85         return regmap_mmio_gather_write(context, data, 4, data + 4, count - 4);
86 }
87
88 static int regmap_mmio_read(void *context,
89                             const void *reg, size_t reg_size,
90                             void *val, size_t val_size)
91 {
92         struct regmap_mmio_context *ctx = context;
93         u32 offset;
94         int ret;
95
96         BUG_ON(reg_size != 4);
97
98         if (!IS_ERR(ctx->clk)) {
99                 ret = clk_enable(ctx->clk);
100                 if (ret < 0)
101                         return ret;
102         }
103
104         offset = *(u32 *)reg;
105
106         while (val_size) {
107                 switch (ctx->val_bytes) {
108                 case 1:
109                         *(u8 *)val = readb(ctx->regs + offset);
110                         break;
111                 case 2:
112                         *(u16 *)val = readw(ctx->regs + offset);
113                         break;
114                 case 4:
115                         *(u32 *)val = readl(ctx->regs + offset);
116                         break;
117 #ifdef CONFIG_64BIT
118                 case 8:
119                         *(u64 *)val = readq(ctx->regs + offset);
120                         break;
121 #endif
122                 default:
123                         /* Should be caught by regmap_mmio_check_config */
124                         BUG();
125                 }
126                 val_size -= ctx->val_bytes;
127                 val += ctx->val_bytes;
128                 offset += ctx->val_bytes;
129         }
130
131         if (!IS_ERR(ctx->clk))
132                 clk_disable(ctx->clk);
133
134         return 0;
135 }
136
137 static void regmap_mmio_free_context(void *context)
138 {
139         struct regmap_mmio_context *ctx = context;
140
141         if (!IS_ERR(ctx->clk)) {
142                 clk_unprepare(ctx->clk);
143                 clk_put(ctx->clk);
144         }
145         kfree(context);
146 }
147
148 static struct regmap_bus regmap_mmio = {
149         .fast_io = true,
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,
156 };
157
158 static struct regmap_mmio_context *regmap_mmio_gen_context(struct device *dev,
159                                         const char *clk_id,
160                                         void __iomem *regs,
161                                         const struct regmap_config *config)
162 {
163         struct regmap_mmio_context *ctx;
164         int min_stride;
165         int ret;
166
167         if (config->reg_bits != 32)
168                 return ERR_PTR(-EINVAL);
169
170         if (config->pad_bits)
171                 return ERR_PTR(-EINVAL);
172
173         switch (config->val_bits) {
174         case 8:
175                 /* The core treats 0 as 1 */
176                 min_stride = 0;
177                 break;
178         case 16:
179                 min_stride = 2;
180                 break;
181         case 32:
182                 min_stride = 4;
183                 break;
184 #ifdef CONFIG_64BIT
185         case 64:
186                 min_stride = 8;
187                 break;
188 #endif
189                 break;
190         default:
191                 return ERR_PTR(-EINVAL);
192         }
193
194         if (config->reg_stride < min_stride)
195                 return ERR_PTR(-EINVAL);
196
197         switch (config->reg_format_endian) {
198         case REGMAP_ENDIAN_DEFAULT:
199         case REGMAP_ENDIAN_NATIVE:
200                 break;
201         default:
202                 return ERR_PTR(-EINVAL);
203         }
204
205         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
206         if (!ctx)
207                 return ERR_PTR(-ENOMEM);
208
209         ctx->regs = regs;
210         ctx->val_bytes = config->val_bits / 8;
211         ctx->clk = ERR_PTR(-ENODEV);
212
213         ctx->clk = clk_get(dev, clk_id);
214         if (!IS_ERR(ctx->clk)) {
215                 ret = clk_prepare(ctx->clk);
216                 if (ret < 0) {
217                         clk_put(ctx->clk);
218                         goto err_free;
219                 }
220         } else {
221                 ctx->clk = NULL;
222         }
223
224         return ctx;
225
226 err_free:
227         kfree(ctx);
228
229         return ERR_PTR(ret);
230 }
231
232 /**
233  * regmap_init_mmio_clk(): Initialise register map with register clock
234  *
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
239  *
240  * The return value will be an ERR_PTR() on error or a valid pointer to
241  * a struct regmap.
242  */
243 struct regmap *regmap_init_mmio_clk(struct device *dev, const char *clk_id,
244                                     void __iomem *regs,
245                                     const struct regmap_config *config)
246 {
247         struct regmap_mmio_context *ctx;
248
249         ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
250         if (IS_ERR(ctx))
251                 return ERR_CAST(ctx);
252
253         return regmap_init(dev, &regmap_mmio, ctx, config);
254 }
255 EXPORT_SYMBOL_GPL(regmap_init_mmio_clk);
256
257 /**
258  * devm_regmap_init_mmio_clk(): Initialise managed register map with clock
259  *
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
264  *
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.
268  */
269 struct regmap *devm_regmap_init_mmio_clk(struct device *dev, const char *clk_id,
270                                          void __iomem *regs,
271                                          const struct regmap_config *config)
272 {
273         struct regmap_mmio_context *ctx;
274
275         ctx = regmap_mmio_gen_context(dev, clk_id, regs, config);
276         if (IS_ERR(ctx))
277                 return ERR_CAST(ctx);
278
279         return devm_regmap_init(dev, &regmap_mmio, ctx, config);
280 }
281 EXPORT_SYMBOL_GPL(devm_regmap_init_mmio_clk);
282
283 MODULE_LICENSE("GPL v2");