mvebu: add support for SFP
[openwrt/openwrt.git] / target / linux / mvebu / patches-4.4 / 129-phy-add-I2C-mdio-bus.patch
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
5
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.
9
10 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
11 ---
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
19
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.
25
26 +config MDIO_I2C
27 + tristate
28 + depends on I2C
29 + help
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.
33 +
34 + This is library mode.
35 +
36 config MDIO_OCTEON
37 tristate "Support for MDIO buses on Octeon and ThunderX SOCs"
38 depends on 64BIT
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
49 --- /dev/null
50 +++ b/drivers/net/phy/mdio-i2c.c
51 @@ -0,0 +1,90 @@
52 +/*
53 + * MDIO I2C bridge
54 + *
55 + * Copyright (C) 2015 Russell King
56 + *
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.
60 + */
61 +#include <linux/i2c.h>
62 +#include <linux/phy.h>
63 +
64 +#include "mdio-i2c.h"
65 +
66 +static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
67 +{
68 + struct i2c_adapter *i2c = bus->priv;
69 + struct i2c_msg msgs[2];
70 + u8 data[2], dev_addr = reg;
71 + int bus_addr, ret;
72 +
73 + bus_addr = 0x40 + phy_id;
74 + if (bus_addr == 0x50 || bus_addr == 0x51)
75 + return 0xffff;
76 +
77 + msgs[0].addr = bus_addr;
78 + msgs[0].flags = 0;
79 + msgs[0].len = 1;
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);
84 + msgs[1].buf = data;
85 +
86 + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
87 + if (ret != ARRAY_SIZE(msgs))
88 + return 0xffff;
89 +
90 + return data[0] << 8 | data[1];
91 +}
92 +
93 +static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
94 +{
95 + struct i2c_adapter *i2c = bus->priv;
96 + struct i2c_msg msg;
97 + int bus_addr, ret;
98 + u8 data[3];
99 +
100 + bus_addr = 0x40 + phy_id;
101 + if (bus_addr == 0x50 || bus_addr == 0x51)
102 + return 0;
103 +
104 + data[0] = reg;
105 + data[1] = val >> 8;
106 + data[2] = val;
107 +
108 + msg.addr = bus_addr;
109 + msg.flags = 0;
110 + msg.len = 3;
111 + msg.buf = data;
112 +
113 + ret = i2c_transfer(i2c, &msg, 1);
114 +
115 + return ret < 0 ? ret : 0;
116 +}
117 +
118 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
119 +{
120 + struct mii_bus *mii;
121 +
122 + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
123 + return ERR_PTR(-EINVAL);
124 +
125 + mii = mdiobus_alloc();
126 + if (!mii)
127 + return ERR_PTR(-ENOMEM);
128 +
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;
133 + mii->priv = i2c;
134 +
135 + return mii;
136 +}
137 +EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
138 +
139 +MODULE_AUTHOR("Russell King");
140 +MODULE_DESCRIPTION("MDIO I2C bridge library");
141 +MODULE_LICENSE("GPL v2");
142 --- /dev/null
143 +++ b/drivers/net/phy/mdio-i2c.h
144 @@ -0,0 +1,19 @@
145 +/*
146 + * MDIO I2C bridge
147 + *
148 + * Copyright (C) 2015 Russell King
149 + *
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.
153 + */
154 +#ifndef MDIO_I2C_H
155 +#define MDIO_I2C_H
156 +
157 +struct device;
158 +struct i2c_adapter;
159 +struct mii_bus;
160 +
161 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c);
162 +
163 +#endif