blob: b999f32374e2d0678a04f3d4378043570e98a863 [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
Andrew Lunnc82fc482018-04-19 01:02:55 +020038static void *mdio_gpio_of_get_data(struct device *dev)
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000039{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000040 struct mdio_gpio_platform_data *pdata;
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000041
Andrew Lunnc82fc482018-04-19 01:02:55 +020042 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000043 if (!pdata)
44 return NULL;
45
Andrew Lunnc82fc482018-04-19 01:02:55 +020046 pdata->mdc = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
47 if (IS_ERR(pdata->mdc))
48 return ERR_CAST(pdata->mdc);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000049
Andrew Lunnc82fc482018-04-19 01:02:55 +020050 pdata->mdio = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN);
51 if (IS_ERR(pdata->mdio))
52 return ERR_CAST(pdata->mdio);
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000053
Andrew Lunnc82fc482018-04-19 01:02:55 +020054 pdata->mdo = devm_gpiod_get_index_optional(dev, NULL, 2, GPIOD_OUT_LOW);
55 if (IS_ERR(pdata->mdo))
56 return ERR_CAST(pdata->mdo);
Guenter Roeckf1d54c42014-04-15 19:16:42 -070057
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +000058 return pdata;
59}
60
Laurent Pincharta5edecc2008-05-26 11:53:21 +020061static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
62{
63 struct mdio_gpio_info *bitbang =
64 container_of(ctrl, struct mdio_gpio_info, ctrl);
65
Guenter Roeckf1d54c42014-04-15 19:16:42 -070066 if (bitbang->mdo) {
67 /* Separate output pin. Always set its value to high
68 * when changing direction. If direction is input,
69 * assume the pin serves as pull-up. If direction is
70 * output, the default value is high.
71 */
Guenter Roeck52aab182017-01-11 12:59:51 -080072 gpiod_set_value(bitbang->mdo, 1);
Guenter Roeckf1d54c42014-04-15 19:16:42 -070073 return;
74 }
75
Laurent Pincharta5edecc2008-05-26 11:53:21 +020076 if (dir)
Guenter Roeck52aab182017-01-11 12:59:51 -080077 gpiod_direction_output(bitbang->mdio, 1);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020078 else
Guenter Roeck7e5fbd12017-01-11 12:59:50 -080079 gpiod_direction_input(bitbang->mdio);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020080}
81
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000082static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020083{
84 struct mdio_gpio_info *bitbang =
85 container_of(ctrl, struct mdio_gpio_info, ctrl);
86
Guenter Roeck52aab182017-01-11 12:59:51 -080087 return gpiod_get_value(bitbang->mdio);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020088}
89
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000090static void mdio_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
Guenter Roeckf1d54c42014-04-15 19:16:42 -070095 if (bitbang->mdo)
Guenter Roeck52aab182017-01-11 12:59:51 -080096 gpiod_set_value(bitbang->mdo, what);
Guenter Roeckf1d54c42014-04-15 19:16:42 -070097 else
Guenter Roeck52aab182017-01-11 12:59:51 -080098 gpiod_set_value(bitbang->mdio, what);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020099}
100
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000101static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200102{
103 struct mdio_gpio_info *bitbang =
104 container_of(ctrl, struct mdio_gpio_info, ctrl);
105
Guenter Roeck52aab182017-01-11 12:59:51 -0800106 gpiod_set_value(bitbang->mdc, what);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200107}
108
Bhumika Goyal41a130f2017-08-22 13:43:29 +0530109static const struct mdiobb_ops mdio_gpio_ops = {
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200110 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000111 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200112 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000113 .set_mdio_data = mdio_set,
114 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200115};
116
Bill Pemberton633d1592012-12-03 09:24:14 -0500117static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000118 struct mdio_gpio_platform_data *pdata,
119 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200120{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000121 struct mii_bus *new_bus;
122 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000123
Guenter Roeck78cdb072014-04-15 19:16:40 -0700124 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000125 if (!bitbang)
Andrew Lunnc82fc482018-04-19 01:02:55 +0200126 return NULL;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000127
128 bitbang->ctrl.ops = &mdio_gpio_ops;
Andrew Lunnc82fc482018-04-19 01:02:55 +0200129 bitbang->mdc = pdata->mdc;
130 bitbang->mdio = pdata->mdio;
131 bitbang->mdo = pdata->mdo;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000132
133 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
134 if (!new_bus)
Andrew Lunnc82fc482018-04-19 01:02:55 +0200135 return NULL;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000136
Andrew Lunn712e5a5c2018-04-19 01:02:49 +0200137 new_bus->name = "GPIO Bitbanged MDIO";
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000138 new_bus->parent = dev;
139
Bert Vermeulen7c0c8262015-05-08 16:18:49 +0200140 if (bus_id != -1)
141 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
142 else
143 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000144
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000145 dev_set_drvdata(dev, new_bus);
146
Mark Waredacac4d2009-07-23 10:56:48 -0700147 return new_bus;
Mark Waredacac4d2009-07-23 10:56:48 -0700148}
149
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000150static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700151{
152 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700153
Mark Waredacac4d2009-07-23 10:56:48 -0700154 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000155}
156
Bill Pemberton633d1592012-12-03 09:24:14 -0500157static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000158{
159 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200160
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000161 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700162 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000163}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200164
Bill Pemberton633d1592012-12-03 09:24:14 -0500165static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000166{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000167 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700168 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000169 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000170
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000171 if (pdev->dev.of_node) {
Andrew Lunnc82fc482018-04-19 01:02:55 +0200172 pdata = mdio_gpio_of_get_data(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000173 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200174 if (bus_id < 0) {
175 dev_warn(&pdev->dev, "failed to get alias id\n");
176 bus_id = 0;
177 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000178 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900179 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000180 bus_id = pdev->id;
181 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000182
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000183 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200184 return -ENODEV;
185
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000186 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700187 if (!new_bus)
188 return -ENODEV;
189
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000190 if (pdev->dev.of_node)
191 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
192 else
193 ret = mdiobus_register(new_bus);
194
Mark Waredacac4d2009-07-23 10:56:48 -0700195 if (ret)
196 mdio_gpio_bus_deinit(&pdev->dev);
197
198 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000199}
200
Bill Pemberton633d1592012-12-03 09:24:14 -0500201static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000202{
203 mdio_gpio_bus_destroy(&pdev->dev);
204
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200205 return 0;
206}
207
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100208static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000209 { .compatible = "virtual,mdio-gpio", },
210 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200211};
Luis de Bethencourt1ccb1412015-09-18 18:16:29 +0200212MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200213
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000214static struct platform_driver mdio_gpio_driver = {
215 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500216 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000217 .driver = {
218 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000219 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000220 },
221};
222
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000223module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000224
225MODULE_ALIAS("platform:mdio-gpio");
226MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
227MODULE_LICENSE("GPL");
228MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");