2008-05-26 09:53:21 +00:00
|
|
|
/*
|
2008-11-14 00:24:34 +00:00
|
|
|
* GPIO based MDIO bitbang driver.
|
|
|
|
* Supports OpenFirmware.
|
2008-05-26 09:53:21 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) 2008 CSE Semaphore Belgium.
|
|
|
|
* by Laurent Pinchart <laurentp@cse-semaphore.com>
|
|
|
|
*
|
2008-11-14 00:24:34 +00:00
|
|
|
* Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
|
|
|
|
*
|
2008-05-26 09:53:21 +00:00
|
|
|
* Based on earlier work by
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Intracom S.A.
|
|
|
|
* by Pantelis Antoniou <panto@intracom.gr>
|
|
|
|
*
|
|
|
|
* 2005 (c) MontaVista Software, Inc.
|
|
|
|
* Vitaly Bordug <vbordug@ru.mvista.com>
|
|
|
|
*
|
|
|
|
* This file is licensed under the terms of the GNU General Public License
|
|
|
|
* version 2. This program is licensed "as is" without any warranty of any
|
|
|
|
* kind, whether express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/interrupt.h>
|
2008-11-14 00:24:34 +00:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/mdio-gpio.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_OF_GPIO
|
2008-05-26 09:53:21 +00:00
|
|
|
#include <linux/of_gpio.h>
|
2009-07-23 17:56:48 +00:00
|
|
|
#include <linux/of_mdio.h>
|
2008-05-26 09:53:21 +00:00
|
|
|
#include <linux/of_platform.h>
|
2008-11-14 00:24:34 +00:00
|
|
|
#endif
|
2008-05-26 09:53:21 +00:00
|
|
|
|
|
|
|
struct mdio_gpio_info {
|
|
|
|
struct mdiobb_ctrl ctrl;
|
|
|
|
int mdc, mdio;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
|
|
|
|
{
|
|
|
|
struct mdio_gpio_info *bitbang =
|
|
|
|
container_of(ctrl, struct mdio_gpio_info, ctrl);
|
|
|
|
|
|
|
|
if (dir)
|
|
|
|
gpio_direction_output(bitbang->mdio, 1);
|
|
|
|
else
|
|
|
|
gpio_direction_input(bitbang->mdio);
|
|
|
|
}
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
static int mdio_get(struct mdiobb_ctrl *ctrl)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
|
|
|
struct mdio_gpio_info *bitbang =
|
|
|
|
container_of(ctrl, struct mdio_gpio_info, ctrl);
|
|
|
|
|
|
|
|
return gpio_get_value(bitbang->mdio);
|
|
|
|
}
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
|
|
|
struct mdio_gpio_info *bitbang =
|
|
|
|
container_of(ctrl, struct mdio_gpio_info, ctrl);
|
|
|
|
|
|
|
|
gpio_set_value(bitbang->mdio, what);
|
|
|
|
}
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
|
|
|
struct mdio_gpio_info *bitbang =
|
|
|
|
container_of(ctrl, struct mdio_gpio_info, ctrl);
|
|
|
|
|
|
|
|
gpio_set_value(bitbang->mdc, what);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mdiobb_ops mdio_gpio_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
2008-11-14 00:24:34 +00:00
|
|
|
.set_mdc = mdc_set,
|
2008-05-26 09:53:21 +00:00
|
|
|
.set_mdio_dir = mdio_dir,
|
2008-11-14 00:24:34 +00:00
|
|
|
.set_mdio_data = mdio_set,
|
|
|
|
.get_mdio_data = mdio_get,
|
2008-05-26 09:53:21 +00:00
|
|
|
};
|
|
|
|
|
2009-07-23 17:56:48 +00:00
|
|
|
static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev,
|
2008-11-14 00:24:34 +00:00
|
|
|
struct mdio_gpio_platform_data *pdata,
|
|
|
|
int bus_id)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
|
|
|
struct mii_bus *new_bus;
|
|
|
|
struct mdio_gpio_info *bitbang;
|
|
|
|
int i;
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
|
2008-05-26 09:53:21 +00:00
|
|
|
if (!bitbang)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
bitbang->ctrl.ops = &mdio_gpio_ops;
|
2011-11-15 11:54:15 +00:00
|
|
|
bitbang->ctrl.reset = pdata->reset;
|
2008-11-14 00:24:34 +00:00
|
|
|
bitbang->mdc = pdata->mdc;
|
|
|
|
bitbang->mdio = pdata->mdio;
|
2008-05-26 09:53:21 +00:00
|
|
|
|
|
|
|
new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
|
|
|
|
if (!new_bus)
|
2008-10-08 23:29:57 +00:00
|
|
|
goto out_free_bitbang;
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
new_bus->name = "GPIO Bitbanged MDIO",
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
new_bus->phy_mask = pdata->phy_mask;
|
|
|
|
new_bus->irq = pdata->irqs;
|
|
|
|
new_bus->parent = dev;
|
|
|
|
|
|
|
|
if (new_bus->phy_mask == ~0)
|
2008-05-26 09:53:21 +00:00
|
|
|
goto out_free_bus;
|
|
|
|
|
|
|
|
for (i = 0; i < PHY_MAX_ADDR; i++)
|
2008-11-14 00:24:34 +00:00
|
|
|
if (!new_bus->irq[i])
|
|
|
|
new_bus->irq[i] = PHY_POLL;
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
|
|
|
|
|
|
|
|
if (gpio_request(bitbang->mdc, "mdc"))
|
|
|
|
goto out_free_bus;
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
if (gpio_request(bitbang->mdio, "mdio"))
|
|
|
|
goto out_free_mdc;
|
|
|
|
|
2009-02-08 23:46:01 +00:00
|
|
|
gpio_direction_output(bitbang->mdc, 0);
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
dev_set_drvdata(dev, new_bus);
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2009-07-23 17:56:48 +00:00
|
|
|
return new_bus;
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
out_free_mdc:
|
|
|
|
gpio_free(bitbang->mdc);
|
2008-05-26 09:53:21 +00:00
|
|
|
out_free_bus:
|
|
|
|
free_mdio_bitbang(new_bus);
|
2008-10-08 23:29:57 +00:00
|
|
|
out_free_bitbang:
|
|
|
|
kfree(bitbang);
|
2008-05-26 09:53:21 +00:00
|
|
|
out:
|
2009-07-23 17:56:48 +00:00
|
|
|
return NULL;
|
2008-05-26 09:53:21 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 22:47:33 +00:00
|
|
|
static void mdio_gpio_bus_deinit(struct device *dev)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
2008-11-14 00:24:34 +00:00
|
|
|
struct mii_bus *bus = dev_get_drvdata(dev);
|
2008-05-26 09:53:21 +00:00
|
|
|
struct mdio_gpio_info *bitbang = bus->priv;
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
dev_set_drvdata(dev, NULL);
|
|
|
|
gpio_free(bitbang->mdio);
|
2009-07-23 17:56:48 +00:00
|
|
|
gpio_free(bitbang->mdc);
|
|
|
|
free_mdio_bitbang(bus);
|
2008-05-26 09:53:21 +00:00
|
|
|
kfree(bitbang);
|
2008-11-14 00:24:34 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 17:56:48 +00:00
|
|
|
static void __devexit mdio_gpio_bus_destroy(struct device *dev)
|
|
|
|
{
|
|
|
|
struct mii_bus *bus = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
mdiobus_unregister(bus);
|
|
|
|
mdio_gpio_bus_deinit(dev);
|
|
|
|
}
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
static int __devinit mdio_gpio_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
|
2009-07-23 17:56:48 +00:00
|
|
|
struct mii_bus *new_bus;
|
|
|
|
int ret;
|
2008-11-14 00:24:34 +00:00
|
|
|
|
|
|
|
if (!pdata)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2009-07-23 17:56:48 +00:00
|
|
|
new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
|
|
|
|
if (!new_bus)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
ret = mdiobus_register(new_bus);
|
|
|
|
if (ret)
|
|
|
|
mdio_gpio_bus_deinit(&pdev->dev);
|
|
|
|
|
|
|
|
return ret;
|
2008-11-14 00:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __devexit mdio_gpio_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
mdio_gpio_bus_destroy(&pdev->dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_OF_GPIO
|
|
|
|
|
2011-02-23 04:05:51 +00:00
|
|
|
static int __devinit mdio_ofgpio_probe(struct platform_device *ofdev)
|
2008-11-14 00:24:34 +00:00
|
|
|
{
|
|
|
|
struct mdio_gpio_platform_data *pdata;
|
2009-07-23 17:56:48 +00:00
|
|
|
struct mii_bus *new_bus;
|
2009-01-20 01:14:21 +00:00
|
|
|
int ret;
|
2008-11-14 00:24:34 +00:00
|
|
|
|
|
|
|
pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
|
|
|
|
if (!pdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-04-13 23:12:29 +00:00
|
|
|
ret = of_get_gpio(ofdev->dev.of_node, 0);
|
2009-01-20 01:14:21 +00:00
|
|
|
if (ret < 0)
|
2008-11-14 00:24:34 +00:00
|
|
|
goto out_free;
|
2009-01-20 01:14:21 +00:00
|
|
|
pdata->mdc = ret;
|
|
|
|
|
2010-04-13 23:12:29 +00:00
|
|
|
ret = of_get_gpio(ofdev->dev.of_node, 1);
|
2009-01-20 01:14:21 +00:00
|
|
|
if (ret < 0)
|
2009-07-23 17:56:48 +00:00
|
|
|
goto out_free;
|
2009-01-20 01:14:21 +00:00
|
|
|
pdata->mdio = ret;
|
2008-11-14 00:24:34 +00:00
|
|
|
|
2009-07-23 17:56:48 +00:00
|
|
|
new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
|
|
|
|
if (!new_bus)
|
2009-09-11 06:22:09 +00:00
|
|
|
goto out_free;
|
2008-11-14 00:24:34 +00:00
|
|
|
|
2010-04-13 23:12:29 +00:00
|
|
|
ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
|
2009-07-23 17:56:48 +00:00
|
|
|
if (ret)
|
|
|
|
mdio_gpio_bus_deinit(&ofdev->dev);
|
|
|
|
|
|
|
|
return ret;
|
2008-11-14 00:24:34 +00:00
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(pdata);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2010-08-06 15:25:50 +00:00
|
|
|
static int __devexit mdio_ofgpio_remove(struct platform_device *ofdev)
|
2008-11-14 00:24:34 +00:00
|
|
|
{
|
|
|
|
mdio_gpio_bus_destroy(&ofdev->dev);
|
|
|
|
kfree(ofdev->dev.platform_data);
|
2008-05-26 09:53:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct of_device_id mdio_ofgpio_match[] = {
|
|
|
|
{
|
|
|
|
.compatible = "virtual,mdio-gpio",
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
};
|
2009-10-14 21:54:52 +00:00
|
|
|
MODULE_DEVICE_TABLE(of, mdio_ofgpio_match);
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2011-02-23 04:05:51 +00:00
|
|
|
static struct platform_driver mdio_ofgpio_driver = {
|
2010-04-13 23:13:02 +00:00
|
|
|
.driver = {
|
2011-10-18 03:04:11 +00:00
|
|
|
.name = "mdio-ofgpio",
|
2010-04-13 23:13:02 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.of_match_table = mdio_ofgpio_match,
|
|
|
|
},
|
2008-05-26 09:53:21 +00:00
|
|
|
.probe = mdio_ofgpio_probe,
|
2008-11-14 00:24:34 +00:00
|
|
|
.remove = __devexit_p(mdio_ofgpio_remove),
|
2008-05-26 09:53:21 +00:00
|
|
|
};
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
static inline int __init mdio_ofgpio_init(void)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
2011-02-23 04:05:51 +00:00
|
|
|
return platform_driver_register(&mdio_ofgpio_driver);
|
2008-05-26 09:53:21 +00:00
|
|
|
}
|
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
static inline void __exit mdio_ofgpio_exit(void)
|
2008-05-26 09:53:21 +00:00
|
|
|
{
|
2011-02-23 04:05:51 +00:00
|
|
|
platform_driver_unregister(&mdio_ofgpio_driver);
|
2008-05-26 09:53:21 +00:00
|
|
|
}
|
2008-11-14 00:24:34 +00:00
|
|
|
#else
|
|
|
|
static inline int __init mdio_ofgpio_init(void) { return 0; }
|
|
|
|
static inline void __exit mdio_ofgpio_exit(void) { }
|
|
|
|
#endif /* CONFIG_OF_GPIO */
|
|
|
|
|
|
|
|
static struct platform_driver mdio_gpio_driver = {
|
|
|
|
.probe = mdio_gpio_probe,
|
|
|
|
.remove = __devexit_p(mdio_gpio_remove),
|
|
|
|
.driver = {
|
|
|
|
.name = "mdio-gpio",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init mdio_gpio_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = mdio_ofgpio_init();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = platform_driver_register(&mdio_gpio_driver);
|
|
|
|
if (ret)
|
|
|
|
mdio_ofgpio_exit();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
module_init(mdio_gpio_init);
|
|
|
|
|
|
|
|
static void __exit mdio_gpio_exit(void)
|
|
|
|
{
|
|
|
|
platform_driver_unregister(&mdio_gpio_driver);
|
|
|
|
mdio_ofgpio_exit();
|
|
|
|
}
|
|
|
|
module_exit(mdio_gpio_exit);
|
2008-05-26 09:53:21 +00:00
|
|
|
|
2008-11-14 00:24:34 +00:00
|
|
|
MODULE_ALIAS("platform:mdio-gpio");
|
|
|
|
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");
|