blob: 7c1b0e14a235a9b1d0d8fa397e2325bbed2c2d07 [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>
24
25static struct class *phy_class;
26static DEFINE_MUTEX(phy_provider_mutex);
27static LIST_HEAD(phy_provider_list);
28static DEFINE_IDA(phy_ida);
29
30static void devm_phy_release(struct device *dev, void *res)
31{
32 struct phy *phy = *(struct phy **)res;
33
34 phy_put(phy);
35}
36
37static void devm_phy_provider_release(struct device *dev, void *res)
38{
39 struct phy_provider *phy_provider = *(struct phy_provider **)res;
40
41 of_phy_provider_unregister(phy_provider);
42}
43
44static void devm_phy_consume(struct device *dev, void *res)
45{
46 struct phy *phy = *(struct phy **)res;
47
48 phy_destroy(phy);
49}
50
51static int devm_phy_match(struct device *dev, void *res, void *match_data)
52{
53 return res == match_data;
54}
55
56static struct phy *phy_lookup(struct device *device, const char *port)
57{
58 unsigned int count;
59 struct phy *phy;
60 struct device *dev;
61 struct phy_consumer *consumers;
62 struct class_dev_iter iter;
63
64 class_dev_iter_init(&iter, phy_class, NULL, NULL);
65 while ((dev = class_dev_iter_next(&iter))) {
66 phy = to_phy(dev);
67 count = phy->init_data->num_consumers;
68 consumers = phy->init_data->consumers;
69 while (count--) {
70 if (!strcmp(consumers->dev_name, dev_name(device)) &&
71 !strcmp(consumers->port, port)) {
72 class_dev_iter_exit(&iter);
73 return phy;
74 }
75 consumers++;
76 }
77 }
78
79 class_dev_iter_exit(&iter);
80 return ERR_PTR(-ENODEV);
81}
82
83static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
84{
85 struct phy_provider *phy_provider;
86
87 list_for_each_entry(phy_provider, &phy_provider_list, list) {
88 if (phy_provider->dev->of_node == node)
89 return phy_provider;
90 }
91
92 return ERR_PTR(-EPROBE_DEFER);
93}
94
95int phy_pm_runtime_get(struct phy *phy)
96{
Felipe Balbicedb7f82013-12-20 15:00:48 -060097 int ret;
98
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053099 if (!pm_runtime_enabled(&phy->dev))
100 return -ENOTSUPP;
101
Felipe Balbicedb7f82013-12-20 15:00:48 -0600102 ret = pm_runtime_get(&phy->dev);
103 if (ret < 0 && ret != -EINPROGRESS)
104 pm_runtime_put_noidle(&phy->dev);
105
106 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530107}
108EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
109
110int phy_pm_runtime_get_sync(struct phy *phy)
111{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600112 int ret;
113
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530114 if (!pm_runtime_enabled(&phy->dev))
115 return -ENOTSUPP;
116
Felipe Balbicedb7f82013-12-20 15:00:48 -0600117 ret = pm_runtime_get_sync(&phy->dev);
118 if (ret < 0)
119 pm_runtime_put_sync(&phy->dev);
120
121 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530122}
123EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
124
125int phy_pm_runtime_put(struct phy *phy)
126{
127 if (!pm_runtime_enabled(&phy->dev))
128 return -ENOTSUPP;
129
130 return pm_runtime_put(&phy->dev);
131}
132EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
133
134int phy_pm_runtime_put_sync(struct phy *phy)
135{
136 if (!pm_runtime_enabled(&phy->dev))
137 return -ENOTSUPP;
138
139 return pm_runtime_put_sync(&phy->dev);
140}
141EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
142
143void phy_pm_runtime_allow(struct phy *phy)
144{
145 if (!pm_runtime_enabled(&phy->dev))
146 return;
147
148 pm_runtime_allow(&phy->dev);
149}
150EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
151
152void phy_pm_runtime_forbid(struct phy *phy)
153{
154 if (!pm_runtime_enabled(&phy->dev))
155 return;
156
157 pm_runtime_forbid(&phy->dev);
158}
159EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
160
161int phy_init(struct phy *phy)
162{
163 int ret;
164
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100165 if (!phy)
166 return 0;
167
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530168 ret = phy_pm_runtime_get_sync(phy);
169 if (ret < 0 && ret != -ENOTSUPP)
170 return ret;
171
172 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530173 if (phy->init_count == 0 && phy->ops->init) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530174 ret = phy->ops->init(phy);
175 if (ret < 0) {
176 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
177 goto out;
178 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530179 } else {
180 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530181 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530182 ++phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530183
184out:
185 mutex_unlock(&phy->mutex);
186 phy_pm_runtime_put(phy);
187 return ret;
188}
189EXPORT_SYMBOL_GPL(phy_init);
190
191int phy_exit(struct phy *phy)
192{
193 int ret;
194
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100195 if (!phy)
196 return 0;
197
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530198 ret = phy_pm_runtime_get_sync(phy);
199 if (ret < 0 && ret != -ENOTSUPP)
200 return ret;
201
202 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530203 if (phy->init_count == 1 && phy->ops->exit) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530204 ret = phy->ops->exit(phy);
205 if (ret < 0) {
206 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
207 goto out;
208 }
209 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530210 --phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530211
212out:
213 mutex_unlock(&phy->mutex);
214 phy_pm_runtime_put(phy);
215 return ret;
216}
217EXPORT_SYMBOL_GPL(phy_exit);
218
219int phy_power_on(struct phy *phy)
220{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530221 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530222
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100223 if (!phy)
224 return 0;
225
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530226 ret = phy_pm_runtime_get_sync(phy);
227 if (ret < 0 && ret != -ENOTSUPP)
228 return ret;
229
230 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530231 if (phy->power_count == 0 && phy->ops->power_on) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530232 ret = phy->ops->power_on(phy);
233 if (ret < 0) {
234 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
235 goto out;
236 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530237 } else {
238 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530239 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530240 ++phy->power_count;
241 mutex_unlock(&phy->mutex);
242 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530243
244out:
245 mutex_unlock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530246 phy_pm_runtime_put_sync(phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530247
248 return ret;
249}
250EXPORT_SYMBOL_GPL(phy_power_on);
251
252int phy_power_off(struct phy *phy)
253{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530254 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530255
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100256 if (!phy)
257 return 0;
258
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530259 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530260 if (phy->power_count == 1 && phy->ops->power_off) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530261 ret = phy->ops->power_off(phy);
262 if (ret < 0) {
263 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530264 mutex_unlock(&phy->mutex);
265 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530266 }
267 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530268 --phy->power_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530269 mutex_unlock(&phy->mutex);
270 phy_pm_runtime_put(phy);
271
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530272 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530273}
274EXPORT_SYMBOL_GPL(phy_power_off);
275
276/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100277 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
278 * @np: device_node for which to get the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530279 * @index: the index of the phy
280 *
281 * Returns the phy associated with the given phandle value,
282 * after getting a refcount to it or -ENODEV if there is no such phy or
283 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
284 * not yet loaded. This function uses of_xlate call back function provided
285 * while registering the phy_provider to find the phy instance.
286 */
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100287static struct phy *_of_phy_get(struct device_node *np, int index)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530288{
289 int ret;
290 struct phy_provider *phy_provider;
291 struct phy *phy = NULL;
292 struct of_phandle_args args;
293
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100294 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530295 index, &args);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100296 if (ret)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530297 return ERR_PTR(-ENODEV);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530298
299 mutex_lock(&phy_provider_mutex);
300 phy_provider = of_phy_provider_lookup(args.np);
301 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
302 phy = ERR_PTR(-EPROBE_DEFER);
303 goto err0;
304 }
305
306 phy = phy_provider->of_xlate(phy_provider->dev, &args);
307 module_put(phy_provider->owner);
308
309err0:
310 mutex_unlock(&phy_provider_mutex);
311 of_node_put(args.np);
312
313 return phy;
314}
315
316/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100317 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
318 * @np: device_node for which to get the phy
319 * @con_id: name of the phy from device's point of view
320 *
321 * Returns the phy driver, after getting a refcount to it; or
322 * -ENODEV if there is no such phy. The caller is responsible for
323 * calling phy_put() to release that count.
324 */
325struct phy *of_phy_get(struct device_node *np, const char *con_id)
326{
327 struct phy *phy = NULL;
328 int index = 0;
329
330 if (con_id)
331 index = of_property_match_string(np, "phy-names", con_id);
332
333 phy = _of_phy_get(np, index);
334 if (IS_ERR(phy))
335 return phy;
336
337 if (!try_module_get(phy->ops->owner))
338 return ERR_PTR(-EPROBE_DEFER);
339
340 get_device(&phy->dev);
341
342 return phy;
343}
344EXPORT_SYMBOL_GPL(of_phy_get);
345
346/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530347 * phy_put() - release the PHY
348 * @phy: the phy returned by phy_get()
349 *
350 * Releases a refcount the caller received from phy_get().
351 */
352void phy_put(struct phy *phy)
353{
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100354 if (!phy || IS_ERR(phy))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530355 return;
356
357 module_put(phy->ops->owner);
358 put_device(&phy->dev);
359}
360EXPORT_SYMBOL_GPL(phy_put);
361
362/**
363 * devm_phy_put() - release the PHY
364 * @dev: device that wants to release this phy
365 * @phy: the phy returned by devm_phy_get()
366 *
367 * destroys the devres associated with this phy and invokes phy_put
368 * to release the phy.
369 */
370void devm_phy_put(struct device *dev, struct phy *phy)
371{
372 int r;
373
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100374 if (!phy)
375 return;
376
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530377 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
378 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
379}
380EXPORT_SYMBOL_GPL(devm_phy_put);
381
382/**
383 * of_phy_simple_xlate() - returns the phy instance from phy provider
384 * @dev: the PHY provider device
385 * @args: of_phandle_args (not used here)
386 *
387 * Intended to be used by phy provider for the common case where #phy-cells is
388 * 0. For other cases where #phy-cells is greater than '0', the phy provider
389 * should provide a custom of_xlate function that reads the *args* and returns
390 * the appropriate phy.
391 */
392struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
393 *args)
394{
395 struct phy *phy;
396 struct class_dev_iter iter;
397 struct device_node *node = dev->of_node;
398
399 class_dev_iter_init(&iter, phy_class, NULL, NULL);
400 while ((dev = class_dev_iter_next(&iter))) {
401 phy = to_phy(dev);
402 if (node != phy->dev.of_node)
403 continue;
404
405 class_dev_iter_exit(&iter);
406 return phy;
407 }
408
409 class_dev_iter_exit(&iter);
410 return ERR_PTR(-ENODEV);
411}
412EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
413
414/**
415 * phy_get() - lookup and obtain a reference to a phy.
416 * @dev: device that requests this phy
417 * @string: the phy name as given in the dt data or the name of the controller
418 * port for non-dt case
419 *
420 * Returns the phy driver, after getting a refcount to it; or
421 * -ENODEV if there is no such phy. The caller is responsible for
422 * calling phy_put() to release that count.
423 */
424struct phy *phy_get(struct device *dev, const char *string)
425{
426 int index = 0;
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530427 struct phy *phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530428
429 if (string == NULL) {
430 dev_WARN(dev, "missing string\n");
431 return ERR_PTR(-EINVAL);
432 }
433
434 if (dev->of_node) {
435 index = of_property_match_string(dev->of_node, "phy-names",
436 string);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100437 phy = _of_phy_get(dev->of_node, index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530438 } else {
439 phy = phy_lookup(dev, string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530440 }
Hans de Goedef40037f2014-02-17 14:29:22 +0530441 if (IS_ERR(phy))
442 return phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530443
444 if (!try_module_get(phy->ops->owner))
445 return ERR_PTR(-EPROBE_DEFER);
446
447 get_device(&phy->dev);
448
449 return phy;
450}
451EXPORT_SYMBOL_GPL(phy_get);
452
453/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100454 * phy_optional_get() - lookup and obtain a reference to an optional phy.
455 * @dev: device that requests this phy
456 * @string: the phy name as given in the dt data or the name of the controller
457 * port for non-dt case
458 *
459 * Returns the phy driver, after getting a refcount to it; or
460 * NULL if there is no such phy. The caller is responsible for
461 * calling phy_put() to release that count.
462 */
463struct phy *phy_optional_get(struct device *dev, const char *string)
464{
465 struct phy *phy = phy_get(dev, string);
466
467 if (PTR_ERR(phy) == -ENODEV)
468 phy = NULL;
469
470 return phy;
471}
472EXPORT_SYMBOL_GPL(phy_optional_get);
473
474/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530475 * devm_phy_get() - lookup and obtain a reference to a phy.
476 * @dev: device that requests this phy
477 * @string: the phy name as given in the dt data or phy device name
478 * for non-dt case
479 *
480 * Gets the phy using phy_get(), and associates a device with it using
481 * devres. On driver detach, release function is invoked on the devres data,
482 * then, devres data is freed.
483 */
484struct phy *devm_phy_get(struct device *dev, const char *string)
485{
486 struct phy **ptr, *phy;
487
488 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
489 if (!ptr)
490 return ERR_PTR(-ENOMEM);
491
492 phy = phy_get(dev, string);
493 if (!IS_ERR(phy)) {
494 *ptr = phy;
495 devres_add(dev, ptr);
496 } else {
497 devres_free(ptr);
498 }
499
500 return phy;
501}
502EXPORT_SYMBOL_GPL(devm_phy_get);
503
504/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100505 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
506 * @dev: device that requests this phy
507 * @string: the phy name as given in the dt data or phy device name
508 * for non-dt case
509 *
510 * Gets the phy using phy_get(), and associates a device with it using
511 * devres. On driver detach, release function is invoked on the devres
512 * data, then, devres data is freed. This differs to devm_phy_get() in
513 * that if the phy does not exist, it is not considered an error and
514 * -ENODEV will not be returned. Instead the NULL phy is returned,
515 * which can be passed to all other phy consumer calls.
516 */
517struct phy *devm_phy_optional_get(struct device *dev, const char *string)
518{
519 struct phy *phy = devm_phy_get(dev, string);
520
521 if (PTR_ERR(phy) == -ENODEV)
522 phy = NULL;
523
524 return phy;
525}
526EXPORT_SYMBOL_GPL(devm_phy_optional_get);
527
528/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530529 * phy_create() - create a new phy
530 * @dev: device that is creating the new phy
531 * @ops: function pointers for performing phy operations
532 * @init_data: contains the list of PHY consumers or NULL
533 *
534 * Called to create a phy using phy framework.
535 */
536struct phy *phy_create(struct device *dev, const struct phy_ops *ops,
537 struct phy_init_data *init_data)
538{
539 int ret;
540 int id;
541 struct phy *phy;
542
Dan Carpenter52797d22013-12-06 17:51:19 +0530543 if (WARN_ON(!dev))
544 return ERR_PTR(-EINVAL);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530545
546 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Dan Carpenter52797d22013-12-06 17:51:19 +0530547 if (!phy)
548 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530549
550 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
551 if (id < 0) {
552 dev_err(dev, "unable to get id\n");
553 ret = id;
Dan Carpenter52797d22013-12-06 17:51:19 +0530554 goto free_phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530555 }
556
557 device_initialize(&phy->dev);
558 mutex_init(&phy->mutex);
559
560 phy->dev.class = phy_class;
561 phy->dev.parent = dev;
562 phy->dev.of_node = dev->of_node;
563 phy->id = id;
564 phy->ops = ops;
565 phy->init_data = init_data;
566
567 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
568 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530569 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530570
571 ret = device_add(&phy->dev);
572 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530573 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530574
575 if (pm_runtime_enabled(dev)) {
576 pm_runtime_enable(&phy->dev);
577 pm_runtime_no_callbacks(&phy->dev);
578 }
579
580 return phy;
581
Dan Carpenter52797d22013-12-06 17:51:19 +0530582put_dev:
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530583 put_device(&phy->dev);
Dan Carpenter52797d22013-12-06 17:51:19 +0530584 ida_remove(&phy_ida, phy->id);
585free_phy:
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530586 kfree(phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530587 return ERR_PTR(ret);
588}
589EXPORT_SYMBOL_GPL(phy_create);
590
591/**
592 * devm_phy_create() - create a new phy
593 * @dev: device that is creating the new phy
594 * @ops: function pointers for performing phy operations
595 * @init_data: contains the list of PHY consumers or NULL
596 *
597 * Creates a new PHY device adding it to the PHY class.
598 * While at that, it also associates the device with the phy using devres.
599 * On driver detach, release function is invoked on the devres data,
600 * then, devres data is freed.
601 */
602struct phy *devm_phy_create(struct device *dev, const struct phy_ops *ops,
603 struct phy_init_data *init_data)
604{
605 struct phy **ptr, *phy;
606
607 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
608 if (!ptr)
609 return ERR_PTR(-ENOMEM);
610
611 phy = phy_create(dev, ops, init_data);
612 if (!IS_ERR(phy)) {
613 *ptr = phy;
614 devres_add(dev, ptr);
615 } else {
616 devres_free(ptr);
617 }
618
619 return phy;
620}
621EXPORT_SYMBOL_GPL(devm_phy_create);
622
623/**
624 * phy_destroy() - destroy the phy
625 * @phy: the phy to be destroyed
626 *
627 * Called to destroy the phy.
628 */
629void phy_destroy(struct phy *phy)
630{
631 pm_runtime_disable(&phy->dev);
632 device_unregister(&phy->dev);
633}
634EXPORT_SYMBOL_GPL(phy_destroy);
635
636/**
637 * devm_phy_destroy() - destroy the PHY
638 * @dev: device that wants to release this phy
639 * @phy: the phy returned by devm_phy_get()
640 *
641 * destroys the devres associated with this phy and invokes phy_destroy
642 * to destroy the phy.
643 */
644void devm_phy_destroy(struct device *dev, struct phy *phy)
645{
646 int r;
647
648 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
649 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
650}
651EXPORT_SYMBOL_GPL(devm_phy_destroy);
652
653/**
654 * __of_phy_provider_register() - create/register phy provider with the framework
655 * @dev: struct device of the phy provider
656 * @owner: the module owner containing of_xlate
657 * @of_xlate: function pointer to obtain phy instance from phy provider
658 *
659 * Creates struct phy_provider from dev and of_xlate function pointer.
660 * This is used in the case of dt boot for finding the phy instance from
661 * phy provider.
662 */
663struct phy_provider *__of_phy_provider_register(struct device *dev,
664 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
665 struct of_phandle_args *args))
666{
667 struct phy_provider *phy_provider;
668
669 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
670 if (!phy_provider)
671 return ERR_PTR(-ENOMEM);
672
673 phy_provider->dev = dev;
674 phy_provider->owner = owner;
675 phy_provider->of_xlate = of_xlate;
676
677 mutex_lock(&phy_provider_mutex);
678 list_add_tail(&phy_provider->list, &phy_provider_list);
679 mutex_unlock(&phy_provider_mutex);
680
681 return phy_provider;
682}
683EXPORT_SYMBOL_GPL(__of_phy_provider_register);
684
685/**
686 * __devm_of_phy_provider_register() - create/register phy provider with the
687 * framework
688 * @dev: struct device of the phy provider
689 * @owner: the module owner containing of_xlate
690 * @of_xlate: function pointer to obtain phy instance from phy provider
691 *
692 * Creates struct phy_provider from dev and of_xlate function pointer.
693 * This is used in the case of dt boot for finding the phy instance from
694 * phy provider. While at that, it also associates the device with the
695 * phy provider using devres. On driver detach, release function is invoked
696 * on the devres data, then, devres data is freed.
697 */
698struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
699 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
700 struct of_phandle_args *args))
701{
702 struct phy_provider **ptr, *phy_provider;
703
704 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
705 if (!ptr)
706 return ERR_PTR(-ENOMEM);
707
708 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
709 if (!IS_ERR(phy_provider)) {
710 *ptr = phy_provider;
711 devres_add(dev, ptr);
712 } else {
713 devres_free(ptr);
714 }
715
716 return phy_provider;
717}
718EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
719
720/**
721 * of_phy_provider_unregister() - unregister phy provider from the framework
722 * @phy_provider: phy provider returned by of_phy_provider_register()
723 *
724 * Removes the phy_provider created using of_phy_provider_register().
725 */
726void of_phy_provider_unregister(struct phy_provider *phy_provider)
727{
728 if (IS_ERR(phy_provider))
729 return;
730
731 mutex_lock(&phy_provider_mutex);
732 list_del(&phy_provider->list);
733 kfree(phy_provider);
734 mutex_unlock(&phy_provider_mutex);
735}
736EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
737
738/**
739 * devm_of_phy_provider_unregister() - remove phy provider from the framework
740 * @dev: struct device of the phy provider
741 *
742 * destroys the devres associated with this phy provider and invokes
743 * of_phy_provider_unregister to unregister the phy provider.
744 */
745void devm_of_phy_provider_unregister(struct device *dev,
746 struct phy_provider *phy_provider) {
747 int r;
748
749 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
750 phy_provider);
751 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
752}
753EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
754
755/**
756 * phy_release() - release the phy
757 * @dev: the dev member within phy
758 *
759 * When the last reference to the device is removed, it is called
760 * from the embedded kobject as release method.
761 */
762static void phy_release(struct device *dev)
763{
764 struct phy *phy;
765
766 phy = to_phy(dev);
767 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
768 ida_remove(&phy_ida, phy->id);
769 kfree(phy);
770}
771
772static int __init phy_core_init(void)
773{
774 phy_class = class_create(THIS_MODULE, "phy");
775 if (IS_ERR(phy_class)) {
776 pr_err("failed to create phy class --> %ld\n",
777 PTR_ERR(phy_class));
778 return PTR_ERR(phy_class);
779 }
780
781 phy_class->dev_release = phy_release;
782
783 return 0;
784}
785module_init(phy_core_init);
786
787static void __exit phy_core_exit(void)
788{
789 class_destroy(phy_class);
790}
791module_exit(phy_core_exit);
792
793MODULE_DESCRIPTION("Generic PHY Framework");
794MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
795MODULE_LICENSE("GPL v2");