]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/phy/mdio-gpio.c
net: mdio-gpio: Convert to use gpiod functions where possible
[linux.git] / 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/platform_data/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         struct gpio_desc *mdc, *mdio, *mdo;
36         int mdc_active_low, mdio_active_low, mdo_active_low;
37 };
38
39 static void *mdio_gpio_of_get_data(struct platform_device *pdev)
40 {
41         struct device_node *np = pdev->dev.of_node;
42         struct mdio_gpio_platform_data *pdata;
43         enum of_gpio_flags flags;
44         int ret;
45
46         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
47         if (!pdata)
48                 return NULL;
49
50         ret = of_get_gpio_flags(np, 0, &flags);
51         if (ret < 0)
52                 return NULL;
53
54         pdata->mdc = ret;
55         pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
56
57         ret = of_get_gpio_flags(np, 1, &flags);
58         if (ret < 0)
59                 return NULL;
60         pdata->mdio = ret;
61         pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
62
63         ret = of_get_gpio_flags(np, 2, &flags);
64         if (ret > 0) {
65                 pdata->mdo = ret;
66                 pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW;
67         }
68
69         return pdata;
70 }
71
72 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
73 {
74         struct mdio_gpio_info *bitbang =
75                 container_of(ctrl, struct mdio_gpio_info, ctrl);
76
77         if (bitbang->mdo) {
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.
82                  */
83                 gpiod_set_value(bitbang->mdo, 1 ^ bitbang->mdo_active_low);
84                 return;
85         }
86
87         if (dir)
88                 gpiod_direction_output(bitbang->mdio,
89                                        1 ^ bitbang->mdio_active_low);
90         else
91                 gpiod_direction_input(bitbang->mdio);
92 }
93
94 static int mdio_get(struct mdiobb_ctrl *ctrl)
95 {
96         struct mdio_gpio_info *bitbang =
97                 container_of(ctrl, struct mdio_gpio_info, ctrl);
98
99         return gpiod_get_value(bitbang->mdio) ^ bitbang->mdio_active_low;
100 }
101
102 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
103 {
104         struct mdio_gpio_info *bitbang =
105                 container_of(ctrl, struct mdio_gpio_info, ctrl);
106
107         if (bitbang->mdo)
108                 gpiod_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low);
109         else
110                 gpiod_set_value(bitbang->mdio, what ^ bitbang->mdio_active_low);
111 }
112
113 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
114 {
115         struct mdio_gpio_info *bitbang =
116                 container_of(ctrl, struct mdio_gpio_info, ctrl);
117
118         gpiod_set_value(bitbang->mdc, what ^ bitbang->mdc_active_low);
119 }
120
121 static struct mdiobb_ops mdio_gpio_ops = {
122         .owner = THIS_MODULE,
123         .set_mdc = mdc_set,
124         .set_mdio_dir = mdio_dir,
125         .set_mdio_data = mdio_set,
126         .get_mdio_data = mdio_get,
127 };
128
129 static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
130                                           struct mdio_gpio_platform_data *pdata,
131                                           int bus_id)
132 {
133         struct mii_bus *new_bus;
134         struct mdio_gpio_info *bitbang;
135         int i;
136         int mdc, mdio, mdo;
137         unsigned long mdc_flags = GPIOF_OUT_INIT_LOW;
138         unsigned long mdio_flags = GPIOF_DIR_IN;
139         unsigned long mdo_flags = GPIOF_OUT_INIT_HIGH;
140
141         bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
142         if (!bitbang)
143                 goto out;
144
145         bitbang->ctrl.ops = &mdio_gpio_ops;
146         bitbang->ctrl.reset = pdata->reset;
147         mdc = pdata->mdc;
148         bitbang->mdc = gpio_to_desc(mdc);
149         bitbang->mdc_active_low = pdata->mdc_active_low;
150         mdio = pdata->mdio;
151         bitbang->mdio = gpio_to_desc(mdio);
152         bitbang->mdio_active_low = pdata->mdio_active_low;
153         mdo = pdata->mdo;
154         if (mdo)
155                 bitbang->mdo = gpio_to_desc(mdo);
156         bitbang->mdo_active_low = pdata->mdo_active_low;
157
158         new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
159         if (!new_bus)
160                 goto out;
161
162         new_bus->name = "GPIO Bitbanged MDIO",
163
164         new_bus->phy_mask = pdata->phy_mask;
165         new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
166         memcpy(new_bus->irq, pdata->irqs, sizeof(new_bus->irq));
167         new_bus->parent = dev;
168
169         if (new_bus->phy_mask == ~0)
170                 goto out_free_bus;
171
172         for (i = 0; i < PHY_MAX_ADDR; i++)
173                 if (!new_bus->irq[i])
174                         new_bus->irq[i] = PHY_POLL;
175
176         if (bus_id != -1)
177                 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
178         else
179                 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
180
181         if (devm_gpio_request_one(dev, mdc, mdc_flags, "mdc"))
182                 goto out_free_bus;
183
184         if (devm_gpio_request_one(dev, mdio, mdio_flags, "mdio"))
185                 goto out_free_bus;
186
187         if (mdo && devm_gpio_request_one(dev, mdo, mdo_flags, "mdo"))
188                 goto out_free_bus;
189
190         dev_set_drvdata(dev, new_bus);
191
192         return new_bus;
193
194 out_free_bus:
195         free_mdio_bitbang(new_bus);
196 out:
197         return NULL;
198 }
199
200 static void mdio_gpio_bus_deinit(struct device *dev)
201 {
202         struct mii_bus *bus = dev_get_drvdata(dev);
203
204         free_mdio_bitbang(bus);
205 }
206
207 static void mdio_gpio_bus_destroy(struct device *dev)
208 {
209         struct mii_bus *bus = dev_get_drvdata(dev);
210
211         mdiobus_unregister(bus);
212         mdio_gpio_bus_deinit(dev);
213 }
214
215 static int mdio_gpio_probe(struct platform_device *pdev)
216 {
217         struct mdio_gpio_platform_data *pdata;
218         struct mii_bus *new_bus;
219         int ret, bus_id;
220
221         if (pdev->dev.of_node) {
222                 pdata = mdio_gpio_of_get_data(pdev);
223                 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
224                 if (bus_id < 0) {
225                         dev_warn(&pdev->dev, "failed to get alias id\n");
226                         bus_id = 0;
227                 }
228         } else {
229                 pdata = dev_get_platdata(&pdev->dev);
230                 bus_id = pdev->id;
231         }
232
233         if (!pdata)
234                 return -ENODEV;
235
236         new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
237         if (!new_bus)
238                 return -ENODEV;
239
240         if (pdev->dev.of_node)
241                 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
242         else
243                 ret = mdiobus_register(new_bus);
244
245         if (ret)
246                 mdio_gpio_bus_deinit(&pdev->dev);
247
248         return ret;
249 }
250
251 static int mdio_gpio_remove(struct platform_device *pdev)
252 {
253         mdio_gpio_bus_destroy(&pdev->dev);
254
255         return 0;
256 }
257
258 static const struct of_device_id mdio_gpio_of_match[] = {
259         { .compatible = "virtual,mdio-gpio", },
260         { /* sentinel */ }
261 };
262 MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
263
264 static struct platform_driver mdio_gpio_driver = {
265         .probe = mdio_gpio_probe,
266         .remove = mdio_gpio_remove,
267         .driver         = {
268                 .name   = "mdio-gpio",
269                 .of_match_table = mdio_gpio_of_match,
270         },
271 };
272
273 module_platform_driver(mdio_gpio_driver);
274
275 MODULE_ALIAS("platform:mdio-gpio");
276 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
277 MODULE_LICENSE("GPL");
278 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");