1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
5 * Partially based on a patch from
6 * Copyright (c) 2014 Stefan Roese <sr@denx.de>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mdio.h>
11 #include <linux/phy.h>
16 /* Generate phy-addr and -reg from the input address */
17 #define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f)
18 #define PHY_REG(x) (((x) >> 1) & 0x1f)
21 struct mdio_device *device;
25 static void lan9303_mdio_real_write(struct mdio_device *mdio, int reg, u16 val)
27 mdio->bus->write(mdio->bus, PHY_ADDR(reg), PHY_REG(reg), val);
30 static int lan9303_mdio_write(void *ctx, uint32_t reg, uint32_t val)
32 struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
34 reg <<= 2; /* reg num to offset */
35 mutex_lock(&sw_dev->device->bus->mdio_lock);
36 lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
37 lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
38 mutex_unlock(&sw_dev->device->bus->mdio_lock);
43 static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
45 return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
48 static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
50 struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
52 reg <<= 2; /* reg num to offset */
53 mutex_lock(&sw_dev->device->bus->mdio_lock);
54 *val = lan9303_mdio_real_read(sw_dev->device, reg);
55 *val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
56 mutex_unlock(&sw_dev->device->bus->mdio_lock);
61 static int lan9303_mdio_phy_write(struct lan9303 *chip, int phy, int reg,
64 struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
66 return mdiobus_write_nested(sw_dev->device->bus, phy, reg, val);
69 static int lan9303_mdio_phy_read(struct lan9303 *chip, int phy, int reg)
71 struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
73 return mdiobus_read_nested(sw_dev->device->bus, phy, reg);
76 static const struct lan9303_phy_ops lan9303_mdio_phy_ops = {
77 .phy_read = lan9303_mdio_phy_read,
78 .phy_write = lan9303_mdio_phy_write,
81 static const struct regmap_config lan9303_mdio_regmap_config = {
85 .can_multi_write = true,
86 .max_register = 0x0ff, /* address bits 0..1 are not used */
87 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
89 .volatile_table = &lan9303_register_set,
90 .wr_table = &lan9303_register_set,
91 .rd_table = &lan9303_register_set,
93 .reg_read = lan9303_mdio_read,
94 .reg_write = lan9303_mdio_write,
96 .cache_type = REGCACHE_NONE,
99 static int lan9303_mdio_probe(struct mdio_device *mdiodev)
101 struct lan9303_mdio *sw_dev;
104 sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
109 sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
110 &lan9303_mdio_regmap_config);
111 if (IS_ERR(sw_dev->chip.regmap)) {
112 ret = PTR_ERR(sw_dev->chip.regmap);
113 dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
117 /* link forward and backward */
118 sw_dev->device = mdiodev;
119 dev_set_drvdata(&mdiodev->dev, sw_dev);
120 sw_dev->chip.dev = &mdiodev->dev;
122 sw_dev->chip.ops = &lan9303_mdio_phy_ops;
124 ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
128 dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
133 static void lan9303_mdio_remove(struct mdio_device *mdiodev)
135 struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
140 lan9303_remove(&sw_dev->chip);
142 dev_set_drvdata(&mdiodev->dev, NULL);
145 static void lan9303_mdio_shutdown(struct mdio_device *mdiodev)
147 struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
152 lan9303_shutdown(&sw_dev->chip);
154 dev_set_drvdata(&mdiodev->dev, NULL);
157 /*-------------------------------------------------------------------------*/
159 static const struct of_device_id lan9303_mdio_of_match[] = {
160 { .compatible = "smsc,lan9303-mdio" },
163 MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
165 static struct mdio_driver lan9303_mdio_driver = {
167 .name = "LAN9303_MDIO",
168 .of_match_table = of_match_ptr(lan9303_mdio_of_match),
170 .probe = lan9303_mdio_probe,
171 .remove = lan9303_mdio_remove,
172 .shutdown = lan9303_mdio_shutdown,
174 mdio_module_driver(lan9303_mdio_driver);
176 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
177 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
178 MODULE_LICENSE("GPL v2");