1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * GPIO based MDIO bitbang driver. |
4 | * Supports OpenFirmware. |
5 | * |
6 | * Copyright (c) 2008 CSE Semaphore Belgium. |
7 | * by Laurent Pinchart <laurentp@cse-semaphore.com> |
8 | * |
9 | * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt> |
10 | * |
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> |
18 | */ |
19 | |
20 | #include <linux/gpio/consumer.h> |
21 | #include <linux/interrupt.h> |
22 | #include <linux/mdio-bitbang.h> |
23 | #include <linux/mdio-gpio.h> |
24 | #include <linux/module.h> |
25 | #include <linux/of_mdio.h> |
26 | #include <linux/platform_data/mdio-gpio.h> |
27 | #include <linux/platform_device.h> |
28 | #include <linux/slab.h> |
29 | |
30 | struct mdio_gpio_info { |
31 | struct mdiobb_ctrl ctrl; |
32 | struct gpio_desc *mdc, *mdio, *mdo; |
33 | }; |
34 | |
35 | static int mdio_gpio_get_data(struct device *dev, |
36 | struct mdio_gpio_info *bitbang) |
37 | { |
38 | bitbang->mdc = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDC, |
39 | flags: GPIOD_OUT_LOW); |
40 | if (IS_ERR(ptr: bitbang->mdc)) |
41 | return PTR_ERR(ptr: bitbang->mdc); |
42 | |
43 | bitbang->mdio = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDIO, |
44 | flags: GPIOD_IN); |
45 | if (IS_ERR(ptr: bitbang->mdio)) |
46 | return PTR_ERR(ptr: bitbang->mdio); |
47 | |
48 | bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, MDIO_GPIO_MDO, |
49 | flags: GPIOD_OUT_LOW); |
50 | return PTR_ERR_OR_ZERO(ptr: bitbang->mdo); |
51 | } |
52 | |
53 | static 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 | |
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 | */ |
64 | gpiod_set_value_cansleep(desc: bitbang->mdo, value: 1); |
65 | return; |
66 | } |
67 | |
68 | if (dir) |
69 | gpiod_direction_output(desc: bitbang->mdio, value: 1); |
70 | else |
71 | gpiod_direction_input(desc: bitbang->mdio); |
72 | } |
73 | |
74 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
75 | { |
76 | struct mdio_gpio_info *bitbang = |
77 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
78 | |
79 | return gpiod_get_value_cansleep(desc: bitbang->mdio); |
80 | } |
81 | |
82 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
83 | { |
84 | struct mdio_gpio_info *bitbang = |
85 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
86 | |
87 | if (bitbang->mdo) |
88 | gpiod_set_value_cansleep(desc: bitbang->mdo, value: what); |
89 | else |
90 | gpiod_set_value_cansleep(desc: bitbang->mdio, value: what); |
91 | } |
92 | |
93 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
94 | { |
95 | struct mdio_gpio_info *bitbang = |
96 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
97 | |
98 | gpiod_set_value_cansleep(desc: bitbang->mdc, value: what); |
99 | } |
100 | |
101 | static const struct mdiobb_ops mdio_gpio_ops = { |
102 | .owner = THIS_MODULE, |
103 | .set_mdc = mdc_set, |
104 | .set_mdio_dir = mdio_dir, |
105 | .set_mdio_data = mdio_set, |
106 | .get_mdio_data = mdio_get, |
107 | }; |
108 | |
109 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
110 | struct mdio_gpio_info *bitbang, |
111 | int bus_id) |
112 | { |
113 | struct mdio_gpio_platform_data *pdata = dev_get_platdata(dev); |
114 | struct mii_bus *new_bus; |
115 | |
116 | bitbang->ctrl.ops = &mdio_gpio_ops; |
117 | |
118 | new_bus = alloc_mdio_bitbang(ctrl: &bitbang->ctrl); |
119 | if (!new_bus) |
120 | return NULL; |
121 | |
122 | new_bus->name = "GPIO Bitbanged MDIO" ; |
123 | new_bus->parent = dev; |
124 | |
125 | if (bus_id != -1) |
126 | snprintf(buf: new_bus->id, size: sizeof(new_bus->id), fmt: "gpio-%x" , bus_id); |
127 | else |
128 | strscpy(new_bus->id, "gpio" , sizeof(new_bus->id)); |
129 | |
130 | if (pdata) { |
131 | new_bus->phy_mask = pdata->phy_mask; |
132 | new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask; |
133 | } |
134 | |
135 | if (dev->of_node && |
136 | of_device_is_compatible(device: 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 | |
142 | dev_set_drvdata(dev, data: new_bus); |
143 | |
144 | return new_bus; |
145 | } |
146 | |
147 | static void mdio_gpio_bus_deinit(struct device *dev) |
148 | { |
149 | struct mii_bus *bus = dev_get_drvdata(dev); |
150 | |
151 | free_mdio_bitbang(bus); |
152 | } |
153 | |
154 | static void mdio_gpio_bus_destroy(struct device *dev) |
155 | { |
156 | struct mii_bus *bus = dev_get_drvdata(dev); |
157 | |
158 | mdiobus_unregister(bus); |
159 | mdio_gpio_bus_deinit(dev); |
160 | } |
161 | |
162 | static int mdio_gpio_probe(struct platform_device *pdev) |
163 | { |
164 | struct mdio_gpio_info *bitbang; |
165 | struct mii_bus *new_bus; |
166 | int ret, bus_id; |
167 | |
168 | bitbang = devm_kzalloc(dev: &pdev->dev, size: sizeof(*bitbang), GFP_KERNEL); |
169 | if (!bitbang) |
170 | return -ENOMEM; |
171 | |
172 | ret = mdio_gpio_get_data(dev: &pdev->dev, bitbang); |
173 | if (ret) |
174 | return ret; |
175 | |
176 | if (pdev->dev.of_node) { |
177 | bus_id = of_alias_get_id(np: pdev->dev.of_node, stem: "mdio-gpio" ); |
178 | if (bus_id < 0) { |
179 | dev_warn(&pdev->dev, "failed to get alias id\n" ); |
180 | bus_id = 0; |
181 | } |
182 | } else { |
183 | bus_id = pdev->id; |
184 | } |
185 | |
186 | new_bus = mdio_gpio_bus_init(dev: &pdev->dev, bitbang, bus_id); |
187 | if (!new_bus) |
188 | return -ENODEV; |
189 | |
190 | ret = of_mdiobus_register(mdio: new_bus, np: pdev->dev.of_node); |
191 | if (ret) |
192 | mdio_gpio_bus_deinit(dev: &pdev->dev); |
193 | |
194 | return ret; |
195 | } |
196 | |
197 | static void mdio_gpio_remove(struct platform_device *pdev) |
198 | { |
199 | mdio_gpio_bus_destroy(dev: &pdev->dev); |
200 | } |
201 | |
202 | static const struct of_device_id mdio_gpio_of_match[] = { |
203 | { .compatible = "virtual,mdio-gpio" , }, |
204 | { .compatible = "microchip,mdio-smi0" }, |
205 | { /* sentinel */ } |
206 | }; |
207 | MODULE_DEVICE_TABLE(of, mdio_gpio_of_match); |
208 | |
209 | static struct platform_driver mdio_gpio_driver = { |
210 | .probe = mdio_gpio_probe, |
211 | .remove_new = mdio_gpio_remove, |
212 | .driver = { |
213 | .name = "mdio-gpio" , |
214 | .of_match_table = mdio_gpio_of_match, |
215 | }, |
216 | }; |
217 | |
218 | module_platform_driver(mdio_gpio_driver); |
219 | |
220 | MODULE_ALIAS("platform:mdio-gpio" ); |
221 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas" ); |
222 | MODULE_LICENSE("GPL v2" ); |
223 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO" ); |
224 | |