mdio-gpio.c 5.0 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
#include <linux/platform_device.h>
27 28
#include <linux/mdio-bitbang.h>
#include <linux/mdio-gpio.h>
29
#include <linux/gpio/consumer.h>
30
#include <linux/of_mdio.h>
31 32 33

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

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

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

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

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

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

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

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

81
	return gpiod_get_value_cansleep(bitbang->mdio);
82 83
}

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

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

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

100
	gpiod_set_value_cansleep(bitbang->mdc, what);
101 102
}

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

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

	bitbang->ctrl.ops = &mdio_gpio_ops;

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

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

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

	dev_set_drvdata(dev, new_bus);
132

133
	return new_bus;
134 135
}

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

140
	free_mdio_bitbang(bus);
141 142
}

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

	mdiobus_unregister(bus);
	mdio_gpio_bus_deinit(dev);
}

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

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

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

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

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

179
	ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
180 181 182 183
	if (ret)
		mdio_gpio_bus_deinit(&pdev->dev);

	return ret;
184 185
}

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

	return 0;
}

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

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

208
module_platform_driver(mdio_gpio_driver);
209

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