1 From 7f36ac946bfbd4090b8b94be3661b41ac73e21f4 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@arm.linux.org.uk>
3 Date: Fri, 25 Sep 2015 17:43:52 +0100
4 Subject: [PATCH 717/744] phy: add I2C mdio bus
6 Add an I2C MDIO bus bridge library, to allow phylib to access PHYs which
7 are connected to an I2C bus instead of the more conventional MDIO bus.
8 Such PHYs can be found in SFP adapters and SFF modules.
10 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
12 drivers/net/phy/Kconfig | 10 ++++++
13 drivers/net/phy/Makefile | 1 +
14 drivers/net/phy/mdio-i2c.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++
15 drivers/net/phy/mdio-i2c.h | 19 ++++++++++
16 4 files changed, 120 insertions(+)
17 create mode 100644 drivers/net/phy/mdio-i2c.c
18 create mode 100644 drivers/net/phy/mdio-i2c.h
20 --- a/drivers/net/phy/Kconfig
21 +++ b/drivers/net/phy/Kconfig
22 @@ -233,6 +233,16 @@ config MDIO_GPIO
23 To compile this driver as a module, choose M here: the module
24 will be called mdio-gpio.
30 + Support I2C based PHYs. This provides a MDIO bus bridged
31 + to I2C to allow PHYs connected in I2C mode to be accessed
32 + using the existing infrastructure.
34 + This is library mode.
37 tristate "Support for MDIO buses on Octeon and ThunderX SOCs"
39 --- a/drivers/net/phy/Makefile
40 +++ b/drivers/net/phy/Makefile
41 @@ -41,6 +41,7 @@ obj-$(CONFIG_SWCONFIG_B53) += b53/
42 obj-$(CONFIG_FIXED_PHY) += fixed_phy.o
43 obj-$(CONFIG_MDIO_BITBANG) += mdio-bitbang.o
44 obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
45 +obj-$(CONFIG_MDIO_I2C) += mdio-i2c.o
46 obj-$(CONFIG_NATIONAL_PHY) += national.o
47 obj-$(CONFIG_DP83640_PHY) += dp83640.o
48 obj-$(CONFIG_DP83848_PHY) += dp83848.o
50 +++ b/drivers/net/phy/mdio-i2c.c
55 + * Copyright (C) 2015 Russell King
57 + * This program is free software; you can redistribute it and/or modify
58 + * it under the terms of the GNU General Public License version 2 as
59 + * published by the Free Software Foundation.
61 +#include <linux/i2c.h>
62 +#include <linux/phy.h>
64 +#include "mdio-i2c.h"
66 +static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
68 + struct i2c_adapter *i2c = bus->priv;
69 + struct i2c_msg msgs[2];
70 + u8 data[2], dev_addr = reg;
73 + bus_addr = 0x40 + phy_id;
74 + if (bus_addr == 0x50 || bus_addr == 0x51)
77 + msgs[0].addr = bus_addr;
80 + msgs[0].buf = &dev_addr;
81 + msgs[1].addr = bus_addr;
82 + msgs[1].flags = I2C_M_RD;
83 + msgs[1].len = sizeof(data);
86 + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
87 + if (ret != ARRAY_SIZE(msgs))
90 + return data[0] << 8 | data[1];
93 +static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
95 + struct i2c_adapter *i2c = bus->priv;
100 + bus_addr = 0x40 + phy_id;
101 + if (bus_addr == 0x50 || bus_addr == 0x51)
105 + data[1] = val >> 8;
108 + msg.addr = bus_addr;
113 + ret = i2c_transfer(i2c, &msg, 1);
115 + return ret < 0 ? ret : 0;
118 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
120 + struct mii_bus *mii;
122 + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
123 + return ERR_PTR(-EINVAL);
125 + mii = mdiobus_alloc();
127 + return ERR_PTR(-ENOMEM);
129 + snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
130 + mii->parent = parent;
131 + mii->read = i2c_mii_read;
132 + mii->write = i2c_mii_write;
137 +EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
139 +MODULE_AUTHOR("Russell King");
140 +MODULE_DESCRIPTION("MDIO I2C bridge library");
141 +MODULE_LICENSE("GPL v2");
143 +++ b/drivers/net/phy/mdio-i2c.h
148 + * Copyright (C) 2015 Russell King
150 + * This program is free software; you can redistribute it and/or modify
151 + * it under the terms of the GNU General Public License version 2 as
152 + * published by the Free Software Foundation.
161 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c);