blob: 899274f2f9b1dd1da0aac0442807b6e42b0cddc2 [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
Laurent Pincharta5edecc2008-05-26 11:53:21 +020031#include <linux/of_gpio.h>
Mark Waredacac4d2009-07-23 10:56:48 -070032#include <linux/of_mdio.h>
Laurent Pincharta5edecc2008-05-26 11:53:21 +020033
34struct mdio_gpio_info {
35 struct mdiobb_ctrl ctrl;
36 int mdc, mdio;
37};
38
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000039static 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 int ret;
44
45 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
46 if (!pdata)
47 return NULL;
48
49 ret = of_get_gpio(np, 0);
50 if (ret < 0)
51 return NULL;
52
53 pdata->mdc = ret;
54
55 ret = of_get_gpio(np, 1);
56 if (ret < 0)
57 return NULL;
58 pdata->mdio = ret;
59
60 return pdata;
61}
62
Laurent Pincharta5edecc2008-05-26 11:53:21 +020063static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
64{
65 struct mdio_gpio_info *bitbang =
66 container_of(ctrl, struct mdio_gpio_info, ctrl);
67
68 if (dir)
69 gpio_direction_output(bitbang->mdio, 1);
70 else
71 gpio_direction_input(bitbang->mdio);
72}
73
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000074static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020075{
76 struct mdio_gpio_info *bitbang =
77 container_of(ctrl, struct mdio_gpio_info, ctrl);
78
79 return gpio_get_value(bitbang->mdio);
80}
81
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000082static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020083{
84 struct mdio_gpio_info *bitbang =
85 container_of(ctrl, struct mdio_gpio_info, ctrl);
86
87 gpio_set_value(bitbang->mdio, what);
88}
89
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000090static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020091{
92 struct mdio_gpio_info *bitbang =
93 container_of(ctrl, struct mdio_gpio_info, ctrl);
94
95 gpio_set_value(bitbang->mdc, what);
96}
97
98static struct mdiobb_ops mdio_gpio_ops = {
99 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000100 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200101 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000102 .set_mdio_data = mdio_set,
103 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200104};
105
Mark Waredacac4d2009-07-23 10:56:48 -0700106static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000107 struct mdio_gpio_platform_data *pdata,
108 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200109{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000110 struct mii_bus *new_bus;
111 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000112 int i;
113
114 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
115 if (!bitbang)
116 goto out;
117
118 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +0000119 bitbang->ctrl.reset = pdata->reset;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000120 bitbang->mdc = pdata->mdc;
121 bitbang->mdio = pdata->mdio;
122
123 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
124 if (!new_bus)
125 goto out_free_bitbang;
126
127 new_bus->name = "GPIO Bitbanged MDIO",
128
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000129 new_bus->phy_mask = pdata->phy_mask;
130 new_bus->irq = pdata->irqs;
131 new_bus->parent = dev;
132
133 if (new_bus->phy_mask == ~0)
134 goto out_free_bus;
135
136 for (i = 0; i < PHY_MAX_ADDR; i++)
137 if (!new_bus->irq[i])
138 new_bus->irq[i] = PHY_POLL;
139
Florian Fainellia77e9292012-01-09 23:59:26 +0000140 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000141
142 if (gpio_request(bitbang->mdc, "mdc"))
143 goto out_free_bus;
144
145 if (gpio_request(bitbang->mdio, "mdio"))
146 goto out_free_mdc;
147
Paulius Zaleckas664f93b2009-02-08 23:46:01 +0000148 gpio_direction_output(bitbang->mdc, 0);
149
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000150 dev_set_drvdata(dev, new_bus);
151
Mark Waredacac4d2009-07-23 10:56:48 -0700152 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000153
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000154out_free_mdc:
155 gpio_free(bitbang->mdc);
156out_free_bus:
157 free_mdio_bitbang(new_bus);
158out_free_bitbang:
159 kfree(bitbang);
160out:
Mark Waredacac4d2009-07-23 10:56:48 -0700161 return NULL;
162}
163
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000164static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700165{
166 struct mii_bus *bus = dev_get_drvdata(dev);
167 struct mdio_gpio_info *bitbang = bus->priv;
168
169 dev_set_drvdata(dev, NULL);
170 gpio_free(bitbang->mdio);
171 gpio_free(bitbang->mdc);
172 free_mdio_bitbang(bus);
173 kfree(bitbang);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000174}
175
176static void __devexit mdio_gpio_bus_destroy(struct device *dev)
177{
178 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200179
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000180 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700181 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000182}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200183
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000184static int __devinit mdio_gpio_probe(struct platform_device *pdev)
185{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000186 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700187 struct mii_bus *new_bus;
188 int ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000189
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000190 if (pdev->dev.of_node)
191 pdata = mdio_gpio_of_get_data(pdev);
192 else
193 pdata = pdev->dev.platform_data;
194
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000195 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200196 return -ENODEV;
197
Mark Waredacac4d2009-07-23 10:56:48 -0700198 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
199 if (!new_bus)
200 return -ENODEV;
201
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000202 if (pdev->dev.of_node)
203 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
204 else
205 ret = mdiobus_register(new_bus);
206
Mark Waredacac4d2009-07-23 10:56:48 -0700207 if (ret)
208 mdio_gpio_bus_deinit(&pdev->dev);
209
210 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000211}
212
213static int __devexit mdio_gpio_remove(struct platform_device *pdev)
214{
215 mdio_gpio_bus_destroy(&pdev->dev);
216
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200217 return 0;
218}
219
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000220static struct of_device_id mdio_gpio_of_match[] = {
221 { .compatible = "virtual,mdio-gpio", },
222 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200223};
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200224
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000225static struct platform_driver mdio_gpio_driver = {
226 .probe = mdio_gpio_probe,
227 .remove = __devexit_p(mdio_gpio_remove),
228 .driver = {
229 .name = "mdio-gpio",
230 .owner = THIS_MODULE,
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000231 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000232 },
233};
234
235static int __init mdio_gpio_init(void)
236{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000237 return platform_driver_register(&mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000238}
239module_init(mdio_gpio_init);
240
241static void __exit mdio_gpio_exit(void)
242{
243 platform_driver_unregister(&mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000244}
245module_exit(mdio_gpio_exit);
246
247MODULE_ALIAS("platform:mdio-gpio");
248MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
249MODULE_LICENSE("GPL");
250MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");