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 | |
Andrew Lunn | 04c2fac | 2014-02-04 18:33:11 +0100 | [diff] [blame] | 165 | if (!phy) |
| 166 | return 0; |
| 167 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 168 | ret = phy_pm_runtime_get_sync(phy); |
| 169 | if (ret < 0 && ret != -ENOTSUPP) |
| 170 | return ret; |
| 171 | |
| 172 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 173 | if (phy->init_count == 0 && phy->ops->init) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 174 | ret = phy->ops->init(phy); |
| 175 | if (ret < 0) { |
| 176 | dev_err(&phy->dev, "phy init failed --> %d\n", ret); |
| 177 | goto out; |
| 178 | } |
Hans de Goede | 767a1b5 | 2014-02-17 14:29:23 +0530 | [diff] [blame] | 179 | } else { |
| 180 | ret = 0; /* Override possible ret == -ENOTSUPP */ |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 181 | } |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 182 | ++phy->init_count; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 183 | |
| 184 | out: |
| 185 | mutex_unlock(&phy->mutex); |
| 186 | phy_pm_runtime_put(phy); |
| 187 | return ret; |
| 188 | } |
| 189 | EXPORT_SYMBOL_GPL(phy_init); |
| 190 | |
| 191 | int phy_exit(struct phy *phy) |
| 192 | { |
| 193 | int ret; |
| 194 | |
Andrew Lunn | 04c2fac | 2014-02-04 18:33:11 +0100 | [diff] [blame] | 195 | if (!phy) |
| 196 | return 0; |
| 197 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 198 | ret = phy_pm_runtime_get_sync(phy); |
| 199 | if (ret < 0 && ret != -ENOTSUPP) |
| 200 | return ret; |
| 201 | |
| 202 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 203 | if (phy->init_count == 1 && phy->ops->exit) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 204 | ret = phy->ops->exit(phy); |
| 205 | if (ret < 0) { |
| 206 | dev_err(&phy->dev, "phy exit failed --> %d\n", ret); |
| 207 | goto out; |
| 208 | } |
| 209 | } |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 210 | --phy->init_count; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 211 | |
| 212 | out: |
| 213 | mutex_unlock(&phy->mutex); |
| 214 | phy_pm_runtime_put(phy); |
| 215 | return ret; |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(phy_exit); |
| 218 | |
| 219 | int phy_power_on(struct phy *phy) |
| 220 | { |
Kishon Vijay Abraham I | d18c960 | 2013-12-19 20:01:44 +0530 | [diff] [blame] | 221 | int ret; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 222 | |
Andrew Lunn | 04c2fac | 2014-02-04 18:33:11 +0100 | [diff] [blame] | 223 | if (!phy) |
| 224 | return 0; |
| 225 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 226 | ret = phy_pm_runtime_get_sync(phy); |
| 227 | if (ret < 0 && ret != -ENOTSUPP) |
| 228 | return ret; |
| 229 | |
| 230 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 231 | if (phy->power_count == 0 && phy->ops->power_on) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 232 | ret = phy->ops->power_on(phy); |
| 233 | if (ret < 0) { |
| 234 | dev_err(&phy->dev, "phy poweron failed --> %d\n", ret); |
| 235 | goto out; |
| 236 | } |
Hans de Goede | 767a1b5 | 2014-02-17 14:29:23 +0530 | [diff] [blame] | 237 | } else { |
| 238 | ret = 0; /* Override possible ret == -ENOTSUPP */ |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 239 | } |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 240 | ++phy->power_count; |
| 241 | mutex_unlock(&phy->mutex); |
| 242 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 243 | |
| 244 | out: |
| 245 | mutex_unlock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 246 | phy_pm_runtime_put_sync(phy); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | EXPORT_SYMBOL_GPL(phy_power_on); |
| 251 | |
| 252 | int phy_power_off(struct phy *phy) |
| 253 | { |
Kishon Vijay Abraham I | d18c960 | 2013-12-19 20:01:44 +0530 | [diff] [blame] | 254 | int ret; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 255 | |
Andrew Lunn | 04c2fac | 2014-02-04 18:33:11 +0100 | [diff] [blame] | 256 | if (!phy) |
| 257 | return 0; |
| 258 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 259 | mutex_lock(&phy->mutex); |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 260 | if (phy->power_count == 1 && phy->ops->power_off) { |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 261 | ret = phy->ops->power_off(phy); |
| 262 | if (ret < 0) { |
| 263 | dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret); |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 264 | mutex_unlock(&phy->mutex); |
| 265 | return ret; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 266 | } |
| 267 | } |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 268 | --phy->power_count; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 269 | mutex_unlock(&phy->mutex); |
| 270 | phy_pm_runtime_put(phy); |
| 271 | |
Kishon Vijay Abraham I | 637d378 | 2013-12-20 10:36:49 +0530 | [diff] [blame] | 272 | return 0; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 273 | } |
| 274 | EXPORT_SYMBOL_GPL(phy_power_off); |
| 275 | |
| 276 | /** |
| 277 | * of_phy_get() - lookup and obtain a reference to a phy by phandle |
| 278 | * @dev: device that requests this phy |
| 279 | * @index: the index of the phy |
| 280 | * |
| 281 | * Returns the phy associated with the given phandle value, |
| 282 | * after getting a refcount to it or -ENODEV if there is no such phy or |
| 283 | * -EPROBE_DEFER if there is a phandle to the phy, but the device is |
| 284 | * not yet loaded. This function uses of_xlate call back function provided |
| 285 | * while registering the phy_provider to find the phy instance. |
| 286 | */ |
| 287 | static struct phy *of_phy_get(struct device *dev, int index) |
| 288 | { |
| 289 | int ret; |
| 290 | struct phy_provider *phy_provider; |
| 291 | struct phy *phy = NULL; |
| 292 | struct of_phandle_args args; |
| 293 | |
| 294 | ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells", |
| 295 | index, &args); |
| 296 | if (ret) { |
| 297 | dev_dbg(dev, "failed to get phy in %s node\n", |
| 298 | dev->of_node->full_name); |
| 299 | return ERR_PTR(-ENODEV); |
| 300 | } |
| 301 | |
| 302 | mutex_lock(&phy_provider_mutex); |
| 303 | phy_provider = of_phy_provider_lookup(args.np); |
| 304 | if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) { |
| 305 | phy = ERR_PTR(-EPROBE_DEFER); |
| 306 | goto err0; |
| 307 | } |
| 308 | |
| 309 | phy = phy_provider->of_xlate(phy_provider->dev, &args); |
| 310 | module_put(phy_provider->owner); |
| 311 | |
| 312 | err0: |
| 313 | mutex_unlock(&phy_provider_mutex); |
| 314 | of_node_put(args.np); |
| 315 | |
| 316 | return phy; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * phy_put() - release the PHY |
| 321 | * @phy: the phy returned by phy_get() |
| 322 | * |
| 323 | * Releases a refcount the caller received from phy_get(). |
| 324 | */ |
| 325 | void phy_put(struct phy *phy) |
| 326 | { |
Andrew Lunn | 04c2fac | 2014-02-04 18:33:11 +0100 | [diff] [blame] | 327 | if (!phy || IS_ERR(phy)) |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 328 | return; |
| 329 | |
| 330 | module_put(phy->ops->owner); |
| 331 | put_device(&phy->dev); |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(phy_put); |
| 334 | |
| 335 | /** |
| 336 | * devm_phy_put() - release the PHY |
| 337 | * @dev: device that wants to release this phy |
| 338 | * @phy: the phy returned by devm_phy_get() |
| 339 | * |
| 340 | * destroys the devres associated with this phy and invokes phy_put |
| 341 | * to release the phy. |
| 342 | */ |
| 343 | void devm_phy_put(struct device *dev, struct phy *phy) |
| 344 | { |
| 345 | int r; |
| 346 | |
Andrew Lunn | 04c2fac | 2014-02-04 18:33:11 +0100 | [diff] [blame] | 347 | if (!phy) |
| 348 | return; |
| 349 | |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 350 | r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy); |
| 351 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); |
| 352 | } |
| 353 | EXPORT_SYMBOL_GPL(devm_phy_put); |
| 354 | |
| 355 | /** |
| 356 | * of_phy_simple_xlate() - returns the phy instance from phy provider |
| 357 | * @dev: the PHY provider device |
| 358 | * @args: of_phandle_args (not used here) |
| 359 | * |
| 360 | * Intended to be used by phy provider for the common case where #phy-cells is |
| 361 | * 0. For other cases where #phy-cells is greater than '0', the phy provider |
| 362 | * should provide a custom of_xlate function that reads the *args* and returns |
| 363 | * the appropriate phy. |
| 364 | */ |
| 365 | struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args |
| 366 | *args) |
| 367 | { |
| 368 | struct phy *phy; |
| 369 | struct class_dev_iter iter; |
| 370 | struct device_node *node = dev->of_node; |
| 371 | |
| 372 | class_dev_iter_init(&iter, phy_class, NULL, NULL); |
| 373 | while ((dev = class_dev_iter_next(&iter))) { |
| 374 | phy = to_phy(dev); |
| 375 | if (node != phy->dev.of_node) |
| 376 | continue; |
| 377 | |
| 378 | class_dev_iter_exit(&iter); |
| 379 | return phy; |
| 380 | } |
| 381 | |
| 382 | class_dev_iter_exit(&iter); |
| 383 | return ERR_PTR(-ENODEV); |
| 384 | } |
| 385 | EXPORT_SYMBOL_GPL(of_phy_simple_xlate); |
| 386 | |
| 387 | /** |
| 388 | * phy_get() - lookup and obtain a reference to a phy. |
| 389 | * @dev: device that requests this phy |
| 390 | * @string: the phy name as given in the dt data or the name of the controller |
| 391 | * port for non-dt case |
| 392 | * |
| 393 | * Returns the phy driver, after getting a refcount to it; or |
| 394 | * -ENODEV if there is no such phy. The caller is responsible for |
| 395 | * calling phy_put() to release that count. |
| 396 | */ |
| 397 | struct phy *phy_get(struct device *dev, const char *string) |
| 398 | { |
| 399 | int index = 0; |
Kishon Vijay Abraham I | d18c960 | 2013-12-19 20:01:44 +0530 | [diff] [blame] | 400 | struct phy *phy; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 401 | |
| 402 | if (string == NULL) { |
| 403 | dev_WARN(dev, "missing string\n"); |
| 404 | return ERR_PTR(-EINVAL); |
| 405 | } |
| 406 | |
| 407 | if (dev->of_node) { |
| 408 | index = of_property_match_string(dev->of_node, "phy-names", |
| 409 | string); |
| 410 | phy = of_phy_get(dev, index); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 411 | } else { |
| 412 | phy = phy_lookup(dev, string); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 413 | } |
Hans de Goede | f40037f | 2014-02-17 14:29:22 +0530 | [diff] [blame] | 414 | if (IS_ERR(phy)) |
| 415 | return phy; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 416 | |
| 417 | if (!try_module_get(phy->ops->owner)) |
| 418 | return ERR_PTR(-EPROBE_DEFER); |
| 419 | |
| 420 | get_device(&phy->dev); |
| 421 | |
| 422 | return phy; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(phy_get); |
| 425 | |
| 426 | /** |
Andrew Lunn | 788a4d5 | 2014-02-04 18:33:12 +0100 | [diff] [blame] | 427 | * phy_optional_get() - lookup and obtain a reference to an optional phy. |
| 428 | * @dev: device that requests this phy |
| 429 | * @string: the phy name as given in the dt data or the name of the controller |
| 430 | * port for non-dt case |
| 431 | * |
| 432 | * Returns the phy driver, after getting a refcount to it; or |
| 433 | * NULL if there is no such phy. The caller is responsible for |
| 434 | * calling phy_put() to release that count. |
| 435 | */ |
| 436 | struct phy *phy_optional_get(struct device *dev, const char *string) |
| 437 | { |
| 438 | struct phy *phy = phy_get(dev, string); |
| 439 | |
| 440 | if (PTR_ERR(phy) == -ENODEV) |
| 441 | phy = NULL; |
| 442 | |
| 443 | return phy; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(phy_optional_get); |
| 446 | |
| 447 | /** |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 448 | * devm_phy_get() - lookup and obtain a reference to a phy. |
| 449 | * @dev: device that requests this phy |
| 450 | * @string: the phy name as given in the dt data or phy device name |
| 451 | * for non-dt case |
| 452 | * |
| 453 | * Gets the phy using phy_get(), and associates a device with it using |
| 454 | * devres. On driver detach, release function is invoked on the devres data, |
| 455 | * then, devres data is freed. |
| 456 | */ |
| 457 | struct phy *devm_phy_get(struct device *dev, const char *string) |
| 458 | { |
| 459 | struct phy **ptr, *phy; |
| 460 | |
| 461 | ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); |
| 462 | if (!ptr) |
| 463 | return ERR_PTR(-ENOMEM); |
| 464 | |
| 465 | phy = phy_get(dev, string); |
| 466 | if (!IS_ERR(phy)) { |
| 467 | *ptr = phy; |
| 468 | devres_add(dev, ptr); |
| 469 | } else { |
| 470 | devres_free(ptr); |
| 471 | } |
| 472 | |
| 473 | return phy; |
| 474 | } |
| 475 | EXPORT_SYMBOL_GPL(devm_phy_get); |
| 476 | |
| 477 | /** |
Andrew Lunn | 788a4d5 | 2014-02-04 18:33:12 +0100 | [diff] [blame] | 478 | * devm_phy_optional_get() - lookup and obtain a reference to an optional phy. |
| 479 | * @dev: device that requests this phy |
| 480 | * @string: the phy name as given in the dt data or phy device name |
| 481 | * for non-dt case |
| 482 | * |
| 483 | * Gets the phy using phy_get(), and associates a device with it using |
| 484 | * devres. On driver detach, release function is invoked on the devres |
| 485 | * data, then, devres data is freed. This differs to devm_phy_get() in |
| 486 | * that if the phy does not exist, it is not considered an error and |
| 487 | * -ENODEV will not be returned. Instead the NULL phy is returned, |
| 488 | * which can be passed to all other phy consumer calls. |
| 489 | */ |
| 490 | struct phy *devm_phy_optional_get(struct device *dev, const char *string) |
| 491 | { |
| 492 | struct phy *phy = devm_phy_get(dev, string); |
| 493 | |
| 494 | if (PTR_ERR(phy) == -ENODEV) |
| 495 | phy = NULL; |
| 496 | |
| 497 | return phy; |
| 498 | } |
| 499 | EXPORT_SYMBOL_GPL(devm_phy_optional_get); |
| 500 | |
| 501 | /** |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 502 | * phy_create() - create a new phy |
| 503 | * @dev: device that is creating the new phy |
| 504 | * @ops: function pointers for performing phy operations |
| 505 | * @init_data: contains the list of PHY consumers or NULL |
| 506 | * |
| 507 | * Called to create a phy using phy framework. |
| 508 | */ |
| 509 | struct phy *phy_create(struct device *dev, const struct phy_ops *ops, |
| 510 | struct phy_init_data *init_data) |
| 511 | { |
| 512 | int ret; |
| 513 | int id; |
| 514 | struct phy *phy; |
| 515 | |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 516 | if (WARN_ON(!dev)) |
| 517 | return ERR_PTR(-EINVAL); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 518 | |
| 519 | phy = kzalloc(sizeof(*phy), GFP_KERNEL); |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 520 | if (!phy) |
| 521 | return ERR_PTR(-ENOMEM); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 522 | |
| 523 | id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL); |
| 524 | if (id < 0) { |
| 525 | dev_err(dev, "unable to get id\n"); |
| 526 | ret = id; |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 527 | goto free_phy; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | device_initialize(&phy->dev); |
| 531 | mutex_init(&phy->mutex); |
| 532 | |
| 533 | phy->dev.class = phy_class; |
| 534 | phy->dev.parent = dev; |
| 535 | phy->dev.of_node = dev->of_node; |
| 536 | phy->id = id; |
| 537 | phy->ops = ops; |
| 538 | phy->init_data = init_data; |
| 539 | |
| 540 | ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); |
| 541 | if (ret) |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 542 | goto put_dev; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 543 | |
| 544 | ret = device_add(&phy->dev); |
| 545 | if (ret) |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 546 | goto put_dev; |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 547 | |
| 548 | if (pm_runtime_enabled(dev)) { |
| 549 | pm_runtime_enable(&phy->dev); |
| 550 | pm_runtime_no_callbacks(&phy->dev); |
| 551 | } |
| 552 | |
| 553 | return phy; |
| 554 | |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 555 | put_dev: |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 556 | put_device(&phy->dev); |
Dan Carpenter | 52797d2 | 2013-12-06 17:51:19 +0530 | [diff] [blame] | 557 | ida_remove(&phy_ida, phy->id); |
| 558 | free_phy: |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 559 | kfree(phy); |
Kishon Vijay Abraham I | ff76496 | 2013-09-27 11:53:25 +0530 | [diff] [blame] | 560 | return ERR_PTR(ret); |
| 561 | } |
| 562 | EXPORT_SYMBOL_GPL(phy_create); |
| 563 | |
| 564 | /** |
| 565 | * devm_phy_create() - create a new phy |
| 566 | * @dev: device that is creating the new phy |
| 567 | * @ops: function pointers for performing phy operations |
| 568 | * @init_data: contains the list of PHY consumers or NULL |
| 569 | * |
| 570 | * Creates a new PHY device adding it to the PHY class. |
| 571 | * While at that, it also associates the device with the phy using devres. |
| 572 | * On driver detach, release function is invoked on the devres data, |
| 573 | * then, devres data is freed. |
| 574 | */ |
| 575 | struct phy *devm_phy_create(struct device *dev, const struct phy_ops *ops, |
| 576 | struct phy_init_data *init_data) |
| 577 | { |
| 578 | struct phy **ptr, *phy; |
| 579 | |
| 580 | ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); |
| 581 | if (!ptr) |
| 582 | return ERR_PTR(-ENOMEM); |
| 583 | |
| 584 | phy = phy_create(dev, ops, init_data); |
| 585 | if (!IS_ERR(phy)) { |
| 586 | *ptr = phy; |
| 587 | devres_add(dev, ptr); |
| 588 | } else { |
| 589 | devres_free(ptr); |
| 590 | } |
| 591 | |
| 592 | return phy; |
| 593 | } |
| 594 | EXPORT_SYMBOL_GPL(devm_phy_create); |
| 595 | |
| 596 | /** |
| 597 | * phy_destroy() - destroy the phy |
| 598 | * @phy: the phy to be destroyed |
| 599 | * |
| 600 | * Called to destroy the phy. |
| 601 | */ |
| 602 | void phy_destroy(struct phy *phy) |
| 603 | { |
| 604 | pm_runtime_disable(&phy->dev); |
| 605 | device_unregister(&phy->dev); |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(phy_destroy); |
| 608 | |
| 609 | /** |
| 610 | * devm_phy_destroy() - destroy the PHY |
| 611 | * @dev: device that wants to release this phy |
| 612 | * @phy: the phy returned by devm_phy_get() |
| 613 | * |
| 614 | * destroys the devres associated with this phy and invokes phy_destroy |
| 615 | * to destroy the phy. |
| 616 | */ |
| 617 | void devm_phy_destroy(struct device *dev, struct phy *phy) |
| 618 | { |
| 619 | int r; |
| 620 | |
| 621 | r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy); |
| 622 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); |
| 623 | } |
| 624 | EXPORT_SYMBOL_GPL(devm_phy_destroy); |
| 625 | |
| 626 | /** |
| 627 | * __of_phy_provider_register() - create/register phy provider with the framework |
| 628 | * @dev: struct device of the phy provider |
| 629 | * @owner: the module owner containing of_xlate |
| 630 | * @of_xlate: function pointer to obtain phy instance from phy provider |
| 631 | * |
| 632 | * Creates struct phy_provider from dev and of_xlate function pointer. |
| 633 | * This is used in the case of dt boot for finding the phy instance from |
| 634 | * phy provider. |
| 635 | */ |
| 636 | struct phy_provider *__of_phy_provider_register(struct device *dev, |
| 637 | struct module *owner, struct phy * (*of_xlate)(struct device *dev, |
| 638 | struct of_phandle_args *args)) |
| 639 | { |
| 640 | struct phy_provider *phy_provider; |
| 641 | |
| 642 | phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL); |
| 643 | if (!phy_provider) |
| 644 | return ERR_PTR(-ENOMEM); |
| 645 | |
| 646 | phy_provider->dev = dev; |
| 647 | phy_provider->owner = owner; |
| 648 | phy_provider->of_xlate = of_xlate; |
| 649 | |
| 650 | mutex_lock(&phy_provider_mutex); |
| 651 | list_add_tail(&phy_provider->list, &phy_provider_list); |
| 652 | mutex_unlock(&phy_provider_mutex); |
| 653 | |
| 654 | return phy_provider; |
| 655 | } |
| 656 | EXPORT_SYMBOL_GPL(__of_phy_provider_register); |
| 657 | |
| 658 | /** |
| 659 | * __devm_of_phy_provider_register() - create/register phy provider with the |
| 660 | * framework |
| 661 | * @dev: struct device of the phy provider |
| 662 | * @owner: the module owner containing of_xlate |
| 663 | * @of_xlate: function pointer to obtain phy instance from phy provider |
| 664 | * |
| 665 | * Creates struct phy_provider from dev and of_xlate function pointer. |
| 666 | * This is used in the case of dt boot for finding the phy instance from |
| 667 | * phy provider. While at that, it also associates the device with the |
| 668 | * phy provider using devres. On driver detach, release function is invoked |
| 669 | * on the devres data, then, devres data is freed. |
| 670 | */ |
| 671 | struct phy_provider *__devm_of_phy_provider_register(struct device *dev, |
| 672 | struct module *owner, struct phy * (*of_xlate)(struct device *dev, |
| 673 | struct of_phandle_args *args)) |
| 674 | { |
| 675 | struct phy_provider **ptr, *phy_provider; |
| 676 | |
| 677 | ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); |
| 678 | if (!ptr) |
| 679 | return ERR_PTR(-ENOMEM); |
| 680 | |
| 681 | phy_provider = __of_phy_provider_register(dev, owner, of_xlate); |
| 682 | if (!IS_ERR(phy_provider)) { |
| 683 | *ptr = phy_provider; |
| 684 | devres_add(dev, ptr); |
| 685 | } else { |
| 686 | devres_free(ptr); |
| 687 | } |
| 688 | |
| 689 | return phy_provider; |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register); |
| 692 | |
| 693 | /** |
| 694 | * of_phy_provider_unregister() - unregister phy provider from the framework |
| 695 | * @phy_provider: phy provider returned by of_phy_provider_register() |
| 696 | * |
| 697 | * Removes the phy_provider created using of_phy_provider_register(). |
| 698 | */ |
| 699 | void of_phy_provider_unregister(struct phy_provider *phy_provider) |
| 700 | { |
| 701 | if (IS_ERR(phy_provider)) |
| 702 | return; |
| 703 | |
| 704 | mutex_lock(&phy_provider_mutex); |
| 705 | list_del(&phy_provider->list); |
| 706 | kfree(phy_provider); |
| 707 | mutex_unlock(&phy_provider_mutex); |
| 708 | } |
| 709 | EXPORT_SYMBOL_GPL(of_phy_provider_unregister); |
| 710 | |
| 711 | /** |
| 712 | * devm_of_phy_provider_unregister() - remove phy provider from the framework |
| 713 | * @dev: struct device of the phy provider |
| 714 | * |
| 715 | * destroys the devres associated with this phy provider and invokes |
| 716 | * of_phy_provider_unregister to unregister the phy provider. |
| 717 | */ |
| 718 | void devm_of_phy_provider_unregister(struct device *dev, |
| 719 | struct phy_provider *phy_provider) { |
| 720 | int r; |
| 721 | |
| 722 | r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match, |
| 723 | phy_provider); |
| 724 | dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n"); |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister); |
| 727 | |
| 728 | /** |
| 729 | * phy_release() - release the phy |
| 730 | * @dev: the dev member within phy |
| 731 | * |
| 732 | * When the last reference to the device is removed, it is called |
| 733 | * from the embedded kobject as release method. |
| 734 | */ |
| 735 | static void phy_release(struct device *dev) |
| 736 | { |
| 737 | struct phy *phy; |
| 738 | |
| 739 | phy = to_phy(dev); |
| 740 | dev_vdbg(dev, "releasing '%s'\n", dev_name(dev)); |
| 741 | ida_remove(&phy_ida, phy->id); |
| 742 | kfree(phy); |
| 743 | } |
| 744 | |
| 745 | static int __init phy_core_init(void) |
| 746 | { |
| 747 | phy_class = class_create(THIS_MODULE, "phy"); |
| 748 | if (IS_ERR(phy_class)) { |
| 749 | pr_err("failed to create phy class --> %ld\n", |
| 750 | PTR_ERR(phy_class)); |
| 751 | return PTR_ERR(phy_class); |
| 752 | } |
| 753 | |
| 754 | phy_class->dev_release = phy_release; |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | module_init(phy_core_init); |
| 759 | |
| 760 | static void __exit phy_core_exit(void) |
| 761 | { |
| 762 | class_destroy(phy_class); |
| 763 | } |
| 764 | module_exit(phy_core_exit); |
| 765 | |
| 766 | MODULE_DESCRIPTION("Generic PHY Framework"); |
| 767 | MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>"); |
| 768 | MODULE_LICENSE("GPL v2"); |