blob: 04fc84f2b289907243113e7e0d697cd2411b4e52 [file] [log] [blame]
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +05301/*
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>
Roger Quadros3be88122014-07-04 12:55:45 +030024#include <linux/regulator/consumer.h>
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053025
26static struct class *phy_class;
27static DEFINE_MUTEX(phy_provider_mutex);
28static LIST_HEAD(phy_provider_list);
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +020029static LIST_HEAD(phys);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053030static DEFINE_IDA(phy_ida);
31
32static void devm_phy_release(struct device *dev, void *res)
33{
34 struct phy *phy = *(struct phy **)res;
35
36 phy_put(phy);
37}
38
39static void devm_phy_provider_release(struct device *dev, void *res)
40{
41 struct phy_provider *phy_provider = *(struct phy_provider **)res;
42
43 of_phy_provider_unregister(phy_provider);
44}
45
46static void devm_phy_consume(struct device *dev, void *res)
47{
48 struct phy *phy = *(struct phy **)res;
49
50 phy_destroy(phy);
51}
52
53static int devm_phy_match(struct device *dev, void *res, void *match_data)
54{
Thierry Reding2f1bce42015-02-25 16:16:29 +010055 struct phy **phy = res;
56
57 return *phy == match_data;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053058}
59
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +020060/**
61 * phy_create_lookup() - allocate and register PHY/device association
62 * @phy: the phy of the association
63 * @con_id: connection ID string on device
64 * @dev_id: the device of the association
65 *
66 * Creates and registers phy_lookup entry.
67 */
68int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
69{
70 struct phy_lookup *pl;
71
72 if (!phy || !dev_id || !con_id)
73 return -EINVAL;
74
75 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
76 if (!pl)
77 return -ENOMEM;
78
79 pl->dev_id = dev_id;
80 pl->con_id = con_id;
81 pl->phy = phy;
82
83 mutex_lock(&phy_provider_mutex);
84 list_add_tail(&pl->node, &phys);
85 mutex_unlock(&phy_provider_mutex);
86
87 return 0;
88}
89EXPORT_SYMBOL_GPL(phy_create_lookup);
90
91/**
92 * phy_remove_lookup() - find and remove PHY/device association
93 * @phy: the phy of the association
94 * @con_id: connection ID string on device
95 * @dev_id: the device of the association
96 *
97 * Finds and unregisters phy_lookup entry that was created with
98 * phy_create_lookup().
99 */
100void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
101{
102 struct phy_lookup *pl;
103
104 if (!phy || !dev_id || !con_id)
105 return;
106
107 mutex_lock(&phy_provider_mutex);
108 list_for_each_entry(pl, &phys, node)
109 if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
110 !strcmp(pl->con_id, con_id)) {
111 list_del(&pl->node);
112 kfree(pl);
113 break;
114 }
115 mutex_unlock(&phy_provider_mutex);
116}
117EXPORT_SYMBOL_GPL(phy_remove_lookup);
118
119static struct phy *phy_find(struct device *dev, const char *con_id)
120{
121 const char *dev_id = dev_name(dev);
122 struct phy_lookup *p, *pl = NULL;
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +0200123
124 mutex_lock(&phy_provider_mutex);
125 list_for_each_entry(p, &phys, node)
126 if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
127 pl = p;
128 break;
129 }
130 mutex_unlock(&phy_provider_mutex);
131
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200132 return pl ? pl->phy : ERR_PTR(-ENODEV);
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +0200133}
134
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530135static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
136{
137 struct phy_provider *phy_provider;
Kishon Vijay Abraham I2a4c3702014-07-14 15:55:01 +0530138 struct device_node *child;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530139
140 list_for_each_entry(phy_provider, &phy_provider_list, list) {
141 if (phy_provider->dev->of_node == node)
142 return phy_provider;
Kishon Vijay Abraham I2a4c3702014-07-14 15:55:01 +0530143
144 for_each_child_of_node(phy_provider->dev->of_node, child)
145 if (child == node)
146 return phy_provider;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530147 }
148
149 return ERR_PTR(-EPROBE_DEFER);
150}
151
152int phy_pm_runtime_get(struct phy *phy)
153{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600154 int ret;
155
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530156 if (!pm_runtime_enabled(&phy->dev))
157 return -ENOTSUPP;
158
Felipe Balbicedb7f82013-12-20 15:00:48 -0600159 ret = pm_runtime_get(&phy->dev);
160 if (ret < 0 && ret != -EINPROGRESS)
161 pm_runtime_put_noidle(&phy->dev);
162
163 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530164}
165EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
166
167int phy_pm_runtime_get_sync(struct phy *phy)
168{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600169 int ret;
170
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530171 if (!pm_runtime_enabled(&phy->dev))
172 return -ENOTSUPP;
173
Felipe Balbicedb7f82013-12-20 15:00:48 -0600174 ret = pm_runtime_get_sync(&phy->dev);
175 if (ret < 0)
176 pm_runtime_put_sync(&phy->dev);
177
178 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530179}
180EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
181
182int phy_pm_runtime_put(struct phy *phy)
183{
184 if (!pm_runtime_enabled(&phy->dev))
185 return -ENOTSUPP;
186
187 return pm_runtime_put(&phy->dev);
188}
189EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
190
191int phy_pm_runtime_put_sync(struct phy *phy)
192{
193 if (!pm_runtime_enabled(&phy->dev))
194 return -ENOTSUPP;
195
196 return pm_runtime_put_sync(&phy->dev);
197}
198EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
199
200void phy_pm_runtime_allow(struct phy *phy)
201{
202 if (!pm_runtime_enabled(&phy->dev))
203 return;
204
205 pm_runtime_allow(&phy->dev);
206}
207EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
208
209void phy_pm_runtime_forbid(struct phy *phy)
210{
211 if (!pm_runtime_enabled(&phy->dev))
212 return;
213
214 pm_runtime_forbid(&phy->dev);
215}
216EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
217
218int phy_init(struct phy *phy)
219{
220 int ret;
221
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100222 if (!phy)
223 return 0;
224
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530225 ret = phy_pm_runtime_get_sync(phy);
226 if (ret < 0 && ret != -ENOTSUPP)
227 return ret;
228
229 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530230 if (phy->init_count == 0 && phy->ops->init) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530231 ret = phy->ops->init(phy);
232 if (ret < 0) {
233 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
234 goto out;
235 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530236 } else {
237 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530238 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530239 ++phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530240
241out:
242 mutex_unlock(&phy->mutex);
243 phy_pm_runtime_put(phy);
244 return ret;
245}
246EXPORT_SYMBOL_GPL(phy_init);
247
248int phy_exit(struct phy *phy)
249{
250 int ret;
251
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100252 if (!phy)
253 return 0;
254
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530255 ret = phy_pm_runtime_get_sync(phy);
256 if (ret < 0 && ret != -ENOTSUPP)
257 return ret;
258
259 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530260 if (phy->init_count == 1 && phy->ops->exit) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530261 ret = phy->ops->exit(phy);
262 if (ret < 0) {
263 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
264 goto out;
265 }
266 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530267 --phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530268
269out:
270 mutex_unlock(&phy->mutex);
271 phy_pm_runtime_put(phy);
272 return ret;
273}
274EXPORT_SYMBOL_GPL(phy_exit);
275
276int phy_power_on(struct phy *phy)
277{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530278 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530279
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100280 if (!phy)
281 return 0;
282
Roger Quadros3be88122014-07-04 12:55:45 +0300283 if (phy->pwr) {
284 ret = regulator_enable(phy->pwr);
285 if (ret)
286 return ret;
287 }
288
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530289 ret = phy_pm_runtime_get_sync(phy);
290 if (ret < 0 && ret != -ENOTSUPP)
291 return ret;
292
293 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530294 if (phy->power_count == 0 && phy->ops->power_on) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530295 ret = phy->ops->power_on(phy);
296 if (ret < 0) {
297 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
298 goto out;
299 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530300 } else {
301 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530302 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530303 ++phy->power_count;
304 mutex_unlock(&phy->mutex);
305 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530306
307out:
308 mutex_unlock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530309 phy_pm_runtime_put_sync(phy);
Roger Quadros3be88122014-07-04 12:55:45 +0300310 if (phy->pwr)
311 regulator_disable(phy->pwr);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530312
313 return ret;
314}
315EXPORT_SYMBOL_GPL(phy_power_on);
316
317int phy_power_off(struct phy *phy)
318{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530319 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530320
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100321 if (!phy)
322 return 0;
323
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530324 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530325 if (phy->power_count == 1 && phy->ops->power_off) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530326 ret = phy->ops->power_off(phy);
327 if (ret < 0) {
328 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530329 mutex_unlock(&phy->mutex);
330 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530331 }
332 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530333 --phy->power_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530334 mutex_unlock(&phy->mutex);
335 phy_pm_runtime_put(phy);
336
Roger Quadros3be88122014-07-04 12:55:45 +0300337 if (phy->pwr)
338 regulator_disable(phy->pwr);
339
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530340 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530341}
342EXPORT_SYMBOL_GPL(phy_power_off);
343
344/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100345 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
346 * @np: device_node for which to get the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530347 * @index: the index of the phy
348 *
349 * Returns the phy associated with the given phandle value,
350 * after getting a refcount to it or -ENODEV if there is no such phy or
351 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
352 * not yet loaded. This function uses of_xlate call back function provided
353 * while registering the phy_provider to find the phy instance.
354 */
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100355static struct phy *_of_phy_get(struct device_node *np, int index)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530356{
357 int ret;
358 struct phy_provider *phy_provider;
359 struct phy *phy = NULL;
360 struct of_phandle_args args;
361
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100362 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530363 index, &args);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100364 if (ret)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530365 return ERR_PTR(-ENODEV);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530366
367 mutex_lock(&phy_provider_mutex);
368 phy_provider = of_phy_provider_lookup(args.np);
369 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
370 phy = ERR_PTR(-EPROBE_DEFER);
371 goto err0;
372 }
373
374 phy = phy_provider->of_xlate(phy_provider->dev, &args);
375 module_put(phy_provider->owner);
376
377err0:
378 mutex_unlock(&phy_provider_mutex);
379 of_node_put(args.np);
380
381 return phy;
382}
383
384/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100385 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
386 * @np: device_node for which to get the phy
387 * @con_id: name of the phy from device's point of view
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 */
393struct phy *of_phy_get(struct device_node *np, const char *con_id)
394{
395 struct phy *phy = NULL;
396 int index = 0;
397
398 if (con_id)
399 index = of_property_match_string(np, "phy-names", con_id);
400
401 phy = _of_phy_get(np, index);
402 if (IS_ERR(phy))
403 return phy;
404
405 if (!try_module_get(phy->ops->owner))
406 return ERR_PTR(-EPROBE_DEFER);
407
408 get_device(&phy->dev);
409
410 return phy;
411}
412EXPORT_SYMBOL_GPL(of_phy_get);
413
414/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530415 * phy_put() - release the PHY
416 * @phy: the phy returned by phy_get()
417 *
418 * Releases a refcount the caller received from phy_get().
419 */
420void phy_put(struct phy *phy)
421{
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100422 if (!phy || IS_ERR(phy))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530423 return;
424
425 module_put(phy->ops->owner);
426 put_device(&phy->dev);
427}
428EXPORT_SYMBOL_GPL(phy_put);
429
430/**
431 * devm_phy_put() - release the PHY
432 * @dev: device that wants to release this phy
433 * @phy: the phy returned by devm_phy_get()
434 *
435 * destroys the devres associated with this phy and invokes phy_put
436 * to release the phy.
437 */
438void devm_phy_put(struct device *dev, struct phy *phy)
439{
440 int r;
441
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100442 if (!phy)
443 return;
444
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530445 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
446 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
447}
448EXPORT_SYMBOL_GPL(devm_phy_put);
449
450/**
451 * of_phy_simple_xlate() - returns the phy instance from phy provider
452 * @dev: the PHY provider device
453 * @args: of_phandle_args (not used here)
454 *
455 * Intended to be used by phy provider for the common case where #phy-cells is
456 * 0. For other cases where #phy-cells is greater than '0', the phy provider
457 * should provide a custom of_xlate function that reads the *args* and returns
458 * the appropriate phy.
459 */
460struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
461 *args)
462{
463 struct phy *phy;
464 struct class_dev_iter iter;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530465
466 class_dev_iter_init(&iter, phy_class, NULL, NULL);
467 while ((dev = class_dev_iter_next(&iter))) {
468 phy = to_phy(dev);
Kishon Vijay Abraham I491e0492014-10-31 14:04:20 +0530469 if (args->np != phy->dev.of_node)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530470 continue;
471
472 class_dev_iter_exit(&iter);
473 return phy;
474 }
475
476 class_dev_iter_exit(&iter);
477 return ERR_PTR(-ENODEV);
478}
479EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
480
481/**
482 * phy_get() - lookup and obtain a reference to a phy.
483 * @dev: device that requests this phy
484 * @string: the phy name as given in the dt data or the name of the controller
485 * port for non-dt case
486 *
487 * Returns the phy driver, after getting a refcount to it; or
488 * -ENODEV if there is no such phy. The caller is responsible for
489 * calling phy_put() to release that count.
490 */
491struct phy *phy_get(struct device *dev, const char *string)
492{
493 int index = 0;
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530494 struct phy *phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530495
496 if (string == NULL) {
497 dev_WARN(dev, "missing string\n");
498 return ERR_PTR(-EINVAL);
499 }
500
501 if (dev->of_node) {
502 index = of_property_match_string(dev->of_node, "phy-names",
503 string);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100504 phy = _of_phy_get(dev->of_node, index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530505 } else {
Heikki Krogerusb7bc15b92014-11-19 17:28:18 +0200506 phy = phy_find(dev, string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530507 }
Hans de Goedef40037f2014-02-17 14:29:22 +0530508 if (IS_ERR(phy))
509 return phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530510
511 if (!try_module_get(phy->ops->owner))
512 return ERR_PTR(-EPROBE_DEFER);
513
514 get_device(&phy->dev);
515
516 return phy;
517}
518EXPORT_SYMBOL_GPL(phy_get);
519
520/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100521 * phy_optional_get() - lookup and obtain a reference to an optional phy.
522 * @dev: device that requests this phy
523 * @string: the phy name as given in the dt data or the name of the controller
524 * port for non-dt case
525 *
526 * Returns the phy driver, after getting a refcount to it; or
527 * NULL if there is no such phy. The caller is responsible for
528 * calling phy_put() to release that count.
529 */
530struct phy *phy_optional_get(struct device *dev, const char *string)
531{
532 struct phy *phy = phy_get(dev, string);
533
534 if (PTR_ERR(phy) == -ENODEV)
535 phy = NULL;
536
537 return phy;
538}
539EXPORT_SYMBOL_GPL(phy_optional_get);
540
541/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530542 * devm_phy_get() - lookup and obtain a reference to a phy.
543 * @dev: device that requests this phy
544 * @string: the phy name as given in the dt data or phy device name
545 * for non-dt case
546 *
547 * Gets the phy using phy_get(), and associates a device with it using
548 * devres. On driver detach, release function is invoked on the devres data,
549 * then, devres data is freed.
550 */
551struct phy *devm_phy_get(struct device *dev, const char *string)
552{
553 struct phy **ptr, *phy;
554
555 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
556 if (!ptr)
557 return ERR_PTR(-ENOMEM);
558
559 phy = phy_get(dev, string);
560 if (!IS_ERR(phy)) {
561 *ptr = phy;
562 devres_add(dev, ptr);
563 } else {
564 devres_free(ptr);
565 }
566
567 return phy;
568}
569EXPORT_SYMBOL_GPL(devm_phy_get);
570
571/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100572 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
573 * @dev: device that requests this phy
574 * @string: the phy name as given in the dt data or phy device name
575 * for non-dt case
576 *
577 * Gets the phy using phy_get(), and associates a device with it using
578 * devres. On driver detach, release function is invoked on the devres
579 * data, then, devres data is freed. This differs to devm_phy_get() in
580 * that if the phy does not exist, it is not considered an error and
581 * -ENODEV will not be returned. Instead the NULL phy is returned,
582 * which can be passed to all other phy consumer calls.
583 */
584struct phy *devm_phy_optional_get(struct device *dev, const char *string)
585{
586 struct phy *phy = devm_phy_get(dev, string);
587
588 if (PTR_ERR(phy) == -ENODEV)
589 phy = NULL;
590
591 return phy;
592}
593EXPORT_SYMBOL_GPL(devm_phy_optional_get);
594
595/**
Kamil Debskib5d682f2014-03-06 12:16:47 +0100596 * devm_of_phy_get() - lookup and obtain a reference to a phy.
597 * @dev: device that requests this phy
598 * @np: node containing the phy
599 * @con_id: name of the phy from device's point of view
600 *
601 * Gets the phy using of_phy_get(), and associates a device with it using
602 * devres. On driver detach, release function is invoked on the devres data,
603 * then, devres data is freed.
604 */
605struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
606 const char *con_id)
607{
608 struct phy **ptr, *phy;
609
610 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
611 if (!ptr)
612 return ERR_PTR(-ENOMEM);
613
614 phy = of_phy_get(np, con_id);
615 if (!IS_ERR(phy)) {
616 *ptr = phy;
617 devres_add(dev, ptr);
618 } else {
619 devres_free(ptr);
620 }
621
622 return phy;
623}
624EXPORT_SYMBOL_GPL(devm_of_phy_get);
625
626/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530627 * phy_create() - create a new phy
628 * @dev: device that is creating the new phy
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530629 * @node: device node of the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530630 * @ops: function pointers for performing phy operations
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530631 *
632 * Called to create a phy using phy framework.
633 */
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530634struct phy *phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200635 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530636{
637 int ret;
638 int id;
639 struct phy *phy;
640
Dan Carpenter52797d22013-12-06 17:51:19 +0530641 if (WARN_ON(!dev))
642 return ERR_PTR(-EINVAL);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530643
644 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Dan Carpenter52797d22013-12-06 17:51:19 +0530645 if (!phy)
646 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530647
648 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
649 if (id < 0) {
650 dev_err(dev, "unable to get id\n");
651 ret = id;
Dan Carpenter52797d22013-12-06 17:51:19 +0530652 goto free_phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530653 }
654
Roger Quadros3be88122014-07-04 12:55:45 +0300655 /* phy-supply */
656 phy->pwr = regulator_get_optional(dev, "phy");
657 if (IS_ERR(phy->pwr)) {
658 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
659 ret = -EPROBE_DEFER;
660 goto free_ida;
661 }
662 phy->pwr = NULL;
663 }
664
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530665 device_initialize(&phy->dev);
666 mutex_init(&phy->mutex);
667
668 phy->dev.class = phy_class;
669 phy->dev.parent = dev;
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530670 phy->dev.of_node = node ?: dev->of_node;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530671 phy->id = id;
672 phy->ops = ops;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530673
674 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
675 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530676 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530677
678 ret = device_add(&phy->dev);
679 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530680 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530681
682 if (pm_runtime_enabled(dev)) {
683 pm_runtime_enable(&phy->dev);
684 pm_runtime_no_callbacks(&phy->dev);
685 }
686
687 return phy;
688
Dan Carpenter52797d22013-12-06 17:51:19 +0530689put_dev:
Roger Quadrose73b49f2014-07-10 11:55:02 +0530690 put_device(&phy->dev); /* calls phy_release() which frees resources */
691 return ERR_PTR(ret);
692
Roger Quadros3be88122014-07-04 12:55:45 +0300693free_ida:
694 ida_simple_remove(&phy_ida, phy->id);
695
Dan Carpenter52797d22013-12-06 17:51:19 +0530696free_phy:
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530697 kfree(phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530698 return ERR_PTR(ret);
699}
700EXPORT_SYMBOL_GPL(phy_create);
701
702/**
703 * devm_phy_create() - create a new phy
704 * @dev: device that is creating the new phy
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530705 * @node: device node of the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530706 * @ops: function pointers for performing phy operations
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530707 *
708 * Creates a new PHY device adding it to the PHY class.
709 * While at that, it also associates the device with the phy using devres.
710 * On driver detach, release function is invoked on the devres data,
711 * then, devres data is freed.
712 */
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530713struct phy *devm_phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200714 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530715{
716 struct phy **ptr, *phy;
717
718 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
719 if (!ptr)
720 return ERR_PTR(-ENOMEM);
721
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200722 phy = phy_create(dev, node, ops);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530723 if (!IS_ERR(phy)) {
724 *ptr = phy;
725 devres_add(dev, ptr);
726 } else {
727 devres_free(ptr);
728 }
729
730 return phy;
731}
732EXPORT_SYMBOL_GPL(devm_phy_create);
733
734/**
735 * phy_destroy() - destroy the phy
736 * @phy: the phy to be destroyed
737 *
738 * Called to destroy the phy.
739 */
740void phy_destroy(struct phy *phy)
741{
742 pm_runtime_disable(&phy->dev);
743 device_unregister(&phy->dev);
744}
745EXPORT_SYMBOL_GPL(phy_destroy);
746
747/**
748 * devm_phy_destroy() - destroy the PHY
749 * @dev: device that wants to release this phy
750 * @phy: the phy returned by devm_phy_get()
751 *
752 * destroys the devres associated with this phy and invokes phy_destroy
753 * to destroy the phy.
754 */
755void devm_phy_destroy(struct device *dev, struct phy *phy)
756{
757 int r;
758
759 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
760 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
761}
762EXPORT_SYMBOL_GPL(devm_phy_destroy);
763
764/**
765 * __of_phy_provider_register() - create/register phy provider with the framework
766 * @dev: struct device of the phy provider
767 * @owner: the module owner containing of_xlate
768 * @of_xlate: function pointer to obtain phy instance from phy provider
769 *
770 * Creates struct phy_provider from dev and of_xlate function pointer.
771 * This is used in the case of dt boot for finding the phy instance from
772 * phy provider.
773 */
774struct phy_provider *__of_phy_provider_register(struct device *dev,
775 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
776 struct of_phandle_args *args))
777{
778 struct phy_provider *phy_provider;
779
780 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
781 if (!phy_provider)
782 return ERR_PTR(-ENOMEM);
783
784 phy_provider->dev = dev;
785 phy_provider->owner = owner;
786 phy_provider->of_xlate = of_xlate;
787
788 mutex_lock(&phy_provider_mutex);
789 list_add_tail(&phy_provider->list, &phy_provider_list);
790 mutex_unlock(&phy_provider_mutex);
791
792 return phy_provider;
793}
794EXPORT_SYMBOL_GPL(__of_phy_provider_register);
795
796/**
797 * __devm_of_phy_provider_register() - create/register phy provider with the
798 * framework
799 * @dev: struct device of the phy provider
800 * @owner: the module owner containing of_xlate
801 * @of_xlate: function pointer to obtain phy instance from phy provider
802 *
803 * Creates struct phy_provider from dev and of_xlate function pointer.
804 * This is used in the case of dt boot for finding the phy instance from
805 * phy provider. While at that, it also associates the device with the
806 * phy provider using devres. On driver detach, release function is invoked
807 * on the devres data, then, devres data is freed.
808 */
809struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
810 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
811 struct of_phandle_args *args))
812{
813 struct phy_provider **ptr, *phy_provider;
814
815 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
816 if (!ptr)
817 return ERR_PTR(-ENOMEM);
818
819 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
820 if (!IS_ERR(phy_provider)) {
821 *ptr = phy_provider;
822 devres_add(dev, ptr);
823 } else {
824 devres_free(ptr);
825 }
826
827 return phy_provider;
828}
829EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
830
831/**
832 * of_phy_provider_unregister() - unregister phy provider from the framework
833 * @phy_provider: phy provider returned by of_phy_provider_register()
834 *
835 * Removes the phy_provider created using of_phy_provider_register().
836 */
837void of_phy_provider_unregister(struct phy_provider *phy_provider)
838{
839 if (IS_ERR(phy_provider))
840 return;
841
842 mutex_lock(&phy_provider_mutex);
843 list_del(&phy_provider->list);
844 kfree(phy_provider);
845 mutex_unlock(&phy_provider_mutex);
846}
847EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
848
849/**
850 * devm_of_phy_provider_unregister() - remove phy provider from the framework
851 * @dev: struct device of the phy provider
852 *
853 * destroys the devres associated with this phy provider and invokes
854 * of_phy_provider_unregister to unregister the phy provider.
855 */
856void devm_of_phy_provider_unregister(struct device *dev,
857 struct phy_provider *phy_provider) {
858 int r;
859
860 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
861 phy_provider);
862 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
863}
864EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
865
866/**
867 * phy_release() - release the phy
868 * @dev: the dev member within phy
869 *
870 * When the last reference to the device is removed, it is called
871 * from the embedded kobject as release method.
872 */
873static void phy_release(struct device *dev)
874{
875 struct phy *phy;
876
877 phy = to_phy(dev);
878 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
Roger Quadros3be88122014-07-04 12:55:45 +0300879 regulator_put(phy->pwr);
Roger Quadrose73b49f2014-07-10 11:55:02 +0530880 ida_simple_remove(&phy_ida, phy->id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530881 kfree(phy);
882}
883
884static int __init phy_core_init(void)
885{
886 phy_class = class_create(THIS_MODULE, "phy");
887 if (IS_ERR(phy_class)) {
888 pr_err("failed to create phy class --> %ld\n",
889 PTR_ERR(phy_class));
890 return PTR_ERR(phy_class);
891 }
892
893 phy_class->dev_release = phy_release;
894
895 return 0;
896}
897module_init(phy_core_init);
898
899static void __exit phy_core_exit(void)
900{
901 class_destroy(phy_class);
902}
903module_exit(phy_core_exit);
904
905MODULE_DESCRIPTION("Generic PHY Framework");
906MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
907MODULE_LICENSE("GPL v2");