1 // SPDX-License-Identifier: GPL-2.0
3 // Register map access API - FSI support
5 // Copyright 2022 IBM Corp
7 // Author: Eddie James <eajames@linux.ibm.com>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
15 static int regmap_fsi32_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
20 ret
= fsi_slave_read(context
, reg
, &v
, sizeof(v
));
28 static int regmap_fsi32_reg_write(void *context
, unsigned int reg
, unsigned int val
)
32 return fsi_slave_write(context
, reg
, &v
, sizeof(v
));
35 static const struct regmap_bus regmap_fsi32
= {
36 .reg_write
= regmap_fsi32_reg_write
,
37 .reg_read
= regmap_fsi32_reg_read
,
40 static int regmap_fsi32le_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
45 ret
= fsi_slave_read(context
, reg
, &v
, sizeof(v
));
49 *val
= be32_to_cpu(v
);
53 static int regmap_fsi32le_reg_write(void *context
, unsigned int reg
, unsigned int val
)
55 __be32 v
= cpu_to_be32(val
);
57 return fsi_slave_write(context
, reg
, &v
, sizeof(v
));
60 static const struct regmap_bus regmap_fsi32le
= {
61 .reg_write
= regmap_fsi32le_reg_write
,
62 .reg_read
= regmap_fsi32le_reg_read
,
65 static int regmap_fsi16_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
70 ret
= fsi_slave_read(context
, reg
, &v
, sizeof(v
));
78 static int regmap_fsi16_reg_write(void *context
, unsigned int reg
, unsigned int val
)
86 return fsi_slave_write(context
, reg
, &v
, sizeof(v
));
89 static const struct regmap_bus regmap_fsi16
= {
90 .reg_write
= regmap_fsi16_reg_write
,
91 .reg_read
= regmap_fsi16_reg_read
,
94 static int regmap_fsi16le_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
99 ret
= fsi_slave_read(context
, reg
, &v
, sizeof(v
));
103 *val
= be16_to_cpu(v
);
107 static int regmap_fsi16le_reg_write(void *context
, unsigned int reg
, unsigned int val
)
114 v
= cpu_to_be16(val
);
115 return fsi_slave_write(context
, reg
, &v
, sizeof(v
));
118 static const struct regmap_bus regmap_fsi16le
= {
119 .reg_write
= regmap_fsi16le_reg_write
,
120 .reg_read
= regmap_fsi16le_reg_read
,
123 static int regmap_fsi8_reg_read(void *context
, unsigned int reg
, unsigned int *val
)
128 ret
= fsi_slave_read(context
, reg
, &v
, sizeof(v
));
136 static int regmap_fsi8_reg_write(void *context
, unsigned int reg
, unsigned int val
)
144 return fsi_slave_write(context
, reg
, &v
, sizeof(v
));
147 static const struct regmap_bus regmap_fsi8
= {
148 .reg_write
= regmap_fsi8_reg_write
,
149 .reg_read
= regmap_fsi8_reg_read
,
152 static const struct regmap_bus
*regmap_get_fsi_bus(struct fsi_device
*fsi_dev
,
153 const struct regmap_config
*config
)
155 const struct regmap_bus
*bus
= NULL
;
157 if (config
->reg_bits
== 8 || config
->reg_bits
== 16 || config
->reg_bits
== 32) {
158 switch (config
->val_bits
) {
163 switch (regmap_get_val_endian(&fsi_dev
->dev
, NULL
, config
)) {
164 case REGMAP_ENDIAN_LITTLE
:
165 #ifdef __LITTLE_ENDIAN
166 case REGMAP_ENDIAN_NATIVE
:
168 bus
= ®map_fsi16le
;
170 case REGMAP_ENDIAN_DEFAULT
:
171 case REGMAP_ENDIAN_BIG
:
173 case REGMAP_ENDIAN_NATIVE
:
182 switch (regmap_get_val_endian(&fsi_dev
->dev
, NULL
, config
)) {
183 case REGMAP_ENDIAN_LITTLE
:
184 #ifdef __LITTLE_ENDIAN
185 case REGMAP_ENDIAN_NATIVE
:
187 bus
= ®map_fsi32le
;
189 case REGMAP_ENDIAN_DEFAULT
:
190 case REGMAP_ENDIAN_BIG
:
192 case REGMAP_ENDIAN_NATIVE
:
203 return bus
?: ERR_PTR(-EOPNOTSUPP
);
206 struct regmap
*__regmap_init_fsi(struct fsi_device
*fsi_dev
, const struct regmap_config
*config
,
207 struct lock_class_key
*lock_key
, const char *lock_name
)
209 const struct regmap_bus
*bus
= regmap_get_fsi_bus(fsi_dev
, config
);
212 return ERR_CAST(bus
);
214 return __regmap_init(&fsi_dev
->dev
, bus
, fsi_dev
->slave
, config
, lock_key
, lock_name
);
216 EXPORT_SYMBOL_GPL(__regmap_init_fsi
);
218 struct regmap
*__devm_regmap_init_fsi(struct fsi_device
*fsi_dev
,
219 const struct regmap_config
*config
,
220 struct lock_class_key
*lock_key
, const char *lock_name
)
222 const struct regmap_bus
*bus
= regmap_get_fsi_bus(fsi_dev
, config
);
225 return ERR_CAST(bus
);
227 return __devm_regmap_init(&fsi_dev
->dev
, bus
, fsi_dev
->slave
, config
, lock_key
, lock_name
);
229 EXPORT_SYMBOL_GPL(__devm_regmap_init_fsi
);
231 MODULE_LICENSE("GPL");