Merge tag 'iommu-updates-v4.21' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / net / phy / mdio-gpio.c
CommitLineData
a5edeccb 1/*
f004f3ea
PZ
2 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
a5edeccb
LP
4 *
5 * Copyright (c) 2008 CSE Semaphore Belgium.
6 * by Laurent Pinchart <laurentp@cse-semaphore.com>
7 *
f004f3ea
PZ
8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
9 *
a5edeccb
LP
10 * 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>
a5edeccb 25#include <linux/interrupt.h>
f004f3ea 26#include <linux/platform_device.h>
04fa26ba 27#include <linux/platform_data/mdio-gpio.h>
fb78a95e
AL
28#include <linux/mdio-bitbang.h>
29#include <linux/mdio-gpio.h>
0207dd11 30#include <linux/gpio/consumer.h>
dacac4da 31#include <linux/of_mdio.h>
a5edeccb
LP
32
33struct mdio_gpio_info {
34 struct mdiobb_ctrl ctrl;
7e5fbd1e 35 struct gpio_desc *mdc, *mdio, *mdo;
a5edeccb
LP
36};
37
4029ea3a
AL
38static int mdio_gpio_get_data(struct device *dev,
39 struct mdio_gpio_info *bitbang)
e92bdf4b 40{
fb78a95e
AL
41 bitbang->mdc = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDC,
42 GPIOD_OUT_LOW);
4029ea3a
AL
43 if (IS_ERR(bitbang->mdc))
44 return PTR_ERR(bitbang->mdc);
e92bdf4b 45
fb78a95e
AL
46 bitbang->mdio = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDIO,
47 GPIOD_IN);
4029ea3a
AL
48 if (IS_ERR(bitbang->mdio))
49 return PTR_ERR(bitbang->mdio);
f1d54c47 50
fb78a95e 51 bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, MDIO_GPIO_MDO,
4029ea3a
AL
52 GPIOD_OUT_LOW);
53 return PTR_ERR_OR_ZERO(bitbang->mdo);
e92bdf4b
SK
54}
55
a5edeccb
LP
56static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
57{
58 struct mdio_gpio_info *bitbang =
59 container_of(ctrl, struct mdio_gpio_info, ctrl);
60
f1d54c47
GR
61 if (bitbang->mdo) {
62 /* Separate output pin. Always set its value to high
63 * when changing direction. If direction is input,
64 * assume the pin serves as pull-up. If direction is
65 * output, the default value is high.
66 */
df5a8ec6 67 gpiod_set_value_cansleep(bitbang->mdo, 1);
f1d54c47
GR
68 return;
69 }
70
a5edeccb 71 if (dir)
52aab18e 72 gpiod_direction_output(bitbang->mdio, 1);
a5edeccb 73 else
7e5fbd1e 74 gpiod_direction_input(bitbang->mdio);
a5edeccb
LP
75}
76
f004f3ea 77static int mdio_get(struct mdiobb_ctrl *ctrl)
a5edeccb
LP
78{
79 struct mdio_gpio_info *bitbang =
80 container_of(ctrl, struct mdio_gpio_info, ctrl);
81
df5a8ec6 82 return gpiod_get_value_cansleep(bitbang->mdio);
a5edeccb
LP
83}
84
f004f3ea 85static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
a5edeccb
LP
86{
87 struct mdio_gpio_info *bitbang =
88 container_of(ctrl, struct mdio_gpio_info, ctrl);
89
f1d54c47 90 if (bitbang->mdo)
df5a8ec6 91 gpiod_set_value_cansleep(bitbang->mdo, what);
f1d54c47 92 else
df5a8ec6 93 gpiod_set_value_cansleep(bitbang->mdio, what);
a5edeccb
LP
94}
95
f004f3ea 96static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
a5edeccb
LP
97{
98 struct mdio_gpio_info *bitbang =
99 container_of(ctrl, struct mdio_gpio_info, ctrl);
100
df5a8ec6 101 gpiod_set_value_cansleep(bitbang->mdc, what);
a5edeccb
LP
102}
103
41a130f7 104static const struct mdiobb_ops mdio_gpio_ops = {
a5edeccb 105 .owner = THIS_MODULE,
f004f3ea 106 .set_mdc = mdc_set,
a5edeccb 107 .set_mdio_dir = mdio_dir,
f004f3ea
PZ
108 .set_mdio_data = mdio_set,
109 .get_mdio_data = mdio_get,
a5edeccb
LP
110};
111
633d1594 112static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
fb766421 113 struct mdio_gpio_info *bitbang,
1dd06ae8 114 int bus_id)
a5edeccb 115{
04fa26ba 116 struct mdio_gpio_platform_data *pdata = dev_get_platdata(dev);
a5edeccb 117 struct mii_bus *new_bus;
a5edeccb
LP
118
119 bitbang->ctrl.ops = &mdio_gpio_ops;
120
121 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
122 if (!new_bus)
c82fc481 123 return NULL;
a5edeccb 124
712e5a5c 125 new_bus->name = "GPIO Bitbanged MDIO";
f004f3ea
PZ
126 new_bus->parent = dev;
127
7c0c8268
BV
128 if (bus_id != -1)
129 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
130 else
131 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
f004f3ea 132
dc9d38ce 133 if (pdata) {
04fa26ba 134 new_bus->phy_mask = pdata->phy_mask;
dc9d38ce
AL
135 new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
136 }
04fa26ba 137
f004f3ea 138 dev_set_drvdata(dev, new_bus);
a5edeccb 139
dacac4da 140 return new_bus;
a5edeccb
LP
141}
142
f99b4a02 143static void mdio_gpio_bus_deinit(struct device *dev)
a5edeccb 144{
f004f3ea 145 struct mii_bus *bus = dev_get_drvdata(dev);
a5edeccb 146
dacac4da 147 free_mdio_bitbang(bus);
f004f3ea
PZ
148}
149
633d1594 150static void mdio_gpio_bus_destroy(struct device *dev)
dacac4da
MW
151{
152 struct mii_bus *bus = dev_get_drvdata(dev);
153
154 mdiobus_unregister(bus);
155 mdio_gpio_bus_deinit(dev);
156}
157
633d1594 158static int mdio_gpio_probe(struct platform_device *pdev)
f004f3ea 159{
fb766421 160 struct mdio_gpio_info *bitbang;
dacac4da 161 struct mii_bus *new_bus;
3272dd9b 162 int ret, bus_id;
f004f3ea 163
fb766421
AL
164 bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
165 if (!bitbang)
166 return -ENOMEM;
167
4029ea3a
AL
168 ret = mdio_gpio_get_data(&pdev->dev, bitbang);
169 if (ret)
170 return ret;
171
3272dd9b 172 if (pdev->dev.of_node) {
3272dd9b 173 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
7f52da56
JH
174 if (bus_id < 0) {
175 dev_warn(&pdev->dev, "failed to get alias id\n");
176 bus_id = 0;
177 }
3272dd9b 178 } else {
3272dd9b
SK
179 bus_id = pdev->id;
180 }
e92bdf4b 181
4029ea3a 182 new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, bus_id);
dacac4da
MW
183 if (!new_bus)
184 return -ENODEV;
185
00e798c7 186 ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
dacac4da
MW
187 if (ret)
188 mdio_gpio_bus_deinit(&pdev->dev);
189
190 return ret;
f004f3ea
PZ
191}
192
633d1594 193static int mdio_gpio_remove(struct platform_device *pdev)
f004f3ea
PZ
194{
195 mdio_gpio_bus_destroy(&pdev->dev);
196
197 return 0;
198}
199
d8a7dadb 200static const struct of_device_id mdio_gpio_of_match[] = {
e92bdf4b
SK
201 { .compatible = "virtual,mdio-gpio", },
202 { /* sentinel */ }
a5edeccb 203};
1ccb141e 204MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
a5edeccb 205
f004f3ea
PZ
206static struct platform_driver mdio_gpio_driver = {
207 .probe = mdio_gpio_probe,
633d1594 208 .remove = mdio_gpio_remove,
f004f3ea
PZ
209 .driver = {
210 .name = "mdio-gpio",
e92bdf4b 211 .of_match_table = mdio_gpio_of_match,
f004f3ea
PZ
212 },
213};
214
f8e5fc8c 215module_platform_driver(mdio_gpio_driver);
a5edeccb 216
f004f3ea
PZ
217MODULE_ALIAS("platform:mdio-gpio");
218MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
219MODULE_LICENSE("GPL");
220MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");