Linux Audio

Check our new training course

Loading...
v3.1
  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/init.h>
 26#include <linux/interrupt.h>
 27#include <linux/platform_device.h>
 28#include <linux/gpio.h>
 29#include <linux/mdio-gpio.h>
 30
 31#ifdef CONFIG_OF_GPIO
 32#include <linux/of_gpio.h>
 33#include <linux/of_mdio.h>
 34#include <linux/of_platform.h>
 35#endif
 36
 37struct mdio_gpio_info {
 38	struct mdiobb_ctrl ctrl;
 39	int mdc, mdio;
 
 40};
 41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
 43{
 44	struct mdio_gpio_info *bitbang =
 45		container_of(ctrl, struct mdio_gpio_info, ctrl);
 46
 
 
 
 
 
 
 
 
 
 
 
 47	if (dir)
 48		gpio_direction_output(bitbang->mdio, 1);
 
 49	else
 50		gpio_direction_input(bitbang->mdio);
 51}
 52
 53static int mdio_get(struct mdiobb_ctrl *ctrl)
 54{
 55	struct mdio_gpio_info *bitbang =
 56		container_of(ctrl, struct mdio_gpio_info, ctrl);
 57
 58	return gpio_get_value(bitbang->mdio);
 
 59}
 60
 61static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
 62{
 63	struct mdio_gpio_info *bitbang =
 64		container_of(ctrl, struct mdio_gpio_info, ctrl);
 65
 66	gpio_set_value(bitbang->mdio, what);
 
 
 
 
 
 67}
 68
 69static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
 70{
 71	struct mdio_gpio_info *bitbang =
 72		container_of(ctrl, struct mdio_gpio_info, ctrl);
 73
 74	gpio_set_value(bitbang->mdc, what);
 75}
 76
 77static struct mdiobb_ops mdio_gpio_ops = {
 78	.owner = THIS_MODULE,
 79	.set_mdc = mdc_set,
 80	.set_mdio_dir = mdio_dir,
 81	.set_mdio_data = mdio_set,
 82	.get_mdio_data = mdio_get,
 83};
 84
 85static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev,
 86					struct mdio_gpio_platform_data *pdata,
 87					int bus_id)
 88{
 89	struct mii_bus *new_bus;
 90	struct mdio_gpio_info *bitbang;
 91	int i;
 92
 93	bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
 94	if (!bitbang)
 95		goto out;
 96
 97	bitbang->ctrl.ops = &mdio_gpio_ops;
 
 98	bitbang->mdc = pdata->mdc;
 
 99	bitbang->mdio = pdata->mdio;
 
 
 
100
101	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
102	if (!new_bus)
103		goto out_free_bitbang;
104
105	new_bus->name = "GPIO Bitbanged MDIO",
106
107	new_bus->phy_mask = pdata->phy_mask;
108	new_bus->irq = pdata->irqs;
 
109	new_bus->parent = dev;
110
111	if (new_bus->phy_mask == ~0)
112		goto out_free_bus;
113
114	for (i = 0; i < PHY_MAX_ADDR; i++)
115		if (!new_bus->irq[i])
116			new_bus->irq[i] = PHY_POLL;
117
118	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
 
 
 
119
120	if (gpio_request(bitbang->mdc, "mdc"))
121		goto out_free_bus;
122
123	if (gpio_request(bitbang->mdio, "mdio"))
124		goto out_free_mdc;
 
 
 
 
 
 
 
125
126	gpio_direction_output(bitbang->mdc, 0);
127
128	dev_set_drvdata(dev, new_bus);
129
130	return new_bus;
131
132out_free_mdc:
133	gpio_free(bitbang->mdc);
134out_free_bus:
135	free_mdio_bitbang(new_bus);
136out_free_bitbang:
137	kfree(bitbang);
138out:
139	return NULL;
140}
141
142static void mdio_gpio_bus_deinit(struct device *dev)
143{
144	struct mii_bus *bus = dev_get_drvdata(dev);
145	struct mdio_gpio_info *bitbang = bus->priv;
146
147	dev_set_drvdata(dev, NULL);
148	gpio_free(bitbang->mdio);
149	gpio_free(bitbang->mdc);
150	free_mdio_bitbang(bus);
151	kfree(bitbang);
152}
153
154static void __devexit mdio_gpio_bus_destroy(struct device *dev)
155{
156	struct mii_bus *bus = dev_get_drvdata(dev);
157
158	mdiobus_unregister(bus);
159	mdio_gpio_bus_deinit(dev);
160}
161
162static int __devinit mdio_gpio_probe(struct platform_device *pdev)
163{
164	struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
165	struct mii_bus *new_bus;
166	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
167
168	if (!pdata)
169		return -ENODEV;
170
171	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
172	if (!new_bus)
173		return -ENODEV;
174
175	ret = mdiobus_register(new_bus);
 
 
 
 
176	if (ret)
177		mdio_gpio_bus_deinit(&pdev->dev);
178
179	return ret;
180}
181
182static int __devexit mdio_gpio_remove(struct platform_device *pdev)
183{
184	mdio_gpio_bus_destroy(&pdev->dev);
185
186	return 0;
187}
188
189#ifdef CONFIG_OF_GPIO
190
191static int __devinit mdio_ofgpio_probe(struct platform_device *ofdev)
192{
193	struct mdio_gpio_platform_data *pdata;
194	struct mii_bus *new_bus;
195	int ret;
196
197	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
198	if (!pdata)
199		return -ENOMEM;
200
201	ret = of_get_gpio(ofdev->dev.of_node, 0);
202	if (ret < 0)
203		goto out_free;
204	pdata->mdc = ret;
205
206	ret = of_get_gpio(ofdev->dev.of_node, 1);
207	if (ret < 0)
208		goto out_free;
209	pdata->mdio = ret;
210
211	new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
212	if (!new_bus)
213		goto out_free;
214
215	ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
216	if (ret)
217		mdio_gpio_bus_deinit(&ofdev->dev);
218
219	return ret;
220
221out_free:
222	kfree(pdata);
223	return -ENODEV;
224}
225
226static int __devexit mdio_ofgpio_remove(struct platform_device *ofdev)
227{
228	mdio_gpio_bus_destroy(&ofdev->dev);
229	kfree(ofdev->dev.platform_data);
230
231	return 0;
232}
233
234static struct of_device_id mdio_ofgpio_match[] = {
235	{
236		.compatible = "virtual,mdio-gpio",
237	},
238	{},
239};
240MODULE_DEVICE_TABLE(of, mdio_ofgpio_match);
241
242static struct platform_driver mdio_ofgpio_driver = {
243	.driver = {
244		.name = "mdio-gpio",
245		.owner = THIS_MODULE,
246		.of_match_table = mdio_ofgpio_match,
247	},
248	.probe = mdio_ofgpio_probe,
249	.remove = __devexit_p(mdio_ofgpio_remove),
250};
251
252static inline int __init mdio_ofgpio_init(void)
253{
254	return platform_driver_register(&mdio_ofgpio_driver);
255}
256
257static inline void __exit mdio_ofgpio_exit(void)
258{
259	platform_driver_unregister(&mdio_ofgpio_driver);
260}
261#else
262static inline int __init mdio_ofgpio_init(void) { return 0; }
263static inline void __exit mdio_ofgpio_exit(void) { }
264#endif /* CONFIG_OF_GPIO */
265
266static struct platform_driver mdio_gpio_driver = {
267	.probe = mdio_gpio_probe,
268	.remove = __devexit_p(mdio_gpio_remove),
269	.driver		= {
270		.name	= "mdio-gpio",
271		.owner	= THIS_MODULE,
272	},
273};
274
275static int __init mdio_gpio_init(void)
276{
277	int ret;
278
279	ret = mdio_ofgpio_init();
280	if (ret)
281		return ret;
282
283	ret = platform_driver_register(&mdio_gpio_driver);
284	if (ret)
285		mdio_ofgpio_exit();
286
287	return ret;
288}
289module_init(mdio_gpio_init);
290
291static void __exit mdio_gpio_exit(void)
292{
293	platform_driver_unregister(&mdio_gpio_driver);
294	mdio_ofgpio_exit();
295}
296module_exit(mdio_gpio_exit);
297
298MODULE_ALIAS("platform:mdio-gpio");
299MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
300MODULE_LICENSE("GPL");
301MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");
v4.6
  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/platform_data/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_cansleep(bitbang->mdo,
 84					1 ^ bitbang->mdo_active_low);
 85		return;
 86	}
 87
 88	if (dir)
 89		gpio_direction_output(bitbang->mdio,
 90				      1 ^ bitbang->mdio_active_low);
 91	else
 92		gpio_direction_input(bitbang->mdio);
 93}
 94
 95static int mdio_get(struct mdiobb_ctrl *ctrl)
 96{
 97	struct mdio_gpio_info *bitbang =
 98		container_of(ctrl, struct mdio_gpio_info, ctrl);
 99
100	return gpio_get_value_cansleep(bitbang->mdio) ^
101		bitbang->mdio_active_low;
102}
103
104static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
105{
106	struct mdio_gpio_info *bitbang =
107		container_of(ctrl, struct mdio_gpio_info, ctrl);
108
109	if (bitbang->mdo)
110		gpio_set_value_cansleep(bitbang->mdo,
111					what ^ bitbang->mdo_active_low);
112	else
113		gpio_set_value_cansleep(bitbang->mdio,
114					what ^ bitbang->mdio_active_low);
115}
116
117static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
118{
119	struct mdio_gpio_info *bitbang =
120		container_of(ctrl, struct mdio_gpio_info, ctrl);
121
122	gpio_set_value_cansleep(bitbang->mdc, what ^ bitbang->mdc_active_low);
123}
124
125static struct mdiobb_ops mdio_gpio_ops = {
126	.owner = THIS_MODULE,
127	.set_mdc = mdc_set,
128	.set_mdio_dir = mdio_dir,
129	.set_mdio_data = mdio_set,
130	.get_mdio_data = mdio_get,
131};
132
133static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
134					  struct mdio_gpio_platform_data *pdata,
135					  int bus_id)
136{
137	struct mii_bus *new_bus;
138	struct mdio_gpio_info *bitbang;
139	int i;
140
141	bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
142	if (!bitbang)
143		goto out;
144
145	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	bitbang->mdo = pdata->mdo;
152	bitbang->mdo_active_low = pdata->mdo_active_low;
153
154	new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
155	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	memcpy(new_bus->irq, pdata->irqs, sizeof(new_bus->irq));
163	new_bus->parent = dev;
164
165	if (new_bus->phy_mask == ~0)
166		goto out_free_bus;
167
168	for (i = 0; i < PHY_MAX_ADDR; i++)
169		if (!new_bus->irq[i])
170			new_bus->irq[i] = PHY_POLL;
171
172	if (bus_id != -1)
173		snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
174	else
175		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	if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
181		goto out_free_bus;
182
183	if (bitbang->mdo) {
184		if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
185			goto out_free_bus;
186		gpio_direction_output(bitbang->mdo, 1);
187		gpio_direction_input(bitbang->mdio);
188	}
189
190	gpio_direction_output(bitbang->mdc, 0);
191
192	dev_set_drvdata(dev, new_bus);
193
194	return new_bus;
195
 
 
196out_free_bus:
197	free_mdio_bitbang(new_bus);
 
 
198out:
199	return NULL;
200}
201
202static 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
209static void mdio_gpio_bus_destroy(struct device *dev)
210{
211	struct mii_bus *bus = dev_get_drvdata(dev);
212
213	mdiobus_unregister(bus);
214	mdio_gpio_bus_deinit(dev);
215}
216
217static 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		if (bus_id < 0) {
227			dev_warn(&pdev->dev, "failed to get alias id\n");
228			bus_id = 0;
229		}
230	} else {
231		pdata = dev_get_platdata(&pdev->dev);
232		bus_id = pdev->id;
233	}
234
235	if (!pdata)
236		return -ENODEV;
237
238	new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
239	if (!new_bus)
240		return -ENODEV;
241
242	if (pdev->dev.of_node)
243		ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
244	else
245		ret = mdiobus_register(new_bus);
246
247	if (ret)
248		mdio_gpio_bus_deinit(&pdev->dev);
249
250	return ret;
251}
252
253static int mdio_gpio_remove(struct platform_device *pdev)
254{
255	mdio_gpio_bus_destroy(&pdev->dev);
256
257	return 0;
258}
259
260static const struct of_device_id mdio_gpio_of_match[] = {
261	{ .compatible = "virtual,mdio-gpio", },
262	{ /* sentinel */ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263};
264MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
 
 
 
 
 
 
 
 
 
 
 
 
 
265
266static struct platform_driver mdio_gpio_driver = {
267	.probe = mdio_gpio_probe,
268	.remove = mdio_gpio_remove,
269	.driver		= {
270		.name	= "mdio-gpio",
271		.of_match_table = mdio_gpio_of_match,
272	},
273};
274
275module_platform_driver(mdio_gpio_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
277MODULE_ALIAS("platform:mdio-gpio");
278MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
279MODULE_LICENSE("GPL");
280MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");