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