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