fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / net / phy / mdio-gpio.c
blobe701433bf52f60a4fb42f0ca631cbd1fdbb68a2c
1 /*
2 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
5 * Copyright (c) 2008 CSE Semaphore Belgium.
6 * by Laurent Pinchart <laurentp@cse-semaphore.com>
8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
10 * Based on earlier work by
12 * Copyright (c) 2003 Intracom S.A.
13 * by Pantelis Antoniou <panto@intracom.gr>
15 * 2005 (c) MontaVista Software, Inc.
16 * Vitaly Bordug <vbordug@ru.mvista.com>
18 * This file is licensed under the terms of the GNU General Public License
19 * version 2. This program is licensed "as is" without any warranty of any
20 * kind, whether express or implied.
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/gpio.h>
28 #include <linux/mdio-gpio.h>
30 #include <linux/of_gpio.h>
31 #include <linux/of_mdio.h>
33 struct mdio_gpio_info {
34 struct mdiobb_ctrl ctrl;
35 int mdc, mdio;
38 static void *mdio_gpio_of_get_data(struct platform_device *pdev)
40 struct device_node *np = pdev->dev.of_node;
41 struct mdio_gpio_platform_data *pdata;
42 int ret;
44 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
45 if (!pdata)
46 return NULL;
48 ret = of_get_gpio(np, 0);
49 if (ret < 0)
50 return NULL;
52 pdata->mdc = ret;
54 ret = of_get_gpio(np, 1);
55 if (ret < 0)
56 return NULL;
57 pdata->mdio = ret;
59 return pdata;
62 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
64 struct mdio_gpio_info *bitbang =
65 container_of(ctrl, struct mdio_gpio_info, ctrl);
67 if (dir)
68 gpio_direction_output(bitbang->mdio, 1);
69 else
70 gpio_direction_input(bitbang->mdio);
73 static int mdio_get(struct mdiobb_ctrl *ctrl)
75 struct mdio_gpio_info *bitbang =
76 container_of(ctrl, struct mdio_gpio_info, ctrl);
78 return gpio_get_value(bitbang->mdio);
81 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
83 struct mdio_gpio_info *bitbang =
84 container_of(ctrl, struct mdio_gpio_info, ctrl);
86 gpio_set_value(bitbang->mdio, what);
89 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
91 struct mdio_gpio_info *bitbang =
92 container_of(ctrl, struct mdio_gpio_info, ctrl);
94 gpio_set_value(bitbang->mdc, what);
97 static struct mdiobb_ops mdio_gpio_ops = {
98 .owner = THIS_MODULE,
99 .set_mdc = mdc_set,
100 .set_mdio_dir = mdio_dir,
101 .set_mdio_data = mdio_set,
102 .get_mdio_data = mdio_get,
105 static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
106 struct mdio_gpio_platform_data *pdata,
107 int bus_id)
109 struct mii_bus *new_bus;
110 struct mdio_gpio_info *bitbang;
111 int i;
113 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
114 if (!bitbang)
115 goto out;
117 bitbang->ctrl.ops = &mdio_gpio_ops;
118 bitbang->ctrl.reset = pdata->reset;
119 bitbang->mdc = pdata->mdc;
120 bitbang->mdio = pdata->mdio;
122 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
123 if (!new_bus)
124 goto out_free_bitbang;
126 new_bus->name = "GPIO Bitbanged MDIO",
128 new_bus->phy_mask = pdata->phy_mask;
129 new_bus->irq = pdata->irqs;
130 new_bus->parent = dev;
132 if (new_bus->phy_mask == ~0)
133 goto out_free_bus;
135 for (i = 0; i < PHY_MAX_ADDR; i++)
136 if (!new_bus->irq[i])
137 new_bus->irq[i] = PHY_POLL;
139 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
141 if (gpio_request(bitbang->mdc, "mdc"))
142 goto out_free_bus;
144 if (gpio_request(bitbang->mdio, "mdio"))
145 goto out_free_mdc;
147 gpio_direction_output(bitbang->mdc, 0);
149 dev_set_drvdata(dev, new_bus);
151 return new_bus;
153 out_free_mdc:
154 gpio_free(bitbang->mdc);
155 out_free_bus:
156 free_mdio_bitbang(new_bus);
157 out_free_bitbang:
158 kfree(bitbang);
159 out:
160 return NULL;
163 static void mdio_gpio_bus_deinit(struct device *dev)
165 struct mii_bus *bus = dev_get_drvdata(dev);
166 struct mdio_gpio_info *bitbang = bus->priv;
168 dev_set_drvdata(dev, NULL);
169 gpio_free(bitbang->mdio);
170 gpio_free(bitbang->mdc);
171 free_mdio_bitbang(bus);
172 kfree(bitbang);
175 static void mdio_gpio_bus_destroy(struct device *dev)
177 struct mii_bus *bus = dev_get_drvdata(dev);
179 mdiobus_unregister(bus);
180 mdio_gpio_bus_deinit(dev);
183 static int mdio_gpio_probe(struct platform_device *pdev)
185 struct mdio_gpio_platform_data *pdata;
186 struct mii_bus *new_bus;
187 int ret, bus_id;
189 if (pdev->dev.of_node) {
190 pdata = mdio_gpio_of_get_data(pdev);
191 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
192 } else {
193 pdata = dev_get_platdata(&pdev->dev);
194 bus_id = pdev->id;
197 if (!pdata)
198 return -ENODEV;
200 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
201 if (!new_bus)
202 return -ENODEV;
204 if (pdev->dev.of_node)
205 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
206 else
207 ret = mdiobus_register(new_bus);
209 if (ret)
210 mdio_gpio_bus_deinit(&pdev->dev);
212 return ret;
215 static int mdio_gpio_remove(struct platform_device *pdev)
217 mdio_gpio_bus_destroy(&pdev->dev);
219 return 0;
222 static struct of_device_id mdio_gpio_of_match[] = {
223 { .compatible = "virtual,mdio-gpio", },
224 { /* sentinel */ }
227 static struct platform_driver mdio_gpio_driver = {
228 .probe = mdio_gpio_probe,
229 .remove = mdio_gpio_remove,
230 .driver = {
231 .name = "mdio-gpio",
232 .owner = THIS_MODULE,
233 .of_match_table = mdio_gpio_of_match,
237 module_platform_driver(mdio_gpio_driver);
239 MODULE_ALIAS("platform:mdio-gpio");
240 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
241 MODULE_LICENSE("GPL");
242 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");