Merge branch 'fib_validate_loopback'
[deliverable/linux.git] / drivers / net / phy / mdio-gpio.c
CommitLineData
a5edeccb 1/*
f004f3ea
PZ
2 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
a5edeccb
LP
4 *
5 * Copyright (c) 2008 CSE Semaphore Belgium.
6 * by Laurent Pinchart <laurentp@cse-semaphore.com>
7 *
f004f3ea
PZ
8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
9 *
a5edeccb
LP
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>
a5edeccb 25#include <linux/interrupt.h>
f004f3ea
PZ
26#include <linux/platform_device.h>
27#include <linux/gpio.h>
28#include <linux/mdio-gpio.h>
29
a5edeccb 30#include <linux/of_gpio.h>
dacac4da 31#include <linux/of_mdio.h>
a5edeccb
LP
32
33struct mdio_gpio_info {
34 struct mdiobb_ctrl ctrl;
35 int mdc, mdio;
36};
37
e92bdf4b
SK
38static void *mdio_gpio_of_get_data(struct platform_device *pdev)
39{
40 struct device_node *np = pdev->dev.of_node;
41 struct mdio_gpio_platform_data *pdata;
42 int ret;
43
44 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
45 if (!pdata)
46 return NULL;
47
48 ret = of_get_gpio(np, 0);
49 if (ret < 0)
50 return NULL;
51
52 pdata->mdc = ret;
53
54 ret = of_get_gpio(np, 1);
55 if (ret < 0)
56 return NULL;
57 pdata->mdio = ret;
58
59 return pdata;
60}
61
a5edeccb
LP
62static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
63{
64 struct mdio_gpio_info *bitbang =
65 container_of(ctrl, struct mdio_gpio_info, ctrl);
66
67 if (dir)
68 gpio_direction_output(bitbang->mdio, 1);
69 else
70 gpio_direction_input(bitbang->mdio);
71}
72
f004f3ea 73static int mdio_get(struct mdiobb_ctrl *ctrl)
a5edeccb
LP
74{
75 struct mdio_gpio_info *bitbang =
76 container_of(ctrl, struct mdio_gpio_info, ctrl);
77
78 return gpio_get_value(bitbang->mdio);
79}
80
f004f3ea 81static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
a5edeccb
LP
82{
83 struct mdio_gpio_info *bitbang =
84 container_of(ctrl, struct mdio_gpio_info, ctrl);
85
86 gpio_set_value(bitbang->mdio, what);
87}
88
f004f3ea 89static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
a5edeccb
LP
90{
91 struct mdio_gpio_info *bitbang =
92 container_of(ctrl, struct mdio_gpio_info, ctrl);
93
94 gpio_set_value(bitbang->mdc, what);
95}
96
97static struct mdiobb_ops mdio_gpio_ops = {
98 .owner = THIS_MODULE,
f004f3ea 99 .set_mdc = mdc_set,
a5edeccb 100 .set_mdio_dir = mdio_dir,
f004f3ea
PZ
101 .set_mdio_data = mdio_set,
102 .get_mdio_data = mdio_get,
a5edeccb
LP
103};
104
633d1594 105static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
1dd06ae8
GKH
106 struct mdio_gpio_platform_data *pdata,
107 int bus_id)
a5edeccb 108{
a5edeccb
LP
109 struct mii_bus *new_bus;
110 struct mdio_gpio_info *bitbang;
a5edeccb
LP
111 int i;
112
f004f3ea 113 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
a5edeccb
LP
114 if (!bitbang)
115 goto out;
116
117 bitbang->ctrl.ops = &mdio_gpio_ops;
64882709 118 bitbang->ctrl.reset = pdata->reset;
f004f3ea
PZ
119 bitbang->mdc = pdata->mdc;
120 bitbang->mdio = pdata->mdio;
a5edeccb
LP
121
122 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
123 if (!new_bus)
298cf9be 124 goto out_free_bitbang;
a5edeccb 125
f004f3ea 126 new_bus->name = "GPIO Bitbanged MDIO",
a5edeccb 127
f004f3ea
PZ
128 new_bus->phy_mask = pdata->phy_mask;
129 new_bus->irq = pdata->irqs;
130 new_bus->parent = dev;
131
132 if (new_bus->phy_mask == ~0)
a5edeccb
LP
133 goto out_free_bus;
134
135 for (i = 0; i < PHY_MAX_ADDR; i++)
f004f3ea
PZ
136 if (!new_bus->irq[i])
137 new_bus->irq[i] = PHY_POLL;
a5edeccb 138
a77e929a 139 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
f004f3ea
PZ
140
141 if (gpio_request(bitbang->mdc, "mdc"))
142 goto out_free_bus;
a5edeccb 143
f004f3ea
PZ
144 if (gpio_request(bitbang->mdio, "mdio"))
145 goto out_free_mdc;
146
664f93b4
PZ
147 gpio_direction_output(bitbang->mdc, 0);
148
f004f3ea 149 dev_set_drvdata(dev, new_bus);
a5edeccb 150
dacac4da 151 return new_bus;
a5edeccb 152
f004f3ea
PZ
153out_free_mdc:
154 gpio_free(bitbang->mdc);
a5edeccb 155out_free_bus:
a5edeccb 156 free_mdio_bitbang(new_bus);
298cf9be
LB
157out_free_bitbang:
158 kfree(bitbang);
a5edeccb 159out:
dacac4da 160 return NULL;
a5edeccb
LP
161}
162
f99b4a02 163static void mdio_gpio_bus_deinit(struct device *dev)
a5edeccb 164{
f004f3ea 165 struct mii_bus *bus = dev_get_drvdata(dev);
a5edeccb
LP
166 struct mdio_gpio_info *bitbang = bus->priv;
167
f004f3ea 168 dev_set_drvdata(dev, NULL);
f004f3ea 169 gpio_free(bitbang->mdio);
dacac4da
MW
170 gpio_free(bitbang->mdc);
171 free_mdio_bitbang(bus);
a5edeccb 172 kfree(bitbang);
f004f3ea
PZ
173}
174
633d1594 175static void mdio_gpio_bus_destroy(struct device *dev)
dacac4da
MW
176{
177 struct mii_bus *bus = dev_get_drvdata(dev);
178
179 mdiobus_unregister(bus);
180 mdio_gpio_bus_deinit(dev);
181}
182
633d1594 183static int mdio_gpio_probe(struct platform_device *pdev)
f004f3ea 184{
e92bdf4b 185 struct mdio_gpio_platform_data *pdata;
dacac4da 186 struct mii_bus *new_bus;
3272dd9b 187 int ret, bus_id;
f004f3ea 188
3272dd9b 189 if (pdev->dev.of_node) {
e92bdf4b 190 pdata = mdio_gpio_of_get_data(pdev);
3272dd9b
SK
191 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
192 } else {
9bc7b1cd 193 pdata = dev_get_platdata(&pdev->dev);
3272dd9b
SK
194 bus_id = pdev->id;
195 }
e92bdf4b 196
f004f3ea
PZ
197 if (!pdata)
198 return -ENODEV;
199
3272dd9b 200 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
dacac4da
MW
201 if (!new_bus)
202 return -ENODEV;
203
e92bdf4b
SK
204 if (pdev->dev.of_node)
205 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
206 else
207 ret = mdiobus_register(new_bus);
208
dacac4da
MW
209 if (ret)
210 mdio_gpio_bus_deinit(&pdev->dev);
211
212 return ret;
f004f3ea
PZ
213}
214
633d1594 215static int mdio_gpio_remove(struct platform_device *pdev)
f004f3ea
PZ
216{
217 mdio_gpio_bus_destroy(&pdev->dev);
218
219 return 0;
220}
221
e92bdf4b
SK
222static struct of_device_id mdio_gpio_of_match[] = {
223 { .compatible = "virtual,mdio-gpio", },
224 { /* sentinel */ }
a5edeccb
LP
225};
226
f004f3ea
PZ
227static struct platform_driver mdio_gpio_driver = {
228 .probe = mdio_gpio_probe,
633d1594 229 .remove = mdio_gpio_remove,
f004f3ea
PZ
230 .driver = {
231 .name = "mdio-gpio",
232 .owner = THIS_MODULE,
e92bdf4b 233 .of_match_table = mdio_gpio_of_match,
f004f3ea
PZ
234 },
235};
236
f8e5fc8c 237module_platform_driver(mdio_gpio_driver);
a5edeccb 238
f004f3ea
PZ
239MODULE_ALIAS("platform:mdio-gpio");
240MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
241MODULE_LICENSE("GPL");
242MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");
This page took 0.49364 seconds and 5 git commands to generate.