mdio-gpio.c 6.3 KB
Newer Older
1
/*
2 3
 * GPIO based MDIO bitbang driver.
 * Supports OpenFirmware.
4 5 6 7
 *
 * Copyright (c) 2008 CSE Semaphore Belgium.
 *  by Laurent Pinchart <laurentp@cse-semaphore.com>
 *
8 9
 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
 *
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 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>
27 28 29 30 31
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/mdio-gpio.h>

#ifdef CONFIG_OF_GPIO
32
#include <linux/of_gpio.h>
33
#include <linux/of_mdio.h>
34
#include <linux/of_platform.h>
35
#endif
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

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);
}

53
static int mdio_get(struct mdiobb_ctrl *ctrl)
54 55 56 57 58 59 60
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

	return gpio_get_value(bitbang->mdio);
}

61
static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
62 63 64 65 66 67 68
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

	gpio_set_value(bitbang->mdio, what);
}

69
static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
70 71 72 73 74 75 76 77 78
{
	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,
79
	.set_mdc = mdc_set,
80
	.set_mdio_dir = mdio_dir,
81 82
	.set_mdio_data = mdio_set,
	.get_mdio_data = mdio_get,
83 84
};

85
static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev,
86 87
					struct mdio_gpio_platform_data *pdata,
					int bus_id)
88 89 90 91 92
{
	struct mii_bus *new_bus;
	struct mdio_gpio_info *bitbang;
	int i;

93
	bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
94 95 96 97
	if (!bitbang)
		goto out;

	bitbang->ctrl.ops = &mdio_gpio_ops;
98 99
	bitbang->mdc = pdata->mdc;
	bitbang->mdio = pdata->mdio;
100 101 102

	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
	if (!new_bus)
103
		goto out_free_bitbang;
104

105
	new_bus->name = "GPIO Bitbanged MDIO",
106

107 108 109 110 111
	new_bus->phy_mask = pdata->phy_mask;
	new_bus->irq = pdata->irqs;
	new_bus->parent = dev;

	if (new_bus->phy_mask == ~0)
112 113 114
		goto out_free_bus;

	for (i = 0; i < PHY_MAX_ADDR; i++)
115 116
		if (!new_bus->irq[i])
			new_bus->irq[i] = PHY_POLL;
117

118 119 120 121
	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);

	if (gpio_request(bitbang->mdc, "mdc"))
		goto out_free_bus;
122

123 124 125
	if (gpio_request(bitbang->mdio, "mdio"))
		goto out_free_mdc;

126 127
	gpio_direction_output(bitbang->mdc, 0);

128
	dev_set_drvdata(dev, new_bus);
129

130
	return new_bus;
131

132 133
out_free_mdc:
	gpio_free(bitbang->mdc);
134 135
out_free_bus:
	free_mdio_bitbang(new_bus);
136 137
out_free_bitbang:
	kfree(bitbang);
138
out:
139
	return NULL;
140 141
}

142
static void __devinit mdio_gpio_bus_deinit(struct device *dev)
143
{
144
	struct mii_bus *bus = dev_get_drvdata(dev);
145 146
	struct mdio_gpio_info *bitbang = bus->priv;

147 148
	dev_set_drvdata(dev, NULL);
	gpio_free(bitbang->mdio);
149 150
	gpio_free(bitbang->mdc);
	free_mdio_bitbang(bus);
151
	kfree(bitbang);
152 153
}

154 155 156 157 158 159 160 161
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);
}

162 163 164
static int __devinit mdio_gpio_probe(struct platform_device *pdev)
{
	struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
165 166
	struct mii_bus *new_bus;
	int ret;
167 168 169 170

	if (!pdata)
		return -ENODEV;

171 172 173 174 175 176 177 178 179
	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;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

static int __devexit mdio_gpio_remove(struct platform_device *pdev)
{
	mdio_gpio_bus_destroy(&pdev->dev);

	return 0;
}

#ifdef CONFIG_OF_GPIO

static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
                                        const struct of_device_id *match)
{
	struct mdio_gpio_platform_data *pdata;
195
	struct mii_bus *new_bus;
R
Roel Kluin 已提交
196
	int ret;
197 198 199 200 201

	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

R
Roel Kluin 已提交
202 203
	ret = of_get_gpio(ofdev->node, 0);
	if (ret < 0)
204
		goto out_free;
R
Roel Kluin 已提交
205 206 207 208
	pdata->mdc = ret;

	ret = of_get_gpio(ofdev->node, 1);
	if (ret < 0)
209
		goto out_free;
R
Roel Kluin 已提交
210
	pdata->mdio = ret;
211

212 213
	new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
	if (!new_bus)
214
		goto out_free;
215

216 217 218 219 220
	ret = of_mdiobus_register(new_bus, ofdev->node);
	if (ret)
		mdio_gpio_bus_deinit(&ofdev->dev);

	return ret;
221 222 223 224 225 226 227 228 229 230

out_free:
	kfree(pdata);
	return -ENODEV;
}

static int __devexit mdio_ofgpio_remove(struct of_device *ofdev)
{
	mdio_gpio_bus_destroy(&ofdev->dev);
	kfree(ofdev->dev.platform_data);
231 232 233 234 235 236 237 238 239 240

	return 0;
}

static struct of_device_id mdio_ofgpio_match[] = {
	{
		.compatible = "virtual,mdio-gpio",
	},
	{},
};
241
MODULE_DEVICE_TABLE(of, mdio_ofgpio_match);
242 243 244 245 246

static struct of_platform_driver mdio_ofgpio_driver = {
	.name = "mdio-gpio",
	.match_table = mdio_ofgpio_match,
	.probe = mdio_ofgpio_probe,
247
	.remove = __devexit_p(mdio_ofgpio_remove),
248 249
};

250
static inline int __init mdio_ofgpio_init(void)
251 252 253 254
{
	return of_register_platform_driver(&mdio_ofgpio_driver);
}

255
static inline void __exit mdio_ofgpio_exit(void)
256 257 258
{
	of_unregister_platform_driver(&mdio_ofgpio_driver);
}
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#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);
295

296 297 298 299
MODULE_ALIAS("platform:mdio-gpio");
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");