blob: e62fcea2d945f734787180e199f14d766292ec83 [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>
Vivien Didelote2aacd92015-10-20 10:08:59 -040028#include <linux/platform_data/mdio-gpio.h>
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000029
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 Roeck7e5fbd12017-01-11 12:59:50 -080035 struct gpio_desc *mdc, *mdio, *mdo;
Guenter Roeckf1d54c42014-04-15 19:16:42 -070036 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 */
Guenter Roeck7e5fbd12017-01-11 12:59:50 -080083 gpiod_set_value(bitbang->mdo, 1 ^ bitbang->mdo_active_low);
Guenter Roeckf1d54c42014-04-15 19:16:42 -070084 return;
85 }
86
Laurent Pincharta5edecc2008-05-26 11:53:21 +020087 if (dir)
Guenter Roeck7e5fbd12017-01-11 12:59:50 -080088 gpiod_direction_output(bitbang->mdio,
89 1 ^ bitbang->mdio_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020090 else
Guenter Roeck7e5fbd12017-01-11 12:59:50 -080091 gpiod_direction_input(bitbang->mdio);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020092}
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 Roeck7e5fbd12017-01-11 12:59:50 -080099 return gpiod_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)
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800108 gpiod_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low);
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700109 else
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800110 gpiod_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 Roeck7e5fbd12017-01-11 12:59:50 -0800118 gpiod_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;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800136 int mdc, mdio, mdo;
Guenter Roeck08d96652017-01-11 12:59:49 -0800137 unsigned long mdc_flags = GPIOF_OUT_INIT_LOW;
138 unsigned long mdio_flags = GPIOF_DIR_IN;
139 unsigned long mdo_flags = GPIOF_OUT_INIT_HIGH;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000140
Guenter Roeck78cdb072014-04-15 19:16:40 -0700141 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000142 if (!bitbang)
143 goto out;
144
145 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +0000146 bitbang->ctrl.reset = pdata->reset;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800147 mdc = pdata->mdc;
148 bitbang->mdc = gpio_to_desc(mdc);
Guenter Roeck1d251482014-04-15 19:16:41 -0700149 bitbang->mdc_active_low = pdata->mdc_active_low;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800150 mdio = pdata->mdio;
151 bitbang->mdio = gpio_to_desc(mdio);
Guenter Roeck1d251482014-04-15 19:16:41 -0700152 bitbang->mdio_active_low = pdata->mdio_active_low;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800153 mdo = pdata->mdo;
154 if (mdo)
155 bitbang->mdo = gpio_to_desc(mdo);
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700156 bitbang->mdo_active_low = pdata->mdo_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000157
158 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
159 if (!new_bus)
Guenter Roeck78cdb072014-04-15 19:16:40 -0700160 goto out;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000161
162 new_bus->name = "GPIO Bitbanged MDIO",
163
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000164 new_bus->phy_mask = pdata->phy_mask;
Bert Vermeulenef7f3a52015-05-13 13:35:39 +0200165 new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100166 memcpy(new_bus->irq, pdata->irqs, sizeof(new_bus->irq));
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000167 new_bus->parent = dev;
168
169 if (new_bus->phy_mask == ~0)
170 goto out_free_bus;
171
172 for (i = 0; i < PHY_MAX_ADDR; i++)
173 if (!new_bus->irq[i])
174 new_bus->irq[i] = PHY_POLL;
175
Bert Vermeulen7c0c8262015-05-08 16:18:49 +0200176 if (bus_id != -1)
177 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
178 else
179 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000180
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800181 if (devm_gpio_request_one(dev, mdc, mdc_flags, "mdc"))
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000182 goto out_free_bus;
183
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800184 if (devm_gpio_request_one(dev, mdio, mdio_flags, "mdio"))
Guenter Roeck78cdb072014-04-15 19:16:40 -0700185 goto out_free_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000186
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800187 if (mdo && devm_gpio_request_one(dev, mdo, mdo_flags, "mdo"))
188 goto out_free_bus;
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700189
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000190 dev_set_drvdata(dev, new_bus);
191
Mark Waredacac4d2009-07-23 10:56:48 -0700192 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000193
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000194out_free_bus:
195 free_mdio_bitbang(new_bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000196out:
Mark Waredacac4d2009-07-23 10:56:48 -0700197 return NULL;
198}
199
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000200static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700201{
202 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700203
Mark Waredacac4d2009-07-23 10:56:48 -0700204 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000205}
206
Bill Pemberton633d1592012-12-03 09:24:14 -0500207static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000208{
209 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200210
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000211 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700212 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000213}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200214
Bill Pemberton633d1592012-12-03 09:24:14 -0500215static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000216{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000217 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700218 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000219 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000220
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000221 if (pdev->dev.of_node) {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000222 pdata = mdio_gpio_of_get_data(pdev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000223 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200224 if (bus_id < 0) {
225 dev_warn(&pdev->dev, "failed to get alias id\n");
226 bus_id = 0;
227 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000228 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900229 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000230 bus_id = pdev->id;
231 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000232
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000233 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200234 return -ENODEV;
235
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000236 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700237 if (!new_bus)
238 return -ENODEV;
239
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000240 if (pdev->dev.of_node)
241 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
242 else
243 ret = mdiobus_register(new_bus);
244
Mark Waredacac4d2009-07-23 10:56:48 -0700245 if (ret)
246 mdio_gpio_bus_deinit(&pdev->dev);
247
248 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000249}
250
Bill Pemberton633d1592012-12-03 09:24:14 -0500251static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000252{
253 mdio_gpio_bus_destroy(&pdev->dev);
254
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200255 return 0;
256}
257
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100258static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000259 { .compatible = "virtual,mdio-gpio", },
260 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200261};
Luis de Bethencourt1ccb1412015-09-18 18:16:29 +0200262MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200263
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000264static struct platform_driver mdio_gpio_driver = {
265 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500266 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000267 .driver = {
268 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000269 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000270 },
271};
272
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000273module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000274
275MODULE_ALIAS("platform:mdio-gpio");
276MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
277MODULE_LICENSE("GPL");
278MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");