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
26void mdio_device_free(struct mdio_device *mdiodev)
27{
28 put_device(dev: &mdiodev->dev);
29}
30EXPORT_SYMBOL(mdio_device_free);
31
32static void mdio_device_release(struct device *dev)
33{
34 fwnode_handle_put(fwnode: dev->fwnode);
35 kfree(to_mdio_device(dev));
36}
37
38static int mdio_device_bus_match(struct device *dev,
39 const struct device_driver *drv)
40{
41 struct mdio_device *mdiodev = to_mdio_device(dev);
42 const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
43
44 if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
45 return 0;
46
47 return strcmp(mdiodev->modalias, drv->name) == 0;
48}
49
50struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
51{
52 struct mdio_device *mdiodev;
53
54 /* We allocate the device, and initialize the default values */
55 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
56 if (!mdiodev)
57 return ERR_PTR(error: -ENOMEM);
58
59 mdiodev->dev.release = mdio_device_release;
60 mdiodev->dev.parent = &bus->dev;
61 mdiodev->dev.bus = &mdio_bus_type;
62 mdiodev->bus_match = mdio_device_bus_match;
63 mdiodev->device_free = mdio_device_free;
64 mdiodev->device_remove = mdio_device_remove;
65 mdiodev->bus = bus;
66 mdiodev->addr = addr;
67 mdiodev->reset_state = -1;
68
69 dev_set_name(dev: &mdiodev->dev, PHY_ID_FMT, bus->id, addr);
70
71 device_initialize(dev: &mdiodev->dev);
72
73 return mdiodev;
74}
75EXPORT_SYMBOL(mdio_device_create);
76
77/**
78 * mdio_device_register - Register the mdio device on the MDIO bus
79 * @mdiodev: mdio_device structure to be added to the MDIO bus
80 */
81int mdio_device_register(struct mdio_device *mdiodev)
82{
83 int err;
84
85 dev_dbg(&mdiodev->dev, "%s\n", __func__);
86
87 err = mdiobus_register_device(mdiodev);
88 if (err)
89 return err;
90
91 err = device_add(dev: &mdiodev->dev);
92 if (err) {
93 pr_err("MDIO %d failed to add\n", mdiodev->addr);
94 goto out;
95 }
96
97 return 0;
98
99 out:
100 mdiobus_unregister_device(mdiodev);
101 return err;
102}
103EXPORT_SYMBOL(mdio_device_register);
104
105/**
106 * mdio_device_remove - Remove a previously registered mdio device from the
107 * MDIO bus
108 * @mdiodev: mdio_device structure to remove
109 *
110 * This doesn't free the mdio_device itself, it merely reverses the effects
111 * of mdio_device_register(). Use mdio_device_free() to free the device
112 * after calling this function.
113 */
114void mdio_device_remove(struct mdio_device *mdiodev)
115{
116 device_del(dev: &mdiodev->dev);
117 mdiobus_unregister_device(mdiodev);
118}
119EXPORT_SYMBOL(mdio_device_remove);
120
121void mdio_device_reset(struct mdio_device *mdiodev, int value)
122{
123 unsigned int d;
124
125 if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
126 return;
127
128 if (mdiodev->reset_state == value)
129 return;
130
131 if (mdiodev->reset_gpio)
132 gpiod_set_value_cansleep(desc: mdiodev->reset_gpio, value);
133
134 if (mdiodev->reset_ctrl) {
135 if (value)
136 reset_control_assert(rstc: mdiodev->reset_ctrl);
137 else
138 reset_control_deassert(rstc: mdiodev->reset_ctrl);
139 }
140
141 d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
142 if (d)
143 fsleep(usecs: d);
144
145 mdiodev->reset_state = value;
146}
147EXPORT_SYMBOL(mdio_device_reset);
148
149/**
150 * mdio_probe - probe an MDIO device
151 * @dev: device to probe
152 *
153 * Description: Take care of setting up the mdio_device structure
154 * and calling the driver to probe the device.
155 */
156static int mdio_probe(struct device *dev)
157{
158 struct mdio_device *mdiodev = to_mdio_device(dev);
159 struct device_driver *drv = mdiodev->dev.driver;
160 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
161 int err = 0;
162
163 /* Deassert the reset signal */
164 mdio_device_reset(mdiodev, 0);
165
166 if (mdiodrv->probe) {
167 err = mdiodrv->probe(mdiodev);
168 if (err) {
169 /* Assert the reset signal */
170 mdio_device_reset(mdiodev, 1);
171 }
172 }
173
174 return err;
175}
176
177static int mdio_remove(struct device *dev)
178{
179 struct mdio_device *mdiodev = to_mdio_device(dev);
180 struct device_driver *drv = mdiodev->dev.driver;
181 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
182
183 if (mdiodrv->remove)
184 mdiodrv->remove(mdiodev);
185
186 /* Assert the reset signal */
187 mdio_device_reset(mdiodev, 1);
188
189 return 0;
190}
191
192static void mdio_shutdown(struct device *dev)
193{
194 struct mdio_device *mdiodev = to_mdio_device(dev);
195 struct device_driver *drv = mdiodev->dev.driver;
196 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
197
198 if (mdiodrv->shutdown)
199 mdiodrv->shutdown(mdiodev);
200}
201
202/**
203 * mdio_driver_register - register an mdio_driver with the MDIO layer
204 * @drv: new mdio_driver to register
205 */
206int mdio_driver_register(struct mdio_driver *drv)
207{
208 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
209 int retval;
210
211 pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
212
213 mdiodrv->driver.bus = &mdio_bus_type;
214 mdiodrv->driver.probe = mdio_probe;
215 mdiodrv->driver.remove = mdio_remove;
216 mdiodrv->driver.shutdown = mdio_shutdown;
217
218 retval = driver_register(drv: &mdiodrv->driver);
219 if (retval) {
220 pr_err("%s: Error %d in registering driver\n",
221 mdiodrv->driver.name, retval);
222
223 return retval;
224 }
225
226 return 0;
227}
228EXPORT_SYMBOL(mdio_driver_register);
229
230void mdio_driver_unregister(struct mdio_driver *drv)
231{
232 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
233
234 driver_unregister(drv: &mdiodrv->driver);
235}
236EXPORT_SYMBOL(mdio_driver_unregister);
237