mdio: Add support for mdio drivers.
[deliverable/linux.git] / drivers / net / phy / mdio_device.c
CommitLineData
a9049e0c
AL
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
26void mdio_device_free(struct mdio_device *mdiodev)
27{
28 put_device(&mdiodev->dev);
29}
30EXPORT_SYMBOL(mdio_device_free);
31
32static void mdio_device_release(struct device *dev)
33{
34 kfree(to_mdio_device(dev));
35}
36
37struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
38{
39 struct mdio_device *mdiodev;
40
41 /* We allocate the device, and initialize the default values */
42 mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
43 if (!mdiodev)
44 return ERR_PTR(-ENOMEM);
45
46 mdiodev->dev.release = mdio_device_release;
47 mdiodev->dev.parent = &bus->dev;
48 mdiodev->dev.bus = &mdio_bus_type;
49 mdiodev->bus = bus;
50 mdiodev->addr = addr;
51
52 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
53
54 device_initialize(&mdiodev->dev);
55
56 return mdiodev;
57}
58EXPORT_SYMBOL(mdio_device_create);
59
60/**
61 * mdio_device_register - Register the mdio device on the MDIO bus
62 * @mdiodev: mdio_device structure to be added to the MDIO bus
63 */
64int mdio_device_register(struct mdio_device *mdiodev)
65{
66 int err;
67
68 dev_info(&mdiodev->dev, "mdio_device_register\n");
69
70 err = mdiobus_register_device(mdiodev);
71 if (err)
72 return err;
73
74 err = device_add(&mdiodev->dev);
75 if (err) {
76 pr_err("MDIO %d failed to add\n", mdiodev->addr);
77 goto out;
78 }
79
80 return 0;
81
82 out:
83 mdiobus_unregister_device(mdiodev);
84 return err;
85}
86EXPORT_SYMBOL(mdio_device_register);
87
88/**
89 * mdio_device_remove - Remove a previously registered mdio device from the
90 * MDIO bus
91 * @mdiodev: mdio_device structure to remove
92 *
93 * This doesn't free the mdio_device itself, it merely reverses the effects
94 * of mdio_device_register(). Use mdio_device_free() to free the device
95 * after calling this function.
96 */
97void mdio_device_remove(struct mdio_device *mdiodev)
98{
99 device_del(&mdiodev->dev);
100 mdiobus_unregister_device(mdiodev);
101}
102EXPORT_SYMBOL(mdio_device_remove);
103
104/**
105 * mdio_probe - probe an MDIO device
106 * @dev: device to probe
107 *
108 * Description: Take care of setting up the mdio_device structure
109 * and calling the driver to probe the device.
110 */
111static int mdio_probe(struct device *dev)
112{
113 struct mdio_device *mdiodev = to_mdio_device(dev);
114 struct device_driver *drv = mdiodev->dev.driver;
115 struct mdio_driver *mdiodrv = to_mdio_driver(drv);
116 int err = 0;
117
118 if (mdiodrv->probe)
119 err = mdiodrv->probe(mdiodev);
120
121 return err;
122}
123
124static int mdio_remove(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
130 if (mdiodrv->remove)
131 mdiodrv->remove(mdiodev);
132
133 return 0;
134}
135
136/**
137 * mdio_driver_register - register an mdio_driver with the MDIO layer
138 * @new_driver: new mdio_driver to register
139 */
140int mdio_driver_register(struct mdio_driver *drv)
141{
142 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
143 int retval;
144
145 pr_info("mdio_driver_register: %s\n", mdiodrv->driver.name);
146
147 mdiodrv->driver.bus = &mdio_bus_type;
148 mdiodrv->driver.probe = mdio_probe;
149 mdiodrv->driver.remove = mdio_remove;
150
151 retval = driver_register(&mdiodrv->driver);
152 if (retval) {
153 pr_err("%s: Error %d in registering driver\n",
154 mdiodrv->driver.name, retval);
155
156 return retval;
157 }
158
159 return 0;
160}
161EXPORT_SYMBOL(mdio_driver_register);
162
163void mdio_driver_unregister(struct mdio_driver *drv)
164{
165 struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
166
167 driver_unregister(&mdiodrv->driver);
168}
169EXPORT_SYMBOL(mdio_driver_unregister);
This page took 0.030597 seconds and 5 git commands to generate.