Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * GPIO based MDIO bitbang driver.
  3 * Supports OpenFirmware.
  4 *
  5 * Copyright (c) 2008 CSE Semaphore Belgium.
  6 *  by Laurent Pinchart <laurentp@cse-semaphore.com>
  7 *
  8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  9 *
 10 * Based on earlier work by
 11 *
 12 * Copyright (c) 2003 Intracom S.A.
 13 *  by Pantelis Antoniou <panto@intracom.gr>
 14 *
 15 * 2005 (c) MontaVista Software, Inc.
 16 * Vitaly Bordug <vbordug@ru.mvista.com>
 17 *
 18 * This file is licensed under the terms of the GNU General Public License
 19 * version 2. This program is licensed "as is" without any warranty of any
 20 * kind, whether express or implied.
 21 */
 22
 23#include <linux/module.h>
 24#include <linux/slab.h>
 25#include <linux/interrupt.h>
 26#include <linux/platform_device.h>
 27#include <linux/gpio.h>
 
 28#include <linux/mdio-gpio.h>
 29
 30#include <linux/of_gpio.h>
 31#include <linux/of_mdio.h>
 32
 33struct mdio_gpio_info {
 34	struct mdiobb_ctrl ctrl;
 35	int mdc, mdio, mdo;
 36	int mdc_active_low, mdio_active_low, mdo_active_low;
 37};
 38
 39static void *mdio_gpio_of_get_data(struct platform_device *pdev)
 
 40{
 41	struct device_node *np = pdev->dev.of_node;
 42	struct mdio_gpio_platform_data *pdata;
 43	enum of_gpio_flags flags;
 44	int ret;
 45
 46	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 47	if (!pdata)
 48		return NULL;
 49
 50	ret = of_get_gpio_flags(np, 0, &flags);
 51	if (ret < 0)
 52		return NULL;
 53
 54	pdata->mdc = ret;
 55	pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
 56
 57	ret = of_get_gpio_flags(np, 1, &flags);
 58	if (ret < 0)
 59		return NULL;
 60	pdata->mdio = ret;
 61	pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
 62
 63	ret = of_get_gpio_flags(np, 2, &flags);
 64	if (ret > 0) {
 65		pdata->mdo = ret;
 66		pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW;
 67	}
 68
 69	return pdata;
 70}
 71
 72static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
 73{
 74	struct mdio_gpio_info *bitbang =
 75		container_of(ctrl, struct mdio_gpio_info, ctrl);
 76
 77	if (bitbang->mdo) {
 78		/* Separate output pin. Always set its value to high
 79		 * when changing direction. If direction is input,
 80		 * assume the pin serves as pull-up. If direction is
 81		 * output, the default value is high.
 82		 */
 83		gpio_set_value(bitbang->mdo, 1 ^ bitbang->mdo_active_low);
 84		return;
 85	}
 86
 87	if (dir)
 88		gpio_direction_output(bitbang->mdio,
 89				      1 ^ bitbang->mdio_active_low);
 90	else
 91		gpio_direction_input(bitbang->mdio);
 92}
 93
 94static int mdio_get(struct mdiobb_ctrl *ctrl)
 95{
 96	struct mdio_gpio_info *bitbang =
 97		container_of(ctrl, struct mdio_gpio_info, ctrl);
 98
 99	return gpio_get_value(bitbang->mdio) ^ bitbang->mdio_active_low;
100}
101
102static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
103{
104	struct mdio_gpio_info *bitbang =
105		container_of(ctrl, struct mdio_gpio_info, ctrl);
106
107	if (bitbang->mdo)
108		gpio_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low);
109	else
110		gpio_set_value(bitbang->mdio, what ^ bitbang->mdio_active_low);
111}
112
113static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
114{
115	struct mdio_gpio_info *bitbang =
116		container_of(ctrl, struct mdio_gpio_info, ctrl);
117
118	gpio_set_value(bitbang->mdc, what ^ bitbang->mdc_active_low);
119}
120
121static struct mdiobb_ops mdio_gpio_ops = {
122	.owner = THIS_MODULE,
123	.set_mdc = mdc_set,
124	.set_mdio_dir = mdio_dir,
125	.set_mdio_data = mdio_set,
126	.get_mdio_data = mdio_get,
127};
128
129static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
130					  struct mdio_gpio_platform_data *pdata,
131					  int bus_id)
132{
 
133	struct mii_bus *new_bus;
134	struct mdio_gpio_info *bitbang;
135	int i;
136
137	bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
138	if (!bitbang)
139		goto out;
140
141	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	bitbang->mdo = pdata->mdo;
148	bitbang->mdo_active_low = pdata->mdo_active_low;
149
150	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
151	if (!new_bus)
152		goto out;
153
154	new_bus->name = "GPIO Bitbanged MDIO",
155
156	new_bus->phy_mask = pdata->phy_mask;
157	new_bus->irq = pdata->irqs;
158	new_bus->parent = dev;
159
160	if (new_bus->phy_mask == ~0)
161		goto out_free_bus;
162
163	for (i = 0; i < PHY_MAX_ADDR; i++)
164		if (!new_bus->irq[i])
165			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	if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
173		goto out_free_bus;
174
175	if (bitbang->mdo) {
176		if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
177			goto out_free_bus;
178		gpio_direction_output(bitbang->mdo, 1);
179		gpio_direction_input(bitbang->mdio);
180	}
181
182	gpio_direction_output(bitbang->mdc, 0);
183
184	dev_set_drvdata(dev, new_bus);
185
186	return new_bus;
187
188out_free_bus:
189	free_mdio_bitbang(new_bus);
190out:
191	return NULL;
192}
193
194static 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
201static void mdio_gpio_bus_destroy(struct device *dev)
202{
203	struct mii_bus *bus = dev_get_drvdata(dev);
204
205	mdiobus_unregister(bus);
206	mdio_gpio_bus_deinit(dev);
207}
208
209static 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		if (bus_id < 0) {
219			dev_warn(&pdev->dev, "failed to get alias id\n");
220			bus_id = 0;
221		}
222	} else {
223		pdata = dev_get_platdata(&pdev->dev);
224		bus_id = pdev->id;
225	}
226
227	if (!pdata)
228		return -ENODEV;
229
230	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
231	if (!new_bus)
232		return -ENODEV;
233
234	if (pdev->dev.of_node)
235		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
236	else
237		ret = mdiobus_register(new_bus);
238
239	if (ret)
240		mdio_gpio_bus_deinit(&pdev->dev);
241
242	return ret;
243}
244
245static int mdio_gpio_remove(struct platform_device *pdev)
246{
247	mdio_gpio_bus_destroy(&pdev->dev);
248
249	return 0;
250}
251
252static struct of_device_id mdio_gpio_of_match[] = {
253	{ .compatible = "virtual,mdio-gpio", },
254	{ /* sentinel */ }
255};
 
256
257static struct platform_driver mdio_gpio_driver = {
258	.probe = mdio_gpio_probe,
259	.remove = mdio_gpio_remove,
260	.driver		= {
261		.name	= "mdio-gpio",
262		.owner	= THIS_MODULE,
263		.of_match_table = mdio_gpio_of_match,
264	},
265};
266
267module_platform_driver(mdio_gpio_driver);
268
269MODULE_ALIAS("platform:mdio-gpio");
270MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
271MODULE_LICENSE("GPL");
272MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * GPIO based MDIO bitbang driver.
  4 * Supports OpenFirmware.
  5 *
  6 * Copyright (c) 2008 CSE Semaphore Belgium.
  7 *  by Laurent Pinchart <laurentp@cse-semaphore.com>
  8 *
  9 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
 10 *
 11 * Based on earlier work by
 12 *
 13 * Copyright (c) 2003 Intracom S.A.
 14 *  by Pantelis Antoniou <panto@intracom.gr>
 15 *
 16 * 2005 (c) MontaVista Software, Inc.
 17 * Vitaly Bordug <vbordug@ru.mvista.com>
 
 
 
 
 18 */
 19
 20#include <linux/module.h>
 21#include <linux/slab.h>
 22#include <linux/interrupt.h>
 23#include <linux/platform_device.h>
 24#include <linux/platform_data/mdio-gpio.h>
 25#include <linux/mdio-bitbang.h>
 26#include <linux/mdio-gpio.h>
 27#include <linux/gpio/consumer.h>
 
 28#include <linux/of_mdio.h>
 29
 30struct mdio_gpio_info {
 31	struct mdiobb_ctrl ctrl;
 32	struct gpio_desc *mdc, *mdio, *mdo;
 
 33};
 34
 35static int mdio_gpio_get_data(struct device *dev,
 36			      struct mdio_gpio_info *bitbang)
 37{
 38	bitbang->mdc = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDC,
 39					    GPIOD_OUT_LOW);
 40	if (IS_ERR(bitbang->mdc))
 41		return PTR_ERR(bitbang->mdc);
 42
 43	bitbang->mdio = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDIO,
 44					     GPIOD_IN);
 45	if (IS_ERR(bitbang->mdio))
 46		return PTR_ERR(bitbang->mdio);
 47
 48	bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, MDIO_GPIO_MDO,
 49						     GPIOD_OUT_LOW);
 50	return PTR_ERR_OR_ZERO(bitbang->mdo);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 51}
 52
 53static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
 54{
 55	struct mdio_gpio_info *bitbang =
 56		container_of(ctrl, struct mdio_gpio_info, ctrl);
 57
 58	if (bitbang->mdo) {
 59		/* Separate output pin. Always set its value to high
 60		 * when changing direction. If direction is input,
 61		 * assume the pin serves as pull-up. If direction is
 62		 * output, the default value is high.
 63		 */
 64		gpiod_set_value_cansleep(bitbang->mdo, 1);
 65		return;
 66	}
 67
 68	if (dir)
 69		gpiod_direction_output(bitbang->mdio, 1);
 
 70	else
 71		gpiod_direction_input(bitbang->mdio);
 72}
 73
 74static int mdio_get(struct mdiobb_ctrl *ctrl)
 75{
 76	struct mdio_gpio_info *bitbang =
 77		container_of(ctrl, struct mdio_gpio_info, ctrl);
 78
 79	return gpiod_get_value_cansleep(bitbang->mdio);
 80}
 81
 82static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
 83{
 84	struct mdio_gpio_info *bitbang =
 85		container_of(ctrl, struct mdio_gpio_info, ctrl);
 86
 87	if (bitbang->mdo)
 88		gpiod_set_value_cansleep(bitbang->mdo, what);
 89	else
 90		gpiod_set_value_cansleep(bitbang->mdio, what);
 91}
 92
 93static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
 94{
 95	struct mdio_gpio_info *bitbang =
 96		container_of(ctrl, struct mdio_gpio_info, ctrl);
 97
 98	gpiod_set_value_cansleep(bitbang->mdc, what);
 99}
100
101static const struct mdiobb_ops mdio_gpio_ops = {
102	.owner = THIS_MODULE,
103	.set_mdc = mdc_set,
104	.set_mdio_dir = mdio_dir,
105	.set_mdio_data = mdio_set,
106	.get_mdio_data = mdio_get,
107};
108
109static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
110					  struct mdio_gpio_info *bitbang,
111					  int bus_id)
112{
113	struct mdio_gpio_platform_data *pdata = dev_get_platdata(dev);
114	struct mii_bus *new_bus;
 
 
 
 
 
 
115
116	bitbang->ctrl.ops = &mdio_gpio_ops;
 
 
 
 
 
 
 
117
118	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
119	if (!new_bus)
120		return NULL;
 
 
121
122	new_bus->name = "GPIO Bitbanged MDIO";
 
123	new_bus->parent = dev;
124
125	if (bus_id != -1)
126		snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
127	else
128		strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
 
 
 
 
 
 
 
 
 
 
129
130	if (pdata) {
131		new_bus->phy_mask = pdata->phy_mask;
132		new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
 
 
133	}
134
 
 
135	dev_set_drvdata(dev, new_bus);
136
137	return new_bus;
 
 
 
 
 
138}
139
140static void mdio_gpio_bus_deinit(struct device *dev)
141{
142	struct mii_bus *bus = dev_get_drvdata(dev);
143
144	free_mdio_bitbang(bus);
145}
146
147static void mdio_gpio_bus_destroy(struct device *dev)
148{
149	struct mii_bus *bus = dev_get_drvdata(dev);
150
151	mdiobus_unregister(bus);
152	mdio_gpio_bus_deinit(dev);
153}
154
155static int mdio_gpio_probe(struct platform_device *pdev)
156{
157	struct mdio_gpio_info *bitbang;
158	struct mii_bus *new_bus;
159	int ret, bus_id;
160
161	bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
162	if (!bitbang)
163		return -ENOMEM;
164
165	ret = mdio_gpio_get_data(&pdev->dev, bitbang);
166	if (ret)
167		return ret;
168
169	if (pdev->dev.of_node) {
 
170		bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
171		if (bus_id < 0) {
172			dev_warn(&pdev->dev, "failed to get alias id\n");
173			bus_id = 0;
174		}
175	} else {
 
176		bus_id = pdev->id;
177	}
178
179	new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, bus_id);
 
 
 
180	if (!new_bus)
181		return -ENODEV;
182
183	ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
 
 
 
 
184	if (ret)
185		mdio_gpio_bus_deinit(&pdev->dev);
186
187	return ret;
188}
189
190static int mdio_gpio_remove(struct platform_device *pdev)
191{
192	mdio_gpio_bus_destroy(&pdev->dev);
193
194	return 0;
195}
196
197static const struct of_device_id mdio_gpio_of_match[] = {
198	{ .compatible = "virtual,mdio-gpio", },
199	{ /* sentinel */ }
200};
201MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
202
203static struct platform_driver mdio_gpio_driver = {
204	.probe = mdio_gpio_probe,
205	.remove = mdio_gpio_remove,
206	.driver		= {
207		.name	= "mdio-gpio",
 
208		.of_match_table = mdio_gpio_of_match,
209	},
210};
211
212module_platform_driver(mdio_gpio_driver);
213
214MODULE_ALIAS("platform:mdio-gpio");
215MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
216MODULE_LICENSE("GPL v2");
217MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");