Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
ff764963 KVA |
2 | /* |
3 | * phy-core.c -- Generic Phy framework. | |
4 | * | |
5 | * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com | |
6 | * | |
7 | * Author: Kishon Vijay Abraham I <kishon@ti.com> | |
ff764963 KVA |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/export.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/err.h> | |
91694772 | 14 | #include <linux/debugfs.h> |
ff764963 KVA |
15 | #include <linux/device.h> |
16 | #include <linux/slab.h> | |
17 | #include <linux/of.h> | |
18 | #include <linux/phy/phy.h> | |
19 | #include <linux/idr.h> | |
20 | #include <linux/pm_runtime.h> | |
3be88125 | 21 | #include <linux/regulator/consumer.h> |
ff764963 | 22 | |
db704bf6 RM |
23 | static void phy_release(struct device *dev); |
24 | static const struct class phy_class = { | |
25 | .name = "phy", | |
26 | .dev_release = phy_release, | |
27 | }; | |
28 | ||
91694772 | 29 | static struct dentry *phy_debugfs_root; |
ff764963 KVA |
30 | static DEFINE_MUTEX(phy_provider_mutex); |
31 | static LIST_HEAD(phy_provider_list); | |
b7bc15b9 | 32 | static LIST_HEAD(phys); |
ff764963 KVA |
33 | static DEFINE_IDA(phy_ida); |
34 | ||
35 | static void devm_phy_release(struct device *dev, void *res) | |
36 | { | |
37 | struct phy *phy = *(struct phy **)res; | |
38 | ||
987351e1 | 39 | phy_put(dev, phy); |
ff764963 KVA |
40 | } |
41 | ||
42 | static void devm_phy_provider_release(struct device *dev, void *res) | |
43 | { | |
44 | struct phy_provider *phy_provider = *(struct phy_provider **)res; | |
45 | ||
46 | of_phy_provider_unregister(phy_provider); | |
47 | } | |
48 | ||
49 | static void devm_phy_consume(struct device *dev, void *res) | |
50 | { | |
51 | struct phy *phy = *(struct phy **)res; | |
52 | ||
53 | phy_destroy(phy); | |
54 | } | |
55 | ||
56 | static int devm_phy_match(struct device *dev, void *res, void *match_data) | |
57 | { | |
2f1bce48 TR |
58 | struct phy **phy = res; |
59 | ||
60 | return *phy == match_data; | |
ff764963 KVA |
61 | } |
62 | ||
b7bc15b9 HK |
63 | /** |
64 | * phy_create_lookup() - allocate and register PHY/device association | |
65 | * @phy: the phy of the association | |
66 | * @con_id: connection ID string on device | |
67 | * @dev_id: the device of the association | |
68 | * | |
69 | * Creates and registers phy_lookup entry. | |
70 | */ | |
71 | int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id) | |
72 | { | |
73 | struct phy_lookup *pl; | |
74 | ||
75 | if (!phy || !dev_id || !con_id) | |
76 | return -EINVAL; | |
77 | ||
78 | pl = kzalloc(sizeof(*pl), GFP_KERNEL); | |
79 | if (!pl) | |
80 | return -ENOMEM; | |
81 | ||
82 | pl->dev_id = dev_id; | |
83 | pl->con_id = con_id; | |
84 | pl->phy = phy; | |
85 | ||
86 | mutex_lock(&phy_provider_mutex); | |
87 | list_add_tail(&pl->node, &phys); | |
88 | mutex_unlock(&phy_provider_mutex); | |
89 | ||
90 | return 0; | |
91 | } | |
92 | EXPORT_SYMBOL_GPL(phy_create_lookup); | |
93 | ||
94 | /** | |
95 | * phy_remove_lookup() - find and remove PHY/device association | |
96 | * @phy: the phy of the association | |
97 | * @con_id: connection ID string on device | |
98 | * @dev_id: the device of the association | |
99 | * | |
100 | * Finds and unregisters phy_lookup entry that was created with | |
101 | * phy_create_lookup(). | |
102 | */ | |
103 | void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id) | |
104 | { | |
105 | struct phy_lookup *pl; | |
106 | ||
107 | if (!phy || !dev_id || !con_id) | |
108 | return; | |
109 | ||
110 | mutex_lock(&phy_provider_mutex); | |
111 | list_for_each_entry(pl, &phys, node) | |
112 | if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) && | |
113 | !strcmp(pl->con_id, con_id)) { | |
114 | list_del(&pl->node); | |
115 | kfree(pl); | |
116 | break; | |
117 | } | |
118 | mutex_unlock(&phy_provider_mutex); | |
119 | } | |
120 | EXPORT_SYMBOL_GPL(phy_remove_lookup); | |
121 | ||
122 | static struct phy *phy_find(struct device *dev, const char *con_id) | |
123 | { | |
124 | const char *dev_id = dev_name(dev); | |
125 | struct phy_lookup *p, *pl = NULL; | |
b7bc15b9 HK |
126 | |
127 | mutex_lock(&phy_provider_mutex); | |
128 | list_for_each_entry(p, &phys, node) | |
129 | if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) { | |
130 | pl = p; | |
131 | break; | |
132 | } | |
133 | mutex_unlock(&phy_provider_mutex); | |
134 | ||
dbc98635 | 135 | return pl ? pl->phy : ERR_PTR(-ENODEV); |
b7bc15b9 HK |
136 | } |
137 | ||
ff764963 KVA |
138 | static struct phy_provider *of_phy_provider_lookup(struct device_node *node) |
139 | { | |
140 | struct phy_provider *phy_provider; | |
2a4c3701 | 141 | struct device_node *child; |
ff764963 KVA |
142 | |
143 | list_for_each_entry(phy_provider, &phy_provider_list, list) { | |
144 | if (phy_provider->dev->of_node == node) | |
145 | return phy_provider; | |
2a4c3701 | 146 | |
1140f7c8 | 147 | for_each_child_of_node(phy_provider->children, child) |
2a4c3701 KVA |
148 | if (child == node) |
149 | return phy_provider; | |
ff764963 KVA |
150 | } |
151 | ||
152 | return ERR_PTR(-EPROBE_DEFER); | |
153 | } | |
154 | ||
155 | int phy_pm_runtime_get(struct phy *phy) | |
156 | { | |
cedb7f89 FB |
157 | int ret; |
158 | ||
8866df25 MG |
159 | if (!phy) |
160 | return 0; | |
161 | ||
ff764963 KVA |
162 | if (!pm_runtime_enabled(&phy->dev)) |
163 | return -ENOTSUPP; | |
164 | ||
cedb7f89 FB |
165 | ret = pm_runtime_get(&phy->dev); |
166 | if (ret < 0 && ret != -EINPROGRESS) | |
167 | pm_runtime_put_noidle(&phy->dev); | |
168 | ||
169 | return ret; | |
ff764963 KVA |
170 | } |
171 | EXPORT_SYMBOL_GPL(phy_pm_runtime_get); | |
172 | ||
173 | int phy_pm_runtime_get_sync(struct phy *phy) | |
174 | { | |
cedb7f89 FB |
175 | int ret; |
176 | ||
8866df25 MG |
177 | if (!phy) |
178 | return 0; | |
179 | ||
ff764963 KVA |
180 | if (!pm_runtime_enabled(&phy->dev)) |
181 | return -ENOTSUPP; | |
182 | ||
cedb7f89 FB |
183 | ret = pm_runtime_get_sync(&phy->dev); |
184 | if (ret < 0) | |
185 | pm_runtime_put_sync(&phy->dev); | |
186 | ||
187 | return ret; | |
ff764963 KVA |
188 | } |
189 | EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync); | |
190 | ||
191 | int phy_pm_runtime_put(struct phy *phy) | |
192 | { | |
8866df25 MG |
193 | if (!phy) |
194 | return 0; | |
195 | ||
ff764963 KVA |
196 | if (!pm_runtime_enabled(&phy->dev)) |
197 | return -ENOTSUPP; | |
198 | ||
199 | return pm_runtime_put(&phy->dev); | |
200 | } | |
201 | EXPORT_SYMBOL_GPL(phy_pm_runtime_put); | |
202 | ||
203 | int phy_pm_runtime_put_sync(struct phy *phy) | |
204 | { | |
8866df25 MG |
205 | if (!phy) |
206 | return 0; | |
207 | ||
ff764963 KVA |
208 | if (!pm_runtime_enabled(&phy->dev)) |
209 | return -ENOTSUPP; | |
210 | ||
211 | return pm_runtime_put_sync(&phy->dev); | |
212 | } | |
213 | EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync); | |
214 | ||
215 | void phy_pm_runtime_allow(struct phy *phy) | |
216 | { | |
8866df25 MG |
217 | if (!phy) |
218 | return; | |
219 | ||
ff764963 KVA |
220 | if (!pm_runtime_enabled(&phy->dev)) |
221 | return; | |
222 | ||
223 | pm_runtime_allow(&phy->dev); | |
224 | } | |
225 | EXPORT_SYMBOL_GPL(phy_pm_runtime_allow); | |
226 | ||
227 | void phy_pm_runtime_forbid(struct phy *phy) | |
228 | { | |
8866df25 MG |
229 | if (!phy) |
230 | return; | |
231 | ||
ff764963 KVA |
232 | if (!pm_runtime_enabled(&phy->dev)) |
233 | return; | |
234 | ||
235 | pm_runtime_forbid(&phy->dev); | |
236 | } | |
237 | EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid); | |
238 | ||
f1b8d335 JM |
239 | /** |
240 | * phy_init - phy internal initialization before phy operation | |
241 | * @phy: the phy returned by phy_get() | |
242 | * | |
243 | * Used to allow phy's driver to perform phy internal initialization, | |
244 | * such as PLL block powering, clock initialization or anything that's | |
245 | * is required by the phy to perform the start of operation. | |
246 | * Must be called before phy_power_on(). | |
247 | * | |
248 | * Return: %0 if successful, a negative error code otherwise | |
249 | */ | |
ff764963 KVA |
250 | int phy_init(struct phy *phy) |
251 | { | |
252 | int ret; | |
253 | ||
04c2faca AL |
254 | if (!phy) |
255 | return 0; | |
256 | ||
ff764963 KVA |
257 | ret = phy_pm_runtime_get_sync(phy); |
258 | if (ret < 0 && ret != -ENOTSUPP) | |
259 | return ret; | |
736b67a3 | 260 | ret = 0; /* Override possible ret == -ENOTSUPP */ |
ff764963 KVA |
261 | |
262 | mutex_lock(&phy->mutex); | |
1599069a JM |
263 | if (phy->power_count > phy->init_count) |
264 | dev_warn(&phy->dev, "phy_power_on was called before phy_init\n"); | |
265 | ||
637d378c | 266 | if (phy->init_count == 0 && phy->ops->init) { |
ff764963 KVA |
267 | ret = phy->ops->init(phy); |
268 | if (ret < 0) { | |
269 | dev_err(&phy->dev, "phy init failed --> %d\n", ret); | |
270 | goto out; | |
271 | } | |
272 | } | |
637d378c | 273 | ++phy->init_count; |
ff764963 KVA |
274 | |
275 | out: | |
276 | mutex_unlock(&phy->mutex); | |
277 | phy_pm_runtime_put(phy); | |
278 | return ret; | |
279 | } | |
280 | EXPORT_SYMBOL_GPL(phy_init); | |
281 | ||
f1b8d335 JM |
282 | /** |
283 | * phy_exit - Phy internal un-initialization | |
284 | * @phy: the phy returned by phy_get() | |
285 | * | |
286 | * Must be called after phy_power_off(). | |
287 | * | |
288 | * Return: %0 if successful, a negative error code otherwise | |
289 | */ | |
ff764963 KVA |
290 | int phy_exit(struct phy *phy) |
291 | { | |
292 | int ret; | |
293 | ||
04c2faca AL |
294 | if (!phy) |
295 | return 0; | |
296 | ||
ff764963 KVA |
297 | ret = phy_pm_runtime_get_sync(phy); |
298 | if (ret < 0 && ret != -ENOTSUPP) | |
299 | return ret; | |
736b67a3 | 300 | ret = 0; /* Override possible ret == -ENOTSUPP */ |
ff764963 KVA |
301 | |
302 | mutex_lock(&phy->mutex); | |
637d378c | 303 | if (phy->init_count == 1 && phy->ops->exit) { |
ff764963 KVA |
304 | ret = phy->ops->exit(phy); |
305 | if (ret < 0) { | |
306 | dev_err(&phy->dev, "phy exit failed --> %d\n", ret); | |
307 | goto out; | |
308 | } | |
309 | } | |
637d378c | 310 | --phy->init_count; |
ff764963 KVA |
311 | |
312 | out: | |
313 | mutex_unlock(&phy->mutex); | |
314 | phy_pm_runtime_put(phy); | |
315 | return ret; | |
316 | } | |
317 | EXPORT_SYMBOL_GPL(phy_exit); | |
318 | ||
f1b8d335 JM |
319 | /** |
320 | * phy_power_on - Enable the phy and enter proper operation | |
321 | * @phy: the phy returned by phy_get() | |
322 | * | |
323 | * Must be called after phy_init(). | |
324 | * | |
325 | * Return: %0 if successful, a negative error code otherwise | |
326 | */ | |
ff764963 KVA |
327 | int phy_power_on(struct phy *phy) |
328 | { | |
b82fcabe | 329 | int ret = 0; |
ff764963 | 330 | |
04c2faca | 331 | if (!phy) |
b82fcabe | 332 | goto out; |
04c2faca | 333 | |
3be88125 RQ |
334 | if (phy->pwr) { |
335 | ret = regulator_enable(phy->pwr); | |
336 | if (ret) | |
b82fcabe | 337 | goto out; |
3be88125 RQ |
338 | } |
339 | ||
ff764963 KVA |
340 | ret = phy_pm_runtime_get_sync(phy); |
341 | if (ret < 0 && ret != -ENOTSUPP) | |
b82fcabe SL |
342 | goto err_pm_sync; |
343 | ||
736b67a3 | 344 | ret = 0; /* Override possible ret == -ENOTSUPP */ |
ff764963 KVA |
345 | |
346 | mutex_lock(&phy->mutex); | |
637d378c | 347 | if (phy->power_count == 0 && phy->ops->power_on) { |
ff764963 KVA |
348 | ret = phy->ops->power_on(phy); |
349 | if (ret < 0) { | |
350 | dev_err(&phy->dev, "phy poweron failed --> %d\n", ret); | |
b82fcabe | 351 | goto err_pwr_on; |
ff764963 KVA |
352 | } |
353 | } | |
637d378c KVA |
354 | ++phy->power_count; |
355 | mutex_unlock(&phy->mutex); | |
356 | return 0; | |
ff764963 | 357 | |
b82fcabe | 358 | err_pwr_on: |
ff764963 | 359 | mutex_unlock(&phy->mutex); |
637d378c | 360 | phy_pm_runtime_put_sync(phy); |
b82fcabe | 361 | err_pm_sync: |
3be88125 RQ |
362 | if (phy->pwr) |
363 | regulator_disable(phy->pwr); | |
b82fcabe | 364 | out: |
ff764963 KVA |
365 | return ret; |
366 | } | |
367 | EXPORT_SYMBOL_GPL(phy_power_on); | |
368 | ||
f1b8d335 JM |
369 | /** |
370 | * phy_power_off - Disable the phy. | |
371 | * @phy: the phy returned by phy_get() | |
372 | * | |
373 | * Must be called before phy_exit(). | |
374 | * | |
375 | * Return: %0 if successful, a negative error code otherwise | |
376 | */ | |
ff764963 KVA |
377 | int phy_power_off(struct phy *phy) |
378 | { | |
d18c9604 | 379 | int ret; |
ff764963 | 380 | |
04c2faca AL |
381 | if (!phy) |
382 | return 0; | |
383 | ||
ff764963 | 384 | mutex_lock(&phy->mutex); |
637d378c | 385 | if (phy->power_count == 1 && phy->ops->power_off) { |
ff764963 KVA |
386 | ret = phy->ops->power_off(phy); |
387 | if (ret < 0) { | |
388 | dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret); | |
637d378c KVA |
389 | mutex_unlock(&phy->mutex); |
390 | return ret; | |
ff764963 KVA |
391 | } |
392 | } | |
637d378c | 393 | --phy->power_count; |
ff764963 KVA |
394 | mutex_unlock(&phy->mutex); |
395 | phy_pm_runtime_put(phy); | |
396 | ||
3be88125 RQ |
397 | if (phy->pwr) |
398 | regulator_disable(phy->pwr); | |
399 | ||
637d378c | 400 | return 0; |
ff764963 KVA |
401 | } |
402 | EXPORT_SYMBOL_GPL(phy_power_off); | |
403 | ||
79a5a18a | 404 | int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode) |
300eb013 DL |
405 | { |
406 | int ret; | |
407 | ||
408 | if (!phy || !phy->ops->set_mode) | |
409 | return 0; | |
410 | ||
411 | mutex_lock(&phy->mutex); | |
79a5a18a | 412 | ret = phy->ops->set_mode(phy, mode, submode); |
3b3cd24a MG |
413 | if (!ret) |
414 | phy->attrs.mode = mode; | |
300eb013 DL |
415 | mutex_unlock(&phy->mutex); |
416 | ||
417 | return ret; | |
418 | } | |
79a5a18a | 419 | EXPORT_SYMBOL_GPL(phy_set_mode_ext); |
300eb013 | 420 | |
6c172e73 SH |
421 | int phy_set_media(struct phy *phy, enum phy_media media) |
422 | { | |
423 | int ret; | |
424 | ||
425 | if (!phy || !phy->ops->set_media) | |
426 | return 0; | |
427 | ||
428 | mutex_lock(&phy->mutex); | |
429 | ret = phy->ops->set_media(phy, media); | |
430 | mutex_unlock(&phy->mutex); | |
431 | ||
432 | return ret; | |
433 | } | |
434 | EXPORT_SYMBOL_GPL(phy_set_media); | |
435 | ||
436 | int phy_set_speed(struct phy *phy, int speed) | |
437 | { | |
438 | int ret; | |
439 | ||
440 | if (!phy || !phy->ops->set_speed) | |
441 | return 0; | |
442 | ||
443 | mutex_lock(&phy->mutex); | |
444 | ret = phy->ops->set_speed(phy, speed); | |
445 | mutex_unlock(&phy->mutex); | |
446 | ||
447 | return ret; | |
448 | } | |
449 | EXPORT_SYMBOL_GPL(phy_set_speed); | |
450 | ||
cac18ecb RL |
451 | int phy_reset(struct phy *phy) |
452 | { | |
453 | int ret; | |
454 | ||
455 | if (!phy || !phy->ops->reset) | |
456 | return 0; | |
457 | ||
4df614c4 KVA |
458 | ret = phy_pm_runtime_get_sync(phy); |
459 | if (ret < 0 && ret != -ENOTSUPP) | |
460 | return ret; | |
461 | ||
cac18ecb RL |
462 | mutex_lock(&phy->mutex); |
463 | ret = phy->ops->reset(phy); | |
464 | mutex_unlock(&phy->mutex); | |
465 | ||
4df614c4 KVA |
466 | phy_pm_runtime_put(phy); |
467 | ||
cac18ecb RL |
468 | return ret; |
469 | } | |
470 | EXPORT_SYMBOL_GPL(phy_reset); | |
471 | ||
bbae18f0 MS |
472 | /** |
473 | * phy_calibrate() - Tunes the phy hw parameters for current configuration | |
474 | * @phy: the phy returned by phy_get() | |
475 | * | |
476 | * Used to calibrate phy hardware, typically by adjusting some parameters in | |
477 | * runtime, which are otherwise lost after host controller reset and cannot | |
478 | * be applied in phy_init() or phy_power_on(). | |
479 | * | |
bd5bd02e | 480 | * Return: %0 if successful, a negative error code otherwise |
bbae18f0 | 481 | */ |
36914111 AP |
482 | int phy_calibrate(struct phy *phy) |
483 | { | |
484 | int ret; | |
485 | ||
486 | if (!phy || !phy->ops->calibrate) | |
487 | return 0; | |
488 | ||
489 | mutex_lock(&phy->mutex); | |
490 | ret = phy->ops->calibrate(phy); | |
491 | mutex_unlock(&phy->mutex); | |
492 | ||
493 | return ret; | |
494 | } | |
495 | EXPORT_SYMBOL_GPL(phy_calibrate); | |
496 | ||
5de5f1e2 SC |
497 | /** |
498 | * phy_notify_connect() - phy connect notification | |
499 | * @phy: the phy returned by phy_get() | |
500 | * @port: the port index for connect | |
501 | * | |
502 | * If the phy needs to get connection status, the callback can be used. | |
503 | * Returns: %0 if successful, a negative error code otherwise | |
504 | */ | |
505 | int phy_notify_connect(struct phy *phy, int port) | |
506 | { | |
507 | int ret; | |
508 | ||
509 | if (!phy || !phy->ops->connect) | |
510 | return 0; | |
511 | ||
512 | mutex_lock(&phy->mutex); | |
513 | ret = phy->ops->connect(phy, port); | |
514 | mutex_unlock(&phy->mutex); | |
515 | ||
516 | return ret; | |
517 | } | |
518 | EXPORT_SYMBOL_GPL(phy_notify_connect); | |
519 | ||
520 | /** | |
521 | * phy_notify_disconnect() - phy disconnect notification | |
522 | * @phy: the phy returned by phy_get() | |
523 | * @port: the port index for disconnect | |
524 | * | |
525 | * If the phy needs to get connection status, the callback can be used. | |
526 | * | |
527 | * Returns: %0 if successful, a negative error code otherwise | |
528 | */ | |
529 | int phy_notify_disconnect(struct phy *phy, int port) | |
530 | { | |
531 | int ret; | |
532 | ||
533 | if (!phy || !phy->ops->disconnect) | |
534 | return 0; | |
535 | ||
536 | mutex_lock(&phy->mutex); | |
537 | ret = phy->ops->disconnect(phy, port); | |
538 | mutex_unlock(&phy->mutex); | |
539 | ||
540 | return ret; | |
541 | } | |
542 | EXPORT_SYMBOL_GPL(phy_notify_disconnect); | |
543 | ||
aeaac93d MR |
544 | /** |
545 | * phy_configure() - Changes the phy parameters | |
546 | * @phy: the phy returned by phy_get() | |
547 | * @opts: New configuration to apply | |
548 | * | |
549 | * Used to change the PHY parameters. phy_init() must have been called | |
550 | * on the phy. The configuration will be applied on the current phy | |
551 | * mode, that can be changed using phy_set_mode(). | |
552 | * | |
bd5bd02e | 553 | * Return: %0 if successful, a negative error code otherwise |
aeaac93d MR |
554 | */ |
555 | int phy_configure(struct phy *phy, union phy_configure_opts *opts) | |
556 | { | |
557 | int ret; | |
558 | ||
559 | if (!phy) | |
560 | return -EINVAL; | |
561 | ||
562 | if (!phy->ops->configure) | |
563 | return -EOPNOTSUPP; | |
564 | ||
565 | mutex_lock(&phy->mutex); | |
566 | ret = phy->ops->configure(phy, opts); | |
567 | mutex_unlock(&phy->mutex); | |
568 | ||
569 | return ret; | |
570 | } | |
571 | EXPORT_SYMBOL_GPL(phy_configure); | |
572 | ||
573 | /** | |
574 | * phy_validate() - Checks the phy parameters | |
575 | * @phy: the phy returned by phy_get() | |
576 | * @mode: phy_mode the configuration is applicable to. | |
577 | * @submode: PHY submode the configuration is applicable to. | |
578 | * @opts: Configuration to check | |
579 | * | |
580 | * Used to check that the current set of parameters can be handled by | |
581 | * the phy. Implementations are free to tune the parameters passed as | |
582 | * arguments if needed by some implementation detail or | |
583 | * constraints. It will not change any actual configuration of the | |
584 | * PHY, so calling it as many times as deemed fit will have no side | |
585 | * effect. | |
586 | * | |
bd5bd02e | 587 | * Return: %0 if successful, a negative error code otherwise |
aeaac93d MR |
588 | */ |
589 | int phy_validate(struct phy *phy, enum phy_mode mode, int submode, | |
590 | union phy_configure_opts *opts) | |
591 | { | |
592 | int ret; | |
593 | ||
594 | if (!phy) | |
595 | return -EINVAL; | |
596 | ||
597 | if (!phy->ops->validate) | |
598 | return -EOPNOTSUPP; | |
599 | ||
600 | mutex_lock(&phy->mutex); | |
601 | ret = phy->ops->validate(phy, mode, submode, opts); | |
602 | mutex_unlock(&phy->mutex); | |
603 | ||
604 | return ret; | |
605 | } | |
606 | EXPORT_SYMBOL_GPL(phy_validate); | |
607 | ||
ff764963 | 608 | /** |
0b3f3b2c KD |
609 | * _of_phy_get() - lookup and obtain a reference to a phy by phandle |
610 | * @np: device_node for which to get the phy | |
ff764963 KVA |
611 | * @index: the index of the phy |
612 | * | |
613 | * Returns the phy associated with the given phandle value, | |
614 | * after getting a refcount to it or -ENODEV if there is no such phy or | |
615 | * -EPROBE_DEFER if there is a phandle to the phy, but the device is | |
616 | * not yet loaded. This function uses of_xlate call back function provided | |
617 | * while registering the phy_provider to find the phy instance. | |
618 | */ | |
0b3f3b2c | 619 | static struct phy *_of_phy_get(struct device_node *np, int index) |
ff764963 KVA |
620 | { |
621 | int ret; | |
622 | struct phy_provider *phy_provider; | |
623 | struct phy *phy = NULL; | |
624 | struct of_phandle_args args; | |
625 | ||
0b3f3b2c | 626 | ret = of_parse_phandle_with_args(np, "phys", "#phy-cells", |
ff764963 | 627 | index, &args); |
0b3f3b2c | 628 | if (ret) |
ff764963 | 629 | return ERR_PTR(-ENODEV); |
ff764963 | 630 | |
b7563e27 AB |
631 | /* This phy type handled by the usb-phy subsystem for now */ |
632 | if (of_device_is_compatible(args.np, "usb-nop-xceiv")) | |
633 | return ERR_PTR(-ENODEV); | |
634 | ||
ff764963 KVA |
635 | mutex_lock(&phy_provider_mutex); |
636 | phy_provider = of_phy_provider_lookup(args.np); | |
637 | if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) { | |
638 | phy = ERR_PTR(-EPROBE_DEFER); | |
33f434d2 AL |
639 | goto out_unlock; |
640 | } | |
641 | ||
642 | if (!of_device_is_available(args.np)) { | |
643 | dev_warn(phy_provider->dev, "Requested PHY is disabled\n"); | |
644 | phy = ERR_PTR(-ENODEV); | |
645 | goto out_put_module; | |
ff764963 KVA |
646 | } |
647 | ||
648 | phy = phy_provider->of_xlate(phy_provider->dev, &args); | |
33f434d2 AL |
649 | |
650 | out_put_module: | |
ff764963 KVA |
651 | module_put(phy_provider->owner); |
652 | ||
33f434d2 | 653 | out_unlock: |
ff764963 KVA |
654 | mutex_unlock(&phy_provider_mutex); |
655 | of_node_put(args.np); | |
656 | ||
657 | return phy; | |
658 | } | |
659 | ||
0b3f3b2c KD |
660 | /** |
661 | * of_phy_get() - lookup and obtain a reference to a phy using a device_node. | |
662 | * @np: device_node for which to get the phy | |
663 | * @con_id: name of the phy from device's point of view | |
664 | * | |
665 | * Returns the phy driver, after getting a refcount to it; or | |
666 | * -ENODEV if there is no such phy. The caller is responsible for | |
4c75fe2a | 667 | * calling of_phy_put() to release that count. |
0b3f3b2c KD |
668 | */ |
669 | struct phy *of_phy_get(struct device_node *np, const char *con_id) | |
670 | { | |
671 | struct phy *phy = NULL; | |
672 | int index = 0; | |
673 | ||
674 | if (con_id) | |
675 | index = of_property_match_string(np, "phy-names", con_id); | |
676 | ||
677 | phy = _of_phy_get(np, index); | |
678 | if (IS_ERR(phy)) | |
679 | return phy; | |
680 | ||
681 | if (!try_module_get(phy->ops->owner)) | |
682 | return ERR_PTR(-EPROBE_DEFER); | |
683 | ||
684 | get_device(&phy->dev); | |
685 | ||
686 | return phy; | |
687 | } | |
688 | EXPORT_SYMBOL_GPL(of_phy_get); | |
689 | ||
ff764963 | 690 | /** |
987351e1 AT |
691 | * of_phy_put() - release the PHY |
692 | * @phy: the phy returned by of_phy_get() | |
ff764963 | 693 | * |
987351e1 | 694 | * Releases a refcount the caller received from of_phy_get(). |
ff764963 | 695 | */ |
987351e1 | 696 | void of_phy_put(struct phy *phy) |
ff764963 | 697 | { |
04c2faca | 698 | if (!phy || IS_ERR(phy)) |
ff764963 KVA |
699 | return; |
700 | ||
fec06b2b KVA |
701 | mutex_lock(&phy->mutex); |
702 | if (phy->ops->release) | |
703 | phy->ops->release(phy); | |
704 | mutex_unlock(&phy->mutex); | |
705 | ||
ff764963 KVA |
706 | module_put(phy->ops->owner); |
707 | put_device(&phy->dev); | |
708 | } | |
987351e1 AT |
709 | EXPORT_SYMBOL_GPL(of_phy_put); |
710 | ||
711 | /** | |
712 | * phy_put() - release the PHY | |
713 | * @dev: device that wants to release this phy | |
714 | * @phy: the phy returned by phy_get() | |
715 | * | |
716 | * Releases a refcount the caller received from phy_get(). | |
717 | */ | |
718 | void phy_put(struct device *dev, struct phy *phy) | |
719 | { | |
720 | device_link_remove(dev, &phy->dev); | |
721 | of_phy_put(phy); | |
722 | } | |
ff764963 KVA |
723 | EXPORT_SYMBOL_GPL(phy_put); |
724 | ||
725 | /** | |
726 | * devm_phy_put() - release the PHY | |
727 | * @dev: device that wants to release this phy | |
728 | * @phy: the phy returned by devm_phy_get() | |
729 | * | |
730 | * destroys the devres associated with this phy and invokes phy_put | |
731 | * to release the phy. | |
732 | */ | |
733 | void devm_phy_put(struct device *dev, struct phy *phy) | |
734 | { | |
735 | int r; | |
736 | ||
04c2faca AL |
737 | if (!phy) |
738 | return; | |
739 | ||
ff764963 KVA |
740 | r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy); |
741 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); | |
742 | } | |
743 | EXPORT_SYMBOL_GPL(devm_phy_put); | |
744 | ||
745 | /** | |
746 | * of_phy_simple_xlate() - returns the phy instance from phy provider | |
747 | * @dev: the PHY provider device | |
748 | * @args: of_phandle_args (not used here) | |
749 | * | |
750 | * Intended to be used by phy provider for the common case where #phy-cells is | |
751 | * 0. For other cases where #phy-cells is greater than '0', the phy provider | |
752 | * should provide a custom of_xlate function that reads the *args* and returns | |
753 | * the appropriate phy. | |
754 | */ | |
00ca8a15 KK |
755 | struct phy *of_phy_simple_xlate(struct device *dev, |
756 | const struct of_phandle_args *args) | |
ff764963 KVA |
757 | { |
758 | struct phy *phy; | |
759 | struct class_dev_iter iter; | |
ff764963 | 760 | |
db704bf6 | 761 | class_dev_iter_init(&iter, &phy_class, NULL, NULL); |
ff764963 KVA |
762 | while ((dev = class_dev_iter_next(&iter))) { |
763 | phy = to_phy(dev); | |
491e0490 | 764 | if (args->np != phy->dev.of_node) |
ff764963 KVA |
765 | continue; |
766 | ||
767 | class_dev_iter_exit(&iter); | |
768 | return phy; | |
769 | } | |
770 | ||
771 | class_dev_iter_exit(&iter); | |
772 | return ERR_PTR(-ENODEV); | |
773 | } | |
774 | EXPORT_SYMBOL_GPL(of_phy_simple_xlate); | |
775 | ||
776 | /** | |
777 | * phy_get() - lookup and obtain a reference to a phy. | |
778 | * @dev: device that requests this phy | |
779 | * @string: the phy name as given in the dt data or the name of the controller | |
780 | * port for non-dt case | |
781 | * | |
782 | * Returns the phy driver, after getting a refcount to it; or | |
783 | * -ENODEV if there is no such phy. The caller is responsible for | |
784 | * calling phy_put() to release that count. | |
785 | */ | |
786 | struct phy *phy_get(struct device *dev, const char *string) | |
787 | { | |
788 | int index = 0; | |
d18c9604 | 789 | struct phy *phy; |
987351e1 | 790 | struct device_link *link; |
ff764963 | 791 | |
ff764963 | 792 | if (dev->of_node) { |
8a917813 RH |
793 | if (string) |
794 | index = of_property_match_string(dev->of_node, "phy-names", | |
795 | string); | |
796 | else | |
797 | index = 0; | |
0b3f3b2c | 798 | phy = _of_phy_get(dev->of_node, index); |
ff764963 | 799 | } else { |
8a917813 RH |
800 | if (string == NULL) { |
801 | dev_WARN(dev, "missing string\n"); | |
802 | return ERR_PTR(-EINVAL); | |
803 | } | |
b7bc15b9 | 804 | phy = phy_find(dev, string); |
ff764963 | 805 | } |
f40037fd HG |
806 | if (IS_ERR(phy)) |
807 | return phy; | |
ff764963 KVA |
808 | |
809 | if (!try_module_get(phy->ops->owner)) | |
810 | return ERR_PTR(-EPROBE_DEFER); | |
811 | ||
812 | get_device(&phy->dev); | |
813 | ||
987351e1 | 814 | link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); |
1d7cb11e KVA |
815 | if (!link) |
816 | dev_dbg(dev, "failed to create device link to %s\n", | |
987351e1 | 817 | dev_name(phy->dev.parent)); |
987351e1 | 818 | |
ff764963 KVA |
819 | return phy; |
820 | } | |
821 | EXPORT_SYMBOL_GPL(phy_get); | |
822 | ||
823 | /** | |
824 | * devm_phy_get() - lookup and obtain a reference to a phy. | |
825 | * @dev: device that requests this phy | |
826 | * @string: the phy name as given in the dt data or phy device name | |
827 | * for non-dt case | |
828 | * | |
829 | * Gets the phy using phy_get(), and associates a device with it using | |
830 | * devres. On driver detach, release function is invoked on the devres data, | |
831 | * then, devres data is freed. | |
832 | */ | |
833 | struct phy *devm_phy_get(struct device *dev, const char *string) | |
834 | { | |
835 | struct phy **ptr, *phy; | |
836 | ||
837 | ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); | |
838 | if (!ptr) | |
839 | return ERR_PTR(-ENOMEM); | |
840 | ||
841 | phy = phy_get(dev, string); | |
842 | if (!IS_ERR(phy)) { | |
843 | *ptr = phy; | |
844 | devres_add(dev, ptr); | |
845 | } else { | |
846 | devres_free(ptr); | |
847 | } | |
848 | ||
849 | return phy; | |
850 | } | |
851 | EXPORT_SYMBOL_GPL(devm_phy_get); | |
852 | ||
788a4d56 AL |
853 | /** |
854 | * devm_phy_optional_get() - lookup and obtain a reference to an optional phy. | |
855 | * @dev: device that requests this phy | |
856 | * @string: the phy name as given in the dt data or phy device name | |
857 | * for non-dt case | |
858 | * | |
859 | * Gets the phy using phy_get(), and associates a device with it using | |
860 | * devres. On driver detach, release function is invoked on the devres | |
861 | * data, then, devres data is freed. This differs to devm_phy_get() in | |
862 | * that if the phy does not exist, it is not considered an error and | |
863 | * -ENODEV will not be returned. Instead the NULL phy is returned, | |
864 | * which can be passed to all other phy consumer calls. | |
865 | */ | |
866 | struct phy *devm_phy_optional_get(struct device *dev, const char *string) | |
867 | { | |
868 | struct phy *phy = devm_phy_get(dev, string); | |
869 | ||
45586c70 | 870 | if (PTR_ERR(phy) == -ENODEV) |
788a4d56 AL |
871 | phy = NULL; |
872 | ||
873 | return phy; | |
874 | } | |
875 | EXPORT_SYMBOL_GPL(devm_phy_optional_get); | |
876 | ||
b5d682f4 KD |
877 | /** |
878 | * devm_of_phy_get() - lookup and obtain a reference to a phy. | |
879 | * @dev: device that requests this phy | |
880 | * @np: node containing the phy | |
881 | * @con_id: name of the phy from device's point of view | |
882 | * | |
883 | * Gets the phy using of_phy_get(), and associates a device with it using | |
884 | * devres. On driver detach, release function is invoked on the devres data, | |
885 | * then, devres data is freed. | |
886 | */ | |
887 | struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, | |
888 | const char *con_id) | |
889 | { | |
890 | struct phy **ptr, *phy; | |
987351e1 | 891 | struct device_link *link; |
b5d682f4 KD |
892 | |
893 | ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); | |
894 | if (!ptr) | |
895 | return ERR_PTR(-ENOMEM); | |
896 | ||
897 | phy = of_phy_get(np, con_id); | |
898 | if (!IS_ERR(phy)) { | |
899 | *ptr = phy; | |
900 | devres_add(dev, ptr); | |
901 | } else { | |
902 | devres_free(ptr); | |
987351e1 AT |
903 | return phy; |
904 | } | |
905 | ||
906 | link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); | |
1d7cb11e KVA |
907 | if (!link) |
908 | dev_dbg(dev, "failed to create device link to %s\n", | |
987351e1 | 909 | dev_name(phy->dev.parent)); |
b5d682f4 KD |
910 | |
911 | return phy; | |
912 | } | |
913 | EXPORT_SYMBOL_GPL(devm_of_phy_get); | |
914 | ||
d02aa181 GU |
915 | /** |
916 | * devm_of_phy_optional_get() - lookup and obtain a reference to an optional | |
917 | * phy. | |
918 | * @dev: device that requests this phy | |
919 | * @np: node containing the phy | |
920 | * @con_id: name of the phy from device's point of view | |
921 | * | |
922 | * Gets the phy using of_phy_get(), and associates a device with it using | |
923 | * devres. On driver detach, release function is invoked on the devres data, | |
924 | * then, devres data is freed. This differs to devm_of_phy_get() in | |
925 | * that if the phy does not exist, it is not considered an error and | |
926 | * -ENODEV will not be returned. Instead the NULL phy is returned, | |
927 | * which can be passed to all other phy consumer calls. | |
928 | */ | |
929 | struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np, | |
930 | const char *con_id) | |
931 | { | |
932 | struct phy *phy = devm_of_phy_get(dev, np, con_id); | |
933 | ||
934 | if (PTR_ERR(phy) == -ENODEV) | |
935 | phy = NULL; | |
936 | ||
937 | if (IS_ERR(phy)) | |
938 | dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s", | |
939 | np, con_id); | |
940 | ||
941 | return phy; | |
942 | } | |
943 | EXPORT_SYMBOL_GPL(devm_of_phy_optional_get); | |
944 | ||
6be109b3 AR |
945 | /** |
946 | * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index. | |
947 | * @dev: device that requests this phy | |
948 | * @np: node containing the phy | |
949 | * @index: index of the phy | |
950 | * | |
70874462 CY |
951 | * Gets the phy using _of_phy_get(), then gets a refcount to it, |
952 | * and associates a device with it using devres. On driver detach, | |
953 | * release function is invoked on the devres data, | |
6be109b3 AR |
954 | * then, devres data is freed. |
955 | * | |
956 | */ | |
957 | struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, | |
958 | int index) | |
959 | { | |
960 | struct phy **ptr, *phy; | |
987351e1 | 961 | struct device_link *link; |
6be109b3 AR |
962 | |
963 | ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL); | |
964 | if (!ptr) | |
965 | return ERR_PTR(-ENOMEM); | |
966 | ||
967 | phy = _of_phy_get(np, index); | |
70874462 | 968 | if (IS_ERR(phy)) { |
6be109b3 | 969 | devres_free(ptr); |
70874462 | 970 | return phy; |
6be109b3 AR |
971 | } |
972 | ||
70874462 CY |
973 | if (!try_module_get(phy->ops->owner)) { |
974 | devres_free(ptr); | |
975 | return ERR_PTR(-EPROBE_DEFER); | |
976 | } | |
977 | ||
978 | get_device(&phy->dev); | |
979 | ||
980 | *ptr = phy; | |
981 | devres_add(dev, ptr); | |
982 | ||
987351e1 | 983 | link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS); |
1d7cb11e KVA |
984 | if (!link) |
985 | dev_dbg(dev, "failed to create device link to %s\n", | |
987351e1 | 986 | dev_name(phy->dev.parent)); |
987351e1 | 987 | |
6be109b3 AR |
988 | return phy; |
989 | } | |
990 | EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index); | |
991 | ||
ff764963 KVA |
992 | /** |
993 | * phy_create() - create a new phy | |
994 | * @dev: device that is creating the new phy | |
f0ed8176 | 995 | * @node: device node of the phy |
ff764963 | 996 | * @ops: function pointers for performing phy operations |
ff764963 KVA |
997 | * |
998 | * Called to create a phy using phy framework. | |
999 | */ | |
f0ed8176 | 1000 | struct phy *phy_create(struct device *dev, struct device_node *node, |
dbc98635 | 1001 | const struct phy_ops *ops) |
ff764963 KVA |
1002 | { |
1003 | int ret; | |
1004 | int id; | |
1005 | struct phy *phy; | |
1006 | ||
52797d29 DC |
1007 | if (WARN_ON(!dev)) |
1008 | return ERR_PTR(-EINVAL); | |
ff764963 KVA |
1009 | |
1010 | phy = kzalloc(sizeof(*phy), GFP_KERNEL); | |
52797d29 DC |
1011 | if (!phy) |
1012 | return ERR_PTR(-ENOMEM); | |
ff764963 | 1013 | |
772dd70a | 1014 | id = ida_alloc(&phy_ida, GFP_KERNEL); |
ff764963 KVA |
1015 | if (id < 0) { |
1016 | dev_err(dev, "unable to get id\n"); | |
1017 | ret = id; | |
52797d29 | 1018 | goto free_phy; |
ff764963 KVA |
1019 | } |
1020 | ||
1021 | device_initialize(&phy->dev); | |
1022 | mutex_init(&phy->mutex); | |
1023 | ||
db704bf6 | 1024 | phy->dev.class = &phy_class; |
ff764963 | 1025 | phy->dev.parent = dev; |
f0ed8176 | 1026 | phy->dev.of_node = node ?: dev->of_node; |
ff764963 KVA |
1027 | phy->id = id; |
1028 | phy->ops = ops; | |
ff764963 KVA |
1029 | |
1030 | ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id); | |
1031 | if (ret) | |
52797d29 | 1032 | goto put_dev; |
ff764963 | 1033 | |
87006dd6 DT |
1034 | /* phy-supply */ |
1035 | phy->pwr = regulator_get_optional(&phy->dev, "phy"); | |
1036 | if (IS_ERR(phy->pwr)) { | |
1037 | ret = PTR_ERR(phy->pwr); | |
1038 | if (ret == -EPROBE_DEFER) | |
1039 | goto put_dev; | |
1040 | ||
1041 | phy->pwr = NULL; | |
1042 | } | |
1043 | ||
ff764963 KVA |
1044 | ret = device_add(&phy->dev); |
1045 | if (ret) | |
52797d29 | 1046 | goto put_dev; |
ff764963 KVA |
1047 | |
1048 | if (pm_runtime_enabled(dev)) { | |
1049 | pm_runtime_enable(&phy->dev); | |
1050 | pm_runtime_no_callbacks(&phy->dev); | |
1051 | } | |
1052 | ||
91694772 CY |
1053 | phy->debugfs = debugfs_create_dir(dev_name(&phy->dev), phy_debugfs_root); |
1054 | ||
ff764963 KVA |
1055 | return phy; |
1056 | ||
52797d29 | 1057 | put_dev: |
e73b49f1 RQ |
1058 | put_device(&phy->dev); /* calls phy_release() which frees resources */ |
1059 | return ERR_PTR(ret); | |
1060 | ||
52797d29 | 1061 | free_phy: |
ff764963 | 1062 | kfree(phy); |
ff764963 KVA |
1063 | return ERR_PTR(ret); |
1064 | } | |
1065 | EXPORT_SYMBOL_GPL(phy_create); | |
1066 | ||
1067 | /** | |
1068 | * devm_phy_create() - create a new phy | |
1069 | * @dev: device that is creating the new phy | |
f0ed8176 | 1070 | * @node: device node of the phy |
ff764963 | 1071 | * @ops: function pointers for performing phy operations |
ff764963 KVA |
1072 | * |
1073 | * Creates a new PHY device adding it to the PHY class. | |
1074 | * While at that, it also associates the device with the phy using devres. | |
1075 | * On driver detach, release function is invoked on the devres data, | |
1076 | * then, devres data is freed. | |
1077 | */ | |
f0ed8176 | 1078 | struct phy *devm_phy_create(struct device *dev, struct device_node *node, |
dbc98635 | 1079 | const struct phy_ops *ops) |
ff764963 KVA |
1080 | { |
1081 | struct phy **ptr, *phy; | |
1082 | ||
1083 | ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL); | |
1084 | if (!ptr) | |
1085 | return ERR_PTR(-ENOMEM); | |
1086 | ||
dbc98635 | 1087 | phy = phy_create(dev, node, ops); |
ff764963 KVA |
1088 | if (!IS_ERR(phy)) { |
1089 | *ptr = phy; | |
1090 | devres_add(dev, ptr); | |
1091 | } else { | |
1092 | devres_free(ptr); | |
1093 | } | |
1094 | ||
1095 | return phy; | |
1096 | } | |
1097 | EXPORT_SYMBOL_GPL(devm_phy_create); | |
1098 | ||
1099 | /** | |
1100 | * phy_destroy() - destroy the phy | |
1101 | * @phy: the phy to be destroyed | |
1102 | * | |
1103 | * Called to destroy the phy. | |
1104 | */ | |
1105 | void phy_destroy(struct phy *phy) | |
1106 | { | |
1107 | pm_runtime_disable(&phy->dev); | |
1108 | device_unregister(&phy->dev); | |
1109 | } | |
1110 | EXPORT_SYMBOL_GPL(phy_destroy); | |
1111 | ||
1112 | /** | |
1113 | * devm_phy_destroy() - destroy the PHY | |
1114 | * @dev: device that wants to release this phy | |
1115 | * @phy: the phy returned by devm_phy_get() | |
1116 | * | |
1117 | * destroys the devres associated with this phy and invokes phy_destroy | |
1118 | * to destroy the phy. | |
1119 | */ | |
1120 | void devm_phy_destroy(struct device *dev, struct phy *phy) | |
1121 | { | |
1122 | int r; | |
1123 | ||
1124 | r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy); | |
1125 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); | |
1126 | } | |
1127 | EXPORT_SYMBOL_GPL(devm_phy_destroy); | |
1128 | ||
1129 | /** | |
1130 | * __of_phy_provider_register() - create/register phy provider with the framework | |
1131 | * @dev: struct device of the phy provider | |
1140f7c8 | 1132 | * @children: device node containing children (if different from dev->of_node) |
ff764963 KVA |
1133 | * @owner: the module owner containing of_xlate |
1134 | * @of_xlate: function pointer to obtain phy instance from phy provider | |
1135 | * | |
1136 | * Creates struct phy_provider from dev and of_xlate function pointer. | |
1137 | * This is used in the case of dt boot for finding the phy instance from | |
1138 | * phy provider. | |
1140f7c8 TR |
1139 | * |
1140 | * If the PHY provider doesn't nest children directly but uses a separate | |
1141 | * child node to contain the individual children, the @children parameter | |
1142 | * can be used to override the default. If NULL, the default (dev->of_node) | |
1143 | * will be used. If non-NULL, the device node must be a child (or further | |
1144 | * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL | |
1145 | * error code is returned. | |
ff764963 KVA |
1146 | */ |
1147 | struct phy_provider *__of_phy_provider_register(struct device *dev, | |
1140f7c8 TR |
1148 | struct device_node *children, struct module *owner, |
1149 | struct phy * (*of_xlate)(struct device *dev, | |
00ca8a15 | 1150 | const struct of_phandle_args *args)) |
ff764963 KVA |
1151 | { |
1152 | struct phy_provider *phy_provider; | |
1153 | ||
1140f7c8 TR |
1154 | /* |
1155 | * If specified, the device node containing the children must itself | |
1156 | * be the provider's device node or a child (or further descendant) | |
1157 | * thereof. | |
1158 | */ | |
1159 | if (children) { | |
1160 | struct device_node *parent = of_node_get(children), *next; | |
1161 | ||
1162 | while (parent) { | |
1163 | if (parent == dev->of_node) | |
1164 | break; | |
1165 | ||
1166 | next = of_get_parent(parent); | |
1167 | of_node_put(parent); | |
1168 | parent = next; | |
1169 | } | |
1170 | ||
1171 | if (!parent) | |
1172 | return ERR_PTR(-EINVAL); | |
1173 | ||
1174 | of_node_put(parent); | |
1175 | } else { | |
1176 | children = dev->of_node; | |
1177 | } | |
1178 | ||
ff764963 KVA |
1179 | phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL); |
1180 | if (!phy_provider) | |
1181 | return ERR_PTR(-ENOMEM); | |
1182 | ||
1183 | phy_provider->dev = dev; | |
1140f7c8 | 1184 | phy_provider->children = of_node_get(children); |
ff764963 KVA |
1185 | phy_provider->owner = owner; |
1186 | phy_provider->of_xlate = of_xlate; | |
1187 | ||
1188 | mutex_lock(&phy_provider_mutex); | |
1189 | list_add_tail(&phy_provider->list, &phy_provider_list); | |
1190 | mutex_unlock(&phy_provider_mutex); | |
1191 | ||
1192 | return phy_provider; | |
1193 | } | |
1194 | EXPORT_SYMBOL_GPL(__of_phy_provider_register); | |
1195 | ||
1196 | /** | |
1197 | * __devm_of_phy_provider_register() - create/register phy provider with the | |
1198 | * framework | |
1199 | * @dev: struct device of the phy provider | |
aad075c1 | 1200 | * @children: device node containing children (if different from dev->of_node) |
ff764963 KVA |
1201 | * @owner: the module owner containing of_xlate |
1202 | * @of_xlate: function pointer to obtain phy instance from phy provider | |
1203 | * | |
1204 | * Creates struct phy_provider from dev and of_xlate function pointer. | |
1205 | * This is used in the case of dt boot for finding the phy instance from | |
1206 | * phy provider. While at that, it also associates the device with the | |
1207 | * phy provider using devres. On driver detach, release function is invoked | |
1208 | * on the devres data, then, devres data is freed. | |
1209 | */ | |
1210 | struct phy_provider *__devm_of_phy_provider_register(struct device *dev, | |
1140f7c8 TR |
1211 | struct device_node *children, struct module *owner, |
1212 | struct phy * (*of_xlate)(struct device *dev, | |
00ca8a15 | 1213 | const struct of_phandle_args *args)) |
ff764963 KVA |
1214 | { |
1215 | struct phy_provider **ptr, *phy_provider; | |
1216 | ||
1217 | ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL); | |
1218 | if (!ptr) | |
1219 | return ERR_PTR(-ENOMEM); | |
1220 | ||
1140f7c8 TR |
1221 | phy_provider = __of_phy_provider_register(dev, children, owner, |
1222 | of_xlate); | |
ff764963 KVA |
1223 | if (!IS_ERR(phy_provider)) { |
1224 | *ptr = phy_provider; | |
1225 | devres_add(dev, ptr); | |
1226 | } else { | |
1227 | devres_free(ptr); | |
1228 | } | |
1229 | ||
1230 | return phy_provider; | |
1231 | } | |
1232 | EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register); | |
1233 | ||
1234 | /** | |
1235 | * of_phy_provider_unregister() - unregister phy provider from the framework | |
1236 | * @phy_provider: phy provider returned by of_phy_provider_register() | |
1237 | * | |
1238 | * Removes the phy_provider created using of_phy_provider_register(). | |
1239 | */ | |
1240 | void of_phy_provider_unregister(struct phy_provider *phy_provider) | |
1241 | { | |
1242 | if (IS_ERR(phy_provider)) | |
1243 | return; | |
1244 | ||
1245 | mutex_lock(&phy_provider_mutex); | |
1246 | list_del(&phy_provider->list); | |
1140f7c8 | 1247 | of_node_put(phy_provider->children); |
ff764963 KVA |
1248 | kfree(phy_provider); |
1249 | mutex_unlock(&phy_provider_mutex); | |
1250 | } | |
1251 | EXPORT_SYMBOL_GPL(of_phy_provider_unregister); | |
1252 | ||
1253 | /** | |
1254 | * devm_of_phy_provider_unregister() - remove phy provider from the framework | |
1255 | * @dev: struct device of the phy provider | |
aad075c1 | 1256 | * @phy_provider: phy provider returned by of_phy_provider_register() |
ff764963 KVA |
1257 | * |
1258 | * destroys the devres associated with this phy provider and invokes | |
1259 | * of_phy_provider_unregister to unregister the phy provider. | |
1260 | */ | |
1261 | void devm_of_phy_provider_unregister(struct device *dev, | |
b555f35f VK |
1262 | struct phy_provider *phy_provider) |
1263 | { | |
ff764963 KVA |
1264 | int r; |
1265 | ||
1266 | r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match, | |
1267 | phy_provider); | |
1268 | dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n"); | |
1269 | } | |
1270 | EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister); | |
1271 | ||
1272 | /** | |
1273 | * phy_release() - release the phy | |
1274 | * @dev: the dev member within phy | |
1275 | * | |
1276 | * When the last reference to the device is removed, it is called | |
1277 | * from the embedded kobject as release method. | |
1278 | */ | |
1279 | static void phy_release(struct device *dev) | |
1280 | { | |
1281 | struct phy *phy; | |
1282 | ||
1283 | phy = to_phy(dev); | |
1284 | dev_vdbg(dev, "releasing '%s'\n", dev_name(dev)); | |
91694772 | 1285 | debugfs_remove_recursive(phy->debugfs); |
3be88125 | 1286 | regulator_put(phy->pwr); |
772dd70a | 1287 | ida_free(&phy_ida, phy->id); |
ff764963 KVA |
1288 | kfree(phy); |
1289 | } | |
1290 | ||
1291 | static int __init phy_core_init(void) | |
1292 | { | |
db704bf6 | 1293 | int err; |
ff764963 | 1294 | |
db704bf6 RM |
1295 | err = class_register(&phy_class); |
1296 | if (err) { | |
1297 | pr_err("failed to register phy class"); | |
1298 | return err; | |
1299 | } | |
ff764963 | 1300 | |
91694772 CY |
1301 | phy_debugfs_root = debugfs_create_dir("phy", NULL); |
1302 | ||
ff764963 KVA |
1303 | return 0; |
1304 | } | |
cc013c28 | 1305 | device_initcall(phy_core_init); |
91694772 CY |
1306 | |
1307 | static void __exit phy_core_exit(void) | |
1308 | { | |
1309 | debugfs_remove_recursive(phy_debugfs_root); | |
db704bf6 | 1310 | class_unregister(&phy_class); |
91694772 CY |
1311 | } |
1312 | module_exit(phy_core_exit); |