48854fd23444fb8f755f2106d6fde6acac90e8ef
[openwrt/staging/ansuel.git] /
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
5 space
6
7 Use regmap API to access the switch register space.
8
9 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
10 Signed-off-by: David S. Miller <davem@davemloft.net>
11 ---
12 drivers/net/dsa/mt7530.c | 99 ++++++++++++++++++++++++----------------
13 drivers/net/dsa/mt7530.h | 2 +
14 2 files changed, 62 insertions(+), 39 deletions(-)
15
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
19 }
20
21 static int
22 -mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
23 +mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
24 {
25 - struct mii_bus *bus = priv->bus;
26 + struct mii_bus *bus = context;
27 u16 page, r, lo, hi;
28 int ret;
29
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);
33 if (ret < 0)
34 - goto err;
35 + return ret;
36
37 ret = bus->write(bus, 0x1f, r, lo);
38 if (ret < 0)
39 - goto err;
40 + return ret;
41
42 ret = bus->write(bus, 0x1f, 0x10, hi);
43 -err:
44 + return ret;
45 +}
46 +
47 +static int
48 +mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
49 +{
50 + int ret;
51 +
52 + ret = regmap_write(priv->regmap, reg, val);
53 +
54 if (ret < 0)
55 - dev_err(&bus->dev,
56 + dev_err(priv->dev,
57 "failed to write mt7530 register\n");
58 +
59 return ret;
60 }
61
62 -static u32
63 -mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
64 +static int
65 +mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
66 {
67 - struct mii_bus *bus = priv->bus;
68 + struct mii_bus *bus = context;
69 u16 page, r, lo, hi;
70 int ret;
71
72 @@ -224,17 +234,32 @@ mt7530_mii_read(struct mt7530_priv *priv
73
74 /* MT7530 uses 31 as the pseudo port */
75 ret = bus->write(bus, 0x1f, 0x1f, page);
76 - if (ret < 0) {
77 + if (ret < 0)
78 + return ret;
79 +
80 + lo = bus->read(bus, 0x1f, r);
81 + hi = bus->read(bus, 0x1f, 0x10);
82 +
83 + *val = (hi << 16) | (lo & 0xffff);
84 +
85 + return 0;
86 +}
87 +
88 +static u32
89 +mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
90 +{
91 + int ret;
92 + u32 val;
93 +
94 + ret = regmap_read(priv->regmap, reg, &val);
95 + if (ret) {
96 WARN_ON_ONCE(1);
97 - dev_err(&bus->dev,
98 + dev_err(priv->dev,
99 "failed to read mt7530 register\n");
100 return 0;
101 }
102
103 - lo = bus->read(bus, 0x1f, r);
104 - hi = bus->read(bus, 0x1f, 0x10);
105 -
106 - return (hi << 16) | (lo & 0xffff);
107 + return val;
108 }
109
110 static void
111 @@ -284,14 +309,10 @@ mt7530_rmw(struct mt7530_priv *priv, u32
112 u32 mask, u32 set)
113 {
114 struct mii_bus *bus = priv->bus;
115 - u32 val;
116
117 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
118
119 - val = mt7530_mii_read(priv, reg);
120 - val &= ~mask;
121 - val |= set;
122 - mt7530_mii_write(priv, reg, val);
123 + regmap_update_bits(priv->regmap, reg, mask, set);
124
125 mutex_unlock(&bus->mdio_lock);
126 }
127 @@ -299,7 +320,7 @@ mt7530_rmw(struct mt7530_priv *priv, u32
128 static void
129 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
130 {
131 - mt7530_rmw(priv, reg, 0, val);
132 + mt7530_rmw(priv, reg, val, val);
133 }
134
135 static void
136 @@ -2965,22 +2986,6 @@ static const struct phylink_pcs_ops mt75
137 .pcs_an_restart = mt7530_pcs_an_restart,
138 };
139
140 -static int mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
141 -{
142 - struct mt7530_priv *priv = context;
143 -
144 - *val = mt7530_mii_read(priv, reg);
145 - return 0;
146 -};
147 -
148 -static int mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
149 -{
150 - struct mt7530_priv *priv = context;
151 -
152 - mt7530_mii_write(priv, reg, val);
153 - return 0;
154 -};
155 -
156 static void
157 mt7530_mdio_regmap_lock(void *mdio_lock)
158 {
159 @@ -2993,7 +2998,7 @@ mt7530_mdio_regmap_unlock(void *mdio_loc
160 mutex_unlock(mdio_lock);
161 }
162
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,
167 };
168 @@ -3026,7 +3031,7 @@ mt7531_create_sgmii(struct mt7530_priv *
169 mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
170
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 @@ -3191,6 +3196,7 @@ MODULE_DEVICE_TABLE(of, mt7530_of_match)
178 static int
179 mt7530_probe(struct mdio_device *mdiodev)
180 {
181 + static struct regmap_config *regmap_config;
182 struct mt7530_priv *priv;
183 struct device_node *dn;
184
185 @@ -3270,6 +3276,21 @@ mt7530_probe(struct mdio_device *mdiodev
186 mutex_init(&priv->reg_mutex);
187 dev_set_drvdata(&mdiodev->dev, priv);
188
189 + regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
190 + GFP_KERNEL);
191 + if (!regmap_config)
192 + return -ENOMEM;
193 +
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);
203 +
204 return dsa_register_switch(priv->ds);
205 }
206
207 --- a/drivers/net/dsa/mt7530.h
208 +++ b/drivers/net/dsa/mt7530.h
209 @@ -774,6 +774,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 @@ -794,6 +795,7 @@ struct mt7530_priv {
218 struct device *dev;
219 struct dsa_switch *ds;
220 struct mii_bus *bus;
221 + struct regmap *regmap;
222 struct reset_control *rstc;
223 struct regulator *core_pwr;
224 struct regulator *io_pwr;