Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 1 | /* |
| 2 | * phy-core.c -- Generic Phy framework. |
| 3 | * |
| 4 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * |
| 6 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/export.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/phy/phy.h> |
| 22 | #include <linux/idr.h> |
| 23 | #include <linux/pm_runtime.h> |
| 24 | |
| 25 | static struct class *phy_class; |
| 26 | static DEFINE_MUTEX(phy_provider_mutex); |
| 27 | static LIST_HEAD(phy_provider_list); |
| 28 | static DEFINE_IDA(phy_ida); |
| 29 | |
| 30 | static void devm_phy_release(struct device *dev, void *res) |
| 31 | { |
| 32 | struct phy *phy = *(struct phy **)res; |
| 33 | |
| 34 | phy_put(phy); |
| 35 | } |
| 36 | |
| 37 | static void devm_phy_provider_release(struct device *dev, void *res) |
| 38 | { |
| 39 | struct phy_provider *phy_provider = *(struct phy_provider **)res; |
| 40 | |
| 41 | of_phy_provider_unregister(phy_provider); |
| 42 | } |
| 43 | |
| 44 | static void devm_phy_consume(struct device *dev, void *res) |
| 45 | { |
| 46 | struct phy *phy = *(struct phy **)res; |
| 47 | |
| 48 | phy_destroy(phy); |
| 49 | } |
| 50 | |
| 51 | static int devm_phy_match(struct device *dev, void *res, void *match_data) |
| 52 | { |
| 53 | return res == match_data; |
| 54 | } |
| 55 | |
| 56 | static struct phy *phy_lookup(struct device *device, const char *port) |
| 57 | { |
| 58 | unsigned int count; |
| 59 | struct phy *phy; |
| 60 | struct device *dev; |
| 61 | struct phy_consumer *consumers; |
| 62 | struct class_dev_iter iter; |
| 63 | |
| 64 | class_dev_iter_init(&iter, phy_class, NULL, NULL); |
| 65 | while ((dev = class_dev_iter_next(&iter))) { |
| 66 | phy = to_phy(dev); |
| 67 | count = phy->init_data->num_consumers; |
| 68 | consumers = phy->init_data->consumers; |
| 69 | while (count--) { |
| 70 | if (!strcmp(consumers->dev_name, dev_name(device)) && |
| 71 | !strcmp(consumers->port, port)) { |
| 72 | class_dev_iter_exit(&iter); |
| 73 | return phy; |
| 74 | } |
| 75 | consumers++; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | class_dev_iter_exit(&iter); |
| 80 | return ERR_PTR(-ENODEV); |
| 81 | } |
| 82 | |
| 83 | static struct phy_provider *of_phy_provider_lookup(struct device_node *node) |
| 84 | { |
| 85 | struct phy_provider *phy_provider; |
| 86 | |
| 87 | list_for_each_entry(phy_provider, &phy_provider_list, list) { |
| 88 | if (phy_provider->dev->of_node == node) |
| 89 | return phy_provider; |
| 90 | } |
| 91 | |
| 92 | return ERR_PTR(-EPROBE_DEFER); |
| 93 | } |
| 94 | |
| 95 | int phy_pm_runtime_get(struct phy *phy) |
| 96 | { |
Felipe Balbi | cedb7f8 | 2013-12-20 15:00:48 -0600 | [diff] [blame] | 97 | int ret; |
| 98 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 99 | if (!pm_runtime_enabled(&phy->dev)) |
| 100 | return -ENOTSUPP; |
| 101 | |
Felipe Balbi | cedb7f8 | 2013-12-20 15:00:48 -0600 | [diff] [blame] | 102 | ret = pm_runtime_get(&phy->dev); |
| 103 | if (ret < 0 && ret != -EINPROGRESS) |
| 104 | pm_runtime_put_noidle(&phy->dev); |
| 105 | |
| 106 | return ret; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 107 | } |
| 108 | EXPORT_SYMBOL_GPL(phy_pm_runtime_get); |
| 109 | |
| 110 | int phy_pm_runtime_get_sync(struct phy *phy) |
| 111 | { |
Felipe Balbi | cedb7f8 | 2013-12-20 15:00:48 -0600 | [diff] [blame] | 112 | int ret; |
| 113 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 114 | if (!pm_runtime_enabled(&phy->dev)) |
| 115 | return -ENOTSUPP; |
| 116 | |
Felipe Balbi | cedb7f8 | 2013-12-20 15:00:48 -0600 | [diff] [blame] | 117 | ret = pm_runtime_get_sync(&phy->dev); |
| 118 | if (ret < 0) |
| 119 | pm_runtime_put_sync(&phy->dev); |
| 120 | |
| 121 | return ret; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 122 | } |
| 123 | EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync); |
| 124 | |
| 125 | int phy_pm_runtime_put(struct phy *phy) |
| 126 | { |
| 127 | if (!pm_runtime_enabled(&phy->dev)) |
| 128 | return -ENOTSUPP; |
| 129 | |
| 130 | return pm_runtime_put(&phy->dev); |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(phy_pm_runtime_put); |
| 133 | |
| 134 | int phy_pm_runtime_put_sync(struct phy *phy) |
| 135 | { |
| 136 | if (!pm_runtime_enabled(&phy->dev)) |
| 137 | return -ENOTSUPP; |
| 138 | |
| 139 | return pm_runtime_put_sync(&phy->dev); |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync); |
| 142 | |
| 143 | void phy_pm_runtime_allow(struct phy *phy) |
| 144 | { |
| 145 | if (!pm_runtime_enabled(&phy->dev)) |
| 146 | return; |
| 147 | |
| 148 | pm_runtime_allow(&phy->dev); |
| 149 | } |
| 150 | EXPORT_SYMBOL_GPL(phy_pm_runtime_allow); |
| 151 | |
| 152 | void phy_pm_runtime_forbid(struct phy *phy) |
| 153 | { |
| 154 | if (!pm_runtime_enabled(&phy->dev)) |
| 155 | return; |
| 156 | |
| 157 | pm_runtime_forbid(&phy->dev); |
| 158 | } |
| 159 | EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid); |
| 160 | |
| 161 | int phy_init(struct phy *phy) |
| 162 | { |
| 163 | int ret; |
| 164 | |
| 165 | ret = phy_pm_runtime_get_sync(phy); |
| 166 | if (ret < 0 && ret != -ENOTSUPP) |
| 167 | return ret; |
| 168 | |
| 169 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 170 | if (phy->init_count == 0 && phy->ops->init) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 171 | ret = phy->ops->init(phy); |
| 172 | if (ret < 0) { |
| 173 | dev_err(&phy->dev, "phy init failed --> %d\n", ret); |
| 174 | goto out; |
| 175 | } |
| 176 | } |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 177 | ++phy->init_count; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 178 | |
| 179 | out: |
| 180 | mutex_unlock(&phy->mutex); |
| 181 | phy_pm_runtime_put(phy); |
| 182 | return ret; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(phy_init); |
| 185 | |
| 186 | int phy_exit(struct phy *phy) |
| 187 | { |
| 188 | int ret; |
| 189 | |
| 190 | ret = phy_pm_runtime_get_sync(phy); |
| 191 | if (ret < 0 && ret != -ENOTSUPP) |
| 192 | return ret; |
| 193 | |
| 194 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 195 | if (phy->init_count == 1 && phy->ops->exit) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 196 | ret = phy->ops->exit(phy); |
| 197 | if (ret < 0) { |
| 198 | dev_err(&phy->dev, "phy exit failed --> %d\n", ret); |
| 199 | goto out; |
| 200 | } |
| 201 | } |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 202 | --phy->init_count; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 203 | |
| 204 | out: |
| 205 | mutex_unlock(&phy->mutex); |
| 206 | phy_pm_runtime_put(phy); |
| 207 | return ret; |
| 208 | } |
| 209 | EXPORT_SYMBOL_GPL(phy_exit); |
| 210 | |
| 211 | int phy_power_on(struct phy *phy) |
| 212 | { |
| 213 | int ret = -ENOTSUPP; |
| 214 | |
| 215 | ret = phy_pm_runtime_get_sync(phy); |
| 216 | if (ret < 0 && ret != -ENOTSUPP) |
| 217 | return ret; |
| 218 | |
| 219 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 220 | if (phy->power_count == 0 && phy->ops->power_on) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 221 | ret = phy->ops->power_on(phy); |
| 222 | if (ret < 0) { |
| 223 | dev_err(&phy->dev, "phy poweron failed --> %d\n", ret); |
| 224 | goto out; |
| 225 | } |
| 226 | } |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 227 | ++phy->power_count; |
| 228 | mutex_unlock(&phy->mutex); |
| 229 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 230 | |
| 231 | out: |
| 232 | mutex_unlock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 233 | phy_pm_runtime_put_sync(phy); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(phy_power_on); |
| 238 | |
| 239 | int phy_power_off(struct phy *phy) |
| 240 | { |
| 241 | int ret = -ENOTSUPP; |
| 242 | |
| 243 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 244 | if (phy->power_count == 1 && phy->ops->power_off) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 245 | ret = phy->ops->power_off(phy); |
| 246 | if (ret < 0) { |
| 247 | dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret); |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 248 | mutex_unlock(&phy->mutex); |
| 249 | return ret; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 250 | } |
| 251 | } |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 252 | --phy->power_count; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 253 | mutex_unlock(&phy->mutex); |
| 254 | phy_pm_runtime_put(phy); |
| 255 | |
Kishon Vijay Abraham I | 637d378c | 2013-12-20 10:36:49 +0530 | [diff] [blame^] | 256 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 257 | } |
| 258 | EXPORT_SYMBOL_GPL(phy_power_off); |
| 259 | |
| 260 | /** |
| 261 | * of_phy_get() - lookup and obtain a reference to a phy by phandle |
| 262 | * @dev: device that requests this phy |
| 263 | * @index: the index of the phy |
| 264 | * |
| 265 | * Returns the phy associated with the given phandle value, |
| 266 | * after getting a refcount to it or -ENODEV if there is no such phy or |
| 267 | * -EPROBE_DEFER if there is a phandle to the phy, but the device is |
| 268 | * not yet loaded. This function uses of_xlate call back function provided |
| 269 | * while registering the phy_provider to find the phy instance. |
| 270 | */ |
| 271 | static struct phy *of_phy_get(struct device *dev, int index) |
| 272 | { |
| 273 | int ret; |
| 274 | struct phy_provider *phy_provider; |
| 275 | struct phy *phy = NULL; |
| 276 | struct of_phandle_args args; |
| 277 | |
| 278 | ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells", |
| 279 | index, &args); |
| 280 | if (ret) { |
| 281 | dev_dbg(dev, "failed to get phy in %s node\n", |
| 282 | dev->of_node->full_name); |
| 283 | return ERR_PTR(-ENODEV); |
| 284 | } |
| 285 | |
| 286 | mutex_lock(&phy_provider_mutex); |
| 287 | phy_provider = of_phy_provider_lookup(args.np); |
| 288 | if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) { |
| 289 | phy = ERR_PTR(-EPROBE_DEFER); |
| 290 | goto err0; |
| 291 | } |
| 292 | |
| 293 | phy = phy_provider->of_xlate(phy_provider->dev, &args); |
| 294 | module_put(phy_provider->owner); |
| 295 | |
| 296 | err0: |
| 297 | mutex_unlock(&phy_provider_mutex); |
| 298 | of_node_put(args.np); |
| 299 | |
| 300 | return phy; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * phy_put() - release the PHY |
| 305 | * @phy: the phy returned by phy_get() |
| 306 | * |
| 307 | * Releases a refcount the caller received from phy_get(). |
| 308 | */ |
| 309 | void phy_put(struct phy *phy) |
| 310 | { |
| 311 | if (IS_ERR(phy)) |
| 312 | return; |
| 313 | |
| 314 | module_put(phy->ops->owner); |
| 315 | put_device(&phy->dev); |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(phy_put); |
| 318 | |
| 319 | /** |
| 320 | * devm_phy_put() - release the PHY |
| 321 | * @dev: device that wants to release this phy |
| 322 | * @phy: the phy returned by devm_phy_get() |
| 323 | * |
| 324 | * destroys the devres associated with this phy and invokes phy_put |
| 325 | * to release the phy. |
| 326 | */ |
| 327 | void devm_phy_put(struct device *dev, struct phy *phy) |
| 328 | { |
| 329 | int r; |
| 330 | |
| 331 | r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy); |
| 332 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(devm_phy_put); |
| 335 | |
| 336 | /** |
| 337 | * of_phy_simple_xlate() - returns the phy instance from phy provider |
| 338 | * @dev: the PHY provider device |
| 339 | * @args: of_phandle_args (not used here) |
| 340 | * |
| 341 | * Intended to be used by phy provider for the common case where #phy-cells is |
| 342 | * 0. For other cases where #phy-cells is greater than '0', the phy provider |
| 343 | * should provide a custom of_xlate function that reads the *args* and returns |
| 344 | * the appropriate phy. |
| 345 | */ |
| 346 | struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args |
| 347 | *args) |
| 348 | { |
| 349 | struct phy *phy; |
| 350 | struct class_dev_iter iter; |
| 351 | struct device_node *node = dev->of_node; |
| 352 | |
| 353 | class_dev_iter_init(&iter, phy_class, NULL, NULL); |
| 354 | while ((dev = class_dev_iter_next(&iter))) { |
| 355 | phy = to_phy(dev); |
| 356 | if (node != phy->dev.of_node) |
| 357 | continue; |
| 358 | |
| 359 | class_dev_iter_exit(&iter); |
| 360 | return phy; |
| 361 | } |
| 362 | |
| 363 | class_dev_iter_exit(&iter); |
| 364 | return ERR_PTR(-ENODEV); |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(of_phy_simple_xlate); |
| 367 | |
| 368 | /** |
| 369 | * phy_get() - lookup and obtain a reference to a phy. |
| 370 | * @dev: device that requests this phy |
| 371 | * @string: the phy name as given in the dt data or the name of the controller |
| 372 | * port for non-dt case |
| 373 | * |
| 374 | * Returns the phy driver, after getting a refcount to it; or |
| 375 | * -ENODEV if there is no such phy. The caller is responsible for |
| 376 | * calling phy_put() to release that count. |
| 377 | */ |
| 378 | struct phy *phy_get(struct device *dev, const char *string) |
| 379 | { |
| 380 | int index = 0; |
| 381 | struct phy *phy = NULL; |
| 382 | |
| 383 | if (string == NULL) { |
| 384 | dev_WARN(dev, "missing string\n"); |
| 385 | return ERR_PTR(-EINVAL); |
| 386 | } |
| 387 | |
| 388 | if (dev->of_node) { |
| 389 | index = of_property_match_string(dev->of_node, "phy-names", |
| 390 | string); |
| 391 | phy = of_phy_get(dev, index); |
| 392 | if (IS_ERR(phy)) { |
| 393 | dev_err(dev, "unable to find phy\n"); |
| 394 | return phy; |
| 395 | } |
| 396 | } else { |
| 397 | phy = phy_lookup(dev, string); |
| 398 | if (IS_ERR(phy)) { |
| 399 | dev_err(dev, "unable to find phy\n"); |
| 400 | return phy; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (!try_module_get(phy->ops->owner)) |
| 405 | return ERR_PTR(-EPROBE_DEFER); |
| 406 | |
| 407 | get_device(&phy->dev); |
| 408 | |
| 409 | return phy; |
| 410 | } |
| 411 | EXPORT_SYMBOL_GPL(phy_get); |
| 412 | |
| 413 | /** |
| 414 | * devm_phy_get() - lookup and obtain a reference to a phy. |
| 415 | * @dev: device that requests this phy |
| 416 | * @string: the phy name as given in the dt data or phy device name |
| 417 | * for non-dt case |
| 418 | * |
| 419 | * Gets the phy using phy_get(), and associates a device with it using |
| 420 | * devres. On driver detach, release function is invoked on the devres data, |
| 421 | * then, devres data is freed. |
| 422 | */ |
| 423 | struct phy *devm_phy_get(struct device *dev, const char *string) |
| 424 | { |
| 425 | struct phy **ptr, *phy; |
| 426 | |
| 427 | ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); |
| 428 | if (!ptr) |
| 429 | return ERR_PTR(-ENOMEM); |
| 430 | |
| 431 | phy = phy_get(dev, string); |
| 432 | if (!IS_ERR(phy)) { |
| 433 | *ptr = phy; |
| 434 | devres_add(dev, ptr); |
| 435 | } else { |
| 436 | devres_free(ptr); |
| 437 | } |
| 438 | |
| 439 | return phy; |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(devm_phy_get); |
| 442 | |
| 443 | /** |
| 444 | * phy_create() - create a new phy |
| 445 | * @dev: device that is creating the new phy |
| 446 | * @ops: function pointers for performing phy operations |
| 447 | * @init_data: contains the list of PHY consumers or NULL |
| 448 | * |
| 449 | * Called to create a phy using phy framework. |
| 450 | */ |
| 451 | struct phy *phy_create(struct device *dev, const struct phy_ops *ops, |
| 452 | struct phy_init_data *init_data) |
| 453 | { |
| 454 | int ret; |
| 455 | int id; |
| 456 | struct phy *phy; |
| 457 | |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 458 | if (WARN_ON(!dev)) |
| 459 | return ERR_PTR(-EINVAL); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 460 | |
| 461 | phy = kzalloc(sizeof(*phy), GFP_KERNEL); |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 462 | if (!phy) |
| 463 | return ERR_PTR(-ENOMEM); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 464 | |
| 465 | id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL); |
| 466 | if (id < 0) { |
| 467 | dev_err(dev, "unable to get id\n"); |
| 468 | ret = id; |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 469 | goto free_phy; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | device_initialize(&phy->dev); |
| 473 | mutex_init(&phy->mutex); |
| 474 | |
| 475 | phy->dev.class = phy_class; |
| 476 | phy->dev.parent = dev; |
| 477 | phy->dev.of_node = dev->of_node; |
| 478 | phy->id = id; |
| 479 | phy->ops = ops; |
| 480 | phy->init_data = init_data; |
| 481 | |
| 482 | ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); |
| 483 | if (ret) |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 484 | goto put_dev; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 485 | |
| 486 | ret = device_add(&phy->dev); |
| 487 | if (ret) |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 488 | goto put_dev; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 489 | |
| 490 | if (pm_runtime_enabled(dev)) { |
| 491 | pm_runtime_enable(&phy->dev); |
| 492 | pm_runtime_no_callbacks(&phy->dev); |
| 493 | } |
| 494 | |
| 495 | return phy; |
| 496 | |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 497 | put_dev: |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 498 | put_device(&phy->dev); |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 499 | ida_remove(&phy_ida, phy->id); |
| 500 | free_phy: |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 501 | kfree(phy); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 502 | return ERR_PTR(ret); |
| 503 | } |
| 504 | EXPORT_SYMBOL_GPL(phy_create); |
| 505 | |
| 506 | /** |
| 507 | * devm_phy_create() - create a new phy |
| 508 | * @dev: device that is creating the new phy |
| 509 | * @ops: function pointers for performing phy operations |
| 510 | * @init_data: contains the list of PHY consumers or NULL |
| 511 | * |
| 512 | * Creates a new PHY device adding it to the PHY class. |
| 513 | * While at that, it also associates the device with the phy using devres. |
| 514 | * On driver detach, release function is invoked on the devres data, |
| 515 | * then, devres data is freed. |
| 516 | */ |
| 517 | struct phy *devm_phy_create(struct device *dev, const struct phy_ops *ops, |
| 518 | struct phy_init_data *init_data) |
| 519 | { |
| 520 | struct phy **ptr, *phy; |
| 521 | |
| 522 | ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); |
| 523 | if (!ptr) |
| 524 | return ERR_PTR(-ENOMEM); |
| 525 | |
| 526 | phy = phy_create(dev, ops, init_data); |
| 527 | if (!IS_ERR(phy)) { |
| 528 | *ptr = phy; |
| 529 | devres_add(dev, ptr); |
| 530 | } else { |
| 531 | devres_free(ptr); |
| 532 | } |
| 533 | |
| 534 | return phy; |
| 535 | } |
| 536 | EXPORT_SYMBOL_GPL(devm_phy_create); |
| 537 | |
| 538 | /** |
| 539 | * phy_destroy() - destroy the phy |
| 540 | * @phy: the phy to be destroyed |
| 541 | * |
| 542 | * Called to destroy the phy. |
| 543 | */ |
| 544 | void phy_destroy(struct phy *phy) |
| 545 | { |
| 546 | pm_runtime_disable(&phy->dev); |
| 547 | device_unregister(&phy->dev); |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(phy_destroy); |
| 550 | |
| 551 | /** |
| 552 | * devm_phy_destroy() - destroy the PHY |
| 553 | * @dev: device that wants to release this phy |
| 554 | * @phy: the phy returned by devm_phy_get() |
| 555 | * |
| 556 | * destroys the devres associated with this phy and invokes phy_destroy |
| 557 | * to destroy the phy. |
| 558 | */ |
| 559 | void devm_phy_destroy(struct device *dev, struct phy *phy) |
| 560 | { |
| 561 | int r; |
| 562 | |
| 563 | r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy); |
| 564 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(devm_phy_destroy); |
| 567 | |
| 568 | /** |
| 569 | * __of_phy_provider_register() - create/register phy provider with the framework |
| 570 | * @dev: struct device of the phy provider |
| 571 | * @owner: the module owner containing of_xlate |
| 572 | * @of_xlate: function pointer to obtain phy instance from phy provider |
| 573 | * |
| 574 | * Creates struct phy_provider from dev and of_xlate function pointer. |
| 575 | * This is used in the case of dt boot for finding the phy instance from |
| 576 | * phy provider. |
| 577 | */ |
| 578 | struct phy_provider *__of_phy_provider_register(struct device *dev, |
| 579 | struct module *owner, struct phy * (*of_xlate)(struct device *dev, |
| 580 | struct of_phandle_args *args)) |
| 581 | { |
| 582 | struct phy_provider *phy_provider; |
| 583 | |
| 584 | phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL); |
| 585 | if (!phy_provider) |
| 586 | return ERR_PTR(-ENOMEM); |
| 587 | |
| 588 | phy_provider->dev = dev; |
| 589 | phy_provider->owner = owner; |
| 590 | phy_provider->of_xlate = of_xlate; |
| 591 | |
| 592 | mutex_lock(&phy_provider_mutex); |
| 593 | list_add_tail(&phy_provider->list, &phy_provider_list); |
| 594 | mutex_unlock(&phy_provider_mutex); |
| 595 | |
| 596 | return phy_provider; |
| 597 | } |
| 598 | EXPORT_SYMBOL_GPL(__of_phy_provider_register); |
| 599 | |
| 600 | /** |
| 601 | * __devm_of_phy_provider_register() - create/register phy provider with the |
| 602 | * framework |
| 603 | * @dev: struct device of the phy provider |
| 604 | * @owner: the module owner containing of_xlate |
| 605 | * @of_xlate: function pointer to obtain phy instance from phy provider |
| 606 | * |
| 607 | * Creates struct phy_provider from dev and of_xlate function pointer. |
| 608 | * This is used in the case of dt boot for finding the phy instance from |
| 609 | * phy provider. While at that, it also associates the device with the |
| 610 | * phy provider using devres. On driver detach, release function is invoked |
| 611 | * on the devres data, then, devres data is freed. |
| 612 | */ |
| 613 | struct phy_provider *__devm_of_phy_provider_register(struct device *dev, |
| 614 | struct module *owner, struct phy * (*of_xlate)(struct device *dev, |
| 615 | struct of_phandle_args *args)) |
| 616 | { |
| 617 | struct phy_provider **ptr, *phy_provider; |
| 618 | |
| 619 | ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); |
| 620 | if (!ptr) |
| 621 | return ERR_PTR(-ENOMEM); |
| 622 | |
| 623 | phy_provider = __of_phy_provider_register(dev, owner, of_xlate); |
| 624 | if (!IS_ERR(phy_provider)) { |
| 625 | *ptr = phy_provider; |
| 626 | devres_add(dev, ptr); |
| 627 | } else { |
| 628 | devres_free(ptr); |
| 629 | } |
| 630 | |
| 631 | return phy_provider; |
| 632 | } |
| 633 | EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register); |
| 634 | |
| 635 | /** |
| 636 | * of_phy_provider_unregister() - unregister phy provider from the framework |
| 637 | * @phy_provider: phy provider returned by of_phy_provider_register() |
| 638 | * |
| 639 | * Removes the phy_provider created using of_phy_provider_register(). |
| 640 | */ |
| 641 | void of_phy_provider_unregister(struct phy_provider *phy_provider) |
| 642 | { |
| 643 | if (IS_ERR(phy_provider)) |
| 644 | return; |
| 645 | |
| 646 | mutex_lock(&phy_provider_mutex); |
| 647 | list_del(&phy_provider->list); |
| 648 | kfree(phy_provider); |
| 649 | mutex_unlock(&phy_provider_mutex); |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(of_phy_provider_unregister); |
| 652 | |
| 653 | /** |
| 654 | * devm_of_phy_provider_unregister() - remove phy provider from the framework |
| 655 | * @dev: struct device of the phy provider |
| 656 | * |
| 657 | * destroys the devres associated with this phy provider and invokes |
| 658 | * of_phy_provider_unregister to unregister the phy provider. |
| 659 | */ |
| 660 | void devm_of_phy_provider_unregister(struct device *dev, |
| 661 | struct phy_provider *phy_provider) { |
| 662 | int r; |
| 663 | |
| 664 | r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match, |
| 665 | phy_provider); |
| 666 | dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n"); |
| 667 | } |
| 668 | EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister); |
| 669 | |
| 670 | /** |
| 671 | * phy_release() - release the phy |
| 672 | * @dev: the dev member within phy |
| 673 | * |
| 674 | * When the last reference to the device is removed, it is called |
| 675 | * from the embedded kobject as release method. |
| 676 | */ |
| 677 | static void phy_release(struct device *dev) |
| 678 | { |
| 679 | struct phy *phy; |
| 680 | |
| 681 | phy = to_phy(dev); |
| 682 | dev_vdbg(dev, "releasing '%s'\n", dev_name(dev)); |
| 683 | ida_remove(&phy_ida, phy->id); |
| 684 | kfree(phy); |
| 685 | } |
| 686 | |
| 687 | static int __init phy_core_init(void) |
| 688 | { |
| 689 | phy_class = class_create(THIS_MODULE, "phy"); |
| 690 | if (IS_ERR(phy_class)) { |
| 691 | pr_err("failed to create phy class --> %ld\n", |
| 692 | PTR_ERR(phy_class)); |
| 693 | return PTR_ERR(phy_class); |
| 694 | } |
| 695 | |
| 696 | phy_class->dev_release = phy_release; |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | module_init(phy_core_init); |
| 701 | |
| 702 | static void __exit phy_core_exit(void) |
| 703 | { |
| 704 | class_destroy(phy_class); |
| 705 | } |
| 706 | module_exit(phy_core_exit); |
| 707 | |
| 708 | MODULE_DESCRIPTION("Generic PHY Framework"); |
| 709 | MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>"); |
| 710 | MODULE_LICENSE("GPL v2"); |