2013-09-27 13:23:25 +07:00
|
|
|
/*
|
|
|
|
* phy-core.c -- Generic Phy framework.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
|
|
|
|
*
|
|
|
|
* Author: Kishon Vijay Abraham I <kishon@ti.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/export.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/phy/phy.h>
|
|
|
|
#include <linux/idr.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
2014-07-04 16:55:45 +07:00
|
|
|
#include <linux/regulator/consumer.h>
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
static struct class *phy_class;
|
|
|
|
static DEFINE_MUTEX(phy_provider_mutex);
|
|
|
|
static LIST_HEAD(phy_provider_list);
|
2014-11-19 22:28:18 +07:00
|
|
|
static LIST_HEAD(phys);
|
2013-09-27 13:23:25 +07:00
|
|
|
static DEFINE_IDA(phy_ida);
|
|
|
|
|
|
|
|
static void devm_phy_release(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
struct phy *phy = *(struct phy **)res;
|
|
|
|
|
|
|
|
phy_put(phy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void devm_phy_provider_release(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
struct phy_provider *phy_provider = *(struct phy_provider **)res;
|
|
|
|
|
|
|
|
of_phy_provider_unregister(phy_provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void devm_phy_consume(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
struct phy *phy = *(struct phy **)res;
|
|
|
|
|
|
|
|
phy_destroy(phy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int devm_phy_match(struct device *dev, void *res, void *match_data)
|
|
|
|
{
|
2015-02-25 22:16:29 +07:00
|
|
|
struct phy **phy = res;
|
|
|
|
|
|
|
|
return *phy == match_data;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
|
2014-11-19 22:28:18 +07:00
|
|
|
/**
|
|
|
|
* phy_create_lookup() - allocate and register PHY/device association
|
|
|
|
* @phy: the phy of the association
|
|
|
|
* @con_id: connection ID string on device
|
|
|
|
* @dev_id: the device of the association
|
|
|
|
*
|
|
|
|
* Creates and registers phy_lookup entry.
|
|
|
|
*/
|
|
|
|
int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
|
|
|
|
{
|
|
|
|
struct phy_lookup *pl;
|
|
|
|
|
|
|
|
if (!phy || !dev_id || !con_id)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
pl = kzalloc(sizeof(*pl), GFP_KERNEL);
|
|
|
|
if (!pl)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
pl->dev_id = dev_id;
|
|
|
|
pl->con_id = con_id;
|
|
|
|
pl->phy = phy;
|
|
|
|
|
|
|
|
mutex_lock(&phy_provider_mutex);
|
|
|
|
list_add_tail(&pl->node, &phys);
|
|
|
|
mutex_unlock(&phy_provider_mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_create_lookup);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_remove_lookup() - find and remove PHY/device association
|
|
|
|
* @phy: the phy of the association
|
|
|
|
* @con_id: connection ID string on device
|
|
|
|
* @dev_id: the device of the association
|
|
|
|
*
|
|
|
|
* Finds and unregisters phy_lookup entry that was created with
|
|
|
|
* phy_create_lookup().
|
|
|
|
*/
|
|
|
|
void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
|
|
|
|
{
|
|
|
|
struct phy_lookup *pl;
|
|
|
|
|
|
|
|
if (!phy || !dev_id || !con_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&phy_provider_mutex);
|
|
|
|
list_for_each_entry(pl, &phys, node)
|
|
|
|
if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
|
|
|
|
!strcmp(pl->con_id, con_id)) {
|
|
|
|
list_del(&pl->node);
|
|
|
|
kfree(pl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&phy_provider_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_remove_lookup);
|
|
|
|
|
|
|
|
static struct phy *phy_find(struct device *dev, const char *con_id)
|
|
|
|
{
|
|
|
|
const char *dev_id = dev_name(dev);
|
|
|
|
struct phy_lookup *p, *pl = NULL;
|
|
|
|
|
|
|
|
mutex_lock(&phy_provider_mutex);
|
|
|
|
list_for_each_entry(p, &phys, node)
|
|
|
|
if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
|
|
|
|
pl = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&phy_provider_mutex);
|
|
|
|
|
2014-11-19 22:28:21 +07:00
|
|
|
return pl ? pl->phy : ERR_PTR(-ENODEV);
|
2014-11-19 22:28:18 +07:00
|
|
|
}
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
|
|
|
|
{
|
|
|
|
struct phy_provider *phy_provider;
|
2014-07-14 17:25:01 +07:00
|
|
|
struct device_node *child;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
list_for_each_entry(phy_provider, &phy_provider_list, list) {
|
|
|
|
if (phy_provider->dev->of_node == node)
|
|
|
|
return phy_provider;
|
2014-07-14 17:25:01 +07:00
|
|
|
|
2016-04-05 22:17:34 +07:00
|
|
|
for_each_child_of_node(phy_provider->children, child)
|
2014-07-14 17:25:01 +07:00
|
|
|
if (child == node)
|
|
|
|
return phy_provider;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_PTR(-EPROBE_DEFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
int phy_pm_runtime_get(struct phy *phy)
|
|
|
|
{
|
2013-12-21 04:00:48 +07:00
|
|
|
int ret;
|
|
|
|
|
2018-03-20 13:01:47 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!pm_runtime_enabled(&phy->dev))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2013-12-21 04:00:48 +07:00
|
|
|
ret = pm_runtime_get(&phy->dev);
|
|
|
|
if (ret < 0 && ret != -EINPROGRESS)
|
|
|
|
pm_runtime_put_noidle(&phy->dev);
|
|
|
|
|
|
|
|
return ret;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
|
|
|
|
|
|
|
|
int phy_pm_runtime_get_sync(struct phy *phy)
|
|
|
|
{
|
2013-12-21 04:00:48 +07:00
|
|
|
int ret;
|
|
|
|
|
2018-03-20 13:01:47 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!pm_runtime_enabled(&phy->dev))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2013-12-21 04:00:48 +07:00
|
|
|
ret = pm_runtime_get_sync(&phy->dev);
|
|
|
|
if (ret < 0)
|
|
|
|
pm_runtime_put_sync(&phy->dev);
|
|
|
|
|
|
|
|
return ret;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
|
|
|
|
|
|
|
|
int phy_pm_runtime_put(struct phy *phy)
|
|
|
|
{
|
2018-03-20 13:01:47 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!pm_runtime_enabled(&phy->dev))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
return pm_runtime_put(&phy->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
|
|
|
|
|
|
|
|
int phy_pm_runtime_put_sync(struct phy *phy)
|
|
|
|
{
|
2018-03-20 13:01:47 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!pm_runtime_enabled(&phy->dev))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
return pm_runtime_put_sync(&phy->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
|
|
|
|
|
|
|
|
void phy_pm_runtime_allow(struct phy *phy)
|
|
|
|
{
|
2018-03-20 13:01:47 +07:00
|
|
|
if (!phy)
|
|
|
|
return;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!pm_runtime_enabled(&phy->dev))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pm_runtime_allow(&phy->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
|
|
|
|
|
|
|
|
void phy_pm_runtime_forbid(struct phy *phy)
|
|
|
|
{
|
2018-03-20 13:01:47 +07:00
|
|
|
if (!phy)
|
|
|
|
return;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!pm_runtime_enabled(&phy->dev))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pm_runtime_forbid(&phy->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
|
|
|
|
|
|
|
|
int phy_init(struct phy *phy)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-02-05 00:33:11 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy_pm_runtime_get_sync(phy);
|
|
|
|
if (ret < 0 && ret != -ENOTSUPP)
|
|
|
|
return ret;
|
2015-03-06 14:55:10 +07:00
|
|
|
ret = 0; /* Override possible ret == -ENOTSUPP */
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
2013-12-20 12:06:49 +07:00
|
|
|
if (phy->init_count == 0 && phy->ops->init) {
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy->ops->init(phy);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&phy->dev, "phy init failed --> %d\n", ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 12:06:49 +07:00
|
|
|
++phy->init_count;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
phy_pm_runtime_put(phy);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_init);
|
|
|
|
|
|
|
|
int phy_exit(struct phy *phy)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-02-05 00:33:11 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy_pm_runtime_get_sync(phy);
|
|
|
|
if (ret < 0 && ret != -ENOTSUPP)
|
|
|
|
return ret;
|
2015-03-06 14:55:10 +07:00
|
|
|
ret = 0; /* Override possible ret == -ENOTSUPP */
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
2013-12-20 12:06:49 +07:00
|
|
|
if (phy->init_count == 1 && phy->ops->exit) {
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy->ops->exit(phy);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 12:06:49 +07:00
|
|
|
--phy->init_count;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
phy_pm_runtime_put(phy);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_exit);
|
|
|
|
|
|
|
|
int phy_power_on(struct phy *phy)
|
|
|
|
{
|
2016-01-28 15:14:18 +07:00
|
|
|
int ret = 0;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
2014-02-05 00:33:11 +07:00
|
|
|
if (!phy)
|
2016-01-28 15:14:18 +07:00
|
|
|
goto out;
|
2014-02-05 00:33:11 +07:00
|
|
|
|
2014-07-04 16:55:45 +07:00
|
|
|
if (phy->pwr) {
|
|
|
|
ret = regulator_enable(phy->pwr);
|
|
|
|
if (ret)
|
2016-01-28 15:14:18 +07:00
|
|
|
goto out;
|
2014-07-04 16:55:45 +07:00
|
|
|
}
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy_pm_runtime_get_sync(phy);
|
|
|
|
if (ret < 0 && ret != -ENOTSUPP)
|
2016-01-28 15:14:18 +07:00
|
|
|
goto err_pm_sync;
|
|
|
|
|
2015-03-06 14:55:10 +07:00
|
|
|
ret = 0; /* Override possible ret == -ENOTSUPP */
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
2013-12-20 12:06:49 +07:00
|
|
|
if (phy->power_count == 0 && phy->ops->power_on) {
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy->ops->power_on(phy);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
|
2016-01-28 15:14:18 +07:00
|
|
|
goto err_pwr_on;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 12:06:49 +07:00
|
|
|
++phy->power_count;
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
return 0;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
2016-01-28 15:14:18 +07:00
|
|
|
err_pwr_on:
|
2013-09-27 13:23:25 +07:00
|
|
|
mutex_unlock(&phy->mutex);
|
2013-12-20 12:06:49 +07:00
|
|
|
phy_pm_runtime_put_sync(phy);
|
2016-01-28 15:14:18 +07:00
|
|
|
err_pm_sync:
|
2014-07-04 16:55:45 +07:00
|
|
|
if (phy->pwr)
|
|
|
|
regulator_disable(phy->pwr);
|
2016-01-28 15:14:18 +07:00
|
|
|
out:
|
2013-09-27 13:23:25 +07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_power_on);
|
|
|
|
|
|
|
|
int phy_power_off(struct phy *phy)
|
|
|
|
{
|
2013-12-19 21:31:44 +07:00
|
|
|
int ret;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
2014-02-05 00:33:11 +07:00
|
|
|
if (!phy)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
mutex_lock(&phy->mutex);
|
2013-12-20 12:06:49 +07:00
|
|
|
if (phy->power_count == 1 && phy->ops->power_off) {
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = phy->ops->power_off(phy);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
|
2013-12-20 12:06:49 +07:00
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
return ret;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 12:06:49 +07:00
|
|
|
--phy->power_count;
|
2013-09-27 13:23:25 +07:00
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
phy_pm_runtime_put(phy);
|
|
|
|
|
2014-07-04 16:55:45 +07:00
|
|
|
if (phy->pwr)
|
|
|
|
regulator_disable(phy->pwr);
|
|
|
|
|
2013-12-20 12:06:49 +07:00
|
|
|
return 0;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_power_off);
|
|
|
|
|
2018-11-20 08:24:20 +07:00
|
|
|
int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
|
2016-05-10 06:39:59 +07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!phy || !phy->ops->set_mode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
2018-11-20 08:24:20 +07:00
|
|
|
ret = phy->ops->set_mode(phy, mode, submode);
|
2018-01-16 17:57:09 +07:00
|
|
|
if (!ret)
|
|
|
|
phy->attrs.mode = mode;
|
2016-05-10 06:39:59 +07:00
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-11-20 08:24:20 +07:00
|
|
|
EXPORT_SYMBOL_GPL(phy_set_mode_ext);
|
2016-05-10 06:39:59 +07:00
|
|
|
|
2016-09-10 01:59:37 +07:00
|
|
|
int phy_reset(struct phy *phy)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!phy || !phy->ops->reset)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
|
|
|
ret = phy->ops->reset(phy);
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_reset);
|
|
|
|
|
2017-10-09 19:00:50 +07:00
|
|
|
int phy_calibrate(struct phy *phy)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!phy || !phy->ops->calibrate)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
|
|
|
ret = phy->ops->calibrate(phy);
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_calibrate);
|
|
|
|
|
phy: Add configuration interface
The phy framework is only allowing to configure the power state of the PHY
using the init and power_on hooks, and their power_off and exit
counterparts.
While it works for most, simple, PHYs supported so far, some more advanced
PHYs need some configuration depending on runtime parameters. These PHYs
have been supported by a number of means already, often by using ad-hoc
drivers in their consumer drivers.
That doesn't work too well however, when a consumer device needs to deal
with multiple PHYs, or when multiple consumers need to deal with the same
PHY (a DSI driver and a CSI driver for example).
So we'll add a new interface, through two funtions, phy_validate and
phy_configure. The first one will allow to check that a current
configuration, for a given mode, is applicable. It will also allow the PHY
driver to tune the settings given as parameters as it sees fit.
phy_configure will actually apply that configuration in the phy itself.
Signed-off-by: Maxime Ripard <maxime.ripard@bootlin.com>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
2018-12-07 20:55:29 +07:00
|
|
|
/**
|
|
|
|
* phy_configure() - Changes the phy parameters
|
|
|
|
* @phy: the phy returned by phy_get()
|
|
|
|
* @opts: New configuration to apply
|
|
|
|
*
|
|
|
|
* Used to change the PHY parameters. phy_init() must have been called
|
|
|
|
* on the phy. The configuration will be applied on the current phy
|
|
|
|
* mode, that can be changed using phy_set_mode().
|
|
|
|
*
|
|
|
|
* Returns: 0 if successful, an negative error code otherwise
|
|
|
|
*/
|
|
|
|
int phy_configure(struct phy *phy, union phy_configure_opts *opts)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!phy)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!phy->ops->configure)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
|
|
|
ret = phy->ops->configure(phy, opts);
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_configure);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_validate() - Checks the phy parameters
|
|
|
|
* @phy: the phy returned by phy_get()
|
|
|
|
* @mode: phy_mode the configuration is applicable to.
|
|
|
|
* @submode: PHY submode the configuration is applicable to.
|
|
|
|
* @opts: Configuration to check
|
|
|
|
*
|
|
|
|
* Used to check that the current set of parameters can be handled by
|
|
|
|
* the phy. Implementations are free to tune the parameters passed as
|
|
|
|
* arguments if needed by some implementation detail or
|
|
|
|
* constraints. It will not change any actual configuration of the
|
|
|
|
* PHY, so calling it as many times as deemed fit will have no side
|
|
|
|
* effect.
|
|
|
|
*
|
|
|
|
* Returns: 0 if successful, an negative error code otherwise
|
|
|
|
*/
|
|
|
|
int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
|
|
|
|
union phy_configure_opts *opts)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!phy)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!phy->ops->validate)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
mutex_lock(&phy->mutex);
|
|
|
|
ret = phy->ops->validate(phy, mode, submode, opts);
|
|
|
|
mutex_unlock(&phy->mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_validate);
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
/**
|
2014-03-06 18:16:46 +07:00
|
|
|
* _of_phy_get() - lookup and obtain a reference to a phy by phandle
|
|
|
|
* @np: device_node for which to get the phy
|
2013-09-27 13:23:25 +07:00
|
|
|
* @index: the index of the phy
|
|
|
|
*
|
|
|
|
* Returns the phy associated with the given phandle value,
|
|
|
|
* after getting a refcount to it or -ENODEV if there is no such phy or
|
|
|
|
* -EPROBE_DEFER if there is a phandle to the phy, but the device is
|
|
|
|
* not yet loaded. This function uses of_xlate call back function provided
|
|
|
|
* while registering the phy_provider to find the phy instance.
|
|
|
|
*/
|
2014-03-06 18:16:46 +07:00
|
|
|
static struct phy *_of_phy_get(struct device_node *np, int index)
|
2013-09-27 13:23:25 +07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct phy_provider *phy_provider;
|
|
|
|
struct phy *phy = NULL;
|
|
|
|
struct of_phandle_args args;
|
|
|
|
|
2014-03-06 18:16:46 +07:00
|
|
|
ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
|
2013-09-27 13:23:25 +07:00
|
|
|
index, &args);
|
2014-03-06 18:16:46 +07:00
|
|
|
if (ret)
|
2013-09-27 13:23:25 +07:00
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
phy: work around 'phys' references to usb-nop-xceiv devices
Stefan Wahren reports a problem with a warning fix that was merged
for v4.15: we had lots of device nodes with a 'phys' property pointing
to a device node that is not compliant with the binding documented in
Documentation/devicetree/bindings/phy/phy-bindings.txt
This generally works because USB HCD drivers that support both the generic
phy subsystem and the older usb-phy subsystem ignore most errors from
phy_get() and related calls and then use the usb-phy driver instead.
However, it turns out that making the usb-nop-xceiv device compatible with
the generic-phy binding changes the phy_get() return code from -EINVAL to
-EPROBE_DEFER, and the dwc2 usb controller driver for bcm2835 now returns
-EPROBE_DEFER from its probe function rather than ignoring the failure,
breaking all USB support on raspberry-pi when CONFIG_GENERIC_PHY is
enabled. The same code is used in the dwc3 driver and the usb_add_hcd()
function, so a reasonable assumption would be that many other platforms
are affected as well.
I have reviewed all the related patches and concluded that "usb-nop-xceiv"
is the only USB phy that is affected by the change, and since it is by far
the most commonly referenced phy, all the other USB phy drivers appear
to be used in ways that are are either safe in DT (they don't use the
'phys' property), or in the driver (they already ignore -EPROBE_DEFER
from generic-phy when usb-phy is available).
To work around the problem, this adds a special case to _of_phy_get()
so we ignore any PHY node that is compatible with "usb-nop-xceiv",
as we know that this can never load no matter how much we defer. In the
future, we might implement a generic-phy driver for "usb-nop-xceiv"
and then remove this workaround.
Since we generally want older kernels to also want to work with the
fixed devicetree files, it would be good to backport the patch into
stable kernels as well (3.13+ are possibly affected), even though they
don't contain any of the patches that may have caused regressions.
Fixes: 014d6da6cb25 ARM: dts: bcm283x: Fix DTC warnings about missing phy-cells
Fixes: c5bbf358b790 arm: dts: nspire: Add missing #phy-cells to usb-nop-xceiv
Fixes: 44e5dced2ef6 arm: dts: marvell: Add missing #phy-cells to usb-nop-xceiv
Fixes: f568f6f554b8 ARM: dts: omap: Add missing #phy-cells to usb-nop-xceiv
Fixes: d745d5f277bf ARM: dts: imx51-zii-rdu1: Add missing #phy-cells to usb-nop-xceiv
Fixes: 915fbe59cbf2 ARM: dts: imx: Add missing #phy-cells to usb-nop-xceiv
Link: https://marc.info/?l=linux-usb&m=151518314314753&w=2
Link: https://patchwork.kernel.org/patch/10158145/
Cc: stable@vger.kernel.org
Cc: Felipe Balbi <balbi@kernel.org>
Cc: Eric Anholt <eric@anholt.net>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Acked-by: Rob Herring <robh@kernel.org>
Tested-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Kishon Vijay Abraham I <kishon@ti.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2018-01-12 17:12:05 +07:00
|
|
|
/* This phy type handled by the usb-phy subsystem for now */
|
|
|
|
if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
mutex_lock(&phy_provider_mutex);
|
|
|
|
phy_provider = of_phy_provider_lookup(args.np);
|
|
|
|
if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
|
|
|
|
phy = ERR_PTR(-EPROBE_DEFER);
|
2015-04-07 11:23:38 +07:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!of_device_is_available(args.np)) {
|
|
|
|
dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
|
|
|
|
phy = ERR_PTR(-ENODEV);
|
|
|
|
goto out_put_module;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
phy = phy_provider->of_xlate(phy_provider->dev, &args);
|
2015-04-07 11:23:38 +07:00
|
|
|
|
|
|
|
out_put_module:
|
2013-09-27 13:23:25 +07:00
|
|
|
module_put(phy_provider->owner);
|
|
|
|
|
2015-04-07 11:23:38 +07:00
|
|
|
out_unlock:
|
2013-09-27 13:23:25 +07:00
|
|
|
mutex_unlock(&phy_provider_mutex);
|
|
|
|
of_node_put(args.np);
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
|
2014-03-06 18:16:46 +07:00
|
|
|
/**
|
|
|
|
* of_phy_get() - lookup and obtain a reference to a phy using a device_node.
|
|
|
|
* @np: device_node for which to get the phy
|
|
|
|
* @con_id: name of the phy from device's point of view
|
|
|
|
*
|
|
|
|
* Returns the phy driver, after getting a refcount to it; or
|
|
|
|
* -ENODEV if there is no such phy. The caller is responsible for
|
|
|
|
* calling phy_put() to release that count.
|
|
|
|
*/
|
|
|
|
struct phy *of_phy_get(struct device_node *np, const char *con_id)
|
|
|
|
{
|
|
|
|
struct phy *phy = NULL;
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
if (con_id)
|
|
|
|
index = of_property_match_string(np, "phy-names", con_id);
|
|
|
|
|
|
|
|
phy = _of_phy_get(np, index);
|
|
|
|
if (IS_ERR(phy))
|
|
|
|
return phy;
|
|
|
|
|
|
|
|
if (!try_module_get(phy->ops->owner))
|
|
|
|
return ERR_PTR(-EPROBE_DEFER);
|
|
|
|
|
|
|
|
get_device(&phy->dev);
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_phy_get);
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
/**
|
|
|
|
* phy_put() - release the PHY
|
|
|
|
* @phy: the phy returned by phy_get()
|
|
|
|
*
|
|
|
|
* Releases a refcount the caller received from phy_get().
|
|
|
|
*/
|
|
|
|
void phy_put(struct phy *phy)
|
|
|
|
{
|
2014-02-05 00:33:11 +07:00
|
|
|
if (!phy || IS_ERR(phy))
|
2013-09-27 13:23:25 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
module_put(phy->ops->owner);
|
|
|
|
put_device(&phy->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_put);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_phy_put() - release the PHY
|
|
|
|
* @dev: device that wants to release this phy
|
|
|
|
* @phy: the phy returned by devm_phy_get()
|
|
|
|
*
|
|
|
|
* destroys the devres associated with this phy and invokes phy_put
|
|
|
|
* to release the phy.
|
|
|
|
*/
|
|
|
|
void devm_phy_put(struct device *dev, struct phy *phy)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2014-02-05 00:33:11 +07:00
|
|
|
if (!phy)
|
|
|
|
return;
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
|
|
|
|
dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_phy_put);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_phy_simple_xlate() - returns the phy instance from phy provider
|
|
|
|
* @dev: the PHY provider device
|
|
|
|
* @args: of_phandle_args (not used here)
|
|
|
|
*
|
|
|
|
* Intended to be used by phy provider for the common case where #phy-cells is
|
|
|
|
* 0. For other cases where #phy-cells is greater than '0', the phy provider
|
|
|
|
* should provide a custom of_xlate function that reads the *args* and returns
|
|
|
|
* the appropriate phy.
|
|
|
|
*/
|
|
|
|
struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
|
|
|
|
*args)
|
|
|
|
{
|
|
|
|
struct phy *phy;
|
|
|
|
struct class_dev_iter iter;
|
|
|
|
|
|
|
|
class_dev_iter_init(&iter, phy_class, NULL, NULL);
|
|
|
|
while ((dev = class_dev_iter_next(&iter))) {
|
|
|
|
phy = to_phy(dev);
|
2014-10-31 15:34:20 +07:00
|
|
|
if (args->np != phy->dev.of_node)
|
2013-09-27 13:23:25 +07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
class_dev_iter_exit(&iter);
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
|
|
|
|
class_dev_iter_exit(&iter);
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_get() - lookup and obtain a reference to a phy.
|
|
|
|
* @dev: device that requests this phy
|
|
|
|
* @string: the phy name as given in the dt data or the name of the controller
|
|
|
|
* port for non-dt case
|
|
|
|
*
|
|
|
|
* Returns the phy driver, after getting a refcount to it; or
|
|
|
|
* -ENODEV if there is no such phy. The caller is responsible for
|
|
|
|
* calling phy_put() to release that count.
|
|
|
|
*/
|
|
|
|
struct phy *phy_get(struct device *dev, const char *string)
|
|
|
|
{
|
|
|
|
int index = 0;
|
2013-12-19 21:31:44 +07:00
|
|
|
struct phy *phy;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
if (string == NULL) {
|
|
|
|
dev_WARN(dev, "missing string\n");
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->of_node) {
|
|
|
|
index = of_property_match_string(dev->of_node, "phy-names",
|
|
|
|
string);
|
2014-03-06 18:16:46 +07:00
|
|
|
phy = _of_phy_get(dev->of_node, index);
|
2013-09-27 13:23:25 +07:00
|
|
|
} else {
|
2014-11-19 22:28:18 +07:00
|
|
|
phy = phy_find(dev, string);
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
2014-02-17 15:59:22 +07:00
|
|
|
if (IS_ERR(phy))
|
|
|
|
return phy;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
if (!try_module_get(phy->ops->owner))
|
|
|
|
return ERR_PTR(-EPROBE_DEFER);
|
|
|
|
|
|
|
|
get_device(&phy->dev);
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_get);
|
|
|
|
|
2014-02-05 00:33:12 +07:00
|
|
|
/**
|
|
|
|
* phy_optional_get() - lookup and obtain a reference to an optional phy.
|
|
|
|
* @dev: device that requests this phy
|
|
|
|
* @string: the phy name as given in the dt data or the name of the controller
|
|
|
|
* port for non-dt case
|
|
|
|
*
|
|
|
|
* Returns the phy driver, after getting a refcount to it; or
|
|
|
|
* NULL if there is no such phy. The caller is responsible for
|
|
|
|
* calling phy_put() to release that count.
|
|
|
|
*/
|
|
|
|
struct phy *phy_optional_get(struct device *dev, const char *string)
|
|
|
|
{
|
|
|
|
struct phy *phy = phy_get(dev, string);
|
|
|
|
|
2015-04-08 06:30:15 +07:00
|
|
|
if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
|
2014-02-05 00:33:12 +07:00
|
|
|
phy = NULL;
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_optional_get);
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
/**
|
|
|
|
* devm_phy_get() - lookup and obtain a reference to a phy.
|
|
|
|
* @dev: device that requests this phy
|
|
|
|
* @string: the phy name as given in the dt data or phy device name
|
|
|
|
* for non-dt case
|
|
|
|
*
|
|
|
|
* Gets the phy using phy_get(), and associates a device with it using
|
|
|
|
* devres. On driver detach, release function is invoked on the devres data,
|
|
|
|
* then, devres data is freed.
|
|
|
|
*/
|
|
|
|
struct phy *devm_phy_get(struct device *dev, const char *string)
|
|
|
|
{
|
|
|
|
struct phy **ptr, *phy;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
phy = phy_get(dev, string);
|
|
|
|
if (!IS_ERR(phy)) {
|
|
|
|
*ptr = phy;
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
} else {
|
|
|
|
devres_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_phy_get);
|
|
|
|
|
2014-02-05 00:33:12 +07:00
|
|
|
/**
|
|
|
|
* devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
|
|
|
|
* @dev: device that requests this phy
|
|
|
|
* @string: the phy name as given in the dt data or phy device name
|
|
|
|
* for non-dt case
|
|
|
|
*
|
|
|
|
* Gets the phy using phy_get(), and associates a device with it using
|
|
|
|
* devres. On driver detach, release function is invoked on the devres
|
|
|
|
* data, then, devres data is freed. This differs to devm_phy_get() in
|
|
|
|
* that if the phy does not exist, it is not considered an error and
|
|
|
|
* -ENODEV will not be returned. Instead the NULL phy is returned,
|
|
|
|
* which can be passed to all other phy consumer calls.
|
|
|
|
*/
|
|
|
|
struct phy *devm_phy_optional_get(struct device *dev, const char *string)
|
|
|
|
{
|
|
|
|
struct phy *phy = devm_phy_get(dev, string);
|
|
|
|
|
2015-04-08 06:30:15 +07:00
|
|
|
if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
|
2014-02-05 00:33:12 +07:00
|
|
|
phy = NULL;
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_phy_optional_get);
|
|
|
|
|
2014-03-06 18:16:47 +07:00
|
|
|
/**
|
|
|
|
* devm_of_phy_get() - lookup and obtain a reference to a phy.
|
|
|
|
* @dev: device that requests this phy
|
|
|
|
* @np: node containing the phy
|
|
|
|
* @con_id: name of the phy from device's point of view
|
|
|
|
*
|
|
|
|
* Gets the phy using of_phy_get(), and associates a device with it using
|
|
|
|
* devres. On driver detach, release function is invoked on the devres data,
|
|
|
|
* then, devres data is freed.
|
|
|
|
*/
|
|
|
|
struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
|
|
|
|
const char *con_id)
|
|
|
|
{
|
|
|
|
struct phy **ptr, *phy;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
phy = of_phy_get(np, con_id);
|
|
|
|
if (!IS_ERR(phy)) {
|
|
|
|
*ptr = phy;
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
} else {
|
|
|
|
devres_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_of_phy_get);
|
|
|
|
|
2015-04-23 06:04:11 +07:00
|
|
|
/**
|
|
|
|
* devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
|
|
|
|
* @dev: device that requests this phy
|
|
|
|
* @np: node containing the phy
|
|
|
|
* @index: index of the phy
|
|
|
|
*
|
2015-12-04 09:06:29 +07:00
|
|
|
* Gets the phy using _of_phy_get(), then gets a refcount to it,
|
|
|
|
* and associates a device with it using devres. On driver detach,
|
|
|
|
* release function is invoked on the devres data,
|
2015-04-23 06:04:11 +07:00
|
|
|
* then, devres data is freed.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
|
|
|
|
int index)
|
|
|
|
{
|
|
|
|
struct phy **ptr, *phy;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
phy = _of_phy_get(np, index);
|
2015-12-04 09:06:29 +07:00
|
|
|
if (IS_ERR(phy)) {
|
2015-04-23 06:04:11 +07:00
|
|
|
devres_free(ptr);
|
2015-12-04 09:06:29 +07:00
|
|
|
return phy;
|
2015-04-23 06:04:11 +07:00
|
|
|
}
|
|
|
|
|
2015-12-04 09:06:29 +07:00
|
|
|
if (!try_module_get(phy->ops->owner)) {
|
|
|
|
devres_free(ptr);
|
|
|
|
return ERR_PTR(-EPROBE_DEFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
get_device(&phy->dev);
|
|
|
|
|
|
|
|
*ptr = phy;
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
|
2015-04-23 06:04:11 +07:00
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
/**
|
|
|
|
* phy_create() - create a new phy
|
|
|
|
* @dev: device that is creating the new phy
|
2014-07-14 17:25:02 +07:00
|
|
|
* @node: device node of the phy
|
2013-09-27 13:23:25 +07:00
|
|
|
* @ops: function pointers for performing phy operations
|
|
|
|
*
|
|
|
|
* Called to create a phy using phy framework.
|
|
|
|
*/
|
2014-07-14 17:25:02 +07:00
|
|
|
struct phy *phy_create(struct device *dev, struct device_node *node,
|
2014-11-19 22:28:21 +07:00
|
|
|
const struct phy_ops *ops)
|
2013-09-27 13:23:25 +07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int id;
|
|
|
|
struct phy *phy;
|
|
|
|
|
2013-12-06 19:21:19 +07:00
|
|
|
if (WARN_ON(!dev))
|
|
|
|
return ERR_PTR(-EINVAL);
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
phy = kzalloc(sizeof(*phy), GFP_KERNEL);
|
2013-12-06 19:21:19 +07:00
|
|
|
if (!phy)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
|
|
|
|
if (id < 0) {
|
|
|
|
dev_err(dev, "unable to get id\n");
|
|
|
|
ret = id;
|
2013-12-06 19:21:19 +07:00
|
|
|
goto free_phy;
|
2013-09-27 13:23:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
device_initialize(&phy->dev);
|
|
|
|
mutex_init(&phy->mutex);
|
|
|
|
|
|
|
|
phy->dev.class = phy_class;
|
|
|
|
phy->dev.parent = dev;
|
2014-07-14 17:25:02 +07:00
|
|
|
phy->dev.of_node = node ?: dev->of_node;
|
2013-09-27 13:23:25 +07:00
|
|
|
phy->id = id;
|
|
|
|
phy->ops = ops;
|
|
|
|
|
|
|
|
ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
|
|
|
|
if (ret)
|
2013-12-06 19:21:19 +07:00
|
|
|
goto put_dev;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
2015-04-23 06:14:37 +07:00
|
|
|
/* phy-supply */
|
|
|
|
phy->pwr = regulator_get_optional(&phy->dev, "phy");
|
|
|
|
if (IS_ERR(phy->pwr)) {
|
|
|
|
ret = PTR_ERR(phy->pwr);
|
|
|
|
if (ret == -EPROBE_DEFER)
|
|
|
|
goto put_dev;
|
|
|
|
|
|
|
|
phy->pwr = NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
ret = device_add(&phy->dev);
|
|
|
|
if (ret)
|
2013-12-06 19:21:19 +07:00
|
|
|
goto put_dev;
|
2013-09-27 13:23:25 +07:00
|
|
|
|
|
|
|
if (pm_runtime_enabled(dev)) {
|
|
|
|
pm_runtime_enable(&phy->dev);
|
|
|
|
pm_runtime_no_callbacks(&phy->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
|
2013-12-06 19:21:19 +07:00
|
|
|
put_dev:
|
2014-07-10 13:25:02 +07:00
|
|
|
put_device(&phy->dev); /* calls phy_release() which frees resources */
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
2013-12-06 19:21:19 +07:00
|
|
|
free_phy:
|
2013-09-27 13:23:25 +07:00
|
|
|
kfree(phy);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_create);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_phy_create() - create a new phy
|
|
|
|
* @dev: device that is creating the new phy
|
2014-07-14 17:25:02 +07:00
|
|
|
* @node: device node of the phy
|
2013-09-27 13:23:25 +07:00
|
|
|
* @ops: function pointers for performing phy operations
|
|
|
|
*
|
|
|
|
* Creates a new PHY device adding it to the PHY class.
|
|
|
|
* While at that, it also associates the device with the phy using devres.
|
|
|
|
* On driver detach, release function is invoked on the devres data,
|
|
|
|
* then, devres data is freed.
|
|
|
|
*/
|
2014-07-14 17:25:02 +07:00
|
|
|
struct phy *devm_phy_create(struct device *dev, struct device_node *node,
|
2014-11-19 22:28:21 +07:00
|
|
|
const struct phy_ops *ops)
|
2013-09-27 13:23:25 +07:00
|
|
|
{
|
|
|
|
struct phy **ptr, *phy;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2014-11-19 22:28:21 +07:00
|
|
|
phy = phy_create(dev, node, ops);
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!IS_ERR(phy)) {
|
|
|
|
*ptr = phy;
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
} else {
|
|
|
|
devres_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_phy_create);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_destroy() - destroy the phy
|
|
|
|
* @phy: the phy to be destroyed
|
|
|
|
*
|
|
|
|
* Called to destroy the phy.
|
|
|
|
*/
|
|
|
|
void phy_destroy(struct phy *phy)
|
|
|
|
{
|
|
|
|
pm_runtime_disable(&phy->dev);
|
|
|
|
device_unregister(&phy->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(phy_destroy);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_phy_destroy() - destroy the PHY
|
|
|
|
* @dev: device that wants to release this phy
|
|
|
|
* @phy: the phy returned by devm_phy_get()
|
|
|
|
*
|
|
|
|
* destroys the devres associated with this phy and invokes phy_destroy
|
|
|
|
* to destroy the phy.
|
|
|
|
*/
|
|
|
|
void devm_phy_destroy(struct device *dev, struct phy *phy)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
|
|
|
|
dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_phy_destroy);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __of_phy_provider_register() - create/register phy provider with the framework
|
|
|
|
* @dev: struct device of the phy provider
|
2016-04-05 22:17:34 +07:00
|
|
|
* @children: device node containing children (if different from dev->of_node)
|
2013-09-27 13:23:25 +07:00
|
|
|
* @owner: the module owner containing of_xlate
|
|
|
|
* @of_xlate: function pointer to obtain phy instance from phy provider
|
|
|
|
*
|
|
|
|
* Creates struct phy_provider from dev and of_xlate function pointer.
|
|
|
|
* This is used in the case of dt boot for finding the phy instance from
|
|
|
|
* phy provider.
|
2016-04-05 22:17:34 +07:00
|
|
|
*
|
|
|
|
* If the PHY provider doesn't nest children directly but uses a separate
|
|
|
|
* child node to contain the individual children, the @children parameter
|
|
|
|
* can be used to override the default. If NULL, the default (dev->of_node)
|
|
|
|
* will be used. If non-NULL, the device node must be a child (or further
|
|
|
|
* descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
|
|
|
|
* error code is returned.
|
2013-09-27 13:23:25 +07:00
|
|
|
*/
|
|
|
|
struct phy_provider *__of_phy_provider_register(struct device *dev,
|
2016-04-05 22:17:34 +07:00
|
|
|
struct device_node *children, struct module *owner,
|
|
|
|
struct phy * (*of_xlate)(struct device *dev,
|
|
|
|
struct of_phandle_args *args))
|
2013-09-27 13:23:25 +07:00
|
|
|
{
|
|
|
|
struct phy_provider *phy_provider;
|
|
|
|
|
2016-04-05 22:17:34 +07:00
|
|
|
/*
|
|
|
|
* If specified, the device node containing the children must itself
|
|
|
|
* be the provider's device node or a child (or further descendant)
|
|
|
|
* thereof.
|
|
|
|
*/
|
|
|
|
if (children) {
|
|
|
|
struct device_node *parent = of_node_get(children), *next;
|
|
|
|
|
|
|
|
while (parent) {
|
|
|
|
if (parent == dev->of_node)
|
|
|
|
break;
|
|
|
|
|
|
|
|
next = of_get_parent(parent);
|
|
|
|
of_node_put(parent);
|
|
|
|
parent = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
of_node_put(parent);
|
|
|
|
} else {
|
|
|
|
children = dev->of_node;
|
|
|
|
}
|
|
|
|
|
2013-09-27 13:23:25 +07:00
|
|
|
phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
|
|
|
|
if (!phy_provider)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
phy_provider->dev = dev;
|
2016-04-05 22:17:34 +07:00
|
|
|
phy_provider->children = of_node_get(children);
|
2013-09-27 13:23:25 +07:00
|
|
|
phy_provider->owner = owner;
|
|
|
|
phy_provider->of_xlate = of_xlate;
|
|
|
|
|
|
|
|
mutex_lock(&phy_provider_mutex);
|
|
|
|
list_add_tail(&phy_provider->list, &phy_provider_list);
|
|
|
|
mutex_unlock(&phy_provider_mutex);
|
|
|
|
|
|
|
|
return phy_provider;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__of_phy_provider_register);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __devm_of_phy_provider_register() - create/register phy provider with the
|
|
|
|
* framework
|
|
|
|
* @dev: struct device of the phy provider
|
|
|
|
* @owner: the module owner containing of_xlate
|
|
|
|
* @of_xlate: function pointer to obtain phy instance from phy provider
|
|
|
|
*
|
|
|
|
* Creates struct phy_provider from dev and of_xlate function pointer.
|
|
|
|
* This is used in the case of dt boot for finding the phy instance from
|
|
|
|
* phy provider. While at that, it also associates the device with the
|
|
|
|
* phy provider using devres. On driver detach, release function is invoked
|
|
|
|
* on the devres data, then, devres data is freed.
|
|
|
|
*/
|
|
|
|
struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
|
2016-04-05 22:17:34 +07:00
|
|
|
struct device_node *children, struct module *owner,
|
|
|
|
struct phy * (*of_xlate)(struct device *dev,
|
|
|
|
struct of_phandle_args *args))
|
2013-09-27 13:23:25 +07:00
|
|
|
{
|
|
|
|
struct phy_provider **ptr, *phy_provider;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2016-04-05 22:17:34 +07:00
|
|
|
phy_provider = __of_phy_provider_register(dev, children, owner,
|
|
|
|
of_xlate);
|
2013-09-27 13:23:25 +07:00
|
|
|
if (!IS_ERR(phy_provider)) {
|
|
|
|
*ptr = phy_provider;
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
} else {
|
|
|
|
devres_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return phy_provider;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_phy_provider_unregister() - unregister phy provider from the framework
|
|
|
|
* @phy_provider: phy provider returned by of_phy_provider_register()
|
|
|
|
*
|
|
|
|
* Removes the phy_provider created using of_phy_provider_register().
|
|
|
|
*/
|
|
|
|
void of_phy_provider_unregister(struct phy_provider *phy_provider)
|
|
|
|
{
|
|
|
|
if (IS_ERR(phy_provider))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&phy_provider_mutex);
|
|
|
|
list_del(&phy_provider->list);
|
2016-04-05 22:17:34 +07:00
|
|
|
of_node_put(phy_provider->children);
|
2013-09-27 13:23:25 +07:00
|
|
|
kfree(phy_provider);
|
|
|
|
mutex_unlock(&phy_provider_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* devm_of_phy_provider_unregister() - remove phy provider from the framework
|
|
|
|
* @dev: struct device of the phy provider
|
|
|
|
*
|
|
|
|
* destroys the devres associated with this phy provider and invokes
|
|
|
|
* of_phy_provider_unregister to unregister the phy provider.
|
|
|
|
*/
|
|
|
|
void devm_of_phy_provider_unregister(struct device *dev,
|
|
|
|
struct phy_provider *phy_provider) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
|
|
|
|
phy_provider);
|
|
|
|
dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* phy_release() - release the phy
|
|
|
|
* @dev: the dev member within phy
|
|
|
|
*
|
|
|
|
* When the last reference to the device is removed, it is called
|
|
|
|
* from the embedded kobject as release method.
|
|
|
|
*/
|
|
|
|
static void phy_release(struct device *dev)
|
|
|
|
{
|
|
|
|
struct phy *phy;
|
|
|
|
|
|
|
|
phy = to_phy(dev);
|
|
|
|
dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
|
2014-07-04 16:55:45 +07:00
|
|
|
regulator_put(phy->pwr);
|
2014-07-10 13:25:02 +07:00
|
|
|
ida_simple_remove(&phy_ida, phy->id);
|
2013-09-27 13:23:25 +07:00
|
|
|
kfree(phy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init phy_core_init(void)
|
|
|
|
{
|
|
|
|
phy_class = class_create(THIS_MODULE, "phy");
|
|
|
|
if (IS_ERR(phy_class)) {
|
|
|
|
pr_err("failed to create phy class --> %ld\n",
|
|
|
|
PTR_ERR(phy_class));
|
|
|
|
return PTR_ERR(phy_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
phy_class->dev_release = phy_release;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-10 07:34:48 +07:00
|
|
|
device_initcall(phy_core_init);
|