blob: bb57a9e89a18c78cf6d7e8fddbedc78a684a8857 [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,
Andrew Lunnfb766422018-04-19 01:02:56 +0200118 struct mdio_gpio_info *bitbang,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000119 struct mdio_gpio_platform_data *pdata,
120 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200121{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000122 struct mii_bus *new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000123
124 bitbang->ctrl.ops = &mdio_gpio_ops;
Andrew Lunnc82fc482018-04-19 01:02:55 +0200125 bitbang->mdc = pdata->mdc;
126 bitbang->mdio = pdata->mdio;
127 bitbang->mdo = pdata->mdo;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000128
129 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
130 if (!new_bus)
Andrew Lunnc82fc482018-04-19 01:02:55 +0200131 return NULL;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000132
Andrew Lunn712e5a5c2018-04-19 01:02:49 +0200133 new_bus->name = "GPIO Bitbanged MDIO";
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000134 new_bus->parent = dev;
135
Bert Vermeulen7c0c8262015-05-08 16:18:49 +0200136 if (bus_id != -1)
137 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
138 else
139 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000140
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000141 dev_set_drvdata(dev, new_bus);
142
Mark Waredacac4d2009-07-23 10:56:48 -0700143 return new_bus;
Mark Waredacac4d2009-07-23 10:56:48 -0700144}
145
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000146static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700147{
148 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700149
Mark Waredacac4d2009-07-23 10:56:48 -0700150 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000151}
152
Bill Pemberton633d1592012-12-03 09:24:14 -0500153static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000154{
155 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200156
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000157 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700158 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000159}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200160
Bill Pemberton633d1592012-12-03 09:24:14 -0500161static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000162{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000163 struct mdio_gpio_platform_data *pdata;
Andrew Lunnfb766422018-04-19 01:02:56 +0200164 struct mdio_gpio_info *bitbang;
Mark Waredacac4d2009-07-23 10:56:48 -0700165 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000166 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000167
Andrew Lunnfb766422018-04-19 01:02:56 +0200168 bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
169 if (!bitbang)
170 return -ENOMEM;
171
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000172 if (pdev->dev.of_node) {
Andrew Lunnc82fc482018-04-19 01:02:55 +0200173 pdata = mdio_gpio_of_get_data(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000174 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200175 if (bus_id < 0) {
176 dev_warn(&pdev->dev, "failed to get alias id\n");
177 bus_id = 0;
178 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000179 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900180 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000181 bus_id = pdev->id;
182 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000183
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000184 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200185 return -ENODEV;
186
Andrew Lunnfb766422018-04-19 01:02:56 +0200187 new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700188 if (!new_bus)
189 return -ENODEV;
190
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000191 if (pdev->dev.of_node)
192 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
193 else
194 ret = mdiobus_register(new_bus);
195
Mark Waredacac4d2009-07-23 10:56:48 -0700196 if (ret)
197 mdio_gpio_bus_deinit(&pdev->dev);
198
199 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000200}
201
Bill Pemberton633d1592012-12-03 09:24:14 -0500202static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000203{
204 mdio_gpio_bus_destroy(&pdev->dev);
205
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200206 return 0;
207}
208
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100209static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000210 { .compatible = "virtual,mdio-gpio", },
211 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200212};
Luis de Bethencourt1ccb1412015-09-18 18:16:29 +0200213MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200214
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000215static struct platform_driver mdio_gpio_driver = {
216 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500217 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000218 .driver = {
219 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000220 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000221 },
222};
223
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000224module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000225
226MODULE_ALIAS("platform:mdio-gpio");
227MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
228MODULE_LICENSE("GPL");
229MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");