mdio-gpio.c 6.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
 * 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
	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.
		 */
83 84
		gpio_set_value_cansleep(bitbang->mdo,
					1 ^ bitbang->mdo_active_low);
85 86 87
		return;
	}

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

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

100 101
	return gpio_get_value_cansleep(bitbang->mdio) ^
		bitbang->mdio_active_low;
102 103
}

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

109
	if (bitbang->mdo)
110 111
		gpio_set_value_cansleep(bitbang->mdo,
					what ^ bitbang->mdo_active_low);
112
	else
113 114
		gpio_set_value_cansleep(bitbang->mdio,
					what ^ bitbang->mdio_active_low);
115 116
}

117
static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
118 119 120 121
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

122
	gpio_set_value_cansleep(bitbang->mdc, what ^ bitbang->mdc_active_low);
123 124 125 126
}

static struct mdiobb_ops mdio_gpio_ops = {
	.owner = THIS_MODULE,
127
	.set_mdc = mdc_set,
128
	.set_mdio_dir = mdio_dir,
129 130
	.set_mdio_data = mdio_set,
	.get_mdio_data = mdio_get,
131 132
};

B
Bill Pemberton 已提交
133
static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
134 135
					  struct mdio_gpio_platform_data *pdata,
					  int bus_id)
136 137 138 139 140
{
	struct mii_bus *new_bus;
	struct mdio_gpio_info *bitbang;
	int i;

141
	bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
142 143 144 145
	if (!bitbang)
		goto out;

	bitbang->ctrl.ops = &mdio_gpio_ops;
146
	bitbang->ctrl.reset = pdata->reset;
147
	bitbang->mdc = pdata->mdc;
148
	bitbang->mdc_active_low = pdata->mdc_active_low;
149
	bitbang->mdio = pdata->mdio;
150
	bitbang->mdio_active_low = pdata->mdio_active_low;
151 152
	bitbang->mdo = pdata->mdo;
	bitbang->mdo_active_low = pdata->mdo_active_low;
153 154 155

	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
	if (!new_bus)
156
		goto out;
157

158
	new_bus->name = "GPIO Bitbanged MDIO",
159

160
	new_bus->phy_mask = pdata->phy_mask;
161
	new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
162 163 164 165
	new_bus->irq = pdata->irqs;
	new_bus->parent = dev;

	if (new_bus->phy_mask == ~0)
166 167 168
		goto out_free_bus;

	for (i = 0; i < PHY_MAX_ADDR; i++)
169 170
		if (!new_bus->irq[i])
			new_bus->irq[i] = PHY_POLL;
171

172 173 174 175
	if (bus_id != -1)
		snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
	else
		strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
176

177
	if (devm_gpio_request(dev, bitbang->mdc, "mdc"))
178
		goto out_free_bus;
179

180 181
	if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
		goto out_free_bus;
182

183 184 185 186 187 188 189
	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);
	}

190 191
	gpio_direction_output(bitbang->mdc, 0);

192
	dev_set_drvdata(dev, new_bus);
193

194
	return new_bus;
195 196 197 198

out_free_bus:
	free_mdio_bitbang(new_bus);
out:
199
	return NULL;
200 201
}

202
static void mdio_gpio_bus_deinit(struct device *dev)
203
{
204
	struct mii_bus *bus = dev_get_drvdata(dev);
205

206
	free_mdio_bitbang(bus);
207 208
}

B
Bill Pemberton 已提交
209
static void mdio_gpio_bus_destroy(struct device *dev)
210 211 212 213 214 215 216
{
	struct mii_bus *bus = dev_get_drvdata(dev);

	mdiobus_unregister(bus);
	mdio_gpio_bus_deinit(dev);
}

B
Bill Pemberton 已提交
217
static int mdio_gpio_probe(struct platform_device *pdev)
218
{
219
	struct mdio_gpio_platform_data *pdata;
220
	struct mii_bus *new_bus;
221
	int ret, bus_id;
222

223
	if (pdev->dev.of_node) {
224
		pdata = mdio_gpio_of_get_data(pdev);
225
		bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
226 227 228 229
		if (bus_id < 0) {
			dev_warn(&pdev->dev, "failed to get alias id\n");
			bus_id = 0;
		}
230
	} else {
231
		pdata = dev_get_platdata(&pdev->dev);
232 233
		bus_id = pdev->id;
	}
234

235 236 237
	if (!pdata)
		return -ENODEV;

238
	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
239 240 241
	if (!new_bus)
		return -ENODEV;

242 243 244 245 246
	if (pdev->dev.of_node)
		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
	else
		ret = mdiobus_register(new_bus);

247 248 249 250
	if (ret)
		mdio_gpio_bus_deinit(&pdev->dev);

	return ret;
251 252
}

B
Bill Pemberton 已提交
253
static int mdio_gpio_remove(struct platform_device *pdev)
254 255 256 257 258 259
{
	mdio_gpio_bus_destroy(&pdev->dev);

	return 0;
}

260
static const struct of_device_id mdio_gpio_of_match[] = {
261 262
	{ .compatible = "virtual,mdio-gpio", },
	{ /* sentinel */ }
263 264
};

265 266
static struct platform_driver mdio_gpio_driver = {
	.probe = mdio_gpio_probe,
B
Bill Pemberton 已提交
267
	.remove = mdio_gpio_remove,
268 269
	.driver		= {
		.name	= "mdio-gpio",
270
		.of_match_table = mdio_gpio_of_match,
271 272 273
	},
};

274
module_platform_driver(mdio_gpio_driver);
275

276 277 278 279
MODULE_ALIAS("platform:mdio-gpio");
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");