phy: twl4030: use the new lookup method
[linux-2.6-block.git] / drivers / phy / phy-core.c
CommitLineData
ff764963
KVA
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>
3be88125 24#include <linux/regulator/consumer.h>
ff764963
KVA
25
26static struct class *phy_class;
27static DEFINE_MUTEX(phy_provider_mutex);
28static LIST_HEAD(phy_provider_list);
b7bc15b9 29static LIST_HEAD(phys);
ff764963
KVA
30static 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
58static struct phy *phy_lookup(struct device *device, const char *port)
59{
60 unsigned int count;
61 struct phy *phy;
62 struct device *dev;
63 struct phy_consumer *consumers;
64 struct class_dev_iter iter;
65
66 class_dev_iter_init(&iter, phy_class, NULL, NULL);
67 while ((dev = class_dev_iter_next(&iter))) {
68 phy = to_phy(dev);
743bb387
SS
69
70 if (!phy->init_data)
71 continue;
ff764963
KVA
72 count = phy->init_data->num_consumers;
73 consumers = phy->init_data->consumers;
74 while (count--) {
75 if (!strcmp(consumers->dev_name, dev_name(device)) &&
76 !strcmp(consumers->port, port)) {
77 class_dev_iter_exit(&iter);
78 return phy;
79 }
80 consumers++;
81 }
82 }
83
84 class_dev_iter_exit(&iter);
85 return ERR_PTR(-ENODEV);
86}
87
b7bc15b9
HK
88/**
89 * phy_create_lookup() - allocate and register PHY/device association
90 * @phy: the phy of the association
91 * @con_id: connection ID string on device
92 * @dev_id: the device of the association
93 *
94 * Creates and registers phy_lookup entry.
95 */
96int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
97{
98 struct phy_lookup *pl;
99
100 if (!phy || !dev_id || !con_id)
101 return -EINVAL;
102
103 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
104 if (!pl)
105 return -ENOMEM;
106
107 pl->dev_id = dev_id;
108 pl->con_id = con_id;
109 pl->phy = phy;
110
111 mutex_lock(&phy_provider_mutex);
112 list_add_tail(&pl->node, &phys);
113 mutex_unlock(&phy_provider_mutex);
114
115 return 0;
116}
117EXPORT_SYMBOL_GPL(phy_create_lookup);
118
119/**
120 * phy_remove_lookup() - find and remove PHY/device association
121 * @phy: the phy of the association
122 * @con_id: connection ID string on device
123 * @dev_id: the device of the association
124 *
125 * Finds and unregisters phy_lookup entry that was created with
126 * phy_create_lookup().
127 */
128void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
129{
130 struct phy_lookup *pl;
131
132 if (!phy || !dev_id || !con_id)
133 return;
134
135 mutex_lock(&phy_provider_mutex);
136 list_for_each_entry(pl, &phys, node)
137 if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
138 !strcmp(pl->con_id, con_id)) {
139 list_del(&pl->node);
140 kfree(pl);
141 break;
142 }
143 mutex_unlock(&phy_provider_mutex);
144}
145EXPORT_SYMBOL_GPL(phy_remove_lookup);
146
147static struct phy *phy_find(struct device *dev, const char *con_id)
148{
149 const char *dev_id = dev_name(dev);
150 struct phy_lookup *p, *pl = NULL;
151 struct phy *phy;
152
153 mutex_lock(&phy_provider_mutex);
154 list_for_each_entry(p, &phys, node)
155 if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
156 pl = p;
157 break;
158 }
159 mutex_unlock(&phy_provider_mutex);
160
161 phy = pl ? pl->phy : ERR_PTR(-ENODEV);
162
163 /* fall-back to the old lookup method for now */
164 if (IS_ERR(phy))
165 phy = phy_lookup(dev, con_id);
166 return phy;
167}
168
ff764963
KVA
169static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
170{
171 struct phy_provider *phy_provider;
2a4c3701 172 struct device_node *child;
ff764963
KVA
173
174 list_for_each_entry(phy_provider, &phy_provider_list, list) {
175 if (phy_provider->dev->of_node == node)
176 return phy_provider;
2a4c3701
KVA
177
178 for_each_child_of_node(phy_provider->dev->of_node, child)
179 if (child == node)
180 return phy_provider;
ff764963
KVA
181 }
182
183 return ERR_PTR(-EPROBE_DEFER);
184}
185
186int phy_pm_runtime_get(struct phy *phy)
187{
cedb7f89
FB
188 int ret;
189
ff764963
KVA
190 if (!pm_runtime_enabled(&phy->dev))
191 return -ENOTSUPP;
192
cedb7f89
FB
193 ret = pm_runtime_get(&phy->dev);
194 if (ret < 0 && ret != -EINPROGRESS)
195 pm_runtime_put_noidle(&phy->dev);
196
197 return ret;
ff764963
KVA
198}
199EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
200
201int phy_pm_runtime_get_sync(struct phy *phy)
202{
cedb7f89
FB
203 int ret;
204
ff764963
KVA
205 if (!pm_runtime_enabled(&phy->dev))
206 return -ENOTSUPP;
207
cedb7f89
FB
208 ret = pm_runtime_get_sync(&phy->dev);
209 if (ret < 0)
210 pm_runtime_put_sync(&phy->dev);
211
212 return ret;
ff764963
KVA
213}
214EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
215
216int phy_pm_runtime_put(struct phy *phy)
217{
218 if (!pm_runtime_enabled(&phy->dev))
219 return -ENOTSUPP;
220
221 return pm_runtime_put(&phy->dev);
222}
223EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
224
225int phy_pm_runtime_put_sync(struct phy *phy)
226{
227 if (!pm_runtime_enabled(&phy->dev))
228 return -ENOTSUPP;
229
230 return pm_runtime_put_sync(&phy->dev);
231}
232EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
233
234void phy_pm_runtime_allow(struct phy *phy)
235{
236 if (!pm_runtime_enabled(&phy->dev))
237 return;
238
239 pm_runtime_allow(&phy->dev);
240}
241EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
242
243void phy_pm_runtime_forbid(struct phy *phy)
244{
245 if (!pm_runtime_enabled(&phy->dev))
246 return;
247
248 pm_runtime_forbid(&phy->dev);
249}
250EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
251
252int phy_init(struct phy *phy)
253{
254 int ret;
255
04c2faca
AL
256 if (!phy)
257 return 0;
258
ff764963
KVA
259 ret = phy_pm_runtime_get_sync(phy);
260 if (ret < 0 && ret != -ENOTSUPP)
261 return ret;
262
263 mutex_lock(&phy->mutex);
637d378c 264 if (phy->init_count == 0 && phy->ops->init) {
ff764963
KVA
265 ret = phy->ops->init(phy);
266 if (ret < 0) {
267 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
268 goto out;
269 }
767a1b5d
HG
270 } else {
271 ret = 0; /* Override possible ret == -ENOTSUPP */
ff764963 272 }
637d378c 273 ++phy->init_count;
ff764963
KVA
274
275out:
276 mutex_unlock(&phy->mutex);
277 phy_pm_runtime_put(phy);
278 return ret;
279}
280EXPORT_SYMBOL_GPL(phy_init);
281
282int phy_exit(struct phy *phy)
283{
284 int ret;
285
04c2faca
AL
286 if (!phy)
287 return 0;
288
ff764963
KVA
289 ret = phy_pm_runtime_get_sync(phy);
290 if (ret < 0 && ret != -ENOTSUPP)
291 return ret;
292
293 mutex_lock(&phy->mutex);
637d378c 294 if (phy->init_count == 1 && phy->ops->exit) {
ff764963
KVA
295 ret = phy->ops->exit(phy);
296 if (ret < 0) {
297 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
298 goto out;
299 }
300 }
637d378c 301 --phy->init_count;
ff764963
KVA
302
303out:
304 mutex_unlock(&phy->mutex);
305 phy_pm_runtime_put(phy);
306 return ret;
307}
308EXPORT_SYMBOL_GPL(phy_exit);
309
310int phy_power_on(struct phy *phy)
311{
d18c9604 312 int ret;
ff764963 313
04c2faca
AL
314 if (!phy)
315 return 0;
316
3be88125
RQ
317 if (phy->pwr) {
318 ret = regulator_enable(phy->pwr);
319 if (ret)
320 return ret;
321 }
322
ff764963
KVA
323 ret = phy_pm_runtime_get_sync(phy);
324 if (ret < 0 && ret != -ENOTSUPP)
325 return ret;
326
327 mutex_lock(&phy->mutex);
637d378c 328 if (phy->power_count == 0 && phy->ops->power_on) {
ff764963
KVA
329 ret = phy->ops->power_on(phy);
330 if (ret < 0) {
331 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
332 goto out;
333 }
767a1b5d
HG
334 } else {
335 ret = 0; /* Override possible ret == -ENOTSUPP */
ff764963 336 }
637d378c
KVA
337 ++phy->power_count;
338 mutex_unlock(&phy->mutex);
339 return 0;
ff764963
KVA
340
341out:
342 mutex_unlock(&phy->mutex);
637d378c 343 phy_pm_runtime_put_sync(phy);
3be88125
RQ
344 if (phy->pwr)
345 regulator_disable(phy->pwr);
ff764963
KVA
346
347 return ret;
348}
349EXPORT_SYMBOL_GPL(phy_power_on);
350
351int phy_power_off(struct phy *phy)
352{
d18c9604 353 int ret;
ff764963 354
04c2faca
AL
355 if (!phy)
356 return 0;
357
ff764963 358 mutex_lock(&phy->mutex);
637d378c 359 if (phy->power_count == 1 && phy->ops->power_off) {
ff764963
KVA
360 ret = phy->ops->power_off(phy);
361 if (ret < 0) {
362 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
637d378c
KVA
363 mutex_unlock(&phy->mutex);
364 return ret;
ff764963
KVA
365 }
366 }
637d378c 367 --phy->power_count;
ff764963
KVA
368 mutex_unlock(&phy->mutex);
369 phy_pm_runtime_put(phy);
370
3be88125
RQ
371 if (phy->pwr)
372 regulator_disable(phy->pwr);
373
637d378c 374 return 0;
ff764963
KVA
375}
376EXPORT_SYMBOL_GPL(phy_power_off);
377
378/**
0b3f3b2c
KD
379 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
380 * @np: device_node for which to get the phy
ff764963
KVA
381 * @index: the index of the phy
382 *
383 * Returns the phy associated with the given phandle value,
384 * after getting a refcount to it or -ENODEV if there is no such phy or
385 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
386 * not yet loaded. This function uses of_xlate call back function provided
387 * while registering the phy_provider to find the phy instance.
388 */
0b3f3b2c 389static struct phy *_of_phy_get(struct device_node *np, int index)
ff764963
KVA
390{
391 int ret;
392 struct phy_provider *phy_provider;
393 struct phy *phy = NULL;
394 struct of_phandle_args args;
395
0b3f3b2c 396 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
ff764963 397 index, &args);
0b3f3b2c 398 if (ret)
ff764963 399 return ERR_PTR(-ENODEV);
ff764963
KVA
400
401 mutex_lock(&phy_provider_mutex);
402 phy_provider = of_phy_provider_lookup(args.np);
403 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
404 phy = ERR_PTR(-EPROBE_DEFER);
405 goto err0;
406 }
407
408 phy = phy_provider->of_xlate(phy_provider->dev, &args);
409 module_put(phy_provider->owner);
410
411err0:
412 mutex_unlock(&phy_provider_mutex);
413 of_node_put(args.np);
414
415 return phy;
416}
417
0b3f3b2c
KD
418/**
419 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
420 * @np: device_node for which to get the phy
421 * @con_id: name of the phy from device's point of view
422 *
423 * Returns the phy driver, after getting a refcount to it; or
424 * -ENODEV if there is no such phy. The caller is responsible for
425 * calling phy_put() to release that count.
426 */
427struct phy *of_phy_get(struct device_node *np, const char *con_id)
428{
429 struct phy *phy = NULL;
430 int index = 0;
431
432 if (con_id)
433 index = of_property_match_string(np, "phy-names", con_id);
434
435 phy = _of_phy_get(np, index);
436 if (IS_ERR(phy))
437 return phy;
438
439 if (!try_module_get(phy->ops->owner))
440 return ERR_PTR(-EPROBE_DEFER);
441
442 get_device(&phy->dev);
443
444 return phy;
445}
446EXPORT_SYMBOL_GPL(of_phy_get);
447
ff764963
KVA
448/**
449 * phy_put() - release the PHY
450 * @phy: the phy returned by phy_get()
451 *
452 * Releases a refcount the caller received from phy_get().
453 */
454void phy_put(struct phy *phy)
455{
04c2faca 456 if (!phy || IS_ERR(phy))
ff764963
KVA
457 return;
458
459 module_put(phy->ops->owner);
460 put_device(&phy->dev);
461}
462EXPORT_SYMBOL_GPL(phy_put);
463
464/**
465 * devm_phy_put() - release the PHY
466 * @dev: device that wants to release this phy
467 * @phy: the phy returned by devm_phy_get()
468 *
469 * destroys the devres associated with this phy and invokes phy_put
470 * to release the phy.
471 */
472void devm_phy_put(struct device *dev, struct phy *phy)
473{
474 int r;
475
04c2faca
AL
476 if (!phy)
477 return;
478
ff764963
KVA
479 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
480 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
481}
482EXPORT_SYMBOL_GPL(devm_phy_put);
483
484/**
485 * of_phy_simple_xlate() - returns the phy instance from phy provider
486 * @dev: the PHY provider device
487 * @args: of_phandle_args (not used here)
488 *
489 * Intended to be used by phy provider for the common case where #phy-cells is
490 * 0. For other cases where #phy-cells is greater than '0', the phy provider
491 * should provide a custom of_xlate function that reads the *args* and returns
492 * the appropriate phy.
493 */
494struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
495 *args)
496{
497 struct phy *phy;
498 struct class_dev_iter iter;
ff764963
KVA
499
500 class_dev_iter_init(&iter, phy_class, NULL, NULL);
501 while ((dev = class_dev_iter_next(&iter))) {
502 phy = to_phy(dev);
491e0490 503 if (args->np != phy->dev.of_node)
ff764963
KVA
504 continue;
505
506 class_dev_iter_exit(&iter);
507 return phy;
508 }
509
510 class_dev_iter_exit(&iter);
511 return ERR_PTR(-ENODEV);
512}
513EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
514
515/**
516 * phy_get() - lookup and obtain a reference to a phy.
517 * @dev: device that requests this phy
518 * @string: the phy name as given in the dt data or the name of the controller
519 * port for non-dt case
520 *
521 * Returns the phy driver, after getting a refcount to it; or
522 * -ENODEV if there is no such phy. The caller is responsible for
523 * calling phy_put() to release that count.
524 */
525struct phy *phy_get(struct device *dev, const char *string)
526{
527 int index = 0;
d18c9604 528 struct phy *phy;
ff764963
KVA
529
530 if (string == NULL) {
531 dev_WARN(dev, "missing string\n");
532 return ERR_PTR(-EINVAL);
533 }
534
535 if (dev->of_node) {
536 index = of_property_match_string(dev->of_node, "phy-names",
537 string);
0b3f3b2c 538 phy = _of_phy_get(dev->of_node, index);
ff764963 539 } else {
b7bc15b9 540 phy = phy_find(dev, string);
ff764963 541 }
f40037fd
HG
542 if (IS_ERR(phy))
543 return phy;
ff764963
KVA
544
545 if (!try_module_get(phy->ops->owner))
546 return ERR_PTR(-EPROBE_DEFER);
547
548 get_device(&phy->dev);
549
550 return phy;
551}
552EXPORT_SYMBOL_GPL(phy_get);
553
788a4d56
AL
554/**
555 * phy_optional_get() - lookup and obtain a reference to an optional phy.
556 * @dev: device that requests this phy
557 * @string: the phy name as given in the dt data or the name of the controller
558 * port for non-dt case
559 *
560 * Returns the phy driver, after getting a refcount to it; or
561 * NULL if there is no such phy. The caller is responsible for
562 * calling phy_put() to release that count.
563 */
564struct phy *phy_optional_get(struct device *dev, const char *string)
565{
566 struct phy *phy = phy_get(dev, string);
567
568 if (PTR_ERR(phy) == -ENODEV)
569 phy = NULL;
570
571 return phy;
572}
573EXPORT_SYMBOL_GPL(phy_optional_get);
574
ff764963
KVA
575/**
576 * devm_phy_get() - lookup and obtain a reference to a phy.
577 * @dev: device that requests this phy
578 * @string: the phy name as given in the dt data or phy device name
579 * for non-dt case
580 *
581 * Gets the phy using phy_get(), and associates a device with it using
582 * devres. On driver detach, release function is invoked on the devres data,
583 * then, devres data is freed.
584 */
585struct phy *devm_phy_get(struct device *dev, const char *string)
586{
587 struct phy **ptr, *phy;
588
589 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
590 if (!ptr)
591 return ERR_PTR(-ENOMEM);
592
593 phy = phy_get(dev, string);
594 if (!IS_ERR(phy)) {
595 *ptr = phy;
596 devres_add(dev, ptr);
597 } else {
598 devres_free(ptr);
599 }
600
601 return phy;
602}
603EXPORT_SYMBOL_GPL(devm_phy_get);
604
788a4d56
AL
605/**
606 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
607 * @dev: device that requests this phy
608 * @string: the phy name as given in the dt data or phy device name
609 * for non-dt case
610 *
611 * Gets the phy using phy_get(), and associates a device with it using
612 * devres. On driver detach, release function is invoked on the devres
613 * data, then, devres data is freed. This differs to devm_phy_get() in
614 * that if the phy does not exist, it is not considered an error and
615 * -ENODEV will not be returned. Instead the NULL phy is returned,
616 * which can be passed to all other phy consumer calls.
617 */
618struct phy *devm_phy_optional_get(struct device *dev, const char *string)
619{
620 struct phy *phy = devm_phy_get(dev, string);
621
622 if (PTR_ERR(phy) == -ENODEV)
623 phy = NULL;
624
625 return phy;
626}
627EXPORT_SYMBOL_GPL(devm_phy_optional_get);
628
b5d682f4
KD
629/**
630 * devm_of_phy_get() - lookup and obtain a reference to a phy.
631 * @dev: device that requests this phy
632 * @np: node containing the phy
633 * @con_id: name of the phy from device's point of view
634 *
635 * Gets the phy using of_phy_get(), and associates a device with it using
636 * devres. On driver detach, release function is invoked on the devres data,
637 * then, devres data is freed.
638 */
639struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
640 const char *con_id)
641{
642 struct phy **ptr, *phy;
643
644 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
645 if (!ptr)
646 return ERR_PTR(-ENOMEM);
647
648 phy = of_phy_get(np, con_id);
649 if (!IS_ERR(phy)) {
650 *ptr = phy;
651 devres_add(dev, ptr);
652 } else {
653 devres_free(ptr);
654 }
655
656 return phy;
657}
658EXPORT_SYMBOL_GPL(devm_of_phy_get);
659
ff764963
KVA
660/**
661 * phy_create() - create a new phy
662 * @dev: device that is creating the new phy
f0ed8176 663 * @node: device node of the phy
ff764963
KVA
664 * @ops: function pointers for performing phy operations
665 * @init_data: contains the list of PHY consumers or NULL
666 *
667 * Called to create a phy using phy framework.
668 */
f0ed8176
KVA
669struct phy *phy_create(struct device *dev, struct device_node *node,
670 const struct phy_ops *ops,
671 struct phy_init_data *init_data)
ff764963
KVA
672{
673 int ret;
674 int id;
675 struct phy *phy;
676
52797d29
DC
677 if (WARN_ON(!dev))
678 return ERR_PTR(-EINVAL);
ff764963
KVA
679
680 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
52797d29
DC
681 if (!phy)
682 return ERR_PTR(-ENOMEM);
ff764963
KVA
683
684 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
685 if (id < 0) {
686 dev_err(dev, "unable to get id\n");
687 ret = id;
52797d29 688 goto free_phy;
ff764963
KVA
689 }
690
3be88125
RQ
691 /* phy-supply */
692 phy->pwr = regulator_get_optional(dev, "phy");
693 if (IS_ERR(phy->pwr)) {
694 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
695 ret = -EPROBE_DEFER;
696 goto free_ida;
697 }
698 phy->pwr = NULL;
699 }
700
ff764963
KVA
701 device_initialize(&phy->dev);
702 mutex_init(&phy->mutex);
703
704 phy->dev.class = phy_class;
705 phy->dev.parent = dev;
f0ed8176 706 phy->dev.of_node = node ?: dev->of_node;
ff764963
KVA
707 phy->id = id;
708 phy->ops = ops;
709 phy->init_data = init_data;
710
711 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
712 if (ret)
52797d29 713 goto put_dev;
ff764963
KVA
714
715 ret = device_add(&phy->dev);
716 if (ret)
52797d29 717 goto put_dev;
ff764963
KVA
718
719 if (pm_runtime_enabled(dev)) {
720 pm_runtime_enable(&phy->dev);
721 pm_runtime_no_callbacks(&phy->dev);
722 }
723
724 return phy;
725
52797d29 726put_dev:
e73b49f1
RQ
727 put_device(&phy->dev); /* calls phy_release() which frees resources */
728 return ERR_PTR(ret);
729
3be88125
RQ
730free_ida:
731 ida_simple_remove(&phy_ida, phy->id);
732
52797d29 733free_phy:
ff764963 734 kfree(phy);
ff764963
KVA
735 return ERR_PTR(ret);
736}
737EXPORT_SYMBOL_GPL(phy_create);
738
739/**
740 * devm_phy_create() - create a new phy
741 * @dev: device that is creating the new phy
f0ed8176 742 * @node: device node of the phy
ff764963
KVA
743 * @ops: function pointers for performing phy operations
744 * @init_data: contains the list of PHY consumers or NULL
745 *
746 * Creates a new PHY device adding it to the PHY class.
747 * While at that, it also associates the device with the phy using devres.
748 * On driver detach, release function is invoked on the devres data,
749 * then, devres data is freed.
750 */
f0ed8176
KVA
751struct phy *devm_phy_create(struct device *dev, struct device_node *node,
752 const struct phy_ops *ops,
753 struct phy_init_data *init_data)
ff764963
KVA
754{
755 struct phy **ptr, *phy;
756
757 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
758 if (!ptr)
759 return ERR_PTR(-ENOMEM);
760
f0ed8176 761 phy = phy_create(dev, node, ops, init_data);
ff764963
KVA
762 if (!IS_ERR(phy)) {
763 *ptr = phy;
764 devres_add(dev, ptr);
765 } else {
766 devres_free(ptr);
767 }
768
769 return phy;
770}
771EXPORT_SYMBOL_GPL(devm_phy_create);
772
773/**
774 * phy_destroy() - destroy the phy
775 * @phy: the phy to be destroyed
776 *
777 * Called to destroy the phy.
778 */
779void phy_destroy(struct phy *phy)
780{
781 pm_runtime_disable(&phy->dev);
782 device_unregister(&phy->dev);
783}
784EXPORT_SYMBOL_GPL(phy_destroy);
785
786/**
787 * devm_phy_destroy() - destroy the PHY
788 * @dev: device that wants to release this phy
789 * @phy: the phy returned by devm_phy_get()
790 *
791 * destroys the devres associated with this phy and invokes phy_destroy
792 * to destroy the phy.
793 */
794void devm_phy_destroy(struct device *dev, struct phy *phy)
795{
796 int r;
797
798 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
799 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
800}
801EXPORT_SYMBOL_GPL(devm_phy_destroy);
802
803/**
804 * __of_phy_provider_register() - create/register phy provider with the framework
805 * @dev: struct device of the phy provider
806 * @owner: the module owner containing of_xlate
807 * @of_xlate: function pointer to obtain phy instance from phy provider
808 *
809 * Creates struct phy_provider from dev and of_xlate function pointer.
810 * This is used in the case of dt boot for finding the phy instance from
811 * phy provider.
812 */
813struct phy_provider *__of_phy_provider_register(struct device *dev,
814 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
815 struct of_phandle_args *args))
816{
817 struct phy_provider *phy_provider;
818
819 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
820 if (!phy_provider)
821 return ERR_PTR(-ENOMEM);
822
823 phy_provider->dev = dev;
824 phy_provider->owner = owner;
825 phy_provider->of_xlate = of_xlate;
826
827 mutex_lock(&phy_provider_mutex);
828 list_add_tail(&phy_provider->list, &phy_provider_list);
829 mutex_unlock(&phy_provider_mutex);
830
831 return phy_provider;
832}
833EXPORT_SYMBOL_GPL(__of_phy_provider_register);
834
835/**
836 * __devm_of_phy_provider_register() - create/register phy provider with the
837 * framework
838 * @dev: struct device of the phy provider
839 * @owner: the module owner containing of_xlate
840 * @of_xlate: function pointer to obtain phy instance from phy provider
841 *
842 * Creates struct phy_provider from dev and of_xlate function pointer.
843 * This is used in the case of dt boot for finding the phy instance from
844 * phy provider. While at that, it also associates the device with the
845 * phy provider using devres. On driver detach, release function is invoked
846 * on the devres data, then, devres data is freed.
847 */
848struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
849 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
850 struct of_phandle_args *args))
851{
852 struct phy_provider **ptr, *phy_provider;
853
854 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
855 if (!ptr)
856 return ERR_PTR(-ENOMEM);
857
858 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
859 if (!IS_ERR(phy_provider)) {
860 *ptr = phy_provider;
861 devres_add(dev, ptr);
862 } else {
863 devres_free(ptr);
864 }
865
866 return phy_provider;
867}
868EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
869
870/**
871 * of_phy_provider_unregister() - unregister phy provider from the framework
872 * @phy_provider: phy provider returned by of_phy_provider_register()
873 *
874 * Removes the phy_provider created using of_phy_provider_register().
875 */
876void of_phy_provider_unregister(struct phy_provider *phy_provider)
877{
878 if (IS_ERR(phy_provider))
879 return;
880
881 mutex_lock(&phy_provider_mutex);
882 list_del(&phy_provider->list);
883 kfree(phy_provider);
884 mutex_unlock(&phy_provider_mutex);
885}
886EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
887
888/**
889 * devm_of_phy_provider_unregister() - remove phy provider from the framework
890 * @dev: struct device of the phy provider
891 *
892 * destroys the devres associated with this phy provider and invokes
893 * of_phy_provider_unregister to unregister the phy provider.
894 */
895void devm_of_phy_provider_unregister(struct device *dev,
896 struct phy_provider *phy_provider) {
897 int r;
898
899 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
900 phy_provider);
901 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
902}
903EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
904
905/**
906 * phy_release() - release the phy
907 * @dev: the dev member within phy
908 *
909 * When the last reference to the device is removed, it is called
910 * from the embedded kobject as release method.
911 */
912static void phy_release(struct device *dev)
913{
914 struct phy *phy;
915
916 phy = to_phy(dev);
917 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
3be88125 918 regulator_put(phy->pwr);
e73b49f1 919 ida_simple_remove(&phy_ida, phy->id);
ff764963
KVA
920 kfree(phy);
921}
922
923static int __init phy_core_init(void)
924{
925 phy_class = class_create(THIS_MODULE, "phy");
926 if (IS_ERR(phy_class)) {
927 pr_err("failed to create phy class --> %ld\n",
928 PTR_ERR(phy_class));
929 return PTR_ERR(phy_class);
930 }
931
932 phy_class->dev_release = phy_release;
933
934 return 0;
935}
936module_init(phy_core_init);
937
938static void __exit phy_core_exit(void)
939{
940 class_destroy(phy_class);
941}
942module_exit(phy_core_exit);
943
944MODULE_DESCRIPTION("Generic PHY Framework");
945MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
946MODULE_LICENSE("GPL v2");