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/platform_data/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 struct gpio_desc *mdc, *mdio, *mdo;
38 static void *mdio_gpio_of_get_data(struct device *dev)
40 struct mdio_gpio_platform_data *pdata;
42 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
46 pdata->mdc = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
47 if (IS_ERR(pdata->mdc))
48 return ERR_CAST(pdata->mdc);
50 pdata->mdio = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN);
51 if (IS_ERR(pdata->mdio))
52 return ERR_CAST(pdata->mdio);
54 pdata->mdo = devm_gpiod_get_index_optional(dev, NULL, 2, GPIOD_OUT_LOW);
55 if (IS_ERR(pdata->mdo))
56 return ERR_CAST(pdata->mdo);
61 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
63 struct mdio_gpio_info *bitbang =
64 container_of(ctrl, struct mdio_gpio_info, ctrl);
67 /* Separate output pin. Always set its value to high
68 * when changing direction. If direction is input,
69 * assume the pin serves as pull-up. If direction is
70 * output, the default value is high.
72 gpiod_set_value(bitbang->mdo, 1);
77 gpiod_direction_output(bitbang->mdio, 1);
79 gpiod_direction_input(bitbang->mdio);
82 static int mdio_get(struct mdiobb_ctrl *ctrl)
84 struct mdio_gpio_info *bitbang =
85 container_of(ctrl, struct mdio_gpio_info, ctrl);
87 return gpiod_get_value(bitbang->mdio);
90 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
92 struct mdio_gpio_info *bitbang =
93 container_of(ctrl, struct mdio_gpio_info, ctrl);
96 gpiod_set_value(bitbang->mdo, what);
98 gpiod_set_value(bitbang->mdio, what);
101 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
103 struct mdio_gpio_info *bitbang =
104 container_of(ctrl, struct mdio_gpio_info, ctrl);
106 gpiod_set_value(bitbang->mdc, what);
109 static const struct mdiobb_ops mdio_gpio_ops = {
110 .owner = THIS_MODULE,
112 .set_mdio_dir = mdio_dir,
113 .set_mdio_data = mdio_set,
114 .get_mdio_data = mdio_get,
117 static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
118 struct mdio_gpio_info *bitbang,
119 struct mdio_gpio_platform_data *pdata,
122 struct mii_bus *new_bus;
124 bitbang->ctrl.ops = &mdio_gpio_ops;
125 bitbang->mdc = pdata->mdc;
126 bitbang->mdio = pdata->mdio;
127 bitbang->mdo = pdata->mdo;
129 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
133 new_bus->name = "GPIO Bitbanged MDIO";
134 new_bus->parent = dev;
137 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
139 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
141 dev_set_drvdata(dev, new_bus);
146 static void mdio_gpio_bus_deinit(struct device *dev)
148 struct mii_bus *bus = dev_get_drvdata(dev);
150 free_mdio_bitbang(bus);
153 static void mdio_gpio_bus_destroy(struct device *dev)
155 struct mii_bus *bus = dev_get_drvdata(dev);
157 mdiobus_unregister(bus);
158 mdio_gpio_bus_deinit(dev);
161 static int mdio_gpio_probe(struct platform_device *pdev)
163 struct mdio_gpio_platform_data *pdata;
164 struct mdio_gpio_info *bitbang;
165 struct mii_bus *new_bus;
168 bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
172 if (pdev->dev.of_node) {
173 pdata = mdio_gpio_of_get_data(&pdev->dev);
174 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
176 dev_warn(&pdev->dev, "failed to get alias id\n");
180 pdata = dev_get_platdata(&pdev->dev);
187 new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, pdata, bus_id);
191 if (pdev->dev.of_node)
192 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
194 ret = mdiobus_register(new_bus);
197 mdio_gpio_bus_deinit(&pdev->dev);
202 static int mdio_gpio_remove(struct platform_device *pdev)
204 mdio_gpio_bus_destroy(&pdev->dev);
209 static const struct of_device_id mdio_gpio_of_match[] = {
210 { .compatible = "virtual,mdio-gpio", },
213 MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
215 static struct platform_driver mdio_gpio_driver = {
216 .probe = mdio_gpio_probe,
217 .remove = mdio_gpio_remove,
220 .of_match_table = mdio_gpio_of_match,
224 module_platform_driver(mdio_gpio_driver);
226 MODULE_ALIAS("platform:mdio-gpio");
227 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
228 MODULE_LICENSE("GPL");
229 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");