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_c22_read(void *context
, unsigned int reg
, unsigned int *val
)
15 struct mdio_device
*mdio_dev
= context
;
18 if (unlikely(reg
& ~REGNUM_C22_MASK
))
21 ret
= mdiodev_read(mdio_dev
, reg
);
25 *val
= ret
& REGVAL_MASK
;
30 static int regmap_mdio_c22_write(void *context
, unsigned int reg
, unsigned int val
)
32 struct mdio_device
*mdio_dev
= context
;
34 if (unlikely(reg
& ~REGNUM_C22_MASK
))
37 return mdiodev_write(mdio_dev
, reg
, val
);
40 static const struct regmap_bus regmap_mdio_c22_bus
= {
41 .reg_write
= regmap_mdio_c22_write
,
42 .reg_read
= regmap_mdio_c22_read
,
45 static int regmap_mdio_c45_read(void *context
, unsigned int reg
, unsigned int *val
)
47 struct mdio_device
*mdio_dev
= context
;
51 if (unlikely(reg
& ~REGNUM_C45_MASK
))
54 devad
= reg
>> REGMAP_MDIO_C45_DEVAD_SHIFT
;
55 reg
= reg
& REGMAP_MDIO_C45_REGNUM_MASK
;
57 ret
= mdiodev_c45_read(mdio_dev
, devad
, reg
);
61 *val
= ret
& REGVAL_MASK
;
66 static int regmap_mdio_c45_write(void *context
, unsigned int reg
, unsigned int val
)
68 struct mdio_device
*mdio_dev
= context
;
71 if (unlikely(reg
& ~REGNUM_C45_MASK
))
74 devad
= reg
>> REGMAP_MDIO_C45_DEVAD_SHIFT
;
75 reg
= reg
& REGMAP_MDIO_C45_REGNUM_MASK
;
77 return mdiodev_c45_write(mdio_dev
, devad
, reg
, val
);
80 static const struct regmap_bus regmap_mdio_c45_bus
= {
81 .reg_write
= regmap_mdio_c45_write
,
82 .reg_read
= regmap_mdio_c45_read
,
85 struct regmap
*__regmap_init_mdio(struct mdio_device
*mdio_dev
,
86 const struct regmap_config
*config
, struct lock_class_key
*lock_key
,
87 const char *lock_name
)
89 const struct regmap_bus
*bus
;
91 if (config
->reg_bits
== 5 && config
->val_bits
== 16)
92 bus
= ®map_mdio_c22_bus
;
93 else if (config
->reg_bits
== 21 && config
->val_bits
== 16)
94 bus
= ®map_mdio_c45_bus
;
96 return ERR_PTR(-EOPNOTSUPP
);
98 return __regmap_init(&mdio_dev
->dev
, bus
, mdio_dev
, config
, lock_key
, lock_name
);
100 EXPORT_SYMBOL_GPL(__regmap_init_mdio
);
102 struct regmap
*__devm_regmap_init_mdio(struct mdio_device
*mdio_dev
,
103 const struct regmap_config
*config
, struct lock_class_key
*lock_key
,
104 const char *lock_name
)
106 const struct regmap_bus
*bus
;
108 if (config
->reg_bits
== 5 && config
->val_bits
== 16)
109 bus
= ®map_mdio_c22_bus
;
110 else if (config
->reg_bits
== 21 && config
->val_bits
== 16)
111 bus
= ®map_mdio_c45_bus
;
113 return ERR_PTR(-EOPNOTSUPP
);
115 return __devm_regmap_init(&mdio_dev
->dev
, bus
, mdio_dev
, config
, lock_key
, lock_name
);
117 EXPORT_SYMBOL_GPL(__devm_regmap_init_mdio
);
119 MODULE_AUTHOR("Sander Vanheule <sander@svanheule.net>");
120 MODULE_DESCRIPTION("regmap MDIO Module");
121 MODULE_LICENSE("GPL v2");