1ff764963SKishon Vijay Abraham I /* 2ff764963SKishon Vijay Abraham I * phy-core.c -- Generic Phy framework. 3ff764963SKishon Vijay Abraham I * 4ff764963SKishon Vijay Abraham I * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com 5ff764963SKishon Vijay Abraham I * 6ff764963SKishon Vijay Abraham I * Author: Kishon Vijay Abraham I <kishon@ti.com> 7ff764963SKishon Vijay Abraham I * 8ff764963SKishon Vijay Abraham I * This program is free software; you can redistribute it and/or modify it 9ff764963SKishon Vijay Abraham I * under the terms of the GNU General Public License as published by the 10ff764963SKishon Vijay Abraham I * Free Software Foundation; either version 2 of the License, or (at your 11ff764963SKishon Vijay Abraham I * option) any later version. 12ff764963SKishon Vijay Abraham I */ 13ff764963SKishon Vijay Abraham I 14ff764963SKishon Vijay Abraham I #include <linux/kernel.h> 15ff764963SKishon Vijay Abraham I #include <linux/export.h> 16ff764963SKishon Vijay Abraham I #include <linux/module.h> 17ff764963SKishon Vijay Abraham I #include <linux/err.h> 18ff764963SKishon Vijay Abraham I #include <linux/device.h> 19ff764963SKishon Vijay Abraham I #include <linux/slab.h> 20ff764963SKishon Vijay Abraham I #include <linux/of.h> 21ff764963SKishon Vijay Abraham I #include <linux/phy/phy.h> 22ff764963SKishon Vijay Abraham I #include <linux/idr.h> 23ff764963SKishon Vijay Abraham I #include <linux/pm_runtime.h> 243be88125SRoger Quadros #include <linux/regulator/consumer.h> 25ff764963SKishon Vijay Abraham I 26ff764963SKishon Vijay Abraham I static struct class *phy_class; 27ff764963SKishon Vijay Abraham I static DEFINE_MUTEX(phy_provider_mutex); 28ff764963SKishon Vijay Abraham I static LIST_HEAD(phy_provider_list); 29b7bc15b9SHeikki Krogerus static LIST_HEAD(phys); 30ff764963SKishon Vijay Abraham I static DEFINE_IDA(phy_ida); 31ff764963SKishon Vijay Abraham I 32ff764963SKishon Vijay Abraham I static void devm_phy_release(struct device *dev, void *res) 33ff764963SKishon Vijay Abraham I { 34ff764963SKishon Vijay Abraham I struct phy *phy = *(struct phy **)res; 35ff764963SKishon Vijay Abraham I 36ff764963SKishon Vijay Abraham I phy_put(phy); 37ff764963SKishon Vijay Abraham I } 38ff764963SKishon Vijay Abraham I 39ff764963SKishon Vijay Abraham I static void devm_phy_provider_release(struct device *dev, void *res) 40ff764963SKishon Vijay Abraham I { 41ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider = *(struct phy_provider **)res; 42ff764963SKishon Vijay Abraham I 43ff764963SKishon Vijay Abraham I of_phy_provider_unregister(phy_provider); 44ff764963SKishon Vijay Abraham I } 45ff764963SKishon Vijay Abraham I 46ff764963SKishon Vijay Abraham I static void devm_phy_consume(struct device *dev, void *res) 47ff764963SKishon Vijay Abraham I { 48ff764963SKishon Vijay Abraham I struct phy *phy = *(struct phy **)res; 49ff764963SKishon Vijay Abraham I 50ff764963SKishon Vijay Abraham I phy_destroy(phy); 51ff764963SKishon Vijay Abraham I } 52ff764963SKishon Vijay Abraham I 53ff764963SKishon Vijay Abraham I static int devm_phy_match(struct device *dev, void *res, void *match_data) 54ff764963SKishon Vijay Abraham I { 552f1bce48SThierry Reding struct phy **phy = res; 562f1bce48SThierry Reding 572f1bce48SThierry Reding return *phy == match_data; 58ff764963SKishon Vijay Abraham I } 59ff764963SKishon Vijay Abraham I 60b7bc15b9SHeikki Krogerus /** 61b7bc15b9SHeikki Krogerus * phy_create_lookup() - allocate and register PHY/device association 62b7bc15b9SHeikki Krogerus * @phy: the phy of the association 63b7bc15b9SHeikki Krogerus * @con_id: connection ID string on device 64b7bc15b9SHeikki Krogerus * @dev_id: the device of the association 65b7bc15b9SHeikki Krogerus * 66b7bc15b9SHeikki Krogerus * Creates and registers phy_lookup entry. 67b7bc15b9SHeikki Krogerus */ 68b7bc15b9SHeikki Krogerus int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) 69b7bc15b9SHeikki Krogerus { 70b7bc15b9SHeikki Krogerus struct phy_lookup *pl; 71b7bc15b9SHeikki Krogerus 72b7bc15b9SHeikki Krogerus if (!phy || !dev_id || !con_id) 73b7bc15b9SHeikki Krogerus return -EINVAL; 74b7bc15b9SHeikki Krogerus 75b7bc15b9SHeikki Krogerus pl = kzalloc(sizeof(*pl), GFP_KERNEL); 76b7bc15b9SHeikki Krogerus if (!pl) 77b7bc15b9SHeikki Krogerus return -ENOMEM; 78b7bc15b9SHeikki Krogerus 79b7bc15b9SHeikki Krogerus pl->dev_id = dev_id; 80b7bc15b9SHeikki Krogerus pl->con_id = con_id; 81b7bc15b9SHeikki Krogerus pl->phy = phy; 82b7bc15b9SHeikki Krogerus 83b7bc15b9SHeikki Krogerus mutex_lock(&phy_provider_mutex); 84b7bc15b9SHeikki Krogerus list_add_tail(&pl->node, &phys); 85b7bc15b9SHeikki Krogerus mutex_unlock(&phy_provider_mutex); 86b7bc15b9SHeikki Krogerus 87b7bc15b9SHeikki Krogerus return 0; 88b7bc15b9SHeikki Krogerus } 89b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_create_lookup); 90b7bc15b9SHeikki Krogerus 91b7bc15b9SHeikki Krogerus /** 92b7bc15b9SHeikki Krogerus * phy_remove_lookup() - find and remove PHY/device association 93b7bc15b9SHeikki Krogerus * @phy: the phy of the association 94b7bc15b9SHeikki Krogerus * @con_id: connection ID string on device 95b7bc15b9SHeikki Krogerus * @dev_id: the device of the association 96b7bc15b9SHeikki Krogerus * 97b7bc15b9SHeikki Krogerus * Finds and unregisters phy_lookup entry that was created with 98b7bc15b9SHeikki Krogerus * phy_create_lookup(). 99b7bc15b9SHeikki Krogerus */ 100b7bc15b9SHeikki Krogerus void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id) 101b7bc15b9SHeikki Krogerus { 102b7bc15b9SHeikki Krogerus struct phy_lookup *pl; 103b7bc15b9SHeikki Krogerus 104b7bc15b9SHeikki Krogerus if (!phy || !dev_id || !con_id) 105b7bc15b9SHeikki Krogerus return; 106b7bc15b9SHeikki Krogerus 107b7bc15b9SHeikki Krogerus mutex_lock(&phy_provider_mutex); 108b7bc15b9SHeikki Krogerus list_for_each_entry(pl, &phys, node) 109b7bc15b9SHeikki Krogerus if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) && 110b7bc15b9SHeikki Krogerus !strcmp(pl->con_id, con_id)) { 111b7bc15b9SHeikki Krogerus list_del(&pl->node); 112b7bc15b9SHeikki Krogerus kfree(pl); 113b7bc15b9SHeikki Krogerus break; 114b7bc15b9SHeikki Krogerus } 115b7bc15b9SHeikki Krogerus mutex_unlock(&phy_provider_mutex); 116b7bc15b9SHeikki Krogerus } 117b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_remove_lookup); 118b7bc15b9SHeikki Krogerus 119b7bc15b9SHeikki Krogerus static struct phy *phy_find(struct device *dev, const char *con_id) 120b7bc15b9SHeikki Krogerus { 121b7bc15b9SHeikki Krogerus const char *dev_id = dev_name(dev); 122b7bc15b9SHeikki Krogerus struct phy_lookup *p, *pl = NULL; 123b7bc15b9SHeikki Krogerus 124b7bc15b9SHeikki Krogerus mutex_lock(&phy_provider_mutex); 125b7bc15b9SHeikki Krogerus list_for_each_entry(p, &phys, node) 126b7bc15b9SHeikki Krogerus if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) { 127b7bc15b9SHeikki Krogerus pl = p; 128b7bc15b9SHeikki Krogerus break; 129b7bc15b9SHeikki Krogerus } 130b7bc15b9SHeikki Krogerus mutex_unlock(&phy_provider_mutex); 131b7bc15b9SHeikki Krogerus 132dbc98635SHeikki Krogerus return pl ? pl->phy : ERR_PTR(-ENODEV); 133b7bc15b9SHeikki Krogerus } 134b7bc15b9SHeikki Krogerus 135ff764963SKishon Vijay Abraham I static struct phy_provider *of_phy_provider_lookup(struct device_node *node) 136ff764963SKishon Vijay Abraham I { 137ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider; 1382a4c3701SKishon Vijay Abraham I struct device_node *child; 139ff764963SKishon Vijay Abraham I 140ff764963SKishon Vijay Abraham I list_for_each_entry(phy_provider, &phy_provider_list, list) { 141ff764963SKishon Vijay Abraham I if (phy_provider->dev->of_node == node) 142ff764963SKishon Vijay Abraham I return phy_provider; 1432a4c3701SKishon Vijay Abraham I 1441140f7c8SThierry Reding for_each_child_of_node(phy_provider->children, child) 1452a4c3701SKishon Vijay Abraham I if (child == node) 1462a4c3701SKishon Vijay Abraham I return phy_provider; 147ff764963SKishon Vijay Abraham I } 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 156ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 157ff764963SKishon Vijay Abraham I return -ENOTSUPP; 158ff764963SKishon Vijay Abraham I 159cedb7f89SFelipe Balbi ret = pm_runtime_get(&phy->dev); 160cedb7f89SFelipe Balbi if (ret < 0 && ret != -EINPROGRESS) 161cedb7f89SFelipe Balbi pm_runtime_put_noidle(&phy->dev); 162cedb7f89SFelipe Balbi 163cedb7f89SFelipe Balbi return ret; 164ff764963SKishon Vijay Abraham I } 165ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get); 166ff764963SKishon Vijay Abraham I 167ff764963SKishon Vijay Abraham I int phy_pm_runtime_get_sync(struct phy *phy) 168ff764963SKishon Vijay Abraham I { 169cedb7f89SFelipe Balbi int ret; 170cedb7f89SFelipe Balbi 171ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 172ff764963SKishon Vijay Abraham I return -ENOTSUPP; 173ff764963SKishon Vijay Abraham I 174cedb7f89SFelipe Balbi ret = pm_runtime_get_sync(&phy->dev); 175cedb7f89SFelipe Balbi if (ret < 0) 176cedb7f89SFelipe Balbi pm_runtime_put_sync(&phy->dev); 177cedb7f89SFelipe Balbi 178cedb7f89SFelipe Balbi return ret; 179ff764963SKishon Vijay Abraham I } 180ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync); 181ff764963SKishon Vijay Abraham I 182ff764963SKishon Vijay Abraham I int phy_pm_runtime_put(struct phy *phy) 183ff764963SKishon Vijay Abraham I { 184ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 185ff764963SKishon Vijay Abraham I return -ENOTSUPP; 186ff764963SKishon Vijay Abraham I 187ff764963SKishon Vijay Abraham I return pm_runtime_put(&phy->dev); 188ff764963SKishon Vijay Abraham I } 189ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put); 190ff764963SKishon Vijay Abraham I 191ff764963SKishon Vijay Abraham I int phy_pm_runtime_put_sync(struct phy *phy) 192ff764963SKishon Vijay Abraham I { 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_sync(&phy->dev); 197ff764963SKishon Vijay Abraham I } 198ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync); 199ff764963SKishon Vijay Abraham I 200ff764963SKishon Vijay Abraham I void phy_pm_runtime_allow(struct phy *phy) 201ff764963SKishon Vijay Abraham I { 202ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 203ff764963SKishon Vijay Abraham I return; 204ff764963SKishon Vijay Abraham I 205ff764963SKishon Vijay Abraham I pm_runtime_allow(&phy->dev); 206ff764963SKishon Vijay Abraham I } 207ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_allow); 208ff764963SKishon Vijay Abraham I 209ff764963SKishon Vijay Abraham I void phy_pm_runtime_forbid(struct phy *phy) 210ff764963SKishon Vijay Abraham I { 211ff764963SKishon Vijay Abraham I if (!pm_runtime_enabled(&phy->dev)) 212ff764963SKishon Vijay Abraham I return; 213ff764963SKishon Vijay Abraham I 214ff764963SKishon Vijay Abraham I pm_runtime_forbid(&phy->dev); 215ff764963SKishon Vijay Abraham I } 216ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid); 217ff764963SKishon Vijay Abraham I 218ff764963SKishon Vijay Abraham I int phy_init(struct phy *phy) 219ff764963SKishon Vijay Abraham I { 220ff764963SKishon Vijay Abraham I int ret; 221ff764963SKishon Vijay Abraham I 22204c2facaSAndrew Lunn if (!phy) 22304c2facaSAndrew Lunn return 0; 22404c2facaSAndrew Lunn 225ff764963SKishon Vijay Abraham I ret = phy_pm_runtime_get_sync(phy); 226ff764963SKishon Vijay Abraham I if (ret < 0 && ret != -ENOTSUPP) 227ff764963SKishon Vijay Abraham I return ret; 228736b67a3SAxel Lin ret = 0; /* Override possible ret == -ENOTSUPP */ 229ff764963SKishon Vijay Abraham I 230ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 231637d378cSKishon Vijay Abraham I if (phy->init_count == 0 && phy->ops->init) { 232ff764963SKishon Vijay Abraham I ret = phy->ops->init(phy); 233ff764963SKishon Vijay Abraham I if (ret < 0) { 234ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy init failed --> %d\n", ret); 235ff764963SKishon Vijay Abraham I goto out; 236ff764963SKishon Vijay Abraham I } 237ff764963SKishon Vijay Abraham I } 238637d378cSKishon Vijay Abraham I ++phy->init_count; 239ff764963SKishon Vijay Abraham I 240ff764963SKishon Vijay Abraham I out: 241ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 242ff764963SKishon Vijay Abraham I phy_pm_runtime_put(phy); 243ff764963SKishon Vijay Abraham I return ret; 244ff764963SKishon Vijay Abraham I } 245ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_init); 246ff764963SKishon Vijay Abraham I 247ff764963SKishon Vijay Abraham I int phy_exit(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); 260637d378cSKishon Vijay Abraham I if (phy->init_count == 1 && phy->ops->exit) { 261ff764963SKishon Vijay Abraham I ret = phy->ops->exit(phy); 262ff764963SKishon Vijay Abraham I if (ret < 0) { 263ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy exit failed --> %d\n", ret); 264ff764963SKishon Vijay Abraham I goto out; 265ff764963SKishon Vijay Abraham I } 266ff764963SKishon Vijay Abraham I } 267637d378cSKishon Vijay Abraham I --phy->init_count; 268ff764963SKishon Vijay Abraham I 269ff764963SKishon Vijay Abraham I out: 270ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 271ff764963SKishon Vijay Abraham I phy_pm_runtime_put(phy); 272ff764963SKishon Vijay Abraham I return ret; 273ff764963SKishon Vijay Abraham I } 274ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_exit); 275ff764963SKishon Vijay Abraham I 276ff764963SKishon Vijay Abraham I int phy_power_on(struct phy *phy) 277ff764963SKishon Vijay Abraham I { 278b82fcabeSShawn Lin int ret = 0; 279ff764963SKishon Vijay Abraham I 28004c2facaSAndrew Lunn if (!phy) 281b82fcabeSShawn Lin goto out; 28204c2facaSAndrew Lunn 2833be88125SRoger Quadros if (phy->pwr) { 2843be88125SRoger Quadros ret = regulator_enable(phy->pwr); 2853be88125SRoger Quadros if (ret) 286b82fcabeSShawn Lin goto out; 2873be88125SRoger Quadros } 2883be88125SRoger Quadros 289ff764963SKishon Vijay Abraham I ret = phy_pm_runtime_get_sync(phy); 290ff764963SKishon Vijay Abraham I if (ret < 0 && ret != -ENOTSUPP) 291b82fcabeSShawn Lin goto err_pm_sync; 292b82fcabeSShawn Lin 293736b67a3SAxel Lin ret = 0; /* Override possible ret == -ENOTSUPP */ 294ff764963SKishon Vijay Abraham I 295ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 296637d378cSKishon Vijay Abraham I if (phy->power_count == 0 && phy->ops->power_on) { 297ff764963SKishon Vijay Abraham I ret = phy->ops->power_on(phy); 298ff764963SKishon Vijay Abraham I if (ret < 0) { 299ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy poweron failed --> %d\n", ret); 300b82fcabeSShawn Lin goto err_pwr_on; 301ff764963SKishon Vijay Abraham I } 302ff764963SKishon Vijay Abraham I } 303637d378cSKishon Vijay Abraham I ++phy->power_count; 304637d378cSKishon Vijay Abraham I mutex_unlock(&phy->mutex); 305637d378cSKishon Vijay Abraham I return 0; 306ff764963SKishon Vijay Abraham I 307b82fcabeSShawn Lin err_pwr_on: 308ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 309637d378cSKishon Vijay Abraham I phy_pm_runtime_put_sync(phy); 310b82fcabeSShawn Lin err_pm_sync: 3113be88125SRoger Quadros if (phy->pwr) 3123be88125SRoger Quadros regulator_disable(phy->pwr); 313b82fcabeSShawn Lin out: 314ff764963SKishon Vijay Abraham I return ret; 315ff764963SKishon Vijay Abraham I } 316ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_on); 317ff764963SKishon Vijay Abraham I 318ff764963SKishon Vijay Abraham I int phy_power_off(struct phy *phy) 319ff764963SKishon Vijay Abraham I { 320d18c9604SKishon Vijay Abraham I int ret; 321ff764963SKishon Vijay Abraham I 32204c2facaSAndrew Lunn if (!phy) 32304c2facaSAndrew Lunn return 0; 32404c2facaSAndrew Lunn 325ff764963SKishon Vijay Abraham I mutex_lock(&phy->mutex); 326637d378cSKishon Vijay Abraham I if (phy->power_count == 1 && phy->ops->power_off) { 327ff764963SKishon Vijay Abraham I ret = phy->ops->power_off(phy); 328ff764963SKishon Vijay Abraham I if (ret < 0) { 329ff764963SKishon Vijay Abraham I dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret); 330637d378cSKishon Vijay Abraham I mutex_unlock(&phy->mutex); 331637d378cSKishon Vijay Abraham I return ret; 332ff764963SKishon Vijay Abraham I } 333ff764963SKishon Vijay Abraham I } 334637d378cSKishon Vijay Abraham I --phy->power_count; 335ff764963SKishon Vijay Abraham I mutex_unlock(&phy->mutex); 336ff764963SKishon Vijay Abraham I phy_pm_runtime_put(phy); 337ff764963SKishon Vijay Abraham I 3383be88125SRoger Quadros if (phy->pwr) 3393be88125SRoger Quadros regulator_disable(phy->pwr); 3403be88125SRoger Quadros 341637d378cSKishon Vijay Abraham I return 0; 342ff764963SKishon Vijay Abraham I } 343ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_off); 344ff764963SKishon Vijay Abraham I 345300eb013SDavid Lechner int phy_set_mode(struct phy *phy, enum phy_mode mode) 346300eb013SDavid Lechner { 347300eb013SDavid Lechner int ret; 348300eb013SDavid Lechner 349300eb013SDavid Lechner if (!phy || !phy->ops->set_mode) 350300eb013SDavid Lechner return 0; 351300eb013SDavid Lechner 352300eb013SDavid Lechner mutex_lock(&phy->mutex); 353300eb013SDavid Lechner ret = phy->ops->set_mode(phy, mode); 354*3b3cd24aSManu Gautam if (!ret) 355*3b3cd24aSManu Gautam phy->attrs.mode = mode; 356300eb013SDavid Lechner mutex_unlock(&phy->mutex); 357300eb013SDavid Lechner 358300eb013SDavid Lechner return ret; 359300eb013SDavid Lechner } 360300eb013SDavid Lechner EXPORT_SYMBOL_GPL(phy_set_mode); 361300eb013SDavid Lechner 362cac18ecbSRandy Li int phy_reset(struct phy *phy) 363cac18ecbSRandy Li { 364cac18ecbSRandy Li int ret; 365cac18ecbSRandy Li 366cac18ecbSRandy Li if (!phy || !phy->ops->reset) 367cac18ecbSRandy Li return 0; 368cac18ecbSRandy Li 369cac18ecbSRandy Li mutex_lock(&phy->mutex); 370cac18ecbSRandy Li ret = phy->ops->reset(phy); 371cac18ecbSRandy Li mutex_unlock(&phy->mutex); 372cac18ecbSRandy Li 373cac18ecbSRandy Li return ret; 374cac18ecbSRandy Li } 375cac18ecbSRandy Li EXPORT_SYMBOL_GPL(phy_reset); 376cac18ecbSRandy Li 37736914111SAndrzej Pietrasiewicz int phy_calibrate(struct phy *phy) 37836914111SAndrzej Pietrasiewicz { 37936914111SAndrzej Pietrasiewicz int ret; 38036914111SAndrzej Pietrasiewicz 38136914111SAndrzej Pietrasiewicz if (!phy || !phy->ops->calibrate) 38236914111SAndrzej Pietrasiewicz return 0; 38336914111SAndrzej Pietrasiewicz 38436914111SAndrzej Pietrasiewicz mutex_lock(&phy->mutex); 38536914111SAndrzej Pietrasiewicz ret = phy->ops->calibrate(phy); 38636914111SAndrzej Pietrasiewicz mutex_unlock(&phy->mutex); 38736914111SAndrzej Pietrasiewicz 38836914111SAndrzej Pietrasiewicz return ret; 38936914111SAndrzej Pietrasiewicz } 39036914111SAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(phy_calibrate); 39136914111SAndrzej Pietrasiewicz 392ff764963SKishon Vijay Abraham I /** 3930b3f3b2cSKamil Debski * _of_phy_get() - lookup and obtain a reference to a phy by phandle 3940b3f3b2cSKamil Debski * @np: device_node for which to get the phy 395ff764963SKishon Vijay Abraham I * @index: the index of the phy 396ff764963SKishon Vijay Abraham I * 397ff764963SKishon Vijay Abraham I * Returns the phy associated with the given phandle value, 398ff764963SKishon Vijay Abraham I * after getting a refcount to it or -ENODEV if there is no such phy or 399ff764963SKishon Vijay Abraham I * -EPROBE_DEFER if there is a phandle to the phy, but the device is 400ff764963SKishon Vijay Abraham I * not yet loaded. This function uses of_xlate call back function provided 401ff764963SKishon Vijay Abraham I * while registering the phy_provider to find the phy instance. 402ff764963SKishon Vijay Abraham I */ 4030b3f3b2cSKamil Debski static struct phy *_of_phy_get(struct device_node *np, int index) 404ff764963SKishon Vijay Abraham I { 405ff764963SKishon Vijay Abraham I int ret; 406ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider; 407ff764963SKishon Vijay Abraham I struct phy *phy = NULL; 408ff764963SKishon Vijay Abraham I struct of_phandle_args args; 409ff764963SKishon Vijay Abraham I 4100b3f3b2cSKamil Debski ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", 411ff764963SKishon Vijay Abraham I index, &args); 4120b3f3b2cSKamil Debski if (ret) 413ff764963SKishon Vijay Abraham I return ERR_PTR(-ENODEV); 414ff764963SKishon Vijay Abraham I 415b7563e27SArnd Bergmann /* This phy type handled by the usb-phy subsystem for now */ 416b7563e27SArnd Bergmann if (of_device_is_compatible(args.np, "usb-nop-xceiv")) 417b7563e27SArnd Bergmann return ERR_PTR(-ENODEV); 418b7563e27SArnd Bergmann 419ff764963SKishon Vijay Abraham I mutex_lock(&phy_provider_mutex); 420ff764963SKishon Vijay Abraham I phy_provider = of_phy_provider_lookup(args.np); 421ff764963SKishon Vijay Abraham I if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) { 422ff764963SKishon Vijay Abraham I phy = ERR_PTR(-EPROBE_DEFER); 42333f434d2SAxel Lin goto out_unlock; 42433f434d2SAxel Lin } 42533f434d2SAxel Lin 42633f434d2SAxel Lin if (!of_device_is_available(args.np)) { 42733f434d2SAxel Lin dev_warn(phy_provider->dev, "Requested PHY is disabled\n"); 42833f434d2SAxel Lin phy = ERR_PTR(-ENODEV); 42933f434d2SAxel Lin goto out_put_module; 430ff764963SKishon Vijay Abraham I } 431ff764963SKishon Vijay Abraham I 432ff764963SKishon Vijay Abraham I phy = phy_provider->of_xlate(phy_provider->dev, &args); 43333f434d2SAxel Lin 43433f434d2SAxel Lin out_put_module: 435ff764963SKishon Vijay Abraham I module_put(phy_provider->owner); 436ff764963SKishon Vijay Abraham I 43733f434d2SAxel Lin out_unlock: 438ff764963SKishon Vijay Abraham I mutex_unlock(&phy_provider_mutex); 439ff764963SKishon Vijay Abraham I of_node_put(args.np); 440ff764963SKishon Vijay Abraham I 441ff764963SKishon Vijay Abraham I return phy; 442ff764963SKishon Vijay Abraham I } 443ff764963SKishon Vijay Abraham I 444ff764963SKishon Vijay Abraham I /** 4450b3f3b2cSKamil Debski * of_phy_get() - lookup and obtain a reference to a phy using a device_node. 4460b3f3b2cSKamil Debski * @np: device_node for which to get the phy 4470b3f3b2cSKamil Debski * @con_id: name of the phy from device's point of view 4480b3f3b2cSKamil Debski * 4490b3f3b2cSKamil Debski * Returns the phy driver, after getting a refcount to it; or 4500b3f3b2cSKamil Debski * -ENODEV if there is no such phy. The caller is responsible for 4510b3f3b2cSKamil Debski * calling phy_put() to release that count. 4520b3f3b2cSKamil Debski */ 4530b3f3b2cSKamil Debski struct phy *of_phy_get(struct device_node *np, const char *con_id) 4540b3f3b2cSKamil Debski { 4550b3f3b2cSKamil Debski struct phy *phy = NULL; 4560b3f3b2cSKamil Debski int index = 0; 4570b3f3b2cSKamil Debski 4580b3f3b2cSKamil Debski if (con_id) 4590b3f3b2cSKamil Debski index = of_property_match_string(np, "phy-names", con_id); 4600b3f3b2cSKamil Debski 4610b3f3b2cSKamil Debski phy = _of_phy_get(np, index); 4620b3f3b2cSKamil Debski if (IS_ERR(phy)) 4630b3f3b2cSKamil Debski return phy; 4640b3f3b2cSKamil Debski 4650b3f3b2cSKamil Debski if (!try_module_get(phy->ops->owner)) 4660b3f3b2cSKamil Debski return ERR_PTR(-EPROBE_DEFER); 4670b3f3b2cSKamil Debski 4680b3f3b2cSKamil Debski get_device(&phy->dev); 4690b3f3b2cSKamil Debski 4700b3f3b2cSKamil Debski return phy; 4710b3f3b2cSKamil Debski } 4720b3f3b2cSKamil Debski EXPORT_SYMBOL_GPL(of_phy_get); 4730b3f3b2cSKamil Debski 4740b3f3b2cSKamil Debski /** 475ff764963SKishon Vijay Abraham I * phy_put() - release the PHY 476ff764963SKishon Vijay Abraham I * @phy: the phy returned by phy_get() 477ff764963SKishon Vijay Abraham I * 478ff764963SKishon Vijay Abraham I * Releases a refcount the caller received from phy_get(). 479ff764963SKishon Vijay Abraham I */ 480ff764963SKishon Vijay Abraham I void phy_put(struct phy *phy) 481ff764963SKishon Vijay Abraham I { 48204c2facaSAndrew Lunn if (!phy || IS_ERR(phy)) 483ff764963SKishon Vijay Abraham I return; 484ff764963SKishon Vijay Abraham I 485ff764963SKishon Vijay Abraham I module_put(phy->ops->owner); 486ff764963SKishon Vijay Abraham I put_device(&phy->dev); 487ff764963SKishon Vijay Abraham I } 488ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_put); 489ff764963SKishon Vijay Abraham I 490ff764963SKishon Vijay Abraham I /** 491ff764963SKishon Vijay Abraham I * devm_phy_put() - release the PHY 492ff764963SKishon Vijay Abraham I * @dev: device that wants to release this phy 493ff764963SKishon Vijay Abraham I * @phy: the phy returned by devm_phy_get() 494ff764963SKishon Vijay Abraham I * 495ff764963SKishon Vijay Abraham I * destroys the devres associated with this phy and invokes phy_put 496ff764963SKishon Vijay Abraham I * to release the phy. 497ff764963SKishon Vijay Abraham I */ 498ff764963SKishon Vijay Abraham I void devm_phy_put(struct device *dev, struct phy *phy) 499ff764963SKishon Vijay Abraham I { 500ff764963SKishon Vijay Abraham I int r; 501ff764963SKishon Vijay Abraham I 50204c2facaSAndrew Lunn if (!phy) 50304c2facaSAndrew Lunn return; 50404c2facaSAndrew Lunn 505ff764963SKishon Vijay Abraham I r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy); 506ff764963SKishon Vijay Abraham I dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 507ff764963SKishon Vijay Abraham I } 508ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_put); 509ff764963SKishon Vijay Abraham I 510ff764963SKishon Vijay Abraham I /** 511ff764963SKishon Vijay Abraham I * of_phy_simple_xlate() - returns the phy instance from phy provider 512ff764963SKishon Vijay Abraham I * @dev: the PHY provider device 513ff764963SKishon Vijay Abraham I * @args: of_phandle_args (not used here) 514ff764963SKishon Vijay Abraham I * 515ff764963SKishon Vijay Abraham I * Intended to be used by phy provider for the common case where #phy-cells is 516ff764963SKishon Vijay Abraham I * 0. For other cases where #phy-cells is greater than '0', the phy provider 517ff764963SKishon Vijay Abraham I * should provide a custom of_xlate function that reads the *args* and returns 518ff764963SKishon Vijay Abraham I * the appropriate phy. 519ff764963SKishon Vijay Abraham I */ 520ff764963SKishon Vijay Abraham I struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args 521ff764963SKishon Vijay Abraham I *args) 522ff764963SKishon Vijay Abraham I { 523ff764963SKishon Vijay Abraham I struct phy *phy; 524ff764963SKishon Vijay Abraham I struct class_dev_iter iter; 525ff764963SKishon Vijay Abraham I 526ff764963SKishon Vijay Abraham I class_dev_iter_init(&iter, phy_class, NULL, NULL); 527ff764963SKishon Vijay Abraham I while ((dev = class_dev_iter_next(&iter))) { 528ff764963SKishon Vijay Abraham I phy = to_phy(dev); 529491e0490SKishon Vijay Abraham I if (args->np != phy->dev.of_node) 530ff764963SKishon Vijay Abraham I continue; 531ff764963SKishon Vijay Abraham I 532ff764963SKishon Vijay Abraham I class_dev_iter_exit(&iter); 533ff764963SKishon Vijay Abraham I return phy; 534ff764963SKishon Vijay Abraham I } 535ff764963SKishon Vijay Abraham I 536ff764963SKishon Vijay Abraham I class_dev_iter_exit(&iter); 537ff764963SKishon Vijay Abraham I return ERR_PTR(-ENODEV); 538ff764963SKishon Vijay Abraham I } 539ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_simple_xlate); 540ff764963SKishon Vijay Abraham I 541ff764963SKishon Vijay Abraham I /** 542ff764963SKishon Vijay Abraham I * phy_get() - lookup and obtain a reference to a phy. 543ff764963SKishon Vijay Abraham I * @dev: device that requests this phy 544ff764963SKishon Vijay Abraham I * @string: the phy name as given in the dt data or the name of the controller 545ff764963SKishon Vijay Abraham I * port for non-dt case 546ff764963SKishon Vijay Abraham I * 547ff764963SKishon Vijay Abraham I * Returns the phy driver, after getting a refcount to it; or 548ff764963SKishon Vijay Abraham I * -ENODEV if there is no such phy. The caller is responsible for 549ff764963SKishon Vijay Abraham I * calling phy_put() to release that count. 550ff764963SKishon Vijay Abraham I */ 551ff764963SKishon Vijay Abraham I struct phy *phy_get(struct device *dev, const char *string) 552ff764963SKishon Vijay Abraham I { 553ff764963SKishon Vijay Abraham I int index = 0; 554d18c9604SKishon Vijay Abraham I struct phy *phy; 555ff764963SKishon Vijay Abraham I 556ff764963SKishon Vijay Abraham I if (string == NULL) { 557ff764963SKishon Vijay Abraham I dev_WARN(dev, "missing string\n"); 558ff764963SKishon Vijay Abraham I return ERR_PTR(-EINVAL); 559ff764963SKishon Vijay Abraham I } 560ff764963SKishon Vijay Abraham I 561ff764963SKishon Vijay Abraham I if (dev->of_node) { 562ff764963SKishon Vijay Abraham I index = of_property_match_string(dev->of_node, "phy-names", 563ff764963SKishon Vijay Abraham I string); 5640b3f3b2cSKamil Debski phy = _of_phy_get(dev->of_node, index); 565ff764963SKishon Vijay Abraham I } else { 566b7bc15b9SHeikki Krogerus phy = phy_find(dev, string); 567f40037fdSHans de Goede } 568f40037fdSHans de Goede if (IS_ERR(phy)) 569ff764963SKishon Vijay Abraham I return phy; 570ff764963SKishon Vijay Abraham I 571ff764963SKishon Vijay Abraham I if (!try_module_get(phy->ops->owner)) 572ff764963SKishon Vijay Abraham I return ERR_PTR(-EPROBE_DEFER); 573ff764963SKishon Vijay Abraham I 574ff764963SKishon Vijay Abraham I get_device(&phy->dev); 575ff764963SKishon Vijay Abraham I 576ff764963SKishon Vijay Abraham I return phy; 577ff764963SKishon Vijay Abraham I } 578ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_get); 579ff764963SKishon Vijay Abraham I 580ff764963SKishon Vijay Abraham I /** 581788a4d56SAndrew Lunn * phy_optional_get() - lookup and obtain a reference to an optional phy. 582788a4d56SAndrew Lunn * @dev: device that requests this phy 583788a4d56SAndrew Lunn * @string: the phy name as given in the dt data or the name of the controller 584788a4d56SAndrew Lunn * port for non-dt case 585788a4d56SAndrew Lunn * 586788a4d56SAndrew Lunn * Returns the phy driver, after getting a refcount to it; or 587788a4d56SAndrew Lunn * NULL if there is no such phy. The caller is responsible for 588788a4d56SAndrew Lunn * calling phy_put() to release that count. 589788a4d56SAndrew Lunn */ 590788a4d56SAndrew Lunn struct phy *phy_optional_get(struct device *dev, const char *string) 591788a4d56SAndrew Lunn { 592788a4d56SAndrew Lunn struct phy *phy = phy_get(dev, string); 593788a4d56SAndrew Lunn 594f83be4c3SAxel Lin if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV)) 595788a4d56SAndrew Lunn phy = NULL; 596788a4d56SAndrew Lunn 597788a4d56SAndrew Lunn return phy; 598788a4d56SAndrew Lunn } 599788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(phy_optional_get); 600788a4d56SAndrew Lunn 601788a4d56SAndrew Lunn /** 602ff764963SKishon Vijay Abraham I * devm_phy_get() - lookup and obtain a reference to a phy. 603ff764963SKishon Vijay Abraham I * @dev: device that requests this phy 604ff764963SKishon Vijay Abraham I * @string: the phy name as given in the dt data or phy device name 605ff764963SKishon Vijay Abraham I * for non-dt case 606ff764963SKishon Vijay Abraham I * 607ff764963SKishon Vijay Abraham I * Gets the phy using phy_get(), and associates a device with it using 608ff764963SKishon Vijay Abraham I * devres. On driver detach, release function is invoked on the devres data, 609ff764963SKishon Vijay Abraham I * then, devres data is freed. 610ff764963SKishon Vijay Abraham I */ 611ff764963SKishon Vijay Abraham I struct phy *devm_phy_get(struct device *dev, const char *string) 612ff764963SKishon Vijay Abraham I { 613ff764963SKishon Vijay Abraham I struct phy **ptr, *phy; 614ff764963SKishon Vijay Abraham I 615ff764963SKishon Vijay Abraham I ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 616ff764963SKishon Vijay Abraham I if (!ptr) 617ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 618ff764963SKishon Vijay Abraham I 619ff764963SKishon Vijay Abraham I phy = phy_get(dev, string); 620ff764963SKishon Vijay Abraham I if (!IS_ERR(phy)) { 621ff764963SKishon Vijay Abraham I *ptr = phy; 622ff764963SKishon Vijay Abraham I devres_add(dev, ptr); 623ff764963SKishon Vijay Abraham I } else { 624ff764963SKishon Vijay Abraham I devres_free(ptr); 625ff764963SKishon Vijay Abraham I } 626ff764963SKishon Vijay Abraham I 627ff764963SKishon Vijay Abraham I return phy; 628ff764963SKishon Vijay Abraham I } 629ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_get); 630ff764963SKishon Vijay Abraham I 631ff764963SKishon Vijay Abraham I /** 632788a4d56SAndrew Lunn * devm_phy_optional_get() - lookup and obtain a reference to an optional phy. 633788a4d56SAndrew Lunn * @dev: device that requests this phy 634788a4d56SAndrew Lunn * @string: the phy name as given in the dt data or phy device name 635788a4d56SAndrew Lunn * for non-dt case 636788a4d56SAndrew Lunn * 637788a4d56SAndrew Lunn * Gets the phy using phy_get(), and associates a device with it using 638788a4d56SAndrew Lunn * devres. On driver detach, release function is invoked on the devres 639788a4d56SAndrew Lunn * data, then, devres data is freed. This differs to devm_phy_get() in 640788a4d56SAndrew Lunn * that if the phy does not exist, it is not considered an error and 641788a4d56SAndrew Lunn * -ENODEV will not be returned. Instead the NULL phy is returned, 642788a4d56SAndrew Lunn * which can be passed to all other phy consumer calls. 643788a4d56SAndrew Lunn */ 644788a4d56SAndrew Lunn struct phy *devm_phy_optional_get(struct device *dev, const char *string) 645788a4d56SAndrew Lunn { 646788a4d56SAndrew Lunn struct phy *phy = devm_phy_get(dev, string); 647788a4d56SAndrew Lunn 648f83be4c3SAxel Lin if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV)) 649788a4d56SAndrew Lunn phy = NULL; 650788a4d56SAndrew Lunn 651788a4d56SAndrew Lunn return phy; 652788a4d56SAndrew Lunn } 653788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(devm_phy_optional_get); 654788a4d56SAndrew Lunn 655788a4d56SAndrew Lunn /** 656b5d682f4SKamil Debski * devm_of_phy_get() - lookup and obtain a reference to a phy. 657b5d682f4SKamil Debski * @dev: device that requests this phy 658b5d682f4SKamil Debski * @np: node containing the phy 659b5d682f4SKamil Debski * @con_id: name of the phy from device's point of view 660b5d682f4SKamil Debski * 661b5d682f4SKamil Debski * Gets the phy using of_phy_get(), and associates a device with it using 662b5d682f4SKamil Debski * devres. On driver detach, release function is invoked on the devres data, 663b5d682f4SKamil Debski * then, devres data is freed. 664b5d682f4SKamil Debski */ 665b5d682f4SKamil Debski struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 666b5d682f4SKamil Debski const char *con_id) 667b5d682f4SKamil Debski { 668b5d682f4SKamil Debski struct phy **ptr, *phy; 669b5d682f4SKamil Debski 670b5d682f4SKamil Debski ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 671b5d682f4SKamil Debski if (!ptr) 672b5d682f4SKamil Debski return ERR_PTR(-ENOMEM); 673b5d682f4SKamil Debski 674b5d682f4SKamil Debski phy = of_phy_get(np, con_id); 675b5d682f4SKamil Debski if (!IS_ERR(phy)) { 676b5d682f4SKamil Debski *ptr = phy; 677b5d682f4SKamil Debski devres_add(dev, ptr); 678b5d682f4SKamil Debski } else { 679b5d682f4SKamil Debski devres_free(ptr); 680b5d682f4SKamil Debski } 681b5d682f4SKamil Debski 682b5d682f4SKamil Debski return phy; 683b5d682f4SKamil Debski } 684b5d682f4SKamil Debski EXPORT_SYMBOL_GPL(devm_of_phy_get); 685b5d682f4SKamil Debski 686b5d682f4SKamil Debski /** 6876be109b3SArun Ramamurthy * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index. 6886be109b3SArun Ramamurthy * @dev: device that requests this phy 6896be109b3SArun Ramamurthy * @np: node containing the phy 6906be109b3SArun Ramamurthy * @index: index of the phy 6916be109b3SArun Ramamurthy * 69270874462SChunfeng Yun * Gets the phy using _of_phy_get(), then gets a refcount to it, 69370874462SChunfeng Yun * and associates a device with it using devres. On driver detach, 69470874462SChunfeng Yun * release function is invoked on the devres data, 6956be109b3SArun Ramamurthy * then, devres data is freed. 6966be109b3SArun Ramamurthy * 6976be109b3SArun Ramamurthy */ 6986be109b3SArun Ramamurthy struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, 6996be109b3SArun Ramamurthy int index) 7006be109b3SArun Ramamurthy { 7016be109b3SArun Ramamurthy struct phy **ptr, *phy; 7026be109b3SArun Ramamurthy 7036be109b3SArun Ramamurthy ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); 7046be109b3SArun Ramamurthy if (!ptr) 7056be109b3SArun Ramamurthy return ERR_PTR(-ENOMEM); 7066be109b3SArun Ramamurthy 7076be109b3SArun Ramamurthy phy = _of_phy_get(np, index); 70870874462SChunfeng Yun if (IS_ERR(phy)) { 70970874462SChunfeng Yun devres_free(ptr); 71070874462SChunfeng Yun return phy; 71170874462SChunfeng Yun } 71270874462SChunfeng Yun 71370874462SChunfeng Yun if (!try_module_get(phy->ops->owner)) { 71470874462SChunfeng Yun devres_free(ptr); 71570874462SChunfeng Yun return ERR_PTR(-EPROBE_DEFER); 71670874462SChunfeng Yun } 71770874462SChunfeng Yun 71870874462SChunfeng Yun get_device(&phy->dev); 71970874462SChunfeng Yun 7206be109b3SArun Ramamurthy *ptr = phy; 7216be109b3SArun Ramamurthy devres_add(dev, ptr); 7226be109b3SArun Ramamurthy 7236be109b3SArun Ramamurthy return phy; 7246be109b3SArun Ramamurthy } 7256be109b3SArun Ramamurthy EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index); 7266be109b3SArun Ramamurthy 7276be109b3SArun Ramamurthy /** 728ff764963SKishon Vijay Abraham I * phy_create() - create a new phy 729ff764963SKishon Vijay Abraham I * @dev: device that is creating the new phy 730f0ed8176SKishon Vijay Abraham I * @node: device node of the phy 731ff764963SKishon Vijay Abraham I * @ops: function pointers for performing phy operations 732ff764963SKishon Vijay Abraham I * 733ff764963SKishon Vijay Abraham I * Called to create a phy using phy framework. 734ff764963SKishon Vijay Abraham I */ 735f0ed8176SKishon Vijay Abraham I struct phy *phy_create(struct device *dev, struct device_node *node, 736dbc98635SHeikki Krogerus const struct phy_ops *ops) 737ff764963SKishon Vijay Abraham I { 738ff764963SKishon Vijay Abraham I int ret; 739ff764963SKishon Vijay Abraham I int id; 740ff764963SKishon Vijay Abraham I struct phy *phy; 741ff764963SKishon Vijay Abraham I 74252797d29SDan Carpenter if (WARN_ON(!dev)) 74352797d29SDan Carpenter return ERR_PTR(-EINVAL); 744ff764963SKishon Vijay Abraham I 745ff764963SKishon Vijay Abraham I phy = kzalloc(sizeof(*phy), GFP_KERNEL); 74652797d29SDan Carpenter if (!phy) 74752797d29SDan Carpenter return ERR_PTR(-ENOMEM); 748ff764963SKishon Vijay Abraham I 749ff764963SKishon Vijay Abraham I id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL); 750ff764963SKishon Vijay Abraham I if (id < 0) { 751ff764963SKishon Vijay Abraham I dev_err(dev, "unable to get id\n"); 752ff764963SKishon Vijay Abraham I ret = id; 75352797d29SDan Carpenter goto free_phy; 754ff764963SKishon Vijay Abraham I } 755ff764963SKishon Vijay Abraham I 756ff764963SKishon Vijay Abraham I device_initialize(&phy->dev); 757ff764963SKishon Vijay Abraham I mutex_init(&phy->mutex); 758ff764963SKishon Vijay Abraham I 759ff764963SKishon Vijay Abraham I phy->dev.class = phy_class; 760ff764963SKishon Vijay Abraham I phy->dev.parent = dev; 761f0ed8176SKishon Vijay Abraham I phy->dev.of_node = node ?: dev->of_node; 762ff764963SKishon Vijay Abraham I phy->id = id; 763ff764963SKishon Vijay Abraham I phy->ops = ops; 764ff764963SKishon Vijay Abraham I 765ff764963SKishon Vijay Abraham I ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); 766ff764963SKishon Vijay Abraham I if (ret) 76752797d29SDan Carpenter goto put_dev; 768ff764963SKishon Vijay Abraham I 76987006dd6SDmitry Torokhov /* phy-supply */ 77087006dd6SDmitry Torokhov phy->pwr = regulator_get_optional(&phy->dev, "phy"); 77187006dd6SDmitry Torokhov if (IS_ERR(phy->pwr)) { 77287006dd6SDmitry Torokhov ret = PTR_ERR(phy->pwr); 77387006dd6SDmitry Torokhov if (ret == -EPROBE_DEFER) 77487006dd6SDmitry Torokhov goto put_dev; 77587006dd6SDmitry Torokhov 77687006dd6SDmitry Torokhov phy->pwr = NULL; 77787006dd6SDmitry Torokhov } 77887006dd6SDmitry Torokhov 779ff764963SKishon Vijay Abraham I ret = device_add(&phy->dev); 780ff764963SKishon Vijay Abraham I if (ret) 78152797d29SDan Carpenter goto put_dev; 782ff764963SKishon Vijay Abraham I 783ff764963SKishon Vijay Abraham I if (pm_runtime_enabled(dev)) { 784ff764963SKishon Vijay Abraham I pm_runtime_enable(&phy->dev); 785ff764963SKishon Vijay Abraham I pm_runtime_no_callbacks(&phy->dev); 786ff764963SKishon Vijay Abraham I } 787ff764963SKishon Vijay Abraham I 788ff764963SKishon Vijay Abraham I return phy; 789ff764963SKishon Vijay Abraham I 79052797d29SDan Carpenter put_dev: 791e73b49f1SRoger Quadros put_device(&phy->dev); /* calls phy_release() which frees resources */ 792e73b49f1SRoger Quadros return ERR_PTR(ret); 793e73b49f1SRoger Quadros 79452797d29SDan Carpenter free_phy: 795ff764963SKishon Vijay Abraham I kfree(phy); 796ff764963SKishon Vijay Abraham I return ERR_PTR(ret); 797ff764963SKishon Vijay Abraham I } 798ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_create); 799ff764963SKishon Vijay Abraham I 800ff764963SKishon Vijay Abraham I /** 801ff764963SKishon Vijay Abraham I * devm_phy_create() - create a new phy 802ff764963SKishon Vijay Abraham I * @dev: device that is creating the new phy 803f0ed8176SKishon Vijay Abraham I * @node: device node of the phy 804ff764963SKishon Vijay Abraham I * @ops: function pointers for performing phy operations 805ff764963SKishon Vijay Abraham I * 806ff764963SKishon Vijay Abraham I * Creates a new PHY device adding it to the PHY class. 807ff764963SKishon Vijay Abraham I * While at that, it also associates the device with the phy using devres. 808ff764963SKishon Vijay Abraham I * On driver detach, release function is invoked on the devres data, 809ff764963SKishon Vijay Abraham I * then, devres data is freed. 810ff764963SKishon Vijay Abraham I */ 811f0ed8176SKishon Vijay Abraham I struct phy *devm_phy_create(struct device *dev, struct device_node *node, 812dbc98635SHeikki Krogerus const struct phy_ops *ops) 813ff764963SKishon Vijay Abraham I { 814ff764963SKishon Vijay Abraham I struct phy **ptr, *phy; 815ff764963SKishon Vijay Abraham I 816ff764963SKishon Vijay Abraham I ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); 817ff764963SKishon Vijay Abraham I if (!ptr) 818ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 819ff764963SKishon Vijay Abraham I 820dbc98635SHeikki Krogerus phy = phy_create(dev, node, ops); 821ff764963SKishon Vijay Abraham I if (!IS_ERR(phy)) { 822ff764963SKishon Vijay Abraham I *ptr = phy; 823ff764963SKishon Vijay Abraham I devres_add(dev, ptr); 824ff764963SKishon Vijay Abraham I } else { 825ff764963SKishon Vijay Abraham I devres_free(ptr); 826ff764963SKishon Vijay Abraham I } 827ff764963SKishon Vijay Abraham I 828ff764963SKishon Vijay Abraham I return phy; 829ff764963SKishon Vijay Abraham I } 830ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_create); 831ff764963SKishon Vijay Abraham I 832ff764963SKishon Vijay Abraham I /** 833ff764963SKishon Vijay Abraham I * phy_destroy() - destroy the phy 834ff764963SKishon Vijay Abraham I * @phy: the phy to be destroyed 835ff764963SKishon Vijay Abraham I * 836ff764963SKishon Vijay Abraham I * Called to destroy the phy. 837ff764963SKishon Vijay Abraham I */ 838ff764963SKishon Vijay Abraham I void phy_destroy(struct phy *phy) 839ff764963SKishon Vijay Abraham I { 840ff764963SKishon Vijay Abraham I pm_runtime_disable(&phy->dev); 841ff764963SKishon Vijay Abraham I device_unregister(&phy->dev); 842ff764963SKishon Vijay Abraham I } 843ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_destroy); 844ff764963SKishon Vijay Abraham I 845ff764963SKishon Vijay Abraham I /** 846ff764963SKishon Vijay Abraham I * devm_phy_destroy() - destroy the PHY 847ff764963SKishon Vijay Abraham I * @dev: device that wants to release this phy 848ff764963SKishon Vijay Abraham I * @phy: the phy returned by devm_phy_get() 849ff764963SKishon Vijay Abraham I * 850ff764963SKishon Vijay Abraham I * destroys the devres associated with this phy and invokes phy_destroy 851ff764963SKishon Vijay Abraham I * to destroy the phy. 852ff764963SKishon Vijay Abraham I */ 853ff764963SKishon Vijay Abraham I void devm_phy_destroy(struct device *dev, struct phy *phy) 854ff764963SKishon Vijay Abraham I { 855ff764963SKishon Vijay Abraham I int r; 856ff764963SKishon Vijay Abraham I 857ff764963SKishon Vijay Abraham I r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy); 858ff764963SKishon Vijay Abraham I dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 859ff764963SKishon Vijay Abraham I } 860ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_destroy); 861ff764963SKishon Vijay Abraham I 862ff764963SKishon Vijay Abraham I /** 863ff764963SKishon Vijay Abraham I * __of_phy_provider_register() - create/register phy provider with the framework 864ff764963SKishon Vijay Abraham I * @dev: struct device of the phy provider 8651140f7c8SThierry Reding * @children: device node containing children (if different from dev->of_node) 866ff764963SKishon Vijay Abraham I * @owner: the module owner containing of_xlate 867ff764963SKishon Vijay Abraham I * @of_xlate: function pointer to obtain phy instance from phy provider 868ff764963SKishon Vijay Abraham I * 869ff764963SKishon Vijay Abraham I * Creates struct phy_provider from dev and of_xlate function pointer. 870ff764963SKishon Vijay Abraham I * This is used in the case of dt boot for finding the phy instance from 871ff764963SKishon Vijay Abraham I * phy provider. 8721140f7c8SThierry Reding * 8731140f7c8SThierry Reding * If the PHY provider doesn't nest children directly but uses a separate 8741140f7c8SThierry Reding * child node to contain the individual children, the @children parameter 8751140f7c8SThierry Reding * can be used to override the default. If NULL, the default (dev->of_node) 8761140f7c8SThierry Reding * will be used. If non-NULL, the device node must be a child (or further 8771140f7c8SThierry Reding * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL 8781140f7c8SThierry Reding * error code is returned. 879ff764963SKishon Vijay Abraham I */ 880ff764963SKishon Vijay Abraham I struct phy_provider *__of_phy_provider_register(struct device *dev, 8811140f7c8SThierry Reding struct device_node *children, struct module *owner, 8821140f7c8SThierry Reding struct phy * (*of_xlate)(struct device *dev, 883ff764963SKishon Vijay Abraham I struct of_phandle_args *args)) 884ff764963SKishon Vijay Abraham I { 885ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider; 886ff764963SKishon Vijay Abraham I 8871140f7c8SThierry Reding /* 8881140f7c8SThierry Reding * If specified, the device node containing the children must itself 8891140f7c8SThierry Reding * be the provider's device node or a child (or further descendant) 8901140f7c8SThierry Reding * thereof. 8911140f7c8SThierry Reding */ 8921140f7c8SThierry Reding if (children) { 8931140f7c8SThierry Reding struct device_node *parent = of_node_get(children), *next; 8941140f7c8SThierry Reding 8951140f7c8SThierry Reding while (parent) { 8961140f7c8SThierry Reding if (parent == dev->of_node) 8971140f7c8SThierry Reding break; 8981140f7c8SThierry Reding 8991140f7c8SThierry Reding next = of_get_parent(parent); 9001140f7c8SThierry Reding of_node_put(parent); 9011140f7c8SThierry Reding parent = next; 9021140f7c8SThierry Reding } 9031140f7c8SThierry Reding 9041140f7c8SThierry Reding if (!parent) 9051140f7c8SThierry Reding return ERR_PTR(-EINVAL); 9061140f7c8SThierry Reding 9071140f7c8SThierry Reding of_node_put(parent); 9081140f7c8SThierry Reding } else { 9091140f7c8SThierry Reding children = dev->of_node; 9101140f7c8SThierry Reding } 9111140f7c8SThierry Reding 912ff764963SKishon Vijay Abraham I phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL); 913ff764963SKishon Vijay Abraham I if (!phy_provider) 914ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 915ff764963SKishon Vijay Abraham I 916ff764963SKishon Vijay Abraham I phy_provider->dev = dev; 9171140f7c8SThierry Reding phy_provider->children = of_node_get(children); 918ff764963SKishon Vijay Abraham I phy_provider->owner = owner; 919ff764963SKishon Vijay Abraham I phy_provider->of_xlate = of_xlate; 920ff764963SKishon Vijay Abraham I 921ff764963SKishon Vijay Abraham I mutex_lock(&phy_provider_mutex); 922ff764963SKishon Vijay Abraham I list_add_tail(&phy_provider->list, &phy_provider_list); 923ff764963SKishon Vijay Abraham I mutex_unlock(&phy_provider_mutex); 924ff764963SKishon Vijay Abraham I 925ff764963SKishon Vijay Abraham I return phy_provider; 926ff764963SKishon Vijay Abraham I } 927ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__of_phy_provider_register); 928ff764963SKishon Vijay Abraham I 929ff764963SKishon Vijay Abraham I /** 930ff764963SKishon Vijay Abraham I * __devm_of_phy_provider_register() - create/register phy provider with the 931ff764963SKishon Vijay Abraham I * framework 932ff764963SKishon Vijay Abraham I * @dev: struct device of the phy provider 933ff764963SKishon Vijay Abraham I * @owner: the module owner containing of_xlate 934ff764963SKishon Vijay Abraham I * @of_xlate: function pointer to obtain phy instance from phy provider 935ff764963SKishon Vijay Abraham I * 936ff764963SKishon Vijay Abraham I * Creates struct phy_provider from dev and of_xlate function pointer. 937ff764963SKishon Vijay Abraham I * This is used in the case of dt boot for finding the phy instance from 938ff764963SKishon Vijay Abraham I * phy provider. While at that, it also associates the device with the 939ff764963SKishon Vijay Abraham I * phy provider using devres. On driver detach, release function is invoked 940ff764963SKishon Vijay Abraham I * on the devres data, then, devres data is freed. 941ff764963SKishon Vijay Abraham I */ 942ff764963SKishon Vijay Abraham I struct phy_provider *__devm_of_phy_provider_register(struct device *dev, 9431140f7c8SThierry Reding struct device_node *children, struct module *owner, 9441140f7c8SThierry Reding struct phy * (*of_xlate)(struct device *dev, 945ff764963SKishon Vijay Abraham I struct of_phandle_args *args)) 946ff764963SKishon Vijay Abraham I { 947ff764963SKishon Vijay Abraham I struct phy_provider **ptr, *phy_provider; 948ff764963SKishon Vijay Abraham I 949ff764963SKishon Vijay Abraham I ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); 950ff764963SKishon Vijay Abraham I if (!ptr) 951ff764963SKishon Vijay Abraham I return ERR_PTR(-ENOMEM); 952ff764963SKishon Vijay Abraham I 9531140f7c8SThierry Reding phy_provider = __of_phy_provider_register(dev, children, owner, 9541140f7c8SThierry Reding of_xlate); 955ff764963SKishon Vijay Abraham I if (!IS_ERR(phy_provider)) { 956ff764963SKishon Vijay Abraham I *ptr = phy_provider; 957ff764963SKishon Vijay Abraham I devres_add(dev, ptr); 958ff764963SKishon Vijay Abraham I } else { 959ff764963SKishon Vijay Abraham I devres_free(ptr); 960ff764963SKishon Vijay Abraham I } 961ff764963SKishon Vijay Abraham I 962ff764963SKishon Vijay Abraham I return phy_provider; 963ff764963SKishon Vijay Abraham I } 964ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register); 965ff764963SKishon Vijay Abraham I 966ff764963SKishon Vijay Abraham I /** 967ff764963SKishon Vijay Abraham I * of_phy_provider_unregister() - unregister phy provider from the framework 968ff764963SKishon Vijay Abraham I * @phy_provider: phy provider returned by of_phy_provider_register() 969ff764963SKishon Vijay Abraham I * 970ff764963SKishon Vijay Abraham I * Removes the phy_provider created using of_phy_provider_register(). 971ff764963SKishon Vijay Abraham I */ 972ff764963SKishon Vijay Abraham I void of_phy_provider_unregister(struct phy_provider *phy_provider) 973ff764963SKishon Vijay Abraham I { 974ff764963SKishon Vijay Abraham I if (IS_ERR(phy_provider)) 975ff764963SKishon Vijay Abraham I return; 976ff764963SKishon Vijay Abraham I 977ff764963SKishon Vijay Abraham I mutex_lock(&phy_provider_mutex); 978ff764963SKishon Vijay Abraham I list_del(&phy_provider->list); 9791140f7c8SThierry Reding of_node_put(phy_provider->children); 980ff764963SKishon Vijay Abraham I kfree(phy_provider); 981ff764963SKishon Vijay Abraham I mutex_unlock(&phy_provider_mutex); 982ff764963SKishon Vijay Abraham I } 983ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_provider_unregister); 984ff764963SKishon Vijay Abraham I 985ff764963SKishon Vijay Abraham I /** 986ff764963SKishon Vijay Abraham I * devm_of_phy_provider_unregister() - remove phy provider from the framework 987ff764963SKishon Vijay Abraham I * @dev: struct device of the phy provider 988ff764963SKishon Vijay Abraham I * 989ff764963SKishon Vijay Abraham I * destroys the devres associated with this phy provider and invokes 990ff764963SKishon Vijay Abraham I * of_phy_provider_unregister to unregister the phy provider. 991ff764963SKishon Vijay Abraham I */ 992ff764963SKishon Vijay Abraham I void devm_of_phy_provider_unregister(struct device *dev, 993ff764963SKishon Vijay Abraham I struct phy_provider *phy_provider) { 994ff764963SKishon Vijay Abraham I int r; 995ff764963SKishon Vijay Abraham I 996ff764963SKishon Vijay Abraham I r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match, 997ff764963SKishon Vijay Abraham I phy_provider); 998ff764963SKishon Vijay Abraham I dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n"); 999ff764963SKishon Vijay Abraham I } 1000ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister); 1001ff764963SKishon Vijay Abraham I 1002ff764963SKishon Vijay Abraham I /** 1003ff764963SKishon Vijay Abraham I * phy_release() - release the phy 1004ff764963SKishon Vijay Abraham I * @dev: the dev member within phy 1005ff764963SKishon Vijay Abraham I * 1006ff764963SKishon Vijay Abraham I * When the last reference to the device is removed, it is called 1007ff764963SKishon Vijay Abraham I * from the embedded kobject as release method. 1008ff764963SKishon Vijay Abraham I */ 1009ff764963SKishon Vijay Abraham I static void phy_release(struct device *dev) 1010ff764963SKishon Vijay Abraham I { 1011ff764963SKishon Vijay Abraham I struct phy *phy; 1012ff764963SKishon Vijay Abraham I 1013ff764963SKishon Vijay Abraham I phy = to_phy(dev); 1014ff764963SKishon Vijay Abraham I dev_vdbg(dev, "releasing '%s'\n", dev_name(dev)); 10153be88125SRoger Quadros regulator_put(phy->pwr); 1016e73b49f1SRoger Quadros ida_simple_remove(&phy_ida, phy->id); 1017ff764963SKishon Vijay Abraham I kfree(phy); 1018ff764963SKishon Vijay Abraham I } 1019ff764963SKishon Vijay Abraham I 1020ff764963SKishon Vijay Abraham I static int __init phy_core_init(void) 1021ff764963SKishon Vijay Abraham I { 1022ff764963SKishon Vijay Abraham I phy_class = class_create(THIS_MODULE, "phy"); 1023ff764963SKishon Vijay Abraham I if (IS_ERR(phy_class)) { 1024ff764963SKishon Vijay Abraham I pr_err("failed to create phy class --> %ld\n", 1025ff764963SKishon Vijay Abraham I PTR_ERR(phy_class)); 1026ff764963SKishon Vijay Abraham I return PTR_ERR(phy_class); 1027ff764963SKishon Vijay Abraham I } 1028ff764963SKishon Vijay Abraham I 1029ff764963SKishon Vijay Abraham I phy_class->dev_release = phy_release; 1030ff764963SKishon Vijay Abraham I 1031ff764963SKishon Vijay Abraham I return 0; 1032ff764963SKishon Vijay Abraham I } 1033ff764963SKishon Vijay Abraham I module_init(phy_core_init); 1034ff764963SKishon Vijay Abraham I 1035ff764963SKishon Vijay Abraham I static void __exit phy_core_exit(void) 1036ff764963SKishon Vijay Abraham I { 1037ff764963SKishon Vijay Abraham I class_destroy(phy_class); 1038ff764963SKishon Vijay Abraham I } 1039ff764963SKishon Vijay Abraham I module_exit(phy_core_exit); 1040ff764963SKishon Vijay Abraham I 1041ff764963SKishon Vijay Abraham I MODULE_DESCRIPTION("Generic PHY Framework"); 1042ff764963SKishon Vijay Abraham I MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>"); 1043ff764963SKishon Vijay Abraham I MODULE_LICENSE("GPL v2"); 1044