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