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/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
;
38 static void *mdio_gpio_of_get_data(struct platform_device
*pdev
)
40 struct device_node
*np
= pdev
->dev
.of_node
;
41 struct mdio_gpio_platform_data
*pdata
;
44 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
48 ret
= of_get_gpio(np
, 0);
54 ret
= of_get_gpio(np
, 1);
62 static void mdio_dir(struct mdiobb_ctrl
*ctrl
, int dir
)
64 struct mdio_gpio_info
*bitbang
=
65 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
68 gpio_direction_output(bitbang
->mdio
, 1);
70 gpio_direction_input(bitbang
->mdio
);
73 static int mdio_get(struct mdiobb_ctrl
*ctrl
)
75 struct mdio_gpio_info
*bitbang
=
76 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
78 return gpio_get_value(bitbang
->mdio
);
81 static void mdio_set(struct mdiobb_ctrl
*ctrl
, int what
)
83 struct mdio_gpio_info
*bitbang
=
84 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
86 gpio_set_value(bitbang
->mdio
, what
);
89 static void mdc_set(struct mdiobb_ctrl
*ctrl
, int what
)
91 struct mdio_gpio_info
*bitbang
=
92 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
94 gpio_set_value(bitbang
->mdc
, what
);
97 static struct mdiobb_ops mdio_gpio_ops
= {
100 .set_mdio_dir
= mdio_dir
,
101 .set_mdio_data
= mdio_set
,
102 .get_mdio_data
= mdio_get
,
105 static struct mii_bus
*mdio_gpio_bus_init(struct device
*dev
,
106 struct mdio_gpio_platform_data
*pdata
,
109 struct mii_bus
*new_bus
;
110 struct mdio_gpio_info
*bitbang
;
113 bitbang
= kzalloc(sizeof(*bitbang
), GFP_KERNEL
);
117 bitbang
->ctrl
.ops
= &mdio_gpio_ops
;
118 bitbang
->ctrl
.reset
= pdata
->reset
;
119 bitbang
->mdc
= pdata
->mdc
;
120 bitbang
->mdio
= pdata
->mdio
;
122 new_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
124 goto out_free_bitbang
;
126 new_bus
->name
= "GPIO Bitbanged MDIO",
128 new_bus
->phy_mask
= pdata
->phy_mask
;
129 new_bus
->irq
= pdata
->irqs
;
130 new_bus
->parent
= dev
;
132 if (new_bus
->phy_mask
== ~0)
135 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
136 if (!new_bus
->irq
[i
])
137 new_bus
->irq
[i
] = PHY_POLL
;
139 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "gpio-%x", bus_id
);
141 if (gpio_request(bitbang
->mdc
, "mdc"))
144 if (gpio_request(bitbang
->mdio
, "mdio"))
147 gpio_direction_output(bitbang
->mdc
, 0);
149 dev_set_drvdata(dev
, new_bus
);
154 gpio_free(bitbang
->mdc
);
156 free_mdio_bitbang(new_bus
);
163 static void mdio_gpio_bus_deinit(struct device
*dev
)
165 struct mii_bus
*bus
= dev_get_drvdata(dev
);
166 struct mdio_gpio_info
*bitbang
= bus
->priv
;
168 dev_set_drvdata(dev
, NULL
);
169 gpio_free(bitbang
->mdio
);
170 gpio_free(bitbang
->mdc
);
171 free_mdio_bitbang(bus
);
175 static void mdio_gpio_bus_destroy(struct device
*dev
)
177 struct mii_bus
*bus
= dev_get_drvdata(dev
);
179 mdiobus_unregister(bus
);
180 mdio_gpio_bus_deinit(dev
);
183 static int mdio_gpio_probe(struct platform_device
*pdev
)
185 struct mdio_gpio_platform_data
*pdata
;
186 struct mii_bus
*new_bus
;
189 if (pdev
->dev
.of_node
) {
190 pdata
= mdio_gpio_of_get_data(pdev
);
191 bus_id
= of_alias_get_id(pdev
->dev
.of_node
, "mdio-gpio");
193 pdata
= dev_get_platdata(&pdev
->dev
);
200 new_bus
= mdio_gpio_bus_init(&pdev
->dev
, pdata
, bus_id
);
204 if (pdev
->dev
.of_node
)
205 ret
= of_mdiobus_register(new_bus
, pdev
->dev
.of_node
);
207 ret
= mdiobus_register(new_bus
);
210 mdio_gpio_bus_deinit(&pdev
->dev
);
215 static int mdio_gpio_remove(struct platform_device
*pdev
)
217 mdio_gpio_bus_destroy(&pdev
->dev
);
222 static struct of_device_id mdio_gpio_of_match
[] = {
223 { .compatible
= "virtual,mdio-gpio", },
227 static struct platform_driver mdio_gpio_driver
= {
228 .probe
= mdio_gpio_probe
,
229 .remove
= mdio_gpio_remove
,
232 .owner
= THIS_MODULE
,
233 .of_match_table
= mdio_gpio_of_match
,
237 module_platform_driver(mdio_gpio_driver
);
239 MODULE_ALIAS("platform:mdio-gpio");
240 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
241 MODULE_LICENSE("GPL");
242 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");