blob: 89c5a3eccc12daa760fa0e1e6c73e392c0a81f3e [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>
Mark Waredacac4d2009-07-23 10:56:48 -070033#include <linux/of_mdio.h>
Laurent Pincharta5edecc2008-05-26 11:53:21 +020034#include <linux/of_platform.h>
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000035#endif
Laurent Pincharta5edecc2008-05-26 11:53:21 +020036
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
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000053static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020054{
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
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000061static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020062{
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
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000069static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020070{
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,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000079 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +020080 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000081 .set_mdio_data = mdio_set,
82 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +020083};
84
Mark Waredacac4d2009-07-23 10:56:48 -070085static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000086 struct mdio_gpio_platform_data *pdata,
87 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020088{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000089 struct mii_bus *new_bus;
90 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000091 int i;
92
93 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
94 if (!bitbang)
95 goto out;
96
97 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +000098 bitbang->ctrl.reset = pdata->reset;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000099 bitbang->mdc = pdata->mdc;
100 bitbang->mdio = pdata->mdio;
101
102 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
103 if (!new_bus)
104 goto out_free_bitbang;
105
106 new_bus->name = "GPIO Bitbanged MDIO",
107
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000108 new_bus->phy_mask = pdata->phy_mask;
109 new_bus->irq = pdata->irqs;
110 new_bus->parent = dev;
111
112 if (new_bus->phy_mask == ~0)
113 goto out_free_bus;
114
115 for (i = 0; i < PHY_MAX_ADDR; i++)
116 if (!new_bus->irq[i])
117 new_bus->irq[i] = PHY_POLL;
118
119 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
120
121 if (gpio_request(bitbang->mdc, "mdc"))
122 goto out_free_bus;
123
124 if (gpio_request(bitbang->mdio, "mdio"))
125 goto out_free_mdc;
126
Paulius Zaleckas664f93b2009-02-08 23:46:01 +0000127 gpio_direction_output(bitbang->mdc, 0);
128
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000129 dev_set_drvdata(dev, new_bus);
130
Mark Waredacac4d2009-07-23 10:56:48 -0700131 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000132
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000133out_free_mdc:
134 gpio_free(bitbang->mdc);
135out_free_bus:
136 free_mdio_bitbang(new_bus);
137out_free_bitbang:
138 kfree(bitbang);
139out:
Mark Waredacac4d2009-07-23 10:56:48 -0700140 return NULL;
141}
142
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000143static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700144{
145 struct mii_bus *bus = dev_get_drvdata(dev);
146 struct mdio_gpio_info *bitbang = bus->priv;
147
148 dev_set_drvdata(dev, NULL);
149 gpio_free(bitbang->mdio);
150 gpio_free(bitbang->mdc);
151 free_mdio_bitbang(bus);
152 kfree(bitbang);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000153}
154
155static void __devexit mdio_gpio_bus_destroy(struct device *dev)
156{
157 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200158
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000159 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700160 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000161}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200162
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000163static int __devinit mdio_gpio_probe(struct platform_device *pdev)
164{
165 struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
Mark Waredacac4d2009-07-23 10:56:48 -0700166 struct mii_bus *new_bus;
167 int ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000168
169 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200170 return -ENODEV;
171
Mark Waredacac4d2009-07-23 10:56:48 -0700172 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
173 if (!new_bus)
174 return -ENODEV;
175
176 ret = mdiobus_register(new_bus);
177 if (ret)
178 mdio_gpio_bus_deinit(&pdev->dev);
179
180 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000181}
182
183static int __devexit mdio_gpio_remove(struct platform_device *pdev)
184{
185 mdio_gpio_bus_destroy(&pdev->dev);
186
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200187 return 0;
188}
189
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000190#ifdef CONFIG_OF_GPIO
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200191
Grant Likely74888762011-02-22 21:05:51 -0700192static int __devinit mdio_ofgpio_probe(struct platform_device *ofdev)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200193{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000194 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700195 struct mii_bus *new_bus;
Roel Kluin57a57492009-01-19 17:14:21 -0800196 int ret;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200197
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000198 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
199 if (!pdata)
200 return -ENOMEM;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200201
Grant Likely61c7a082010-04-13 16:12:29 -0700202 ret = of_get_gpio(ofdev->dev.of_node, 0);
Roel Kluin57a57492009-01-19 17:14:21 -0800203 if (ret < 0)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000204 goto out_free;
Roel Kluin57a57492009-01-19 17:14:21 -0800205 pdata->mdc = ret;
206
Grant Likely61c7a082010-04-13 16:12:29 -0700207 ret = of_get_gpio(ofdev->dev.of_node, 1);
Roel Kluin57a57492009-01-19 17:14:21 -0800208 if (ret < 0)
Mark Waredacac4d2009-07-23 10:56:48 -0700209 goto out_free;
Roel Kluin57a57492009-01-19 17:14:21 -0800210 pdata->mdio = ret;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200211
Mark Waredacac4d2009-07-23 10:56:48 -0700212 new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
213 if (!new_bus)
Julia Lawalla4b11642009-09-11 06:22:09 +0000214 goto out_free;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200215
Grant Likely61c7a082010-04-13 16:12:29 -0700216 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
Mark Waredacac4d2009-07-23 10:56:48 -0700217 if (ret)
218 mdio_gpio_bus_deinit(&ofdev->dev);
219
220 return ret;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200221
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000222out_free:
223 kfree(pdata);
224 return -ENODEV;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200225}
226
Grant Likely2dc11582010-08-06 09:25:50 -0600227static int __devexit mdio_ofgpio_remove(struct platform_device *ofdev)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200228{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000229 mdio_gpio_bus_destroy(&ofdev->dev);
230 kfree(ofdev->dev.platform_data);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200231
232 return 0;
233}
234
235static struct of_device_id mdio_ofgpio_match[] = {
236 {
237 .compatible = "virtual,mdio-gpio",
238 },
239 {},
240};
Anton Vorontsove72701a2009-10-14 14:54:52 -0700241MODULE_DEVICE_TABLE(of, mdio_ofgpio_match);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200242
Grant Likely74888762011-02-22 21:05:51 -0700243static struct platform_driver mdio_ofgpio_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700244 .driver = {
Dirk Eibachf42af6c2011-10-18 03:04:11 +0000245 .name = "mdio-ofgpio",
Grant Likely40182942010-04-13 16:13:02 -0700246 .owner = THIS_MODULE,
247 .of_match_table = mdio_ofgpio_match,
248 },
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200249 .probe = mdio_ofgpio_probe,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000250 .remove = __devexit_p(mdio_ofgpio_remove),
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200251};
252
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000253static inline int __init mdio_ofgpio_init(void)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200254{
Grant Likely74888762011-02-22 21:05:51 -0700255 return platform_driver_register(&mdio_ofgpio_driver);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200256}
257
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000258static inline void __exit mdio_ofgpio_exit(void)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200259{
Grant Likely74888762011-02-22 21:05:51 -0700260 platform_driver_unregister(&mdio_ofgpio_driver);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200261}
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000262#else
263static inline int __init mdio_ofgpio_init(void) { return 0; }
264static inline void __exit mdio_ofgpio_exit(void) { }
265#endif /* CONFIG_OF_GPIO */
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200266
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000267static struct platform_driver mdio_gpio_driver = {
268 .probe = mdio_gpio_probe,
269 .remove = __devexit_p(mdio_gpio_remove),
270 .driver = {
271 .name = "mdio-gpio",
272 .owner = THIS_MODULE,
273 },
274};
275
276static int __init mdio_gpio_init(void)
277{
278 int ret;
279
280 ret = mdio_ofgpio_init();
281 if (ret)
282 return ret;
283
284 ret = platform_driver_register(&mdio_gpio_driver);
285 if (ret)
286 mdio_ofgpio_exit();
287
288 return ret;
289}
290module_init(mdio_gpio_init);
291
292static void __exit mdio_gpio_exit(void)
293{
294 platform_driver_unregister(&mdio_gpio_driver);
295 mdio_ofgpio_exit();
296}
297module_exit(mdio_gpio_exit);
298
299MODULE_ALIAS("platform:mdio-gpio");
300MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
301MODULE_LICENSE("GPL");
302MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");