blob: 33984b737233c8cb13d9d0dbf9a2e0d6d915601e [file] [log] [blame]
Laurent Pincharta5edecc2008-05-26 11:53:21 +02001/*
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +00002 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
Laurent Pincharta5edecc2008-05-26 11:53:21 +02004 *
5 * Copyright (c) 2008 CSE Semaphore Belgium.
6 * by Laurent Pinchart <laurentp@cse-semaphore.com>
7 *
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +00008 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
9 *
Laurent Pincharta5edecc2008-05-26 11:53:21 +020010 * 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>
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000027#include <linux/platform_device.h>
28#include <linux/gpio.h>
29#include <linux/mdio-gpio.h>
30
31#ifdef CONFIG_OF_GPIO
Laurent Pincharta5edecc2008-05-26 11:53:21 +020032#include <linux/of_gpio.h>
33#include <linux/of_platform.h>
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000034#endif
Laurent Pincharta5edecc2008-05-26 11:53:21 +020035
36struct mdio_gpio_info {
37 struct mdiobb_ctrl ctrl;
38 int mdc, mdio;
39};
40
41static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
42{
43 struct mdio_gpio_info *bitbang =
44 container_of(ctrl, struct mdio_gpio_info, ctrl);
45
46 if (dir)
47 gpio_direction_output(bitbang->mdio, 1);
48 else
49 gpio_direction_input(bitbang->mdio);
50}
51
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000052static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020053{
54 struct mdio_gpio_info *bitbang =
55 container_of(ctrl, struct mdio_gpio_info, ctrl);
56
57 return gpio_get_value(bitbang->mdio);
58}
59
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000060static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020061{
62 struct mdio_gpio_info *bitbang =
63 container_of(ctrl, struct mdio_gpio_info, ctrl);
64
65 gpio_set_value(bitbang->mdio, what);
66}
67
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000068static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020069{
70 struct mdio_gpio_info *bitbang =
71 container_of(ctrl, struct mdio_gpio_info, ctrl);
72
73 gpio_set_value(bitbang->mdc, what);
74}
75
76static struct mdiobb_ops mdio_gpio_ops = {
77 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000078 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +020079 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000080 .set_mdio_data = mdio_set,
81 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +020082};
83
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000084static int __devinit mdio_gpio_bus_init(struct device *dev,
85 struct mdio_gpio_platform_data *pdata,
86 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020087{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000088 struct mii_bus *new_bus;
89 struct mdio_gpio_info *bitbang;
90 int ret = -ENOMEM;
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 ret = -ENODEV;
108
109 new_bus->phy_mask = pdata->phy_mask;
110 new_bus->irq = pdata->irqs;
111 new_bus->parent = dev;
112
113 if (new_bus->phy_mask == ~0)
114 goto out_free_bus;
115
116 for (i = 0; i < PHY_MAX_ADDR; i++)
117 if (!new_bus->irq[i])
118 new_bus->irq[i] = PHY_POLL;
119
120 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
121
122 if (gpio_request(bitbang->mdc, "mdc"))
123 goto out_free_bus;
124
125 if (gpio_request(bitbang->mdio, "mdio"))
126 goto out_free_mdc;
127
Paulius Zaleckas664f93b2009-02-08 23:46:01 +0000128 gpio_direction_output(bitbang->mdc, 0);
129
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000130 dev_set_drvdata(dev, new_bus);
131
132 ret = mdiobus_register(new_bus);
133 if (ret)
134 goto out_free_all;
135
136 return 0;
137
138out_free_all:
139 dev_set_drvdata(dev, NULL);
140 gpio_free(bitbang->mdio);
141out_free_mdc:
142 gpio_free(bitbang->mdc);
143out_free_bus:
144 free_mdio_bitbang(new_bus);
145out_free_bitbang:
146 kfree(bitbang);
147out:
148 return ret;
149}
150
151static void __devexit mdio_gpio_bus_destroy(struct device *dev)
152{
153 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200154 struct mdio_gpio_info *bitbang = bus->priv;
155
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000156 mdiobus_unregister(bus);
157 free_mdio_bitbang(bus);
158 dev_set_drvdata(dev, NULL);
159 gpio_free(bitbang->mdc);
160 gpio_free(bitbang->mdio);
161 kfree(bitbang);
162}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200163
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000164static int __devinit mdio_gpio_probe(struct platform_device *pdev)
165{
166 struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
167
168 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200169 return -ENODEV;
170
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000171 return mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
172}
173
174static int __devexit mdio_gpio_remove(struct platform_device *pdev)
175{
176 mdio_gpio_bus_destroy(&pdev->dev);
177
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200178 return 0;
179}
180
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000181#ifdef CONFIG_OF_GPIO
182static void __devinit add_phy(struct mdio_gpio_platform_data *pdata,
183 struct device_node *np)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200184{
185 const u32 *data;
186 int len, id, irq;
187
188 data = of_get_property(np, "reg", &len);
189 if (!data || len != 4)
190 return;
191
192 id = *data;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000193 pdata->phy_mask &= ~(1 << id);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200194
195 irq = of_irq_to_resource(np, 0, NULL);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000196 if (irq)
197 pdata->irqs[id] = irq;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200198}
199
200static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
201 const struct of_device_id *match)
202{
203 struct device_node *np = NULL;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000204 struct mdio_gpio_platform_data *pdata;
Roel Kluin57a574992009-01-19 17:14:21 -0800205 int ret;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200206
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000207 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
208 if (!pdata)
209 return -ENOMEM;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200210
Roel Kluin57a574992009-01-19 17:14:21 -0800211 ret = of_get_gpio(ofdev->node, 0);
212 if (ret < 0)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000213 goto out_free;
Roel Kluin57a574992009-01-19 17:14:21 -0800214 pdata->mdc = ret;
215
216 ret = of_get_gpio(ofdev->node, 1);
217 if (ret < 0)
218 goto out_free;
219 pdata->mdio = ret;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200220
221 while ((np = of_get_next_child(ofdev->node, np)))
222 if (!strcmp(np->type, "ethernet-phy"))
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000223 add_phy(pdata, np);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200224
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000225 return mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200226
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000227out_free:
228 kfree(pdata);
229 return -ENODEV;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200230}
231
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000232static int __devexit mdio_ofgpio_remove(struct of_device *ofdev)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200233{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000234 mdio_gpio_bus_destroy(&ofdev->dev);
235 kfree(ofdev->dev.platform_data);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200236
237 return 0;
238}
239
240static struct of_device_id mdio_ofgpio_match[] = {
241 {
242 .compatible = "virtual,mdio-gpio",
243 },
244 {},
245};
246
247static struct of_platform_driver mdio_ofgpio_driver = {
248 .name = "mdio-gpio",
249 .match_table = mdio_ofgpio_match,
250 .probe = mdio_ofgpio_probe,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000251 .remove = __devexit_p(mdio_ofgpio_remove),
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200252};
253
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000254static inline int __init mdio_ofgpio_init(void)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200255{
256 return of_register_platform_driver(&mdio_ofgpio_driver);
257}
258
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000259static inline void __exit mdio_ofgpio_exit(void)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200260{
261 of_unregister_platform_driver(&mdio_ofgpio_driver);
262}
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000263#else
264static inline int __init mdio_ofgpio_init(void) { return 0; }
265static inline void __exit mdio_ofgpio_exit(void) { }
266#endif /* CONFIG_OF_GPIO */
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200267
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000268static struct platform_driver mdio_gpio_driver = {
269 .probe = mdio_gpio_probe,
270 .remove = __devexit_p(mdio_gpio_remove),
271 .driver = {
272 .name = "mdio-gpio",
273 .owner = THIS_MODULE,
274 },
275};
276
277static int __init mdio_gpio_init(void)
278{
279 int ret;
280
281 ret = mdio_ofgpio_init();
282 if (ret)
283 return ret;
284
285 ret = platform_driver_register(&mdio_gpio_driver);
286 if (ret)
287 mdio_ofgpio_exit();
288
289 return ret;
290}
291module_init(mdio_gpio_init);
292
293static void __exit mdio_gpio_exit(void)
294{
295 platform_driver_unregister(&mdio_gpio_driver);
296 mdio_ofgpio_exit();
297}
298module_exit(mdio_gpio_exit);
299
300MODULE_ALIAS("platform:mdio-gpio");
301MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
302MODULE_LICENSE("GPL");
303MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");