blob: f6e773256c82b8b6aeb7833fffd3bc761bc43fbf [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 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;
Guenter Roeck08d96652017-01-11 12:59:49 -0800140 unsigned long mdc_flags = GPIOF_OUT_INIT_LOW;
141 unsigned long mdio_flags = GPIOF_DIR_IN;
142 unsigned long mdo_flags = GPIOF_OUT_INIT_HIGH;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000143
Guenter Roeck78cdb072014-04-15 19:16:40 -0700144 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000145 if (!bitbang)
146 goto out;
147
148 bitbang->ctrl.ops = &mdio_gpio_ops;
Srinivas Kandagatla64882702011-11-15 11:54:15 +0000149 bitbang->ctrl.reset = pdata->reset;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000150 bitbang->mdc = pdata->mdc;
Guenter Roeck1d251482014-04-15 19:16:41 -0700151 bitbang->mdc_active_low = pdata->mdc_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000152 bitbang->mdio = pdata->mdio;
Guenter Roeck1d251482014-04-15 19:16:41 -0700153 bitbang->mdio_active_low = pdata->mdio_active_low;
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700154 bitbang->mdo = pdata->mdo;
155 bitbang->mdo_active_low = pdata->mdo_active_low;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000156
157 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
158 if (!new_bus)
Guenter Roeck78cdb072014-04-15 19:16:40 -0700159 goto out;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000160
161 new_bus->name = "GPIO Bitbanged MDIO",
162
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000163 new_bus->phy_mask = pdata->phy_mask;
Bert Vermeulenef7f3a52015-05-13 13:35:39 +0200164 new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100165 memcpy(new_bus->irq, pdata->irqs, sizeof(new_bus->irq));
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000166 new_bus->parent = dev;
167
168 if (new_bus->phy_mask == ~0)
169 goto out_free_bus;
170
171 for (i = 0; i < PHY_MAX_ADDR; i++)
172 if (!new_bus->irq[i])
173 new_bus->irq[i] = PHY_POLL;
174
Bert Vermeulen7c0c8262015-05-08 16:18:49 +0200175 if (bus_id != -1)
176 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
177 else
178 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000179
Guenter Roeck08d96652017-01-11 12:59:49 -0800180 if (devm_gpio_request_one(dev, bitbang->mdc, mdc_flags, "mdc"))
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000181 goto out_free_bus;
182
Guenter Roeck08d96652017-01-11 12:59:49 -0800183 if (devm_gpio_request_one(dev, bitbang->mdio, mdio_flags, "mdio"))
Guenter Roeck78cdb072014-04-15 19:16:40 -0700184 goto out_free_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000185
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700186 if (bitbang->mdo) {
Guenter Roeck08d96652017-01-11 12:59:49 -0800187 if (devm_gpio_request_one(dev, bitbang->mdo, mdo_flags, "mdo"))
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700188 goto out_free_bus;
Guenter Roeckf1d54c42014-04-15 19:16:42 -0700189 }
190
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000191 dev_set_drvdata(dev, new_bus);
192
Mark Waredacac4d2009-07-23 10:56:48 -0700193 return new_bus;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000194
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000195out_free_bus:
196 free_mdio_bitbang(new_bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000197out:
Mark Waredacac4d2009-07-23 10:56:48 -0700198 return NULL;
199}
200
Stephen Rothwellf99b4a02009-11-16 22:47:33 +0000201static void mdio_gpio_bus_deinit(struct device *dev)
Mark Waredacac4d2009-07-23 10:56:48 -0700202{
203 struct mii_bus *bus = dev_get_drvdata(dev);
Mark Waredacac4d2009-07-23 10:56:48 -0700204
Mark Waredacac4d2009-07-23 10:56:48 -0700205 free_mdio_bitbang(bus);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000206}
207
Bill Pemberton633d1592012-12-03 09:24:14 -0500208static void mdio_gpio_bus_destroy(struct device *dev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000209{
210 struct mii_bus *bus = dev_get_drvdata(dev);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200211
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000212 mdiobus_unregister(bus);
Mark Waredacac4d2009-07-23 10:56:48 -0700213 mdio_gpio_bus_deinit(dev);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000214}
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200215
Bill Pemberton633d1592012-12-03 09:24:14 -0500216static int mdio_gpio_probe(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000217{
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000218 struct mdio_gpio_platform_data *pdata;
Mark Waredacac4d2009-07-23 10:56:48 -0700219 struct mii_bus *new_bus;
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000220 int ret, bus_id;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000221
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000222 if (pdev->dev.of_node) {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000223 pdata = mdio_gpio_of_get_data(pdev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000224 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
Johan Hovold7f52da52014-05-08 10:09:21 +0200225 if (bus_id < 0) {
226 dev_warn(&pdev->dev, "failed to get alias id\n");
227 bus_id = 0;
228 }
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000229 } else {
Jingoo Han9bc7b1c2013-08-30 14:08:55 +0900230 pdata = dev_get_platdata(&pdev->dev);
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000231 bus_id = pdev->id;
232 }
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000233
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000234 if (!pdata)
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200235 return -ENODEV;
236
Srinivas Kandagatla3272dd92012-11-16 00:33:59 +0000237 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id);
Mark Waredacac4d2009-07-23 10:56:48 -0700238 if (!new_bus)
239 return -ENODEV;
240
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000241 if (pdev->dev.of_node)
242 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
243 else
244 ret = mdiobus_register(new_bus);
245
Mark Waredacac4d2009-07-23 10:56:48 -0700246 if (ret)
247 mdio_gpio_bus_deinit(&pdev->dev);
248
249 return ret;
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000250}
251
Bill Pemberton633d1592012-12-03 09:24:14 -0500252static int mdio_gpio_remove(struct platform_device *pdev)
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000253{
254 mdio_gpio_bus_destroy(&pdev->dev);
255
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200256 return 0;
257}
258
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100259static const struct of_device_id mdio_gpio_of_match[] = {
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000260 { .compatible = "virtual,mdio-gpio", },
261 { /* sentinel */ }
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200262};
Luis de Bethencourt1ccb1412015-09-18 18:16:29 +0200263MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
Laurent Pincharta5edecc2008-05-26 11:53:21 +0200264
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000265static struct platform_driver mdio_gpio_driver = {
266 .probe = mdio_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500267 .remove = mdio_gpio_remove,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000268 .driver = {
269 .name = "mdio-gpio",
Srinivas Kandagatlae92bdf4b2012-08-24 01:59:17 +0000270 .of_match_table = mdio_gpio_of_match,
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000271 },
272};
273
Sachin Kamatf8e5fc82013-03-20 01:41:31 +0000274module_platform_driver(mdio_gpio_driver);
Paulius Zaleckasf004f3e2008-11-14 00:24:34 +0000275
276MODULE_ALIAS("platform:mdio-gpio");
277MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
278MODULE_LICENSE("GPL");
279MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");