blob: 527e744a3809acaaab24f431854c02b60b268553 [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;
417 struct device_node *node = dev->of_node;
418
419 class_dev_iter_init(&iter, phy_class, NULL, NULL);
420 while ((dev = class_dev_iter_next(&iter))) {
421 phy = to_phy(dev);
422 if (node != phy->dev.of_node)
423 continue;
424
425 class_dev_iter_exit(&iter);
426 return phy;
427 }
428
429 class_dev_iter_exit(&iter);
430 return ERR_PTR(-ENODEV);
431}
432EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
433
434/**
435 * phy_get() - lookup and obtain a reference to a phy.
436 * @dev: device that requests this phy
437 * @string: the phy name as given in the dt data or the name of the controller
438 * port for non-dt case
439 *
440 * Returns the phy driver, after getting a refcount to it; or
441 * -ENODEV if there is no such phy. The caller is responsible for
442 * calling phy_put() to release that count.
443 */
444struct phy *phy_get(struct device *dev, const char *string)
445{
446 int index = 0;
Kishon Vijay Abraham Id18c9602013-12-19 20:01:44 +0530447 struct phy *phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530448
449 if (string == NULL) {
450 dev_WARN(dev, "missing string\n");
451 return ERR_PTR(-EINVAL);
452 }
453
454 if (dev->of_node) {
455 index = of_property_match_string(dev->of_node, "phy-names",
456 string);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100457 phy = _of_phy_get(dev->of_node, index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530458 } else {
459 phy = phy_lookup(dev, string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530460 }
Hans de Goedef40037f2014-02-17 14:29:22 +0530461 if (IS_ERR(phy))
462 return phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530463
464 if (!try_module_get(phy->ops->owner))
465 return ERR_PTR(-EPROBE_DEFER);
466
467 get_device(&phy->dev);
468
469 return phy;
470}
471EXPORT_SYMBOL_GPL(phy_get);
472
473/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100474 * phy_optional_get() - lookup and obtain a reference to an optional phy.
475 * @dev: device that requests this phy
476 * @string: the phy name as given in the dt data or the name of the controller
477 * port for non-dt case
478 *
479 * Returns the phy driver, after getting a refcount to it; or
480 * NULL if there is no such phy. The caller is responsible for
481 * calling phy_put() to release that count.
482 */
483struct phy *phy_optional_get(struct device *dev, const char *string)
484{
485 struct phy *phy = phy_get(dev, string);
486
487 if (PTR_ERR(phy) == -ENODEV)
488 phy = NULL;
489
490 return phy;
491}
492EXPORT_SYMBOL_GPL(phy_optional_get);
493
494/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530495 * devm_phy_get() - lookup and obtain a reference to a phy.
496 * @dev: device that requests this phy
497 * @string: the phy name as given in the dt data or phy device name
498 * for non-dt case
499 *
500 * Gets the phy using phy_get(), and associates a device with it using
501 * devres. On driver detach, release function is invoked on the devres data,
502 * then, devres data is freed.
503 */
504struct phy *devm_phy_get(struct device *dev, const char *string)
505{
506 struct phy **ptr, *phy;
507
508 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
509 if (!ptr)
510 return ERR_PTR(-ENOMEM);
511
512 phy = phy_get(dev, string);
513 if (!IS_ERR(phy)) {
514 *ptr = phy;
515 devres_add(dev, ptr);
516 } else {
517 devres_free(ptr);
518 }
519
520 return phy;
521}
522EXPORT_SYMBOL_GPL(devm_phy_get);
523
524/**
Andrew Lunn788a4d52014-02-04 18:33:12 +0100525 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
526 * @dev: device that requests this phy
527 * @string: the phy name as given in the dt data or phy device name
528 * for non-dt case
529 *
530 * Gets the phy using phy_get(), and associates a device with it using
531 * devres. On driver detach, release function is invoked on the devres
532 * data, then, devres data is freed. This differs to devm_phy_get() in
533 * that if the phy does not exist, it is not considered an error and
534 * -ENODEV will not be returned. Instead the NULL phy is returned,
535 * which can be passed to all other phy consumer calls.
536 */
537struct phy *devm_phy_optional_get(struct device *dev, const char *string)
538{
539 struct phy *phy = devm_phy_get(dev, string);
540
541 if (PTR_ERR(phy) == -ENODEV)
542 phy = NULL;
543
544 return phy;
545}
546EXPORT_SYMBOL_GPL(devm_phy_optional_get);
547
548/**
Kamil Debskib5d682f2014-03-06 12:16:47 +0100549 * devm_of_phy_get() - lookup and obtain a reference to a phy.
550 * @dev: device that requests this phy
551 * @np: node containing the phy
552 * @con_id: name of the phy from device's point of view
553 *
554 * Gets the phy using of_phy_get(), and associates a device with it using
555 * devres. On driver detach, release function is invoked on the devres data,
556 * then, devres data is freed.
557 */
558struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
559 const char *con_id)
560{
561 struct phy **ptr, *phy;
562
563 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
564 if (!ptr)
565 return ERR_PTR(-ENOMEM);
566
567 phy = of_phy_get(np, con_id);
568 if (!IS_ERR(phy)) {
569 *ptr = phy;
570 devres_add(dev, ptr);
571 } else {
572 devres_free(ptr);
573 }
574
575 return phy;
576}
577EXPORT_SYMBOL_GPL(devm_of_phy_get);
578
579/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530580 * phy_create() - create a new phy
581 * @dev: device that is creating the new phy
582 * @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 */
587struct phy *phy_create(struct device *dev, const struct phy_ops *ops,
588 struct phy_init_data *init_data)
589{
590 int ret;
591 int id;
592 struct phy *phy;
593
Dan Carpenter52797d22013-12-06 17:51:19 +0530594 if (WARN_ON(!dev))
595 return ERR_PTR(-EINVAL);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530596
597 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
Dan Carpenter52797d22013-12-06 17:51:19 +0530598 if (!phy)
599 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530600
601 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
602 if (id < 0) {
603 dev_err(dev, "unable to get id\n");
604 ret = id;
Dan Carpenter52797d22013-12-06 17:51:19 +0530605 goto free_phy;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530606 }
607
Roger Quadros3be88122014-07-04 12:55:45 +0300608 /* phy-supply */
609 phy->pwr = regulator_get_optional(dev, "phy");
610 if (IS_ERR(phy->pwr)) {
611 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
612 ret = -EPROBE_DEFER;
613 goto free_ida;
614 }
615 phy->pwr = NULL;
616 }
617
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530618 device_initialize(&phy->dev);
619 mutex_init(&phy->mutex);
620
621 phy->dev.class = phy_class;
622 phy->dev.parent = dev;
623 phy->dev.of_node = dev->of_node;
624 phy->id = id;
625 phy->ops = ops;
626 phy->init_data = init_data;
627
628 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
629 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530630 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530631
632 ret = device_add(&phy->dev);
633 if (ret)
Dan Carpenter52797d22013-12-06 17:51:19 +0530634 goto put_dev;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530635
636 if (pm_runtime_enabled(dev)) {
637 pm_runtime_enable(&phy->dev);
638 pm_runtime_no_callbacks(&phy->dev);
639 }
640
641 return phy;
642
Dan Carpenter52797d22013-12-06 17:51:19 +0530643put_dev:
Roger Quadrose73b49f2014-07-10 11:55:02 +0530644 put_device(&phy->dev); /* calls phy_release() which frees resources */
645 return ERR_PTR(ret);
646
Roger Quadros3be88122014-07-04 12:55:45 +0300647free_ida:
648 ida_simple_remove(&phy_ida, phy->id);
649
Dan Carpenter52797d22013-12-06 17:51:19 +0530650free_phy:
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530651 kfree(phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530652 return ERR_PTR(ret);
653}
654EXPORT_SYMBOL_GPL(phy_create);
655
656/**
657 * devm_phy_create() - create a new phy
658 * @dev: device that is creating the new phy
659 * @ops: function pointers for performing phy operations
660 * @init_data: contains the list of PHY consumers or NULL
661 *
662 * Creates a new PHY device adding it to the PHY class.
663 * While at that, it also associates the device with the phy using devres.
664 * On driver detach, release function is invoked on the devres data,
665 * then, devres data is freed.
666 */
667struct phy *devm_phy_create(struct device *dev, const struct phy_ops *ops,
668 struct phy_init_data *init_data)
669{
670 struct phy **ptr, *phy;
671
672 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
673 if (!ptr)
674 return ERR_PTR(-ENOMEM);
675
676 phy = phy_create(dev, ops, init_data);
677 if (!IS_ERR(phy)) {
678 *ptr = phy;
679 devres_add(dev, ptr);
680 } else {
681 devres_free(ptr);
682 }
683
684 return phy;
685}
686EXPORT_SYMBOL_GPL(devm_phy_create);
687
688/**
689 * phy_destroy() - destroy the phy
690 * @phy: the phy to be destroyed
691 *
692 * Called to destroy the phy.
693 */
694void phy_destroy(struct phy *phy)
695{
696 pm_runtime_disable(&phy->dev);
697 device_unregister(&phy->dev);
698}
699EXPORT_SYMBOL_GPL(phy_destroy);
700
701/**
702 * devm_phy_destroy() - destroy the PHY
703 * @dev: device that wants to release this phy
704 * @phy: the phy returned by devm_phy_get()
705 *
706 * destroys the devres associated with this phy and invokes phy_destroy
707 * to destroy the phy.
708 */
709void devm_phy_destroy(struct device *dev, struct phy *phy)
710{
711 int r;
712
713 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
714 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
715}
716EXPORT_SYMBOL_GPL(devm_phy_destroy);
717
718/**
719 * __of_phy_provider_register() - create/register phy provider with the framework
720 * @dev: struct device of the phy provider
721 * @owner: the module owner containing of_xlate
722 * @of_xlate: function pointer to obtain phy instance from phy provider
723 *
724 * Creates struct phy_provider from dev and of_xlate function pointer.
725 * This is used in the case of dt boot for finding the phy instance from
726 * phy provider.
727 */
728struct phy_provider *__of_phy_provider_register(struct device *dev,
729 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
730 struct of_phandle_args *args))
731{
732 struct phy_provider *phy_provider;
733
734 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
735 if (!phy_provider)
736 return ERR_PTR(-ENOMEM);
737
738 phy_provider->dev = dev;
739 phy_provider->owner = owner;
740 phy_provider->of_xlate = of_xlate;
741
742 mutex_lock(&phy_provider_mutex);
743 list_add_tail(&phy_provider->list, &phy_provider_list);
744 mutex_unlock(&phy_provider_mutex);
745
746 return phy_provider;
747}
748EXPORT_SYMBOL_GPL(__of_phy_provider_register);
749
750/**
751 * __devm_of_phy_provider_register() - create/register phy provider with the
752 * framework
753 * @dev: struct device of the phy provider
754 * @owner: the module owner containing of_xlate
755 * @of_xlate: function pointer to obtain phy instance from phy provider
756 *
757 * Creates struct phy_provider from dev and of_xlate function pointer.
758 * This is used in the case of dt boot for finding the phy instance from
759 * phy provider. While at that, it also associates the device with the
760 * phy provider using devres. On driver detach, release function is invoked
761 * on the devres data, then, devres data is freed.
762 */
763struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
764 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
765 struct of_phandle_args *args))
766{
767 struct phy_provider **ptr, *phy_provider;
768
769 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
770 if (!ptr)
771 return ERR_PTR(-ENOMEM);
772
773 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
774 if (!IS_ERR(phy_provider)) {
775 *ptr = phy_provider;
776 devres_add(dev, ptr);
777 } else {
778 devres_free(ptr);
779 }
780
781 return phy_provider;
782}
783EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
784
785/**
786 * of_phy_provider_unregister() - unregister phy provider from the framework
787 * @phy_provider: phy provider returned by of_phy_provider_register()
788 *
789 * Removes the phy_provider created using of_phy_provider_register().
790 */
791void of_phy_provider_unregister(struct phy_provider *phy_provider)
792{
793 if (IS_ERR(phy_provider))
794 return;
795
796 mutex_lock(&phy_provider_mutex);
797 list_del(&phy_provider->list);
798 kfree(phy_provider);
799 mutex_unlock(&phy_provider_mutex);
800}
801EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
802
803/**
804 * devm_of_phy_provider_unregister() - remove phy provider from the framework
805 * @dev: struct device of the phy provider
806 *
807 * destroys the devres associated with this phy provider and invokes
808 * of_phy_provider_unregister to unregister the phy provider.
809 */
810void devm_of_phy_provider_unregister(struct device *dev,
811 struct phy_provider *phy_provider) {
812 int r;
813
814 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
815 phy_provider);
816 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
817}
818EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
819
820/**
821 * phy_release() - release the phy
822 * @dev: the dev member within phy
823 *
824 * When the last reference to the device is removed, it is called
825 * from the embedded kobject as release method.
826 */
827static void phy_release(struct device *dev)
828{
829 struct phy *phy;
830
831 phy = to_phy(dev);
832 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
Roger Quadros3be88122014-07-04 12:55:45 +0300833 regulator_put(phy->pwr);
Roger Quadrose73b49f2014-07-10 11:55:02 +0530834 ida_simple_remove(&phy_ida, phy->id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530835 kfree(phy);
836}
837
838static int __init phy_core_init(void)
839{
840 phy_class = class_create(THIS_MODULE, "phy");
841 if (IS_ERR(phy_class)) {
842 pr_err("failed to create phy class --> %ld\n",
843 PTR_ERR(phy_class));
844 return PTR_ERR(phy_class);
845 }
846
847 phy_class->dev_release = phy_release;
848
849 return 0;
850}
851module_init(phy_core_init);
852
853static void __exit phy_core_exit(void)
854{
855 class_destroy(phy_class);
856}
857module_exit(phy_core_exit);
858
859MODULE_DESCRIPTION("Generic PHY Framework");
860MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
861MODULE_LICENSE("GPL v2");