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