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/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/gpio.h>
29 #include <linux/mdio-gpio.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_mdio.h>
34 #include <linux/of_platform.h>
37 struct mdio_gpio_info
{
38 struct mdiobb_ctrl ctrl
;
42 static void mdio_dir(struct mdiobb_ctrl
*ctrl
, int dir
)
44 struct mdio_gpio_info
*bitbang
=
45 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
48 gpio_direction_output(bitbang
->mdio
, 1);
50 gpio_direction_input(bitbang
->mdio
);
53 static int mdio_get(struct mdiobb_ctrl
*ctrl
)
55 struct mdio_gpio_info
*bitbang
=
56 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
58 return gpio_get_value(bitbang
->mdio
);
61 static void mdio_set(struct mdiobb_ctrl
*ctrl
, int what
)
63 struct mdio_gpio_info
*bitbang
=
64 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
66 gpio_set_value(bitbang
->mdio
, what
);
69 static void mdc_set(struct mdiobb_ctrl
*ctrl
, int what
)
71 struct mdio_gpio_info
*bitbang
=
72 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
74 gpio_set_value(bitbang
->mdc
, what
);
77 static struct mdiobb_ops mdio_gpio_ops
= {
80 .set_mdio_dir
= mdio_dir
,
81 .set_mdio_data
= mdio_set
,
82 .get_mdio_data
= mdio_get
,
85 static struct mii_bus
* __devinit
mdio_gpio_bus_init(struct device
*dev
,
86 struct mdio_gpio_platform_data
*pdata
,
89 struct mii_bus
*new_bus
;
90 struct mdio_gpio_info
*bitbang
;
93 bitbang
= kzalloc(sizeof(*bitbang
), GFP_KERNEL
);
97 bitbang
->ctrl
.ops
= &mdio_gpio_ops
;
98 bitbang
->mdc
= pdata
->mdc
;
99 bitbang
->mdio
= pdata
->mdio
;
101 new_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
103 goto out_free_bitbang
;
105 new_bus
->name
= "GPIO Bitbanged MDIO",
107 new_bus
->phy_mask
= pdata
->phy_mask
;
108 new_bus
->irq
= pdata
->irqs
;
109 new_bus
->parent
= dev
;
111 if (new_bus
->phy_mask
== ~0)
114 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
115 if (!new_bus
->irq
[i
])
116 new_bus
->irq
[i
] = PHY_POLL
;
118 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "%x", bus_id
);
120 if (gpio_request(bitbang
->mdc
, "mdc"))
123 if (gpio_request(bitbang
->mdio
, "mdio"))
126 gpio_direction_output(bitbang
->mdc
, 0);
128 dev_set_drvdata(dev
, new_bus
);
133 gpio_free(bitbang
->mdc
);
135 free_mdio_bitbang(new_bus
);
142 static void mdio_gpio_bus_deinit(struct device
*dev
)
144 struct mii_bus
*bus
= dev_get_drvdata(dev
);
145 struct mdio_gpio_info
*bitbang
= bus
->priv
;
147 dev_set_drvdata(dev
, NULL
);
148 gpio_free(bitbang
->mdio
);
149 gpio_free(bitbang
->mdc
);
150 free_mdio_bitbang(bus
);
154 static void __devexit
mdio_gpio_bus_destroy(struct device
*dev
)
156 struct mii_bus
*bus
= dev_get_drvdata(dev
);
158 mdiobus_unregister(bus
);
159 mdio_gpio_bus_deinit(dev
);
162 static int __devinit
mdio_gpio_probe(struct platform_device
*pdev
)
164 struct mdio_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
165 struct mii_bus
*new_bus
;
171 new_bus
= mdio_gpio_bus_init(&pdev
->dev
, pdata
, pdev
->id
);
175 ret
= mdiobus_register(new_bus
);
177 mdio_gpio_bus_deinit(&pdev
->dev
);
182 static int __devexit
mdio_gpio_remove(struct platform_device
*pdev
)
184 mdio_gpio_bus_destroy(&pdev
->dev
);
189 #ifdef CONFIG_OF_GPIO
191 static int __devinit
mdio_ofgpio_probe(struct platform_device
*ofdev
)
193 struct mdio_gpio_platform_data
*pdata
;
194 struct mii_bus
*new_bus
;
197 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
201 ret
= of_get_gpio(ofdev
->dev
.of_node
, 0);
206 ret
= of_get_gpio(ofdev
->dev
.of_node
, 1);
211 new_bus
= mdio_gpio_bus_init(&ofdev
->dev
, pdata
, pdata
->mdc
);
215 ret
= of_mdiobus_register(new_bus
, ofdev
->dev
.of_node
);
217 mdio_gpio_bus_deinit(&ofdev
->dev
);
226 static int __devexit
mdio_ofgpio_remove(struct platform_device
*ofdev
)
228 mdio_gpio_bus_destroy(&ofdev
->dev
);
229 kfree(ofdev
->dev
.platform_data
);
234 static struct of_device_id mdio_ofgpio_match
[] = {
236 .compatible
= "virtual,mdio-gpio",
240 MODULE_DEVICE_TABLE(of
, mdio_ofgpio_match
);
242 static struct platform_driver mdio_ofgpio_driver
= {
245 .owner
= THIS_MODULE
,
246 .of_match_table
= mdio_ofgpio_match
,
248 .probe
= mdio_ofgpio_probe
,
249 .remove
= __devexit_p(mdio_ofgpio_remove
),
252 static inline int __init
mdio_ofgpio_init(void)
254 return platform_driver_register(&mdio_ofgpio_driver
);
257 static inline void __exit
mdio_ofgpio_exit(void)
259 platform_driver_unregister(&mdio_ofgpio_driver
);
262 static inline int __init
mdio_ofgpio_init(void) { return 0; }
263 static inline void __exit
mdio_ofgpio_exit(void) { }
264 #endif /* CONFIG_OF_GPIO */
266 static struct platform_driver mdio_gpio_driver
= {
267 .probe
= mdio_gpio_probe
,
268 .remove
= __devexit_p(mdio_gpio_remove
),
271 .owner
= THIS_MODULE
,
275 static int __init
mdio_gpio_init(void)
279 ret
= mdio_ofgpio_init();
283 ret
= platform_driver_register(&mdio_gpio_driver
);
289 module_init(mdio_gpio_init
);
291 static void __exit
mdio_gpio_exit(void)
293 platform_driver_unregister(&mdio_gpio_driver
);
296 module_exit(mdio_gpio_exit
);
298 MODULE_ALIAS("platform:mdio-gpio");
299 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
300 MODULE_LICENSE("GPL");
301 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");