2 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
5 * Copyright (c) 2008 CSE Semaphore Belgium.
10 * Based on earlier work by
12 * Copyright (c) 2003 Intracom S.A.
15 * 2005 (c) MontaVista Software, Inc.
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;
36 int mdc_active_low, mdio_active_low, mdo_active_low;
39 static void *mdio_gpio_of_get_data(struct platform_device *pdev)
41 struct device_node *np = pdev->dev.of_node;
42 struct mdio_gpio_platform_data *pdata;
43 enum of_gpio_flags flags;
46 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
50 ret = of_get_gpio_flags(np, 0, &flags);
55 pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
57 ret = of_get_gpio_flags(np, 1, &flags);
61 pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
63 ret = of_get_gpio_flags(np, 2, &flags);
66 pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW;
72 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
74 struct mdio_gpio_info *bitbang =
75 container_of(ctrl, struct mdio_gpio_info, ctrl);
78 /* Separate output pin. Always set its value to high
79 * when changing direction. If direction is input,
80 * assume the pin serves as pull-up. If direction is
81 * output, the default value is high.
83 gpio_set_value(bitbang->mdo, 1 ^ bitbang->mdo_active_low);
88 gpio_direction_output(bitbang->mdio,
89 1 ^ bitbang->mdio_active_low);
91 gpio_direction_input(bitbang->mdio);
94 static int mdio_get(struct mdiobb_ctrl *ctrl)
96 struct mdio_gpio_info *bitbang =
97 container_of(ctrl, struct mdio_gpio_info, ctrl);
99 return gpio_get_value(bitbang->mdio) ^ bitbang->mdio_active_low;
102 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
104 struct mdio_gpio_info *bitbang =
105 container_of(ctrl, struct mdio_gpio_info, ctrl);
108 gpio_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low);
110 gpio_set_value(bitbang->mdio, what ^ bitbang->mdio_active_low);
113 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
115 struct mdio_gpio_info *bitbang =
116 container_of(ctrl, struct mdio_gpio_info, ctrl);
118 gpio_set_value(bitbang->mdc, what ^ bitbang->mdc_active_low);
121 static struct mdiobb_ops mdio_gpio_ops = {
122 .owner = THIS_MODULE,
124 .set_mdio_dir = mdio_dir,
125 .set_mdio_data = mdio_set,
126 .get_mdio_data = mdio_get,
129 static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
130 struct mdio_gpio_platform_data *pdata,
133 struct mii_bus *new_bus;
134 struct mdio_gpio_info *bitbang;
137 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
141 bitbang->ctrl.ops = &mdio_gpio_ops;
142 bitbang->ctrl.reset = pdata->reset;
143 bitbang->mdc = pdata->mdc;
144 bitbang->mdc_active_low = pdata->mdc_active_low;
145 bitbang->mdio = pdata->mdio;
146 bitbang->mdio_active_low = pdata->mdio_active_low;
147 bitbang->mdo = pdata->mdo;
148 bitbang->mdo_active_low = pdata->mdo_active_low;
150 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
154 new_bus->name = "GPIO Bitbanged MDIO",
156 new_bus->phy_mask = pdata->phy_mask;
157 new_bus->irq = pdata->irqs;
158 new_bus->parent = dev;
160 if (new_bus->phy_mask == ~0)
163 for (i = 0; i < PHY_MAX_ADDR; i++)
164 if (!new_bus->irq[i])
165 new_bus->irq[i] = PHY_POLL;
167 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
169 if (devm_gpio_request(dev, bitbang->mdc, "mdc"))
172 if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
176 if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
178 gpio_direction_output(bitbang->mdo, 1);
179 gpio_direction_input(bitbang->mdio);
182 gpio_direction_output(bitbang->mdc, 0);
184 dev_set_drvdata(dev, new_bus);
189 free_mdio_bitbang(new_bus);
194 static void mdio_gpio_bus_deinit(struct device *dev)
196 struct mii_bus *bus = dev_get_drvdata(dev);
198 free_mdio_bitbang(bus);
201 static void mdio_gpio_bus_destroy(struct device *dev)
203 struct mii_bus *bus = dev_get_drvdata(dev);
205 mdiobus_unregister(bus);
206 mdio_gpio_bus_deinit(dev);
209 static int mdio_gpio_probe(struct platform_device *pdev)
211 struct mdio_gpio_platform_data *pdata;
212 struct mii_bus *new_bus;
215 if (pdev->dev.of_node) {
216 pdata = mdio_gpio_of_get_data(pdev);
217 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
219 pdata = dev_get_platdata(&pdev->dev);
226 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
230 if (pdev->dev.of_node)
231 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
233 ret = mdiobus_register(new_bus);
236 mdio_gpio_bus_deinit(&pdev->dev);
241 static int mdio_gpio_remove(struct platform_device *pdev)
243 mdio_gpio_bus_destroy(&pdev->dev);
248 static struct of_device_id mdio_gpio_of_match[] = {
249 { .compatible = "virtual,mdio-gpio", },
253 static struct platform_driver mdio_gpio_driver = {
254 .probe = mdio_gpio_probe,
255 .remove = mdio_gpio_remove,
258 .owner = THIS_MODULE,
259 .of_match_table = mdio_gpio_of_match,
263 module_platform_driver(mdio_gpio_driver);
265 MODULE_ALIAS("platform:mdio-gpio");
266 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
267 MODULE_LICENSE("GPL");
268 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");