blob: c9cb486c753d053c8b6da529a0c078d89f9c2a2f [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 */
Vivien Didelot2d6c9092015-04-22 13:06:54 -040083 gpio_set_value_cansleep(bitbang->mdo,
84 1 ^ bitbang->mdo_active_low);
Guenter Roeckf1d54c42014-04-15 19:16:42 -070085 return;
86 }
87
Laurent Pincharta5edecc2008-05-26 11:53:21 +020088 if (dir)
Guenter Roeck1d251482014-04-15 19:16:41 -070089 gpio_direction_output(bitbang->mdio,
90 1 ^ bitbang->mdio_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +020091 else
92 gpio_direction_input(bitbang->mdio);
93}
94
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +000095static int mdio_get(struct mdiobb_ctrl *ctrl)
Laurent Pincharta5edecc2008-05-26 11:53:21 +020096{
97 struct mdio_gpio_info *bitbang =
98 container_of(ctrl, struct mdio_gpio_info, ctrl);
99
Vivien Didelot2d6c9092015-04-22 13:06:54 -0400100 return gpio_get_value_cansleep(bitbang->mdio) ^
101 bitbang->mdio_active_low;
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200102}
103
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000104static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200105{
106 struct mdio_gpio_info *bitbang =
107 container_of(ctrl, struct mdio_gpio_info, ctrl);
108
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700109 if (bitbang->mdo)
Vivien Didelot2d6c9092015-04-22 13:06:54 -0400110 gpio_set_value_cansleep(bitbang->mdo,
111 what ^ bitbang->mdo_active_low);
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700112 else
Vivien Didelot2d6c9092015-04-22 13:06:54 -0400113 gpio_set_value_cansleep(bitbang->mdio,
114 what ^ bitbang->mdio_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200115}
116
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000117static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200118{
119 struct mdio_gpio_info *bitbang =
120 container_of(ctrl, struct mdio_gpio_info, ctrl);
121
Vivien Didelot2d6c9092015-04-22 13:06:54 -0400122 gpio_set_value_cansleep(bitbang->mdc, what ^ bitbang->mdc_active_low);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200123}
124
125static struct mdiobb_ops mdio_gpio_ops = {
126 .owner = THIS_MODULE,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000127 .set_mdc = mdc_set,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200128 .set_mdio_dir = mdio_dir,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000129 .set_mdio_data = mdio_set,
130 .get_mdio_data = mdio_get,
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200131};
132
Bill Pemberton633d1592012-12-03 09:24:14 -0500133static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000134 struct mdio_gpio_platform_data *pdata,
135 int bus_id)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200136{
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000137 struct mii_bus *new_bus;
138 struct mdio_gpio_info *bitbang;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000139 int i;
140
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;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000147 bitbang->mdc = pdata->mdc;
Guenter Roeck1d251482014-04-15 19:16:41 -0700148 bitbang->mdc_active_low = pdata->mdc_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000149 bitbang->mdio = pdata->mdio;
Guenter Roeck1d251482014-04-15 19:16:41 -0700150 bitbang->mdio_active_low = pdata->mdio_active_low;
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700151 bitbang->mdo = pdata->mdo;
152 bitbang->mdo_active_low = pdata->mdo_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000153
154 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
155 if (!new_bus)
Guenter Roeck78cdb072014-04-15 19:16:40 -0700156 goto out;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000157
158 new_bus->name = "GPIO Bitbanged MDIO",
159
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000160 new_bus->phy_mask = pdata->phy_mask;
161 new_bus->irq = pdata->irqs;
162 new_bus->parent = dev;
163
164 if (new_bus->phy_mask == ~0)
165 goto out_free_bus;
166
167 for (i = 0; i < PHY_MAX_ADDR; i++)
168 if (!new_bus->irq[i])
169 new_bus->irq[i] = PHY_POLL;
170
Florian Fainellia77e9292012-01-09 23:59:26 +0000171 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000172
Guenter Roeck78cdb072014-04-15 19:16:40 -0700173 if (devm_gpio_request(dev, bitbang->mdc, "mdc"))
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000174 goto out_free_bus;
175
Guenter Roeck78cdb072014-04-15 19:16:40 -0700176 if (devm_gpio_request(dev, bitbang->mdio, "mdio"))
177 goto out_free_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000178
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700179 if (bitbang->mdo) {
180 if (devm_gpio_request(dev, bitbang->mdo, "mdo"))
181 goto out_free_bus;
182 gpio_direction_output(bitbang->mdo, 1);
183 gpio_direction_input(bitbang->mdio);
184 }
185
Paulius Zaleckas664f93b2009-02-08 23:46:01 +0000186 gpio_direction_output(bitbang->mdc, 0);
187
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000188 dev_set_drvdata(dev, new_bus);
189
Mark Waredacac4d2009-07-23 10:56:48 -0700190 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000191
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000192out_free_bus:
193 free_mdio_bitbang(new_bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000194out:
Mark Waredacac4d2009-07-23 10:56:48 -0700195 return NULL;
196}
197
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000198static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700199{
200 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700201
Mark Waredacac4d2009-07-23 10:56:48 -0700202 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000203}
204
Bill Pemberton633d1592012-12-03 09:24:14 -0500205static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000206{
207 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200208
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000209 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700210 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000211}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200212
Bill Pemberton633d1592012-12-03 09:24:14 -0500213static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000214{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000215 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700216 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000217 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000218
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000219 if (pdev->dev.of_node) {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000220 pdata = mdio_gpio_of_get_data(pdev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000221 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200222 if (bus_id < 0) {
223 dev_warn(&pdev->dev, "failed to get alias id\n");
224 bus_id = 0;
225 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000226 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900227 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000228 bus_id = pdev->id;
229 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000230
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000231 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200232 return -ENODEV;
233
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000234 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700235 if (!new_bus)
236 return -ENODEV;
237
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000238 if (pdev->dev.of_node)
239 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
240 else
241 ret = mdiobus_register(new_bus);
242
Mark Waredacac4d2009-07-23 10:56:48 -0700243 if (ret)
244 mdio_gpio_bus_deinit(&pdev->dev);
245
246 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000247}
248
Bill Pemberton633d1592012-12-03 09:24:14 -0500249static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000250{
251 mdio_gpio_bus_destroy(&pdev->dev);
252
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200253 return 0;
254}
255
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100256static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000257 { .compatible = "virtual,mdio-gpio", },
258 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200259};
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200260
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000261static struct platform_driver mdio_gpio_driver = {
262 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500263 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000264 .driver = {
265 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000266 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000267 },
268};
269
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000270module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000271
272MODULE_ALIAS("platform:mdio-gpio");
273MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
274MODULE_LICENSE("GPL");
275MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");