1 From a08c045580e060a6886bbb656c50ae20b0c780b5 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Mon, 3 Apr 2023 02:17:52 +0100
4 Subject: [PATCH 07/16] net: dsa: mt7530: use regmap to access switch register
7 Use regmap API to access the switch register space.
9 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
10 Signed-off-by: David S. Miller <davem@davemloft.net>
12 drivers/net/dsa/mt7530.c | 99 ++++++++++++++++++++++++----------------
13 drivers/net/dsa/mt7530.h | 2 +
14 2 files changed, 62 insertions(+), 39 deletions(-)
16 --- a/drivers/net/dsa/mt7530.c
17 +++ b/drivers/net/dsa/mt7530.c
18 @@ -184,9 +184,9 @@ core_clear(struct mt7530_priv *priv, u32
22 -mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
23 +mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
25 - struct mii_bus *bus = priv->bus;
26 + struct mii_bus *bus = context;
30 @@ -198,24 +198,34 @@ mt7530_mii_write(struct mt7530_priv *pri
31 /* MT7530 uses 31 as the pseudo port */
32 ret = bus->write(bus, 0x1f, 0x1f, page);
37 ret = bus->write(bus, 0x1f, r, lo);
42 ret = bus->write(bus, 0x1f, 0x10, hi);
48 +mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
52 + ret = regmap_write(priv->regmap, reg, val);
57 "failed to write mt7530 register\n");
63 -mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
65 +mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
67 - struct mii_bus *bus = priv->bus;
68 + struct mii_bus *bus = context;
72 @@ -224,17 +234,32 @@ mt7530_mii_read(struct mt7530_priv *priv
74 /* MT7530 uses 31 as the pseudo port */
75 ret = bus->write(bus, 0x1f, 0x1f, page);
80 + lo = bus->read(bus, 0x1f, r);
81 + hi = bus->read(bus, 0x1f, 0x10);
83 + *val = (hi << 16) | (lo & 0xffff);
89 +mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
94 + ret = regmap_read(priv->regmap, reg, &val);
99 "failed to read mt7530 register\n");
103 - lo = bus->read(bus, 0x1f, r);
104 - hi = bus->read(bus, 0x1f, 0x10);
106 - return (hi << 16) | (lo & 0xffff);
111 @@ -284,14 +309,10 @@ mt7530_rmw(struct mt7530_priv *priv, u32
114 struct mii_bus *bus = priv->bus;
117 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
119 - val = mt7530_mii_read(priv, reg);
122 - mt7530_mii_write(priv, reg, val);
123 + regmap_update_bits(priv->regmap, reg, mask, set);
125 mutex_unlock(&bus->mdio_lock);
127 @@ -299,7 +320,7 @@ mt7530_rmw(struct mt7530_priv *priv, u32
129 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
131 - mt7530_rmw(priv, reg, 0, val);
132 + mt7530_rmw(priv, reg, val, val);
136 @@ -3126,22 +3147,6 @@ static const struct phylink_pcs_ops mt75
137 .pcs_an_restart = mt7530_pcs_an_restart,
140 -static int mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
142 - struct mt7530_priv *priv = context;
144 - *val = mt7530_mii_read(priv, reg);
148 -static int mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
150 - struct mt7530_priv *priv = context;
152 - mt7530_mii_write(priv, reg, val);
157 mt7530_mdio_regmap_lock(void *mdio_lock)
159 @@ -3154,7 +3159,7 @@ mt7530_mdio_regmap_unlock(void *mdio_loc
160 mutex_unlock(mdio_lock);
163 -static const struct regmap_bus mt7531_regmap_bus = {
164 +static const struct regmap_bus mt7530_regmap_bus = {
165 .reg_write = mt7530_regmap_write,
166 .reg_read = mt7530_regmap_read,
168 @@ -3187,7 +3192,7 @@ mt7531_create_sgmii(struct mt7530_priv *
169 mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
171 regmap = devm_regmap_init(priv->dev,
172 - &mt7531_regmap_bus, priv,
173 + &mt7530_regmap_bus, priv->bus,
174 mt7531_pcs_config[i]);
175 if (IS_ERR(regmap)) {
176 ret = PTR_ERR(regmap);
177 @@ -3352,6 +3357,7 @@ MODULE_DEVICE_TABLE(of, mt7530_of_match)
179 mt7530_probe(struct mdio_device *mdiodev)
181 + static struct regmap_config *regmap_config;
182 struct mt7530_priv *priv;
183 struct device_node *dn;
185 @@ -3431,6 +3437,21 @@ mt7530_probe(struct mdio_device *mdiodev
186 mutex_init(&priv->reg_mutex);
187 dev_set_drvdata(&mdiodev->dev, priv);
189 + regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
191 + if (!regmap_config)
194 + regmap_config->reg_bits = 16;
195 + regmap_config->val_bits = 32;
196 + regmap_config->reg_stride = 4;
197 + regmap_config->max_register = MT7530_CREV;
198 + regmap_config->disable_locking = true;
199 + priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus,
200 + priv->bus, regmap_config);
201 + if (IS_ERR(priv->regmap))
202 + return PTR_ERR(priv->regmap);
204 return dsa_register_switch(priv->ds);
207 --- a/drivers/net/dsa/mt7530.h
208 +++ b/drivers/net/dsa/mt7530.h
209 @@ -779,6 +779,7 @@ struct mt753x_info {
210 * @dev: The device pointer
211 * @ds: The pointer to the dsa core structure
212 * @bus: The bus used for the device and built-in PHY
213 + * @regmap: The regmap instance representing all switch registers
214 * @rstc: The pointer to reset control used by MCM
215 * @core_pwr: The power supplied into the core
216 * @io_pwr: The power supplied into the I/O
217 @@ -799,6 +800,7 @@ struct mt7530_priv {
219 struct dsa_switch *ds;
221 + struct regmap *regmap;
222 struct reset_control *rstc;
223 struct regulator *core_pwr;
224 struct regulator *io_pwr;