net: phy: mdio-gpio: Move allocation for bitbanging data
[linux-2.6-block.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 };
37
38 static void *mdio_gpio_of_get_data(struct device *dev)
39 {
40         struct mdio_gpio_platform_data *pdata;
41
42         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
43         if (!pdata)
44                 return NULL;
45
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);
49
50         pdata->mdio = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN);
51         if (IS_ERR(pdata->mdio))
52                 return ERR_CAST(pdata->mdio);
53
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);
57
58         return pdata;
59 }
60
61 static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
62 {
63         struct mdio_gpio_info *bitbang =
64                 container_of(ctrl, struct mdio_gpio_info, ctrl);
65
66         if (bitbang->mdo) {
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.
71                  */
72                 gpiod_set_value(bitbang->mdo, 1);
73                 return;
74         }
75
76         if (dir)
77                 gpiod_direction_output(bitbang->mdio, 1);
78         else
79                 gpiod_direction_input(bitbang->mdio);
80 }
81
82 static int mdio_get(struct mdiobb_ctrl *ctrl)
83 {
84         struct mdio_gpio_info *bitbang =
85                 container_of(ctrl, struct mdio_gpio_info, ctrl);
86
87         return gpiod_get_value(bitbang->mdio);
88 }
89
90 static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
91 {
92         struct mdio_gpio_info *bitbang =
93                 container_of(ctrl, struct mdio_gpio_info, ctrl);
94
95         if (bitbang->mdo)
96                 gpiod_set_value(bitbang->mdo, what);
97         else
98                 gpiod_set_value(bitbang->mdio, what);
99 }
100
101 static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
102 {
103         struct mdio_gpio_info *bitbang =
104                 container_of(ctrl, struct mdio_gpio_info, ctrl);
105
106         gpiod_set_value(bitbang->mdc, what);
107 }
108
109 static const struct mdiobb_ops mdio_gpio_ops = {
110         .owner = THIS_MODULE,
111         .set_mdc = mdc_set,
112         .set_mdio_dir = mdio_dir,
113         .set_mdio_data = mdio_set,
114         .get_mdio_data = mdio_get,
115 };
116
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,
120                                           int bus_id)
121 {
122         struct mii_bus *new_bus;
123
124         bitbang->ctrl.ops = &mdio_gpio_ops;
125         bitbang->mdc = pdata->mdc;
126         bitbang->mdio = pdata->mdio;
127         bitbang->mdo = pdata->mdo;
128
129         new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
130         if (!new_bus)
131                 return NULL;
132
133         new_bus->name = "GPIO Bitbanged MDIO";
134         new_bus->parent = dev;
135
136         if (bus_id != -1)
137                 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
138         else
139                 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
140
141         dev_set_drvdata(dev, new_bus);
142
143         return new_bus;
144 }
145
146 static void mdio_gpio_bus_deinit(struct device *dev)
147 {
148         struct mii_bus *bus = dev_get_drvdata(dev);
149
150         free_mdio_bitbang(bus);
151 }
152
153 static void mdio_gpio_bus_destroy(struct device *dev)
154 {
155         struct mii_bus *bus = dev_get_drvdata(dev);
156
157         mdiobus_unregister(bus);
158         mdio_gpio_bus_deinit(dev);
159 }
160
161 static int mdio_gpio_probe(struct platform_device *pdev)
162 {
163         struct mdio_gpio_platform_data *pdata;
164         struct mdio_gpio_info *bitbang;
165         struct mii_bus *new_bus;
166         int ret, bus_id;
167
168         bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
169         if (!bitbang)
170                 return -ENOMEM;
171
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");
175                 if (bus_id < 0) {
176                         dev_warn(&pdev->dev, "failed to get alias id\n");
177                         bus_id = 0;
178                 }
179         } else {
180                 pdata = dev_get_platdata(&pdev->dev);
181                 bus_id = pdev->id;
182         }
183
184         if (!pdata)
185                 return -ENODEV;
186
187         new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, pdata, bus_id);
188         if (!new_bus)
189                 return -ENODEV;
190
191         if (pdev->dev.of_node)
192                 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
193         else
194                 ret = mdiobus_register(new_bus);
195
196         if (ret)
197                 mdio_gpio_bus_deinit(&pdev->dev);
198
199         return ret;
200 }
201
202 static int mdio_gpio_remove(struct platform_device *pdev)
203 {
204         mdio_gpio_bus_destroy(&pdev->dev);
205
206         return 0;
207 }
208
209 static const struct of_device_id mdio_gpio_of_match[] = {
210         { .compatible = "virtual,mdio-gpio", },
211         { /* sentinel */ }
212 };
213 MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
214
215 static struct platform_driver mdio_gpio_driver = {
216         .probe = mdio_gpio_probe,
217         .remove = mdio_gpio_remove,
218         .driver         = {
219                 .name   = "mdio-gpio",
220                 .of_match_table = mdio_gpio_of_match,
221         },
222 };
223
224 module_platform_driver(mdio_gpio_driver);
225
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");