2
0

790-06-v6.4-net-dsa-mt7530-use-regmap-to-access-switch-register-.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. From c7945d11a060797c31b3f47d9c9e515b0bf2082f Mon Sep 17 00:00:00 2001
  2. From: Daniel Golle <[email protected]>
  3. Date: Mon, 3 Apr 2023 02:17:52 +0100
  4. Subject: [PATCH 06/48] net: dsa: mt7530: use regmap to access switch register
  5. space
  6. Use regmap API to access the switch register space.
  7. Signed-off-by: Daniel Golle <[email protected]>
  8. Signed-off-by: David S. Miller <[email protected]>
  9. ---
  10. drivers/net/dsa/mt7530.c | 99 ++++++++++++++++++++++++----------------
  11. drivers/net/dsa/mt7530.h | 2 +
  12. 2 files changed, 62 insertions(+), 39 deletions(-)
  13. --- a/drivers/net/dsa/mt7530.c
  14. +++ b/drivers/net/dsa/mt7530.c
  15. @@ -183,9 +183,9 @@ core_clear(struct mt7530_priv *priv, u32
  16. }
  17. static int
  18. -mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
  19. +mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
  20. {
  21. - struct mii_bus *bus = priv->bus;
  22. + struct mii_bus *bus = context;
  23. u16 page, r, lo, hi;
  24. int ret;
  25. @@ -197,24 +197,34 @@ mt7530_mii_write(struct mt7530_priv *pri
  26. /* MT7530 uses 31 as the pseudo port */
  27. ret = bus->write(bus, 0x1f, 0x1f, page);
  28. if (ret < 0)
  29. - goto err;
  30. + return ret;
  31. ret = bus->write(bus, 0x1f, r, lo);
  32. if (ret < 0)
  33. - goto err;
  34. + return ret;
  35. ret = bus->write(bus, 0x1f, 0x10, hi);
  36. -err:
  37. + return ret;
  38. +}
  39. +
  40. +static int
  41. +mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val)
  42. +{
  43. + int ret;
  44. +
  45. + ret = regmap_write(priv->regmap, reg, val);
  46. +
  47. if (ret < 0)
  48. - dev_err(&bus->dev,
  49. + dev_err(priv->dev,
  50. "failed to write mt7530 register\n");
  51. +
  52. return ret;
  53. }
  54. -static u32
  55. -mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
  56. +static int
  57. +mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
  58. {
  59. - struct mii_bus *bus = priv->bus;
  60. + struct mii_bus *bus = context;
  61. u16 page, r, lo, hi;
  62. int ret;
  63. @@ -223,17 +233,32 @@ mt7530_mii_read(struct mt7530_priv *priv
  64. /* MT7530 uses 31 as the pseudo port */
  65. ret = bus->write(bus, 0x1f, 0x1f, page);
  66. - if (ret < 0) {
  67. + if (ret < 0)
  68. + return ret;
  69. +
  70. + lo = bus->read(bus, 0x1f, r);
  71. + hi = bus->read(bus, 0x1f, 0x10);
  72. +
  73. + *val = (hi << 16) | (lo & 0xffff);
  74. +
  75. + return 0;
  76. +}
  77. +
  78. +static u32
  79. +mt7530_mii_read(struct mt7530_priv *priv, u32 reg)
  80. +{
  81. + int ret;
  82. + u32 val;
  83. +
  84. + ret = regmap_read(priv->regmap, reg, &val);
  85. + if (ret) {
  86. WARN_ON_ONCE(1);
  87. - dev_err(&bus->dev,
  88. + dev_err(priv->dev,
  89. "failed to read mt7530 register\n");
  90. return 0;
  91. }
  92. - lo = bus->read(bus, 0x1f, r);
  93. - hi = bus->read(bus, 0x1f, 0x10);
  94. -
  95. - return (hi << 16) | (lo & 0xffff);
  96. + return val;
  97. }
  98. static void
  99. @@ -283,14 +308,10 @@ mt7530_rmw(struct mt7530_priv *priv, u32
  100. u32 mask, u32 set)
  101. {
  102. struct mii_bus *bus = priv->bus;
  103. - u32 val;
  104. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  105. - val = mt7530_mii_read(priv, reg);
  106. - val &= ~mask;
  107. - val |= set;
  108. - mt7530_mii_write(priv, reg, val);
  109. + regmap_update_bits(priv->regmap, reg, mask, set);
  110. mutex_unlock(&bus->mdio_lock);
  111. }
  112. @@ -298,7 +319,7 @@ mt7530_rmw(struct mt7530_priv *priv, u32
  113. static void
  114. mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val)
  115. {
  116. - mt7530_rmw(priv, reg, 0, val);
  117. + mt7530_rmw(priv, reg, val, val);
  118. }
  119. static void
  120. @@ -2970,22 +2991,6 @@ static const struct phylink_pcs_ops mt75
  121. .pcs_an_restart = mt7530_pcs_an_restart,
  122. };
  123. -static int mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
  124. -{
  125. - struct mt7530_priv *priv = context;
  126. -
  127. - *val = mt7530_mii_read(priv, reg);
  128. - return 0;
  129. -};
  130. -
  131. -static int mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
  132. -{
  133. - struct mt7530_priv *priv = context;
  134. -
  135. - mt7530_mii_write(priv, reg, val);
  136. - return 0;
  137. -};
  138. -
  139. static void
  140. mt7530_mdio_regmap_lock(void *mdio_lock)
  141. {
  142. @@ -2998,7 +3003,7 @@ mt7530_mdio_regmap_unlock(void *mdio_loc
  143. mutex_unlock(mdio_lock);
  144. }
  145. -static const struct regmap_bus mt7531_regmap_bus = {
  146. +static const struct regmap_bus mt7530_regmap_bus = {
  147. .reg_write = mt7530_regmap_write,
  148. .reg_read = mt7530_regmap_read,
  149. };
  150. @@ -3031,7 +3036,7 @@ mt7531_create_sgmii(struct mt7530_priv *
  151. mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
  152. regmap = devm_regmap_init(priv->dev,
  153. - &mt7531_regmap_bus, priv,
  154. + &mt7530_regmap_bus, priv->bus,
  155. mt7531_pcs_config[i]);
  156. if (IS_ERR(regmap)) {
  157. ret = PTR_ERR(regmap);
  158. @@ -3196,6 +3201,7 @@ MODULE_DEVICE_TABLE(of, mt7530_of_match)
  159. static int
  160. mt7530_probe(struct mdio_device *mdiodev)
  161. {
  162. + static struct regmap_config *regmap_config;
  163. struct mt7530_priv *priv;
  164. struct device_node *dn;
  165. @@ -3275,6 +3281,21 @@ mt7530_probe(struct mdio_device *mdiodev
  166. mutex_init(&priv->reg_mutex);
  167. dev_set_drvdata(&mdiodev->dev, priv);
  168. + regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
  169. + GFP_KERNEL);
  170. + if (!regmap_config)
  171. + return -ENOMEM;
  172. +
  173. + regmap_config->reg_bits = 16;
  174. + regmap_config->val_bits = 32;
  175. + regmap_config->reg_stride = 4;
  176. + regmap_config->max_register = MT7530_CREV;
  177. + regmap_config->disable_locking = true;
  178. + priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus,
  179. + priv->bus, regmap_config);
  180. + if (IS_ERR(priv->regmap))
  181. + return PTR_ERR(priv->regmap);
  182. +
  183. return dsa_register_switch(priv->ds);
  184. }
  185. --- a/drivers/net/dsa/mt7530.h
  186. +++ b/drivers/net/dsa/mt7530.h
  187. @@ -774,6 +774,7 @@ struct mt753x_info {
  188. * @dev: The device pointer
  189. * @ds: The pointer to the dsa core structure
  190. * @bus: The bus used for the device and built-in PHY
  191. + * @regmap: The regmap instance representing all switch registers
  192. * @rstc: The pointer to reset control used by MCM
  193. * @core_pwr: The power supplied into the core
  194. * @io_pwr: The power supplied into the I/O
  195. @@ -794,6 +795,7 @@ struct mt7530_priv {
  196. struct device *dev;
  197. struct dsa_switch *ds;
  198. struct mii_bus *bus;
  199. + struct regmap *regmap;
  200. struct reset_control *rstc;
  201. struct regulator *core_pwr;
  202. struct regulator *io_pwr;