1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/errno.h>
4 #include <linux/mdio.h>
5 #include <linux/module.h>
6 #include <linux/regmap.h>
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)
13 static int regmap_mdio_read(struct mdio_device *mdio_dev, u32 reg, unsigned int *val)
17 ret = mdiobus_read(mdio_dev->bus, mdio_dev->addr, reg);
21 *val = ret & REGVAL_MASK;
25 static int regmap_mdio_write(struct mdio_device *mdio_dev, u32 reg, unsigned int val)
27 return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg, val);
30 static int regmap_mdio_c22_read(void *context, unsigned int reg, unsigned int *val)
32 struct mdio_device *mdio_dev = context;
34 if (unlikely(reg & ~REGNUM_C22_MASK))
37 return regmap_mdio_read(mdio_dev, reg, val);
40 static int regmap_mdio_c22_write(void *context, unsigned int reg, unsigned int val)
42 struct mdio_device *mdio_dev = context;
44 if (unlikely(reg & ~REGNUM_C22_MASK))
47 return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg, val);
50 static const struct regmap_bus regmap_mdio_c22_bus = {
51 .reg_write = regmap_mdio_c22_write,
52 .reg_read = regmap_mdio_c22_read,
55 static int regmap_mdio_c45_read(void *context, unsigned int reg, unsigned int *val)
57 struct mdio_device *mdio_dev = context;
59 if (unlikely(reg & ~REGNUM_C45_MASK))
62 return regmap_mdio_read(mdio_dev, MII_ADDR_C45 | reg, val);
65 static int regmap_mdio_c45_write(void *context, unsigned int reg, unsigned int val)
67 struct mdio_device *mdio_dev = context;
69 if (unlikely(reg & ~REGNUM_C45_MASK))
72 return regmap_mdio_write(mdio_dev, MII_ADDR_C45 | reg, val);
75 static const struct regmap_bus regmap_mdio_c45_bus = {
76 .reg_write = regmap_mdio_c45_write,
77 .reg_read = regmap_mdio_c45_read,
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)
84 const struct regmap_bus *bus;
86 if (config->reg_bits == 5 && config->val_bits == 16)
87 bus = ®map_mdio_c22_bus;
88 else if (config->reg_bits == 21 && config->val_bits == 16)
89 bus = ®map_mdio_c45_bus;
91 return ERR_PTR(-EOPNOTSUPP);
93 return __regmap_init(&mdio_dev->dev, bus, mdio_dev, config, lock_key, lock_name);
95 EXPORT_SYMBOL_GPL(__regmap_init_mdio);
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)
101 const struct regmap_bus *bus;
103 if (config->reg_bits == 5 && config->val_bits == 16)
104 bus = ®map_mdio_c22_bus;
105 else if (config->reg_bits == 21 && config->val_bits == 16)
106 bus = ®map_mdio_c45_bus;
108 return ERR_PTR(-EOPNOTSUPP);
110 return __devm_regmap_init(&mdio_dev->dev, bus, mdio_dev, config, lock_key, lock_name);
112 EXPORT_SYMBOL_GPL(__devm_regmap_init_mdio);
114 MODULE_AUTHOR("Sander Vanheule <sander@svanheule.net>");
115 MODULE_DESCRIPTION("Regmap MDIO Module");
116 MODULE_LICENSE("GPL v2");