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/err.h>
20 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
26 struct regmap_mmio_context
{
31 static int regmap_mmio_gather_write(void *context
,
32 const void *reg
, size_t reg_size
,
33 const void *val
, size_t val_size
)
35 struct regmap_mmio_context
*ctx
= context
;
38 BUG_ON(reg_size
!= 4);
40 offset
= be32_to_cpup(reg
);
43 switch (ctx
->val_bytes
) {
45 writeb(*(u8
*)val
, ctx
->regs
+ offset
);
48 writew(be16_to_cpup(val
), ctx
->regs
+ offset
);
51 writel(be32_to_cpup(val
), ctx
->regs
+ offset
);
55 writeq(be64_to_cpup(val
), ctx
->regs
+ offset
);
59 /* Should be caught by regmap_mmio_check_config */
62 val_size
-= ctx
->val_bytes
;
63 val
+= ctx
->val_bytes
;
64 offset
+= ctx
->val_bytes
;
70 static int regmap_mmio_write(void *context
, const void *data
, size_t count
)
74 return regmap_mmio_gather_write(context
, data
, 4, data
+ 4, count
- 4);
77 static int regmap_mmio_read(void *context
,
78 const void *reg
, size_t reg_size
,
79 void *val
, size_t val_size
)
81 struct regmap_mmio_context
*ctx
= context
;
84 BUG_ON(reg_size
!= 4);
86 offset
= be32_to_cpup(reg
);
89 switch (ctx
->val_bytes
) {
91 *(u8
*)val
= readb(ctx
->regs
+ offset
);
94 *(u16
*)val
= cpu_to_be16(readw(ctx
->regs
+ offset
));
97 *(u32
*)val
= cpu_to_be32(readl(ctx
->regs
+ offset
));
101 *(u64
*)val
= cpu_to_be32(readq(ctx
->regs
+ offset
));
105 /* Should be caught by regmap_mmio_check_config */
108 val_size
-= ctx
->val_bytes
;
109 val
+= ctx
->val_bytes
;
110 offset
+= ctx
->val_bytes
;
116 static void regmap_mmio_free_context(void *context
)
121 static struct regmap_bus regmap_mmio
= {
123 .write
= regmap_mmio_write
,
124 .gather_write
= regmap_mmio_gather_write
,
125 .read
= regmap_mmio_read
,
126 .free_context
= regmap_mmio_free_context
,
129 struct regmap_mmio_context
*regmap_mmio_gen_context(void __iomem
*regs
,
130 const struct regmap_config
*config
)
132 struct regmap_mmio_context
*ctx
;
135 if (config
->reg_bits
!= 32)
136 return ERR_PTR(-EINVAL
);
138 if (config
->pad_bits
)
139 return ERR_PTR(-EINVAL
);
141 switch (config
->val_bits
) {
143 /* The core treats 0 as 1 */
159 return ERR_PTR(-EINVAL
);
162 if (config
->reg_stride
< min_stride
)
163 return ERR_PTR(-EINVAL
);
165 ctx
= kzalloc(GFP_KERNEL
, sizeof(*ctx
));
167 return ERR_PTR(-ENOMEM
);
170 ctx
->val_bytes
= config
->val_bits
/ 8;
176 * regmap_init_mmio(): Initialise register map
178 * @dev: Device that will be interacted with
179 * @regs: Pointer to memory-mapped IO region
180 * @config: Configuration for register map
182 * The return value will be an ERR_PTR() on error or a valid pointer to
185 struct regmap
*regmap_init_mmio(struct device
*dev
,
187 const struct regmap_config
*config
)
189 struct regmap_mmio_context
*ctx
;
191 ctx
= regmap_mmio_gen_context(regs
, config
);
193 return ERR_CAST(ctx
);
195 return regmap_init(dev
, ®map_mmio
, ctx
, config
);
197 EXPORT_SYMBOL_GPL(regmap_init_mmio
);
200 * devm_regmap_init_mmio(): Initialise managed register map
202 * @dev: Device that will be interacted with
203 * @regs: Pointer to memory-mapped IO region
204 * @config: Configuration for register map
206 * The return value will be an ERR_PTR() on error or a valid pointer
207 * to a struct regmap. The regmap will be automatically freed by the
208 * device management code.
210 struct regmap
*devm_regmap_init_mmio(struct device
*dev
,
212 const struct regmap_config
*config
)
214 struct regmap_mmio_context
*ctx
;
216 ctx
= regmap_mmio_gen_context(regs
, config
);
218 return ERR_CAST(ctx
);
220 return devm_regmap_init(dev
, ®map_mmio
, ctx
, config
);
222 EXPORT_SYMBOL_GPL(devm_regmap_init_mmio
);
224 MODULE_LICENSE("GPL v2");
This page took 0.037917 seconds and 5 git commands to generate.