Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/* Framework for MDIO devices, other than PHYs.
3 *
4 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
5 */
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9#include <linux/delay.h>
10#include <linux/errno.h>
11#include <linux/gpio.h>
12#include <linux/gpio/consumer.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/mdio.h>
17#include <linux/mii.h>
18#include <linux/module.h>
19#include <linux/phy.h>
20#include <linux/reset.h>
21#include <linux/slab.h>
22#include <linux/string.h>
23#include <linux/unistd.h>
24#include <linux/property.h>
25#include "mdio-private.h"
26
27void mdio_device_free(struct mdio_device *mdiodev)
28{
29 put_device(&mdiodev->dev);
30}
31EXPORT_SYMBOL(mdio_device_free);
32
33static void mdio_device_release(struct device *dev)
34{
35 fwnode_handle_put(dev->fwnode);
36 kfree(to_mdio_device(dev));
37}
38
39static int mdio_device_bus_match(struct device *dev,
40 const struct device_driver *drv)
41{
42 struct mdio_device *mdiodev = to_mdio_device(dev);
43 const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
44
45 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
46 return 0;
47
48 return strcmp(mdiodev->modalias, drv->name) == 0;
49}
50
51struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
52{
53 struct mdio_device *mdiodev;
54
55 /* We allocate the device, and initialize the default values */
56 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
57 if (!mdiodev)
58 return ERR_PTR(-ENOMEM);
59
60 mdiodev->dev.release = mdio_device_release;
61 mdiodev->dev.parent = &bus->dev;
62 mdiodev->dev.bus = &mdio_bus_type;
63 mdiodev->bus_match = mdio_device_bus_match;
64 mdiodev->device_free = mdio_device_free;
65 mdiodev->device_remove = mdio_device_remove;
66 mdiodev->bus = bus;
67 mdiodev->addr = addr;
68 mdiodev->reset_state = -1;
69
70 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
71
72 device_initialize(&mdiodev->dev);
73
74 return mdiodev;
75}
76EXPORT_SYMBOL(mdio_device_create);
77
78/**
79 * mdio_device_register - Register the mdio device on the MDIO bus
80 * @mdiodev: mdio_device structure to be added to the MDIO bus
81 *
82 * Return: Zero if successful, negative error code on failure
83 */
84int mdio_device_register(struct mdio_device *mdiodev)
85{
86 int err;
87
88 dev_dbg(&mdiodev->dev, "%s\n", __func__);
89
90 err = mdiobus_register_device(mdiodev);
91 if (err)
92 return err;
93
94 err = device_add(&mdiodev->dev);
95 if (err) {
96 pr_err("MDIO %d failed to add\n", mdiodev->addr);
97 goto out;
98 }
99
100 return 0;
101
102 out:
103 mdiobus_unregister_device(mdiodev);
104 return err;
105}
106EXPORT_SYMBOL(mdio_device_register);
107
108/**
109 * mdio_device_remove - Remove a previously registered mdio device from the
110 * MDIO bus
111 * @mdiodev: mdio_device structure to remove
112 *
113 * This doesn't free the mdio_device itself, it merely reverses the effects
114 * of mdio_device_register(). Use mdio_device_free() to free the device
115 * after calling this function.
116 */
117void mdio_device_remove(struct mdio_device *mdiodev)
118{
119 device_del(&mdiodev->dev);
120 mdiobus_unregister_device(mdiodev);
121}
122EXPORT_SYMBOL(mdio_device_remove);
123
124/**
125 * mdio_device_register_reset - Read and initialize the reset properties of
126 * an mdio device
127 * @mdiodev: mdio_device structure
128 *
129 * Return: Zero if successful, negative error code on failure
130 */
131int mdio_device_register_reset(struct mdio_device *mdiodev)
132{
133 struct reset_control *reset;
134
135 /* Deassert the optional reset signal */
136 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
137 "reset", GPIOD_OUT_LOW);
138 if (IS_ERR(mdiodev->reset_gpio))
139 return PTR_ERR(mdiodev->reset_gpio);
140
141 if (mdiodev->reset_gpio)
142 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
143
144 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
145 if (IS_ERR(reset)) {
146 gpiod_put(mdiodev->reset_gpio);
147 mdiodev->reset_gpio = NULL;
148 return PTR_ERR(reset);
149 }
150
151 mdiodev->reset_ctrl = reset;
152
153 /* Read optional firmware properties */
154 device_property_read_u32(&mdiodev->dev, "reset-assert-us",
155 &mdiodev->reset_assert_delay);
156 device_property_read_u32(&mdiodev->dev, "reset-deassert-us",
157 &mdiodev->reset_deassert_delay);
158
159 return 0;
160}
161
162/**
163 * mdio_device_unregister_reset - uninitialize the reset properties of
164 * an mdio device
165 * @mdiodev: mdio_device structure
166 */
167void mdio_device_unregister_reset(struct mdio_device *mdiodev)
168{
169 gpiod_put(mdiodev->reset_gpio);
170 mdiodev->reset_gpio = NULL;
171 reset_control_put(mdiodev->reset_ctrl);
172 mdiodev->reset_ctrl = NULL;
173 mdiodev->reset_assert_delay = 0;
174 mdiodev->reset_deassert_delay = 0;
175}
176
177void mdio_device_reset(struct mdio_device *mdiodev, int value)
178{
179 unsigned int d;
180
181 if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
182 return;
183
184 if (mdiodev->reset_state == value)
185 return;
186
187 if (mdiodev->reset_gpio)
188 gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
189
190 if (mdiodev->reset_ctrl) {
191 if (value)
192 reset_control_assert(mdiodev->reset_ctrl);
193 else
194 reset_control_deassert(mdiodev->reset_ctrl);
195 }
196
197 d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
198 if (d)
199 fsleep(d);
200
201 mdiodev->reset_state = value;
202}
203EXPORT_SYMBOL(mdio_device_reset);
204
205/**
206 * mdio_probe - probe an MDIO device
207 * @dev: device to probe
208 *
209 * Description: Take care of setting up the mdio_device structure
210 * and calling the driver to probe the device.
211 *
212 * Return: Zero if successful, negative error code on failure
213 */
214static int mdio_probe(struct device *dev)
215{
216 struct mdio_device *mdiodev = to_mdio_device(dev);
217 struct device_driver *drv = mdiodev->dev.driver;
218 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
219 int err = 0;
220
221 /* Deassert the reset signal */
222 mdio_device_reset(mdiodev, 0);
223
224 if (mdiodrv->probe) {
225 err = mdiodrv->probe(mdiodev);
226 if (err) {
227 /* Assert the reset signal */
228 mdio_device_reset(mdiodev, 1);
229 }
230 }
231
232 return err;
233}
234
235static int mdio_remove(struct device *dev)
236{
237 struct mdio_device *mdiodev = to_mdio_device(dev);
238 struct device_driver *drv = mdiodev->dev.driver;
239 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
240
241 if (mdiodrv->remove)
242 mdiodrv->remove(mdiodev);
243
244 /* Assert the reset signal */
245 mdio_device_reset(mdiodev, 1);
246
247 return 0;
248}
249
250static void mdio_shutdown(struct device *dev)
251{
252 struct mdio_device *mdiodev = to_mdio_device(dev);
253 struct device_driver *drv = mdiodev->dev.driver;
254 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
255
256 if (mdiodrv->shutdown)
257 mdiodrv->shutdown(mdiodev);
258}
259
260/**
261 * mdio_driver_register - register an mdio_driver with the MDIO layer
262 * @drv: new mdio_driver to register
263 *
264 * Return: Zero if successful, negative error code on failure
265 */
266int mdio_driver_register(struct mdio_driver *drv)
267{
268 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
269 int retval;
270
271 pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
272
273 mdiodrv->driver.bus = &mdio_bus_type;
274 mdiodrv->driver.probe = mdio_probe;
275 mdiodrv->driver.remove = mdio_remove;
276 mdiodrv->driver.shutdown = mdio_shutdown;
277
278 retval = driver_register(&mdiodrv->driver);
279 if (retval) {
280 pr_err("%s: Error %d in registering driver\n",
281 mdiodrv->driver.name, retval);
282
283 return retval;
284 }
285
286 return 0;
287}
288EXPORT_SYMBOL(mdio_driver_register);
289
290void mdio_driver_unregister(struct mdio_driver *drv)
291{
292 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
293
294 driver_unregister(&mdiodrv->driver);
295}
296EXPORT_SYMBOL(mdio_driver_unregister);