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