blob: c4b3e409b3e02461a5e4759faf34a43e971b0d65 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +02002/*
3 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
4 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +02005 */
6
7#include <common.h>
8#include <dm.h>
9#include <generic-phy.h>
10
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020011static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
12{
13 return (struct phy_ops *)dev->driver->ops;
14}
15
16static int generic_phy_xlate_offs_flags(struct phy *phy,
Simon Glass23558bb2017-05-18 20:09:47 -060017 struct ofnode_phandle_args *args)
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020018{
19 debug("%s(phy=%p)\n", __func__, phy);
20
21 if (args->args_count > 1) {
22 debug("Invaild args_count: %d\n", args->args_count);
23 return -EINVAL;
24 }
25
26 if (args->args_count)
27 phy->id = args->args[0];
28 else
29 phy->id = 0;
30
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020031 return 0;
32}
33
34int generic_phy_get_by_index(struct udevice *dev, int index,
35 struct phy *phy)
36{
Simon Glass23558bb2017-05-18 20:09:47 -060037 struct ofnode_phandle_args args;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020038 struct phy_ops *ops;
39 int ret;
40 struct udevice *phydev;
41
42 debug("%s(dev=%p, index=%d, phy=%p)\n", __func__, dev, index, phy);
43
44 assert(phy);
Patrice Chotardb9688df2017-07-18 11:38:42 +020045 phy->dev = NULL;
Simon Glass23558bb2017-05-18 20:09:47 -060046 ret = dev_read_phandle_with_args(dev, "phys", "#phy-cells", 0, index,
47 &args);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020048 if (ret) {
Simon Glass23558bb2017-05-18 20:09:47 -060049 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020050 __func__, ret);
51 return ret;
52 }
53
Simon Glass23558bb2017-05-18 20:09:47 -060054 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020055 if (ret) {
Simon Glass23558bb2017-05-18 20:09:47 -060056 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020057 __func__, ret);
58 return ret;
59 }
60
61 phy->dev = phydev;
62
63 ops = phy_dev_ops(phydev);
64
65 if (ops->of_xlate)
66 ret = ops->of_xlate(phy, &args);
67 else
68 ret = generic_phy_xlate_offs_flags(phy, &args);
69 if (ret) {
70 debug("of_xlate() failed: %d\n", ret);
71 goto err;
72 }
73
74 return 0;
75
76err:
77 return ret;
78}
79
80int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
81 struct phy *phy)
82{
83 int index;
84
85 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
86
Simon Glass23558bb2017-05-18 20:09:47 -060087 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020088 if (index < 0) {
Simon Glass23558bb2017-05-18 20:09:47 -060089 debug("dev_read_stringlist_search() failed: %d\n", index);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020090 return index;
91 }
92
93 return generic_phy_get_by_index(dev, index, phy);
94}
95
96int generic_phy_init(struct phy *phy)
97{
98 struct phy_ops const *ops = phy_dev_ops(phy->dev);
99
100 return ops->init ? ops->init(phy) : 0;
101}
102
103int generic_phy_reset(struct phy *phy)
104{
105 struct phy_ops const *ops = phy_dev_ops(phy->dev);
106
107 return ops->reset ? ops->reset(phy) : 0;
108}
109
110int generic_phy_exit(struct phy *phy)
111{
112 struct phy_ops const *ops = phy_dev_ops(phy->dev);
113
114 return ops->exit ? ops->exit(phy) : 0;
115}
116
117int generic_phy_power_on(struct phy *phy)
118{
119 struct phy_ops const *ops = phy_dev_ops(phy->dev);
120
121 return ops->power_on ? ops->power_on(phy) : 0;
122}
123
124int generic_phy_power_off(struct phy *phy)
125{
126 struct phy_ops const *ops = phy_dev_ops(phy->dev);
127
128 return ops->power_off ? ops->power_off(phy) : 0;
129}
130
131UCLASS_DRIVER(phy) = {
132 .id = UCLASS_PHY,
133 .name = "phy",
134};