2019-01-21 18:05:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-01-06 19:11:26 +00:00
|
|
|
/* Framework for MDIO devices, other than PHYs.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2020-06-26 15:53:21 +00:00
|
|
|
#include <linux/delay.h>
|
2016-01-06 19:11:26 +00:00
|
|
|
#include <linux/errno.h>
|
2017-12-04 12:35:05 +00:00
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/gpio/consumer.h>
|
2016-01-06 19:11:26 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mdio.h>
|
|
|
|
#include <linux/mii.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/phy.h>
|
2019-04-17 21:59:21 +00:00
|
|
|
#include <linux/reset.h>
|
2016-01-06 19:11:26 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/unistd.h>
|
2022-12-03 07:34:41 +00:00
|
|
|
#include <linux/property.h>
|
2016-01-06 19:11:26 +00:00
|
|
|
|
|
|
|
void mdio_device_free(struct mdio_device *mdiodev)
|
|
|
|
{
|
|
|
|
put_device(&mdiodev->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_device_free);
|
|
|
|
|
|
|
|
static void mdio_device_release(struct device *dev)
|
|
|
|
{
|
2022-12-03 07:34:41 +00:00
|
|
|
fwnode_handle_put(dev->fwnode);
|
2016-01-06 19:11:26 +00:00
|
|
|
kfree(to_mdio_device(dev));
|
|
|
|
}
|
|
|
|
|
2017-02-04 21:02:44 +00:00
|
|
|
int mdio_device_bus_match(struct device *dev, struct device_driver *drv)
|
|
|
|
{
|
|
|
|
struct mdio_device *mdiodev = to_mdio_device(dev);
|
|
|
|
struct mdio_driver *mdiodrv = to_mdio_driver(drv);
|
|
|
|
|
|
|
|
if (mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return strcmp(mdiodev->modalias, drv->name) == 0;
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:11:26 +00:00
|
|
|
struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr)
|
|
|
|
{
|
|
|
|
struct mdio_device *mdiodev;
|
|
|
|
|
|
|
|
/* We allocate the device, and initialize the default values */
|
|
|
|
mdiodev = kzalloc(sizeof(*mdiodev), GFP_KERNEL);
|
|
|
|
if (!mdiodev)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
mdiodev->dev.release = mdio_device_release;
|
|
|
|
mdiodev->dev.parent = &bus->dev;
|
|
|
|
mdiodev->dev.bus = &mdio_bus_type;
|
2016-01-06 19:11:27 +00:00
|
|
|
mdiodev->device_free = mdio_device_free;
|
|
|
|
mdiodev->device_remove = mdio_device_remove;
|
2016-01-06 19:11:26 +00:00
|
|
|
mdiodev->bus = bus;
|
|
|
|
mdiodev->addr = addr;
|
|
|
|
|
|
|
|
dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
|
|
|
|
|
|
|
|
device_initialize(&mdiodev->dev);
|
|
|
|
|
|
|
|
return mdiodev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_device_create);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mdio_device_register - Register the mdio device on the MDIO bus
|
|
|
|
* @mdiodev: mdio_device structure to be added to the MDIO bus
|
|
|
|
*/
|
|
|
|
int mdio_device_register(struct mdio_device *mdiodev)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2021-06-16 10:01:24 +00:00
|
|
|
dev_dbg(&mdiodev->dev, "%s\n", __func__);
|
2016-01-06 19:11:26 +00:00
|
|
|
|
|
|
|
err = mdiobus_register_device(mdiodev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = device_add(&mdiodev->dev);
|
|
|
|
if (err) {
|
|
|
|
pr_err("MDIO %d failed to add\n", mdiodev->addr);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mdiobus_unregister_device(mdiodev);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_device_register);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mdio_device_remove - Remove a previously registered mdio device from the
|
|
|
|
* MDIO bus
|
|
|
|
* @mdiodev: mdio_device structure to remove
|
|
|
|
*
|
|
|
|
* This doesn't free the mdio_device itself, it merely reverses the effects
|
|
|
|
* of mdio_device_register(). Use mdio_device_free() to free the device
|
|
|
|
* after calling this function.
|
|
|
|
*/
|
|
|
|
void mdio_device_remove(struct mdio_device *mdiodev)
|
|
|
|
{
|
|
|
|
device_del(&mdiodev->dev);
|
|
|
|
mdiobus_unregister_device(mdiodev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_device_remove);
|
|
|
|
|
2017-12-04 12:35:05 +00:00
|
|
|
void mdio_device_reset(struct mdio_device *mdiodev, int value)
|
|
|
|
{
|
2017-12-11 12:16:57 +00:00
|
|
|
unsigned int d;
|
|
|
|
|
2019-04-17 21:59:22 +00:00
|
|
|
if (!mdiodev->reset_gpio && !mdiodev->reset_ctrl)
|
2017-12-11 12:16:57 +00:00
|
|
|
return;
|
|
|
|
|
2019-04-17 21:59:22 +00:00
|
|
|
if (mdiodev->reset_gpio)
|
2019-10-04 13:53:32 +00:00
|
|
|
gpiod_set_value_cansleep(mdiodev->reset_gpio, value);
|
2019-04-17 21:59:21 +00:00
|
|
|
|
|
|
|
if (mdiodev->reset_ctrl) {
|
|
|
|
if (value)
|
|
|
|
reset_control_assert(mdiodev->reset_ctrl);
|
|
|
|
else
|
|
|
|
reset_control_deassert(mdiodev->reset_ctrl);
|
|
|
|
}
|
2017-12-11 12:16:57 +00:00
|
|
|
|
2017-12-22 10:08:09 +00:00
|
|
|
d = value ? mdiodev->reset_assert_delay : mdiodev->reset_deassert_delay;
|
2017-12-11 12:16:57 +00:00
|
|
|
if (d)
|
2020-07-30 19:57:49 +00:00
|
|
|
fsleep(d);
|
2017-12-04 12:35:05 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_device_reset);
|
|
|
|
|
2016-01-06 19:11:26 +00:00
|
|
|
/**
|
|
|
|
* mdio_probe - probe an MDIO device
|
|
|
|
* @dev: device to probe
|
|
|
|
*
|
|
|
|
* Description: Take care of setting up the mdio_device structure
|
|
|
|
* and calling the driver to probe the device.
|
|
|
|
*/
|
|
|
|
static int mdio_probe(struct device *dev)
|
|
|
|
{
|
|
|
|
struct mdio_device *mdiodev = to_mdio_device(dev);
|
|
|
|
struct device_driver *drv = mdiodev->dev.driver;
|
|
|
|
struct mdio_driver *mdiodrv = to_mdio_driver(drv);
|
|
|
|
int err = 0;
|
|
|
|
|
2020-06-26 15:53:25 +00:00
|
|
|
/* Deassert the reset signal */
|
|
|
|
mdio_device_reset(mdiodev, 0);
|
2017-12-04 12:35:05 +00:00
|
|
|
|
2020-06-26 15:53:25 +00:00
|
|
|
if (mdiodrv->probe) {
|
2016-01-06 19:11:26 +00:00
|
|
|
err = mdiodrv->probe(mdiodev);
|
2017-12-04 12:35:05 +00:00
|
|
|
if (err) {
|
|
|
|
/* Assert the reset signal */
|
|
|
|
mdio_device_reset(mdiodev, 1);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 19:11:26 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mdio_remove(struct device *dev)
|
|
|
|
{
|
|
|
|
struct mdio_device *mdiodev = to_mdio_device(dev);
|
|
|
|
struct device_driver *drv = mdiodev->dev.driver;
|
|
|
|
struct mdio_driver *mdiodrv = to_mdio_driver(drv);
|
|
|
|
|
2020-06-26 15:53:25 +00:00
|
|
|
if (mdiodrv->remove)
|
2016-01-06 19:11:26 +00:00
|
|
|
mdiodrv->remove(mdiodev);
|
|
|
|
|
2020-06-26 15:53:25 +00:00
|
|
|
/* Assert the reset signal */
|
|
|
|
mdio_device_reset(mdiodev, 1);
|
2017-12-04 12:35:05 +00:00
|
|
|
|
2016-01-06 19:11:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-17 13:34:32 +00:00
|
|
|
static void mdio_shutdown(struct device *dev)
|
|
|
|
{
|
|
|
|
struct mdio_device *mdiodev = to_mdio_device(dev);
|
|
|
|
struct device_driver *drv = mdiodev->dev.driver;
|
|
|
|
struct mdio_driver *mdiodrv = to_mdio_driver(drv);
|
|
|
|
|
|
|
|
if (mdiodrv->shutdown)
|
|
|
|
mdiodrv->shutdown(mdiodev);
|
|
|
|
}
|
|
|
|
|
2016-01-06 19:11:26 +00:00
|
|
|
/**
|
|
|
|
* mdio_driver_register - register an mdio_driver with the MDIO layer
|
2020-07-07 01:49:34 +00:00
|
|
|
* @drv: new mdio_driver to register
|
2016-01-06 19:11:26 +00:00
|
|
|
*/
|
|
|
|
int mdio_driver_register(struct mdio_driver *drv)
|
|
|
|
{
|
|
|
|
struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
|
|
|
|
int retval;
|
|
|
|
|
2021-06-16 10:01:24 +00:00
|
|
|
pr_debug("%s: %s\n", __func__, mdiodrv->driver.name);
|
2016-01-06 19:11:26 +00:00
|
|
|
|
|
|
|
mdiodrv->driver.bus = &mdio_bus_type;
|
|
|
|
mdiodrv->driver.probe = mdio_probe;
|
|
|
|
mdiodrv->driver.remove = mdio_remove;
|
2021-09-17 13:34:32 +00:00
|
|
|
mdiodrv->driver.shutdown = mdio_shutdown;
|
2016-01-06 19:11:26 +00:00
|
|
|
|
|
|
|
retval = driver_register(&mdiodrv->driver);
|
|
|
|
if (retval) {
|
|
|
|
pr_err("%s: Error %d in registering driver\n",
|
|
|
|
mdiodrv->driver.name, retval);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_driver_register);
|
|
|
|
|
|
|
|
void mdio_driver_unregister(struct mdio_driver *drv)
|
|
|
|
{
|
|
|
|
struct mdio_driver_common *mdiodrv = &drv->mdiodrv;
|
|
|
|
|
|
|
|
driver_unregister(&mdiodrv->driver);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mdio_driver_unregister);
|