xref: /linux/drivers/net/phy/mdio_device.c (revision acde7ad968f6c536397b39b23e4c70dc466fbc59)
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 
27 void mdio_device_free(struct mdio_device *mdiodev)
28 {
29 	put_device(&mdiodev->dev);
30 }
31 EXPORT_SYMBOL(mdio_device_free);
32 
33 static void mdio_device_release(struct device *dev)
34 {
35 	fwnode_handle_put(dev->fwnode);
36 	kfree(to_mdio_device(dev));
37 }
38 
39 static 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 
51 struct 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 }
76 EXPORT_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 int mdio_device_register(struct mdio_device *mdiodev)
83 {
84 	int err;
85 
86 	dev_dbg(&mdiodev->dev, "%s\n", __func__);
87 
88 	err = mdiobus_register_device(mdiodev);
89 	if (err)
90 		return err;
91 
92 	err = device_add(&mdiodev->dev);
93 	if (err) {
94 		pr_err("MDIO %d failed to add\n", mdiodev->addr);
95 		goto out;
96 	}
97 
98 	return 0;
99 
100  out:
101 	mdiobus_unregister_device(mdiodev);
102 	return err;
103 }
104 EXPORT_SYMBOL(mdio_device_register);
105 
106 /**
107  * mdio_device_remove - Remove a previously registered mdio device from the
108  *			MDIO bus
109  * @mdiodev: mdio_device structure to remove
110  *
111  * This doesn't free the mdio_device itself, it merely reverses the effects
112  * of mdio_device_register(). Use mdio_device_free() to free the device
113  * after calling this function.
114  */
115 void mdio_device_remove(struct mdio_device *mdiodev)
116 {
117 	device_del(&mdiodev->dev);
118 	mdiobus_unregister_device(mdiodev);
119 }
120 EXPORT_SYMBOL(mdio_device_remove);
121 
122 /**
123  * mdio_device_register_reset - Read and initialize the reset properties of
124  *				an mdio device
125  * @mdiodev: mdio_device structure
126  *
127  * Return: Zero if successful, negative error code on failure
128  */
129 int mdio_device_register_reset(struct mdio_device *mdiodev)
130 {
131 	struct reset_control *reset;
132 
133 	/* Deassert the optional reset signal */
134 	mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
135 						 "reset", GPIOD_OUT_LOW);
136 	if (IS_ERR(mdiodev->reset_gpio))
137 		return PTR_ERR(mdiodev->reset_gpio);
138 
139 	if (mdiodev->reset_gpio)
140 		gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
141 
142 	reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
143 	if (IS_ERR(reset)) {
144 		gpiod_put(mdiodev->reset_gpio);
145 		mdiodev->reset_gpio = NULL;
146 		return PTR_ERR(reset);
147 	}
148 
149 	mdiodev->reset_ctrl = reset;
150 
151 	/* Read optional firmware properties */
152 	fwnode_property_read_u32(dev_fwnode(&mdiodev->dev), "reset-assert-us",
153 				 &mdiodev->reset_assert_delay);
154 	fwnode_property_read_u32(dev_fwnode(&mdiodev->dev), "reset-deassert-us",
155 				 &mdiodev->reset_deassert_delay);
156 
157 	return 0;
158 }
159 
160 /**
161  * mdio_device_unregister_reset - uninitialize the reset properties of
162  *				  an mdio device
163  * @mdiodev: mdio_device structure
164  */
165 void mdio_device_unregister_reset(struct mdio_device *mdiodev)
166 {
167 	gpiod_put(mdiodev->reset_gpio);
168 	reset_control_put(mdiodev->reset_ctrl);
169 }
170 
171 void mdio_device_reset(struct mdio_device *mdiodev, int value)
172 {
173 	unsigned int d;
174 
175 	if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
176 		return;
177 
178 	if (mdiodev->reset_state == value)
179 		return;
180 
181 	if (mdiodev->reset_gpio)
182 		gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
183 
184 	if (mdiodev->reset_ctrl) {
185 		if (value)
186 			reset_control_assert(mdiodev->reset_ctrl);
187 		else
188 			reset_control_deassert(mdiodev->reset_ctrl);
189 	}
190 
191 	d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
192 	if (d)
193 		fsleep(d);
194 
195 	mdiodev->reset_state = value;
196 }
197 EXPORT_SYMBOL(mdio_device_reset);
198 
199 /**
200  * mdio_probe - probe an MDIO device
201  * @dev: device to probe
202  *
203  * Description: Take care of setting up the mdio_device structure
204  * and calling the driver to probe the device.
205  */
206 static int mdio_probe(struct device *dev)
207 {
208 	struct mdio_device *mdiodev = to_mdio_device(dev);
209 	struct device_driver *drv = mdiodev->dev.driver;
210 	struct mdio_driver *mdiodrv = to_mdio_driver(drv);
211 	int err = 0;
212 
213 	/* Deassert the reset signal */
214 	mdio_device_reset(mdiodev, 0);
215 
216 	if (mdiodrv->probe) {
217 		err = mdiodrv->probe(mdiodev);
218 		if (err) {
219 			/* Assert the reset signal */
220 			mdio_device_reset(mdiodev, 1);
221 		}
222 	}
223 
224 	return err;
225 }
226 
227 static int mdio_remove(struct device *dev)
228 {
229 	struct mdio_device *mdiodev = to_mdio_device(dev);
230 	struct device_driver *drv = mdiodev->dev.driver;
231 	struct mdio_driver *mdiodrv = to_mdio_driver(drv);
232 
233 	if (mdiodrv->remove)
234 		mdiodrv->remove(mdiodev);
235 
236 	/* Assert the reset signal */
237 	mdio_device_reset(mdiodev, 1);
238 
239 	return 0;
240 }
241 
242 static void mdio_shutdown(struct device *dev)
243 {
244 	struct mdio_device *mdiodev = to_mdio_device(dev);
245 	struct device_driver *drv = mdiodev->dev.driver;
246 	struct mdio_driver *mdiodrv = to_mdio_driver(drv);
247 
248 	if (mdiodrv->shutdown)
249 		mdiodrv->shutdown(mdiodev);
250 }
251 
252 /**
253  * mdio_driver_register - register an mdio_driver with the MDIO layer
254  * @drv: new mdio_driver to register
255  */
256 int mdio_driver_register(struct mdio_driver *drv)
257 {
258 	struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
259 	int retval;
260 
261 	pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
262 
263 	mdiodrv->driver.bus = &mdio_bus_type;
264 	mdiodrv->driver.probe = mdio_probe;
265 	mdiodrv->driver.remove = mdio_remove;
266 	mdiodrv->driver.shutdown = mdio_shutdown;
267 
268 	retval = driver_register(&mdiodrv->driver);
269 	if (retval) {
270 		pr_err("%s: Error %d in registering driver\n",
271 		       mdiodrv->driver.name, retval);
272 
273 		return retval;
274 	}
275 
276 	return 0;
277 }
278 EXPORT_SYMBOL(mdio_driver_register);
279 
280 void mdio_driver_unregister(struct mdio_driver *drv)
281 {
282 	struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
283 
284 	driver_unregister(&mdiodrv->driver);
285 }
286 EXPORT_SYMBOL(mdio_driver_unregister);
287