blob: 4333c6e14742bd8a326c4d8fca64249a977ad61f [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;
Laurent Pincharta5edecc2008-05-26 11:53:21 +020036};
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;
Guenter Roeck1d251482014-04-15 19:16:41 -070042 enum of_gpio_flags flags;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000043 int ret;
44
45 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
46 if (!pdata)
47 return NULL;
48
Guenter Roeck1d251482014-04-15 19:16:41 -070049 ret = of_get_gpio_flags(np, 0, &flags);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000050 if (ret < 0)
51 return NULL;
52
53 pdata->mdc = ret;
Guenter Roeck1d251482014-04-15 19:16:41 -070054 pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000055
Guenter Roeck1d251482014-04-15 19:16:41 -070056 ret = of_get_gpio_flags(np, 1, &flags);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000057 if (ret < 0)
58 return NULL;
59 pdata->mdio = ret;
Guenter Roeck1d251482014-04-15 19:16:41 -070060 pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000061
Guenter Roeckf1d54c42014-04-15 19:16:42 -070062 ret = of_get_gpio_flags(np, 2, &flags);
63 if (ret > 0) {
64 pdata->mdo = ret;
65 pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW;
66 }
67
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000068 return pdata;
69}
70
Laurent Pincharta5edecc2008-05-26 11:53:21 +020071static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
72{
73 struct mdio_gpio_info *bitbang =
74 container_of(ctrl, struct mdio_gpio_info, ctrl);
75
Guenter Roeckf1d54c42014-04-15 19:16:42 -070076 if (bitbang->mdo) {
77 /* Separate output pin. Always set its value to high
78 * when changing direction. If direction is input,
79 * assume the pin serves as pull-up. If direction is
80 * output, the default value is high.
81 */
Guenter Roeck52aab182017-01-11 12:59:51 -080082 gpiod_set_value(bitbang->mdo, 1);
Guenter Roeckf1d54c42014-04-15 19:16:42 -070083 return;
84 }
85
Laurent Pincharta5edecc2008-05-26 11:53:21 +020086 if (dir)
Guenter Roeck52aab182017-01-11 12:59:51 -080087 gpiod_direction_output(bitbang->mdio, 1);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020088 else
Guenter Roeck7e5fbd12017-01-11 12:59:50 -080089 gpiod_direction_input(bitbang->mdio);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020090}
91
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000092static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020093{
94 struct mdio_gpio_info *bitbang =
95 container_of(ctrl, struct mdio_gpio_info, ctrl);
96
Guenter Roeck52aab182017-01-11 12:59:51 -080097 return gpiod_get_value(bitbang->mdio);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020098}
99
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000100static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200101{
102 struct mdio_gpio_info *bitbang =
103 container_of(ctrl, struct mdio_gpio_info, ctrl);
104
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700105 if (bitbang->mdo)
Guenter Roeck52aab182017-01-11 12:59:51 -0800106 gpiod_set_value(bitbang->mdo, what);
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700107 else
Guenter Roeck52aab182017-01-11 12:59:51 -0800108 gpiod_set_value(bitbang->mdio, what);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200109}
110
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000111static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200112{
113 struct mdio_gpio_info *bitbang =
114 container_of(ctrl, struct mdio_gpio_info, ctrl);
115
Guenter Roeck52aab182017-01-11 12:59:51 -0800116 gpiod_set_value(bitbang->mdc, what);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200117}
118
Bhumika Goyal41a130f2017-08-22 13:43:29 +0530119static const struct mdiobb_ops mdio_gpio_ops = {
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200120 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000121 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200122 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000123 .set_mdio_data = mdio_set,
124 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200125};
126
Bill Pemberton633d1592012-12-03 09:24:14 -0500127static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000128 struct mdio_gpio_platform_data *pdata,
129 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200130{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000131 struct mii_bus *new_bus;
132 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000133 int i;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800134 int mdc, mdio, mdo;
Guenter Roeck08d96652017-01-11 12:59:49 -0800135 unsigned long mdc_flags = GPIOF_OUT_INIT_LOW;
136 unsigned long mdio_flags = GPIOF_DIR_IN;
137 unsigned long mdo_flags = GPIOF_OUT_INIT_HIGH;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000138
Guenter Roeck78cdb072014-04-15 19:16:40 -0700139 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000140 if (!bitbang)
141 goto out;
142
143 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +0000144 bitbang->ctrl.reset = pdata->reset;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800145 mdc = pdata->mdc;
146 bitbang->mdc = gpio_to_desc(mdc);
Guenter Roeck52aab182017-01-11 12:59:51 -0800147 if (pdata->mdc_active_low)
148 mdc_flags = GPIOF_OUT_INIT_HIGH | GPIOF_ACTIVE_LOW;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800149 mdio = pdata->mdio;
150 bitbang->mdio = gpio_to_desc(mdio);
Guenter Roeck52aab182017-01-11 12:59:51 -0800151 if (pdata->mdio_active_low)
152 mdio_flags |= GPIOF_ACTIVE_LOW;
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800153 mdo = pdata->mdo;
Guenter Roeck52aab182017-01-11 12:59:51 -0800154 if (mdo) {
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800155 bitbang->mdo = gpio_to_desc(mdo);
Guenter Roeck52aab182017-01-11 12:59:51 -0800156 if (pdata->mdo_active_low)
157 mdo_flags = GPIOF_OUT_INIT_LOW | GPIOF_ACTIVE_LOW;
158 }
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000159
160 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
161 if (!new_bus)
Guenter Roeck78cdb072014-04-15 19:16:40 -0700162 goto out;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000163
164 new_bus->name = "GPIO Bitbanged MDIO",
165
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000166 new_bus->phy_mask = pdata->phy_mask;
Bert Vermeulenef7f3a52015-05-13 13:35:39 +0200167 new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100168 memcpy(new_bus->irq, pdata->irqs, sizeof(new_bus->irq));
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000169 new_bus->parent = dev;
170
171 if (new_bus->phy_mask == ~0)
172 goto out_free_bus;
173
174 for (i = 0; i < PHY_MAX_ADDR; i++)
175 if (!new_bus->irq[i])
176 new_bus->irq[i] = PHY_POLL;
177
Bert Vermeulen7c0c8262015-05-08 16:18:49 +0200178 if (bus_id != -1)
179 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
180 else
181 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000182
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800183 if (devm_gpio_request_one(dev, mdc, mdc_flags, "mdc"))
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000184 goto out_free_bus;
185
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800186 if (devm_gpio_request_one(dev, mdio, mdio_flags, "mdio"))
Guenter Roeck78cdb072014-04-15 19:16:40 -0700187 goto out_free_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000188
Guenter Roeck7e5fbd12017-01-11 12:59:50 -0800189 if (mdo && devm_gpio_request_one(dev, mdo, mdo_flags, "mdo"))
190 goto out_free_bus;
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700191
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000192 dev_set_drvdata(dev, new_bus);
193
Mark Waredacac4d2009-07-23 10:56:48 -0700194 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000195
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000196out_free_bus:
197 free_mdio_bitbang(new_bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000198out:
Mark Waredacac4d2009-07-23 10:56:48 -0700199 return NULL;
200}
201
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000202static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700203{
204 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700205
Mark Waredacac4d2009-07-23 10:56:48 -0700206 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000207}
208
Bill Pemberton633d1592012-12-03 09:24:14 -0500209static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000210{
211 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200212
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000213 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700214 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000215}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200216
Bill Pemberton633d1592012-12-03 09:24:14 -0500217static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000218{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000219 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700220 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000221 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000222
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000223 if (pdev->dev.of_node) {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000224 pdata = mdio_gpio_of_get_data(pdev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000225 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200226 if (bus_id < 0) {
227 dev_warn(&pdev->dev, "failed to get alias id\n");
228 bus_id = 0;
229 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000230 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900231 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000232 bus_id = pdev->id;
233 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000234
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000235 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200236 return -ENODEV;
237
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000238 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700239 if (!new_bus)
240 return -ENODEV;
241
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000242 if (pdev->dev.of_node)
243 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
244 else
245 ret = mdiobus_register(new_bus);
246
Mark Waredacac4d2009-07-23 10:56:48 -0700247 if (ret)
248 mdio_gpio_bus_deinit(&pdev->dev);
249
250 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000251}
252
Bill Pemberton633d1592012-12-03 09:24:14 -0500253static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000254{
255 mdio_gpio_bus_destroy(&pdev->dev);
256
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200257 return 0;
258}
259
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100260static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000261 { .compatible = "virtual,mdio-gpio", },
262 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200263};
Luis de Bethencourt1ccb1412015-09-18 18:16:29 +0200264MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200265
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000266static struct platform_driver mdio_gpio_driver = {
267 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500268 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000269 .driver = {
270 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000271 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000272 },
273};
274
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000275module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000276
277MODULE_ALIAS("platform:mdio-gpio");
278MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
279MODULE_LICENSE("GPL");
280MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");