1 /*
2 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
3 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <generic-phy.h>
11
12 DECLARE_GLOBAL_DATA_PTR;
13
phy_dev_ops(struct udevice * dev)14 static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
15 {
16 return (struct phy_ops *)dev->driver->ops;
17 }
18
generic_phy_xlate_offs_flags(struct phy * phy,struct ofnode_phandle_args * args)19 static int generic_phy_xlate_offs_flags(struct phy *phy,
20 struct ofnode_phandle_args *args)
21 {
22 debug("%s(phy=%p)\n", __func__, phy);
23
24 if (args->args_count > 1) {
25 debug("Invaild args_count: %d\n", args->args_count);
26 return -EINVAL;
27 }
28
29 if (args->args_count)
30 phy->id = args->args[0];
31 else
32 phy->id = 0;
33
34 return 0;
35 }
36
generic_phy_get_by_index(struct udevice * dev,int index,struct phy * phy)37 int generic_phy_get_by_index(struct udevice *dev, int index,
38 struct phy *phy)
39 {
40 struct ofnode_phandle_args args;
41 struct phy_ops *ops;
42 int ret;
43 struct udevice *phydev;
44
45 debug("%s(dev=%p, index=%d, phy=%p)\n", __func__, dev, index, phy);
46
47 assert(phy);
48 phy->dev = NULL;
49 ret = dev_read_phandle_with_args(dev, "phys", "#phy-cells", 0, index,
50 &args);
51 if (ret) {
52 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
53 __func__, ret);
54 return ret;
55 }
56
57 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
58 if (ret) {
59 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
60 __func__, ret);
61 return ret;
62 }
63
64 phy->dev = phydev;
65
66 ops = phy_dev_ops(phydev);
67
68 if (ops->of_xlate)
69 ret = ops->of_xlate(phy, &args);
70 else
71 ret = generic_phy_xlate_offs_flags(phy, &args);
72 if (ret) {
73 debug("of_xlate() failed: %d\n", ret);
74 goto err;
75 }
76
77 return 0;
78
79 err:
80 return ret;
81 }
82
generic_phy_get_by_name(struct udevice * dev,const char * phy_name,struct phy * phy)83 int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
84 struct phy *phy)
85 {
86 int index;
87
88 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
89
90 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
91 if (index < 0) {
92 debug("dev_read_stringlist_search() failed: %d\n", index);
93 return index;
94 }
95
96 return generic_phy_get_by_index(dev, index, phy);
97 }
98
generic_phy_init(struct phy * phy)99 int generic_phy_init(struct phy *phy)
100 {
101 struct phy_ops const *ops;
102
103 if (!generic_phy_valid(phy))
104 return 0;
105 ops = phy_dev_ops(phy->dev);
106
107 return ops->init ? ops->init(phy) : 0;
108 }
109
generic_phy_reset(struct phy * phy)110 int generic_phy_reset(struct phy *phy)
111 {
112 struct phy_ops const *ops;
113
114 if (!generic_phy_valid(phy))
115 return 0;
116 ops = phy_dev_ops(phy->dev);
117
118 return ops->reset ? ops->reset(phy) : 0;
119 }
120
generic_phy_exit(struct phy * phy)121 int generic_phy_exit(struct phy *phy)
122 {
123 struct phy_ops const *ops;
124
125 if (!generic_phy_valid(phy))
126 return 0;
127 ops = phy_dev_ops(phy->dev);
128
129 return ops->exit ? ops->exit(phy) : 0;
130 }
131
generic_phy_power_on(struct phy * phy)132 int generic_phy_power_on(struct phy *phy)
133 {
134 struct phy_ops const *ops;
135
136 if (!generic_phy_valid(phy))
137 return 0;
138 ops = phy_dev_ops(phy->dev);
139
140 return ops->power_on ? ops->power_on(phy) : 0;
141 }
142
generic_phy_power_off(struct phy * phy)143 int generic_phy_power_off(struct phy *phy)
144 {
145 struct phy_ops const *ops;
146
147 if (!generic_phy_valid(phy))
148 return 0;
149 ops = phy_dev_ops(phy->dev);
150
151 return ops->power_off ? ops->power_off(phy) : 0;
152 }
153
generic_phy_configure(struct phy * phy,union phy_configure_opts * opts)154 int generic_phy_configure(struct phy *phy, union phy_configure_opts *opts)
155 {
156 struct phy_ops const *ops;
157
158 if (!generic_phy_valid(phy))
159 return 0;
160 ops = phy_dev_ops(phy->dev);
161
162 return ops->configure ? ops->configure(phy, opts) : 0;
163 }
164
generic_phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)165 int generic_phy_validate(struct phy *phy, enum phy_mode mode, int submode,
166 union phy_configure_opts *opts)
167 {
168 struct phy_ops const *ops;
169
170 if (!generic_phy_valid(phy))
171 return 0;
172 ops = phy_dev_ops(phy->dev);
173
174 return ops->validate ? ops->validate(phy, mode, submode, opts) : 0;
175 }
176
generic_phy_set_mode_ext(struct phy * phy,enum phy_mode mode,int submode)177 int generic_phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
178 {
179 struct phy_ops const *ops;
180 int ret;
181
182 if (!generic_phy_valid(phy))
183 return 0;
184 ops = phy_dev_ops(phy->dev);
185
186 if (!ops->set_mode)
187 return 0;
188
189 ret = ops->set_mode(phy, mode, submode);
190 if (!ret)
191 phy->attrs.mode = mode;
192
193 return ret;
194 }
195
196 UCLASS_DRIVER(phy) = {
197 .id = UCLASS_PHY,
198 .name = "phy",
199 };
200