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 
10 static int regmap_slimbus_byte_reg_read(void *context, unsigned int reg,
11 					unsigned int *val)
12 {
13 	struct slim_device *sdev = context;
14 	int v;
15 
16 	v = slim_readb(sdev, reg);
17 
18 	if (v < 0)
19 		return v;
20 
21 	*val = v;
22 
23 	return 0;
24 }
25 
26 static int regmap_slimbus_byte_reg_write(void *context, unsigned int reg,
27 					 unsigned int val)
28 {
29 	struct slim_device *sdev = context;
30 
31 	return slim_writeb(sdev, reg, val);
32 }
33 
34 static struct regmap_bus regmap_slimbus_bus = {
35 	.reg_write = regmap_slimbus_byte_reg_write,
36 	.reg_read = regmap_slimbus_byte_reg_read,
37 	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
38 	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
39 };
40 
41 static const struct regmap_bus *regmap_get_slimbus(struct slim_device *slim,
42 					const struct regmap_config *config)
43 {
44 	if (config->val_bits == 8 && config->reg_bits == 8)
45 		return &regmap_slimbus_bus;
46 
47 	return ERR_PTR(-ENOTSUPP);
48 }
49 
50 struct regmap *__regmap_init_slimbus(struct slim_device *slimbus,
51 				     const struct regmap_config *config,
52 				     struct lock_class_key *lock_key,
53 				     const char *lock_name)
54 {
55 	const struct regmap_bus *bus = regmap_get_slimbus(slimbus, config);
56 
57 	if (IS_ERR(bus))
58 		return ERR_CAST(bus);
59 
60 	return __regmap_init(&slimbus->dev, bus, &slimbus->dev, config,
61 			     lock_key, lock_name);
62 }
63 EXPORT_SYMBOL_GPL(__regmap_init_slimbus);
64 
65 struct regmap *__devm_regmap_init_slimbus(struct slim_device *slimbus,
66 					  const struct regmap_config *config,
67 					  struct lock_class_key *lock_key,
68 					  const char *lock_name)
69 {
70 	const struct regmap_bus *bus = regmap_get_slimbus(slimbus, config);
71 
72 	if (IS_ERR(bus))
73 		return ERR_CAST(bus);
74 
75 	return __devm_regmap_init(&slimbus->dev, bus, &slimbus, config,
76 				  lock_key, lock_name);
77 }
78 EXPORT_SYMBOL_GPL(__devm_regmap_init_slimbus);
79 
80 MODULE_LICENSE("GPL v2");
81