mdio-gpio.c 5.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
#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
static void *mdio_gpio_of_get_data(struct device *dev)
39 40 41
{
	struct mdio_gpio_platform_data *pdata;

42
	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
43 44 45
	if (!pdata)
		return NULL;

46 47 48
	pdata->mdc = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
	if (IS_ERR(pdata->mdc))
		return ERR_CAST(pdata->mdc);
49

50 51 52
	pdata->mdio = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN);
	if (IS_ERR(pdata->mdio))
		return ERR_CAST(pdata->mdio);
53

54 55 56
	pdata->mdo = devm_gpiod_get_index_optional(dev, NULL, 2, GPIOD_OUT_LOW);
	if (IS_ERR(pdata->mdo))
		return ERR_CAST(pdata->mdo);
57

58 59 60
	return pdata;
}

61 62 63 64 65
static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

66 67 68 69 70 71
	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.
		 */
72
		gpiod_set_value(bitbang->mdo, 1);
73 74 75
		return;
	}

76
	if (dir)
77
		gpiod_direction_output(bitbang->mdio, 1);
78
	else
79
		gpiod_direction_input(bitbang->mdio);
80 81
}

82
static int mdio_get(struct mdiobb_ctrl *ctrl)
83 84 85 86
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

87
	return gpiod_get_value(bitbang->mdio);
88 89
}

90
static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
91 92 93 94
{
	struct mdio_gpio_info *bitbang =
		container_of(ctrl, struct mdio_gpio_info, ctrl);

95
	if (bitbang->mdo)
96
		gpiod_set_value(bitbang->mdo, what);
97
	else
98
		gpiod_set_value(bitbang->mdio, what);
99 100
}

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

106
	gpiod_set_value(bitbang->mdc, what);
107 108
}

109
static const struct mdiobb_ops mdio_gpio_ops = {
110
	.owner = THIS_MODULE,
111
	.set_mdc = mdc_set,
112
	.set_mdio_dir = mdio_dir,
113 114
	.set_mdio_data = mdio_set,
	.get_mdio_data = mdio_get,
115 116
};

B
Bill Pemberton 已提交
117
static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
118 119
					  struct mdio_gpio_platform_data *pdata,
					  int bus_id)
120 121 122 123
{
	struct mii_bus *new_bus;
	struct mdio_gpio_info *bitbang;

124
	bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
125
	if (!bitbang)
126
		return NULL;
127 128

	bitbang->ctrl.ops = &mdio_gpio_ops;
129 130 131
	bitbang->mdc = pdata->mdc;
	bitbang->mdio = pdata->mdio;
	bitbang->mdo = pdata->mdo;
132 133 134

	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
	if (!new_bus)
135
		return NULL;
136

137
	new_bus->name = "GPIO Bitbanged MDIO";
138 139
	new_bus->parent = dev;

140 141 142 143
	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);
144 145

	dev_set_drvdata(dev, new_bus);
146

147
	return new_bus;
148 149
}

150
static void mdio_gpio_bus_deinit(struct device *dev)
151
{
152
	struct mii_bus *bus = dev_get_drvdata(dev);
153

154
	free_mdio_bitbang(bus);
155 156
}

B
Bill Pemberton 已提交
157
static void mdio_gpio_bus_destroy(struct device *dev)
158 159 160 161 162 163 164
{
	struct mii_bus *bus = dev_get_drvdata(dev);

	mdiobus_unregister(bus);
	mdio_gpio_bus_deinit(dev);
}

B
Bill Pemberton 已提交
165
static int mdio_gpio_probe(struct platform_device *pdev)
166
{
167
	struct mdio_gpio_platform_data *pdata;
168
	struct mii_bus *new_bus;
169
	int ret, bus_id;
170

171
	if (pdev->dev.of_node) {
172
		pdata = mdio_gpio_of_get_data(&pdev->dev);
173
		bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
174 175 176 177
		if (bus_id < 0) {
			dev_warn(&pdev->dev, "failed to get alias id\n");
			bus_id = 0;
		}
178
	} else {
179
		pdata = dev_get_platdata(&pdev->dev);
180 181
		bus_id = pdev->id;
	}
182

183 184 185
	if (!pdata)
		return -ENODEV;

186
	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
187 188 189
	if (!new_bus)
		return -ENODEV;

190 191 192 193 194
	if (pdev->dev.of_node)
		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
	else
		ret = mdiobus_register(new_bus);

195 196 197 198
	if (ret)
		mdio_gpio_bus_deinit(&pdev->dev);

	return ret;
199 200
}

B
Bill Pemberton 已提交
201
static int mdio_gpio_remove(struct platform_device *pdev)
202 203 204 205 206 207
{
	mdio_gpio_bus_destroy(&pdev->dev);

	return 0;
}

208
static const struct of_device_id mdio_gpio_of_match[] = {
209 210
	{ .compatible = "virtual,mdio-gpio", },
	{ /* sentinel */ }
211
};
212
MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
213

214 215
static struct platform_driver mdio_gpio_driver = {
	.probe = mdio_gpio_probe,
B
Bill Pemberton 已提交
216
	.remove = mdio_gpio_remove,
217 218
	.driver		= {
		.name	= "mdio-gpio",
219
		.of_match_table = mdio_gpio_of_match,
220 221 222
	},
};

223
module_platform_driver(mdio_gpio_driver);
224

225 226 227 228
MODULE_ALIAS("platform:mdio-gpio");
MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");