12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ff764963SKishon Vijay Abraham I /* 3ff764963SKishon Vijay Abraham I * phy-core.c -- Generic Phy framework. 4ff764963SKishon Vijay Abraham I * 5ff764963SKishon Vijay Abraham I * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com 6ff764963SKishon Vijay Abraham I * 7ff764963SKishon Vijay Abraham I * Author: Kishon Vijay Abraham I <kishon@ti.com> 8ff764963SKishon Vijay Abraham I */ 9ff764963SKishon Vijay Abraham I 10ff764963SKishon Vijay Abraham I #include <linux/kernel.h> 11ff764963SKishon Vijay Abraham I #include <linux/export.h> 12ff764963SKishon Vijay Abraham I #include <linux/module.h> 13ff764963SKishon Vijay Abraham I #include <linux/err.h> 1491694772SChunfeng Yun #include <linux/debugfs.h> 15ff764963SKishon Vijay Abraham I #include <linux/device.h> 16ff764963SKishon Vijay Abraham I #include <linux/slab.h> 17ff764963SKishon Vijay Abraham I #include <linux/of.h> 18ff764963SKishon Vijay Abraham I #include <linux/phy/phy.h> 19ff764963SKishon Vijay Abraham I #include <linux/idr.h> 20ff764963SKishon Vijay Abraham I #include <linux/pm_runtime.h> 213be88125SRoger Quadros #include <linux/regulator/consumer.h> 22ff764963SKishon Vijay Abraham I 23ff764963SKishon Vijay Abraham I static struct class *phy_class; 2491694772SChunfeng Yun static struct dentry *phy_debugfs_root; 25ff764963SKishon Vijay Abraham I static DEFINE_MUTEX(phy_provider_mutex); 26ff764963SKishon Vijay Abraham I static LIST_HEAD(phy_provider_list); 27b7bc15b9SHeikki Krogerus static LIST_HEAD(phys); 28ff764963SKishon Vijay Abraham I static DEFINE_IDA(phy_ida); 29ff764963SKishon Vijay Abraham I 30ff764963SKishon Vijay Abraham I static void devm_phy_release(struct device *dev, void *res) 31ff764963SKishon Vijay Abraham I { 32ff764963SKishon Vijay Abraham I struct phy *phy = *(struct phy **)res; 33ff764963SKishon Vijay Abraham I 34987351e1SAlexandre Torgue phy_put(dev, phy); 35ff764963SKishon Vijay Abraham I } 36ff764963SKishon Vijay Abraham I 37ff764963SKishon Vijay Abraham I static void devm_phy_provider_release(struct device *dev, void *res) 38ff764963SKishon Vijay Abraham I { 39ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider = *(struct phy_provider **)res; 40ff764963SKishon Vijay Abraham I 41ff764963SKishon Vijay Abraham I of_phy_provider_unregister(phy_provider); 42ff764963SKishon Vijay Abraham I } 43ff764963SKishon Vijay Abraham I 44ff764963SKishon Vijay Abraham I static void devm_phy_consume(struct device *dev, void *res) 45ff764963SKishon Vijay Abraham I { 46ff764963SKishon Vijay Abraham I struct phy *phy = *(struct phy **)res; 47ff764963SKishon Vijay Abraham I 48ff764963SKishon Vijay Abraham I phy_destroy(phy); 49ff764963SKishon Vijay Abraham I } 50ff764963SKishon Vijay Abraham I 51ff764963SKishon Vijay Abraham I static int devm_phy_match(struct device *dev, void *res, void *match_data) 52ff764963SKishon Vijay Abraham I { 532f1bce48SThierry Reding struct phy **phy = res; 542f1bce48SThierry Reding 552f1bce48SThierry Reding return *phy == match_data; 56ff764963SKishon Vijay Abraham I } 57ff764963SKishon Vijay Abraham I 58b7bc15b9SHeikki Krogerus /** 59b7bc15b9SHeikki Krogerus * phy_create_lookup() - allocate and register PHY/device association 60b7bc15b9SHeikki Krogerus * @phy: the phy of the association 61b7bc15b9SHeikki Krogerus * @con_id: connection ID string on device 62b7bc15b9SHeikki Krogerus * @dev_id: the device of the association 63b7bc15b9SHeikki Krogerus * 64b7bc15b9SHeikki Krogerus * Creates and registers phy_lookup entry. 65b7bc15b9SHeikki Krogerus */ 66b7bc15b9SHeikki Krogerus int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) 67b7bc15b9SHeikki Krogerus { 68b7bc15b9SHeikki Krogerus struct phy_lookup *pl; 69b7bc15b9SHeikki Krogerus 70b7bc15b9SHeikki Krogerus if (!phy || !dev_id || !con_id) 71b7bc15b9SHeikki Krogerus return -EINVAL; 72b7bc15b9SHeikki Krogerus 73b7bc15b9SHeikki Krogerus pl = kzalloc(sizeof(*pl), GFP_KERNEL); 74b7bc15b9SHeikki Krogerus if (!pl) 75b7bc15b9SHeikki Krogerus return -ENOMEM; 76b7bc15b9SHeikki Krogerus 77b7bc15b9SHeikki Krogerus pl->dev_id = dev_id; 78b7bc15b9SHeikki Krogerus pl->con_id = con_id; 79b7bc15b9SHeikki Krogerus pl->phy = phy; 80b7bc15b9SHeikki Krogerus 81b7bc15b9SHeikki Krogerus mutex_lock(&phy_provider_mutex); 82b7bc15b9SHeikki Krogerus list_add_tail(&pl->node, &phys); 83b7bc15b9SHeikki Krogerus mutex_unlock(&phy_provider_mutex); 84b7bc15b9SHeikki Krogerus 85b7bc15b9SHeikki Krogerus return 0; 86b7bc15b9SHeikki Krogerus } 87b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_create_lookup); 88b7bc15b9SHeikki Krogerus 89b7bc15b9SHeikki Krogerus /** 90b7bc15b9SHeikki Krogerus * phy_remove_lookup() - find and remove PHY/device association 91b7bc15b9SHeikki Krogerus * @phy: the phy of the association 92b7bc15b9SHeikki Krogerus * @con_id: connection ID string on device 93b7bc15b9SHeikki Krogerus * @dev_id: the device of the association 94b7bc15b9SHeikki Krogerus * 95b7bc15b9SHeikki Krogerus * Finds and unregisters phy_lookup entry that was created with 96b7bc15b9SHeikki Krogerus * phy_create_lookup(). 97b7bc15b9SHeikki Krogerus */ 98b7bc15b9SHeikki Krogerus void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id) 99b7bc15b9SHeikki Krogerus { 100b7bc15b9SHeikki Krogerus struct phy_lookup *pl; 101b7bc15b9SHeikki Krogerus 102b7bc15b9SHeikki Krogerus if (!phy || !dev_id || !con_id) 103b7bc15b9SHeikki Krogerus return; 104b7bc15b9SHeikki Krogerus 105b7bc15b9SHeikki Krogerus mutex_lock(&phy_provider_mutex); 106b7bc15b9SHeikki Krogerus list_for_each_entry(pl, &phys, node) 107b7bc15b9SHeikki Krogerus if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) && 108b7bc15b9SHeikki Krogerus !strcmp(pl->con_id, con_id)) { 109b7bc15b9SHeikki Krogerus list_del(&pl->node); 110b7bc15b9SHeikki Krogerus kfree(pl); 111b7bc15b9SHeikki Krogerus break; 112b7bc15b9SHeikki Krogerus } 113b7bc15b9SHeikki Krogerus mutex_unlock(&phy_provider_mutex); 114b7bc15b9SHeikki Krogerus } 115b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_remove_lookup); 116b7bc15b9SHeikki Krogerus 117b7bc15b9SHeikki Krogerus static struct phy *phy_find(struct device *dev, const char *con_id) 118b7bc15b9SHeikki Krogerus { 119b7bc15b9SHeikki Krogerus const char *dev_id = dev_name(dev); 120b7bc15b9SHeikki Krogerus struct phy_lookup *p, *pl = NULL; 121b7bc15b9SHeikki Krogerus 122b7bc15b9SHeikki Krogerus mutex_lock(&phy_provider_mutex); 123b7bc15b9SHeikki Krogerus list_for_each_entry(p, &phys, node) 124b7bc15b9SHeikki Krogerus if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) { 125b7bc15b9SHeikki Krogerus pl = p; 126b7bc15b9SHeikki Krogerus break; 127b7bc15b9SHeikki Krogerus } 128b7bc15b9SHeikki Krogerus mutex_unlock(&phy_provider_mutex); 129b7bc15b9SHeikki Krogerus 130dbc98635SHeikki Krogerus return pl ? pl->phy : ERR_PTR(-ENODEV); 131b7bc15b9SHeikki Krogerus } 132b7bc15b9SHeikki Krogerus 133ff764963SKishon Vijay Abraham I static struct phy_provider *of_phy_provider_lookup(struct device_node *node) 134ff764963SKishon Vijay Abraham I { 135ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider; 1362a4c3701SKishon Vijay Abraham I struct device_node *child; 137ff764963SKishon Vijay Abraham I 138ff764963SKishon Vijay Abraham I list_for_each_entry(phy_provider, &phy_provider_list, list) { 139ff764963SKishon Vijay Abraham I if (phy_provider->dev->of_node == node) 140ff764963SKishon Vijay Abraham I return phy_provider; 1412a4c3701SKishon Vijay Abraham I 1421140f7c8SThierry Reding for_each_child_of_node(phy_provider->children, child) 14380a6eeb3SZijun Hu if (child == node) { 14480a6eeb3SZijun Hu of_node_put(child); 1452a4c3701SKishon Vijay Abraham I return phy_provider; 146ff764963SKishon Vijay Abraham I } 14780a6eeb3SZijun Hu } 148ff764963SKishon Vijay Abraham I 149ff764963SKishon Vijay Abraham I return ERR_PTR(-EPROBE_DEFER); 150ff764963SKishon Vijay Abraham I } 151ff764963SKishon Vijay Abraham I 152ff764963SKishon Vijay Abraham I int phy_pm_runtime_get(struct phy *phy) 153ff764963SKishon Vijay Abraham I { 154cedb7f89SFelipe Balbi int ret; 155cedb7f89SFelipe Balbi 1568866df25SManu Gautam if (!phy) 1578866df25SManu Gautam return 0; 1588866df25SManu Gautam 159ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 160ff764963SKishon Vijay Abraham I return -ENOTSUPP; 161ff764963SKishon Vijay Abraham I 162cedb7f89SFelipe Balbi ret = pm_runtime_get(&phy->dev); 163cedb7f89SFelipe Balbi if (ret < 0 && ret != -EINPROGRESS) 164cedb7f89SFelipe Balbi pm_runtime_put_noidle(&phy->dev); 165cedb7f89SFelipe Balbi 166cedb7f89SFelipe Balbi return ret; 167ff764963SKishon Vijay Abraham I } 168ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get); 169ff764963SKishon Vijay Abraham I 170ff764963SKishon Vijay Abraham I int phy_pm_runtime_get_sync(struct phy *phy) 171ff764963SKishon Vijay Abraham I { 172cedb7f89SFelipe Balbi int ret; 173cedb7f89SFelipe Balbi 1748866df25SManu Gautam if (!phy) 1758866df25SManu Gautam return 0; 1768866df25SManu Gautam 177ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 178ff764963SKishon Vijay Abraham I return -ENOTSUPP; 179ff764963SKishon Vijay Abraham I 180cedb7f89SFelipe Balbi ret = pm_runtime_get_sync(&phy->dev); 181cedb7f89SFelipe Balbi if (ret < 0) 182cedb7f89SFelipe Balbi pm_runtime_put_sync(&phy->dev); 183cedb7f89SFelipe Balbi 184cedb7f89SFelipe Balbi return ret; 185ff764963SKishon Vijay Abraham I } 186ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync); 187ff764963SKishon Vijay Abraham I 188ff764963SKishon Vijay Abraham I int phy_pm_runtime_put(struct phy *phy) 189ff764963SKishon Vijay Abraham I { 1908866df25SManu Gautam if (!phy) 1918866df25SManu Gautam return 0; 1928866df25SManu Gautam 193ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 194ff764963SKishon Vijay Abraham I return -ENOTSUPP; 195ff764963SKishon Vijay Abraham I 196ff764963SKishon Vijay Abraham I return pm_runtime_put(&phy->dev); 197ff764963SKishon Vijay Abraham I } 198ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put); 199ff764963SKishon Vijay Abraham I 200ff764963SKishon Vijay Abraham I int phy_pm_runtime_put_sync(struct phy *phy) 201ff764963SKishon Vijay Abraham I { 2028866df25SManu Gautam if (!phy) 2038866df25SManu Gautam return 0; 2048866df25SManu Gautam 205ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 206ff764963SKishon Vijay Abraham I return -ENOTSUPP; 207ff764963SKishon Vijay Abraham I 208ff764963SKishon Vijay Abraham I return pm_runtime_put_sync(&phy->dev); 209ff764963SKishon Vijay Abraham I } 210ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync); 211ff764963SKishon Vijay Abraham I 212ff764963SKishon Vijay Abraham I void phy_pm_runtime_allow(struct phy *phy) 213ff764963SKishon Vijay Abraham I { 2148866df25SManu Gautam if (!phy) 2158866df25SManu Gautam return; 2168866df25SManu Gautam 217ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 218ff764963SKishon Vijay Abraham I return; 219ff764963SKishon Vijay Abraham I 220ff764963SKishon Vijay Abraham I pm_runtime_allow(&phy->dev); 221ff764963SKishon Vijay Abraham I } 222ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_allow); 223ff764963SKishon Vijay Abraham I 224ff764963SKishon Vijay Abraham I void phy_pm_runtime_forbid(struct phy *phy) 225ff764963SKishon Vijay Abraham I { 2268866df25SManu Gautam if (!phy) 2278866df25SManu Gautam return; 2288866df25SManu Gautam 229ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 230ff764963SKishon Vijay Abraham I return; 231ff764963SKishon Vijay Abraham I 232ff764963SKishon Vijay Abraham I pm_runtime_forbid(&phy->dev); 233ff764963SKishon Vijay Abraham I } 234ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid); 235ff764963SKishon Vijay Abraham I 236f1b8d335SJules Maselbas /** 237f1b8d335SJules Maselbas * phy_init - phy internal initialization before phy operation 238f1b8d335SJules Maselbas * @phy: the phy returned by phy_get() 239f1b8d335SJules Maselbas * 240f1b8d335SJules Maselbas * Used to allow phy's driver to perform phy internal initialization, 241f1b8d335SJules Maselbas * such as PLL block powering, clock initialization or anything that's 242f1b8d335SJules Maselbas * is required by the phy to perform the start of operation. 243f1b8d335SJules Maselbas * Must be called before phy_power_on(). 244f1b8d335SJules Maselbas * 245f1b8d335SJules Maselbas * Return: %0 if successful, a negative error code otherwise 246f1b8d335SJules Maselbas */ 247ff764963SKishon Vijay Abraham I int phy_init(struct phy *phy) 248ff764963SKishon Vijay Abraham I { 249ff764963SKishon Vijay Abraham I int ret; 250ff764963SKishon Vijay Abraham I 25104c2facaSAndrew Lunn if (!phy) 25204c2facaSAndrew Lunn return 0; 25304c2facaSAndrew Lunn 254ff764963SKishon Vijay Abraham I ret = phy_pm_runtime_get_sync(phy); 255ff764963SKishon Vijay Abraham I if (ret < 0 && ret != -ENOTSUPP) 256ff764963SKishon Vijay Abraham I return ret; 257736b67a3SAxel Lin ret = 0; /* Override possible ret == -ENOTSUPP */ 258ff764963SKishon Vijay Abraham I 259ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 2601599069aSJules Maselbas if (phy->power_count > phy->init_count) 2611599069aSJules Maselbas dev_warn(&phy->dev, "phy_power_on was called before phy_init\n"); 2621599069aSJules Maselbas 263637d378cSKishon Vijay Abraham I if (phy->init_count == 0 && phy->ops->init) { 264ff764963SKishon Vijay Abraham I ret = phy->ops->init(phy); 265ff764963SKishon Vijay Abraham I if (ret < 0) { 266ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy init failed --> %d\n", ret); 267ff764963SKishon Vijay Abraham I goto out; 268ff764963SKishon Vijay Abraham I } 269ff764963SKishon Vijay Abraham I } 270637d378cSKishon Vijay Abraham I ++phy->init_count; 271ff764963SKishon Vijay Abraham I 272ff764963SKishon Vijay Abraham I out: 273ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 274ff764963SKishon Vijay Abraham I phy_pm_runtime_put(phy); 275ff764963SKishon Vijay Abraham I return ret; 276ff764963SKishon Vijay Abraham I } 277ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_init); 278ff764963SKishon Vijay Abraham I 279f1b8d335SJules Maselbas /** 280f1b8d335SJules Maselbas * phy_exit - Phy internal un-initialization 281f1b8d335SJules Maselbas * @phy: the phy returned by phy_get() 282f1b8d335SJules Maselbas * 283f1b8d335SJules Maselbas * Must be called after phy_power_off(). 284f1b8d335SJules Maselbas * 285f1b8d335SJules Maselbas * Return: %0 if successful, a negative error code otherwise 286f1b8d335SJules Maselbas */ 287ff764963SKishon Vijay Abraham I int phy_exit(struct phy *phy) 288ff764963SKishon Vijay Abraham I { 289ff764963SKishon Vijay Abraham I int ret; 290ff764963SKishon Vijay Abraham I 29104c2facaSAndrew Lunn if (!phy) 29204c2facaSAndrew Lunn return 0; 29304c2facaSAndrew Lunn 294ff764963SKishon Vijay Abraham I ret = phy_pm_runtime_get_sync(phy); 295ff764963SKishon Vijay Abraham I if (ret < 0 && ret != -ENOTSUPP) 296ff764963SKishon Vijay Abraham I return ret; 297736b67a3SAxel Lin ret = 0; /* Override possible ret == -ENOTSUPP */ 298ff764963SKishon Vijay Abraham I 299ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 300637d378cSKishon Vijay Abraham I if (phy->init_count == 1 && phy->ops->exit) { 301ff764963SKishon Vijay Abraham I ret = phy->ops->exit(phy); 302ff764963SKishon Vijay Abraham I if (ret < 0) { 303ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy exit failed --> %d\n", ret); 304ff764963SKishon Vijay Abraham I goto out; 305ff764963SKishon Vijay Abraham I } 306ff764963SKishon Vijay Abraham I } 307637d378cSKishon Vijay Abraham I --phy->init_count; 308ff764963SKishon Vijay Abraham I 309ff764963SKishon Vijay Abraham I out: 310ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 311ff764963SKishon Vijay Abraham I phy_pm_runtime_put(phy); 312ff764963SKishon Vijay Abraham I return ret; 313ff764963SKishon Vijay Abraham I } 314ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_exit); 315ff764963SKishon Vijay Abraham I 316f1b8d335SJules Maselbas /** 317f1b8d335SJules Maselbas * phy_power_on - Enable the phy and enter proper operation 318f1b8d335SJules Maselbas * @phy: the phy returned by phy_get() 319f1b8d335SJules Maselbas * 320f1b8d335SJules Maselbas * Must be called after phy_init(). 321f1b8d335SJules Maselbas * 322f1b8d335SJules Maselbas * Return: %0 if successful, a negative error code otherwise 323f1b8d335SJules Maselbas */ 324ff764963SKishon Vijay Abraham I int phy_power_on(struct phy *phy) 325ff764963SKishon Vijay Abraham I { 326b82fcabeSShawn Lin int ret = 0; 327ff764963SKishon Vijay Abraham I 32804c2facaSAndrew Lunn if (!phy) 329b82fcabeSShawn Lin goto out; 33004c2facaSAndrew Lunn 3313be88125SRoger Quadros if (phy->pwr) { 3323be88125SRoger Quadros ret = regulator_enable(phy->pwr); 3333be88125SRoger Quadros if (ret) 334b82fcabeSShawn Lin goto out; 3353be88125SRoger Quadros } 3363be88125SRoger Quadros 337ff764963SKishon Vijay Abraham I ret = phy_pm_runtime_get_sync(phy); 338ff764963SKishon Vijay Abraham I if (ret < 0 && ret != -ENOTSUPP) 339b82fcabeSShawn Lin goto err_pm_sync; 340b82fcabeSShawn Lin 341736b67a3SAxel Lin ret = 0; /* Override possible ret == -ENOTSUPP */ 342ff764963SKishon Vijay Abraham I 343ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 344637d378cSKishon Vijay Abraham I if (phy->power_count == 0 && phy->ops->power_on) { 345ff764963SKishon Vijay Abraham I ret = phy->ops->power_on(phy); 346ff764963SKishon Vijay Abraham I if (ret < 0) { 347ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy poweron failed --> %d\n", ret); 348b82fcabeSShawn Lin goto err_pwr_on; 349ff764963SKishon Vijay Abraham I } 350ff764963SKishon Vijay Abraham I } 351637d378cSKishon Vijay Abraham I ++phy->power_count; 352637d378cSKishon Vijay Abraham I mutex_unlock(&phy->mutex); 353637d378cSKishon Vijay Abraham I return 0; 354ff764963SKishon Vijay Abraham I 355b82fcabeSShawn Lin err_pwr_on: 356ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 357637d378cSKishon Vijay Abraham I phy_pm_runtime_put_sync(phy); 358b82fcabeSShawn Lin err_pm_sync: 3593be88125SRoger Quadros if (phy->pwr) 3603be88125SRoger Quadros regulator_disable(phy->pwr); 361b82fcabeSShawn Lin out: 362ff764963SKishon Vijay Abraham I return ret; 363ff764963SKishon Vijay Abraham I } 364ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_on); 365ff764963SKishon Vijay Abraham I 366f1b8d335SJules Maselbas /** 367f1b8d335SJules Maselbas * phy_power_off - Disable the phy. 368f1b8d335SJules Maselbas * @phy: the phy returned by phy_get() 369f1b8d335SJules Maselbas * 370f1b8d335SJules Maselbas * Must be called before phy_exit(). 371f1b8d335SJules Maselbas * 372f1b8d335SJules Maselbas * Return: %0 if successful, a negative error code otherwise 373f1b8d335SJules Maselbas */ 374ff764963SKishon Vijay Abraham I int phy_power_off(struct phy *phy) 375ff764963SKishon Vijay Abraham I { 376d18c9604SKishon Vijay Abraham I int ret; 377ff764963SKishon Vijay Abraham I 37804c2facaSAndrew Lunn if (!phy) 37904c2facaSAndrew Lunn return 0; 38004c2facaSAndrew Lunn 381ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 382637d378cSKishon Vijay Abraham I if (phy->power_count == 1 && phy->ops->power_off) { 383ff764963SKishon Vijay Abraham I ret = phy->ops->power_off(phy); 384ff764963SKishon Vijay Abraham I if (ret < 0) { 385ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret); 386637d378cSKishon Vijay Abraham I mutex_unlock(&phy->mutex); 387637d378cSKishon Vijay Abraham I return ret; 388ff764963SKishon Vijay Abraham I } 389ff764963SKishon Vijay Abraham I } 390637d378cSKishon Vijay Abraham I --phy->power_count; 391ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 392ff764963SKishon Vijay Abraham I phy_pm_runtime_put(phy); 393ff764963SKishon Vijay Abraham I 3943be88125SRoger Quadros if (phy->pwr) 3953be88125SRoger Quadros regulator_disable(phy->pwr); 3963be88125SRoger Quadros 397637d378cSKishon Vijay Abraham I return 0; 398ff764963SKishon Vijay Abraham I } 399ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_off); 400ff764963SKishon Vijay Abraham I 40179a5a18aSGrygorii Strashko int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode) 402300eb013SDavid Lechner { 403300eb013SDavid Lechner int ret; 404300eb013SDavid Lechner 405300eb013SDavid Lechner if (!phy || !phy->ops->set_mode) 406300eb013SDavid Lechner return 0; 407300eb013SDavid Lechner 408300eb013SDavid Lechner mutex_lock(&phy->mutex); 40979a5a18aSGrygorii Strashko ret = phy->ops->set_mode(phy, mode, submode); 4103b3cd24aSManu Gautam if (!ret) 4113b3cd24aSManu Gautam phy->attrs.mode = mode; 412300eb013SDavid Lechner mutex_unlock(&phy->mutex); 413300eb013SDavid Lechner 414300eb013SDavid Lechner return ret; 415300eb013SDavid Lechner } 41679a5a18aSGrygorii Strashko EXPORT_SYMBOL_GPL(phy_set_mode_ext); 417300eb013SDavid Lechner 4186c172e73SSteen Hegelund int phy_set_media(struct phy *phy, enum phy_media media) 4196c172e73SSteen Hegelund { 4206c172e73SSteen Hegelund int ret; 4216c172e73SSteen Hegelund 4226c172e73SSteen Hegelund if (!phy || !phy->ops->set_media) 4236c172e73SSteen Hegelund return 0; 4246c172e73SSteen Hegelund 4256c172e73SSteen Hegelund mutex_lock(&phy->mutex); 4266c172e73SSteen Hegelund ret = phy->ops->set_media(phy, media); 4276c172e73SSteen Hegelund mutex_unlock(&phy->mutex); 4286c172e73SSteen Hegelund 4296c172e73SSteen Hegelund return ret; 4306c172e73SSteen Hegelund } 4316c172e73SSteen Hegelund EXPORT_SYMBOL_GPL(phy_set_media); 4326c172e73SSteen Hegelund 4336c172e73SSteen Hegelund int phy_set_speed(struct phy *phy, int speed) 4346c172e73SSteen Hegelund { 4356c172e73SSteen Hegelund int ret; 4366c172e73SSteen Hegelund 4376c172e73SSteen Hegelund if (!phy || !phy->ops->set_speed) 4386c172e73SSteen Hegelund return 0; 4396c172e73SSteen Hegelund 4406c172e73SSteen Hegelund mutex_lock(&phy->mutex); 4416c172e73SSteen Hegelund ret = phy->ops->set_speed(phy, speed); 4426c172e73SSteen Hegelund mutex_unlock(&phy->mutex); 4436c172e73SSteen Hegelund 4446c172e73SSteen Hegelund return ret; 4456c172e73SSteen Hegelund } 4466c172e73SSteen Hegelund EXPORT_SYMBOL_GPL(phy_set_speed); 4476c172e73SSteen Hegelund 448cac18ecbSRandy Li int phy_reset(struct phy *phy) 449cac18ecbSRandy Li { 450cac18ecbSRandy Li int ret; 451cac18ecbSRandy Li 452cac18ecbSRandy Li if (!phy || !phy->ops->reset) 453cac18ecbSRandy Li return 0; 454cac18ecbSRandy Li 4554df614c4SKishon Vijay Abraham I ret = phy_pm_runtime_get_sync(phy); 4564df614c4SKishon Vijay Abraham I if (ret < 0 && ret != -ENOTSUPP) 4574df614c4SKishon Vijay Abraham I return ret; 4584df614c4SKishon Vijay Abraham I 459cac18ecbSRandy Li mutex_lock(&phy->mutex); 460cac18ecbSRandy Li ret = phy->ops->reset(phy); 461cac18ecbSRandy Li mutex_unlock(&phy->mutex); 462cac18ecbSRandy Li 4634df614c4SKishon Vijay Abraham I phy_pm_runtime_put(phy); 4644df614c4SKishon Vijay Abraham I 465cac18ecbSRandy Li return ret; 466cac18ecbSRandy Li } 467cac18ecbSRandy Li EXPORT_SYMBOL_GPL(phy_reset); 468cac18ecbSRandy Li 469bbae18f0SMarek Szyprowski /** 470bbae18f0SMarek Szyprowski * phy_calibrate() - Tunes the phy hw parameters for current configuration 471bbae18f0SMarek Szyprowski * @phy: the phy returned by phy_get() 472bbae18f0SMarek Szyprowski * 473bbae18f0SMarek Szyprowski * Used to calibrate phy hardware, typically by adjusting some parameters in 474bbae18f0SMarek Szyprowski * runtime, which are otherwise lost after host controller reset and cannot 475bbae18f0SMarek Szyprowski * be applied in phy_init() or phy_power_on(). 476bbae18f0SMarek Szyprowski * 477bd5bd02eSJules Maselbas * Return: %0 if successful, a negative error code otherwise 478bbae18f0SMarek Szyprowski */ 47936914111SAndrzej Pietrasiewicz int phy_calibrate(struct phy *phy) 48036914111SAndrzej Pietrasiewicz { 48136914111SAndrzej Pietrasiewicz int ret; 48236914111SAndrzej Pietrasiewicz 48336914111SAndrzej Pietrasiewicz if (!phy || !phy->ops->calibrate) 48436914111SAndrzej Pietrasiewicz return 0; 48536914111SAndrzej Pietrasiewicz 48636914111SAndrzej Pietrasiewicz mutex_lock(&phy->mutex); 48736914111SAndrzej Pietrasiewicz ret = phy->ops->calibrate(phy); 48836914111SAndrzej Pietrasiewicz mutex_unlock(&phy->mutex); 48936914111SAndrzej Pietrasiewicz 49036914111SAndrzej Pietrasiewicz return ret; 49136914111SAndrzej Pietrasiewicz } 49236914111SAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(phy_calibrate); 49336914111SAndrzej Pietrasiewicz 494ff764963SKishon Vijay Abraham I /** 495aeaac93dSMaxime Ripard * phy_configure() - Changes the phy parameters 496aeaac93dSMaxime Ripard * @phy: the phy returned by phy_get() 497aeaac93dSMaxime Ripard * @opts: New configuration to apply 498aeaac93dSMaxime Ripard * 499aeaac93dSMaxime Ripard * Used to change the PHY parameters. phy_init() must have been called 500aeaac93dSMaxime Ripard * on the phy. The configuration will be applied on the current phy 501aeaac93dSMaxime Ripard * mode, that can be changed using phy_set_mode(). 502aeaac93dSMaxime Ripard * 503bd5bd02eSJules Maselbas * Return: %0 if successful, a negative error code otherwise 504aeaac93dSMaxime Ripard */ 505aeaac93dSMaxime Ripard int phy_configure(struct phy *phy, union phy_configure_opts *opts) 506aeaac93dSMaxime Ripard { 507aeaac93dSMaxime Ripard int ret; 508aeaac93dSMaxime Ripard 509aeaac93dSMaxime Ripard if (!phy) 510aeaac93dSMaxime Ripard return -EINVAL; 511aeaac93dSMaxime Ripard 512aeaac93dSMaxime Ripard if (!phy->ops->configure) 513aeaac93dSMaxime Ripard return -EOPNOTSUPP; 514aeaac93dSMaxime Ripard 515aeaac93dSMaxime Ripard mutex_lock(&phy->mutex); 516aeaac93dSMaxime Ripard ret = phy->ops->configure(phy, opts); 517aeaac93dSMaxime Ripard mutex_unlock(&phy->mutex); 518aeaac93dSMaxime Ripard 519aeaac93dSMaxime Ripard return ret; 520aeaac93dSMaxime Ripard } 521aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_configure); 522aeaac93dSMaxime Ripard 523aeaac93dSMaxime Ripard /** 524aeaac93dSMaxime Ripard * phy_validate() - Checks the phy parameters 525aeaac93dSMaxime Ripard * @phy: the phy returned by phy_get() 526aeaac93dSMaxime Ripard * @mode: phy_mode the configuration is applicable to. 527aeaac93dSMaxime Ripard * @submode: PHY submode the configuration is applicable to. 528aeaac93dSMaxime Ripard * @opts: Configuration to check 529aeaac93dSMaxime Ripard * 530aeaac93dSMaxime Ripard * Used to check that the current set of parameters can be handled by 531aeaac93dSMaxime Ripard * the phy. Implementations are free to tune the parameters passed as 532aeaac93dSMaxime Ripard * arguments if needed by some implementation detail or 533aeaac93dSMaxime Ripard * constraints. It will not change any actual configuration of the 534aeaac93dSMaxime Ripard * PHY, so calling it as many times as deemed fit will have no side 535aeaac93dSMaxime Ripard * effect. 536aeaac93dSMaxime Ripard * 537bd5bd02eSJules Maselbas * Return: %0 if successful, a negative error code otherwise 538aeaac93dSMaxime Ripard */ 539aeaac93dSMaxime Ripard int phy_validate(struct phy *phy, enum phy_mode mode, int submode, 540aeaac93dSMaxime Ripard union phy_configure_opts *opts) 541aeaac93dSMaxime Ripard { 542aeaac93dSMaxime Ripard int ret; 543aeaac93dSMaxime Ripard 544aeaac93dSMaxime Ripard if (!phy) 545aeaac93dSMaxime Ripard return -EINVAL; 546aeaac93dSMaxime Ripard 547aeaac93dSMaxime Ripard if (!phy->ops->validate) 548aeaac93dSMaxime Ripard return -EOPNOTSUPP; 549aeaac93dSMaxime Ripard 550aeaac93dSMaxime Ripard mutex_lock(&phy->mutex); 551aeaac93dSMaxime Ripard ret = phy->ops->validate(phy, mode, submode, opts); 552aeaac93dSMaxime Ripard mutex_unlock(&phy->mutex); 553aeaac93dSMaxime Ripard 554aeaac93dSMaxime Ripard return ret; 555aeaac93dSMaxime Ripard } 556aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_validate); 557aeaac93dSMaxime Ripard 558aeaac93dSMaxime Ripard /** 5590b3f3b2cSKamil Debski * _of_phy_get() - lookup and obtain a reference to a phy by phandle 5600b3f3b2cSKamil Debski * @np: device_node for which to get the phy 561ff764963SKishon Vijay Abraham I * @index: the index of the phy 562ff764963SKishon Vijay Abraham I * 563ff764963SKishon Vijay Abraham I * Returns the phy associated with the given phandle value, 564ff764963SKishon Vijay Abraham I * after getting a refcount to it or -ENODEV if there is no such phy or 565ff764963SKishon Vijay Abraham I * -EPROBE_DEFER if there is a phandle to the phy, but the device is 566ff764963SKishon Vijay Abraham I * not yet loaded. This function uses of_xlate call back function provided 567ff764963SKishon Vijay Abraham I * while registering the phy_provider to find the phy instance. 568ff764963SKishon Vijay Abraham I */ 5690b3f3b2cSKamil Debski static struct phy *_of_phy_get(struct device_node *np, int index) 570ff764963SKishon Vijay Abraham I { 571ff764963SKishon Vijay Abraham I int ret; 572ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider; 573ff764963SKishon Vijay Abraham I struct phy *phy = NULL; 574ff764963SKishon Vijay Abraham I struct of_phandle_args args; 575ff764963SKishon Vijay Abraham I 5760b3f3b2cSKamil Debski ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", 577ff764963SKishon Vijay Abraham I index, &args); 5780b3f3b2cSKamil Debski if (ret) 579ff764963SKishon Vijay Abraham I return ERR_PTR(-ENODEV); 580ff764963SKishon Vijay Abraham I 581b7563e27SArnd Bergmann /* This phy type handled by the usb-phy subsystem for now */ 582293b3748SZijun Hu if (of_device_is_compatible(args.np, "usb-nop-xceiv")) { 583293b3748SZijun Hu phy = ERR_PTR(-ENODEV); 584293b3748SZijun Hu goto out_put_node; 585293b3748SZijun Hu } 586b7563e27SArnd Bergmann 587ff764963SKishon Vijay Abraham I mutex_lock(&phy_provider_mutex); 588ff764963SKishon Vijay Abraham I phy_provider = of_phy_provider_lookup(args.np); 589ff764963SKishon Vijay Abraham I if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) { 590ff764963SKishon Vijay Abraham I phy = ERR_PTR(-EPROBE_DEFER); 59133f434d2SAxel Lin goto out_unlock; 59233f434d2SAxel Lin } 59333f434d2SAxel Lin 59433f434d2SAxel Lin if (!of_device_is_available(args.np)) { 59533f434d2SAxel Lin dev_warn(phy_provider->dev, "Requested PHY is disabled\n"); 59633f434d2SAxel Lin phy = ERR_PTR(-ENODEV); 59733f434d2SAxel Lin goto out_put_module; 598ff764963SKishon Vijay Abraham I } 599ff764963SKishon Vijay Abraham I 600ff764963SKishon Vijay Abraham I phy = phy_provider->of_xlate(phy_provider->dev, &args); 60133f434d2SAxel Lin 60233f434d2SAxel Lin out_put_module: 603ff764963SKishon Vijay Abraham I module_put(phy_provider->owner); 604ff764963SKishon Vijay Abraham I 60533f434d2SAxel Lin out_unlock: 606ff764963SKishon Vijay Abraham I mutex_unlock(&phy_provider_mutex); 607293b3748SZijun Hu out_put_node: 608ff764963SKishon Vijay Abraham I of_node_put(args.np); 609ff764963SKishon Vijay Abraham I 610ff764963SKishon Vijay Abraham I return phy; 611ff764963SKishon Vijay Abraham I } 612ff764963SKishon Vijay Abraham I 613ff764963SKishon Vijay Abraham I /** 6140b3f3b2cSKamil Debski * of_phy_get() - lookup and obtain a reference to a phy using a device_node. 6150b3f3b2cSKamil Debski * @np: device_node for which to get the phy 6160b3f3b2cSKamil Debski * @con_id: name of the phy from device's point of view 6170b3f3b2cSKamil Debski * 6180b3f3b2cSKamil Debski * Returns the phy driver, after getting a refcount to it; or 6190b3f3b2cSKamil Debski * -ENODEV if there is no such phy. The caller is responsible for 6200b3f3b2cSKamil Debski * calling phy_put() to release that count. 6210b3f3b2cSKamil Debski */ 6220b3f3b2cSKamil Debski struct phy *of_phy_get(struct device_node *np, const char *con_id) 6230b3f3b2cSKamil Debski { 6240b3f3b2cSKamil Debski struct phy *phy = NULL; 6250b3f3b2cSKamil Debski int index = 0; 6260b3f3b2cSKamil Debski 6270b3f3b2cSKamil Debski if (con_id) 6280b3f3b2cSKamil Debski index = of_property_match_string(np, "phy-names", con_id); 6290b3f3b2cSKamil Debski 6300b3f3b2cSKamil Debski phy = _of_phy_get(np, index); 6310b3f3b2cSKamil Debski if (IS_ERR(phy)) 6320b3f3b2cSKamil Debski return phy; 6330b3f3b2cSKamil Debski 6340b3f3b2cSKamil Debski if (!try_module_get(phy->ops->owner)) 6350b3f3b2cSKamil Debski return ERR_PTR(-EPROBE_DEFER); 6360b3f3b2cSKamil Debski 6370b3f3b2cSKamil Debski get_device(&phy->dev); 6380b3f3b2cSKamil Debski 6390b3f3b2cSKamil Debski return phy; 6400b3f3b2cSKamil Debski } 6410b3f3b2cSKamil Debski EXPORT_SYMBOL_GPL(of_phy_get); 6420b3f3b2cSKamil Debski 6430b3f3b2cSKamil Debski /** 644987351e1SAlexandre Torgue * of_phy_put() - release the PHY 645987351e1SAlexandre Torgue * @phy: the phy returned by of_phy_get() 646ff764963SKishon Vijay Abraham I * 647987351e1SAlexandre Torgue * Releases a refcount the caller received from of_phy_get(). 648ff764963SKishon Vijay Abraham I */ 649987351e1SAlexandre Torgue void of_phy_put(struct phy *phy) 650ff764963SKishon Vijay Abraham I { 65104c2facaSAndrew Lunn if (!phy || IS_ERR(phy)) 652ff764963SKishon Vijay Abraham I return; 653ff764963SKishon Vijay Abraham I 654fec06b2bSKishon Vijay Abraham I mutex_lock(&phy->mutex); 655fec06b2bSKishon Vijay Abraham I if (phy->ops->release) 656fec06b2bSKishon Vijay Abraham I phy->ops->release(phy); 657fec06b2bSKishon Vijay Abraham I mutex_unlock(&phy->mutex); 658fec06b2bSKishon Vijay Abraham I 659ff764963SKishon Vijay Abraham I module_put(phy->ops->owner); 660ff764963SKishon Vijay Abraham I put_device(&phy->dev); 661ff764963SKishon Vijay Abraham I } 662987351e1SAlexandre Torgue EXPORT_SYMBOL_GPL(of_phy_put); 663987351e1SAlexandre Torgue 664987351e1SAlexandre Torgue /** 665987351e1SAlexandre Torgue * phy_put() - release the PHY 666987351e1SAlexandre Torgue * @dev: device that wants to release this phy 667987351e1SAlexandre Torgue * @phy: the phy returned by phy_get() 668987351e1SAlexandre Torgue * 669987351e1SAlexandre Torgue * Releases a refcount the caller received from phy_get(). 670987351e1SAlexandre Torgue */ 671987351e1SAlexandre Torgue void phy_put(struct device *dev, struct phy *phy) 672987351e1SAlexandre Torgue { 673987351e1SAlexandre Torgue device_link_remove(dev, &phy->dev); 674987351e1SAlexandre Torgue of_phy_put(phy); 675987351e1SAlexandre Torgue } 676ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_put); 677ff764963SKishon Vijay Abraham I 678ff764963SKishon Vijay Abraham I /** 679ff764963SKishon Vijay Abraham I * devm_phy_put() - release the PHY 680ff764963SKishon Vijay Abraham I * @dev: device that wants to release this phy 681ff764963SKishon Vijay Abraham I * @phy: the phy returned by devm_phy_get() 682ff764963SKishon Vijay Abraham I * 683ff764963SKishon Vijay Abraham I * destroys the devres associated with this phy and invokes phy_put 684ff764963SKishon Vijay Abraham I * to release the phy. 685ff764963SKishon Vijay Abraham I */ 686ff764963SKishon Vijay Abraham I void devm_phy_put(struct device *dev, struct phy *phy) 687ff764963SKishon Vijay Abraham I { 688ff764963SKishon Vijay Abraham I int r; 689ff764963SKishon Vijay Abraham I 69004c2facaSAndrew Lunn if (!phy) 69104c2facaSAndrew Lunn return; 69204c2facaSAndrew Lunn 69358068f8bSZijun Hu r = devres_release(dev, devm_phy_release, devm_phy_match, phy); 694ff764963SKishon Vijay Abraham I dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 695ff764963SKishon Vijay Abraham I } 696ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_put); 697ff764963SKishon Vijay Abraham I 698ff764963SKishon Vijay Abraham I /** 699ff764963SKishon Vijay Abraham I * of_phy_simple_xlate() - returns the phy instance from phy provider 700ff764963SKishon Vijay Abraham I * @dev: the PHY provider device 701ff764963SKishon Vijay Abraham I * @args: of_phandle_args (not used here) 702ff764963SKishon Vijay Abraham I * 703ff764963SKishon Vijay Abraham I * Intended to be used by phy provider for the common case where #phy-cells is 704ff764963SKishon Vijay Abraham I * 0. For other cases where #phy-cells is greater than '0', the phy provider 705ff764963SKishon Vijay Abraham I * should provide a custom of_xlate function that reads the *args* and returns 706ff764963SKishon Vijay Abraham I * the appropriate phy. 707ff764963SKishon Vijay Abraham I */ 708ff764963SKishon Vijay Abraham I struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args 709ff764963SKishon Vijay Abraham I *args) 710ff764963SKishon Vijay Abraham I { 711ff764963SKishon Vijay Abraham I struct phy *phy; 712ff764963SKishon Vijay Abraham I struct class_dev_iter iter; 713ff764963SKishon Vijay Abraham I 714ff764963SKishon Vijay Abraham I class_dev_iter_init(&iter, phy_class, NULL, NULL); 715ff764963SKishon Vijay Abraham I while ((dev = class_dev_iter_next(&iter))) { 716ff764963SKishon Vijay Abraham I phy = to_phy(dev); 717491e0490SKishon Vijay Abraham I if (args->np != phy->dev.of_node) 718ff764963SKishon Vijay Abraham I continue; 719ff764963SKishon Vijay Abraham I 720ff764963SKishon Vijay Abraham I class_dev_iter_exit(&iter); 721ff764963SKishon Vijay Abraham I return phy; 722ff764963SKishon Vijay Abraham I } 723ff764963SKishon Vijay Abraham I 724ff764963SKishon Vijay Abraham I class_dev_iter_exit(&iter); 725ff764963SKishon Vijay Abraham I return ERR_PTR(-ENODEV); 726ff764963SKishon Vijay Abraham I } 727ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_simple_xlate); 728ff764963SKishon Vijay Abraham I 729ff764963SKishon Vijay Abraham I /** 730ff764963SKishon Vijay Abraham I * phy_get() - lookup and obtain a reference to a phy. 731ff764963SKishon Vijay Abraham I * @dev: device that requests this phy 732ff764963SKishon Vijay Abraham I * @string: the phy name as given in the dt data or the name of the controller 733ff764963SKishon Vijay Abraham I * port for non-dt case 734ff764963SKishon Vijay Abraham I * 735ff764963SKishon Vijay Abraham I * Returns the phy driver, after getting a refcount to it; or 736ff764963SKishon Vijay Abraham I * -ENODEV if there is no such phy. The caller is responsible for 737ff764963SKishon Vijay Abraham I * calling phy_put() to release that count. 738ff764963SKishon Vijay Abraham I */ 739ff764963SKishon Vijay Abraham I struct phy *phy_get(struct device *dev, const char *string) 740ff764963SKishon Vijay Abraham I { 741ff764963SKishon Vijay Abraham I int index = 0; 742d18c9604SKishon Vijay Abraham I struct phy *phy; 743987351e1SAlexandre Torgue struct device_link *link; 744ff764963SKishon Vijay Abraham I 7458a917813SRob Herring if (dev->of_node) { 7468a917813SRob Herring if (string) 7478a917813SRob Herring index = of_property_match_string(dev->of_node, "phy-names", 7488a917813SRob Herring string); 7498a917813SRob Herring else 7508a917813SRob Herring index = 0; 7518a917813SRob Herring phy = _of_phy_get(dev->of_node, index); 7528a917813SRob Herring } else { 753ff764963SKishon Vijay Abraham I if (string == NULL) { 754ff764963SKishon Vijay Abraham I dev_WARN(dev, "missing string\n"); 755ff764963SKishon Vijay Abraham I return ERR_PTR(-EINVAL); 756ff764963SKishon Vijay Abraham I } 757b7bc15b9SHeikki Krogerus phy = phy_find(dev, string); 758f40037fdSHans de Goede } 759f40037fdSHans de Goede if (IS_ERR(phy)) 760ff764963SKishon Vijay Abraham I return phy; 761ff764963SKishon Vijay Abraham I 762ff764963SKishon Vijay Abraham I if (!try_module_get(phy->ops->owner)) 763ff764963SKishon Vijay Abraham I return ERR_PTR(-EPROBE_DEFER); 764ff764963SKishon Vijay Abraham I 765ff764963SKishon Vijay Abraham I get_device(&phy->dev); 766ff764963SKishon Vijay Abraham I 767987351e1SAlexandre Torgue link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 7681d7cb11eSKishon Vijay Abraham I if (!link) 7691d7cb11eSKishon Vijay Abraham I dev_dbg(dev, "failed to create device link to %s\n", 770987351e1SAlexandre Torgue dev_name(phy->dev.parent)); 771987351e1SAlexandre Torgue 772ff764963SKishon Vijay Abraham I return phy; 773ff764963SKishon Vijay Abraham I } 774ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_get); 775ff764963SKishon Vijay Abraham I 776ff764963SKishon Vijay Abraham I /** 777ff764963SKishon Vijay Abraham I * devm_phy_get() - lookup and obtain a reference to a phy. 778ff764963SKishon Vijay Abraham I * @dev: device that requests this phy 779ff764963SKishon Vijay Abraham I * @string: the phy name as given in the dt data or phy device name 780ff764963SKishon Vijay Abraham I * for non-dt case 781ff764963SKishon Vijay Abraham I * 782ff764963SKishon Vijay Abraham I * Gets the phy using phy_get(), and associates a device with it using 783ff764963SKishon Vijay Abraham I * devres. On driver detach, release function is invoked on the devres data, 784ff764963SKishon Vijay Abraham I * then, devres data is freed. 785ff764963SKishon Vijay Abraham I */ 786ff764963SKishon Vijay Abraham I struct phy *devm_phy_get(struct device *dev, const char *string) 787ff764963SKishon Vijay Abraham I { 788ff764963SKishon Vijay Abraham I struct phy **ptr, *phy; 789ff764963SKishon Vijay Abraham I 790ff764963SKishon Vijay Abraham I ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 791ff764963SKishon Vijay Abraham I if (!ptr) 792ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 793ff764963SKishon Vijay Abraham I 794ff764963SKishon Vijay Abraham I phy = phy_get(dev, string); 795ff764963SKishon Vijay Abraham I if (!IS_ERR(phy)) { 796ff764963SKishon Vijay Abraham I *ptr = phy; 797ff764963SKishon Vijay Abraham I devres_add(dev, ptr); 798ff764963SKishon Vijay Abraham I } else { 799ff764963SKishon Vijay Abraham I devres_free(ptr); 800ff764963SKishon Vijay Abraham I } 801ff764963SKishon Vijay Abraham I 802ff764963SKishon Vijay Abraham I return phy; 803ff764963SKishon Vijay Abraham I } 804ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_get); 805ff764963SKishon Vijay Abraham I 806ff764963SKishon Vijay Abraham I /** 807788a4d56SAndrew Lunn * devm_phy_optional_get() - lookup and obtain a reference to an optional phy. 808788a4d56SAndrew Lunn * @dev: device that requests this phy 809788a4d56SAndrew Lunn * @string: the phy name as given in the dt data or phy device name 810788a4d56SAndrew Lunn * for non-dt case 811788a4d56SAndrew Lunn * 812788a4d56SAndrew Lunn * Gets the phy using phy_get(), and associates a device with it using 813788a4d56SAndrew Lunn * devres. On driver detach, release function is invoked on the devres 814788a4d56SAndrew Lunn * data, then, devres data is freed. This differs to devm_phy_get() in 815788a4d56SAndrew Lunn * that if the phy does not exist, it is not considered an error and 816788a4d56SAndrew Lunn * -ENODEV will not be returned. Instead the NULL phy is returned, 817788a4d56SAndrew Lunn * which can be passed to all other phy consumer calls. 818788a4d56SAndrew Lunn */ 819788a4d56SAndrew Lunn struct phy *devm_phy_optional_get(struct device *dev, const char *string) 820788a4d56SAndrew Lunn { 821788a4d56SAndrew Lunn struct phy *phy = devm_phy_get(dev, string); 822788a4d56SAndrew Lunn 82345586c70SMasahiro Yamada if (PTR_ERR(phy) == -ENODEV) 824788a4d56SAndrew Lunn phy = NULL; 825788a4d56SAndrew Lunn 826788a4d56SAndrew Lunn return phy; 827788a4d56SAndrew Lunn } 828788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(devm_phy_optional_get); 829788a4d56SAndrew Lunn 830788a4d56SAndrew Lunn /** 831b5d682f4SKamil Debski * devm_of_phy_get() - lookup and obtain a reference to a phy. 832b5d682f4SKamil Debski * @dev: device that requests this phy 833b5d682f4SKamil Debski * @np: node containing the phy 834b5d682f4SKamil Debski * @con_id: name of the phy from device's point of view 835b5d682f4SKamil Debski * 836b5d682f4SKamil Debski * Gets the phy using of_phy_get(), and associates a device with it using 837b5d682f4SKamil Debski * devres. On driver detach, release function is invoked on the devres data, 838b5d682f4SKamil Debski * then, devres data is freed. 839b5d682f4SKamil Debski */ 840b5d682f4SKamil Debski struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 841b5d682f4SKamil Debski const char *con_id) 842b5d682f4SKamil Debski { 843b5d682f4SKamil Debski struct phy **ptr, *phy; 844987351e1SAlexandre Torgue struct device_link *link; 845b5d682f4SKamil Debski 846b5d682f4SKamil Debski ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 847b5d682f4SKamil Debski if (!ptr) 848b5d682f4SKamil Debski return ERR_PTR(-ENOMEM); 849b5d682f4SKamil Debski 850b5d682f4SKamil Debski phy = of_phy_get(np, con_id); 851b5d682f4SKamil Debski if (!IS_ERR(phy)) { 852b5d682f4SKamil Debski *ptr = phy; 853b5d682f4SKamil Debski devres_add(dev, ptr); 854b5d682f4SKamil Debski } else { 855b5d682f4SKamil Debski devres_free(ptr); 856987351e1SAlexandre Torgue return phy; 857987351e1SAlexandre Torgue } 858987351e1SAlexandre Torgue 859987351e1SAlexandre Torgue link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 8601d7cb11eSKishon Vijay Abraham I if (!link) 8611d7cb11eSKishon Vijay Abraham I dev_dbg(dev, "failed to create device link to %s\n", 862987351e1SAlexandre Torgue dev_name(phy->dev.parent)); 863b5d682f4SKamil Debski 864b5d682f4SKamil Debski return phy; 865b5d682f4SKamil Debski } 866b5d682f4SKamil Debski EXPORT_SYMBOL_GPL(devm_of_phy_get); 867b5d682f4SKamil Debski 868b5d682f4SKamil Debski /** 869d02aa181SGeert Uytterhoeven * devm_of_phy_optional_get() - lookup and obtain a reference to an optional 870d02aa181SGeert Uytterhoeven * phy. 871d02aa181SGeert Uytterhoeven * @dev: device that requests this phy 872d02aa181SGeert Uytterhoeven * @np: node containing the phy 873d02aa181SGeert Uytterhoeven * @con_id: name of the phy from device's point of view 874d02aa181SGeert Uytterhoeven * 875d02aa181SGeert Uytterhoeven * Gets the phy using of_phy_get(), and associates a device with it using 876d02aa181SGeert Uytterhoeven * devres. On driver detach, release function is invoked on the devres data, 877d02aa181SGeert Uytterhoeven * then, devres data is freed. This differs to devm_of_phy_get() in 878d02aa181SGeert Uytterhoeven * that if the phy does not exist, it is not considered an error and 879d02aa181SGeert Uytterhoeven * -ENODEV will not be returned. Instead the NULL phy is returned, 880d02aa181SGeert Uytterhoeven * which can be passed to all other phy consumer calls. 881d02aa181SGeert Uytterhoeven */ 882d02aa181SGeert Uytterhoeven struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np, 883d02aa181SGeert Uytterhoeven const char *con_id) 884d02aa181SGeert Uytterhoeven { 885d02aa181SGeert Uytterhoeven struct phy *phy = devm_of_phy_get(dev, np, con_id); 886d02aa181SGeert Uytterhoeven 887d02aa181SGeert Uytterhoeven if (PTR_ERR(phy) == -ENODEV) 888d02aa181SGeert Uytterhoeven phy = NULL; 889d02aa181SGeert Uytterhoeven 890d02aa181SGeert Uytterhoeven if (IS_ERR(phy)) 891d02aa181SGeert Uytterhoeven dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s", 892d02aa181SGeert Uytterhoeven np, con_id); 893d02aa181SGeert Uytterhoeven 894d02aa181SGeert Uytterhoeven return phy; 895d02aa181SGeert Uytterhoeven } 896d02aa181SGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_of_phy_optional_get); 897d02aa181SGeert Uytterhoeven 898d02aa181SGeert Uytterhoeven /** 8996be109b3SArun Ramamurthy * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index. 9006be109b3SArun Ramamurthy * @dev: device that requests this phy 9016be109b3SArun Ramamurthy * @np: node containing the phy 9026be109b3SArun Ramamurthy * @index: index of the phy 9036be109b3SArun Ramamurthy * 90470874462SChunfeng Yun * Gets the phy using _of_phy_get(), then gets a refcount to it, 90570874462SChunfeng Yun * and associates a device with it using devres. On driver detach, 90670874462SChunfeng Yun * release function is invoked on the devres data, 9076be109b3SArun Ramamurthy * then, devres data is freed. 9086be109b3SArun Ramamurthy * 9096be109b3SArun Ramamurthy */ 9106be109b3SArun Ramamurthy struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, 9116be109b3SArun Ramamurthy int index) 9126be109b3SArun Ramamurthy { 9136be109b3SArun Ramamurthy struct phy **ptr, *phy; 914987351e1SAlexandre Torgue struct device_link *link; 9156be109b3SArun Ramamurthy 9166be109b3SArun Ramamurthy ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 9176be109b3SArun Ramamurthy if (!ptr) 9186be109b3SArun Ramamurthy return ERR_PTR(-ENOMEM); 9196be109b3SArun Ramamurthy 9206be109b3SArun Ramamurthy phy = _of_phy_get(np, index); 92170874462SChunfeng Yun if (IS_ERR(phy)) { 92270874462SChunfeng Yun devres_free(ptr); 92370874462SChunfeng Yun return phy; 92470874462SChunfeng Yun } 92570874462SChunfeng Yun 92670874462SChunfeng Yun if (!try_module_get(phy->ops->owner)) { 92770874462SChunfeng Yun devres_free(ptr); 92870874462SChunfeng Yun return ERR_PTR(-EPROBE_DEFER); 92970874462SChunfeng Yun } 93070874462SChunfeng Yun 93170874462SChunfeng Yun get_device(&phy->dev); 93270874462SChunfeng Yun 9336be109b3SArun Ramamurthy *ptr = phy; 9346be109b3SArun Ramamurthy devres_add(dev, ptr); 9356be109b3SArun Ramamurthy 936987351e1SAlexandre Torgue link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); 9371d7cb11eSKishon Vijay Abraham I if (!link) 9381d7cb11eSKishon Vijay Abraham I dev_dbg(dev, "failed to create device link to %s\n", 939987351e1SAlexandre Torgue dev_name(phy->dev.parent)); 940987351e1SAlexandre Torgue 9416be109b3SArun Ramamurthy return phy; 9426be109b3SArun Ramamurthy } 9436be109b3SArun Ramamurthy EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index); 9446be109b3SArun Ramamurthy 9456be109b3SArun Ramamurthy /** 946ff764963SKishon Vijay Abraham I * phy_create() - create a new phy 947ff764963SKishon Vijay Abraham I * @dev: device that is creating the new phy 948f0ed8176SKishon Vijay Abraham I * @node: device node of the phy 949ff764963SKishon Vijay Abraham I * @ops: function pointers for performing phy operations 950ff764963SKishon Vijay Abraham I * 951ff764963SKishon Vijay Abraham I * Called to create a phy using phy framework. 952ff764963SKishon Vijay Abraham I */ 953f0ed8176SKishon Vijay Abraham I struct phy *phy_create(struct device *dev, struct device_node *node, 954dbc98635SHeikki Krogerus const struct phy_ops *ops) 955ff764963SKishon Vijay Abraham I { 956ff764963SKishon Vijay Abraham I int ret; 957ff764963SKishon Vijay Abraham I int id; 958ff764963SKishon Vijay Abraham I struct phy *phy; 959ff764963SKishon Vijay Abraham I 96052797d29SDan Carpenter if (WARN_ON(!dev)) 96152797d29SDan Carpenter return ERR_PTR(-EINVAL); 962ff764963SKishon Vijay Abraham I 963ff764963SKishon Vijay Abraham I phy = kzalloc(sizeof(*phy), GFP_KERNEL); 96452797d29SDan Carpenter if (!phy) 96552797d29SDan Carpenter return ERR_PTR(-ENOMEM); 966ff764963SKishon Vijay Abraham I 967ff764963SKishon Vijay Abraham I id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL); 968ff764963SKishon Vijay Abraham I if (id < 0) { 969ff764963SKishon Vijay Abraham I dev_err(dev, "unable to get id\n"); 970ff764963SKishon Vijay Abraham I ret = id; 97152797d29SDan Carpenter goto free_phy; 972ff764963SKishon Vijay Abraham I } 973ff764963SKishon Vijay Abraham I 974ff764963SKishon Vijay Abraham I device_initialize(&phy->dev); 975ff764963SKishon Vijay Abraham I mutex_init(&phy->mutex); 976ff764963SKishon Vijay Abraham I 977ff764963SKishon Vijay Abraham I phy->dev.class = phy_class; 978ff764963SKishon Vijay Abraham I phy->dev.parent = dev; 979f0ed8176SKishon Vijay Abraham I phy->dev.of_node = node ?: dev->of_node; 980ff764963SKishon Vijay Abraham I phy->id = id; 981ff764963SKishon Vijay Abraham I phy->ops = ops; 982ff764963SKishon Vijay Abraham I 983ff764963SKishon Vijay Abraham I ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); 984ff764963SKishon Vijay Abraham I if (ret) 98552797d29SDan Carpenter goto put_dev; 986ff764963SKishon Vijay Abraham I 98787006dd6SDmitry Torokhov /* phy-supply */ 98887006dd6SDmitry Torokhov phy->pwr = regulator_get_optional(&phy->dev, "phy"); 98987006dd6SDmitry Torokhov if (IS_ERR(phy->pwr)) { 99087006dd6SDmitry Torokhov ret = PTR_ERR(phy->pwr); 99187006dd6SDmitry Torokhov if (ret == -EPROBE_DEFER) 99287006dd6SDmitry Torokhov goto put_dev; 99387006dd6SDmitry Torokhov 99487006dd6SDmitry Torokhov phy->pwr = NULL; 99587006dd6SDmitry Torokhov } 99687006dd6SDmitry Torokhov 997ff764963SKishon Vijay Abraham I ret = device_add(&phy->dev); 998ff764963SKishon Vijay Abraham I if (ret) 99952797d29SDan Carpenter goto put_dev; 1000ff764963SKishon Vijay Abraham I 1001ff764963SKishon Vijay Abraham I if (pm_runtime_enabled(dev)) { 1002ff764963SKishon Vijay Abraham I pm_runtime_enable(&phy->dev); 1003ff764963SKishon Vijay Abraham I pm_runtime_no_callbacks(&phy->dev); 1004ff764963SKishon Vijay Abraham I } 1005ff764963SKishon Vijay Abraham I 100691694772SChunfeng Yun phy->debugfs = debugfs_create_dir(dev_name(&phy->dev), phy_debugfs_root); 100791694772SChunfeng Yun 1008ff764963SKishon Vijay Abraham I return phy; 1009ff764963SKishon Vijay Abraham I 101052797d29SDan Carpenter put_dev: 1011e73b49f1SRoger Quadros put_device(&phy->dev); /* calls phy_release() which frees resources */ 1012e73b49f1SRoger Quadros return ERR_PTR(ret); 1013e73b49f1SRoger Quadros 101452797d29SDan Carpenter free_phy: 1015ff764963SKishon Vijay Abraham I kfree(phy); 1016ff764963SKishon Vijay Abraham I return ERR_PTR(ret); 1017ff764963SKishon Vijay Abraham I } 1018ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_create); 1019ff764963SKishon Vijay Abraham I 1020ff764963SKishon Vijay Abraham I /** 1021ff764963SKishon Vijay Abraham I * devm_phy_create() - create a new phy 1022ff764963SKishon Vijay Abraham I * @dev: device that is creating the new phy 1023f0ed8176SKishon Vijay Abraham I * @node: device node of the phy 1024ff764963SKishon Vijay Abraham I * @ops: function pointers for performing phy operations 1025ff764963SKishon Vijay Abraham I * 1026ff764963SKishon Vijay Abraham I * Creates a new PHY device adding it to the PHY class. 1027ff764963SKishon Vijay Abraham I * While at that, it also associates the device with the phy using devres. 1028ff764963SKishon Vijay Abraham I * On driver detach, release function is invoked on the devres data, 1029ff764963SKishon Vijay Abraham I * then, devres data is freed. 1030ff764963SKishon Vijay Abraham I */ 1031f0ed8176SKishon Vijay Abraham I struct phy *devm_phy_create(struct device *dev, struct device_node *node, 1032dbc98635SHeikki Krogerus const struct phy_ops *ops) 1033ff764963SKishon Vijay Abraham I { 1034ff764963SKishon Vijay Abraham I struct phy **ptr, *phy; 1035ff764963SKishon Vijay Abraham I 1036ff764963SKishon Vijay Abraham I ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); 1037ff764963SKishon Vijay Abraham I if (!ptr) 1038ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 1039ff764963SKishon Vijay Abraham I 1040dbc98635SHeikki Krogerus phy = phy_create(dev, node, ops); 1041ff764963SKishon Vijay Abraham I if (!IS_ERR(phy)) { 1042ff764963SKishon Vijay Abraham I *ptr = phy; 1043ff764963SKishon Vijay Abraham I devres_add(dev, ptr); 1044ff764963SKishon Vijay Abraham I } else { 1045ff764963SKishon Vijay Abraham I devres_free(ptr); 1046ff764963SKishon Vijay Abraham I } 1047ff764963SKishon Vijay Abraham I 1048ff764963SKishon Vijay Abraham I return phy; 1049ff764963SKishon Vijay Abraham I } 1050ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_create); 1051ff764963SKishon Vijay Abraham I 1052ff764963SKishon Vijay Abraham I /** 1053ff764963SKishon Vijay Abraham I * phy_destroy() - destroy the phy 1054ff764963SKishon Vijay Abraham I * @phy: the phy to be destroyed 1055ff764963SKishon Vijay Abraham I * 1056ff764963SKishon Vijay Abraham I * Called to destroy the phy. 1057ff764963SKishon Vijay Abraham I */ 1058ff764963SKishon Vijay Abraham I void phy_destroy(struct phy *phy) 1059ff764963SKishon Vijay Abraham I { 1060ff764963SKishon Vijay Abraham I pm_runtime_disable(&phy->dev); 1061ff764963SKishon Vijay Abraham I device_unregister(&phy->dev); 1062ff764963SKishon Vijay Abraham I } 1063ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_destroy); 1064ff764963SKishon Vijay Abraham I 1065ff764963SKishon Vijay Abraham I /** 1066ff764963SKishon Vijay Abraham I * devm_phy_destroy() - destroy the PHY 1067ff764963SKishon Vijay Abraham I * @dev: device that wants to release this phy 1068ff764963SKishon Vijay Abraham I * @phy: the phy returned by devm_phy_get() 1069ff764963SKishon Vijay Abraham I * 1070ff764963SKishon Vijay Abraham I * destroys the devres associated with this phy and invokes phy_destroy 1071ff764963SKishon Vijay Abraham I * to destroy the phy. 1072ff764963SKishon Vijay Abraham I */ 1073ff764963SKishon Vijay Abraham I void devm_phy_destroy(struct device *dev, struct phy *phy) 1074ff764963SKishon Vijay Abraham I { 1075ff764963SKishon Vijay Abraham I int r; 1076ff764963SKishon Vijay Abraham I 1077ff764963SKishon Vijay Abraham I r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy); 1078ff764963SKishon Vijay Abraham I dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 1079ff764963SKishon Vijay Abraham I } 1080ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_destroy); 1081ff764963SKishon Vijay Abraham I 1082ff764963SKishon Vijay Abraham I /** 1083ff764963SKishon Vijay Abraham I * __of_phy_provider_register() - create/register phy provider with the framework 1084ff764963SKishon Vijay Abraham I * @dev: struct device of the phy provider 10851140f7c8SThierry Reding * @children: device node containing children (if different from dev->of_node) 1086ff764963SKishon Vijay Abraham I * @owner: the module owner containing of_xlate 1087ff764963SKishon Vijay Abraham I * @of_xlate: function pointer to obtain phy instance from phy provider 1088ff764963SKishon Vijay Abraham I * 1089ff764963SKishon Vijay Abraham I * Creates struct phy_provider from dev and of_xlate function pointer. 1090ff764963SKishon Vijay Abraham I * This is used in the case of dt boot for finding the phy instance from 1091ff764963SKishon Vijay Abraham I * phy provider. 10921140f7c8SThierry Reding * 10931140f7c8SThierry Reding * If the PHY provider doesn't nest children directly but uses a separate 10941140f7c8SThierry Reding * child node to contain the individual children, the @children parameter 10951140f7c8SThierry Reding * can be used to override the default. If NULL, the default (dev->of_node) 10961140f7c8SThierry Reding * will be used. If non-NULL, the device node must be a child (or further 10971140f7c8SThierry Reding * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL 10981140f7c8SThierry Reding * error code is returned. 1099ff764963SKishon Vijay Abraham I */ 1100ff764963SKishon Vijay Abraham I struct phy_provider *__of_phy_provider_register(struct device *dev, 11011140f7c8SThierry Reding struct device_node *children, struct module *owner, 11021140f7c8SThierry Reding struct phy * (*of_xlate)(struct device *dev, 1103ff764963SKishon Vijay Abraham I struct of_phandle_args *args)) 1104ff764963SKishon Vijay Abraham I { 1105ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider; 1106ff764963SKishon Vijay Abraham I 11071140f7c8SThierry Reding /* 11081140f7c8SThierry Reding * If specified, the device node containing the children must itself 11091140f7c8SThierry Reding * be the provider's device node or a child (or further descendant) 11101140f7c8SThierry Reding * thereof. 11111140f7c8SThierry Reding */ 11121140f7c8SThierry Reding if (children) { 11131140f7c8SThierry Reding struct device_node *parent = of_node_get(children), *next; 11141140f7c8SThierry Reding 11151140f7c8SThierry Reding while (parent) { 11161140f7c8SThierry Reding if (parent == dev->of_node) 11171140f7c8SThierry Reding break; 11181140f7c8SThierry Reding 11191140f7c8SThierry Reding next = of_get_parent(parent); 11201140f7c8SThierry Reding of_node_put(parent); 11211140f7c8SThierry Reding parent = next; 11221140f7c8SThierry Reding } 11231140f7c8SThierry Reding 11241140f7c8SThierry Reding if (!parent) 11251140f7c8SThierry Reding return ERR_PTR(-EINVAL); 11261140f7c8SThierry Reding 11271140f7c8SThierry Reding of_node_put(parent); 11281140f7c8SThierry Reding } else { 11291140f7c8SThierry Reding children = dev->of_node; 11301140f7c8SThierry Reding } 11311140f7c8SThierry Reding 1132ff764963SKishon Vijay Abraham I phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL); 1133ff764963SKishon Vijay Abraham I if (!phy_provider) 1134ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 1135ff764963SKishon Vijay Abraham I 1136ff764963SKishon Vijay Abraham I phy_provider->dev = dev; 11371140f7c8SThierry Reding phy_provider->children = of_node_get(children); 1138ff764963SKishon Vijay Abraham I phy_provider->owner = owner; 1139ff764963SKishon Vijay Abraham I phy_provider->of_xlate = of_xlate; 1140ff764963SKishon Vijay Abraham I 1141ff764963SKishon Vijay Abraham I mutex_lock(&phy_provider_mutex); 1142ff764963SKishon Vijay Abraham I list_add_tail(&phy_provider->list, &phy_provider_list); 1143ff764963SKishon Vijay Abraham I mutex_unlock(&phy_provider_mutex); 1144ff764963SKishon Vijay Abraham I 1145ff764963SKishon Vijay Abraham I return phy_provider; 1146ff764963SKishon Vijay Abraham I } 1147ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__of_phy_provider_register); 1148ff764963SKishon Vijay Abraham I 1149ff764963SKishon Vijay Abraham I /** 1150ff764963SKishon Vijay Abraham I * __devm_of_phy_provider_register() - create/register phy provider with the 1151ff764963SKishon Vijay Abraham I * framework 1152ff764963SKishon Vijay Abraham I * @dev: struct device of the phy provider 1153aad075c1SVinod Koul * @children: device node containing children (if different from dev->of_node) 1154ff764963SKishon Vijay Abraham I * @owner: the module owner containing of_xlate 1155ff764963SKishon Vijay Abraham I * @of_xlate: function pointer to obtain phy instance from phy provider 1156ff764963SKishon Vijay Abraham I * 1157ff764963SKishon Vijay Abraham I * Creates struct phy_provider from dev and of_xlate function pointer. 1158ff764963SKishon Vijay Abraham I * This is used in the case of dt boot for finding the phy instance from 1159ff764963SKishon Vijay Abraham I * phy provider. While at that, it also associates the device with the 1160ff764963SKishon Vijay Abraham I * phy provider using devres. On driver detach, release function is invoked 1161ff764963SKishon Vijay Abraham I * on the devres data, then, devres data is freed. 1162ff764963SKishon Vijay Abraham I */ 1163ff764963SKishon Vijay Abraham I struct phy_provider *__devm_of_phy_provider_register(struct device *dev, 11641140f7c8SThierry Reding struct device_node *children, struct module *owner, 11651140f7c8SThierry Reding struct phy * (*of_xlate)(struct device *dev, 1166ff764963SKishon Vijay Abraham I struct of_phandle_args *args)) 1167ff764963SKishon Vijay Abraham I { 1168ff764963SKishon Vijay Abraham I struct phy_provider **ptr, *phy_provider; 1169ff764963SKishon Vijay Abraham I 1170ff764963SKishon Vijay Abraham I ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); 1171ff764963SKishon Vijay Abraham I if (!ptr) 1172ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 1173ff764963SKishon Vijay Abraham I 11741140f7c8SThierry Reding phy_provider = __of_phy_provider_register(dev, children, owner, 11751140f7c8SThierry Reding of_xlate); 1176ff764963SKishon Vijay Abraham I if (!IS_ERR(phy_provider)) { 1177ff764963SKishon Vijay Abraham I *ptr = phy_provider; 1178ff764963SKishon Vijay Abraham I devres_add(dev, ptr); 1179ff764963SKishon Vijay Abraham I } else { 1180ff764963SKishon Vijay Abraham I devres_free(ptr); 1181ff764963SKishon Vijay Abraham I } 1182ff764963SKishon Vijay Abraham I 1183ff764963SKishon Vijay Abraham I return phy_provider; 1184ff764963SKishon Vijay Abraham I } 1185ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register); 1186ff764963SKishon Vijay Abraham I 1187ff764963SKishon Vijay Abraham I /** 1188ff764963SKishon Vijay Abraham I * of_phy_provider_unregister() - unregister phy provider from the framework 1189ff764963SKishon Vijay Abraham I * @phy_provider: phy provider returned by of_phy_provider_register() 1190ff764963SKishon Vijay Abraham I * 1191ff764963SKishon Vijay Abraham I * Removes the phy_provider created using of_phy_provider_register(). 1192ff764963SKishon Vijay Abraham I */ 1193ff764963SKishon Vijay Abraham I void of_phy_provider_unregister(struct phy_provider *phy_provider) 1194ff764963SKishon Vijay Abraham I { 1195ff764963SKishon Vijay Abraham I if (IS_ERR(phy_provider)) 1196ff764963SKishon Vijay Abraham I return; 1197ff764963SKishon Vijay Abraham I 1198ff764963SKishon Vijay Abraham I mutex_lock(&phy_provider_mutex); 1199ff764963SKishon Vijay Abraham I list_del(&phy_provider->list); 12001140f7c8SThierry Reding of_node_put(phy_provider->children); 1201ff764963SKishon Vijay Abraham I kfree(phy_provider); 1202ff764963SKishon Vijay Abraham I mutex_unlock(&phy_provider_mutex); 1203ff764963SKishon Vijay Abraham I } 1204ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_provider_unregister); 1205ff764963SKishon Vijay Abraham I 1206ff764963SKishon Vijay Abraham I /** 1207ff764963SKishon Vijay Abraham I * devm_of_phy_provider_unregister() - remove phy provider from the framework 1208ff764963SKishon Vijay Abraham I * @dev: struct device of the phy provider 1209aad075c1SVinod Koul * @phy_provider: phy provider returned by of_phy_provider_register() 1210ff764963SKishon Vijay Abraham I * 1211ff764963SKishon Vijay Abraham I * destroys the devres associated with this phy provider and invokes 1212ff764963SKishon Vijay Abraham I * of_phy_provider_unregister to unregister the phy provider. 1213ff764963SKishon Vijay Abraham I */ 1214ff764963SKishon Vijay Abraham I void devm_of_phy_provider_unregister(struct device *dev, 1215b555f35fSVinod Koul struct phy_provider *phy_provider) 1216b555f35fSVinod Koul { 1217ff764963SKishon Vijay Abraham I int r; 1218ff764963SKishon Vijay Abraham I 1219*216b9f83SZijun Hu r = devres_release(dev, devm_phy_provider_release, devm_phy_match, 1220ff764963SKishon Vijay Abraham I phy_provider); 1221ff764963SKishon Vijay Abraham I dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n"); 1222ff764963SKishon Vijay Abraham I } 1223ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister); 1224ff764963SKishon Vijay Abraham I 1225ff764963SKishon Vijay Abraham I /** 1226ff764963SKishon Vijay Abraham I * phy_release() - release the phy 1227ff764963SKishon Vijay Abraham I * @dev: the dev member within phy 1228ff764963SKishon Vijay Abraham I * 1229ff764963SKishon Vijay Abraham I * When the last reference to the device is removed, it is called 1230ff764963SKishon Vijay Abraham I * from the embedded kobject as release method. 1231ff764963SKishon Vijay Abraham I */ 1232ff764963SKishon Vijay Abraham I static void phy_release(struct device *dev) 1233ff764963SKishon Vijay Abraham I { 1234ff764963SKishon Vijay Abraham I struct phy *phy; 1235ff764963SKishon Vijay Abraham I 1236ff764963SKishon Vijay Abraham I phy = to_phy(dev); 1237ff764963SKishon Vijay Abraham I dev_vdbg(dev, "releasing '%s'\n", dev_name(dev)); 123891694772SChunfeng Yun debugfs_remove_recursive(phy->debugfs); 12393be88125SRoger Quadros regulator_put(phy->pwr); 1240e73b49f1SRoger Quadros ida_simple_remove(&phy_ida, phy->id); 1241ff764963SKishon Vijay Abraham I kfree(phy); 1242ff764963SKishon Vijay Abraham I } 1243ff764963SKishon Vijay Abraham I 1244ff764963SKishon Vijay Abraham I static int __init phy_core_init(void) 1245ff764963SKishon Vijay Abraham I { 12461aaba11dSGreg Kroah-Hartman phy_class = class_create("phy"); 1247ff764963SKishon Vijay Abraham I if (IS_ERR(phy_class)) { 1248ff764963SKishon Vijay Abraham I pr_err("failed to create phy class --> %ld\n", 1249ff764963SKishon Vijay Abraham I PTR_ERR(phy_class)); 1250ff764963SKishon Vijay Abraham I return PTR_ERR(phy_class); 1251ff764963SKishon Vijay Abraham I } 1252ff764963SKishon Vijay Abraham I 1253ff764963SKishon Vijay Abraham I phy_class->dev_release = phy_release; 1254ff764963SKishon Vijay Abraham I 125591694772SChunfeng Yun phy_debugfs_root = debugfs_create_dir("phy", NULL); 125691694772SChunfeng Yun 1257ff764963SKishon Vijay Abraham I return 0; 1258ff764963SKishon Vijay Abraham I } 1259cc013c28SPaul Gortmaker device_initcall(phy_core_init); 126091694772SChunfeng Yun 126191694772SChunfeng Yun static void __exit phy_core_exit(void) 126291694772SChunfeng Yun { 126391694772SChunfeng Yun debugfs_remove_recursive(phy_debugfs_root); 126491694772SChunfeng Yun class_destroy(phy_class); 126591694772SChunfeng Yun } 126691694772SChunfeng Yun module_exit(phy_core_exit); 1267