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