Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / phy / mdio-gpio.c
blob0fbcedcdf6e2ae5b6d9d8ecca66937532db263cb
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/mdio-bitbang.h>
28 #include <linux/mdio-gpio.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/of_mdio.h>
32 struct mdio_gpio_info {
33 struct mdiobb_ctrl ctrl;
34 struct gpio_desc *mdc, *mdio, *mdo;
37 static int mdio_gpio_get_data(struct device *dev,
38 struct mdio_gpio_info *bitbang)
40 bitbang->mdc = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDC,
41 GPIOD_OUT_LOW);
42 if (IS_ERR(bitbang->mdc))
43 return PTR_ERR(bitbang->mdc);
45 bitbang->mdio = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDIO,
46 GPIOD_IN);
47 if (IS_ERR(bitbang->mdio))
48 return PTR_ERR(bitbang->mdio);
50 bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, MDIO_GPIO_MDO,
51 GPIOD_OUT_LOW);
52 return PTR_ERR_OR_ZERO(bitbang->mdo);
55 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
57 struct mdio_gpio_info *bitbang =
58 container_of(ctrl, struct mdio_gpio_info, ctrl);
60 if (bitbang->mdo) {
61 /* Separate output pin. Always set its value to high
62 * when changing direction. If direction is input,
63 * assume the pin serves as pull-up. If direction is
64 * output, the default value is high.
66 gpiod_set_value_cansleep(bitbang->mdo, 1);
67 return;
70 if (dir)
71 gpiod_direction_output(bitbang->mdio, 1);
72 else
73 gpiod_direction_input(bitbang->mdio);
76 static int mdio_get(struct mdiobb_ctrl *ctrl)
78 struct mdio_gpio_info *bitbang =
79 container_of(ctrl, struct mdio_gpio_info, ctrl);
81 return gpiod_get_value_cansleep(bitbang->mdio);
84 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
86 struct mdio_gpio_info *bitbang =
87 container_of(ctrl, struct mdio_gpio_info, ctrl);
89 if (bitbang->mdo)
90 gpiod_set_value_cansleep(bitbang->mdo, what);
91 else
92 gpiod_set_value_cansleep(bitbang->mdio, what);
95 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
97 struct mdio_gpio_info *bitbang =
98 container_of(ctrl, struct mdio_gpio_info, ctrl);
100 gpiod_set_value_cansleep(bitbang->mdc, what);
103 static const struct mdiobb_ops mdio_gpio_ops = {
104 .owner = THIS_MODULE,
105 .set_mdc = mdc_set,
106 .set_mdio_dir = mdio_dir,
107 .set_mdio_data = mdio_set,
108 .get_mdio_data = mdio_get,
111 static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
112 struct mdio_gpio_info *bitbang,
113 int bus_id)
115 struct mii_bus *new_bus;
117 bitbang->ctrl.ops = &mdio_gpio_ops;
119 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
120 if (!new_bus)
121 return NULL;
123 new_bus->name = "GPIO Bitbanged MDIO";
124 new_bus->parent = dev;
126 if (bus_id != -1)
127 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
128 else
129 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
131 dev_set_drvdata(dev, new_bus);
133 return new_bus;
136 static void mdio_gpio_bus_deinit(struct device *dev)
138 struct mii_bus *bus = dev_get_drvdata(dev);
140 free_mdio_bitbang(bus);
143 static void mdio_gpio_bus_destroy(struct device *dev)
145 struct mii_bus *bus = dev_get_drvdata(dev);
147 mdiobus_unregister(bus);
148 mdio_gpio_bus_deinit(dev);
151 static int mdio_gpio_probe(struct platform_device *pdev)
153 struct mdio_gpio_info *bitbang;
154 struct mii_bus *new_bus;
155 int ret, bus_id;
157 bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
158 if (!bitbang)
159 return -ENOMEM;
161 ret = mdio_gpio_get_data(&pdev->dev, bitbang);
162 if (ret)
163 return ret;
165 if (pdev->dev.of_node) {
166 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
167 if (bus_id < 0) {
168 dev_warn(&pdev->dev, "failed to get alias id\n");
169 bus_id = 0;
171 } else {
172 bus_id = pdev->id;
175 new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, bus_id);
176 if (!new_bus)
177 return -ENODEV;
179 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
180 if (ret)
181 mdio_gpio_bus_deinit(&pdev->dev);
183 return ret;
186 static int mdio_gpio_remove(struct platform_device *pdev)
188 mdio_gpio_bus_destroy(&pdev->dev);
190 return 0;
193 static const struct of_device_id mdio_gpio_of_match[] = {
194 { .compatible = "virtual,mdio-gpio", },
195 { /* sentinel */ }
197 MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
199 static struct platform_driver mdio_gpio_driver = {
200 .probe = mdio_gpio_probe,
201 .remove = mdio_gpio_remove,
202 .driver = {
203 .name = "mdio-gpio",
204 .of_match_table = mdio_gpio_of_match,
208 module_platform_driver(mdio_gpio_driver);
210 MODULE_ALIAS("platform:mdio-gpio");
211 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
212 MODULE_LICENSE("GPL");
213 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");