Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / net / dsa / lan9303_mdio.c
blob93c36c0541cf95fc9f6a73c6442a2e5cc3136736
1 /*
2 * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
4 * Partially based on a patch from
5 * Copyright (c) 2014 Stefan Roese <sr@denx.de>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mdio.h>
20 #include <linux/phy.h>
21 #include <linux/of.h>
23 #include "lan9303.h"
25 /* Generate phy-addr and -reg from the input address */
26 #define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f)
27 #define PHY_REG(x) (((x) >> 1) & 0x1f)
29 struct lan9303_mdio {
30 struct mdio_device *device;
31 struct lan9303 chip;
34 static void lan9303_mdio_real_write(struct mdio_device *mdio, int reg, u16 val)
36 mdio->bus->write(mdio->bus, PHY_ADDR(reg), PHY_REG(reg), val);
39 static int lan9303_mdio_write(void *ctx, uint32_t reg, uint32_t val)
41 struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
43 mutex_lock(&sw_dev->device->bus->mdio_lock);
44 lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
45 lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
46 mutex_unlock(&sw_dev->device->bus->mdio_lock);
48 return 0;
51 static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
53 return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
56 static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
58 struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
60 mutex_lock(&sw_dev->device->bus->mdio_lock);
61 *val = lan9303_mdio_real_read(sw_dev->device, reg);
62 *val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
63 mutex_unlock(&sw_dev->device->bus->mdio_lock);
65 return 0;
68 static const struct regmap_config lan9303_mdio_regmap_config = {
69 .reg_bits = 8,
70 .val_bits = 32,
71 .reg_stride = 1,
72 .can_multi_write = true,
73 .max_register = 0x0ff, /* address bits 0..1 are not used */
74 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
76 .volatile_table = &lan9303_register_set,
77 .wr_table = &lan9303_register_set,
78 .rd_table = &lan9303_register_set,
80 .reg_read = lan9303_mdio_read,
81 .reg_write = lan9303_mdio_write,
83 .cache_type = REGCACHE_NONE,
86 static int lan9303_mdio_probe(struct mdio_device *mdiodev)
88 struct lan9303_mdio *sw_dev;
89 int ret;
91 sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
92 GFP_KERNEL);
93 if (!sw_dev)
94 return -ENOMEM;
96 sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
97 &lan9303_mdio_regmap_config);
98 if (IS_ERR(sw_dev->chip.regmap)) {
99 ret = PTR_ERR(sw_dev->chip.regmap);
100 dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
101 return ret;
104 /* link forward and backward */
105 sw_dev->device = mdiodev;
106 dev_set_drvdata(&mdiodev->dev, sw_dev);
107 sw_dev->chip.dev = &mdiodev->dev;
109 ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
110 if (ret != 0)
111 return ret;
113 dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
115 return 0;
118 static void lan9303_mdio_remove(struct mdio_device *mdiodev)
120 struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
122 if (!sw_dev)
123 return;
125 lan9303_remove(&sw_dev->chip);
128 /*-------------------------------------------------------------------------*/
130 static const struct of_device_id lan9303_mdio_of_match[] = {
131 { .compatible = "smsc,lan9303-mdio" },
132 { /* sentinel */ },
134 MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
136 static struct mdio_driver lan9303_mdio_driver = {
137 .mdiodrv.driver = {
138 .name = "LAN9303_MDIO",
139 .of_match_table = of_match_ptr(lan9303_mdio_of_match),
141 .probe = lan9303_mdio_probe,
142 .remove = lan9303_mdio_remove,
144 mdio_module_driver(lan9303_mdio_driver);
146 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
147 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
148 MODULE_LICENSE("GPL v2");