Linux Audio

Check our new training course

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