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