Linux-libre 4.14.69-gnu
[librecmc/linux-libre.git] / drivers / net / phy / mdio_device.c
1 /* Framework for MDIO devices, other than PHYs.
2  *
3  * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
4  *
5  * This program is free software; you can redistribute  it and/or modify it
6  * under  the terms of  the GNU General  Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  *
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mdio.h>
19 #include <linux/mii.h>
20 #include <linux/module.h>
21 #include <linux/phy.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/unistd.h>
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 {
34         kfree(to_mdio_device(dev));
35 }
36
37 int mdio_device_bus_match(struct device *dev, struct device_driver *drv)
38 {
39         struct mdio_device *mdiodev = to_mdio_device(dev);
40         struct mdio_driver *mdiodrv = to_mdio_driver(drv);
41
42         if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
43                 return 0;
44
45         return strcmp(mdiodev->modalias, drv->name) == 0;
46 }
47
48 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
49 {
50         struct mdio_device *mdiodev;
51
52         /* We allocate the device, and initialize the default values */
53         mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
54         if (!mdiodev)
55                 return ERR_PTR(-ENOMEM);
56
57         mdiodev->dev.release = mdio_device_release;
58         mdiodev->dev.parent = &bus->dev;
59         mdiodev->dev.bus = &mdio_bus_type;
60         mdiodev->device_free = mdio_device_free;
61         mdiodev->device_remove = mdio_device_remove;
62         mdiodev->bus = bus;
63         mdiodev->addr = addr;
64
65         dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
66
67         device_initialize(&mdiodev->dev);
68
69         return mdiodev;
70 }
71 EXPORT_SYMBOL(mdio_device_create);
72
73 /**
74  * mdio_device_register - Register the mdio device on the MDIO bus
75  * @mdiodev: mdio_device structure to be added to the MDIO bus
76  */
77 int mdio_device_register(struct mdio_device *mdiodev)
78 {
79         int err;
80
81         dev_dbg(&mdiodev->dev, "mdio_device_register\n");
82
83         err = mdiobus_register_device(mdiodev);
84         if (err)
85                 return err;
86
87         err = device_add(&mdiodev->dev);
88         if (err) {
89                 pr_err("MDIO %d failed to add\n", mdiodev->addr);
90                 goto out;
91         }
92
93         return 0;
94
95  out:
96         mdiobus_unregister_device(mdiodev);
97         return err;
98 }
99 EXPORT_SYMBOL(mdio_device_register);
100
101 /**
102  * mdio_device_remove - Remove a previously registered mdio device from the
103  *                      MDIO bus
104  * @mdiodev: mdio_device structure to remove
105  *
106  * This doesn't free the mdio_device itself, it merely reverses the effects
107  * of mdio_device_register(). Use mdio_device_free() to free the device
108  * after calling this function.
109  */
110 void mdio_device_remove(struct mdio_device *mdiodev)
111 {
112         device_del(&mdiodev->dev);
113         mdiobus_unregister_device(mdiodev);
114 }
115 EXPORT_SYMBOL(mdio_device_remove);
116
117 /**
118  * mdio_probe - probe an MDIO device
119  * @dev: device to probe
120  *
121  * Description: Take care of setting up the mdio_device structure
122  * and calling the driver to probe the device.
123  */
124 static int mdio_probe(struct device *dev)
125 {
126         struct mdio_device *mdiodev = to_mdio_device(dev);
127         struct device_driver *drv = mdiodev->dev.driver;
128         struct mdio_driver *mdiodrv = to_mdio_driver(drv);
129         int err = 0;
130
131         if (mdiodrv->probe)
132                 err = mdiodrv->probe(mdiodev);
133
134         return err;
135 }
136
137 static int mdio_remove(struct device *dev)
138 {
139         struct mdio_device *mdiodev = to_mdio_device(dev);
140         struct device_driver *drv = mdiodev->dev.driver;
141         struct mdio_driver *mdiodrv = to_mdio_driver(drv);
142
143         if (mdiodrv->remove)
144                 mdiodrv->remove(mdiodev);
145
146         return 0;
147 }
148
149 /**
150  * mdio_driver_register - register an mdio_driver with the MDIO layer
151  * @new_driver: new mdio_driver to register
152  */
153 int mdio_driver_register(struct mdio_driver *drv)
154 {
155         struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
156         int retval;
157
158         pr_debug("mdio_driver_register: %s\n", mdiodrv->driver.name);
159
160         mdiodrv->driver.bus = &mdio_bus_type;
161         mdiodrv->driver.probe = mdio_probe;
162         mdiodrv->driver.remove = mdio_remove;
163
164         retval = driver_register(&mdiodrv->driver);
165         if (retval) {
166                 pr_err("%s: Error %d in registering driver\n",
167                        mdiodrv->driver.name, retval);
168
169                 return retval;
170         }
171
172         return 0;
173 }
174 EXPORT_SYMBOL(mdio_driver_register);
175
176 void mdio_driver_unregister(struct mdio_driver *drv)
177 {
178         struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
179
180         driver_unregister(&mdiodrv->driver);
181 }
182 EXPORT_SYMBOL(mdio_driver_unregister);