mdio-gpio.c 4.9 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
#include <linux/platform_device.h>
#include <linux/gpio.h>
28
#include <linux/platform_data/mdio-gpio.h>
29

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

struct mdio_gpio_info {
	struct mdiobb_ctrl ctrl;
35
	struct gpio_desc *mdc, *mdio, *mdo;
36 37
};

38 39
static int mdio_gpio_get_data(struct device *dev,
			      struct mdio_gpio_info *bitbang)
40
{
41 42 43
	bitbang->mdc = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
	if (IS_ERR(bitbang->mdc))
		return PTR_ERR(bitbang->mdc);
44

45 46 47
	bitbang->mdio = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN);
	if (IS_ERR(bitbang->mdio))
		return PTR_ERR(bitbang->mdio);
48

49 50 51
	bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, 2,
						     GPIOD_OUT_LOW);
	return PTR_ERR_OR_ZERO(bitbang->mdo);
52 53
}

54 55 56 57 58
static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

59 60 61 62 63 64
	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.
		 */
65
		gpiod_set_value(bitbang->mdo, 1);
66 67 68
		return;
	}

69
	if (dir)
70
		gpiod_direction_output(bitbang->mdio, 1);
71
	else
72
		gpiod_direction_input(bitbang->mdio);
73 74
}

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

80
	return gpiod_get_value(bitbang->mdio);
81 82
}

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

88
	if (bitbang->mdo)
89
		gpiod_set_value(bitbang->mdo, what);
90
	else
91
		gpiod_set_value(bitbang->mdio, what);
92 93
}

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

99
	gpiod_set_value(bitbang->mdc, what);
100 101
}

102
static const struct mdiobb_ops mdio_gpio_ops = {
103
	.owner = THIS_MODULE,
104
	.set_mdc = mdc_set,
105
	.set_mdio_dir = mdio_dir,
106 107
	.set_mdio_data = mdio_set,
	.get_mdio_data = mdio_get,
108 109
};

B
Bill Pemberton 已提交
110
static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
111
					  struct mdio_gpio_info *bitbang,
112
					  int bus_id)
113 114 115 116 117 118 119
{
	struct mii_bus *new_bus;

	bitbang->ctrl.ops = &mdio_gpio_ops;

	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
	if (!new_bus)
120
		return NULL;
121

122
	new_bus->name = "GPIO Bitbanged MDIO";
123 124
	new_bus->parent = dev;

125 126 127 128
	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);
129 130

	dev_set_drvdata(dev, new_bus);
131

132
	return new_bus;
133 134
}

135
static void mdio_gpio_bus_deinit(struct device *dev)
136
{
137
	struct mii_bus *bus = dev_get_drvdata(dev);
138

139
	free_mdio_bitbang(bus);
140 141
}

B
Bill Pemberton 已提交
142
static void mdio_gpio_bus_destroy(struct device *dev)
143 144 145 146 147 148 149
{
	struct mii_bus *bus = dev_get_drvdata(dev);

	mdiobus_unregister(bus);
	mdio_gpio_bus_deinit(dev);
}

B
Bill Pemberton 已提交
150
static int mdio_gpio_probe(struct platform_device *pdev)
151
{
152
	struct mdio_gpio_info *bitbang;
153
	struct mii_bus *new_bus;
154
	int ret, bus_id;
155

156 157 158 159
	bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
	if (!bitbang)
		return -ENOMEM;

160 161 162 163
	ret = mdio_gpio_get_data(&pdev->dev, bitbang);
	if (ret)
		return ret;

164 165
	if (pdev->dev.of_node) {
		bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
166 167 168 169
		if (bus_id < 0) {
			dev_warn(&pdev->dev, "failed to get alias id\n");
			bus_id = 0;
		}
170 171 172
	} else {
		bus_id = pdev->id;
	}
173

174
	new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, bus_id);
175 176 177
	if (!new_bus)
		return -ENODEV;

178 179 180 181 182
	if (pdev->dev.of_node)
		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
	else
		ret = mdiobus_register(new_bus);

183 184 185 186
	if (ret)
		mdio_gpio_bus_deinit(&pdev->dev);

	return ret;
187 188
}

B
Bill Pemberton 已提交
189
static int mdio_gpio_remove(struct platform_device *pdev)
190 191 192 193 194 195
{
	mdio_gpio_bus_destroy(&pdev->dev);

	return 0;
}

196
static const struct of_device_id mdio_gpio_of_match[] = {
197 198
	{ .compatible = "virtual,mdio-gpio", },
	{ /* sentinel */ }
199
};
200
MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
201

202 203
static struct platform_driver mdio_gpio_driver = {
	.probe = mdio_gpio_probe,
B
Bill Pemberton 已提交
204
	.remove = mdio_gpio_remove,
205 206
	.driver		= {
		.name	= "mdio-gpio",
207
		.of_match_table = mdio_gpio_of_match,
208 209 210
	},
};

211
module_platform_driver(mdio_gpio_driver);
212

213 214 215 216
MODULE_ALIAS("platform:mdio-gpio");
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");