blob: 1606ce9805d03aa699a6a4426e5ba142bacd2449 [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);
29static DEFINE_IDA(phy_ida);
30
31static void devm_phy_release(struct device *dev, void *res)
32{
33 struct phy *phy = *(struct phy **)res;
34
35 phy_put(phy);
36}
37
38static 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
45static void devm_phy_consume(struct device *dev, void *res)
46{
47 struct phy *phy = *(struct phy **)res;
48
49 phy_destroy(phy);
50}
51
52static int devm_phy_match(struct device *dev, void *res, void *match_data)
53{
54 return res == match_data;
55}
56
57static 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);
Sergei Shtylyov743bb382014-04-19 08:51:43 +053068
69 if (!phy->init_data)
70 continue;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053071 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
87static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
88{
89 struct phy_provider *phy_provider;
Kishon Vijay Abraham I2a4c3702014-07-14 15:55:01 +053090 struct device_node *child;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053091
92 list_for_each_entry(phy_provider, &phy_provider_list, list) {
93 if (phy_provider->dev->of_node == node)
94 return phy_provider;
Kishon Vijay Abraham I2a4c3702014-07-14 15:55:01 +053095
96 for_each_child_of_node(phy_provider->dev->of_node, child)
97 if (child == node)
98 return phy_provider;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053099 }
100
101 return ERR_PTR(-EPROBE_DEFER);
102}
103
104int phy_pm_runtime_get(struct phy *phy)
105{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600106 int ret;
107
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530108 if (!pm_runtime_enabled(&phy->dev))
109 return -ENOTSUPP;
110
Felipe Balbicedb7f82013-12-20 15:00:48 -0600111 ret = pm_runtime_get(&phy->dev);
112 if (ret < 0 && ret != -EINPROGRESS)
113 pm_runtime_put_noidle(&phy->dev);
114
115 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530116}
117EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
118
119int phy_pm_runtime_get_sync(struct phy *phy)
120{
Felipe Balbicedb7f82013-12-20 15:00:48 -0600121 int ret;
122
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530123 if (!pm_runtime_enabled(&phy->dev))
124 return -ENOTSUPP;
125
Felipe Balbicedb7f82013-12-20 15:00:48 -0600126 ret = pm_runtime_get_sync(&phy->dev);
127 if (ret < 0)
128 pm_runtime_put_sync(&phy->dev);
129
130 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530131}
132EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
133
134int phy_pm_runtime_put(struct phy *phy)
135{
136 if (!pm_runtime_enabled(&phy->dev))
137 return -ENOTSUPP;
138
139 return pm_runtime_put(&phy->dev);
140}
141EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
142
143int phy_pm_runtime_put_sync(struct phy *phy)
144{
145 if (!pm_runtime_enabled(&phy->dev))
146 return -ENOTSUPP;
147
148 return pm_runtime_put_sync(&phy->dev);
149}
150EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
151
152void phy_pm_runtime_allow(struct phy *phy)
153{
154 if (!pm_runtime_enabled(&phy->dev))
155 return;
156
157 pm_runtime_allow(&phy->dev);
158}
159EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
160
161void phy_pm_runtime_forbid(struct phy *phy)
162{
163 if (!pm_runtime_enabled(&phy->dev))
164 return;
165
166 pm_runtime_forbid(&phy->dev);
167}
168EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
169
170int phy_init(struct phy *phy)
171{
172 int ret;
173
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100174 if (!phy)
175 return 0;
176
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530177 ret = phy_pm_runtime_get_sync(phy);
178 if (ret < 0 && ret != -ENOTSUPP)
179 return ret;
180
181 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530182 if (phy->init_count == 0 && phy->ops->init) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530183 ret = phy->ops->init(phy);
184 if (ret < 0) {
185 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
186 goto out;
187 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530188 } else {
189 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530190 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530191 ++phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530192
193out:
194 mutex_unlock(&phy->mutex);
195 phy_pm_runtime_put(phy);
196 return ret;
197}
198EXPORT_SYMBOL_GPL(phy_init);
199
200int phy_exit(struct phy *phy)
201{
202 int ret;
203
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100204 if (!phy)
205 return 0;
206
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530207 ret = phy_pm_runtime_get_sync(phy);
208 if (ret < 0 && ret != -ENOTSUPP)
209 return ret;
210
211 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530212 if (phy->init_count == 1 && phy->ops->exit) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530213 ret = phy->ops->exit(phy);
214 if (ret < 0) {
215 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
216 goto out;
217 }
218 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530219 --phy->init_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530220
221out:
222 mutex_unlock(&phy->mutex);
223 phy_pm_runtime_put(phy);
224 return ret;
225}
226EXPORT_SYMBOL_GPL(phy_exit);
227
228int phy_power_on(struct phy *phy)
229{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530230 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530231
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100232 if (!phy)
233 return 0;
234
Roger Quadros3be88122014-07-04 12:55:45 +0300235 if (phy->pwr) {
236 ret = regulator_enable(phy->pwr);
237 if (ret)
238 return ret;
239 }
240
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530241 ret = phy_pm_runtime_get_sync(phy);
242 if (ret < 0 && ret != -ENOTSUPP)
243 return ret;
244
245 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530246 if (phy->power_count == 0 && phy->ops->power_on) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530247 ret = phy->ops->power_on(phy);
248 if (ret < 0) {
249 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
250 goto out;
251 }
Hans de Goede767a1b52014-02-17 14:29:23 +0530252 } else {
253 ret = 0; /* Override possible ret == -ENOTSUPP */
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530254 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530255 ++phy->power_count;
256 mutex_unlock(&phy->mutex);
257 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530258
259out:
260 mutex_unlock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530261 phy_pm_runtime_put_sync(phy);
Roger Quadros3be88122014-07-04 12:55:45 +0300262 if (phy->pwr)
263 regulator_disable(phy->pwr);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530264
265 return ret;
266}
267EXPORT_SYMBOL_GPL(phy_power_on);
268
269int phy_power_off(struct phy *phy)
270{
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530271 int ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530272
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100273 if (!phy)
274 return 0;
275
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530276 mutex_lock(&phy->mutex);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530277 if (phy->power_count == 1 && phy->ops->power_off) {
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530278 ret = phy->ops->power_off(phy);
279 if (ret < 0) {
280 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530281 mutex_unlock(&phy->mutex);
282 return ret;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530283 }
284 }
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530285 --phy->power_count;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530286 mutex_unlock(&phy->mutex);
287 phy_pm_runtime_put(phy);
288
Roger Quadros3be88122014-07-04 12:55:45 +0300289 if (phy->pwr)
290 regulator_disable(phy->pwr);
291
Kishon Vijay Abraham I637d3782013-12-20 10:36:49 +0530292 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530293}
294EXPORT_SYMBOL_GPL(phy_power_off);
295
296/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100297 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
298 * @np: device_node for which to get the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530299 * @index: the index of the phy
300 *
301 * Returns the phy associated with the given phandle value,
302 * after getting a refcount to it or -ENODEV if there is no such phy or
303 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
304 * not yet loaded. This function uses of_xlate call back function provided
305 * while registering the phy_provider to find the phy instance.
306 */
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100307static struct phy *_of_phy_get(struct device_node *np, int index)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530308{
309 int ret;
310 struct phy_provider *phy_provider;
311 struct phy *phy = NULL;
312 struct of_phandle_args args;
313
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100314 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530315 index, &args);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100316 if (ret)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530317 return ERR_PTR(-ENODEV);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530318
319 mutex_lock(&phy_provider_mutex);
320 phy_provider = of_phy_provider_lookup(args.np);
321 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
322 phy = ERR_PTR(-EPROBE_DEFER);
323 goto err0;
324 }
325
326 phy = phy_provider->of_xlate(phy_provider->dev, &args);
327 module_put(phy_provider->owner);
328
329err0:
330 mutex_unlock(&phy_provider_mutex);
331 of_node_put(args.np);
332
333 return phy;
334}
335
336/**
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100337 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
338 * @np: device_node for which to get the phy
339 * @con_id: name of the phy from device's point of view
340 *
341 * Returns the phy driver, after getting a refcount to it; or
342 * -ENODEV if there is no such phy. The caller is responsible for
343 * calling phy_put() to release that count.
344 */
345struct phy *of_phy_get(struct device_node *np, const char *con_id)
346{
347 struct phy *phy = NULL;
348 int index = 0;
349
350 if (con_id)
351 index = of_property_match_string(np, "phy-names", con_id);
352
353 phy = _of_phy_get(np, index);
354 if (IS_ERR(phy))
355 return phy;
356
357 if (!try_module_get(phy->ops->owner))
358 return ERR_PTR(-EPROBE_DEFER);
359
360 get_device(&phy->dev);
361
362 return phy;
363}
364EXPORT_SYMBOL_GPL(of_phy_get);
365
366/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530367 * phy_put() - release the PHY
368 * @phy: the phy returned by phy_get()
369 *
370 * Releases a refcount the caller received from phy_get().
371 */
372void phy_put(struct phy *phy)
373{
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100374 if (!phy || IS_ERR(phy))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530375 return;
376
377 module_put(phy->ops->owner);
378 put_device(&phy->dev);
379}
380EXPORT_SYMBOL_GPL(phy_put);
381
382/**
383 * devm_phy_put() - release the PHY
384 * @dev: device that wants to release this phy
385 * @phy: the phy returned by devm_phy_get()
386 *
387 * destroys the devres associated with this phy and invokes phy_put
388 * to release the phy.
389 */
390void devm_phy_put(struct device *dev, struct phy *phy)
391{
392 int r;
393
Andrew Lunn04c2fac2014-02-04 18:33:11 +0100394 if (!phy)
395 return;
396
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530397 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
398 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
399}
400EXPORT_SYMBOL_GPL(devm_phy_put);
401
402/**
403 * of_phy_simple_xlate() - returns the phy instance from phy provider
404 * @dev: the PHY provider device
405 * @args: of_phandle_args (not used here)
406 *
407 * Intended to be used by phy provider for the common case where #phy-cells is
408 * 0. For other cases where #phy-cells is greater than '0', the phy provider
409 * should provide a custom of_xlate function that reads the *args* and returns
410 * the appropriate phy.
411 */
412struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
413 *args)
414{
415 struct phy *phy;
416 struct class_dev_iter iter;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530417
418 class_dev_iter_init(&iter, phy_class, NULL, NULL);
419 while ((dev = class_dev_iter_next(&iter))) {
420 phy = to_phy(dev);
Kishon Vijay Abraham I491e0492014-10-31 14:04:20 +0530421 if (args->np != phy->dev.of_node)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530422 continue;
423
424 class_dev_iter_exit(&iter);
425 return phy;
426 }
427
428 class_dev_iter_exit(&iter);
429 return ERR_PTR(-ENODEV);
430}
431EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
432
433/**
434 * phy_get() - lookup and obtain a reference to a phy.
435 * @dev: device that requests this phy
436 * @string: the phy name as given in the dt data or the name of the controller
437 * port for non-dt case
438 *
439 * Returns the phy driver, after getting a refcount to it; or
440 * -ENODEV if there is no such phy. The caller is responsible for
441 * calling phy_put() to release that count.
442 */
443struct phy *phy_get(struct device *dev, const char *string)
444{
445 int index = 0;
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530446 struct phy *phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530447
448 if (string == NULL) {
449 dev_WARN(dev, "missing string\n");
450 return ERR_PTR(-EINVAL);
451 }
452
453 if (dev->of_node) {
454 index = of_property_match_string(dev->of_node, "phy-names",
455 string);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100456 phy = _of_phy_get(dev->of_node, index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530457 } else {
458 phy = phy_lookup(dev, string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530459 }
Hans de Goedef40037f2014-02-17 14:29:22 +0530460 if (IS_ERR(phy))
461 return phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530462
463 if (!try_module_get(phy->ops->owner))
464 return ERR_PTR(-EPROBE_DEFER);
465
466 get_device(&phy->dev);
467
468 return phy;
469}
470EXPORT_SYMBOL_GPL(phy_get);
471
472/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100473 * phy_optional_get() - lookup and obtain a reference to an optional phy.
474 * @dev: device that requests this phy
475 * @string: the phy name as given in the dt data or the name of the controller
476 * port for non-dt case
477 *
478 * Returns the phy driver, after getting a refcount to it; or
479 * NULL if there is no such phy. The caller is responsible for
480 * calling phy_put() to release that count.
481 */
482struct phy *phy_optional_get(struct device *dev, const char *string)
483{
484 struct phy *phy = phy_get(dev, string);
485
486 if (PTR_ERR(phy) == -ENODEV)
487 phy = NULL;
488
489 return phy;
490}
491EXPORT_SYMBOL_GPL(phy_optional_get);
492
493/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530494 * devm_phy_get() - lookup and obtain a reference to a phy.
495 * @dev: device that requests this phy
496 * @string: the phy name as given in the dt data or phy device name
497 * for non-dt case
498 *
499 * Gets the phy using phy_get(), and associates a device with it using
500 * devres. On driver detach, release function is invoked on the devres data,
501 * then, devres data is freed.
502 */
503struct phy *devm_phy_get(struct device *dev, const char *string)
504{
505 struct phy **ptr, *phy;
506
507 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
508 if (!ptr)
509 return ERR_PTR(-ENOMEM);
510
511 phy = phy_get(dev, string);
512 if (!IS_ERR(phy)) {
513 *ptr = phy;
514 devres_add(dev, ptr);
515 } else {
516 devres_free(ptr);
517 }
518
519 return phy;
520}
521EXPORT_SYMBOL_GPL(devm_phy_get);
522
523/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100524 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
525 * @dev: device that requests this phy
526 * @string: the phy name as given in the dt data or phy device name
527 * for non-dt case
528 *
529 * Gets the phy using phy_get(), and associates a device with it using
530 * devres. On driver detach, release function is invoked on the devres
531 * data, then, devres data is freed. This differs to devm_phy_get() in
532 * that if the phy does not exist, it is not considered an error and
533 * -ENODEV will not be returned. Instead the NULL phy is returned,
534 * which can be passed to all other phy consumer calls.
535 */
536struct phy *devm_phy_optional_get(struct device *dev, const char *string)
537{
538 struct phy *phy = devm_phy_get(dev, string);
539
540 if (PTR_ERR(phy) == -ENODEV)
541 phy = NULL;
542
543 return phy;
544}
545EXPORT_SYMBOL_GPL(devm_phy_optional_get);
546
547/**
Kamil Debskib5d682f2014-03-06 12:16:47 +0100548 * devm_of_phy_get() - lookup and obtain a reference to a phy.
549 * @dev: device that requests this phy
550 * @np: node containing the phy
551 * @con_id: name of the phy from device's point of view
552 *
553 * Gets the phy using of_phy_get(), and associates a device with it using
554 * devres. On driver detach, release function is invoked on the devres data,
555 * then, devres data is freed.
556 */
557struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
558 const char *con_id)
559{
560 struct phy **ptr, *phy;
561
562 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
563 if (!ptr)
564 return ERR_PTR(-ENOMEM);
565
566 phy = of_phy_get(np, con_id);
567 if (!IS_ERR(phy)) {
568 *ptr = phy;
569 devres_add(dev, ptr);
570 } else {
571 devres_free(ptr);
572 }
573
574 return phy;
575}
576EXPORT_SYMBOL_GPL(devm_of_phy_get);
577
578/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530579 * phy_create() - create a new phy
580 * @dev: device that is creating the new phy
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530581 * @node: device node of the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530582 * @ops: function pointers for performing phy operations
583 * @init_data: contains the list of PHY consumers or NULL
584 *
585 * Called to create a phy using phy framework.
586 */
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530587struct phy *phy_create(struct device *dev, struct device_node *node,
588 const struct phy_ops *ops,
589 struct phy_init_data *init_data)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530590{
591 int ret;
592 int id;
593 struct phy *phy;
594
Dan Carpenter52797d22013-12-06 17:51:19 +0530595 if (WARN_ON(!dev))
596 return ERR_PTR(-EINVAL);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530597
598 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Dan Carpenter52797d22013-12-06 17:51:19 +0530599 if (!phy)
600 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530601
602 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
603 if (id < 0) {
604 dev_err(dev, "unable to get id\n");
605 ret = id;
Dan Carpenter52797d22013-12-06 17:51:19 +0530606 goto free_phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530607 }
608
Roger Quadros3be88122014-07-04 12:55:45 +0300609 /* phy-supply */
610 phy->pwr = regulator_get_optional(dev, "phy");
611 if (IS_ERR(phy->pwr)) {
612 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
613 ret = -EPROBE_DEFER;
614 goto free_ida;
615 }
616 phy->pwr = NULL;
617 }
618
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530619 device_initialize(&phy->dev);
620 mutex_init(&phy->mutex);
621
622 phy->dev.class = phy_class;
623 phy->dev.parent = dev;
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530624 phy->dev.of_node = node ?: dev->of_node;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530625 phy->id = id;
626 phy->ops = ops;
627 phy->init_data = init_data;
628
629 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
630 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530631 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530632
633 ret = device_add(&phy->dev);
634 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530635 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530636
637 if (pm_runtime_enabled(dev)) {
638 pm_runtime_enable(&phy->dev);
639 pm_runtime_no_callbacks(&phy->dev);
640 }
641
642 return phy;
643
Dan Carpenter52797d22013-12-06 17:51:19 +0530644put_dev:
Roger Quadrose73b49f2014-07-10 11:55:02 +0530645 put_device(&phy->dev); /* calls phy_release() which frees resources */
646 return ERR_PTR(ret);
647
Roger Quadros3be88122014-07-04 12:55:45 +0300648free_ida:
649 ida_simple_remove(&phy_ida, phy->id);
650
Dan Carpenter52797d22013-12-06 17:51:19 +0530651free_phy:
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530652 kfree(phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530653 return ERR_PTR(ret);
654}
655EXPORT_SYMBOL_GPL(phy_create);
656
657/**
658 * devm_phy_create() - create a new phy
659 * @dev: device that is creating the new phy
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530660 * @node: device node of the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530661 * @ops: function pointers for performing phy operations
662 * @init_data: contains the list of PHY consumers or NULL
663 *
664 * Creates a new PHY device adding it to the PHY class.
665 * While at that, it also associates the device with the phy using devres.
666 * On driver detach, release function is invoked on the devres data,
667 * then, devres data is freed.
668 */
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530669struct phy *devm_phy_create(struct device *dev, struct device_node *node,
670 const struct phy_ops *ops,
671 struct phy_init_data *init_data)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530672{
673 struct phy **ptr, *phy;
674
675 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
676 if (!ptr)
677 return ERR_PTR(-ENOMEM);
678
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530679 phy = phy_create(dev, node, ops, init_data);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530680 if (!IS_ERR(phy)) {
681 *ptr = phy;
682 devres_add(dev, ptr);
683 } else {
684 devres_free(ptr);
685 }
686
687 return phy;
688}
689EXPORT_SYMBOL_GPL(devm_phy_create);
690
691/**
692 * phy_destroy() - destroy the phy
693 * @phy: the phy to be destroyed
694 *
695 * Called to destroy the phy.
696 */
697void phy_destroy(struct phy *phy)
698{
699 pm_runtime_disable(&phy->dev);
700 device_unregister(&phy->dev);
701}
702EXPORT_SYMBOL_GPL(phy_destroy);
703
704/**
705 * devm_phy_destroy() - destroy the PHY
706 * @dev: device that wants to release this phy
707 * @phy: the phy returned by devm_phy_get()
708 *
709 * destroys the devres associated with this phy and invokes phy_destroy
710 * to destroy the phy.
711 */
712void devm_phy_destroy(struct device *dev, struct phy *phy)
713{
714 int r;
715
716 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
717 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
718}
719EXPORT_SYMBOL_GPL(devm_phy_destroy);
720
721/**
722 * __of_phy_provider_register() - create/register phy provider with the framework
723 * @dev: struct device of the phy provider
724 * @owner: the module owner containing of_xlate
725 * @of_xlate: function pointer to obtain phy instance from phy provider
726 *
727 * Creates struct phy_provider from dev and of_xlate function pointer.
728 * This is used in the case of dt boot for finding the phy instance from
729 * phy provider.
730 */
731struct phy_provider *__of_phy_provider_register(struct device *dev,
732 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
733 struct of_phandle_args *args))
734{
735 struct phy_provider *phy_provider;
736
737 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
738 if (!phy_provider)
739 return ERR_PTR(-ENOMEM);
740
741 phy_provider->dev = dev;
742 phy_provider->owner = owner;
743 phy_provider->of_xlate = of_xlate;
744
745 mutex_lock(&phy_provider_mutex);
746 list_add_tail(&phy_provider->list, &phy_provider_list);
747 mutex_unlock(&phy_provider_mutex);
748
749 return phy_provider;
750}
751EXPORT_SYMBOL_GPL(__of_phy_provider_register);
752
753/**
754 * __devm_of_phy_provider_register() - create/register phy provider with the
755 * framework
756 * @dev: struct device of the phy provider
757 * @owner: the module owner containing of_xlate
758 * @of_xlate: function pointer to obtain phy instance from phy provider
759 *
760 * Creates struct phy_provider from dev and of_xlate function pointer.
761 * This is used in the case of dt boot for finding the phy instance from
762 * phy provider. While at that, it also associates the device with the
763 * phy provider using devres. On driver detach, release function is invoked
764 * on the devres data, then, devres data is freed.
765 */
766struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
767 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
768 struct of_phandle_args *args))
769{
770 struct phy_provider **ptr, *phy_provider;
771
772 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
773 if (!ptr)
774 return ERR_PTR(-ENOMEM);
775
776 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
777 if (!IS_ERR(phy_provider)) {
778 *ptr = phy_provider;
779 devres_add(dev, ptr);
780 } else {
781 devres_free(ptr);
782 }
783
784 return phy_provider;
785}
786EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
787
788/**
789 * of_phy_provider_unregister() - unregister phy provider from the framework
790 * @phy_provider: phy provider returned by of_phy_provider_register()
791 *
792 * Removes the phy_provider created using of_phy_provider_register().
793 */
794void of_phy_provider_unregister(struct phy_provider *phy_provider)
795{
796 if (IS_ERR(phy_provider))
797 return;
798
799 mutex_lock(&phy_provider_mutex);
800 list_del(&phy_provider->list);
801 kfree(phy_provider);
802 mutex_unlock(&phy_provider_mutex);
803}
804EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
805
806/**
807 * devm_of_phy_provider_unregister() - remove phy provider from the framework
808 * @dev: struct device of the phy provider
809 *
810 * destroys the devres associated with this phy provider and invokes
811 * of_phy_provider_unregister to unregister the phy provider.
812 */
813void devm_of_phy_provider_unregister(struct device *dev,
814 struct phy_provider *phy_provider) {
815 int r;
816
817 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
818 phy_provider);
819 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
820}
821EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
822
823/**
824 * phy_release() - release the phy
825 * @dev: the dev member within phy
826 *
827 * When the last reference to the device is removed, it is called
828 * from the embedded kobject as release method.
829 */
830static void phy_release(struct device *dev)
831{
832 struct phy *phy;
833
834 phy = to_phy(dev);
835 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
Roger Quadros3be88122014-07-04 12:55:45 +0300836 regulator_put(phy->pwr);
Roger Quadrose73b49f2014-07-10 11:55:02 +0530837 ida_simple_remove(&phy_ida, phy->id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530838 kfree(phy);
839}
840
841static int __init phy_core_init(void)
842{
843 phy_class = class_create(THIS_MODULE, "phy");
844 if (IS_ERR(phy_class)) {
845 pr_err("failed to create phy class --> %ld\n",
846 PTR_ERR(phy_class));
847 return PTR_ERR(phy_class);
848 }
849
850 phy_class->dev_release = phy_release;
851
852 return 0;
853}
854module_init(phy_core_init);
855
856static void __exit phy_core_exit(void)
857{
858 class_destroy(phy_class);
859}
860module_exit(phy_core_exit);
861
862MODULE_DESCRIPTION("Generic PHY Framework");
863MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
864MODULE_LICENSE("GPL v2");