blob: 6ab78448af31822b1a856b2e4209fbb2b850bcf7 [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>
Chunfeng Yunb13307b2020-05-02 11:35:11 +02009#include <dm/devres.h>
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020010#include <generic-phy.h>
11
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020012static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
13{
14 return (struct phy_ops *)dev->driver->ops;
15}
16
17static int generic_phy_xlate_offs_flags(struct phy *phy,
Simon Glass23558bb2017-05-18 20:09:47 -060018 struct ofnode_phandle_args *args)
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020019{
20 debug("%s(phy=%p)\n", __func__, phy);
21
22 if (args->args_count > 1) {
23 debug("Invaild args_count: %d\n", args->args_count);
24 return -EINVAL;
25 }
26
27 if (args->args_count)
28 phy->id = args->args[0];
29 else
30 phy->id = 0;
31
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020032 return 0;
33}
34
Neil Armstrongc2b9aa92020-03-30 11:27:23 +020035int generic_phy_get_by_node(ofnode node, int index, struct phy *phy)
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020036{
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;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020039 struct udevice *phydev;
Patrice Chotarda1b2fae2018-06-27 11:55:42 +020040 int i, ret;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020041
Neil Armstrongc2b9aa92020-03-30 11:27:23 +020042 debug("%s(node=%s, index=%d, phy=%p)\n",
43 __func__, ofnode_get_name(node), index, phy);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020044
45 assert(phy);
Patrice Chotardb9688df2017-07-18 11:38:42 +020046 phy->dev = NULL;
Neil Armstrongc2b9aa92020-03-30 11:27:23 +020047 ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0,
48 index, &args);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020049 if (ret) {
Simon Glass23558bb2017-05-18 20:09:47 -060050 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020051 __func__, ret);
52 return ret;
53 }
54
Simon Glass23558bb2017-05-18 20:09:47 -060055 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020056 if (ret) {
Simon Glass23558bb2017-05-18 20:09:47 -060057 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020058 __func__, ret);
Patrice Chotarda1b2fae2018-06-27 11:55:42 +020059
60 /* Check if args.node's parent is a PHY provider */
61 ret = uclass_get_device_by_ofnode(UCLASS_PHY,
62 ofnode_get_parent(args.node),
63 &phydev);
64 if (ret)
65 return ret;
66
67 /* insert phy idx at first position into args array */
Marek Vasut5e50adf2018-08-07 12:24:35 +020068 for (i = args.args_count; i >= 1 ; i--)
Patrice Chotarda1b2fae2018-06-27 11:55:42 +020069 args.args[i] = args.args[i - 1];
70
71 args.args_count++;
72 args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020073 }
74
75 phy->dev = phydev;
76
77 ops = phy_dev_ops(phydev);
78
79 if (ops->of_xlate)
80 ret = ops->of_xlate(phy, &args);
81 else
82 ret = generic_phy_xlate_offs_flags(phy, &args);
83 if (ret) {
84 debug("of_xlate() failed: %d\n", ret);
85 goto err;
86 }
87
88 return 0;
89
90err:
91 return ret;
92}
93
Neil Armstrongc2b9aa92020-03-30 11:27:23 +020094int generic_phy_get_by_index(struct udevice *dev, int index,
95 struct phy *phy)
96{
97 return generic_phy_get_by_node(dev_ofnode(dev), index, phy);
98}
99
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200100int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
101 struct phy *phy)
102{
103 int index;
104
105 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
106
Simon Glass23558bb2017-05-18 20:09:47 -0600107 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200108 if (index < 0) {
Simon Glass23558bb2017-05-18 20:09:47 -0600109 debug("dev_read_stringlist_search() failed: %d\n", index);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200110 return index;
111 }
112
113 return generic_phy_get_by_index(dev, index, phy);
114}
115
116int generic_phy_init(struct phy *phy)
117{
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200118 struct phy_ops const *ops;
119
120 if (!phy)
121 return 0;
122 ops = phy_dev_ops(phy->dev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200123
124 return ops->init ? ops->init(phy) : 0;
125}
126
127int generic_phy_reset(struct phy *phy)
128{
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200129 struct phy_ops const *ops;
130
131 if (!phy)
132 return 0;
133 ops = phy_dev_ops(phy->dev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200134
135 return ops->reset ? ops->reset(phy) : 0;
136}
137
138int generic_phy_exit(struct phy *phy)
139{
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200140 struct phy_ops const *ops;
141
142 if (!phy)
143 return 0;
144 ops = phy_dev_ops(phy->dev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200145
146 return ops->exit ? ops->exit(phy) : 0;
147}
148
149int generic_phy_power_on(struct phy *phy)
150{
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200151 struct phy_ops const *ops;
152
153 if (!phy)
154 return 0;
155 ops = phy_dev_ops(phy->dev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200156
157 return ops->power_on ? ops->power_on(phy) : 0;
158}
159
160int generic_phy_power_off(struct phy *phy)
161{
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200162 struct phy_ops const *ops;
163
164 if (!phy)
165 return 0;
166 ops = phy_dev_ops(phy->dev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200167
168 return ops->power_off ? ops->power_off(phy) : 0;
169}
170
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200171int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk)
172{
173 int i, ret, count;
174
175 bulk->count = 0;
176
177 /* Return if no phy declared */
178 if (!dev_read_prop(dev, "phys", NULL))
179 return 0;
180
181 count = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
182 if (count < 1)
183 return count;
184
185 bulk->phys = devm_kcalloc(dev, count, sizeof(struct phy), GFP_KERNEL);
186 if (!bulk->phys)
187 return -ENOMEM;
188
189 for (i = 0; i < count; i++) {
190 ret = generic_phy_get_by_index(dev, i, &bulk->phys[i]);
191 if (ret) {
192 pr_err("Failed to get PHY%d for %s\n", i, dev->name);
193 return ret;
194 }
195 bulk->count++;
196 }
197
198 return 0;
199}
200
201int generic_phy_init_bulk(struct phy_bulk *bulk)
202{
203 struct phy *phys = bulk->phys;
204 int i, ret;
205
206 for (i = 0; i < bulk->count; i++) {
207 ret = generic_phy_init(&phys[i]);
208 if (ret) {
209 pr_err("Can't init PHY%d\n", i);
210 goto phys_init_err;
211 }
212 }
213
214 return 0;
215
216phys_init_err:
217 for (; i > 0; i--)
218 generic_phy_exit(&phys[i - 1]);
219
220 return ret;
221}
222
223int generic_phy_exit_bulk(struct phy_bulk *bulk)
224{
225 struct phy *phys = bulk->phys;
226 int i, ret = 0;
227
228 for (i = 0; i < bulk->count; i++)
229 ret |= generic_phy_exit(&phys[i]);
230
231 return ret;
232}
233
234int generic_phy_power_on_bulk(struct phy_bulk *bulk)
235{
236 struct phy *phys = bulk->phys;
237 int i, ret;
238
239 for (i = 0; i < bulk->count; i++) {
240 ret = generic_phy_power_on(&phys[i]);
241 if (ret) {
242 pr_err("Can't power on PHY%d\n", i);
243 goto phys_poweron_err;
244 }
245 }
246
247 return 0;
248
249phys_poweron_err:
250 for (; i > 0; i--)
251 generic_phy_power_off(&phys[i - 1]);
252
253 return ret;
254}
255
256int generic_phy_power_off_bulk(struct phy_bulk *bulk)
257{
258 struct phy *phys = bulk->phys;
259 int i, ret = 0;
260
261 for (i = 0; i < bulk->count; i++)
262 ret |= generic_phy_power_off(&phys[i]);
263
264 return ret;
265}
266
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200267UCLASS_DRIVER(phy) = {
268 .id = UCLASS_PHY,
269 .name = "phy",
270};