blob: a12d35338313bd4f4a67117f3c5c3f76030e53fd [file] [log] [blame]
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +05301/*
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>
Roger Quadros3be88122014-07-04 12:55:45 +030024#include <linux/regulator/consumer.h>
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053025
26static struct class *phy_class;
27static DEFINE_MUTEX(phy_provider_mutex);
28static LIST_HEAD(phy_provider_list);
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +020029static LIST_HEAD(phys);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053030static DEFINE_IDA(phy_ida);
31
32static void devm_phy_release(struct device *dev, void *res)
33{
34 struct phy *phy = *(struct phy **)res;
35
36 phy_put(phy);
37}
38
39static void devm_phy_provider_release(struct device *dev, void *res)
40{
41 struct phy_provider *phy_provider = *(struct phy_provider **)res;
42
43 of_phy_provider_unregister(phy_provider);
44}
45
46static void devm_phy_consume(struct device *dev, void *res)
47{
48 struct phy *phy = *(struct phy **)res;
49
50 phy_destroy(phy);
51}
52
53static int devm_phy_match(struct device *dev, void *res, void *match_data)
54{
55 return res == match_data;
56}
57
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +020058/**
59 * phy_create_lookup() - allocate and register PHY/device association
60 * @phy: the phy of the association
61 * @con_id: connection ID string on device
62 * @dev_id: the device of the association
63 *
64 * Creates and registers phy_lookup entry.
65 */
66int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
67{
68 struct phy_lookup *pl;
69
70 if (!phy || !dev_id || !con_id)
71 return -EINVAL;
72
73 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
74 if (!pl)
75 return -ENOMEM;
76
77 pl->dev_id = dev_id;
78 pl->con_id = con_id;
79 pl->phy = phy;
80
81 mutex_lock(&phy_provider_mutex);
82 list_add_tail(&pl->node, &phys);
83 mutex_unlock(&phy_provider_mutex);
84
85 return 0;
86}
87EXPORT_SYMBOL_GPL(phy_create_lookup);
88
89/**
90 * phy_remove_lookup() - find and remove PHY/device association
91 * @phy: the phy of the association
92 * @con_id: connection ID string on device
93 * @dev_id: the device of the association
94 *
95 * Finds and unregisters phy_lookup entry that was created with
96 * phy_create_lookup().
97 */
98void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
99{
100 struct phy_lookup *pl;
101
102 if (!phy || !dev_id || !con_id)
103 return;
104
105 mutex_lock(&phy_provider_mutex);
106 list_for_each_entry(pl, &phys, node)
107 if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
108 !strcmp(pl->con_id, con_id)) {
109 list_del(&pl->node);
110 kfree(pl);
111 break;
112 }
113 mutex_unlock(&phy_provider_mutex);
114}
115EXPORT_SYMBOL_GPL(phy_remove_lookup);
116
117static struct phy *phy_find(struct device *dev, const char *con_id)
118{
119 const char *dev_id = dev_name(dev);
120 struct phy_lookup *p, *pl = NULL;
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +0200121
122 mutex_lock(&phy_provider_mutex);
123 list_for_each_entry(p, &phys, node)
124 if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
125 pl = p;
126 break;
127 }
128 mutex_unlock(&phy_provider_mutex);
129
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200130 return pl ? pl->phy : ERR_PTR(-ENODEV);
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +0200131}
132
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530133static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
134{
135 struct phy_provider *phy_provider;
Kishon Vijay Abraham I2a4c3702014-07-14 15:55:01 +0530136 struct device_node *child;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530137
138 list_for_each_entry(phy_provider, &phy_provider_list, list) {
139 if (phy_provider->dev->of_node == node)
140 return phy_provider;
Kishon Vijay Abraham I2a4c3702014-07-14 15:55:01 +0530141
142 for_each_child_of_node(phy_provider->dev->of_node, child)
143 if (child == node)
144 return phy_provider;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530145 }
146
147 return ERR_PTR(-EPROBE_DEFER);
148}
149
150int phy_pm_runtime_get(struct phy *phy)
151{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600152 int ret;
153
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530154 if (!pm_runtime_enabled(&phy->dev))
155 return -ENOTSUPP;
156
Felipe Balbicedb7f82013-12-20 15:00:48 -0600157 ret = pm_runtime_get(&phy->dev);
158 if (ret < 0 && ret != -EINPROGRESS)
159 pm_runtime_put_noidle(&phy->dev);
160
161 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530162}
163EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
164
165int phy_pm_runtime_get_sync(struct phy *phy)
166{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600167 int ret;
168
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530169 if (!pm_runtime_enabled(&phy->dev))
170 return -ENOTSUPP;
171
Felipe Balbicedb7f82013-12-20 15:00:48 -0600172 ret = pm_runtime_get_sync(&phy->dev);
173 if (ret < 0)
174 pm_runtime_put_sync(&phy->dev);
175
176 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530177}
178EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
179
180int phy_pm_runtime_put(struct phy *phy)
181{
182 if (!pm_runtime_enabled(&phy->dev))
183 return -ENOTSUPP;
184
185 return pm_runtime_put(&phy->dev);
186}
187EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
188
189int phy_pm_runtime_put_sync(struct phy *phy)
190{
191 if (!pm_runtime_enabled(&phy->dev))
192 return -ENOTSUPP;
193
194 return pm_runtime_put_sync(&phy->dev);
195}
196EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
197
198void phy_pm_runtime_allow(struct phy *phy)
199{
200 if (!pm_runtime_enabled(&phy->dev))
201 return;
202
203 pm_runtime_allow(&phy->dev);
204}
205EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
206
207void phy_pm_runtime_forbid(struct phy *phy)
208{
209 if (!pm_runtime_enabled(&phy->dev))
210 return;
211
212 pm_runtime_forbid(&phy->dev);
213}
214EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
215
216int phy_init(struct phy *phy)
217{
218 int ret;
219
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100220 if (!phy)
221 return 0;
222
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530223 ret = phy_pm_runtime_get_sync(phy);
224 if (ret < 0 && ret != -ENOTSUPP)
225 return ret;
226
227 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530228 if (phy->init_count == 0 && phy->ops->init) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530229 ret = phy->ops->init(phy);
230 if (ret < 0) {
231 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
232 goto out;
233 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530234 } else {
235 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530236 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530237 ++phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530238
239out:
240 mutex_unlock(&phy->mutex);
241 phy_pm_runtime_put(phy);
242 return ret;
243}
244EXPORT_SYMBOL_GPL(phy_init);
245
246int phy_exit(struct phy *phy)
247{
248 int ret;
249
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100250 if (!phy)
251 return 0;
252
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530253 ret = phy_pm_runtime_get_sync(phy);
254 if (ret < 0 && ret != -ENOTSUPP)
255 return ret;
256
257 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530258 if (phy->init_count == 1 && phy->ops->exit) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530259 ret = phy->ops->exit(phy);
260 if (ret < 0) {
261 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
262 goto out;
263 }
264 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530265 --phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530266
267out:
268 mutex_unlock(&phy->mutex);
269 phy_pm_runtime_put(phy);
270 return ret;
271}
272EXPORT_SYMBOL_GPL(phy_exit);
273
274int phy_power_on(struct phy *phy)
275{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530276 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530277
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100278 if (!phy)
279 return 0;
280
Roger Quadros3be88122014-07-04 12:55:45 +0300281 if (phy->pwr) {
282 ret = regulator_enable(phy->pwr);
283 if (ret)
284 return ret;
285 }
286
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530287 ret = phy_pm_runtime_get_sync(phy);
288 if (ret < 0 && ret != -ENOTSUPP)
289 return ret;
290
291 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530292 if (phy->power_count == 0 && phy->ops->power_on) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530293 ret = phy->ops->power_on(phy);
294 if (ret < 0) {
295 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
296 goto out;
297 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530298 } else {
299 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530300 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530301 ++phy->power_count;
302 mutex_unlock(&phy->mutex);
303 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530304
305out:
306 mutex_unlock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530307 phy_pm_runtime_put_sync(phy);
Roger Quadros3be88122014-07-04 12:55:45 +0300308 if (phy->pwr)
309 regulator_disable(phy->pwr);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530310
311 return ret;
312}
313EXPORT_SYMBOL_GPL(phy_power_on);
314
315int phy_power_off(struct phy *phy)
316{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530317 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530318
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100319 if (!phy)
320 return 0;
321
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530322 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530323 if (phy->power_count == 1 && phy->ops->power_off) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530324 ret = phy->ops->power_off(phy);
325 if (ret < 0) {
326 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530327 mutex_unlock(&phy->mutex);
328 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530329 }
330 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530331 --phy->power_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530332 mutex_unlock(&phy->mutex);
333 phy_pm_runtime_put(phy);
334
Roger Quadros3be88122014-07-04 12:55:45 +0300335 if (phy->pwr)
336 regulator_disable(phy->pwr);
337
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530338 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530339}
340EXPORT_SYMBOL_GPL(phy_power_off);
341
342/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100343 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
344 * @np: device_node for which to get the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530345 * @index: the index of the phy
346 *
347 * Returns the phy associated with the given phandle value,
348 * after getting a refcount to it or -ENODEV if there is no such phy or
349 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
350 * not yet loaded. This function uses of_xlate call back function provided
351 * while registering the phy_provider to find the phy instance.
352 */
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100353static struct phy *_of_phy_get(struct device_node *np, int index)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530354{
355 int ret;
356 struct phy_provider *phy_provider;
357 struct phy *phy = NULL;
358 struct of_phandle_args args;
359
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100360 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530361 index, &args);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100362 if (ret)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530363 return ERR_PTR(-ENODEV);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530364
365 mutex_lock(&phy_provider_mutex);
366 phy_provider = of_phy_provider_lookup(args.np);
367 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
368 phy = ERR_PTR(-EPROBE_DEFER);
369 goto err0;
370 }
371
372 phy = phy_provider->of_xlate(phy_provider->dev, &args);
373 module_put(phy_provider->owner);
374
375err0:
376 mutex_unlock(&phy_provider_mutex);
377 of_node_put(args.np);
378
379 return phy;
380}
381
382/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100383 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
384 * @np: device_node for which to get the phy
385 * @con_id: name of the phy from device's point of view
386 *
387 * Returns the phy driver, after getting a refcount to it; or
388 * -ENODEV if there is no such phy. The caller is responsible for
389 * calling phy_put() to release that count.
390 */
391struct phy *of_phy_get(struct device_node *np, const char *con_id)
392{
393 struct phy *phy = NULL;
394 int index = 0;
395
396 if (con_id)
397 index = of_property_match_string(np, "phy-names", con_id);
398
399 phy = _of_phy_get(np, index);
400 if (IS_ERR(phy))
401 return phy;
402
403 if (!try_module_get(phy->ops->owner))
404 return ERR_PTR(-EPROBE_DEFER);
405
406 get_device(&phy->dev);
407
408 return phy;
409}
410EXPORT_SYMBOL_GPL(of_phy_get);
411
412/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530413 * phy_put() - release the PHY
414 * @phy: the phy returned by phy_get()
415 *
416 * Releases a refcount the caller received from phy_get().
417 */
418void phy_put(struct phy *phy)
419{
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100420 if (!phy || IS_ERR(phy))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530421 return;
422
423 module_put(phy->ops->owner);
424 put_device(&phy->dev);
425}
426EXPORT_SYMBOL_GPL(phy_put);
427
428/**
429 * devm_phy_put() - release the PHY
430 * @dev: device that wants to release this phy
431 * @phy: the phy returned by devm_phy_get()
432 *
433 * destroys the devres associated with this phy and invokes phy_put
434 * to release the phy.
435 */
436void devm_phy_put(struct device *dev, struct phy *phy)
437{
438 int r;
439
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100440 if (!phy)
441 return;
442
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530443 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
444 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
445}
446EXPORT_SYMBOL_GPL(devm_phy_put);
447
448/**
449 * of_phy_simple_xlate() - returns the phy instance from phy provider
450 * @dev: the PHY provider device
451 * @args: of_phandle_args (not used here)
452 *
453 * Intended to be used by phy provider for the common case where #phy-cells is
454 * 0. For other cases where #phy-cells is greater than '0', the phy provider
455 * should provide a custom of_xlate function that reads the *args* and returns
456 * the appropriate phy.
457 */
458struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
459 *args)
460{
461 struct phy *phy;
462 struct class_dev_iter iter;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530463
464 class_dev_iter_init(&iter, phy_class, NULL, NULL);
465 while ((dev = class_dev_iter_next(&iter))) {
466 phy = to_phy(dev);
Kishon Vijay Abraham I491e0492014-10-31 14:04:20 +0530467 if (args->np != phy->dev.of_node)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530468 continue;
469
470 class_dev_iter_exit(&iter);
471 return phy;
472 }
473
474 class_dev_iter_exit(&iter);
475 return ERR_PTR(-ENODEV);
476}
477EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
478
479/**
480 * phy_get() - lookup and obtain a reference to a phy.
481 * @dev: device that requests this phy
482 * @string: the phy name as given in the dt data or the name of the controller
483 * port for non-dt case
484 *
485 * Returns the phy driver, after getting a refcount to it; or
486 * -ENODEV if there is no such phy. The caller is responsible for
487 * calling phy_put() to release that count.
488 */
489struct phy *phy_get(struct device *dev, const char *string)
490{
491 int index = 0;
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530492 struct phy *phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530493
494 if (string == NULL) {
495 dev_WARN(dev, "missing string\n");
496 return ERR_PTR(-EINVAL);
497 }
498
499 if (dev->of_node) {
500 index = of_property_match_string(dev->of_node, "phy-names",
501 string);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100502 phy = _of_phy_get(dev->of_node, index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530503 } else {
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +0200504 phy = phy_find(dev, string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530505 }
Hans de Goedef40037f2014-02-17 14:29:22 +0530506 if (IS_ERR(phy))
507 return phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530508
509 if (!try_module_get(phy->ops->owner))
510 return ERR_PTR(-EPROBE_DEFER);
511
512 get_device(&phy->dev);
513
514 return phy;
515}
516EXPORT_SYMBOL_GPL(phy_get);
517
518/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100519 * phy_optional_get() - lookup and obtain a reference to an optional phy.
520 * @dev: device that requests this phy
521 * @string: the phy name as given in the dt data or the name of the controller
522 * port for non-dt case
523 *
524 * Returns the phy driver, after getting a refcount to it; or
525 * NULL if there is no such phy. The caller is responsible for
526 * calling phy_put() to release that count.
527 */
528struct phy *phy_optional_get(struct device *dev, const char *string)
529{
530 struct phy *phy = phy_get(dev, string);
531
532 if (PTR_ERR(phy) == -ENODEV)
533 phy = NULL;
534
535 return phy;
536}
537EXPORT_SYMBOL_GPL(phy_optional_get);
538
539/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530540 * devm_phy_get() - lookup and obtain a reference to a phy.
541 * @dev: device that requests this phy
542 * @string: the phy name as given in the dt data or phy device name
543 * for non-dt case
544 *
545 * Gets the phy using phy_get(), and associates a device with it using
546 * devres. On driver detach, release function is invoked on the devres data,
547 * then, devres data is freed.
548 */
549struct phy *devm_phy_get(struct device *dev, const char *string)
550{
551 struct phy **ptr, *phy;
552
553 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
554 if (!ptr)
555 return ERR_PTR(-ENOMEM);
556
557 phy = phy_get(dev, string);
558 if (!IS_ERR(phy)) {
559 *ptr = phy;
560 devres_add(dev, ptr);
561 } else {
562 devres_free(ptr);
563 }
564
565 return phy;
566}
567EXPORT_SYMBOL_GPL(devm_phy_get);
568
569/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100570 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
571 * @dev: device that requests this phy
572 * @string: the phy name as given in the dt data or phy device name
573 * for non-dt case
574 *
575 * Gets the phy using phy_get(), and associates a device with it using
576 * devres. On driver detach, release function is invoked on the devres
577 * data, then, devres data is freed. This differs to devm_phy_get() in
578 * that if the phy does not exist, it is not considered an error and
579 * -ENODEV will not be returned. Instead the NULL phy is returned,
580 * which can be passed to all other phy consumer calls.
581 */
582struct phy *devm_phy_optional_get(struct device *dev, const char *string)
583{
584 struct phy *phy = devm_phy_get(dev, string);
585
586 if (PTR_ERR(phy) == -ENODEV)
587 phy = NULL;
588
589 return phy;
590}
591EXPORT_SYMBOL_GPL(devm_phy_optional_get);
592
593/**
Kamil Debskib5d682f2014-03-06 12:16:47 +0100594 * devm_of_phy_get() - lookup and obtain a reference to a phy.
595 * @dev: device that requests this phy
596 * @np: node containing the phy
597 * @con_id: name of the phy from device's point of view
598 *
599 * Gets the phy using of_phy_get(), and associates a device with it using
600 * devres. On driver detach, release function is invoked on the devres data,
601 * then, devres data is freed.
602 */
603struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
604 const char *con_id)
605{
606 struct phy **ptr, *phy;
607
608 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
609 if (!ptr)
610 return ERR_PTR(-ENOMEM);
611
612 phy = of_phy_get(np, con_id);
613 if (!IS_ERR(phy)) {
614 *ptr = phy;
615 devres_add(dev, ptr);
616 } else {
617 devres_free(ptr);
618 }
619
620 return phy;
621}
622EXPORT_SYMBOL_GPL(devm_of_phy_get);
623
624/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530625 * phy_create() - create a new phy
626 * @dev: device that is creating the new phy
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530627 * @node: device node of the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530628 * @ops: function pointers for performing phy operations
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530629 *
630 * Called to create a phy using phy framework.
631 */
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530632struct phy *phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200633 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530634{
635 int ret;
636 int id;
637 struct phy *phy;
638
Dan Carpenter52797d22013-12-06 17:51:19 +0530639 if (WARN_ON(!dev))
640 return ERR_PTR(-EINVAL);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530641
642 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Dan Carpenter52797d22013-12-06 17:51:19 +0530643 if (!phy)
644 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530645
646 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
647 if (id < 0) {
648 dev_err(dev, "unable to get id\n");
649 ret = id;
Dan Carpenter52797d22013-12-06 17:51:19 +0530650 goto free_phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530651 }
652
Roger Quadros3be88122014-07-04 12:55:45 +0300653 /* phy-supply */
654 phy->pwr = regulator_get_optional(dev, "phy");
655 if (IS_ERR(phy->pwr)) {
656 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
657 ret = -EPROBE_DEFER;
658 goto free_ida;
659 }
660 phy->pwr = NULL;
661 }
662
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530663 device_initialize(&phy->dev);
664 mutex_init(&phy->mutex);
665
666 phy->dev.class = phy_class;
667 phy->dev.parent = dev;
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530668 phy->dev.of_node = node ?: dev->of_node;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530669 phy->id = id;
670 phy->ops = ops;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530671
672 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
673 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530674 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530675
676 ret = device_add(&phy->dev);
677 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530678 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530679
680 if (pm_runtime_enabled(dev)) {
681 pm_runtime_enable(&phy->dev);
682 pm_runtime_no_callbacks(&phy->dev);
683 }
684
685 return phy;
686
Dan Carpenter52797d22013-12-06 17:51:19 +0530687put_dev:
Roger Quadrose73b49f2014-07-10 11:55:02 +0530688 put_device(&phy->dev); /* calls phy_release() which frees resources */
689 return ERR_PTR(ret);
690
Roger Quadros3be88122014-07-04 12:55:45 +0300691free_ida:
692 ida_simple_remove(&phy_ida, phy->id);
693
Dan Carpenter52797d22013-12-06 17:51:19 +0530694free_phy:
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530695 kfree(phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530696 return ERR_PTR(ret);
697}
698EXPORT_SYMBOL_GPL(phy_create);
699
700/**
701 * devm_phy_create() - create a new phy
702 * @dev: device that is creating the new phy
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530703 * @node: device node of the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530704 * @ops: function pointers for performing phy operations
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530705 *
706 * Creates a new PHY device adding it to the PHY class.
707 * While at that, it also associates the device with the phy using devres.
708 * On driver detach, release function is invoked on the devres data,
709 * then, devres data is freed.
710 */
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530711struct phy *devm_phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200712 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530713{
714 struct phy **ptr, *phy;
715
716 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
717 if (!ptr)
718 return ERR_PTR(-ENOMEM);
719
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200720 phy = phy_create(dev, node, ops);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530721 if (!IS_ERR(phy)) {
722 *ptr = phy;
723 devres_add(dev, ptr);
724 } else {
725 devres_free(ptr);
726 }
727
728 return phy;
729}
730EXPORT_SYMBOL_GPL(devm_phy_create);
731
732/**
733 * phy_destroy() - destroy the phy
734 * @phy: the phy to be destroyed
735 *
736 * Called to destroy the phy.
737 */
738void phy_destroy(struct phy *phy)
739{
740 pm_runtime_disable(&phy->dev);
741 device_unregister(&phy->dev);
742}
743EXPORT_SYMBOL_GPL(phy_destroy);
744
745/**
746 * devm_phy_destroy() - destroy the PHY
747 * @dev: device that wants to release this phy
748 * @phy: the phy returned by devm_phy_get()
749 *
750 * destroys the devres associated with this phy and invokes phy_destroy
751 * to destroy the phy.
752 */
753void devm_phy_destroy(struct device *dev, struct phy *phy)
754{
755 int r;
756
757 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
758 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
759}
760EXPORT_SYMBOL_GPL(devm_phy_destroy);
761
762/**
763 * __of_phy_provider_register() - create/register phy provider with the framework
764 * @dev: struct device of the phy provider
765 * @owner: the module owner containing of_xlate
766 * @of_xlate: function pointer to obtain phy instance from phy provider
767 *
768 * Creates struct phy_provider from dev and of_xlate function pointer.
769 * This is used in the case of dt boot for finding the phy instance from
770 * phy provider.
771 */
772struct phy_provider *__of_phy_provider_register(struct device *dev,
773 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
774 struct of_phandle_args *args))
775{
776 struct phy_provider *phy_provider;
777
778 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
779 if (!phy_provider)
780 return ERR_PTR(-ENOMEM);
781
782 phy_provider->dev = dev;
783 phy_provider->owner = owner;
784 phy_provider->of_xlate = of_xlate;
785
786 mutex_lock(&phy_provider_mutex);
787 list_add_tail(&phy_provider->list, &phy_provider_list);
788 mutex_unlock(&phy_provider_mutex);
789
790 return phy_provider;
791}
792EXPORT_SYMBOL_GPL(__of_phy_provider_register);
793
794/**
795 * __devm_of_phy_provider_register() - create/register phy provider with the
796 * framework
797 * @dev: struct device of the phy provider
798 * @owner: the module owner containing of_xlate
799 * @of_xlate: function pointer to obtain phy instance from phy provider
800 *
801 * Creates struct phy_provider from dev and of_xlate function pointer.
802 * This is used in the case of dt boot for finding the phy instance from
803 * phy provider. While at that, it also associates the device with the
804 * phy provider using devres. On driver detach, release function is invoked
805 * on the devres data, then, devres data is freed.
806 */
807struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
808 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
809 struct of_phandle_args *args))
810{
811 struct phy_provider **ptr, *phy_provider;
812
813 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
814 if (!ptr)
815 return ERR_PTR(-ENOMEM);
816
817 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
818 if (!IS_ERR(phy_provider)) {
819 *ptr = phy_provider;
820 devres_add(dev, ptr);
821 } else {
822 devres_free(ptr);
823 }
824
825 return phy_provider;
826}
827EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
828
829/**
830 * of_phy_provider_unregister() - unregister phy provider from the framework
831 * @phy_provider: phy provider returned by of_phy_provider_register()
832 *
833 * Removes the phy_provider created using of_phy_provider_register().
834 */
835void of_phy_provider_unregister(struct phy_provider *phy_provider)
836{
837 if (IS_ERR(phy_provider))
838 return;
839
840 mutex_lock(&phy_provider_mutex);
841 list_del(&phy_provider->list);
842 kfree(phy_provider);
843 mutex_unlock(&phy_provider_mutex);
844}
845EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
846
847/**
848 * devm_of_phy_provider_unregister() - remove phy provider from the framework
849 * @dev: struct device of the phy provider
850 *
851 * destroys the devres associated with this phy provider and invokes
852 * of_phy_provider_unregister to unregister the phy provider.
853 */
854void devm_of_phy_provider_unregister(struct device *dev,
855 struct phy_provider *phy_provider) {
856 int r;
857
858 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
859 phy_provider);
860 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
861}
862EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
863
864/**
865 * phy_release() - release the phy
866 * @dev: the dev member within phy
867 *
868 * When the last reference to the device is removed, it is called
869 * from the embedded kobject as release method.
870 */
871static void phy_release(struct device *dev)
872{
873 struct phy *phy;
874
875 phy = to_phy(dev);
876 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
Roger Quadros3be88122014-07-04 12:55:45 +0300877 regulator_put(phy->pwr);
Roger Quadrose73b49f2014-07-10 11:55:02 +0530878 ida_simple_remove(&phy_ida, phy->id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530879 kfree(phy);
880}
881
882static int __init phy_core_init(void)
883{
884 phy_class = class_create(THIS_MODULE, "phy");
885 if (IS_ERR(phy_class)) {
886 pr_err("failed to create phy class --> %ld\n",
887 PTR_ERR(phy_class));
888 return PTR_ERR(phy_class);
889 }
890
891 phy_class->dev_release = phy_release;
892
893 return 0;
894}
895module_init(phy_core_init);
896
897static void __exit phy_core_exit(void)
898{
899 class_destroy(phy_class);
900}
901module_exit(phy_core_exit);
902
903MODULE_DESCRIPTION("Generic PHY Framework");
904MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
905MODULE_LICENSE("GPL v2");