Commit | Line | Data |
---|---|---|
a2443fd1 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
a9049e0c AL |
2 | /* Framework for MDIO devices, other than PHYs. |
3 | * | |
4 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> | |
a9049e0c AL |
5 | */ |
6 | ||
7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
8 | ||
1d0018a4 | 9 | #include <linux/delay.h> |
a9049e0c | 10 | #include <linux/errno.h> |
bafbdd52 SS |
11 | #include <linux/gpio.h> |
12 | #include <linux/gpio/consumer.h> | |
a9049e0c AL |
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> | |
71dd6c0d | 20 | #include <linux/reset.h> |
a9049e0c AL |
21 | #include <linux/slab.h> |
22 | #include <linux/string.h> | |
23 | #include <linux/unistd.h> | |
cb376176 | 24 | #include <linux/property.h> |
a9049e0c AL |
25 | |
26 | void mdio_device_free(struct mdio_device *mdiodev) | |
27 | { | |
28 | put_device(&mdiodev->dev); | |
29 | } | |
30 | EXPORT_SYMBOL(mdio_device_free); | |
31 | ||
32 | static void mdio_device_release(struct device *dev) | |
33 | { | |
cb376176 | 34 | fwnode_handle_put(dev->fwnode); |
a9049e0c AL |
35 | kfree(to_mdio_device(dev)); |
36 | } | |
37 | ||
d69d8048 | 38 | int mdio_device_bus_match(struct device *dev, const struct device_driver *drv) |
648ea013 FF |
39 | { |
40 | struct mdio_device *mdiodev = to_mdio_device(dev); | |
d69d8048 | 41 | const struct mdio_driver *mdiodrv = to_mdio_driver(drv); |
648ea013 FF |
42 | |
43 | if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) | |
44 | return 0; | |
45 | ||
46 | return strcmp(mdiodev->modalias, drv->name) == 0; | |
47 | } | |
31be641d | 48 | EXPORT_SYMBOL_GPL(mdio_device_bus_match); |
648ea013 | 49 | |
a9049e0c AL |
50 | struct 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(-ENOMEM); | |
58 | ||
59 | mdiodev->dev.release = mdio_device_release; | |
60 | mdiodev->dev.parent = &bus->dev; | |
61 | mdiodev->dev.bus = &mdio_bus_type; | |
711fdba3 AL |
62 | mdiodev->device_free = mdio_device_free; |
63 | mdiodev->device_remove = mdio_device_remove; | |
a9049e0c AL |
64 | mdiodev->bus = bus; |
65 | mdiodev->addr = addr; | |
df16c1c5 | 66 | mdiodev->reset_state = -1; |
a9049e0c AL |
67 | |
68 | dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); | |
69 | ||
70 | device_initialize(&mdiodev->dev); | |
71 | ||
72 | return mdiodev; | |
73 | } | |
74 | EXPORT_SYMBOL(mdio_device_create); | |
75 | ||
76 | /** | |
77 | * mdio_device_register - Register the mdio device on the MDIO bus | |
78 | * @mdiodev: mdio_device structure to be added to the MDIO bus | |
79 | */ | |
80 | int mdio_device_register(struct mdio_device *mdiodev) | |
81 | { | |
82 | int err; | |
83 | ||
450bf1f0 | 84 | dev_dbg(&mdiodev->dev, "%s\n", __func__); |
a9049e0c AL |
85 | |
86 | err = mdiobus_register_device(mdiodev); | |
87 | if (err) | |
88 | return err; | |
89 | ||
90 | err = device_add(&mdiodev->dev); | |
91 | if (err) { | |
92 | pr_err("MDIO %d failed to add\n", mdiodev->addr); | |
93 | goto out; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | ||
98 | out: | |
99 | mdiobus_unregister_device(mdiodev); | |
100 | return err; | |
101 | } | |
102 | EXPORT_SYMBOL(mdio_device_register); | |
103 | ||
104 | /** | |
105 | * mdio_device_remove - Remove a previously registered mdio device from the | |
106 | * MDIO bus | |
107 | * @mdiodev: mdio_device structure to remove | |
108 | * | |
109 | * This doesn't free the mdio_device itself, it merely reverses the effects | |
110 | * of mdio_device_register(). Use mdio_device_free() to free the device | |
111 | * after calling this function. | |
112 | */ | |
113 | void mdio_device_remove(struct mdio_device *mdiodev) | |
114 | { | |
115 | device_del(&mdiodev->dev); | |
116 | mdiobus_unregister_device(mdiodev); | |
117 | } | |
118 | EXPORT_SYMBOL(mdio_device_remove); | |
119 | ||
bafbdd52 SS |
120 | void mdio_device_reset(struct mdio_device *mdiodev, int value) |
121 | { | |
3a30ae6e RL |
122 | unsigned int d; |
123 | ||
6110ed2d | 124 | if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl) |
3a30ae6e RL |
125 | return; |
126 | ||
df16c1c5 AH |
127 | if (mdiodev->reset_state == value) |
128 | return; | |
129 | ||
6110ed2d | 130 | if (mdiodev->reset_gpio) |
ea977d19 | 131 | gpiod_set_value_cansleep(mdiodev->reset_gpio, value); |
71dd6c0d DB |
132 | |
133 | if (mdiodev->reset_ctrl) { | |
134 | if (value) | |
135 | reset_control_assert(mdiodev->reset_ctrl); | |
136 | else | |
137 | reset_control_deassert(mdiodev->reset_ctrl); | |
138 | } | |
3a30ae6e | 139 | |
04f629f7 | 140 | d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay; |
3a30ae6e | 141 | if (d) |
e4d5efdd | 142 | fsleep(d); |
df16c1c5 AH |
143 | |
144 | mdiodev->reset_state = value; | |
bafbdd52 SS |
145 | } |
146 | EXPORT_SYMBOL(mdio_device_reset); | |
147 | ||
a9049e0c AL |
148 | /** |
149 | * mdio_probe - probe an MDIO device | |
150 | * @dev: device to probe | |
151 | * | |
152 | * Description: Take care of setting up the mdio_device structure | |
153 | * and calling the driver to probe the device. | |
154 | */ | |
155 | static int mdio_probe(struct device *dev) | |
156 | { | |
157 | struct mdio_device *mdiodev = to_mdio_device(dev); | |
158 | struct device_driver *drv = mdiodev->dev.driver; | |
159 | struct mdio_driver *mdiodrv = to_mdio_driver(drv); | |
160 | int err = 0; | |
161 | ||
96e26359 BG |
162 | /* Deassert the reset signal */ |
163 | mdio_device_reset(mdiodev, 0); | |
bafbdd52 | 164 | |
96e26359 | 165 | if (mdiodrv->probe) { |
a9049e0c | 166 | err = mdiodrv->probe(mdiodev); |
bafbdd52 SS |
167 | if (err) { |
168 | /* Assert the reset signal */ | |
169 | mdio_device_reset(mdiodev, 1); | |
170 | } | |
171 | } | |
a9049e0c AL |
172 | |
173 | return err; | |
174 | } | |
175 | ||
176 | static int mdio_remove(struct device *dev) | |
177 | { | |
178 | struct mdio_device *mdiodev = to_mdio_device(dev); | |
179 | struct device_driver *drv = mdiodev->dev.driver; | |
180 | struct mdio_driver *mdiodrv = to_mdio_driver(drv); | |
181 | ||
96e26359 | 182 | if (mdiodrv->remove) |
a9049e0c AL |
183 | mdiodrv->remove(mdiodev); |
184 | ||
96e26359 BG |
185 | /* Assert the reset signal */ |
186 | mdio_device_reset(mdiodev, 1); | |
bafbdd52 | 187 | |
a9049e0c AL |
188 | return 0; |
189 | } | |
190 | ||
cf957997 VO |
191 | static void mdio_shutdown(struct device *dev) |
192 | { | |
193 | struct mdio_device *mdiodev = to_mdio_device(dev); | |
194 | struct device_driver *drv = mdiodev->dev.driver; | |
195 | struct mdio_driver *mdiodrv = to_mdio_driver(drv); | |
196 | ||
197 | if (mdiodrv->shutdown) | |
198 | mdiodrv->shutdown(mdiodev); | |
199 | } | |
200 | ||
a9049e0c AL |
201 | /** |
202 | * mdio_driver_register - register an mdio_driver with the MDIO layer | |
19c5a5fe | 203 | * @drv: new mdio_driver to register |
a9049e0c AL |
204 | */ |
205 | int mdio_driver_register(struct mdio_driver *drv) | |
206 | { | |
207 | struct mdio_driver_common *mdiodrv = &drv->mdiodrv; | |
208 | int retval; | |
209 | ||
450bf1f0 | 210 | pr_debug("%s: %s\n", __func__, mdiodrv->driver.name); |
a9049e0c AL |
211 | |
212 | mdiodrv->driver.bus = &mdio_bus_type; | |
213 | mdiodrv->driver.probe = mdio_probe; | |
214 | mdiodrv->driver.remove = mdio_remove; | |
cf957997 | 215 | mdiodrv->driver.shutdown = mdio_shutdown; |
a9049e0c AL |
216 | |
217 | retval = driver_register(&mdiodrv->driver); | |
218 | if (retval) { | |
219 | pr_err("%s: Error %d in registering driver\n", | |
220 | mdiodrv->driver.name, retval); | |
221 | ||
222 | return retval; | |
223 | } | |
224 | ||
225 | return 0; | |
226 | } | |
227 | EXPORT_SYMBOL(mdio_driver_register); | |
228 | ||
229 | void mdio_driver_unregister(struct mdio_driver *drv) | |
230 | { | |
231 | struct mdio_driver_common *mdiodrv = &drv->mdiodrv; | |
232 | ||
233 | driver_unregister(&mdiodrv->driver); | |
234 | } | |
235 | EXPORT_SYMBOL(mdio_driver_unregister); |