1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/errno.h>
4 #include <linux/mdio.h>
5 #include <linux/module.h>
6 #include <linux/regmap.h>
7 
8 #define REGVAL_MASK		GENMASK(15, 0)
9 #define REGNUM_C22_MASK		GENMASK(4, 0)
10 /* Clause-45 mask includes the device type (5 bit) and actual register number (16 bit) */
11 #define REGNUM_C45_MASK		GENMASK(20, 0)
12 
13 static int regmap_mdio_read(struct mdio_device *mdio_dev, u32 reg, unsigned int *val)
14 {
15 	int ret;
16 
17 	ret = mdiobus_read(mdio_dev->bus, mdio_dev->addr, reg);
18 	if (ret < 0)
19 		return ret;
20 
21 	*val = ret & REGVAL_MASK;
22 	return 0;
23 }
24 
25 static int regmap_mdio_write(struct mdio_device *mdio_dev, u32 reg, unsigned int val)
26 {
27 	return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg, val);
28 }
29 
30 static int regmap_mdio_c22_read(void *context, unsigned int reg, unsigned int *val)
31 {
32 	struct mdio_device *mdio_dev = context;
33 
34 	if (unlikely(reg & ~REGNUM_C22_MASK))
35 		return -ENXIO;
36 
37 	return regmap_mdio_read(mdio_dev, reg, val);
38 }
39 
40 static int regmap_mdio_c22_write(void *context, unsigned int reg, unsigned int val)
41 {
42 	struct mdio_device *mdio_dev = context;
43 
44 	if (unlikely(reg & ~REGNUM_C22_MASK))
45 		return -ENXIO;
46 
47 	return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg, val);
48 }
49 
50 static const struct regmap_bus regmap_mdio_c22_bus = {
51 	.reg_write = regmap_mdio_c22_write,
52 	.reg_read = regmap_mdio_c22_read,
53 };
54 
55 static int regmap_mdio_c45_read(void *context, unsigned int reg, unsigned int *val)
56 {
57 	struct mdio_device *mdio_dev = context;
58 
59 	if (unlikely(reg & ~REGNUM_C45_MASK))
60 		return -ENXIO;
61 
62 	return regmap_mdio_read(mdio_dev, MII_ADDR_C45 | reg, val);
63 }
64 
65 static int regmap_mdio_c45_write(void *context, unsigned int reg, unsigned int val)
66 {
67 	struct mdio_device *mdio_dev = context;
68 
69 	if (unlikely(reg & ~REGNUM_C45_MASK))
70 		return -ENXIO;
71 
72 	return regmap_mdio_write(mdio_dev, MII_ADDR_C45 | reg, val);
73 }
74 
75 static const struct regmap_bus regmap_mdio_c45_bus = {
76 	.reg_write = regmap_mdio_c45_write,
77 	.reg_read = regmap_mdio_c45_read,
78 };
79 
80 struct regmap *__regmap_init_mdio(struct mdio_device *mdio_dev,
81 	const struct regmap_config *config, struct lock_class_key *lock_key,
82 	const char *lock_name)
83 {
84 	const struct regmap_bus *bus;
85 
86 	if (config->reg_bits == 5 && config->val_bits == 16)
87 		bus = &regmap_mdio_c22_bus;
88 	else if (config->reg_bits == 21 && config->val_bits == 16)
89 		bus = &regmap_mdio_c45_bus;
90 	else
91 		return ERR_PTR(-EOPNOTSUPP);
92 
93 	return __regmap_init(&mdio_dev->dev, bus, mdio_dev, config, lock_key, lock_name);
94 }
95 EXPORT_SYMBOL_GPL(__regmap_init_mdio);
96 
97 struct regmap *__devm_regmap_init_mdio(struct mdio_device *mdio_dev,
98 	const struct regmap_config *config, struct lock_class_key *lock_key,
99 	const char *lock_name)
100 {
101 	const struct regmap_bus *bus;
102 
103 	if (config->reg_bits == 5 && config->val_bits == 16)
104 		bus = &regmap_mdio_c22_bus;
105 	else if (config->reg_bits == 21 && config->val_bits == 16)
106 		bus = &regmap_mdio_c45_bus;
107 	else
108 		return ERR_PTR(-EOPNOTSUPP);
109 
110 	return __devm_regmap_init(&mdio_dev->dev, bus, mdio_dev, config, lock_key, lock_name);
111 }
112 EXPORT_SYMBOL_GPL(__devm_regmap_init_mdio);
113 
114 MODULE_AUTHOR("Sander Vanheule <sander@svanheule.net>");
115 MODULE_DESCRIPTION("Regmap MDIO Module");
116 MODULE_LICENSE("GPL v2");
117