treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / dsa / lan9303_mdio.c
blob9cbe80460b53c0d13838a9e5dc94f9a45e6012cd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
7 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mdio.h>
11 #include <linux/phy.h>
12 #include <linux/of.h>
14 #include "lan9303.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)
20 struct lan9303_mdio {
21 struct mdio_device *device;
22 struct lan9303 chip;
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);
40 return 0;
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);
58 return 0;
61 static int lan9303_mdio_phy_write(struct lan9303 *chip, int phy, int reg,
62 u16 val)
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 = {
82 .reg_bits = 8,
83 .val_bits = 32,
84 .reg_stride = 1,
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;
102 int ret;
104 sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
105 GFP_KERNEL);
106 if (!sw_dev)
107 return -ENOMEM;
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);
114 return 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);
125 if (ret != 0)
126 return ret;
128 dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
130 return 0;
133 static void lan9303_mdio_remove(struct mdio_device *mdiodev)
135 struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
137 if (!sw_dev)
138 return;
140 lan9303_remove(&sw_dev->chip);
143 /*-------------------------------------------------------------------------*/
145 static const struct of_device_id lan9303_mdio_of_match[] = {
146 { .compatible = "smsc,lan9303-mdio" },
147 { /* sentinel */ },
149 MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
151 static struct mdio_driver lan9303_mdio_driver = {
152 .mdiodrv.driver = {
153 .name = "LAN9303_MDIO",
154 .of_match_table = of_match_ptr(lan9303_mdio_of_match),
156 .probe = lan9303_mdio_probe,
157 .remove = lan9303_mdio_remove,
159 mdio_module_driver(lan9303_mdio_driver);
161 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
162 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
163 MODULE_LICENSE("GPL v2");