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 const 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
,
94 struct regmap
*__regmap_init_spmi_base(struct spmi_device
*sdev
,
95 const struct regmap_config
*config
,
96 struct lock_class_key
*lock_key
,
97 const char *lock_name
)
99 return __regmap_init(&sdev
->dev
, ®map_spmi_base
, sdev
, config
,
100 lock_key
, lock_name
);
102 EXPORT_SYMBOL_GPL(__regmap_init_spmi_base
);
104 struct regmap
*__devm_regmap_init_spmi_base(struct spmi_device
*sdev
,
105 const struct regmap_config
*config
,
106 struct lock_class_key
*lock_key
,
107 const char *lock_name
)
109 return __devm_regmap_init(&sdev
->dev
, ®map_spmi_base
, sdev
, config
,
110 lock_key
, lock_name
);
112 EXPORT_SYMBOL_GPL(__devm_regmap_init_spmi_base
);
114 static int regmap_spmi_ext_read(void *context
,
115 const void *reg
, size_t reg_size
,
116 void *val
, size_t val_size
)
122 BUG_ON(reg_size
!= 2);
127 * Split accesses into two to take advantage of the more
128 * bandwidth-efficient 'Extended Register Read' command when possible
130 while (addr
<= 0xFF && val_size
) {
131 len
= min_t(size_t, val_size
, 16);
133 err
= spmi_ext_register_read(context
, addr
, val
, len
);
143 len
= min_t(size_t, val_size
, 8);
145 err
= spmi_ext_register_readl(context
, addr
, val
, len
);
158 static int regmap_spmi_ext_gather_write(void *context
,
159 const void *reg
, size_t reg_size
,
160 const void *val
, size_t val_size
)
166 BUG_ON(reg_size
!= 2);
170 while (addr
<= 0xFF && val_size
) {
171 len
= min_t(size_t, val_size
, 16);
173 err
= spmi_ext_register_write(context
, addr
, val
, len
);
183 len
= min_t(size_t, val_size
, 8);
185 err
= spmi_ext_register_writel(context
, addr
, val
, len
);
198 static int regmap_spmi_ext_write(void *context
, const void *data
,
202 return regmap_spmi_ext_gather_write(context
, data
, 2, data
+ 2,
206 static const struct regmap_bus regmap_spmi_ext
= {
207 .read
= regmap_spmi_ext_read
,
208 .write
= regmap_spmi_ext_write
,
209 .gather_write
= regmap_spmi_ext_gather_write
,
210 .reg_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
211 .val_format_endian_default
= REGMAP_ENDIAN_NATIVE
,
214 struct regmap
*__regmap_init_spmi_ext(struct spmi_device
*sdev
,
215 const struct regmap_config
*config
,
216 struct lock_class_key
*lock_key
,
217 const char *lock_name
)
219 return __regmap_init(&sdev
->dev
, ®map_spmi_ext
, sdev
, config
,
220 lock_key
, lock_name
);
222 EXPORT_SYMBOL_GPL(__regmap_init_spmi_ext
);
224 struct regmap
*__devm_regmap_init_spmi_ext(struct spmi_device
*sdev
,
225 const struct regmap_config
*config
,
226 struct lock_class_key
*lock_key
,
227 const char *lock_name
)
229 return __devm_regmap_init(&sdev
->dev
, ®map_spmi_ext
, sdev
, config
,
230 lock_key
, lock_name
);
232 EXPORT_SYMBOL_GPL(__devm_regmap_init_spmi_ext
);
234 MODULE_LICENSE("GPL");