1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017, Linaro Ltd.
3 
4 #include <linux/regmap.h>
5 #include <linux/slimbus.h>
6 #include <linux/module.h>
7 
8 #include "internal.h"
9 
regmap_slimbus_write(void * context,const void * data,size_t count)10 static int regmap_slimbus_write(void *context, const void *data, size_t count)
11 {
12 	struct slim_device *sdev = context;
13 
14 	return slim_write(sdev, *(u16 *)data, count - 2, (u8 *)data + 2);
15 }
16 
regmap_slimbus_read(void * context,const void * reg,size_t reg_size,void * val,size_t val_size)17 static int regmap_slimbus_read(void *context, const void *reg, size_t reg_size,
18 			       void *val, size_t val_size)
19 {
20 	struct slim_device *sdev = context;
21 
22 	return slim_read(sdev, *(u16 *)reg, val_size, val);
23 }
24 
25 static struct regmap_bus regmap_slimbus_bus = {
26 	.write = regmap_slimbus_write,
27 	.read = regmap_slimbus_read,
28 	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
29 	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
30 };
31 
regmap_get_slimbus(struct slim_device * slim,const struct regmap_config * config)32 static const struct regmap_bus *regmap_get_slimbus(struct slim_device *slim,
33 					const struct regmap_config *config)
34 {
35 	if (config->val_bits == 8 && config->reg_bits == 16)
36 		return &regmap_slimbus_bus;
37 
38 	return ERR_PTR(-ENOTSUPP);
39 }
40 
__regmap_init_slimbus(struct slim_device * slimbus,const struct regmap_config * config,struct lock_class_key * lock_key,const char * lock_name)41 struct regmap *__regmap_init_slimbus(struct slim_device *slimbus,
42 				     const struct regmap_config *config,
43 				     struct lock_class_key *lock_key,
44 				     const char *lock_name)
45 {
46 	const struct regmap_bus *bus = regmap_get_slimbus(slimbus, config);
47 
48 	if (IS_ERR(bus))
49 		return ERR_CAST(bus);
50 
51 	return __regmap_init(&slimbus->dev, bus, &slimbus->dev, config,
52 			     lock_key, lock_name);
53 }
54 EXPORT_SYMBOL_GPL(__regmap_init_slimbus);
55 
__devm_regmap_init_slimbus(struct slim_device * slimbus,const struct regmap_config * config,struct lock_class_key * lock_key,const char * lock_name)56 struct regmap *__devm_regmap_init_slimbus(struct slim_device *slimbus,
57 					  const struct regmap_config *config,
58 					  struct lock_class_key *lock_key,
59 					  const char *lock_name)
60 {
61 	const struct regmap_bus *bus = regmap_get_slimbus(slimbus, config);
62 
63 	if (IS_ERR(bus))
64 		return ERR_CAST(bus);
65 
66 	return __devm_regmap_init(&slimbus->dev, bus, &slimbus, config,
67 				  lock_key, lock_name);
68 }
69 EXPORT_SYMBOL_GPL(__devm_regmap_init_slimbus);
70 
71 MODULE_LICENSE("GPL v2");
72