Something went wrong. Try again.
Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313// 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 * * 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_disconnect(struct phy *phy, int port){ int ret;
if (!phy || !phy->ops->disconnect) return 0;
mutex_lock(&phy->mutex); ret = phy->ops->disconnect(phy, port); mutex_unlock(&phy->mutex);
return ret;}EXPORT_SYMBOL_GPL(phy_notify_disconnect);
/** * phy_notify_state() - phy state notification * @phy: the PHY returned by phy_get() * @state: the PHY state * * Notify the PHY of a state transition. Used to notify and * configure the PHY accordingly. * * Returns: %0 if successful, a negative error code otherwise */int phy_notify_state(struct phy *phy, union phy_notify state){ int ret;
if (!phy || !phy->ops->notify_phystate) return 0;
mutex_lock(&phy->mutex); ret = phy->ops->notify_phystate(phy, state); mutex_unlock(&phy->mutex);
return ret;}EXPORT_SYMBOL_GPL(phy_notify_state);
/** * 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(). * * Return: %0 if successful, a 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. * * Return: %0 if successful, a 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);
/** * _of_phy_get() - lookup and obtain a reference to a phy by phandle * @np: device_node for which to get the phy * @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. */static struct phy *_of_phy_get(struct device_node *np, int index){ int ret; struct phy_provider *phy_provider; struct phy *phy = NULL; struct of_phandle_args args;
ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", index, &args); if (ret) return ERR_PTR(-ENODEV);
/* This phy type handled by the usb-phy subsystem for now */ if (of_device_is_compatible(args.np, "usb-nop-xceiv")) { phy = ERR_PTR(-ENODEV); goto out_put_node; }
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); 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; }
phy = phy_provider->of_xlate(phy_provider->dev, &args);
out_put_module: module_put(phy_provider->owner);
out_unlock: mutex_unlock(&phy_provider_mutex);out_put_node: of_node_put(args.np);
return phy;}
/** * 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 of_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);
/** * of_phy_put() - release the PHY * @phy: the phy returned by of_phy_get() * * Releases a refcount the caller received from of_phy_get(). */void of_phy_put(struct phy *phy){ if (!phy || IS_ERR(phy)) return;
mutex_lock(&phy->mutex); if (phy->ops->release) phy->ops->release(phy); mutex_unlock(&phy->mutex);
module_put(phy->ops->owner); put_device(&phy->dev);}EXPORT_SYMBOL_GPL(of_phy_put);
/** * phy_put() - release the PHY * @dev: device that wants to release this phy * @phy: the phy returned by phy_get() * * Releases a refcount the caller received from phy_get(). */void phy_put(struct device *dev, struct phy *phy){ device_link_remove(dev, &phy->dev); of_phy_put(phy);}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;
if (!phy) return;
r = devres_release(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 (not used here) * @args: of_phandle_args * * 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, const struct of_phandle_args *args){ struct device *target_dev;
target_dev = class_find_device_by_of_node(&phy_class, args->np); if (!target_dev) return ERR_PTR(-ENODEV);
put_device(target_dev); return to_phy(target_dev);}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; struct phy *phy; struct device_link *link;
if (dev->of_node) { if (string) index = of_property_match_string(dev->of_node, "phy-names", string); else index = 0; phy = _of_phy_get(dev->of_node, index); } else { if (string == NULL) { dev_WARN(dev, "missing string\n"); return ERR_PTR(-EINVAL); } phy = phy_find(dev, string); } if (IS_ERR(phy)) return phy;
if (!try_module_get(phy->ops->owner)) return ERR_PTR(-EPROBE_DEFER);
get_device(&phy->dev);
link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); if (!link) dev_dbg(dev, "failed to create device link to %s\n", dev_name(phy->dev.parent));
return phy;}EXPORT_SYMBOL_GPL(phy_get);
/** * 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);
/** * 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);
if (PTR_ERR(phy) == -ENODEV) phy = NULL;
return phy;}EXPORT_SYMBOL_GPL(devm_phy_optional_get);
/** * 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; struct device_link *link;
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; }
link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); if (!link) dev_dbg(dev, "failed to create device link to %s\n", dev_name(phy->dev.parent));
return phy;}EXPORT_SYMBOL_GPL(devm_of_phy_get);
/** * devm_of_phy_optional_get() - lookup and obtain a reference to an optional * 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. This differs to devm_of_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_of_phy_optional_get(struct device *dev, struct device_node *np, const char *con_id){ struct phy *phy = devm_of_phy_get(dev, np, con_id);
if (PTR_ERR(phy) == -ENODEV) phy = NULL;
if (IS_ERR(phy)) dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s", np, con_id);
return phy;}EXPORT_SYMBOL_GPL(devm_of_phy_optional_get);
/** * 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 * * 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, * 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; struct device_link *link;
ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) return ERR_PTR(-ENOMEM);
phy = _of_phy_get(np, index); if (IS_ERR(phy)) { devres_free(ptr); return phy; }
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);
link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); if (!link) dev_dbg(dev, "failed to create device link to %s\n", dev_name(phy->dev.parent));
return phy;}EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
/** * phy_create() - create a new phy * @dev: device that is creating the new phy * @node: device node of the phy * @ops: function pointers for performing phy operations * * Called to create a phy using phy framework. */struct phy *phy_create(struct device *dev, struct device_node *node, const struct phy_ops *ops){ int ret; int id; struct phy *phy;
if (WARN_ON(!dev)) return ERR_PTR(-EINVAL);
phy = kzalloc_obj(*phy); if (!phy) return ERR_PTR(-ENOMEM);
id = ida_alloc(&phy_ida, GFP_KERNEL); if (id < 0) { dev_err(dev, "unable to get id\n"); ret = id; goto free_phy; }
device_initialize(&phy->dev); lockdep_register_key(&phy->lockdep_key); mutex_init_with_key(&phy->mutex, &phy->lockdep_key);
phy->dev.class = &phy_class; phy->dev.parent = dev; phy->dev.of_node = node ?: dev->of_node; phy->id = id; phy->ops = ops;
ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); if (ret) goto put_dev;
/* 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; }
ret = device_add(&phy->dev); if (ret) goto put_dev;
if (pm_runtime_enabled(dev)) { pm_runtime_enable(&phy->dev); pm_runtime_no_callbacks(&phy->dev); }
phy->debugfs = debugfs_create_dir(dev_name(&phy->dev), phy_debugfs_root);
return phy;
put_dev: put_device(&phy->dev); /* calls phy_release() which frees resources */ return ERR_PTR(ret);
free_phy: 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 * @node: device node of the phy * @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. */struct phy *devm_phy_create(struct device *dev, struct device_node *node, const struct phy_ops *ops){ struct phy **ptr, *phy;
ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); if (!ptr) return ERR_PTR(-ENOMEM);
phy = phy_create(dev, node, ops); 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_release(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 * @children: device node containing children (if different from dev->of_node) * @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. * * 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. */struct phy_provider *__of_phy_provider_register(struct device *dev, struct device_node *children, struct module *owner, struct phy * (*of_xlate)(struct device *dev, const struct of_phandle_args *args)){ struct phy_provider *phy_provider;
/* * 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; }
phy_provider = kzalloc_obj(*phy_provider); if (!phy_provider) return ERR_PTR(-ENOMEM);
phy_provider->dev = dev; phy_provider->children = of_node_get(children); 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 * @children: device node containing children (if different from dev->of_node) * @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, struct device_node *children, struct module *owner, struct phy * (*of_xlate)(struct device *dev, const struct of_phandle_args *args)){ struct phy_provider **ptr, *phy_provider;
ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) return ERR_PTR(-ENOMEM);
phy_provider = __of_phy_provider_register(dev, children, owner, of_xlate); 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); of_node_put(phy_provider->children); 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 * @phy_provider: phy provider returned by of_phy_provider_register() * * 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_release(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)); debugfs_remove_recursive(phy->debugfs); regulator_put(phy->pwr); mutex_destroy(&phy->mutex); lockdep_unregister_key(&phy->lockdep_key); ida_free(&phy_ida, phy->id); kfree(phy);}
static int __init phy_core_init(void){ int err;
err = class_register(&phy_class); if (err) { pr_err("failed to register phy class"); return err; }
phy_debugfs_root = debugfs_create_dir("phy", NULL);
return 0;}device_initcall(phy_core_init);
static void __exit phy_core_exit(void){ debugfs_remove_recursive(phy_debugfs_root); class_unregister(&phy_class);}module_exit(phy_core_exit);