mdio-gpio.c 6.2 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
 * 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/interrupt.h>
26 27 28 29
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/mdio-gpio.h>

30
#include <linux/of_gpio.h>
31
#include <linux/of_mdio.h>
32 33 34

struct mdio_gpio_info {
	struct mdiobb_ctrl ctrl;
35 36
	int mdc, mdio, mdo;
	int mdc_active_low, mdio_active_low, mdo_active_low;
37 38
};

39 40 41 42
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;
43
	enum of_gpio_flags flags;
44 45 46 47 48 49
	int ret;

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

50
	ret = of_get_gpio_flags(np, 0, &flags);
51 52 53 54
	if (ret < 0)
		return NULL;

	pdata->mdc = ret;
55
	pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
56

57
	ret = of_get_gpio_flags(np, 1, &flags);
58 59 60
	if (ret < 0)
		return NULL;
	pdata->mdio = ret;
61
	pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
62

63 64 65 66 67 68
	ret = of_get_gpio_flags(np, 2, &flags);
	if (ret > 0) {
		pdata->mdo = ret;
		pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW;
	}

69 70 71
	return pdata;
}

72 73 74 75 76
static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

77 78 79 80 81 82 83 84 85 86
	if (bitbang->mdo) {
		/* Separate output pin. Always set its value to high
		 * when changing direction. If direction is input,
		 * assume the pin serves as pull-up. If direction is
		 * output, the default value is high.
		 */
		gpio_set_value(bitbang->mdo, 1 ^ bitbang->mdo_active_low);
		return;
	}

87
	if (dir)
88 89
		gpio_direction_output(bitbang->mdio,
				      1 ^ bitbang->mdio_active_low);
90 91 92 93
	else
		gpio_direction_input(bitbang->mdio);
}

94
static int mdio_get(struct mdiobb_ctrl *ctrl)
95 96 97 98
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

99
	return gpio_get_value(bitbang->mdio) ^ bitbang->mdio_active_low;
100 101
}

102
static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
103 104 105 106
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

107 108 109 110
	if (bitbang->mdo)
		gpio_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low);
	else
		gpio_set_value(bitbang->mdio, what ^ bitbang->mdio_active_low);
111 112
}

113
static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
114 115 116 117
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

118
	gpio_set_value(bitbang->mdc, what ^ bitbang->mdc_active_low);
119 120 121 122
}

static struct mdiobb_ops mdio_gpio_ops = {
	.owner = THIS_MODULE,
123
	.set_mdc = mdc_set,
124
	.set_mdio_dir = mdio_dir,
125 126
	.set_mdio_data = mdio_set,
	.get_mdio_data = mdio_get,
127 128
};

B
Bill Pemberton 已提交
129
static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
130 131
					  struct mdio_gpio_platform_data *pdata,
					  int bus_id)
132 133 134 135 136
{
	struct mii_bus *new_bus;
	struct mdio_gpio_info *bitbang;
	int i;

137
	bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
138 139 140 141
	if (!bitbang)
		goto out;

	bitbang->ctrl.ops = &mdio_gpio_ops;
142
	bitbang->ctrl.reset = pdata->reset;
143
	bitbang->mdc = pdata->mdc;
144
	bitbang->mdc_active_low = pdata->mdc_active_low;
145
	bitbang->mdio = pdata->mdio;
146
	bitbang->mdio_active_low = pdata->mdio_active_low;
147 148
	bitbang->mdo = pdata->mdo;
	bitbang->mdo_active_low = pdata->mdo_active_low;
149 150 151

	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
	if (!new_bus)
152
		goto out;
153

154
	new_bus->name = "GPIO Bitbanged MDIO",
155

156 157 158 159 160
	new_bus->phy_mask = pdata->phy_mask;
	new_bus->irq = pdata->irqs;
	new_bus->parent = dev;

	if (new_bus->phy_mask == ~0)
161 162 163
		goto out_free_bus;

	for (i = 0; i < PHY_MAX_ADDR; i++)
164 165
		if (!new_bus->irq[i])
			new_bus->irq[i] = PHY_POLL;
166

167
	snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
168

169
	if (devm_gpio_request(dev, bitbang->mdc, "mdc"))
170
		goto out_free_bus;
171

172 173
	if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
		goto out_free_bus;
174

175 176 177 178 179 180 181
	if (bitbang->mdo) {
		if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
			goto out_free_bus;
		gpio_direction_output(bitbang->mdo, 1);
		gpio_direction_input(bitbang->mdio);
	}

182 183
	gpio_direction_output(bitbang->mdc, 0);

184
	dev_set_drvdata(dev, new_bus);
185

186
	return new_bus;
187 188 189 190

out_free_bus:
	free_mdio_bitbang(new_bus);
out:
191
	return NULL;
192 193
}

194
static void mdio_gpio_bus_deinit(struct device *dev)
195
{
196
	struct mii_bus *bus = dev_get_drvdata(dev);
197

198
	free_mdio_bitbang(bus);
199 200
}

B
Bill Pemberton 已提交
201
static void mdio_gpio_bus_destroy(struct device *dev)
202 203 204 205 206 207 208
{
	struct mii_bus *bus = dev_get_drvdata(dev);

	mdiobus_unregister(bus);
	mdio_gpio_bus_deinit(dev);
}

B
Bill Pemberton 已提交
209
static int mdio_gpio_probe(struct platform_device *pdev)
210
{
211
	struct mdio_gpio_platform_data *pdata;
212
	struct mii_bus *new_bus;
213
	int ret, bus_id;
214

215
	if (pdev->dev.of_node) {
216
		pdata = mdio_gpio_of_get_data(pdev);
217
		bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
218 219 220 221
		if (bus_id < 0) {
			dev_warn(&pdev->dev, "failed to get alias id\n");
			bus_id = 0;
		}
222
	} else {
223
		pdata = dev_get_platdata(&pdev->dev);
224 225
		bus_id = pdev->id;
	}
226

227 228 229
	if (!pdata)
		return -ENODEV;

230
	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
231 232 233
	if (!new_bus)
		return -ENODEV;

234 235 236 237 238
	if (pdev->dev.of_node)
		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
	else
		ret = mdiobus_register(new_bus);

239 240 241 242
	if (ret)
		mdio_gpio_bus_deinit(&pdev->dev);

	return ret;
243 244
}

B
Bill Pemberton 已提交
245
static int mdio_gpio_remove(struct platform_device *pdev)
246 247 248 249 250 251
{
	mdio_gpio_bus_destroy(&pdev->dev);

	return 0;
}

252 253 254
static struct of_device_id mdio_gpio_of_match[] = {
	{ .compatible = "virtual,mdio-gpio", },
	{ /* sentinel */ }
255 256
};

257 258
static struct platform_driver mdio_gpio_driver = {
	.probe = mdio_gpio_probe,
B
Bill Pemberton 已提交
259
	.remove = mdio_gpio_remove,
260 261 262
	.driver		= {
		.name	= "mdio-gpio",
		.owner	= THIS_MODULE,
263
		.of_match_table = mdio_gpio_of_match,
264 265 266
	},
};

267
module_platform_driver(mdio_gpio_driver);
268

269 270 271 272
MODULE_ALIAS("platform:mdio-gpio");
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");