1312 lines · 31.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * phy-core.c  --  Generic Phy framework. * * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com * * Author: Kishon Vijay Abraham I <kishon@ti.com> */ #include <linux/kernel.h>#include <linux/export.h>#include <linux/module.h>#include <linux/err.h>#include <linux/debugfs.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>#include <linux/regulator/consumer.h> static void phy_release(struct device *dev);static const struct class phy_class = {	.name = "phy",	.dev_release = phy_release,}; static struct dentry *phy_debugfs_root;static DEFINE_MUTEX(phy_provider_mutex);static LIST_HEAD(phy_provider_list);static LIST_HEAD(phys);static DEFINE_IDA(phy_ida); static void devm_phy_release(struct device *dev, void *res){	struct phy *phy = *(struct phy **)res; 	phy_put(dev, 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){	struct phy **phy = res; 	return *phy == match_data;} /** * 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_obj(*pl);	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); 	return pl ? pl->phy : ERR_PTR(-ENODEV);} static struct phy_provider *of_phy_provider_lookup(struct device_node *node){	struct phy_provider *phy_provider; 	list_for_each_entry(phy_provider, &phy_provider_list, list) {		if (phy_provider->dev->of_node == node)			return phy_provider; 		for_each_child_of_node_scoped(phy_provider->children, child)			if (child == node)				return phy_provider;	} 	return ERR_PTR(-EPROBE_DEFER);} int phy_pm_runtime_get(struct phy *phy){	int ret; 	if (!phy)		return 0; 	if (!pm_runtime_enabled(&phy->dev))		return -ENOTSUPP; 	ret = pm_runtime_get(&phy->dev);	if (ret < 0 && ret != -EINPROGRESS)		pm_runtime_put_noidle(&phy->dev); 	return ret;}EXPORT_SYMBOL_GPL(phy_pm_runtime_get); int phy_pm_runtime_get_sync(struct phy *phy){	int ret; 	if (!phy)		return 0; 	if (!pm_runtime_enabled(&phy->dev))		return -ENOTSUPP; 	ret = pm_runtime_get_sync(&phy->dev);	if (ret < 0)		pm_runtime_put_sync(&phy->dev); 	return ret;}EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync); void phy_pm_runtime_put(struct phy *phy){	if (!phy)		return; 	if (!pm_runtime_enabled(&phy->dev))		return; 	pm_runtime_put(&phy->dev);}EXPORT_SYMBOL_GPL(phy_pm_runtime_put); int phy_pm_runtime_put_sync(struct phy *phy){	if (!phy)		return 0; 	if (!pm_runtime_enabled(&phy->dev))		return -ENOTSUPP; 	return pm_runtime_put_sync(&phy->dev);}EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync); /** * phy_init - phy internal initialization before phy operation * @phy: the phy returned by phy_get() * * Used to allow phy's driver to perform phy internal initialization, * such as PLL block powering, clock initialization or anything that's * is required by the phy to perform the start of operation. * Must be called before phy_power_on(). * * Return: %0 if successful, a negative error code otherwise */int phy_init(struct phy *phy){	int ret; 	if (!phy)		return 0; 	ret = phy_pm_runtime_get_sync(phy);	if (ret < 0 && ret != -ENOTSUPP)		return ret;	ret = 0; /* Override possible ret == -ENOTSUPP */ 	mutex_lock(&phy->mutex);	if (phy->power_count > phy->init_count)		dev_warn(&phy->dev, "phy_power_on was called before phy_init\n"); 	if (phy->init_count == 0 && phy->ops->init) {		ret = phy->ops->init(phy);		if (ret < 0) {			dev_err(&phy->dev, "phy init failed --> %d\n", ret);			goto out;		}	}	++phy->init_count; out:	mutex_unlock(&phy->mutex);	phy_pm_runtime_put(phy);	return ret;}EXPORT_SYMBOL_GPL(phy_init); /** * phy_exit - Phy internal un-initialization * @phy: the phy returned by phy_get() * * Must be called after phy_power_off(). * * Return: %0 if successful, a negative error code otherwise */int phy_exit(struct phy *phy){	int ret; 	if (!phy)		return 0; 	ret = phy_pm_runtime_get_sync(phy);	if (ret < 0 && ret != -ENOTSUPP)		return ret;	ret = 0; /* Override possible ret == -ENOTSUPP */ 	mutex_lock(&phy->mutex);	if (phy->init_count == 1 && phy->ops->exit) {		ret = phy->ops->exit(phy);		if (ret < 0) {			dev_err(&phy->dev, "phy exit failed --> %d\n", ret);			goto out;		}	}	--phy->init_count; out:	mutex_unlock(&phy->mutex);	phy_pm_runtime_put(phy);	return ret;}EXPORT_SYMBOL_GPL(phy_exit); /** * phy_power_on - Enable the phy and enter proper operation * @phy: the phy returned by phy_get() * * Must be called after phy_init(). * * Return: %0 if successful, a negative error code otherwise */int phy_power_on(struct phy *phy){	int ret = 0; 	if (!phy)		goto out; 	if (phy->pwr) {		ret = regulator_enable(phy->pwr);		if (ret)			goto out;	} 	ret = phy_pm_runtime_get_sync(phy);	if (ret < 0 && ret != -ENOTSUPP)		goto err_pm_sync; 	ret = 0; /* Override possible ret == -ENOTSUPP */ 	mutex_lock(&phy->mutex);	if (phy->power_count == 0 && phy->ops->power_on) {		ret = phy->ops->power_on(phy);		if (ret < 0) {			dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);			goto err_pwr_on;		}	}	++phy->power_count;	mutex_unlock(&phy->mutex);	return 0; err_pwr_on:	mutex_unlock(&phy->mutex);	phy_pm_runtime_put_sync(phy);err_pm_sync:	if (phy->pwr)		regulator_disable(phy->pwr);out:	return ret;}EXPORT_SYMBOL_GPL(phy_power_on); /** * phy_power_off - Disable the phy. * @phy: the phy returned by phy_get() * * Must be called before phy_exit(). * * Return: %0 if successful, a negative error code otherwise */int phy_power_off(struct phy *phy){	int ret; 	if (!phy)		return 0; 	mutex_lock(&phy->mutex);	if (phy->power_count == 1 && phy->ops->power_off) {		ret = phy->ops->power_off(phy);		if (ret < 0) {			dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);			mutex_unlock(&phy->mutex);			return ret;		}	}	--phy->power_count;	mutex_unlock(&phy->mutex);	phy_pm_runtime_put(phy); 	if (phy->pwr)		regulator_disable(phy->pwr); 	return 0;}EXPORT_SYMBOL_GPL(phy_power_off); int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode){	int ret = 0; 	if (!phy)		return 0; 	mutex_lock(&phy->mutex);	if (phy->ops->set_mode)		ret = phy->ops->set_mode(phy, mode, submode);	if (!ret)		phy->attrs.mode = mode;	mutex_unlock(&phy->mutex); 	return ret;}EXPORT_SYMBOL_GPL(phy_set_mode_ext); int phy_set_media(struct phy *phy, enum phy_media media){	int ret; 	if (!phy || !phy->ops->set_media)		return 0; 	mutex_lock(&phy->mutex);	ret = phy->ops->set_media(phy, media);	mutex_unlock(&phy->mutex); 	return ret;}EXPORT_SYMBOL_GPL(phy_set_media); int phy_set_speed(struct phy *phy, int speed){	int ret; 	if (!phy || !phy->ops->set_speed)		return 0; 	mutex_lock(&phy->mutex);	ret = phy->ops->set_speed(phy, speed);	mutex_unlock(&phy->mutex); 	return ret;}EXPORT_SYMBOL_GPL(phy_set_speed); int phy_reset(struct phy *phy){	int ret; 	if (!phy || !phy->ops->reset)		return 0; 	ret = phy_pm_runtime_get_sync(phy);	if (ret < 0 && ret != -ENOTSUPP)		return ret; 	mutex_lock(&phy->mutex);	ret = phy->ops->reset(phy);	mutex_unlock(&phy->mutex); 	phy_pm_runtime_put(phy); 	return ret;}EXPORT_SYMBOL_GPL(phy_reset); /** * phy_calibrate() - Tunes the phy hw parameters for current configuration * @phy: the phy returned by phy_get() * * Used to calibrate phy hardware, typically by adjusting some parameters in * runtime, which are otherwise lost after host controller reset and cannot * be applied in phy_init() or phy_power_on(). * * Return: %0 if successful, a negative error code otherwise */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_notify_connect() - phy connect notification * @phy: the phy returned by phy_get() * @port: the port index for connect * * If the phy needs to get connection status, the callback can be used. * Returns: %0 if successful, a negative error code otherwise */int phy_notify_connect(struct phy *phy, int port){	int ret; 	if (!phy || !phy->ops->connect)		return 0; 	mutex_lock(&phy->mutex);	ret = phy->ops->connect(phy, port);	mutex_unlock(&phy->mutex); 	return ret;}EXPORT_SYMBOL_GPL(phy_notify_connect); /** * phy_notify_disconnect() - phy disconnect notification * @phy: the phy returned by phy_get() * @port: the port index for disconnect *

Lines 1–500 of 1312.