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