blob: 28cc0e6f2b8074042e005b8809674856dbb60ff6 [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
Patrick Delaunayb953ec22021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_PHY
8
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +02009#include <common.h>
10#include <dm.h>
Sean Andersonbdc1fdf2020-10-04 21:39:47 -040011#include <dm/device_compat.h>
Chunfeng Yunb13307b2020-05-02 11:35:11 +020012#include <dm/devres.h>
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020013#include <generic-phy.h>
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030014#include <linux/list.h>
Eugen Hristevc57e0dc2023-05-15 12:59:47 +030015#include <power/regulator.h>
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030016
17/**
18 * struct phy_counts - Init and power-on counts of a single PHY port
19 *
20 * This structure is used to keep track of PHY initialization and power
21 * state change requests, so that we don't power off and deinitialize a
22 * PHY instance until all of its users want it done. Otherwise, multiple
23 * consumers using the same PHY port can cause problems (e.g. one might
24 * call power_off() after another's exit() and hang indefinitely).
25 *
26 * @id: The PHY ID within a PHY provider
27 * @power_on_count: Times generic_phy_power_on() was called for this ID
28 * without a matching generic_phy_power_off() afterwards
29 * @init_count: Times generic_phy_init() was called for this ID
30 * without a matching generic_phy_exit() afterwards
31 * @list: Handle for a linked list of these structures corresponding to
32 * ports of the same PHY provider
Eugen Hristevc57e0dc2023-05-15 12:59:47 +030033 * @supply: Handle to a phy-supply device
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030034 */
35struct phy_counts {
36 unsigned long id;
37 int power_on_count;
38 int init_count;
39 struct list_head list;
Eugen Hristevc57e0dc2023-05-15 12:59:47 +030040 struct udevice *supply;
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030041};
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020042
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +020043static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
44{
45 return (struct phy_ops *)dev->driver->ops;
46}
47
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030048static struct phy_counts *phy_get_counts(struct phy *phy)
49{
50 struct list_head *uc_priv;
51 struct phy_counts *counts;
52
53 if (!generic_phy_valid(phy))
54 return NULL;
55
56 uc_priv = dev_get_uclass_priv(phy->dev);
57 list_for_each_entry(counts, uc_priv, list)
58 if (counts->id == phy->id)
59 return counts;
60
61 return NULL;
62}
63
Eugen Hristevc57e0dc2023-05-15 12:59:47 +030064static int phy_alloc_counts(struct phy *phy, struct udevice *supply)
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030065{
66 struct list_head *uc_priv;
67 struct phy_counts *counts;
68
69 if (!generic_phy_valid(phy))
70 return 0;
71 if (phy_get_counts(phy))
72 return 0;
73
74 uc_priv = dev_get_uclass_priv(phy->dev);
75 counts = kzalloc(sizeof(*counts), GFP_KERNEL);
76 if (!counts)
77 return -ENOMEM;
78
79 counts->id = phy->id;
80 counts->power_on_count = 0;
81 counts->init_count = 0;
Eugen Hristevc57e0dc2023-05-15 12:59:47 +030082 counts->supply = supply;
Alper Nebi Yasak226fce62021-12-30 22:36:51 +030083 list_add(&counts->list, uc_priv);
84
85 return 0;
86}
87
88static int phy_uclass_pre_probe(struct udevice *dev)
89{
90 struct list_head *uc_priv = dev_get_uclass_priv(dev);
91
92 INIT_LIST_HEAD(uc_priv);
93
94 return 0;
95}
96
97static int phy_uclass_pre_remove(struct udevice *dev)
98{
99 struct list_head *uc_priv = dev_get_uclass_priv(dev);
100 struct phy_counts *counts, *next;
101
102 list_for_each_entry_safe(counts, next, uc_priv, list)
103 kfree(counts);
104
105 return 0;
106}
107
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200108static int generic_phy_xlate_offs_flags(struct phy *phy,
Simon Glass23558bb2017-05-18 20:09:47 -0600109 struct ofnode_phandle_args *args)
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200110{
111 debug("%s(phy=%p)\n", __func__, phy);
112
113 if (args->args_count > 1) {
Sean Anderson46ad7ce2021-12-01 14:26:53 -0500114 debug("Invalid args_count: %d\n", args->args_count);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200115 return -EINVAL;
116 }
117
118 if (args->args_count)
119 phy->id = args->args[0];
120 else
121 phy->id = 0;
122
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200123 return 0;
124}
125
Jagan Teki5a2b6772020-05-01 23:44:18 +0530126int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy)
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200127{
Simon Glass23558bb2017-05-18 20:09:47 -0600128 struct ofnode_phandle_args args;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200129 struct phy_ops *ops;
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300130 struct udevice *phydev, *supply = NULL;
Patrice Chotarda1b2fae2018-06-27 11:55:42 +0200131 int i, ret;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200132
Neil Armstrongc2b9aa92020-03-30 11:27:23 +0200133 debug("%s(node=%s, index=%d, phy=%p)\n",
134 __func__, ofnode_get_name(node), index, phy);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200135
136 assert(phy);
Patrice Chotardb9688df2017-07-18 11:38:42 +0200137 phy->dev = NULL;
Neil Armstrongc2b9aa92020-03-30 11:27:23 +0200138 ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0,
139 index, &args);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200140 if (ret) {
Simon Glass23558bb2017-05-18 20:09:47 -0600141 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200142 __func__, ret);
143 return ret;
144 }
145
Simon Glass23558bb2017-05-18 20:09:47 -0600146 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200147 if (ret) {
Simon Glass23558bb2017-05-18 20:09:47 -0600148 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200149 __func__, ret);
Patrice Chotarda1b2fae2018-06-27 11:55:42 +0200150
151 /* Check if args.node's parent is a PHY provider */
152 ret = uclass_get_device_by_ofnode(UCLASS_PHY,
153 ofnode_get_parent(args.node),
154 &phydev);
155 if (ret)
156 return ret;
157
158 /* insert phy idx at first position into args array */
Marek Vasut5e50adf2018-08-07 12:24:35 +0200159 for (i = args.args_count; i >= 1 ; i--)
Patrice Chotarda1b2fae2018-06-27 11:55:42 +0200160 args.args[i] = args.args[i - 1];
161
162 args.args_count++;
163 args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200164 }
165
166 phy->dev = phydev;
167
168 ops = phy_dev_ops(phydev);
169
170 if (ops->of_xlate)
171 ret = ops->of_xlate(phy, &args);
172 else
173 ret = generic_phy_xlate_offs_flags(phy, &args);
174 if (ret) {
175 debug("of_xlate() failed: %d\n", ret);
176 goto err;
177 }
178
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300179 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
180 ret = device_get_supply_regulator(phydev, "phy-supply",
181 &supply);
182 if (ret && ret != -ENOENT) {
183 debug("%s: device_get_supply_regulator failed: %d\n",
184 __func__, ret);
185 goto err;
186 }
187 }
188
189 ret = phy_alloc_counts(phy, supply);
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300190 if (ret) {
191 debug("phy_alloc_counts() failed: %d\n", ret);
192 goto err;
193 }
194
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200195 return 0;
196
197err:
198 return ret;
199}
200
Neil Armstrongc2b9aa92020-03-30 11:27:23 +0200201int generic_phy_get_by_index(struct udevice *dev, int index,
202 struct phy *phy)
203{
Jagan Teki5a2b6772020-05-01 23:44:18 +0530204 return generic_phy_get_by_index_nodev(dev_ofnode(dev), index, phy);
Neil Armstrongc2b9aa92020-03-30 11:27:23 +0200205}
206
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200207int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
208 struct phy *phy)
209{
210 int index;
211
212 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
213
Simon Glass23558bb2017-05-18 20:09:47 -0600214 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200215 if (index < 0) {
Simon Glass23558bb2017-05-18 20:09:47 -0600216 debug("dev_read_stringlist_search() failed: %d\n", index);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200217 return index;
218 }
219
220 return generic_phy_get_by_index(dev, index, phy);
221}
222
223int generic_phy_init(struct phy *phy)
224{
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300225 struct phy_counts *counts;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200226 struct phy_ops const *ops;
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200227 int ret;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200228
Vignesh Raghavendra64b69f82020-05-20 22:35:41 +0530229 if (!generic_phy_valid(phy))
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200230 return 0;
231 ops = phy_dev_ops(phy->dev);
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200232 if (!ops->init)
233 return 0;
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300234
235 counts = phy_get_counts(phy);
236 if (counts->init_count > 0) {
237 counts->init_count++;
238 return 0;
239 }
240
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200241 ret = ops->init(phy);
242 if (ret)
243 dev_err(phy->dev, "PHY: Failed to init %s: %d.\n",
244 phy->dev->name, ret);
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300245 else
246 counts->init_count = 1;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200247
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200248 return ret;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200249}
250
251int generic_phy_reset(struct phy *phy)
252{
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200253 struct phy_ops const *ops;
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200254 int ret;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200255
Vignesh Raghavendra64b69f82020-05-20 22:35:41 +0530256 if (!generic_phy_valid(phy))
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200257 return 0;
258 ops = phy_dev_ops(phy->dev);
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200259 if (!ops->reset)
260 return 0;
261 ret = ops->reset(phy);
262 if (ret)
263 dev_err(phy->dev, "PHY: Failed to reset %s: %d.\n",
264 phy->dev->name, ret);
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200265
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200266 return ret;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200267}
268
269int generic_phy_exit(struct phy *phy)
270{
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300271 struct phy_counts *counts;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200272 struct phy_ops const *ops;
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200273 int ret;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200274
Vignesh Raghavendra64b69f82020-05-20 22:35:41 +0530275 if (!generic_phy_valid(phy))
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200276 return 0;
277 ops = phy_dev_ops(phy->dev);
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200278 if (!ops->exit)
279 return 0;
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300280
281 counts = phy_get_counts(phy);
282 if (counts->init_count == 0)
283 return 0;
284 if (counts->init_count > 1) {
285 counts->init_count--;
286 return 0;
287 }
288
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200289 ret = ops->exit(phy);
290 if (ret)
291 dev_err(phy->dev, "PHY: Failed to exit %s: %d.\n",
292 phy->dev->name, ret);
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300293 else
294 counts->init_count = 0;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200295
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200296 return ret;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200297}
298
299int generic_phy_power_on(struct phy *phy)
300{
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300301 struct phy_counts *counts;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200302 struct phy_ops const *ops;
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200303 int ret;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200304
Vignesh Raghavendra64b69f82020-05-20 22:35:41 +0530305 if (!generic_phy_valid(phy))
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200306 return 0;
307 ops = phy_dev_ops(phy->dev);
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200308 if (!ops->power_on)
309 return 0;
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300310
311 counts = phy_get_counts(phy);
312 if (counts->power_on_count > 0) {
313 counts->power_on_count++;
314 return 0;
315 }
316
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300317 ret = regulator_set_enable_if_allowed(counts->supply, true);
318 if (ret && ret != -ENOSYS) {
319 dev_err(phy->dev, "PHY: Failed to enable regulator %s: %d.\n",
320 counts->supply->name, ret);
321 return ret;
322 }
323
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200324 ret = ops->power_on(phy);
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300325 if (ret) {
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200326 dev_err(phy->dev, "PHY: Failed to power on %s: %d.\n",
327 phy->dev->name, ret);
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300328 regulator_set_enable_if_allowed(counts->supply, false);
329 return ret;
330 }
331 counts->power_on_count = 1;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200332
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300333 return 0;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200334}
335
336int generic_phy_power_off(struct phy *phy)
337{
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300338 struct phy_counts *counts;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200339 struct phy_ops const *ops;
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200340 int ret;
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200341
Vignesh Raghavendra64b69f82020-05-20 22:35:41 +0530342 if (!generic_phy_valid(phy))
Jean-Jacques Hiblot4e184292019-10-01 14:03:26 +0200343 return 0;
344 ops = phy_dev_ops(phy->dev);
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200345 if (!ops->power_off)
346 return 0;
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300347
348 counts = phy_get_counts(phy);
349 if (counts->power_on_count == 0)
350 return 0;
351 if (counts->power_on_count > 1) {
352 counts->power_on_count--;
353 return 0;
354 }
355
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200356 ret = ops->power_off(phy);
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300357 if (ret) {
Patrick Delaunay2041ae52020-07-03 17:36:40 +0200358 dev_err(phy->dev, "PHY: Failed to power off %s: %d.\n",
359 phy->dev->name, ret);
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300360 return ret;
361 }
362 counts->power_on_count = 0;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200363
Eugen Hristevc57e0dc2023-05-15 12:59:47 +0300364 ret = regulator_set_enable_if_allowed(counts->supply, false);
365 if (ret && ret != -ENOSYS)
366 dev_err(phy->dev, "PHY: Failed to disable regulator %s: %d.\n",
367 counts->supply->name, ret);
368
369 return 0;
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200370}
371
Neil Armstrongf8da8a82020-12-29 14:58:59 +0100372int generic_phy_configure(struct phy *phy, void *params)
373{
374 struct phy_ops const *ops;
375
376 if (!generic_phy_valid(phy))
377 return 0;
378 ops = phy_dev_ops(phy->dev);
379
380 return ops->configure ? ops->configure(phy, params) : 0;
381}
382
Marek Vasutb0177a22023-03-19 18:09:42 +0100383int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
384{
385 struct phy_ops const *ops;
386
387 if (!generic_phy_valid(phy))
388 return 0;
389 ops = phy_dev_ops(phy->dev);
390
391 return ops->set_mode ? ops->set_mode(phy, mode, submode) : 0;
392}
393
394int generic_phy_set_speed(struct phy *phy, int speed)
395{
396 struct phy_ops const *ops;
397
398 if (!generic_phy_valid(phy))
399 return 0;
400 ops = phy_dev_ops(phy->dev);
401
402 return ops->set_speed ? ops->set_speed(phy, speed) : 0;
403}
404
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200405int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk)
406{
407 int i, ret, count;
Angus Ainslie606a14b2022-02-03 10:08:38 -0800408 struct udevice *phydev = dev;
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200409
410 bulk->count = 0;
411
412 /* Return if no phy declared */
Angus Ainslie606a14b2022-02-03 10:08:38 -0800413 if (!dev_read_prop(dev, "phys", NULL)) {
414 phydev = dev->parent;
415 if (!dev_read_prop(phydev, "phys", NULL)) {
416 pr_err("%s : no phys property\n", __func__);
417 return 0;
418 }
419 }
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200420
Angus Ainslie606a14b2022-02-03 10:08:38 -0800421 count = dev_count_phandle_with_args(phydev, "phys", "#phy-cells", 0);
422 if (count < 1) {
423 pr_err("%s : no phys found %d\n", __func__, count);
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200424 return count;
Angus Ainslie606a14b2022-02-03 10:08:38 -0800425 }
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200426
Angus Ainslie606a14b2022-02-03 10:08:38 -0800427 bulk->phys = devm_kcalloc(phydev, count, sizeof(struct phy), GFP_KERNEL);
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200428 if (!bulk->phys)
429 return -ENOMEM;
430
431 for (i = 0; i < count; i++) {
Angus Ainslie606a14b2022-02-03 10:08:38 -0800432 ret = generic_phy_get_by_index(phydev, i, &bulk->phys[i]);
Chunfeng Yunb13307b2020-05-02 11:35:11 +0200433 if (ret) {
434 pr_err("Failed to get PHY%d for %s\n", i, dev->name);
435 return ret;
436 }
437 bulk->count++;
438 }
439
440 return 0;
441}
442
443int generic_phy_init_bulk(struct phy_bulk *bulk)
444{
445 struct phy *phys = bulk->phys;
446 int i, ret;
447
448 for (i = 0; i < bulk->count; i++) {
449 ret = generic_phy_init(&phys[i]);
450 if (ret) {
451 pr_err("Can't init PHY%d\n", i);
452 goto phys_init_err;
453 }
454 }
455
456 return 0;
457
458phys_init_err:
459 for (; i > 0; i--)
460 generic_phy_exit(&phys[i - 1]);
461
462 return ret;
463}
464
465int generic_phy_exit_bulk(struct phy_bulk *bulk)
466{
467 struct phy *phys = bulk->phys;
468 int i, ret = 0;
469
470 for (i = 0; i < bulk->count; i++)
471 ret |= generic_phy_exit(&phys[i]);
472
473 return ret;
474}
475
476int generic_phy_power_on_bulk(struct phy_bulk *bulk)
477{
478 struct phy *phys = bulk->phys;
479 int i, ret;
480
481 for (i = 0; i < bulk->count; i++) {
482 ret = generic_phy_power_on(&phys[i]);
483 if (ret) {
484 pr_err("Can't power on PHY%d\n", i);
485 goto phys_poweron_err;
486 }
487 }
488
489 return 0;
490
491phys_poweron_err:
492 for (; i > 0; i--)
493 generic_phy_power_off(&phys[i - 1]);
494
495 return ret;
496}
497
498int generic_phy_power_off_bulk(struct phy_bulk *bulk)
499{
500 struct phy *phys = bulk->phys;
501 int i, ret = 0;
502
503 for (i = 0; i < bulk->count; i++)
504 ret |= generic_phy_power_off(&phys[i]);
505
506 return ret;
507}
508
Patrice Chotard84e56142022-09-06 08:15:26 +0200509int generic_setup_phy(struct udevice *dev, struct phy *phy, int index)
510{
511 int ret = 0;
512
513 if (!phy)
514 return 0;
515
516 ret = generic_phy_get_by_index(dev, index, phy);
517 if (ret) {
518 if (ret != -ENOENT)
519 return ret;
520 } else {
521 ret = generic_phy_init(phy);
522 if (ret)
523 return ret;
524
525 ret = generic_phy_power_on(phy);
526 if (ret)
527 ret = generic_phy_exit(phy);
528 }
529
530 return ret;
531}
532
533int generic_shutdown_phy(struct phy *phy)
534{
535 int ret = 0;
536
537 if (!phy)
538 return 0;
539
540 if (generic_phy_valid(phy)) {
541 ret = generic_phy_power_off(phy);
542 if (ret)
543 return ret;
544
545 ret = generic_phy_exit(phy);
546 }
547
548 return ret;
549}
550
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200551UCLASS_DRIVER(phy) = {
552 .id = UCLASS_PHY,
553 .name = "phy",
Alper Nebi Yasak226fce62021-12-30 22:36:51 +0300554 .pre_probe = phy_uclass_pre_probe,
555 .pre_remove = phy_uclass_pre_remove,
556 .per_device_auto = sizeof(struct list_head),
Jean-Jacques Hiblot72e50162017-04-24 11:51:27 +0200557};