mdio-gpio.c 5.4 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
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/mdio-gpio.h>

31
#include <linux/of_gpio.h>
32
#include <linux/of_mdio.h>
33 34 35 36 37 38

struct mdio_gpio_info {
	struct mdiobb_ctrl ctrl;
	int mdc, mdio;
};

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static void *mdio_gpio_of_get_data(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct mdio_gpio_platform_data *pdata;
	int ret;

	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return NULL;

	ret = of_get_gpio(np, 0);
	if (ret < 0)
		return NULL;

	pdata->mdc = ret;

	ret = of_get_gpio(np, 1);
	if (ret < 0)
		return NULL;
	pdata->mdio = ret;

	return pdata;
}

63 64 65 66 67 68 69 70 71 72 73
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);
}

74
static int mdio_get(struct mdiobb_ctrl *ctrl)
75 76 77 78 79 80 81
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

	return gpio_get_value(bitbang->mdio);
}

82
static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
83 84 85 86 87 88 89
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

	gpio_set_value(bitbang->mdio, what);
}

90
static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
91 92 93 94 95 96 97 98 99
{
	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,
100
	.set_mdc = mdc_set,
101
	.set_mdio_dir = mdio_dir,
102 103
	.set_mdio_data = mdio_set,
	.get_mdio_data = mdio_get,
104 105
};

B
Bill Pemberton 已提交
106
static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
107 108
					struct mdio_gpio_platform_data *pdata,
					int bus_id)
109 110 111 112 113
{
	struct mii_bus *new_bus;
	struct mdio_gpio_info *bitbang;
	int i;

114
	bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
115 116 117 118
	if (!bitbang)
		goto out;

	bitbang->ctrl.ops = &mdio_gpio_ops;
119
	bitbang->ctrl.reset = pdata->reset;
120 121
	bitbang->mdc = pdata->mdc;
	bitbang->mdio = pdata->mdio;
122 123 124

	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
	if (!new_bus)
125
		goto out_free_bitbang;
126

127
	new_bus->name = "GPIO Bitbanged MDIO",
128

129 130 131 132 133
	new_bus->phy_mask = pdata->phy_mask;
	new_bus->irq = pdata->irqs;
	new_bus->parent = dev;

	if (new_bus->phy_mask == ~0)
134 135 136
		goto out_free_bus;

	for (i = 0; i < PHY_MAX_ADDR; i++)
137 138
		if (!new_bus->irq[i])
			new_bus->irq[i] = PHY_POLL;
139

140
	snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
141 142 143

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

145 146 147
	if (gpio_request(bitbang->mdio, "mdio"))
		goto out_free_mdc;

148 149
	gpio_direction_output(bitbang->mdc, 0);

150
	dev_set_drvdata(dev, new_bus);
151

152
	return new_bus;
153

154 155
out_free_mdc:
	gpio_free(bitbang->mdc);
156 157
out_free_bus:
	free_mdio_bitbang(new_bus);
158 159
out_free_bitbang:
	kfree(bitbang);
160
out:
161
	return NULL;
162 163
}

164
static void mdio_gpio_bus_deinit(struct device *dev)
165
{
166
	struct mii_bus *bus = dev_get_drvdata(dev);
167 168
	struct mdio_gpio_info *bitbang = bus->priv;

169 170
	dev_set_drvdata(dev, NULL);
	gpio_free(bitbang->mdio);
171 172
	gpio_free(bitbang->mdc);
	free_mdio_bitbang(bus);
173
	kfree(bitbang);
174 175
}

B
Bill Pemberton 已提交
176
static void mdio_gpio_bus_destroy(struct device *dev)
177 178 179 180 181 182 183
{
	struct mii_bus *bus = dev_get_drvdata(dev);

	mdiobus_unregister(bus);
	mdio_gpio_bus_deinit(dev);
}

B
Bill Pemberton 已提交
184
static int mdio_gpio_probe(struct platform_device *pdev)
185
{
186
	struct mdio_gpio_platform_data *pdata;
187
	struct mii_bus *new_bus;
188
	int ret, bus_id;
189

190
	if (pdev->dev.of_node) {
191
		pdata = mdio_gpio_of_get_data(pdev);
192 193
		bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
	} else {
194
		pdata = pdev->dev.platform_data;
195 196
		bus_id = pdev->id;
	}
197

198 199 200
	if (!pdata)
		return -ENODEV;

201
	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
202 203 204
	if (!new_bus)
		return -ENODEV;

205 206 207 208 209
	if (pdev->dev.of_node)
		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
	else
		ret = mdiobus_register(new_bus);

210 211 212 213
	if (ret)
		mdio_gpio_bus_deinit(&pdev->dev);

	return ret;
214 215
}

B
Bill Pemberton 已提交
216
static int mdio_gpio_remove(struct platform_device *pdev)
217 218 219 220 221 222
{
	mdio_gpio_bus_destroy(&pdev->dev);

	return 0;
}

223 224 225
static struct of_device_id mdio_gpio_of_match[] = {
	{ .compatible = "virtual,mdio-gpio", },
	{ /* sentinel */ }
226 227
};

228 229
static struct platform_driver mdio_gpio_driver = {
	.probe = mdio_gpio_probe,
B
Bill Pemberton 已提交
230
	.remove = mdio_gpio_remove,
231 232 233
	.driver		= {
		.name	= "mdio-gpio",
		.owner	= THIS_MODULE,
234
		.of_match_table = mdio_gpio_of_match,
235 236 237 238 239
	},
};

static int __init mdio_gpio_init(void)
{
240
	return platform_driver_register(&mdio_gpio_driver);
241 242 243 244 245 246 247 248
}
module_init(mdio_gpio_init);

static void __exit mdio_gpio_exit(void)
{
	platform_driver_unregister(&mdio_gpio_driver);
}
module_exit(mdio_gpio_exit);
249

250 251 252 253
MODULE_ALIAS("platform:mdio-gpio");
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");