1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * phy-core.c -- Generic Phy framework.
4 *
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Author: Kishon Vijay Abraham I <kishon@ti.com>
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/export.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/debugfs.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/of.h>
18 #include <linux/phy/phy.h>
19 #include <linux/idr.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22
23 static void phy_release(struct device *dev);
24 static const struct class phy_class = {
25 .name = "phy",
26 .dev_release = phy_release,
27 };
28
29 static struct dentry *phy_debugfs_root;
30 static DEFINE_MUTEX(phy_provider_mutex);
31 static LIST_HEAD(phy_provider_list);
32 static LIST_HEAD(phys);
33 static DEFINE_IDA(phy_ida);
34
devm_phy_release(struct device * dev,void * res)35 static void devm_phy_release(struct device *dev, void *res)
36 {
37 struct phy *phy = *(struct phy **)res;
38
39 phy_put(dev, phy);
40 }
41
devm_phy_provider_release(struct device * dev,void * res)42 static void devm_phy_provider_release(struct device *dev, void *res)
43 {
44 struct phy_provider *phy_provider = *(struct phy_provider **)res;
45
46 of_phy_provider_unregister(phy_provider);
47 }
48
devm_phy_consume(struct device * dev,void * res)49 static void devm_phy_consume(struct device *dev, void *res)
50 {
51 struct phy *phy = *(struct phy **)res;
52
53 phy_destroy(phy);
54 }
55
devm_phy_match(struct device * dev,void * res,void * match_data)56 static int devm_phy_match(struct device *dev, void *res, void *match_data)
57 {
58 struct phy **phy = res;
59
60 return *phy == match_data;
61 }
62
63 /**
64 * phy_create_lookup() - allocate and register PHY/device association
65 * @phy: the phy of the association
66 * @con_id: connection ID string on device
67 * @dev_id: the device of the association
68 *
69 * Creates and registers phy_lookup entry.
70 */
phy_create_lookup(struct phy * phy,const char * con_id,const char * dev_id)71 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
72 {
73 struct phy_lookup *pl;
74
75 if (!phy || !dev_id || !con_id)
76 return -EINVAL;
77
78 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
79 if (!pl)
80 return -ENOMEM;
81
82 pl->dev_id = dev_id;
83 pl->con_id = con_id;
84 pl->phy = phy;
85
86 mutex_lock(&phy_provider_mutex);
87 list_add_tail(&pl->node, &phys);
88 mutex_unlock(&phy_provider_mutex);
89
90 return 0;
91 }
92 EXPORT_SYMBOL_GPL(phy_create_lookup);
93
94 /**
95 * phy_remove_lookup() - find and remove PHY/device association
96 * @phy: the phy of the association
97 * @con_id: connection ID string on device
98 * @dev_id: the device of the association
99 *
100 * Finds and unregisters phy_lookup entry that was created with
101 * phy_create_lookup().
102 */
phy_remove_lookup(struct phy * phy,const char * con_id,const char * dev_id)103 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
104 {
105 struct phy_lookup *pl;
106
107 if (!phy || !dev_id || !con_id)
108 return;
109
110 mutex_lock(&phy_provider_mutex);
111 list_for_each_entry(pl, &phys, node)
112 if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
113 !strcmp(pl->con_id, con_id)) {
114 list_del(&pl->node);
115 kfree(pl);
116 break;
117 }
118 mutex_unlock(&phy_provider_mutex);
119 }
120 EXPORT_SYMBOL_GPL(phy_remove_lookup);
121
phy_find(struct device * dev,const char * con_id)122 static struct phy *phy_find(struct device *dev, const char *con_id)
123 {
124 const char *dev_id = dev_name(dev);
125 struct phy_lookup *p, *pl = NULL;
126
127 mutex_lock(&phy_provider_mutex);
128 list_for_each_entry(p, &phys, node)
129 if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
130 pl = p;
131 break;
132 }
133 mutex_unlock(&phy_provider_mutex);
134
135 return pl ? pl->phy : ERR_PTR(-ENODEV);
136 }
137
of_phy_provider_lookup(struct device_node * node)138 static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
139 {
140 struct phy_provider *phy_provider;
141 struct device_node *child;
142
143 list_for_each_entry(phy_provider, &phy_provider_list, list) {
144 if (phy_provider->dev->of_node == node)
145 return phy_provider;
146
147 for_each_child_of_node(phy_provider->children, child)
148 if (child == node)
149 return phy_provider;
150 }
151
152 return ERR_PTR(-EPROBE_DEFER);
153 }
154
phy_pm_runtime_get(struct phy * phy)155 int phy_pm_runtime_get(struct phy *phy)
156 {
157 int ret;
158
159 if (!phy)
160 return 0;
161
162 if (!pm_runtime_enabled(&phy->dev))
163 return -ENOTSUPP;
164
165 ret = pm_runtime_get(&phy->dev);
166 if (ret < 0 && ret != -EINPROGRESS)
167 pm_runtime_put_noidle(&phy->dev);
168
169 return ret;
170 }
171 EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
172
phy_pm_runtime_get_sync(struct phy * phy)173 int phy_pm_runtime_get_sync(struct phy *phy)
174 {
175 int ret;
176
177 if (!phy)
178 return 0;
179
180 if (!pm_runtime_enabled(&phy->dev))
181 return -ENOTSUPP;
182
183 ret = pm_runtime_get_sync(&phy->dev);
184 if (ret < 0)
185 pm_runtime_put_sync(&phy->dev);
186
187 return ret;
188 }
189 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
190
phy_pm_runtime_put(struct phy * phy)191 int phy_pm_runtime_put(struct phy *phy)
192 {
193 if (!phy)
194 return 0;
195
196 if (!pm_runtime_enabled(&phy->dev))
197 return -ENOTSUPP;
198
199 return pm_runtime_put(&phy->dev);
200 }
201 EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
202
phy_pm_runtime_put_sync(struct phy * phy)203 int phy_pm_runtime_put_sync(struct phy *phy)
204 {
205 if (!phy)
206 return 0;
207
208 if (!pm_runtime_enabled(&phy->dev))
209 return -ENOTSUPP;
210
211 return pm_runtime_put_sync(&phy->dev);
212 }
213 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
214
phy_pm_runtime_allow(struct phy * phy)215 void phy_pm_runtime_allow(struct phy *phy)
216 {
217 if (!phy)
218 return;
219
220 if (!pm_runtime_enabled(&phy->dev))
221 return;
222
223 pm_runtime_allow(&phy->dev);
224 }
225 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
226
phy_pm_runtime_forbid(struct phy * phy)227 void phy_pm_runtime_forbid(struct phy *phy)
228 {
229 if (!phy)
230 return;
231
232 if (!pm_runtime_enabled(&phy->dev))
233 return;
234
235 pm_runtime_forbid(&phy->dev);
236 }
237 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
238
239 /**
240 * phy_init - phy internal initialization before phy operation
241 * @phy: the phy returned by phy_get()
242 *
243 * Used to allow phy's driver to perform phy internal initialization,
244 * such as PLL block powering, clock initialization or anything that's
245 * is required by the phy to perform the start of operation.
246 * Must be called before phy_power_on().
247 *
248 * Return: %0 if successful, a negative error code otherwise
249 */
phy_init(struct phy * phy)250 int phy_init(struct phy *phy)
251 {
252 int ret;
253
254 if (!phy)
255 return 0;
256
257 ret = phy_pm_runtime_get_sync(phy);
258 if (ret < 0 && ret != -ENOTSUPP)
259 return ret;
260 ret = 0; /* Override possible ret == -ENOTSUPP */
261
262 mutex_lock(&phy->mutex);
263 if (phy->power_count > phy->init_count)
264 dev_warn(&phy->dev, "phy_power_on was called before phy_init\n");
265
266 if (phy->init_count == 0 && phy->ops->init) {
267 ret = phy->ops->init(phy);
268 if (ret < 0) {
269 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
270 goto out;
271 }
272 }
273 ++phy->init_count;
274
275 out:
276 mutex_unlock(&phy->mutex);
277 phy_pm_runtime_put(phy);
278 return ret;
279 }
280 EXPORT_SYMBOL_GPL(phy_init);
281
282 /**
283 * phy_exit - Phy internal un-initialization
284 * @phy: the phy returned by phy_get()
285 *
286 * Must be called after phy_power_off().
287 *
288 * Return: %0 if successful, a negative error code otherwise
289 */
phy_exit(struct phy * phy)290 int phy_exit(struct phy *phy)
291 {
292 int ret;
293
294 if (!phy)
295 return 0;
296
297 ret = phy_pm_runtime_get_sync(phy);
298 if (ret < 0 && ret != -ENOTSUPP)
299 return ret;
300 ret = 0; /* Override possible ret == -ENOTSUPP */
301
302 mutex_lock(&phy->mutex);
303 if (phy->init_count == 1 && phy->ops->exit) {
304 ret = phy->ops->exit(phy);
305 if (ret < 0) {
306 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
307 goto out;
308 }
309 }
310 --phy->init_count;
311
312 out:
313 mutex_unlock(&phy->mutex);
314 phy_pm_runtime_put(phy);
315 return ret;
316 }
317 EXPORT_SYMBOL_GPL(phy_exit);
318
319 /**
320 * phy_power_on - Enable the phy and enter proper operation
321 * @phy: the phy returned by phy_get()
322 *
323 * Must be called after phy_init().
324 *
325 * Return: %0 if successful, a negative error code otherwise
326 */
phy_power_on(struct phy * phy)327 int phy_power_on(struct phy *phy)
328 {
329 int ret = 0;
330
331 if (!phy)
332 goto out;
333
334 if (phy->pwr) {
335 ret = regulator_enable(phy->pwr);
336 if (ret)
337 goto out;
338 }
339
340 ret = phy_pm_runtime_get_sync(phy);
341 if (ret < 0 && ret != -ENOTSUPP)
342 goto err_pm_sync;
343
344 ret = 0; /* Override possible ret == -ENOTSUPP */
345
346 mutex_lock(&phy->mutex);
347 if (phy->power_count == 0 && phy->ops->power_on) {
348 ret = phy->ops->power_on(phy);
349 if (ret < 0) {
350 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
351 goto err_pwr_on;
352 }
353 }
354 ++phy->power_count;
355 mutex_unlock(&phy->mutex);
356 return 0;
357
358 err_pwr_on:
359 mutex_unlock(&phy->mutex);
360 phy_pm_runtime_put_sync(phy);
361 err_pm_sync:
362 if (phy->pwr)
363 regulator_disable(phy->pwr);
364 out:
365 return ret;
366 }
367 EXPORT_SYMBOL_GPL(phy_power_on);
368
369 /**
370 * phy_power_off - Disable the phy.
371 * @phy: the phy returned by phy_get()
372 *
373 * Must be called before phy_exit().
374 *
375 * Return: %0 if successful, a negative error code otherwise
376 */
phy_power_off(struct phy * phy)377 int phy_power_off(struct phy *phy)
378 {
379 int ret;
380
381 if (!phy)
382 return 0;
383
384 mutex_lock(&phy->mutex);
385 if (phy->power_count == 1 && phy->ops->power_off) {
386 ret = phy->ops->power_off(phy);
387 if (ret < 0) {
388 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
389 mutex_unlock(&phy->mutex);
390 return ret;
391 }
392 }
393 --phy->power_count;
394 mutex_unlock(&phy->mutex);
395 phy_pm_runtime_put(phy);
396
397 if (phy->pwr)
398 regulator_disable(phy->pwr);
399
400 return 0;
401 }
402 EXPORT_SYMBOL_GPL(phy_power_off);
403
phy_set_mode_ext(struct phy * phy,enum phy_mode mode,int submode)404 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
405 {
406 int ret;
407
408 if (!phy || !phy->ops->set_mode)
409 return 0;
410
411 mutex_lock(&phy->mutex);
412 ret = phy->ops->set_mode(phy, mode, submode);
413 if (!ret)
414 phy->attrs.mode = mode;
415 mutex_unlock(&phy->mutex);
416
417 return ret;
418 }
419 EXPORT_SYMBOL_GPL(phy_set_mode_ext);
420
phy_set_media(struct phy * phy,enum phy_media media)421 int phy_set_media(struct phy *phy, enum phy_media media)
422 {
423 int ret;
424
425 if (!phy || !phy->ops->set_media)
426 return 0;
427
428 mutex_lock(&phy->mutex);
429 ret = phy->ops->set_media(phy, media);
430 mutex_unlock(&phy->mutex);
431
432 return ret;
433 }
434 EXPORT_SYMBOL_GPL(phy_set_media);
435
phy_set_speed(struct phy * phy,int speed)436 int phy_set_speed(struct phy *phy, int speed)
437 {
438 int ret;
439
440 if (!phy || !phy->ops->set_speed)
441 return 0;
442
443 mutex_lock(&phy->mutex);
444 ret = phy->ops->set_speed(phy, speed);
445 mutex_unlock(&phy->mutex);
446
447 return ret;
448 }
449 EXPORT_SYMBOL_GPL(phy_set_speed);
450
phy_reset(struct phy * phy)451 int phy_reset(struct phy *phy)
452 {
453 int ret;
454
455 if (!phy || !phy->ops->reset)
456 return 0;
457
458 ret = phy_pm_runtime_get_sync(phy);
459 if (ret < 0 && ret != -ENOTSUPP)
460 return ret;
461
462 mutex_lock(&phy->mutex);
463 ret = phy->ops->reset(phy);
464 mutex_unlock(&phy->mutex);
465
466 phy_pm_runtime_put(phy);
467
468 return ret;
469 }
470 EXPORT_SYMBOL_GPL(phy_reset);
471
472 /**
473 * phy_calibrate() - Tunes the phy hw parameters for current configuration
474 * @phy: the phy returned by phy_get()
475 *
476 * Used to calibrate phy hardware, typically by adjusting some parameters in
477 * runtime, which are otherwise lost after host controller reset and cannot
478 * be applied in phy_init() or phy_power_on().
479 *
480 * Return: %0 if successful, a negative error code otherwise
481 */
phy_calibrate(struct phy * phy)482 int phy_calibrate(struct phy *phy)
483 {
484 int ret;
485
486 if (!phy || !phy->ops->calibrate)
487 return 0;
488
489 mutex_lock(&phy->mutex);
490 ret = phy->ops->calibrate(phy);
491 mutex_unlock(&phy->mutex);
492
493 return ret;
494 }
495 EXPORT_SYMBOL_GPL(phy_calibrate);
496
497 /**
498 * phy_notify_connect() - phy connect notification
499 * @phy: the phy returned by phy_get()
500 * @port: the port index for connect
501 *
502 * If the phy needs to get connection status, the callback can be used.
503 * Returns: %0 if successful, a negative error code otherwise
504 */
phy_notify_connect(struct phy * phy,int port)505 int phy_notify_connect(struct phy *phy, int port)
506 {
507 int ret;
508
509 if (!phy || !phy->ops->connect)
510 return 0;
511
512 mutex_lock(&phy->mutex);
513 ret = phy->ops->connect(phy, port);
514 mutex_unlock(&phy->mutex);
515
516 return ret;
517 }
518 EXPORT_SYMBOL_GPL(phy_notify_connect);
519
520 /**
521 * phy_notify_disconnect() - phy disconnect notification
522 * @phy: the phy returned by phy_get()
523 * @port: the port index for disconnect
524 *
525 * If the phy needs to get connection status, the callback can be used.
526 *
527 * Returns: %0 if successful, a negative error code otherwise
528 */
phy_notify_disconnect(struct phy * phy,int port)529 int phy_notify_disconnect(struct phy *phy, int port)
530 {
531 int ret;
532
533 if (!phy || !phy->ops->disconnect)
534 return 0;
535
536 mutex_lock(&phy->mutex);
537 ret = phy->ops->disconnect(phy, port);
538 mutex_unlock(&phy->mutex);
539
540 return ret;
541 }
542 EXPORT_SYMBOL_GPL(phy_notify_disconnect);
543
544 /**
545 * phy_configure() - Changes the phy parameters
546 * @phy: the phy returned by phy_get()
547 * @opts: New configuration to apply
548 *
549 * Used to change the PHY parameters. phy_init() must have been called
550 * on the phy. The configuration will be applied on the current phy
551 * mode, that can be changed using phy_set_mode().
552 *
553 * Return: %0 if successful, a negative error code otherwise
554 */
phy_configure(struct phy * phy,union phy_configure_opts * opts)555 int phy_configure(struct phy *phy, union phy_configure_opts *opts)
556 {
557 int ret;
558
559 if (!phy)
560 return -EINVAL;
561
562 if (!phy->ops->configure)
563 return -EOPNOTSUPP;
564
565 mutex_lock(&phy->mutex);
566 ret = phy->ops->configure(phy, opts);
567 mutex_unlock(&phy->mutex);
568
569 return ret;
570 }
571 EXPORT_SYMBOL_GPL(phy_configure);
572
573 /**
574 * phy_validate() - Checks the phy parameters
575 * @phy: the phy returned by phy_get()
576 * @mode: phy_mode the configuration is applicable to.
577 * @submode: PHY submode the configuration is applicable to.
578 * @opts: Configuration to check
579 *
580 * Used to check that the current set of parameters can be handled by
581 * the phy. Implementations are free to tune the parameters passed as
582 * arguments if needed by some implementation detail or
583 * constraints. It will not change any actual configuration of the
584 * PHY, so calling it as many times as deemed fit will have no side
585 * effect.
586 *
587 * Return: %0 if successful, a negative error code otherwise
588 */
phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)589 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
590 union phy_configure_opts *opts)
591 {
592 int ret;
593
594 if (!phy)
595 return -EINVAL;
596
597 if (!phy->ops->validate)
598 return -EOPNOTSUPP;
599
600 mutex_lock(&phy->mutex);
601 ret = phy->ops->validate(phy, mode, submode, opts);
602 mutex_unlock(&phy->mutex);
603
604 return ret;
605 }
606 EXPORT_SYMBOL_GPL(phy_validate);
607
608 /**
609 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
610 * @np: device_node for which to get the phy
611 * @index: the index of the phy
612 *
613 * Returns the phy associated with the given phandle value,
614 * after getting a refcount to it or -ENODEV if there is no such phy or
615 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
616 * not yet loaded. This function uses of_xlate call back function provided
617 * while registering the phy_provider to find the phy instance.
618 */
_of_phy_get(struct device_node * np,int index)619 static struct phy *_of_phy_get(struct device_node *np, int index)
620 {
621 int ret;
622 struct phy_provider *phy_provider;
623 struct phy *phy = NULL;
624 struct of_phandle_args args;
625
626 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
627 index, &args);
628 if (ret)
629 return ERR_PTR(-ENODEV);
630
631 /* This phy type handled by the usb-phy subsystem for now */
632 if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
633 return ERR_PTR(-ENODEV);
634
635 mutex_lock(&phy_provider_mutex);
636 phy_provider = of_phy_provider_lookup(args.np);
637 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
638 phy = ERR_PTR(-EPROBE_DEFER);
639 goto out_unlock;
640 }
641
642 if (!of_device_is_available(args.np)) {
643 dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
644 phy = ERR_PTR(-ENODEV);
645 goto out_put_module;
646 }
647
648 phy = phy_provider->of_xlate(phy_provider->dev, &args);
649
650 out_put_module:
651 module_put(phy_provider->owner);
652
653 out_unlock:
654 mutex_unlock(&phy_provider_mutex);
655 of_node_put(args.np);
656
657 return phy;
658 }
659
660 /**
661 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
662 * @np: device_node for which to get the phy
663 * @con_id: name of the phy from device's point of view
664 *
665 * Returns the phy driver, after getting a refcount to it; or
666 * -ENODEV if there is no such phy. The caller is responsible for
667 * calling of_phy_put() to release that count.
668 */
of_phy_get(struct device_node * np,const char * con_id)669 struct phy *of_phy_get(struct device_node *np, const char *con_id)
670 {
671 struct phy *phy = NULL;
672 int index = 0;
673
674 if (con_id)
675 index = of_property_match_string(np, "phy-names", con_id);
676
677 phy = _of_phy_get(np, index);
678 if (IS_ERR(phy))
679 return phy;
680
681 if (!try_module_get(phy->ops->owner))
682 return ERR_PTR(-EPROBE_DEFER);
683
684 get_device(&phy->dev);
685
686 return phy;
687 }
688 EXPORT_SYMBOL_GPL(of_phy_get);
689
690 /**
691 * of_phy_put() - release the PHY
692 * @phy: the phy returned by of_phy_get()
693 *
694 * Releases a refcount the caller received from of_phy_get().
695 */
of_phy_put(struct phy * phy)696 void of_phy_put(struct phy *phy)
697 {
698 if (!phy || IS_ERR(phy))
699 return;
700
701 mutex_lock(&phy->mutex);
702 if (phy->ops->release)
703 phy->ops->release(phy);
704 mutex_unlock(&phy->mutex);
705
706 module_put(phy->ops->owner);
707 put_device(&phy->dev);
708 }
709 EXPORT_SYMBOL_GPL(of_phy_put);
710
711 /**
712 * phy_put() - release the PHY
713 * @dev: device that wants to release this phy
714 * @phy: the phy returned by phy_get()
715 *
716 * Releases a refcount the caller received from phy_get().
717 */
phy_put(struct device * dev,struct phy * phy)718 void phy_put(struct device *dev, struct phy *phy)
719 {
720 device_link_remove(dev, &phy->dev);
721 of_phy_put(phy);
722 }
723 EXPORT_SYMBOL_GPL(phy_put);
724
725 /**
726 * devm_phy_put() - release the PHY
727 * @dev: device that wants to release this phy
728 * @phy: the phy returned by devm_phy_get()
729 *
730 * destroys the devres associated with this phy and invokes phy_put
731 * to release the phy.
732 */
devm_phy_put(struct device * dev,struct phy * phy)733 void devm_phy_put(struct device *dev, struct phy *phy)
734 {
735 int r;
736
737 if (!phy)
738 return;
739
740 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
741 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
742 }
743 EXPORT_SYMBOL_GPL(devm_phy_put);
744
745 /**
746 * of_phy_simple_xlate() - returns the phy instance from phy provider
747 * @dev: the PHY provider device
748 * @args: of_phandle_args (not used here)
749 *
750 * Intended to be used by phy provider for the common case where #phy-cells is
751 * 0. For other cases where #phy-cells is greater than '0', the phy provider
752 * should provide a custom of_xlate function that reads the *args* and returns
753 * the appropriate phy.
754 */
of_phy_simple_xlate(struct device * dev,const struct of_phandle_args * args)755 struct phy *of_phy_simple_xlate(struct device *dev,
756 const struct of_phandle_args *args)
757 {
758 struct phy *phy;
759 struct class_dev_iter iter;
760
761 class_dev_iter_init(&iter, &phy_class, NULL, NULL);
762 while ((dev = class_dev_iter_next(&iter))) {
763 phy = to_phy(dev);
764 if (args->np != phy->dev.of_node)
765 continue;
766
767 class_dev_iter_exit(&iter);
768 return phy;
769 }
770
771 class_dev_iter_exit(&iter);
772 return ERR_PTR(-ENODEV);
773 }
774 EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
775
776 /**
777 * phy_get() - lookup and obtain a reference to a phy.
778 * @dev: device that requests this phy
779 * @string: the phy name as given in the dt data or the name of the controller
780 * port for non-dt case
781 *
782 * Returns the phy driver, after getting a refcount to it; or
783 * -ENODEV if there is no such phy. The caller is responsible for
784 * calling phy_put() to release that count.
785 */
phy_get(struct device * dev,const char * string)786 struct phy *phy_get(struct device *dev, const char *string)
787 {
788 int index = 0;
789 struct phy *phy;
790 struct device_link *link;
791
792 if (dev->of_node) {
793 if (string)
794 index = of_property_match_string(dev->of_node, "phy-names",
795 string);
796 else
797 index = 0;
798 phy = _of_phy_get(dev->of_node, index);
799 } else {
800 if (string == NULL) {
801 dev_WARN(dev, "missing string\n");
802 return ERR_PTR(-EINVAL);
803 }
804 phy = phy_find(dev, string);
805 }
806 if (IS_ERR(phy))
807 return phy;
808
809 if (!try_module_get(phy->ops->owner))
810 return ERR_PTR(-EPROBE_DEFER);
811
812 get_device(&phy->dev);
813
814 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
815 if (!link)
816 dev_dbg(dev, "failed to create device link to %s\n",
817 dev_name(phy->dev.parent));
818
819 return phy;
820 }
821 EXPORT_SYMBOL_GPL(phy_get);
822
823 /**
824 * devm_phy_get() - lookup and obtain a reference to a phy.
825 * @dev: device that requests this phy
826 * @string: the phy name as given in the dt data or phy device name
827 * for non-dt case
828 *
829 * Gets the phy using phy_get(), and associates a device with it using
830 * devres. On driver detach, release function is invoked on the devres data,
831 * then, devres data is freed.
832 */
devm_phy_get(struct device * dev,const char * string)833 struct phy *devm_phy_get(struct device *dev, const char *string)
834 {
835 struct phy **ptr, *phy;
836
837 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
838 if (!ptr)
839 return ERR_PTR(-ENOMEM);
840
841 phy = phy_get(dev, string);
842 if (!IS_ERR(phy)) {
843 *ptr = phy;
844 devres_add(dev, ptr);
845 } else {
846 devres_free(ptr);
847 }
848
849 return phy;
850 }
851 EXPORT_SYMBOL_GPL(devm_phy_get);
852
853 /**
854 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
855 * @dev: device that requests this phy
856 * @string: the phy name as given in the dt data or phy device name
857 * for non-dt case
858 *
859 * Gets the phy using phy_get(), and associates a device with it using
860 * devres. On driver detach, release function is invoked on the devres
861 * data, then, devres data is freed. This differs to devm_phy_get() in
862 * that if the phy does not exist, it is not considered an error and
863 * -ENODEV will not be returned. Instead the NULL phy is returned,
864 * which can be passed to all other phy consumer calls.
865 */
devm_phy_optional_get(struct device * dev,const char * string)866 struct phy *devm_phy_optional_get(struct device *dev, const char *string)
867 {
868 struct phy *phy = devm_phy_get(dev, string);
869
870 if (PTR_ERR(phy) == -ENODEV)
871 phy = NULL;
872
873 return phy;
874 }
875 EXPORT_SYMBOL_GPL(devm_phy_optional_get);
876
877 /**
878 * devm_of_phy_get() - lookup and obtain a reference to a phy.
879 * @dev: device that requests this phy
880 * @np: node containing the phy
881 * @con_id: name of the phy from device's point of view
882 *
883 * Gets the phy using of_phy_get(), and associates a device with it using
884 * devres. On driver detach, release function is invoked on the devres data,
885 * then, devres data is freed.
886 */
devm_of_phy_get(struct device * dev,struct device_node * np,const char * con_id)887 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
888 const char *con_id)
889 {
890 struct phy **ptr, *phy;
891 struct device_link *link;
892
893 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
894 if (!ptr)
895 return ERR_PTR(-ENOMEM);
896
897 phy = of_phy_get(np, con_id);
898 if (!IS_ERR(phy)) {
899 *ptr = phy;
900 devres_add(dev, ptr);
901 } else {
902 devres_free(ptr);
903 return phy;
904 }
905
906 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
907 if (!link)
908 dev_dbg(dev, "failed to create device link to %s\n",
909 dev_name(phy->dev.parent));
910
911 return phy;
912 }
913 EXPORT_SYMBOL_GPL(devm_of_phy_get);
914
915 /**
916 * devm_of_phy_optional_get() - lookup and obtain a reference to an optional
917 * phy.
918 * @dev: device that requests this phy
919 * @np: node containing the phy
920 * @con_id: name of the phy from device's point of view
921 *
922 * Gets the phy using of_phy_get(), and associates a device with it using
923 * devres. On driver detach, release function is invoked on the devres data,
924 * then, devres data is freed. This differs to devm_of_phy_get() in
925 * that if the phy does not exist, it is not considered an error and
926 * -ENODEV will not be returned. Instead the NULL phy is returned,
927 * which can be passed to all other phy consumer calls.
928 */
devm_of_phy_optional_get(struct device * dev,struct device_node * np,const char * con_id)929 struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np,
930 const char *con_id)
931 {
932 struct phy *phy = devm_of_phy_get(dev, np, con_id);
933
934 if (PTR_ERR(phy) == -ENODEV)
935 phy = NULL;
936
937 if (IS_ERR(phy))
938 dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s",
939 np, con_id);
940
941 return phy;
942 }
943 EXPORT_SYMBOL_GPL(devm_of_phy_optional_get);
944
945 /**
946 * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
947 * @dev: device that requests this phy
948 * @np: node containing the phy
949 * @index: index of the phy
950 *
951 * Gets the phy using _of_phy_get(), then gets a refcount to it,
952 * and associates a device with it using devres. On driver detach,
953 * release function is invoked on the devres data,
954 * then, devres data is freed.
955 *
956 */
devm_of_phy_get_by_index(struct device * dev,struct device_node * np,int index)957 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
958 int index)
959 {
960 struct phy **ptr, *phy;
961 struct device_link *link;
962
963 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
964 if (!ptr)
965 return ERR_PTR(-ENOMEM);
966
967 phy = _of_phy_get(np, index);
968 if (IS_ERR(phy)) {
969 devres_free(ptr);
970 return phy;
971 }
972
973 if (!try_module_get(phy->ops->owner)) {
974 devres_free(ptr);
975 return ERR_PTR(-EPROBE_DEFER);
976 }
977
978 get_device(&phy->dev);
979
980 *ptr = phy;
981 devres_add(dev, ptr);
982
983 link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
984 if (!link)
985 dev_dbg(dev, "failed to create device link to %s\n",
986 dev_name(phy->dev.parent));
987
988 return phy;
989 }
990 EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
991
992 /**
993 * phy_create() - create a new phy
994 * @dev: device that is creating the new phy
995 * @node: device node of the phy
996 * @ops: function pointers for performing phy operations
997 *
998 * Called to create a phy using phy framework.
999 */
phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)1000 struct phy *phy_create(struct device *dev, struct device_node *node,
1001 const struct phy_ops *ops)
1002 {
1003 int ret;
1004 int id;
1005 struct phy *phy;
1006
1007 if (WARN_ON(!dev))
1008 return ERR_PTR(-EINVAL);
1009
1010 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
1011 if (!phy)
1012 return ERR_PTR(-ENOMEM);
1013
1014 id = ida_alloc(&phy_ida, GFP_KERNEL);
1015 if (id < 0) {
1016 dev_err(dev, "unable to get id\n");
1017 ret = id;
1018 goto free_phy;
1019 }
1020
1021 device_initialize(&phy->dev);
1022 mutex_init(&phy->mutex);
1023
1024 phy->dev.class = &phy_class;
1025 phy->dev.parent = dev;
1026 phy->dev.of_node = node ?: dev->of_node;
1027 phy->id = id;
1028 phy->ops = ops;
1029
1030 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
1031 if (ret)
1032 goto put_dev;
1033
1034 /* phy-supply */
1035 phy->pwr = regulator_get_optional(&phy->dev, "phy");
1036 if (IS_ERR(phy->pwr)) {
1037 ret = PTR_ERR(phy->pwr);
1038 if (ret == -EPROBE_DEFER)
1039 goto put_dev;
1040
1041 phy->pwr = NULL;
1042 }
1043
1044 ret = device_add(&phy->dev);
1045 if (ret)
1046 goto put_dev;
1047
1048 if (pm_runtime_enabled(dev)) {
1049 pm_runtime_enable(&phy->dev);
1050 pm_runtime_no_callbacks(&phy->dev);
1051 }
1052
1053 phy->debugfs = debugfs_create_dir(dev_name(&phy->dev), phy_debugfs_root);
1054
1055 return phy;
1056
1057 put_dev:
1058 put_device(&phy->dev); /* calls phy_release() which frees resources */
1059 return ERR_PTR(ret);
1060
1061 free_phy:
1062 kfree(phy);
1063 return ERR_PTR(ret);
1064 }
1065 EXPORT_SYMBOL_GPL(phy_create);
1066
1067 /**
1068 * devm_phy_create() - create a new phy
1069 * @dev: device that is creating the new phy
1070 * @node: device node of the phy
1071 * @ops: function pointers for performing phy operations
1072 *
1073 * Creates a new PHY device adding it to the PHY class.
1074 * While at that, it also associates the device with the phy using devres.
1075 * On driver detach, release function is invoked on the devres data,
1076 * then, devres data is freed.
1077 */
devm_phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)1078 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
1079 const struct phy_ops *ops)
1080 {
1081 struct phy **ptr, *phy;
1082
1083 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
1084 if (!ptr)
1085 return ERR_PTR(-ENOMEM);
1086
1087 phy = phy_create(dev, node, ops);
1088 if (!IS_ERR(phy)) {
1089 *ptr = phy;
1090 devres_add(dev, ptr);
1091 } else {
1092 devres_free(ptr);
1093 }
1094
1095 return phy;
1096 }
1097 EXPORT_SYMBOL_GPL(devm_phy_create);
1098
1099 /**
1100 * phy_destroy() - destroy the phy
1101 * @phy: the phy to be destroyed
1102 *
1103 * Called to destroy the phy.
1104 */
phy_destroy(struct phy * phy)1105 void phy_destroy(struct phy *phy)
1106 {
1107 pm_runtime_disable(&phy->dev);
1108 device_unregister(&phy->dev);
1109 }
1110 EXPORT_SYMBOL_GPL(phy_destroy);
1111
1112 /**
1113 * devm_phy_destroy() - destroy the PHY
1114 * @dev: device that wants to release this phy
1115 * @phy: the phy returned by devm_phy_get()
1116 *
1117 * destroys the devres associated with this phy and invokes phy_destroy
1118 * to destroy the phy.
1119 */
devm_phy_destroy(struct device * dev,struct phy * phy)1120 void devm_phy_destroy(struct device *dev, struct phy *phy)
1121 {
1122 int r;
1123
1124 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
1125 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
1126 }
1127 EXPORT_SYMBOL_GPL(devm_phy_destroy);
1128
1129 /**
1130 * __of_phy_provider_register() - create/register phy provider with the framework
1131 * @dev: struct device of the phy provider
1132 * @children: device node containing children (if different from dev->of_node)
1133 * @owner: the module owner containing of_xlate
1134 * @of_xlate: function pointer to obtain phy instance from phy provider
1135 *
1136 * Creates struct phy_provider from dev and of_xlate function pointer.
1137 * This is used in the case of dt boot for finding the phy instance from
1138 * phy provider.
1139 *
1140 * If the PHY provider doesn't nest children directly but uses a separate
1141 * child node to contain the individual children, the @children parameter
1142 * can be used to override the default. If NULL, the default (dev->of_node)
1143 * will be used. If non-NULL, the device node must be a child (or further
1144 * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
1145 * error code is returned.
1146 */
__of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,const struct of_phandle_args * args))1147 struct phy_provider *__of_phy_provider_register(struct device *dev,
1148 struct device_node *children, struct module *owner,
1149 struct phy * (*of_xlate)(struct device *dev,
1150 const struct of_phandle_args *args))
1151 {
1152 struct phy_provider *phy_provider;
1153
1154 /*
1155 * If specified, the device node containing the children must itself
1156 * be the provider's device node or a child (or further descendant)
1157 * thereof.
1158 */
1159 if (children) {
1160 struct device_node *parent = of_node_get(children), *next;
1161
1162 while (parent) {
1163 if (parent == dev->of_node)
1164 break;
1165
1166 next = of_get_parent(parent);
1167 of_node_put(parent);
1168 parent = next;
1169 }
1170
1171 if (!parent)
1172 return ERR_PTR(-EINVAL);
1173
1174 of_node_put(parent);
1175 } else {
1176 children = dev->of_node;
1177 }
1178
1179 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
1180 if (!phy_provider)
1181 return ERR_PTR(-ENOMEM);
1182
1183 phy_provider->dev = dev;
1184 phy_provider->children = of_node_get(children);
1185 phy_provider->owner = owner;
1186 phy_provider->of_xlate = of_xlate;
1187
1188 mutex_lock(&phy_provider_mutex);
1189 list_add_tail(&phy_provider->list, &phy_provider_list);
1190 mutex_unlock(&phy_provider_mutex);
1191
1192 return phy_provider;
1193 }
1194 EXPORT_SYMBOL_GPL(__of_phy_provider_register);
1195
1196 /**
1197 * __devm_of_phy_provider_register() - create/register phy provider with the
1198 * framework
1199 * @dev: struct device of the phy provider
1200 * @children: device node containing children (if different from dev->of_node)
1201 * @owner: the module owner containing of_xlate
1202 * @of_xlate: function pointer to obtain phy instance from phy provider
1203 *
1204 * Creates struct phy_provider from dev and of_xlate function pointer.
1205 * This is used in the case of dt boot for finding the phy instance from
1206 * phy provider. While at that, it also associates the device with the
1207 * phy provider using devres. On driver detach, release function is invoked
1208 * on the devres data, then, devres data is freed.
1209 */
__devm_of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,const struct of_phandle_args * args))1210 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
1211 struct device_node *children, struct module *owner,
1212 struct phy * (*of_xlate)(struct device *dev,
1213 const struct of_phandle_args *args))
1214 {
1215 struct phy_provider **ptr, *phy_provider;
1216
1217 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
1218 if (!ptr)
1219 return ERR_PTR(-ENOMEM);
1220
1221 phy_provider = __of_phy_provider_register(dev, children, owner,
1222 of_xlate);
1223 if (!IS_ERR(phy_provider)) {
1224 *ptr = phy_provider;
1225 devres_add(dev, ptr);
1226 } else {
1227 devres_free(ptr);
1228 }
1229
1230 return phy_provider;
1231 }
1232 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
1233
1234 /**
1235 * of_phy_provider_unregister() - unregister phy provider from the framework
1236 * @phy_provider: phy provider returned by of_phy_provider_register()
1237 *
1238 * Removes the phy_provider created using of_phy_provider_register().
1239 */
of_phy_provider_unregister(struct phy_provider * phy_provider)1240 void of_phy_provider_unregister(struct phy_provider *phy_provider)
1241 {
1242 if (IS_ERR(phy_provider))
1243 return;
1244
1245 mutex_lock(&phy_provider_mutex);
1246 list_del(&phy_provider->list);
1247 of_node_put(phy_provider->children);
1248 kfree(phy_provider);
1249 mutex_unlock(&phy_provider_mutex);
1250 }
1251 EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
1252
1253 /**
1254 * devm_of_phy_provider_unregister() - remove phy provider from the framework
1255 * @dev: struct device of the phy provider
1256 * @phy_provider: phy provider returned by of_phy_provider_register()
1257 *
1258 * destroys the devres associated with this phy provider and invokes
1259 * of_phy_provider_unregister to unregister the phy provider.
1260 */
devm_of_phy_provider_unregister(struct device * dev,struct phy_provider * phy_provider)1261 void devm_of_phy_provider_unregister(struct device *dev,
1262 struct phy_provider *phy_provider)
1263 {
1264 int r;
1265
1266 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
1267 phy_provider);
1268 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
1269 }
1270 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
1271
1272 /**
1273 * phy_release() - release the phy
1274 * @dev: the dev member within phy
1275 *
1276 * When the last reference to the device is removed, it is called
1277 * from the embedded kobject as release method.
1278 */
phy_release(struct device * dev)1279 static void phy_release(struct device *dev)
1280 {
1281 struct phy *phy;
1282
1283 phy = to_phy(dev);
1284 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
1285 debugfs_remove_recursive(phy->debugfs);
1286 regulator_put(phy->pwr);
1287 ida_free(&phy_ida, phy->id);
1288 kfree(phy);
1289 }
1290
phy_core_init(void)1291 static int __init phy_core_init(void)
1292 {
1293 int err;
1294
1295 err = class_register(&phy_class);
1296 if (err) {
1297 pr_err("failed to register phy class");
1298 return err;
1299 }
1300
1301 phy_debugfs_root = debugfs_create_dir("phy", NULL);
1302
1303 return 0;
1304 }
1305 device_initcall(phy_core_init);
1306
phy_core_exit(void)1307 static void __exit phy_core_exit(void)
1308 {
1309 debugfs_remove_recursive(phy_debugfs_root);
1310 class_unregister(&phy_class);
1311 }
1312 module_exit(phy_core_exit);
1313