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