Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/ |
| 4 | * Written by Jean-Jacques Hiblot <jjhiblot@ti.com> |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <generic-phy.h> |
| 10 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 11 | static inline struct phy_ops *phy_dev_ops(struct udevice *dev) |
| 12 | { |
| 13 | return (struct phy_ops *)dev->driver->ops; |
| 14 | } |
| 15 | |
| 16 | static int generic_phy_xlate_offs_flags(struct phy *phy, |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 17 | struct ofnode_phandle_args *args) |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 18 | { |
| 19 | debug("%s(phy=%p)\n", __func__, phy); |
| 20 | |
| 21 | if (args->args_count > 1) { |
| 22 | debug("Invaild args_count: %d\n", args->args_count); |
| 23 | return -EINVAL; |
| 24 | } |
| 25 | |
| 26 | if (args->args_count) |
| 27 | phy->id = args->args[0]; |
| 28 | else |
| 29 | phy->id = 0; |
| 30 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 31 | return 0; |
| 32 | } |
| 33 | |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 34 | int generic_phy_get_by_node(ofnode node, int index, struct phy *phy) |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 35 | { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 36 | struct ofnode_phandle_args args; |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 37 | struct phy_ops *ops; |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 38 | struct udevice *phydev; |
Patrice Chotard | a1b2fae | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 39 | int i, ret; |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 40 | |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 41 | debug("%s(node=%s, index=%d, phy=%p)\n", |
| 42 | __func__, ofnode_get_name(node), index, phy); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 43 | |
| 44 | assert(phy); |
Patrice Chotard | b9688df | 2017-07-18 11:38:42 +0200 | [diff] [blame] | 45 | phy->dev = NULL; |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 46 | ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0, |
| 47 | index, &args); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 48 | if (ret) { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 49 | debug("%s: dev_read_phandle_with_args failed: err=%d\n", |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 50 | __func__, ret); |
| 51 | return ret; |
| 52 | } |
| 53 | |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 54 | ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 55 | if (ret) { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 56 | debug("%s: uclass_get_device_by_ofnode failed: err=%d\n", |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 57 | __func__, ret); |
Patrice Chotard | a1b2fae | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 58 | |
| 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), |
| 62 | &phydev); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
| 66 | /* insert phy idx at first position into args array */ |
Marek Vasut | 5e50adf | 2018-08-07 12:24:35 +0200 | [diff] [blame] | 67 | for (i = args.args_count; i >= 1 ; i--) |
Patrice Chotard | a1b2fae | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 68 | args.args[i] = args.args[i - 1]; |
| 69 | |
| 70 | args.args_count++; |
| 71 | args.args[0] = ofnode_read_u32_default(args.node, "reg", -1); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | phy->dev = phydev; |
| 75 | |
| 76 | ops = phy_dev_ops(phydev); |
| 77 | |
| 78 | if (ops->of_xlate) |
| 79 | ret = ops->of_xlate(phy, &args); |
| 80 | else |
| 81 | ret = generic_phy_xlate_offs_flags(phy, &args); |
| 82 | if (ret) { |
| 83 | debug("of_xlate() failed: %d\n", ret); |
| 84 | goto err; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | |
| 89 | err: |
| 90 | return ret; |
| 91 | } |
| 92 | |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 93 | int generic_phy_get_by_index(struct udevice *dev, int index, |
| 94 | struct phy *phy) |
| 95 | { |
| 96 | return generic_phy_get_by_node(dev_ofnode(dev), index, phy); |
| 97 | } |
| 98 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 99 | int generic_phy_get_by_name(struct udevice *dev, const char *phy_name, |
| 100 | struct phy *phy) |
| 101 | { |
| 102 | int index; |
| 103 | |
| 104 | debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy); |
| 105 | |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 106 | index = dev_read_stringlist_search(dev, "phy-names", phy_name); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 107 | if (index < 0) { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 108 | debug("dev_read_stringlist_search() failed: %d\n", index); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 109 | return index; |
| 110 | } |
| 111 | |
| 112 | return generic_phy_get_by_index(dev, index, phy); |
| 113 | } |
| 114 | |
| 115 | int generic_phy_init(struct phy *phy) |
| 116 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 117 | struct phy_ops const *ops; |
| 118 | |
| 119 | if (!phy) |
| 120 | return 0; |
| 121 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 122 | |
| 123 | return ops->init ? ops->init(phy) : 0; |
| 124 | } |
| 125 | |
| 126 | int generic_phy_reset(struct phy *phy) |
| 127 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 128 | struct phy_ops const *ops; |
| 129 | |
| 130 | if (!phy) |
| 131 | return 0; |
| 132 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 133 | |
| 134 | return ops->reset ? ops->reset(phy) : 0; |
| 135 | } |
| 136 | |
| 137 | int generic_phy_exit(struct phy *phy) |
| 138 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 139 | struct phy_ops const *ops; |
| 140 | |
| 141 | if (!phy) |
| 142 | return 0; |
| 143 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 144 | |
| 145 | return ops->exit ? ops->exit(phy) : 0; |
| 146 | } |
| 147 | |
| 148 | int generic_phy_power_on(struct phy *phy) |
| 149 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 150 | struct phy_ops const *ops; |
| 151 | |
| 152 | if (!phy) |
| 153 | return 0; |
| 154 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 155 | |
| 156 | return ops->power_on ? ops->power_on(phy) : 0; |
| 157 | } |
| 158 | |
| 159 | int generic_phy_power_off(struct phy *phy) |
| 160 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 161 | struct phy_ops const *ops; |
| 162 | |
| 163 | if (!phy) |
| 164 | return 0; |
| 165 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 166 | |
| 167 | return ops->power_off ? ops->power_off(phy) : 0; |
| 168 | } |
| 169 | |
| 170 | UCLASS_DRIVER(phy) = { |
| 171 | .id = UCLASS_PHY, |
| 172 | .name = "phy", |
| 173 | }; |