blob: e701433bf52f60a4fb42f0ca631cbd1fdbb68a2c [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>
Laurent Pincharta5edecc2008-05-26 11:53:21 +020025#include <linux/interrupt.h>
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000026#include <linux/platform_device.h>
27#include <linux/gpio.h>
28#include <linux/mdio-gpio.h>
29
Laurent Pincharta5edecc2008-05-26 11:53:21 +020030#include <linux/of_gpio.h>
Mark Waredacac4d2009-07-23 10:56:48 -070031#include <linux/of_mdio.h>
Laurent Pincharta5edecc2008-05-26 11:53:21 +020032
33struct mdio_gpio_info {
34 struct mdiobb_ctrl ctrl;
35 int mdc, mdio;
36};
37
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000038static void *mdio_gpio_of_get_data(struct platform_device *pdev)
39{
40 struct device_node *np = pdev->dev.of_node;
41 struct mdio_gpio_platform_data *pdata;
42 int ret;
43
44 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
45 if (!pdata)
46 return NULL;
47
48 ret = of_get_gpio(np, 0);
49 if (ret < 0)
50 return NULL;
51
52 pdata->mdc = ret;
53
54 ret = of_get_gpio(np, 1);
55 if (ret < 0)
56 return NULL;
57 pdata->mdio = ret;
58
59 return pdata;
60}
61
Laurent Pincharta5edecc2008-05-26 11:53:21 +020062static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
63{
64 struct mdio_gpio_info *bitbang =
65 container_of(ctrl, struct mdio_gpio_info, ctrl);
66
67 if (dir)
68 gpio_direction_output(bitbang->mdio, 1);
69 else
70 gpio_direction_input(bitbang->mdio);
71}
72
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000073static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020074{
75 struct mdio_gpio_info *bitbang =
76 container_of(ctrl, struct mdio_gpio_info, ctrl);
77
78 return gpio_get_value(bitbang->mdio);
79}
80
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000081static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020082{
83 struct mdio_gpio_info *bitbang =
84 container_of(ctrl, struct mdio_gpio_info, ctrl);
85
86 gpio_set_value(bitbang->mdio, what);
87}
88
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000089static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020090{
91 struct mdio_gpio_info *bitbang =
92 container_of(ctrl, struct mdio_gpio_info, ctrl);
93
94 gpio_set_value(bitbang->mdc, what);
95}
96
97static struct mdiobb_ops mdio_gpio_ops = {
98 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000099 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200100 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000101 .set_mdio_data = mdio_set,
102 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200103};
104
Bill Pemberton633d1592012-12-03 09:24:14 -0500105static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000106 struct mdio_gpio_platform_data *pdata,
107 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200108{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000109 struct mii_bus *new_bus;
110 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000111 int i;
112
113 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
114 if (!bitbang)
115 goto out;
116
117 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +0000118 bitbang->ctrl.reset = pdata->reset;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000119 bitbang->mdc = pdata->mdc;
120 bitbang->mdio = pdata->mdio;
121
122 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
123 if (!new_bus)
124 goto out_free_bitbang;
125
126 new_bus->name = "GPIO Bitbanged MDIO",
127
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000128 new_bus->phy_mask = pdata->phy_mask;
129 new_bus->irq = pdata->irqs;
130 new_bus->parent = dev;
131
132 if (new_bus->phy_mask == ~0)
133 goto out_free_bus;
134
135 for (i = 0; i < PHY_MAX_ADDR; i++)
136 if (!new_bus->irq[i])
137 new_bus->irq[i] = PHY_POLL;
138
Florian Fainellia77e9292012-01-09 23:59:26 +0000139 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000140
141 if (gpio_request(bitbang->mdc, "mdc"))
142 goto out_free_bus;
143
144 if (gpio_request(bitbang->mdio, "mdio"))
145 goto out_free_mdc;
146
Paulius Zaleckas664f93b2009-02-08 23:46:01 +0000147 gpio_direction_output(bitbang->mdc, 0);
148
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000149 dev_set_drvdata(dev, new_bus);
150
Mark Waredacac4d2009-07-23 10:56:48 -0700151 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000152
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000153out_free_mdc:
154 gpio_free(bitbang->mdc);
155out_free_bus:
156 free_mdio_bitbang(new_bus);
157out_free_bitbang:
158 kfree(bitbang);
159out:
Mark Waredacac4d2009-07-23 10:56:48 -0700160 return NULL;
161}
162
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000163static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700164{
165 struct mii_bus *bus = dev_get_drvdata(dev);
166 struct mdio_gpio_info *bitbang = bus->priv;
167
168 dev_set_drvdata(dev, NULL);
169 gpio_free(bitbang->mdio);
170 gpio_free(bitbang->mdc);
171 free_mdio_bitbang(bus);
172 kfree(bitbang);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000173}
174
Bill Pemberton633d1592012-12-03 09:24:14 -0500175static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000176{
177 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200178
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000179 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700180 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000181}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200182
Bill Pemberton633d1592012-12-03 09:24:14 -0500183static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000184{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000185 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700186 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000187 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000188
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000189 if (pdev->dev.of_node) {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000190 pdata = mdio_gpio_of_get_data(pdev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000191 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
192 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900193 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000194 bus_id = pdev->id;
195 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000196
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000197 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200198 return -ENODEV;
199
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000200 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700201 if (!new_bus)
202 return -ENODEV;
203
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000204 if (pdev->dev.of_node)
205 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
206 else
207 ret = mdiobus_register(new_bus);
208
Mark Waredacac4d2009-07-23 10:56:48 -0700209 if (ret)
210 mdio_gpio_bus_deinit(&pdev->dev);
211
212 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000213}
214
Bill Pemberton633d1592012-12-03 09:24:14 -0500215static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000216{
217 mdio_gpio_bus_destroy(&pdev->dev);
218
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200219 return 0;
220}
221
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000222static struct of_device_id mdio_gpio_of_match[] = {
223 { .compatible = "virtual,mdio-gpio", },
224 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200225};
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200226
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000227static struct platform_driver mdio_gpio_driver = {
228 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500229 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000230 .driver = {
231 .name = "mdio-gpio",
232 .owner = THIS_MODULE,
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000233 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000234 },
235};
236
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000237module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000238
239MODULE_ALIAS("platform:mdio-gpio");
240MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
241MODULE_LICENSE("GPL");
242MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");