Version:  2.6.34 2.6.35 2.6.36 2.6.37 2.6.38 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14

Linux/drivers/net/phy/mdio-gpio.c

  1 /*
  2  * GPIO based MDIO bitbang driver.
  3  * Supports OpenFirmware.
  4  *
  5  * Copyright (c) 2008 CSE Semaphore Belgium.
  6  *  by Laurent Pinchart <laurentp@cse-semaphore.com>
  7  *
  8  * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  9  *
 10  * Based on earlier work by
 11  *
 12  * Copyright (c) 2003 Intracom S.A.
 13  *  by Pantelis Antoniou <panto@intracom.gr>
 14  *
 15  * 2005 (c) MontaVista Software, Inc.
 16  * Vitaly Bordug <vbordug@ru.mvista.com>
 17  *
 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.
 21  */
 22 
 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>
 29 
 30 #include <linux/of_gpio.h>
 31 #include <linux/of_mdio.h>
 32 
 33 struct mdio_gpio_info {
 34         struct mdiobb_ctrl ctrl;
 35         int mdc, mdio;
 36 };
 37 
 38 static void *mdio_gpio_of_get_data(struct platform_device *pdev)
 39 {
 40         struct device_node *np = pdev->dev.of_node;
 41         struct mdio_gpio_platform_data *pdata;
 42         int ret;
 43 
 44         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 45         if (!pdata)
 46                 return NULL;
 47 
 48         ret = of_get_gpio(np, 0);
 49         if (ret < 0)
 50                 return NULL;
 51 
 52         pdata->mdc = ret;
 53 
 54         ret = of_get_gpio(np, 1);
 55         if (ret < 0)
 56                 return NULL;
 57         pdata->mdio = ret;
 58 
 59         return pdata;
 60 }
 61 
 62 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
 63 {
 64         struct mdio_gpio_info *bitbang =
 65                 container_of(ctrl, struct mdio_gpio_info, ctrl);
 66 
 67         if (dir)
 68                 gpio_direction_output(bitbang->mdio, 1);
 69         else
 70                 gpio_direction_input(bitbang->mdio);
 71 }
 72 
 73 static int mdio_get(struct mdiobb_ctrl *ctrl)
 74 {
 75         struct mdio_gpio_info *bitbang =
 76                 container_of(ctrl, struct mdio_gpio_info, ctrl);
 77 
 78         return gpio_get_value(bitbang->mdio);
 79 }
 80 
 81 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
 82 {
 83         struct mdio_gpio_info *bitbang =
 84                 container_of(ctrl, struct mdio_gpio_info, ctrl);
 85 
 86         gpio_set_value(bitbang->mdio, what);
 87 }
 88 
 89 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
 90 {
 91         struct mdio_gpio_info *bitbang =
 92                 container_of(ctrl, struct mdio_gpio_info, ctrl);
 93 
 94         gpio_set_value(bitbang->mdc, what);
 95 }
 96 
 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,
103 };
104 
105 static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
106                                           struct mdio_gpio_platform_data *pdata,
107                                           int bus_id)
108 {
109         struct mii_bus *new_bus;
110         struct mdio_gpio_info *bitbang;
111         int i;
112 
113         bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
114         if (!bitbang)
115                 goto out;
116 
117         bitbang->ctrl.ops = &mdio_gpio_ops;
118         bitbang->ctrl.reset = pdata->reset;
119         bitbang->mdc = pdata->mdc;
120         bitbang->mdio = pdata->mdio;
121 
122         new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
123         if (!new_bus)
124                 goto out_free_bitbang;
125 
126         new_bus->name = "GPIO Bitbanged MDIO",
127 
128         new_bus->phy_mask = pdata->phy_mask;
129         new_bus->irq = pdata->irqs;
130         new_bus->parent = dev;
131 
132         if (new_bus->phy_mask == ~0)
133                 goto out_free_bus;
134 
135         for (i = 0; i < PHY_MAX_ADDR; i++)
136                 if (!new_bus->irq[i])
137                         new_bus->irq[i] = PHY_POLL;
138 
139         snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
140 
141         if (gpio_request(bitbang->mdc, "mdc"))
142                 goto out_free_bus;
143 
144         if (gpio_request(bitbang->mdio, "mdio"))
145                 goto out_free_mdc;
146 
147         gpio_direction_output(bitbang->mdc, 0);
148 
149         dev_set_drvdata(dev, new_bus);
150 
151         return new_bus;
152 
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;
161 }
162 
163 static void mdio_gpio_bus_deinit(struct device *dev)
164 {
165         struct mii_bus *bus = dev_get_drvdata(dev);
166         struct mdio_gpio_info *bitbang = bus->priv;
167 
168         dev_set_drvdata(dev, NULL);
169         gpio_free(bitbang->mdio);
170         gpio_free(bitbang->mdc);
171         free_mdio_bitbang(bus);
172         kfree(bitbang);
173 }
174 
175 static void mdio_gpio_bus_destroy(struct device *dev)
176 {
177         struct mii_bus *bus = dev_get_drvdata(dev);
178 
179         mdiobus_unregister(bus);
180         mdio_gpio_bus_deinit(dev);
181 }
182 
183 static int mdio_gpio_probe(struct platform_device *pdev)
184 {
185         struct mdio_gpio_platform_data *pdata;
186         struct mii_bus *new_bus;
187         int ret, bus_id;
188 
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;
195         }
196 
197         if (!pdata)
198                 return -ENODEV;
199 
200         new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
201         if (!new_bus)
202                 return -ENODEV;
203 
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);
208 
209         if (ret)
210                 mdio_gpio_bus_deinit(&pdev->dev);
211 
212         return ret;
213 }
214 
215 static int mdio_gpio_remove(struct platform_device *pdev)
216 {
217         mdio_gpio_bus_destroy(&pdev->dev);
218 
219         return 0;
220 }
221 
222 static struct of_device_id mdio_gpio_of_match[] = {
223         { .compatible = "virtual,mdio-gpio", },
224         { /* sentinel */ }
225 };
226 
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,
234         },
235 };
236 
237 module_platform_driver(mdio_gpio_driver);
238 
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");
243 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us