]> Git Repo - linux.git/blob - drivers/net/phy/mdio-i2c.c
ASoC: simple-card: Use snd_soc_of_parse_aux_devs()
[linux.git] / drivers / net / phy / mdio-i2c.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MDIO I2C bridge
4  *
5  * Copyright (C) 2015-2016 Russell King
6  *
7  * Network PHYs can appear on I2C buses when they are part of SFP module.
8  * This driver exposes these PHYs to the networking PHY code, allowing
9  * our PHY drivers access to these PHYs, and so allowing configuration
10  * of their settings.
11  */
12 #include <linux/i2c.h>
13 #include <linux/phy.h>
14
15 #include "mdio-i2c.h"
16
17 /*
18  * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
19  * specified to be present in SFP modules.  These correspond with PHY
20  * addresses 16 and 17.  Disallow access to these "phy" addresses.
21  */
22 static bool i2c_mii_valid_phy_id(int phy_id)
23 {
24         return phy_id != 0x10 && phy_id != 0x11;
25 }
26
27 static unsigned int i2c_mii_phy_addr(int phy_id)
28 {
29         return phy_id + 0x40;
30 }
31
32 static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
33 {
34         struct i2c_adapter *i2c = bus->priv;
35         struct i2c_msg msgs[2];
36         u8 addr[3], data[2], *p;
37         int bus_addr, ret;
38
39         if (!i2c_mii_valid_phy_id(phy_id))
40                 return 0xffff;
41
42         p = addr;
43         if (reg & MII_ADDR_C45) {
44                 *p++ = 0x20 | ((reg >> 16) & 31);
45                 *p++ = reg >> 8;
46         }
47         *p++ = reg;
48
49         bus_addr = i2c_mii_phy_addr(phy_id);
50         msgs[0].addr = bus_addr;
51         msgs[0].flags = 0;
52         msgs[0].len = p - addr;
53         msgs[0].buf = addr;
54         msgs[1].addr = bus_addr;
55         msgs[1].flags = I2C_M_RD;
56         msgs[1].len = sizeof(data);
57         msgs[1].buf = data;
58
59         ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
60         if (ret != ARRAY_SIZE(msgs))
61                 return 0xffff;
62
63         return data[0] << 8 | data[1];
64 }
65
66 static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
67 {
68         struct i2c_adapter *i2c = bus->priv;
69         struct i2c_msg msg;
70         int ret;
71         u8 data[5], *p;
72
73         if (!i2c_mii_valid_phy_id(phy_id))
74                 return 0;
75
76         p = data;
77         if (reg & MII_ADDR_C45) {
78                 *p++ = (reg >> 16) & 31;
79                 *p++ = reg >> 8;
80         }
81         *p++ = reg;
82         *p++ = val >> 8;
83         *p++ = val;
84
85         msg.addr = i2c_mii_phy_addr(phy_id);
86         msg.flags = 0;
87         msg.len = p - data;
88         msg.buf = data;
89
90         ret = i2c_transfer(i2c, &msg, 1);
91
92         return ret < 0 ? ret : 0;
93 }
94
95 struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
96 {
97         struct mii_bus *mii;
98
99         if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
100                 return ERR_PTR(-EINVAL);
101
102         mii = mdiobus_alloc();
103         if (!mii)
104                 return ERR_PTR(-ENOMEM);
105
106         snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
107         mii->parent = parent;
108         mii->read = i2c_mii_read;
109         mii->write = i2c_mii_write;
110         mii->priv = i2c;
111
112         return mii;
113 }
114 EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
115
116 MODULE_AUTHOR("Russell King");
117 MODULE_DESCRIPTION("MDIO I2C bridge library");
118 MODULE_LICENSE("GPL v2");
This page took 0.038239 seconds and 4 git commands to generate.