1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
4 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
9 #include <generic-phy.h>
11 static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
13 return (struct phy_ops *)dev->driver->ops;
16 static int generic_phy_xlate_offs_flags(struct phy *phy,
17 struct ofnode_phandle_args *args)
19 debug("%s(phy=%p)\n", __func__, phy);
21 if (args->args_count > 1) {
22 debug("Invaild args_count: %d\n", args->args_count);
27 phy->id = args->args[0];
34 int generic_phy_get_by_index(struct udevice *dev, int index,
37 struct ofnode_phandle_args args;
39 struct udevice *phydev;
42 debug("%s(dev=%p, index=%d, phy=%p)\n", __func__, dev, index, phy);
46 ret = dev_read_phandle_with_args(dev, "phys", "#phy-cells", 0, index,
49 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
54 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
56 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
59 /* Check if args.node's parent is a PHY provider */
60 ret = uclass_get_device_by_ofnode(UCLASS_PHY,
61 ofnode_get_parent(args.node),
66 /* insert phy idx at first position into args array */
67 for (i = args.args_count; i >= 1 ; i--)
68 args.args[i] = args.args[i - 1];
71 args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
76 ops = phy_dev_ops(phydev);
79 ret = ops->of_xlate(phy, &args);
81 ret = generic_phy_xlate_offs_flags(phy, &args);
83 debug("of_xlate() failed: %d\n", ret);
93 int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
98 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
100 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
102 debug("dev_read_stringlist_search() failed: %d\n", index);
106 return generic_phy_get_by_index(dev, index, phy);
109 int generic_phy_init(struct phy *phy)
111 struct phy_ops const *ops = phy_dev_ops(phy->dev);
113 return ops->init ? ops->init(phy) : 0;
116 int generic_phy_reset(struct phy *phy)
118 struct phy_ops const *ops = phy_dev_ops(phy->dev);
120 return ops->reset ? ops->reset(phy) : 0;
123 int generic_phy_exit(struct phy *phy)
125 struct phy_ops const *ops = phy_dev_ops(phy->dev);
127 return ops->exit ? ops->exit(phy) : 0;
130 int generic_phy_power_on(struct phy *phy)
132 struct phy_ops const *ops = phy_dev_ops(phy->dev);
134 return ops->power_on ? ops->power_on(phy) : 0;
137 int generic_phy_power_off(struct phy *phy)
139 struct phy_ops const *ops = phy_dev_ops(phy->dev);
141 return ops->power_off ? ops->power_off(phy) : 0;
144 UCLASS_DRIVER(phy) = {