2 * Register map access API - SPMI support
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
6 * Based on regmap-i2c.c:
7 * Copyright 2011 Wolfson Microelectronics plc
8 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 and
12 * only version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/regmap.h>
21 #include <linux/spmi.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
25 static int regmap_spmi_base_read(void *context
,
26 const void *reg
, size_t reg_size
,
27 void *val
, size_t val_size
)
32 BUG_ON(reg_size
!= 1);
34 while (val_size
-- && !err
)
35 err
= spmi_register_read(context
, addr
++, val
++);
40 static int regmap_spmi_base_gather_write(void *context
,
41 const void *reg
, size_t reg_size
,
42 const void *val
, size_t val_size
)
48 BUG_ON(reg_size
!= 1);
51 * SPMI defines a more bandwidth-efficient 'Register 0 Write' sequence,
52 * use it when possible.
54 if (addr
== 0 && val_size
) {
55 err
= spmi_register_zero_write(context
, *data
);
65 err
= spmi_register_write(context
, addr
, *data
);
78 static int regmap_spmi_base_write(void *context
, const void *data
,
82 return regmap_spmi_base_gather_write(context
, data
, 1, data
+ 1,
86 static struct regmap_bus regmap_spmi_base
= {
87 .read
= regmap_spmi_base_read
,
88 .write
= regmap_spmi_base_write
,
89 .gather_write
= regmap_spmi_base_gather_write
,
90 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
91 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
95 * regmap_init_spmi_base(): Create regmap for the Base register space
96 * @sdev: SPMI device that will be interacted with
97 * @config: Configuration for register map
99 * The return value will be an ERR_PTR() on error or a valid pointer to
102 struct regmap
*regmap_init_spmi_base(struct spmi_device
*sdev
,
103 const struct regmap_config
*config
)
105 return regmap_init(&sdev
->dev
, ®map_spmi_base
, sdev
, config
);
107 EXPORT_SYMBOL_GPL(regmap_init_spmi_base
);
110 * devm_regmap_init_spmi_base(): Create managed regmap for Base register space
111 * @sdev: SPMI device that will be interacted with
112 * @config: Configuration for register map
114 * The return value will be an ERR_PTR() on error or a valid pointer
115 * to a struct regmap. The regmap will be automatically freed by the
116 * device management code.
118 struct regmap
*devm_regmap_init_spmi_base(struct spmi_device
*sdev
,
119 const struct regmap_config
*config
)
121 return devm_regmap_init(&sdev
->dev
, ®map_spmi_base
, sdev
, config
);
123 EXPORT_SYMBOL_GPL(devm_regmap_init_spmi_base
);
125 static int regmap_spmi_ext_read(void *context
,
126 const void *reg
, size_t reg_size
,
127 void *val
, size_t val_size
)
133 BUG_ON(reg_size
!= 2);
138 * Split accesses into two to take advantage of the more
139 * bandwidth-efficient 'Extended Register Read' command when possible
141 while (addr
<= 0xFF && val_size
) {
142 len
= min_t(size_t, val_size
, 16);
144 err
= spmi_ext_register_read(context
, addr
, val
, len
);
154 len
= min_t(size_t, val_size
, 8);
156 err
= spmi_ext_register_readl(context
, addr
, val
, val_size
);
169 static int regmap_spmi_ext_gather_write(void *context
,
170 const void *reg
, size_t reg_size
,
171 const void *val
, size_t val_size
)
177 BUG_ON(reg_size
!= 2);
181 while (addr
<= 0xFF && val_size
) {
182 len
= min_t(size_t, val_size
, 16);
184 err
= spmi_ext_register_write(context
, addr
, val
, len
);
194 len
= min_t(size_t, val_size
, 8);
196 err
= spmi_ext_register_writel(context
, addr
, val
, len
);
209 static int regmap_spmi_ext_write(void *context
, const void *data
,
213 return regmap_spmi_ext_gather_write(context
, data
, 2, data
+ 2,
217 static struct regmap_bus regmap_spmi_ext
= {
218 .read
= regmap_spmi_ext_read
,
219 .write
= regmap_spmi_ext_write
,
220 .gather_write
= regmap_spmi_ext_gather_write
,
221 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
222 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
226 * regmap_init_spmi_ext(): Create regmap for Ext register space
227 * @sdev: Device that will be interacted with
228 * @config: Configuration for register map
230 * The return value will be an ERR_PTR() on error or a valid pointer to
233 struct regmap
*regmap_init_spmi_ext(struct spmi_device
*sdev
,
234 const struct regmap_config
*config
)
236 return regmap_init(&sdev
->dev
, ®map_spmi_ext
, sdev
, config
);
238 EXPORT_SYMBOL_GPL(regmap_init_spmi_ext
);
241 * devm_regmap_init_spmi_ext(): Create managed regmap for Ext register space
242 * @sdev: SPMI device that will be interacted with
243 * @config: Configuration for register map
245 * The return value will be an ERR_PTR() on error or a valid pointer
246 * to a struct regmap. The regmap will be automatically freed by the
247 * device management code.
249 struct regmap
*devm_regmap_init_spmi_ext(struct spmi_device
*sdev
,
250 const struct regmap_config
*config
)
252 return devm_regmap_init(&sdev
->dev
, ®map_spmi_ext
, sdev
, config
);
254 EXPORT_SYMBOL_GPL(devm_regmap_init_spmi_ext
);
256 MODULE_LICENSE("GPL");