1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
7 #define LOG_CATEGORY UCLASS_PHY
11 #include <dm/device_compat.h>
12 #include <dm/devres.h>
13 #include <generic-phy.h>
14 #include <linux/list.h>
17 * struct phy_counts - Init and power-on counts of a single PHY port
19 * This structure is used to keep track of PHY initialization and power
20 * state change requests, so that we don't power off and deinitialize a
21 * PHY instance until all of its users want it done. Otherwise, multiple
22 * consumers using the same PHY port can cause problems (e.g. one might
23 * call power_off() after another's exit() and hang indefinitely).
25 * @id: The PHY ID within a PHY provider
26 * @power_on_count: Times generic_phy_power_on() was called for this ID
27 * without a matching generic_phy_power_off() afterwards
28 * @init_count: Times generic_phy_init() was called for this ID
29 * without a matching generic_phy_exit() afterwards
30 * @list: Handle for a linked list of these structures corresponding to
31 * ports of the same PHY provider
37 struct list_head list;
40 static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
42 return (struct phy_ops *)dev->driver->ops;
45 static struct phy_counts *phy_get_counts(struct phy *phy)
47 struct list_head *uc_priv;
48 struct phy_counts *counts;
50 if (!generic_phy_valid(phy))
53 uc_priv = dev_get_uclass_priv(phy->dev);
54 list_for_each_entry(counts, uc_priv, list)
55 if (counts->id == phy->id)
61 static int phy_alloc_counts(struct phy *phy)
63 struct list_head *uc_priv;
64 struct phy_counts *counts;
66 if (!generic_phy_valid(phy))
68 if (phy_get_counts(phy))
71 uc_priv = dev_get_uclass_priv(phy->dev);
72 counts = kzalloc(sizeof(*counts), GFP_KERNEL);
77 counts->power_on_count = 0;
78 counts->init_count = 0;
79 list_add(&counts->list, uc_priv);
84 static int phy_uclass_pre_probe(struct udevice *dev)
86 struct list_head *uc_priv = dev_get_uclass_priv(dev);
88 INIT_LIST_HEAD(uc_priv);
93 static int phy_uclass_pre_remove(struct udevice *dev)
95 struct list_head *uc_priv = dev_get_uclass_priv(dev);
96 struct phy_counts *counts, *next;
98 list_for_each_entry_safe(counts, next, uc_priv, list)
104 static int generic_phy_xlate_offs_flags(struct phy *phy,
105 struct ofnode_phandle_args *args)
107 debug("%s(phy=%p)\n", __func__, phy);
109 if (args->args_count > 1) {
110 debug("Invalid args_count: %d\n", args->args_count);
114 if (args->args_count)
115 phy->id = args->args[0];
122 int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy)
124 struct ofnode_phandle_args args;
126 struct udevice *phydev;
129 debug("%s(node=%s, index=%d, phy=%p)\n",
130 __func__, ofnode_get_name(node), index, phy);
134 ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0,
137 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
142 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
144 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
147 /* Check if args.node's parent is a PHY provider */
148 ret = uclass_get_device_by_ofnode(UCLASS_PHY,
149 ofnode_get_parent(args.node),
154 /* insert phy idx at first position into args array */
155 for (i = args.args_count; i >= 1 ; i--)
156 args.args[i] = args.args[i - 1];
159 args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
164 ops = phy_dev_ops(phydev);
167 ret = ops->of_xlate(phy, &args);
169 ret = generic_phy_xlate_offs_flags(phy, &args);
171 debug("of_xlate() failed: %d\n", ret);
175 ret = phy_alloc_counts(phy);
177 debug("phy_alloc_counts() failed: %d\n", ret);
187 int generic_phy_get_by_index(struct udevice *dev, int index,
190 return generic_phy_get_by_index_nodev(dev_ofnode(dev), index, phy);
193 int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
198 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
200 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
202 debug("dev_read_stringlist_search() failed: %d\n", index);
206 return generic_phy_get_by_index(dev, index, phy);
209 int generic_phy_init(struct phy *phy)
211 struct phy_counts *counts;
212 struct phy_ops const *ops;
215 if (!generic_phy_valid(phy))
217 ops = phy_dev_ops(phy->dev);
221 counts = phy_get_counts(phy);
222 if (counts->init_count > 0) {
223 counts->init_count++;
227 ret = ops->init(phy);
229 dev_err(phy->dev, "PHY: Failed to init %s: %d.\n",
230 phy->dev->name, ret);
232 counts->init_count = 1;
237 int generic_phy_reset(struct phy *phy)
239 struct phy_ops const *ops;
242 if (!generic_phy_valid(phy))
244 ops = phy_dev_ops(phy->dev);
247 ret = ops->reset(phy);
249 dev_err(phy->dev, "PHY: Failed to reset %s: %d.\n",
250 phy->dev->name, ret);
255 int generic_phy_exit(struct phy *phy)
257 struct phy_counts *counts;
258 struct phy_ops const *ops;
261 if (!generic_phy_valid(phy))
263 ops = phy_dev_ops(phy->dev);
267 counts = phy_get_counts(phy);
268 if (counts->init_count == 0)
270 if (counts->init_count > 1) {
271 counts->init_count--;
275 ret = ops->exit(phy);
277 dev_err(phy->dev, "PHY: Failed to exit %s: %d.\n",
278 phy->dev->name, ret);
280 counts->init_count = 0;
285 int generic_phy_power_on(struct phy *phy)
287 struct phy_counts *counts;
288 struct phy_ops const *ops;
291 if (!generic_phy_valid(phy))
293 ops = phy_dev_ops(phy->dev);
297 counts = phy_get_counts(phy);
298 if (counts->power_on_count > 0) {
299 counts->power_on_count++;
303 ret = ops->power_on(phy);
305 dev_err(phy->dev, "PHY: Failed to power on %s: %d.\n",
306 phy->dev->name, ret);
308 counts->power_on_count = 1;
313 int generic_phy_power_off(struct phy *phy)
315 struct phy_counts *counts;
316 struct phy_ops const *ops;
319 if (!generic_phy_valid(phy))
321 ops = phy_dev_ops(phy->dev);
325 counts = phy_get_counts(phy);
326 if (counts->power_on_count == 0)
328 if (counts->power_on_count > 1) {
329 counts->power_on_count--;
333 ret = ops->power_off(phy);
335 dev_err(phy->dev, "PHY: Failed to power off %s: %d.\n",
336 phy->dev->name, ret);
338 counts->power_on_count = 0;
343 int generic_phy_configure(struct phy *phy, void *params)
345 struct phy_ops const *ops;
347 if (!generic_phy_valid(phy))
349 ops = phy_dev_ops(phy->dev);
351 return ops->configure ? ops->configure(phy, params) : 0;
354 int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
356 struct phy_ops const *ops;
358 if (!generic_phy_valid(phy))
360 ops = phy_dev_ops(phy->dev);
362 return ops->set_mode ? ops->set_mode(phy, mode, submode) : 0;
365 int generic_phy_set_speed(struct phy *phy, int speed)
367 struct phy_ops const *ops;
369 if (!generic_phy_valid(phy))
371 ops = phy_dev_ops(phy->dev);
373 return ops->set_speed ? ops->set_speed(phy, speed) : 0;
376 int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk)
379 struct udevice *phydev = dev;
383 /* Return if no phy declared */
384 if (!dev_read_prop(dev, "phys", NULL)) {
385 phydev = dev->parent;
386 if (!dev_read_prop(phydev, "phys", NULL)) {
387 pr_err("%s : no phys property\n", __func__);
392 count = dev_count_phandle_with_args(phydev, "phys", "#phy-cells", 0);
394 pr_err("%s : no phys found %d\n", __func__, count);
398 bulk->phys = devm_kcalloc(phydev, count, sizeof(struct phy), GFP_KERNEL);
402 for (i = 0; i < count; i++) {
403 ret = generic_phy_get_by_index(phydev, i, &bulk->phys[i]);
405 pr_err("Failed to get PHY%d for %s\n", i, dev->name);
414 int generic_phy_init_bulk(struct phy_bulk *bulk)
416 struct phy *phys = bulk->phys;
419 for (i = 0; i < bulk->count; i++) {
420 ret = generic_phy_init(&phys[i]);
422 pr_err("Can't init PHY%d\n", i);
431 generic_phy_exit(&phys[i - 1]);
436 int generic_phy_exit_bulk(struct phy_bulk *bulk)
438 struct phy *phys = bulk->phys;
441 for (i = 0; i < bulk->count; i++)
442 ret |= generic_phy_exit(&phys[i]);
447 int generic_phy_power_on_bulk(struct phy_bulk *bulk)
449 struct phy *phys = bulk->phys;
452 for (i = 0; i < bulk->count; i++) {
453 ret = generic_phy_power_on(&phys[i]);
455 pr_err("Can't power on PHY%d\n", i);
456 goto phys_poweron_err;
464 generic_phy_power_off(&phys[i - 1]);
469 int generic_phy_power_off_bulk(struct phy_bulk *bulk)
471 struct phy *phys = bulk->phys;
474 for (i = 0; i < bulk->count; i++)
475 ret |= generic_phy_power_off(&phys[i]);
480 int generic_setup_phy(struct udevice *dev, struct phy *phy, int index)
487 ret = generic_phy_get_by_index(dev, index, phy);
492 ret = generic_phy_init(phy);
496 ret = generic_phy_power_on(phy);
498 ret = generic_phy_exit(phy);
504 int generic_shutdown_phy(struct phy *phy)
511 if (generic_phy_valid(phy)) {
512 ret = generic_phy_power_off(phy);
516 ret = generic_phy_exit(phy);
522 UCLASS_DRIVER(phy) = {
525 .pre_probe = phy_uclass_pre_probe,
526 .pre_remove = phy_uclass_pre_remove,
527 .per_device_auto = sizeof(struct list_head),