blob: 49ce7ece5af30c04c3e632a1719ba45d9b47f3b5 [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;
Guenter Roeckf1d54c42014-04-15 19:16:42 -070035 int mdc, mdio, mdo;
36 int mdc_active_low, mdio_active_low, mdo_active_low;
Laurent Pincharta5edecc2008-05-26 11:53:21 +020037};
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;
Guenter Roeck1d251482014-04-15 19:16:41 -070043 enum of_gpio_flags flags;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000044 int ret;
45
46 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
47 if (!pdata)
48 return NULL;
49
Guenter Roeck1d251482014-04-15 19:16:41 -070050 ret = of_get_gpio_flags(np, 0, &flags);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000051 if (ret < 0)
52 return NULL;
53
54 pdata->mdc = ret;
Guenter Roeck1d251482014-04-15 19:16:41 -070055 pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000056
Guenter Roeck1d251482014-04-15 19:16:41 -070057 ret = of_get_gpio_flags(np, 1, &flags);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000058 if (ret < 0)
59 return NULL;
60 pdata->mdio = ret;
Guenter Roeck1d251482014-04-15 19:16:41 -070061 pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000062
Guenter Roeckf1d54c42014-04-15 19:16:42 -070063 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
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000069 return pdata;
70}
71
Laurent Pincharta5edecc2008-05-26 11:53:21 +020072static 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
Guenter Roeckf1d54c42014-04-15 19:16:42 -070077 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(bitbang->mdo, 1 ^ bitbang->mdo_active_low);
84 return;
85 }
86
Laurent Pincharta5edecc2008-05-26 11:53:21 +020087 if (dir)
Guenter Roeck1d251482014-04-15 19:16:41 -070088 gpio_direction_output(bitbang->mdio,
89 1 ^ bitbang->mdio_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020090 else
91 gpio_direction_input(bitbang->mdio);
92}
93
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000094static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020095{
96 struct mdio_gpio_info *bitbang =
97 container_of(ctrl, struct mdio_gpio_info, ctrl);
98
Guenter Roeck1d251482014-04-15 19:16:41 -070099 return gpio_get_value(bitbang->mdio) ^ bitbang->mdio_active_low;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200100}
101
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000102static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200103{
104 struct mdio_gpio_info *bitbang =
105 container_of(ctrl, struct mdio_gpio_info, ctrl);
106
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700107 if (bitbang->mdo)
108 gpio_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low);
109 else
110 gpio_set_value(bitbang->mdio, what ^ bitbang->mdio_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200111}
112
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000113static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200114{
115 struct mdio_gpio_info *bitbang =
116 container_of(ctrl, struct mdio_gpio_info, ctrl);
117
Guenter Roeck1d251482014-04-15 19:16:41 -0700118 gpio_set_value(bitbang->mdc, what ^ bitbang->mdc_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200119}
120
121static struct mdiobb_ops mdio_gpio_ops = {
122 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000123 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200124 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000125 .set_mdio_data = mdio_set,
126 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200127};
128
Bill Pemberton633d1592012-12-03 09:24:14 -0500129static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000130 struct mdio_gpio_platform_data *pdata,
131 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200132{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000133 struct mii_bus *new_bus;
134 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000135 int i;
136
Guenter Roeck78cdb072014-04-15 19:16:40 -0700137 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000138 if (!bitbang)
139 goto out;
140
141 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +0000142 bitbang->ctrl.reset = pdata->reset;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000143 bitbang->mdc = pdata->mdc;
Guenter Roeck1d251482014-04-15 19:16:41 -0700144 bitbang->mdc_active_low = pdata->mdc_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000145 bitbang->mdio = pdata->mdio;
Guenter Roeck1d251482014-04-15 19:16:41 -0700146 bitbang->mdio_active_low = pdata->mdio_active_low;
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700147 bitbang->mdo = pdata->mdo;
148 bitbang->mdo_active_low = pdata->mdo_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000149
150 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
151 if (!new_bus)
Guenter Roeck78cdb072014-04-15 19:16:40 -0700152 goto out;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000153
154 new_bus->name = "GPIO Bitbanged MDIO",
155
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000156 new_bus->phy_mask = pdata->phy_mask;
157 new_bus->irq = pdata->irqs;
158 new_bus->parent = dev;
159
160 if (new_bus->phy_mask == ~0)
161 goto out_free_bus;
162
163 for (i = 0; i < PHY_MAX_ADDR; i++)
164 if (!new_bus->irq[i])
165 new_bus->irq[i] = PHY_POLL;
166
Florian Fainellia77e9292012-01-09 23:59:26 +0000167 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000168
Guenter Roeck78cdb072014-04-15 19:16:40 -0700169 if (devm_gpio_request(dev, bitbang->mdc, "mdc"))
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000170 goto out_free_bus;
171
Guenter Roeck78cdb072014-04-15 19:16:40 -0700172 if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
173 goto out_free_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000174
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700175 if (bitbang->mdo) {
176 if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
177 goto out_free_bus;
178 gpio_direction_output(bitbang->mdo, 1);
179 gpio_direction_input(bitbang->mdio);
180 }
181
Paulius Zaleckas664f93b2009-02-08 23:46:01 +0000182 gpio_direction_output(bitbang->mdc, 0);
183
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000184 dev_set_drvdata(dev, new_bus);
185
Mark Waredacac4d2009-07-23 10:56:48 -0700186 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000187
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000188out_free_bus:
189 free_mdio_bitbang(new_bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000190out:
Mark Waredacac4d2009-07-23 10:56:48 -0700191 return NULL;
192}
193
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000194static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700195{
196 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700197
Mark Waredacac4d2009-07-23 10:56:48 -0700198 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000199}
200
Bill Pemberton633d1592012-12-03 09:24:14 -0500201static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000202{
203 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200204
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000205 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700206 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000207}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200208
Bill Pemberton633d1592012-12-03 09:24:14 -0500209static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000210{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000211 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700212 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000213 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000214
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000215 if (pdev->dev.of_node) {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000216 pdata = mdio_gpio_of_get_data(pdev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000217 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200218 if (bus_id < 0) {
219 dev_warn(&pdev->dev, "failed to get alias id\n");
220 bus_id = 0;
221 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000222 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900223 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000224 bus_id = pdev->id;
225 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000226
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000227 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200228 return -ENODEV;
229
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000230 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700231 if (!new_bus)
232 return -ENODEV;
233
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000234 if (pdev->dev.of_node)
235 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
236 else
237 ret = mdiobus_register(new_bus);
238
Mark Waredacac4d2009-07-23 10:56:48 -0700239 if (ret)
240 mdio_gpio_bus_deinit(&pdev->dev);
241
242 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000243}
244
Bill Pemberton633d1592012-12-03 09:24:14 -0500245static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000246{
247 mdio_gpio_bus_destroy(&pdev->dev);
248
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200249 return 0;
250}
251
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100252static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000253 { .compatible = "virtual,mdio-gpio", },
254 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200255};
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200256
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000257static struct platform_driver mdio_gpio_driver = {
258 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500259 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000260 .driver = {
261 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000262 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000263 },
264};
265
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000266module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000267
268MODULE_ALIAS("platform:mdio-gpio");
269MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
270MODULE_LICENSE("GPL");
271MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");