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