added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / phy / mdio-gpio.c
blobaf28ff7ae1767a2c9ec6491c36d48b5797286197
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/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/gpio.h>
29 #include <linux/mdio-gpio.h>
31 #ifdef CONFIG_OF_GPIO
32 #include <linux/of_gpio.h>
33 #include <linux/of_platform.h>
34 #endif
36 struct mdio_gpio_info {
37 struct mdiobb_ctrl ctrl;
38 int mdc, mdio;
41 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
43 struct mdio_gpio_info *bitbang =
44 container_of(ctrl, struct mdio_gpio_info, ctrl);
46 if (dir)
47 gpio_direction_output(bitbang->mdio, 1);
48 else
49 gpio_direction_input(bitbang->mdio);
52 static int mdio_get(struct mdiobb_ctrl *ctrl)
54 struct mdio_gpio_info *bitbang =
55 container_of(ctrl, struct mdio_gpio_info, ctrl);
57 return gpio_get_value(bitbang->mdio);
60 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
62 struct mdio_gpio_info *bitbang =
63 container_of(ctrl, struct mdio_gpio_info, ctrl);
65 gpio_set_value(bitbang->mdio, what);
68 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
70 struct mdio_gpio_info *bitbang =
71 container_of(ctrl, struct mdio_gpio_info, ctrl);
73 gpio_set_value(bitbang->mdc, what);
76 static struct mdiobb_ops mdio_gpio_ops = {
77 .owner = THIS_MODULE,
78 .set_mdc = mdc_set,
79 .set_mdio_dir = mdio_dir,
80 .set_mdio_data = mdio_set,
81 .get_mdio_data = mdio_get,
84 static int __devinit mdio_gpio_bus_init(struct device *dev,
85 struct mdio_gpio_platform_data *pdata,
86 int bus_id)
88 struct mii_bus *new_bus;
89 struct mdio_gpio_info *bitbang;
90 int ret = -ENOMEM;
91 int i;
93 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
94 if (!bitbang)
95 goto out;
97 bitbang->ctrl.ops = &mdio_gpio_ops;
98 bitbang->mdc = pdata->mdc;
99 bitbang->mdio = pdata->mdio;
101 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
102 if (!new_bus)
103 goto out_free_bitbang;
105 new_bus->name = "GPIO Bitbanged MDIO",
107 ret = -ENODEV;
109 new_bus->phy_mask = pdata->phy_mask;
110 new_bus->irq = pdata->irqs;
111 new_bus->parent = dev;
113 if (new_bus->phy_mask == ~0)
114 goto out_free_bus;
116 for (i = 0; i < PHY_MAX_ADDR; i++)
117 if (!new_bus->irq[i])
118 new_bus->irq[i] = PHY_POLL;
120 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
122 if (gpio_request(bitbang->mdc, "mdc"))
123 goto out_free_bus;
125 if (gpio_request(bitbang->mdio, "mdio"))
126 goto out_free_mdc;
128 gpio_direction_output(bitbang->mdc, 0);
130 dev_set_drvdata(dev, new_bus);
132 ret = mdiobus_register(new_bus);
133 if (ret)
134 goto out_free_all;
136 return 0;
138 out_free_all:
139 dev_set_drvdata(dev, NULL);
140 gpio_free(bitbang->mdio);
141 out_free_mdc:
142 gpio_free(bitbang->mdc);
143 out_free_bus:
144 free_mdio_bitbang(new_bus);
145 out_free_bitbang:
146 kfree(bitbang);
147 out:
148 return ret;
151 static void __devexit mdio_gpio_bus_destroy(struct device *dev)
153 struct mii_bus *bus = dev_get_drvdata(dev);
154 struct mdio_gpio_info *bitbang = bus->priv;
156 mdiobus_unregister(bus);
157 free_mdio_bitbang(bus);
158 dev_set_drvdata(dev, NULL);
159 gpio_free(bitbang->mdc);
160 gpio_free(bitbang->mdio);
161 kfree(bitbang);
164 static int __devinit mdio_gpio_probe(struct platform_device *pdev)
166 struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
168 if (!pdata)
169 return -ENODEV;
171 return mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
174 static int __devexit mdio_gpio_remove(struct platform_device *pdev)
176 mdio_gpio_bus_destroy(&pdev->dev);
178 return 0;
181 #ifdef CONFIG_OF_GPIO
182 static void __devinit add_phy(struct mdio_gpio_platform_data *pdata,
183 struct device_node *np)
185 const u32 *data;
186 int len, id, irq;
188 data = of_get_property(np, "reg", &len);
189 if (!data || len != 4)
190 return;
192 id = *data;
193 pdata->phy_mask &= ~(1 << id);
195 irq = of_irq_to_resource(np, 0, NULL);
196 if (irq)
197 pdata->irqs[id] = irq;
200 static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
201 const struct of_device_id *match)
203 struct device_node *np = NULL;
204 struct mdio_gpio_platform_data *pdata;
206 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
207 if (!pdata)
208 return -ENOMEM;
210 pdata->mdc = of_get_gpio(ofdev->node, 0);
211 pdata->mdio = of_get_gpio(ofdev->node, 1);
213 if (pdata->mdc < 0 || pdata->mdio < 0)
214 goto out_free;
216 while ((np = of_get_next_child(ofdev->node, np)))
217 if (!strcmp(np->type, "ethernet-phy"))
218 add_phy(pdata, np);
220 return mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
222 out_free:
223 kfree(pdata);
224 return -ENODEV;
227 static int __devexit mdio_ofgpio_remove(struct of_device *ofdev)
229 mdio_gpio_bus_destroy(&ofdev->dev);
230 kfree(ofdev->dev.platform_data);
232 return 0;
235 static struct of_device_id mdio_ofgpio_match[] = {
237 .compatible = "virtual,mdio-gpio",
242 static struct of_platform_driver mdio_ofgpio_driver = {
243 .name = "mdio-gpio",
244 .match_table = mdio_ofgpio_match,
245 .probe = mdio_ofgpio_probe,
246 .remove = __devexit_p(mdio_ofgpio_remove),
249 static inline int __init mdio_ofgpio_init(void)
251 return of_register_platform_driver(&mdio_ofgpio_driver);
254 static inline void __exit mdio_ofgpio_exit(void)
256 of_unregister_platform_driver(&mdio_ofgpio_driver);
258 #else
259 static inline int __init mdio_ofgpio_init(void) { return 0; }
260 static inline void __exit mdio_ofgpio_exit(void) { }
261 #endif /* CONFIG_OF_GPIO */
263 static struct platform_driver mdio_gpio_driver = {
264 .probe = mdio_gpio_probe,
265 .remove = __devexit_p(mdio_gpio_remove),
266 .driver = {
267 .name = "mdio-gpio",
268 .owner = THIS_MODULE,
272 static int __init mdio_gpio_init(void)
274 int ret;
276 ret = mdio_ofgpio_init();
277 if (ret)
278 return ret;
280 ret = platform_driver_register(&mdio_gpio_driver);
281 if (ret)
282 mdio_ofgpio_exit();
284 return ret;
286 module_init(mdio_gpio_init);
288 static void __exit mdio_gpio_exit(void)
290 platform_driver_unregister(&mdio_gpio_driver);
291 mdio_ofgpio_exit();
293 module_exit(mdio_gpio_exit);
295 MODULE_ALIAS("platform:mdio-gpio");
296 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
297 MODULE_LICENSE("GPL");
298 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");