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> |
Chunfeng Yun | b13307b | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 9 | #include <dm/devres.h> |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 10 | #include <generic-phy.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 12 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 13 | static inline struct phy_ops *phy_dev_ops(struct udevice *dev) |
| 14 | { |
| 15 | return (struct phy_ops *)dev->driver->ops; |
| 16 | } |
| 17 | |
| 18 | static int generic_phy_xlate_offs_flags(struct phy *phy, |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 19 | struct ofnode_phandle_args *args) |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 20 | { |
| 21 | debug("%s(phy=%p)\n", __func__, phy); |
| 22 | |
| 23 | if (args->args_count > 1) { |
| 24 | debug("Invaild args_count: %d\n", args->args_count); |
| 25 | return -EINVAL; |
| 26 | } |
| 27 | |
| 28 | if (args->args_count) |
| 29 | phy->id = args->args[0]; |
| 30 | else |
| 31 | phy->id = 0; |
| 32 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
Jagan Teki | 5a2b677 | 2020-05-01 23:44:18 +0530 | [diff] [blame] | 36 | int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy) |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 37 | { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 38 | struct ofnode_phandle_args args; |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 39 | struct phy_ops *ops; |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 40 | struct udevice *phydev; |
Patrice Chotard | a1b2fae | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 41 | int i, ret; |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 42 | |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 43 | debug("%s(node=%s, index=%d, phy=%p)\n", |
| 44 | __func__, ofnode_get_name(node), index, phy); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 45 | |
| 46 | assert(phy); |
Patrice Chotard | b9688df | 2017-07-18 11:38:42 +0200 | [diff] [blame] | 47 | phy->dev = NULL; |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 48 | ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0, |
| 49 | index, &args); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 50 | if (ret) { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 51 | debug("%s: dev_read_phandle_with_args failed: err=%d\n", |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 52 | __func__, ret); |
| 53 | return ret; |
| 54 | } |
| 55 | |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 56 | ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 57 | if (ret) { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 58 | debug("%s: uclass_get_device_by_ofnode failed: err=%d\n", |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 59 | __func__, ret); |
Patrice Chotard | a1b2fae | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 60 | |
| 61 | /* Check if args.node's parent is a PHY provider */ |
| 62 | ret = uclass_get_device_by_ofnode(UCLASS_PHY, |
| 63 | ofnode_get_parent(args.node), |
| 64 | &phydev); |
| 65 | if (ret) |
| 66 | return ret; |
| 67 | |
| 68 | /* insert phy idx at first position into args array */ |
Marek Vasut | 5e50adf | 2018-08-07 12:24:35 +0200 | [diff] [blame] | 69 | for (i = args.args_count; i >= 1 ; i--) |
Patrice Chotard | a1b2fae | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 70 | args.args[i] = args.args[i - 1]; |
| 71 | |
| 72 | args.args_count++; |
| 73 | args.args[0] = ofnode_read_u32_default(args.node, "reg", -1); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | phy->dev = phydev; |
| 77 | |
| 78 | ops = phy_dev_ops(phydev); |
| 79 | |
| 80 | if (ops->of_xlate) |
| 81 | ret = ops->of_xlate(phy, &args); |
| 82 | else |
| 83 | ret = generic_phy_xlate_offs_flags(phy, &args); |
| 84 | if (ret) { |
| 85 | debug("of_xlate() failed: %d\n", ret); |
| 86 | goto err; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | |
| 91 | err: |
| 92 | return ret; |
| 93 | } |
| 94 | |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 95 | int generic_phy_get_by_index(struct udevice *dev, int index, |
| 96 | struct phy *phy) |
| 97 | { |
Jagan Teki | 5a2b677 | 2020-05-01 23:44:18 +0530 | [diff] [blame] | 98 | return generic_phy_get_by_index_nodev(dev_ofnode(dev), index, phy); |
Neil Armstrong | c2b9aa9 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 101 | int generic_phy_get_by_name(struct udevice *dev, const char *phy_name, |
| 102 | struct phy *phy) |
| 103 | { |
| 104 | int index; |
| 105 | |
| 106 | debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy); |
| 107 | |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 108 | index = dev_read_stringlist_search(dev, "phy-names", phy_name); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 109 | if (index < 0) { |
Simon Glass | 23558bb | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 110 | debug("dev_read_stringlist_search() failed: %d\n", index); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 111 | return index; |
| 112 | } |
| 113 | |
| 114 | return generic_phy_get_by_index(dev, index, phy); |
| 115 | } |
| 116 | |
| 117 | int generic_phy_init(struct phy *phy) |
| 118 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 119 | struct phy_ops const *ops; |
| 120 | |
Vignesh Raghavendra | 64b69f8 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 121 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 124 | |
| 125 | return ops->init ? ops->init(phy) : 0; |
| 126 | } |
| 127 | |
| 128 | int generic_phy_reset(struct phy *phy) |
| 129 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 130 | struct phy_ops const *ops; |
| 131 | |
Vignesh Raghavendra | 64b69f8 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 132 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 135 | |
| 136 | return ops->reset ? ops->reset(phy) : 0; |
| 137 | } |
| 138 | |
| 139 | int generic_phy_exit(struct phy *phy) |
| 140 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 141 | struct phy_ops const *ops; |
| 142 | |
Vignesh Raghavendra | 64b69f8 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 143 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 144 | return 0; |
| 145 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 146 | |
| 147 | return ops->exit ? ops->exit(phy) : 0; |
| 148 | } |
| 149 | |
| 150 | int generic_phy_power_on(struct phy *phy) |
| 151 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 152 | struct phy_ops const *ops; |
| 153 | |
Vignesh Raghavendra | 64b69f8 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 154 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 157 | |
| 158 | return ops->power_on ? ops->power_on(phy) : 0; |
| 159 | } |
| 160 | |
| 161 | int generic_phy_power_off(struct phy *phy) |
| 162 | { |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 163 | struct phy_ops const *ops; |
| 164 | |
Vignesh Raghavendra | 64b69f8 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 165 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | 4e18429 | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 166 | return 0; |
| 167 | ops = phy_dev_ops(phy->dev); |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 168 | |
| 169 | return ops->power_off ? ops->power_off(phy) : 0; |
| 170 | } |
| 171 | |
Chunfeng Yun | b13307b | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 172 | int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk) |
| 173 | { |
| 174 | int i, ret, count; |
| 175 | |
| 176 | bulk->count = 0; |
| 177 | |
| 178 | /* Return if no phy declared */ |
| 179 | if (!dev_read_prop(dev, "phys", NULL)) |
| 180 | return 0; |
| 181 | |
| 182 | count = dev_count_phandle_with_args(dev, "phys", "#phy-cells"); |
| 183 | if (count < 1) |
| 184 | return count; |
| 185 | |
| 186 | bulk->phys = devm_kcalloc(dev, count, sizeof(struct phy), GFP_KERNEL); |
| 187 | if (!bulk->phys) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | for (i = 0; i < count; i++) { |
| 191 | ret = generic_phy_get_by_index(dev, i, &bulk->phys[i]); |
| 192 | if (ret) { |
| 193 | pr_err("Failed to get PHY%d for %s\n", i, dev->name); |
| 194 | return ret; |
| 195 | } |
| 196 | bulk->count++; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | int generic_phy_init_bulk(struct phy_bulk *bulk) |
| 203 | { |
| 204 | struct phy *phys = bulk->phys; |
| 205 | int i, ret; |
| 206 | |
| 207 | for (i = 0; i < bulk->count; i++) { |
| 208 | ret = generic_phy_init(&phys[i]); |
| 209 | if (ret) { |
| 210 | pr_err("Can't init PHY%d\n", i); |
| 211 | goto phys_init_err; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | |
| 217 | phys_init_err: |
| 218 | for (; i > 0; i--) |
| 219 | generic_phy_exit(&phys[i - 1]); |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | int generic_phy_exit_bulk(struct phy_bulk *bulk) |
| 225 | { |
| 226 | struct phy *phys = bulk->phys; |
| 227 | int i, ret = 0; |
| 228 | |
| 229 | for (i = 0; i < bulk->count; i++) |
| 230 | ret |= generic_phy_exit(&phys[i]); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | int generic_phy_power_on_bulk(struct phy_bulk *bulk) |
| 236 | { |
| 237 | struct phy *phys = bulk->phys; |
| 238 | int i, ret; |
| 239 | |
| 240 | for (i = 0; i < bulk->count; i++) { |
| 241 | ret = generic_phy_power_on(&phys[i]); |
| 242 | if (ret) { |
| 243 | pr_err("Can't power on PHY%d\n", i); |
| 244 | goto phys_poweron_err; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | |
| 250 | phys_poweron_err: |
| 251 | for (; i > 0; i--) |
| 252 | generic_phy_power_off(&phys[i - 1]); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | int generic_phy_power_off_bulk(struct phy_bulk *bulk) |
| 258 | { |
| 259 | struct phy *phys = bulk->phys; |
| 260 | int i, ret = 0; |
| 261 | |
| 262 | for (i = 0; i < bulk->count; i++) |
| 263 | ret |= generic_phy_power_off(&phys[i]); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
Jean-Jacques Hiblot | 72e5016 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 268 | UCLASS_DRIVER(phy) = { |
| 269 | .id = UCLASS_PHY, |
| 270 | .name = "phy", |
| 271 | }; |