Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / phy / fixed_phy.c
blob001fe1df75572687e527c34eb608370d446bc670
1 /*
2 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
4 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
7 * Copyright (c) 2006-2007 MontaVista Software, Inc.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/list.h>
19 #include <linux/mii.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/of.h>
25 #include <linux/gpio.h>
26 #include <linux/seqlock.h>
27 #include <linux/idr.h>
29 #include "swphy.h"
31 struct fixed_mdio_bus {
32 struct mii_bus *mii_bus;
33 struct list_head phys;
36 struct fixed_phy {
37 int addr;
38 struct phy_device *phydev;
39 seqcount_t seqcount;
40 struct fixed_phy_status status;
41 int (*link_update)(struct net_device *, struct fixed_phy_status *);
42 struct list_head node;
43 int link_gpio;
46 static struct platform_device *pdev;
47 static struct fixed_mdio_bus platform_fmb = {
48 .phys = LIST_HEAD_INIT(platform_fmb.phys),
51 static void fixed_phy_update(struct fixed_phy *fp)
53 if (gpio_is_valid(fp->link_gpio))
54 fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
57 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
59 struct fixed_mdio_bus *fmb = bus->priv;
60 struct fixed_phy *fp;
62 list_for_each_entry(fp, &fmb->phys, node) {
63 if (fp->addr == phy_addr) {
64 struct fixed_phy_status state;
65 int s;
67 do {
68 s = read_seqcount_begin(&fp->seqcount);
69 /* Issue callback if user registered it. */
70 if (fp->link_update) {
71 fp->link_update(fp->phydev->attached_dev,
72 &fp->status);
73 fixed_phy_update(fp);
75 state = fp->status;
76 } while (read_seqcount_retry(&fp->seqcount, s));
78 return swphy_read_reg(reg_num, &state);
82 return 0xFFFF;
85 static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
86 u16 val)
88 return 0;
92 * If something weird is required to be done with link/speed,
93 * network driver is able to assign a function to implement this.
94 * May be useful for PHY's that need to be software-driven.
96 int fixed_phy_set_link_update(struct phy_device *phydev,
97 int (*link_update)(struct net_device *,
98 struct fixed_phy_status *))
100 struct fixed_mdio_bus *fmb = &platform_fmb;
101 struct fixed_phy *fp;
103 if (!phydev || !phydev->mdio.bus)
104 return -EINVAL;
106 list_for_each_entry(fp, &fmb->phys, node) {
107 if (fp->addr == phydev->mdio.addr) {
108 fp->link_update = link_update;
109 fp->phydev = phydev;
110 return 0;
114 return -ENOENT;
116 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
118 int fixed_phy_add(unsigned int irq, int phy_addr,
119 struct fixed_phy_status *status,
120 int link_gpio)
122 int ret;
123 struct fixed_mdio_bus *fmb = &platform_fmb;
124 struct fixed_phy *fp;
126 ret = swphy_validate_state(status);
127 if (ret < 0)
128 return ret;
130 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
131 if (!fp)
132 return -ENOMEM;
134 seqcount_init(&fp->seqcount);
136 if (irq != PHY_POLL)
137 fmb->mii_bus->irq[phy_addr] = irq;
139 fp->addr = phy_addr;
140 fp->status = *status;
141 fp->link_gpio = link_gpio;
143 if (gpio_is_valid(fp->link_gpio)) {
144 ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
145 "fixed-link-gpio-link");
146 if (ret)
147 goto err_regs;
150 fixed_phy_update(fp);
152 list_add_tail(&fp->node, &fmb->phys);
154 return 0;
156 err_regs:
157 kfree(fp);
158 return ret;
160 EXPORT_SYMBOL_GPL(fixed_phy_add);
162 static DEFINE_IDA(phy_fixed_ida);
164 static void fixed_phy_del(int phy_addr)
166 struct fixed_mdio_bus *fmb = &platform_fmb;
167 struct fixed_phy *fp, *tmp;
169 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
170 if (fp->addr == phy_addr) {
171 list_del(&fp->node);
172 if (gpio_is_valid(fp->link_gpio))
173 gpio_free(fp->link_gpio);
174 kfree(fp);
175 ida_simple_remove(&phy_fixed_ida, phy_addr);
176 return;
181 struct phy_device *fixed_phy_register(unsigned int irq,
182 struct fixed_phy_status *status,
183 int link_gpio,
184 struct device_node *np)
186 struct fixed_mdio_bus *fmb = &platform_fmb;
187 struct phy_device *phy;
188 int phy_addr;
189 int ret;
191 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
192 return ERR_PTR(-EPROBE_DEFER);
194 /* Get the next available PHY address, up to PHY_MAX_ADDR */
195 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
196 if (phy_addr < 0)
197 return ERR_PTR(phy_addr);
199 ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
200 if (ret < 0) {
201 ida_simple_remove(&phy_fixed_ida, phy_addr);
202 return ERR_PTR(ret);
205 phy = get_phy_device(fmb->mii_bus, phy_addr, false);
206 if (IS_ERR(phy)) {
207 fixed_phy_del(phy_addr);
208 return ERR_PTR(-EINVAL);
211 /* propagate the fixed link values to struct phy_device */
212 phy->link = status->link;
213 if (status->link) {
214 phy->speed = status->speed;
215 phy->duplex = status->duplex;
216 phy->pause = status->pause;
217 phy->asym_pause = status->asym_pause;
220 of_node_get(np);
221 phy->mdio.dev.of_node = np;
222 phy->is_pseudo_fixed_link = true;
224 switch (status->speed) {
225 case SPEED_1000:
226 phy->supported = PHY_1000BT_FEATURES;
227 break;
228 case SPEED_100:
229 phy->supported = PHY_100BT_FEATURES;
230 break;
231 case SPEED_10:
232 default:
233 phy->supported = PHY_10BT_FEATURES;
236 ret = phy_device_register(phy);
237 if (ret) {
238 phy_device_free(phy);
239 of_node_put(np);
240 fixed_phy_del(phy_addr);
241 return ERR_PTR(ret);
244 return phy;
246 EXPORT_SYMBOL_GPL(fixed_phy_register);
248 void fixed_phy_unregister(struct phy_device *phy)
250 phy_device_remove(phy);
251 of_node_put(phy->mdio.dev.of_node);
252 fixed_phy_del(phy->mdio.addr);
254 EXPORT_SYMBOL_GPL(fixed_phy_unregister);
256 static int __init fixed_mdio_bus_init(void)
258 struct fixed_mdio_bus *fmb = &platform_fmb;
259 int ret;
261 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
262 if (IS_ERR(pdev)) {
263 ret = PTR_ERR(pdev);
264 goto err_pdev;
267 fmb->mii_bus = mdiobus_alloc();
268 if (fmb->mii_bus == NULL) {
269 ret = -ENOMEM;
270 goto err_mdiobus_reg;
273 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
274 fmb->mii_bus->name = "Fixed MDIO Bus";
275 fmb->mii_bus->priv = fmb;
276 fmb->mii_bus->parent = &pdev->dev;
277 fmb->mii_bus->read = &fixed_mdio_read;
278 fmb->mii_bus->write = &fixed_mdio_write;
280 ret = mdiobus_register(fmb->mii_bus);
281 if (ret)
282 goto err_mdiobus_alloc;
284 return 0;
286 err_mdiobus_alloc:
287 mdiobus_free(fmb->mii_bus);
288 err_mdiobus_reg:
289 platform_device_unregister(pdev);
290 err_pdev:
291 return ret;
293 module_init(fixed_mdio_bus_init);
295 static void __exit fixed_mdio_bus_exit(void)
297 struct fixed_mdio_bus *fmb = &platform_fmb;
298 struct fixed_phy *fp, *tmp;
300 mdiobus_unregister(fmb->mii_bus);
301 mdiobus_free(fmb->mii_bus);
302 platform_device_unregister(pdev);
304 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
305 list_del(&fp->node);
306 kfree(fp);
308 ida_destroy(&phy_fixed_ida);
310 module_exit(fixed_mdio_bus_exit);
312 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
313 MODULE_AUTHOR("Vitaly Bordug");
314 MODULE_LICENSE("GPL");