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