Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 2 | /* |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 3 | * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/ |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 4 | * Written by Jean-Jacques Hiblot <jjhiblot@ti.com> |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_PHY |
| 8 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
Sean Anderson | 03036a2 | 2020-10-04 21:39:47 -0400 | [diff] [blame] | 11 | #include <dm/device_compat.h> |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 12 | #include <dm/devres.h> |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 13 | #include <generic-phy.h> |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 14 | #include <linux/list.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 15 | #include <linux/printk.h> |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 16 | #include <power/regulator.h> |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * struct phy_counts - Init and power-on counts of a single PHY port |
| 20 | * |
| 21 | * This structure is used to keep track of PHY initialization and power |
| 22 | * state change requests, so that we don't power off and deinitialize a |
| 23 | * PHY instance until all of its users want it done. Otherwise, multiple |
| 24 | * consumers using the same PHY port can cause problems (e.g. one might |
| 25 | * call power_off() after another's exit() and hang indefinitely). |
| 26 | * |
| 27 | * @id: The PHY ID within a PHY provider |
| 28 | * @power_on_count: Times generic_phy_power_on() was called for this ID |
| 29 | * without a matching generic_phy_power_off() afterwards |
| 30 | * @init_count: Times generic_phy_init() was called for this ID |
| 31 | * without a matching generic_phy_exit() afterwards |
| 32 | * @list: Handle for a linked list of these structures corresponding to |
| 33 | * ports of the same PHY provider |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 34 | * @supply: Handle to a phy-supply device |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 35 | */ |
| 36 | struct phy_counts { |
| 37 | unsigned long id; |
| 38 | int power_on_count; |
| 39 | int init_count; |
| 40 | struct list_head list; |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 41 | struct udevice *supply; |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 42 | }; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 43 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 44 | static inline struct phy_ops *phy_dev_ops(struct udevice *dev) |
| 45 | { |
| 46 | return (struct phy_ops *)dev->driver->ops; |
| 47 | } |
| 48 | |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 49 | static struct phy_counts *phy_get_counts(struct phy *phy) |
| 50 | { |
| 51 | struct list_head *uc_priv; |
| 52 | struct phy_counts *counts; |
| 53 | |
| 54 | if (!generic_phy_valid(phy)) |
| 55 | return NULL; |
| 56 | |
| 57 | uc_priv = dev_get_uclass_priv(phy->dev); |
| 58 | list_for_each_entry(counts, uc_priv, list) |
| 59 | if (counts->id == phy->id) |
| 60 | return counts; |
| 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 65 | static int phy_alloc_counts(struct phy *phy, struct udevice *supply) |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 66 | { |
| 67 | struct list_head *uc_priv; |
| 68 | struct phy_counts *counts; |
| 69 | |
| 70 | if (!generic_phy_valid(phy)) |
| 71 | return 0; |
| 72 | if (phy_get_counts(phy)) |
| 73 | return 0; |
| 74 | |
| 75 | uc_priv = dev_get_uclass_priv(phy->dev); |
| 76 | counts = kzalloc(sizeof(*counts), GFP_KERNEL); |
| 77 | if (!counts) |
| 78 | return -ENOMEM; |
| 79 | |
| 80 | counts->id = phy->id; |
| 81 | counts->power_on_count = 0; |
| 82 | counts->init_count = 0; |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 83 | counts->supply = supply; |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 84 | list_add(&counts->list, uc_priv); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int phy_uclass_pre_probe(struct udevice *dev) |
| 90 | { |
| 91 | struct list_head *uc_priv = dev_get_uclass_priv(dev); |
| 92 | |
| 93 | INIT_LIST_HEAD(uc_priv); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int phy_uclass_pre_remove(struct udevice *dev) |
| 99 | { |
| 100 | struct list_head *uc_priv = dev_get_uclass_priv(dev); |
| 101 | struct phy_counts *counts, *next; |
| 102 | |
| 103 | list_for_each_entry_safe(counts, next, uc_priv, list) |
| 104 | kfree(counts); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 109 | static int generic_phy_xlate_offs_flags(struct phy *phy, |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 110 | struct ofnode_phandle_args *args) |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 111 | { |
| 112 | debug("%s(phy=%p)\n", __func__, phy); |
| 113 | |
| 114 | if (args->args_count > 1) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 115 | debug("Invalid args_count: %d\n", args->args_count); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | if (args->args_count) |
| 120 | phy->id = args->args[0]; |
| 121 | else |
| 122 | phy->id = 0; |
| 123 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Jagan Teki | a4e8eee | 2020-05-01 23:44:18 +0530 | [diff] [blame] | 127 | int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy) |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 128 | { |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 129 | struct ofnode_phandle_args args; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 130 | struct phy_ops *ops; |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 131 | struct udevice *phydev, *supply = NULL; |
Patrice Chotard | cf65fa4 | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 132 | int i, ret; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 133 | |
Neil Armstrong | 8ab77e3 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 134 | debug("%s(node=%s, index=%d, phy=%p)\n", |
| 135 | __func__, ofnode_get_name(node), index, phy); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 136 | |
| 137 | assert(phy); |
Patrice Chotard | 956b7ad | 2017-07-18 11:38:42 +0200 | [diff] [blame] | 138 | phy->dev = NULL; |
Neil Armstrong | 8ab77e3 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 139 | ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0, |
| 140 | index, &args); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 141 | if (ret) { |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 142 | debug("%s: dev_read_phandle_with_args failed: err=%d\n", |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 143 | __func__, ret); |
| 144 | return ret; |
| 145 | } |
| 146 | |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 147 | ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 148 | if (ret) { |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 149 | debug("%s: uclass_get_device_by_ofnode failed: err=%d\n", |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 150 | __func__, ret); |
Patrice Chotard | cf65fa4 | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 151 | |
| 152 | /* Check if args.node's parent is a PHY provider */ |
| 153 | ret = uclass_get_device_by_ofnode(UCLASS_PHY, |
| 154 | ofnode_get_parent(args.node), |
| 155 | &phydev); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | /* insert phy idx at first position into args array */ |
Marek Vasut | 61b17ed | 2018-08-07 12:24:35 +0200 | [diff] [blame] | 160 | for (i = args.args_count; i >= 1 ; i--) |
Patrice Chotard | cf65fa4 | 2018-06-27 11:55:42 +0200 | [diff] [blame] | 161 | args.args[i] = args.args[i - 1]; |
| 162 | |
| 163 | args.args_count++; |
| 164 | args.args[0] = ofnode_read_u32_default(args.node, "reg", -1); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | phy->dev = phydev; |
| 168 | |
| 169 | ops = phy_dev_ops(phydev); |
| 170 | |
| 171 | if (ops->of_xlate) |
| 172 | ret = ops->of_xlate(phy, &args); |
| 173 | else |
| 174 | ret = generic_phy_xlate_offs_flags(phy, &args); |
| 175 | if (ret) { |
| 176 | debug("of_xlate() failed: %d\n", ret); |
| 177 | goto err; |
| 178 | } |
| 179 | |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 180 | if (CONFIG_IS_ENABLED(DM_REGULATOR)) { |
| 181 | ret = device_get_supply_regulator(phydev, "phy-supply", |
| 182 | &supply); |
| 183 | if (ret && ret != -ENOENT) { |
| 184 | debug("%s: device_get_supply_regulator failed: %d\n", |
| 185 | __func__, ret); |
| 186 | goto err; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | ret = phy_alloc_counts(phy, supply); |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 191 | if (ret) { |
| 192 | debug("phy_alloc_counts() failed: %d\n", ret); |
| 193 | goto err; |
| 194 | } |
| 195 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 196 | return 0; |
| 197 | |
| 198 | err: |
Jonas Karlman | 9f89e68 | 2023-08-31 22:16:35 +0000 | [diff] [blame] | 199 | phy->dev = NULL; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
Neil Armstrong | 8ab77e3 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 203 | int generic_phy_get_by_index(struct udevice *dev, int index, |
| 204 | struct phy *phy) |
| 205 | { |
Jagan Teki | a4e8eee | 2020-05-01 23:44:18 +0530 | [diff] [blame] | 206 | return generic_phy_get_by_index_nodev(dev_ofnode(dev), index, phy); |
Neil Armstrong | 8ab77e3 | 2020-03-30 11:27:23 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 209 | int generic_phy_get_by_name(struct udevice *dev, const char *phy_name, |
| 210 | struct phy *phy) |
| 211 | { |
| 212 | int index; |
| 213 | |
| 214 | debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy); |
| 215 | |
Jonas Karlman | ffe06b4 | 2023-08-31 22:16:33 +0000 | [diff] [blame] | 216 | assert(phy); |
| 217 | phy->dev = NULL; |
| 218 | |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 219 | index = dev_read_stringlist_search(dev, "phy-names", phy_name); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 220 | if (index < 0) { |
Simon Glass | 8195551 | 2017-05-18 20:09:47 -0600 | [diff] [blame] | 221 | debug("dev_read_stringlist_search() failed: %d\n", index); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 222 | return index; |
| 223 | } |
| 224 | |
| 225 | return generic_phy_get_by_index(dev, index, phy); |
| 226 | } |
| 227 | |
| 228 | int generic_phy_init(struct phy *phy) |
| 229 | { |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 230 | struct phy_counts *counts; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 231 | struct phy_ops const *ops; |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 232 | int ret; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 233 | |
Vignesh Raghavendra | 62bd5b1 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 234 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 235 | return 0; |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 236 | counts = phy_get_counts(phy); |
| 237 | if (counts->init_count > 0) { |
| 238 | counts->init_count++; |
| 239 | return 0; |
| 240 | } |
| 241 | |
Jonas Karlman | c2cd02e | 2023-05-15 12:59:50 +0300 | [diff] [blame] | 242 | ops = phy_dev_ops(phy->dev); |
| 243 | if (ops->init) { |
| 244 | ret = ops->init(phy); |
| 245 | if (ret) { |
| 246 | dev_err(phy->dev, "PHY: Failed to init %s: %d.\n", |
| 247 | phy->dev->name, ret); |
| 248 | return ret; |
| 249 | } |
| 250 | } |
| 251 | counts->init_count = 1; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 252 | |
Jonas Karlman | c2cd02e | 2023-05-15 12:59:50 +0300 | [diff] [blame] | 253 | return 0; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | int generic_phy_reset(struct phy *phy) |
| 257 | { |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 258 | struct phy_ops const *ops; |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 259 | int ret; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 260 | |
Vignesh Raghavendra | 62bd5b1 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 261 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 262 | return 0; |
| 263 | ops = phy_dev_ops(phy->dev); |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 264 | if (!ops->reset) |
| 265 | return 0; |
| 266 | ret = ops->reset(phy); |
| 267 | if (ret) |
| 268 | dev_err(phy->dev, "PHY: Failed to reset %s: %d.\n", |
| 269 | phy->dev->name, ret); |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 270 | |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 271 | return ret; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int generic_phy_exit(struct phy *phy) |
| 275 | { |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 276 | struct phy_counts *counts; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 277 | struct phy_ops const *ops; |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 278 | int ret; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 279 | |
Vignesh Raghavendra | 62bd5b1 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 280 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 281 | return 0; |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 282 | counts = phy_get_counts(phy); |
| 283 | if (counts->init_count == 0) |
| 284 | return 0; |
| 285 | if (counts->init_count > 1) { |
| 286 | counts->init_count--; |
| 287 | return 0; |
| 288 | } |
| 289 | |
Jonas Karlman | c2cd02e | 2023-05-15 12:59:50 +0300 | [diff] [blame] | 290 | ops = phy_dev_ops(phy->dev); |
| 291 | if (ops->exit) { |
| 292 | ret = ops->exit(phy); |
| 293 | if (ret) { |
| 294 | dev_err(phy->dev, "PHY: Failed to exit %s: %d.\n", |
| 295 | phy->dev->name, ret); |
| 296 | return ret; |
| 297 | } |
| 298 | } |
| 299 | counts->init_count = 0; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 300 | |
Jonas Karlman | c2cd02e | 2023-05-15 12:59:50 +0300 | [diff] [blame] | 301 | return 0; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | int generic_phy_power_on(struct phy *phy) |
| 305 | { |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 306 | struct phy_counts *counts; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 307 | struct phy_ops const *ops; |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 308 | int ret; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 309 | |
Vignesh Raghavendra | 62bd5b1 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 310 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 311 | return 0; |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 312 | counts = phy_get_counts(phy); |
| 313 | if (counts->power_on_count > 0) { |
| 314 | counts->power_on_count++; |
| 315 | return 0; |
| 316 | } |
| 317 | |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 318 | ret = regulator_set_enable_if_allowed(counts->supply, true); |
| 319 | if (ret && ret != -ENOSYS) { |
| 320 | dev_err(phy->dev, "PHY: Failed to enable regulator %s: %d.\n", |
| 321 | counts->supply->name, ret); |
| 322 | return ret; |
| 323 | } |
| 324 | |
Jonas Karlman | c2cd02e | 2023-05-15 12:59:50 +0300 | [diff] [blame] | 325 | ops = phy_dev_ops(phy->dev); |
| 326 | if (ops->power_on) { |
| 327 | ret = ops->power_on(phy); |
| 328 | if (ret) { |
| 329 | dev_err(phy->dev, "PHY: Failed to power on %s: %d.\n", |
| 330 | phy->dev->name, ret); |
| 331 | regulator_set_enable_if_allowed(counts->supply, false); |
| 332 | return ret; |
| 333 | } |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 334 | } |
| 335 | counts->power_on_count = 1; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 336 | |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 337 | return 0; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | int generic_phy_power_off(struct phy *phy) |
| 341 | { |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 342 | struct phy_counts *counts; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 343 | struct phy_ops const *ops; |
Patrick Delaunay | c2e5efd | 2020-07-03 17:36:40 +0200 | [diff] [blame] | 344 | int ret; |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 345 | |
Vignesh Raghavendra | 62bd5b1 | 2020-05-20 22:35:41 +0530 | [diff] [blame] | 346 | if (!generic_phy_valid(phy)) |
Jean-Jacques Hiblot | eae1eeb | 2019-10-01 14:03:26 +0200 | [diff] [blame] | 347 | return 0; |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 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 | |
Jonas Karlman | c2cd02e | 2023-05-15 12:59:50 +0300 | [diff] [blame] | 356 | ops = phy_dev_ops(phy->dev); |
| 357 | if (ops->power_off) { |
| 358 | ret = ops->power_off(phy); |
| 359 | if (ret) { |
| 360 | dev_err(phy->dev, "PHY: Failed to power off %s: %d.\n", |
| 361 | phy->dev->name, ret); |
| 362 | return ret; |
| 363 | } |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 364 | } |
| 365 | counts->power_on_count = 0; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 366 | |
Eugen Hristev | 24ab68d | 2023-05-15 12:59:47 +0300 | [diff] [blame] | 367 | ret = regulator_set_enable_if_allowed(counts->supply, false); |
| 368 | if (ret && ret != -ENOSYS) |
| 369 | dev_err(phy->dev, "PHY: Failed to disable regulator %s: %d.\n", |
| 370 | counts->supply->name, ret); |
| 371 | |
| 372 | return 0; |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Neil Armstrong | 963ae6c | 2020-12-29 14:58:59 +0100 | [diff] [blame] | 375 | int generic_phy_configure(struct phy *phy, void *params) |
| 376 | { |
| 377 | struct phy_ops const *ops; |
| 378 | |
| 379 | if (!generic_phy_valid(phy)) |
| 380 | return 0; |
| 381 | ops = phy_dev_ops(phy->dev); |
| 382 | |
| 383 | return ops->configure ? ops->configure(phy, params) : 0; |
| 384 | } |
| 385 | |
Marek Vasut | 4332c09 | 2023-03-19 18:09:42 +0100 | [diff] [blame] | 386 | int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode) |
| 387 | { |
| 388 | struct phy_ops const *ops; |
| 389 | |
| 390 | if (!generic_phy_valid(phy)) |
| 391 | return 0; |
| 392 | ops = phy_dev_ops(phy->dev); |
| 393 | |
| 394 | return ops->set_mode ? ops->set_mode(phy, mode, submode) : 0; |
| 395 | } |
| 396 | |
| 397 | int generic_phy_set_speed(struct phy *phy, int speed) |
| 398 | { |
| 399 | struct phy_ops const *ops; |
| 400 | |
| 401 | if (!generic_phy_valid(phy)) |
| 402 | return 0; |
| 403 | ops = phy_dev_ops(phy->dev); |
| 404 | |
| 405 | return ops->set_speed ? ops->set_speed(phy, speed) : 0; |
| 406 | } |
| 407 | |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 408 | int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk) |
| 409 | { |
| 410 | int i, ret, count; |
Angus Ainslie | 3365fd3 | 2022-02-03 10:08:38 -0800 | [diff] [blame] | 411 | struct udevice *phydev = dev; |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 412 | |
| 413 | bulk->count = 0; |
| 414 | |
| 415 | /* Return if no phy declared */ |
Angus Ainslie | 3365fd3 | 2022-02-03 10:08:38 -0800 | [diff] [blame] | 416 | if (!dev_read_prop(dev, "phys", NULL)) { |
| 417 | phydev = dev->parent; |
| 418 | if (!dev_read_prop(phydev, "phys", NULL)) { |
| 419 | pr_err("%s : no phys property\n", __func__); |
| 420 | return 0; |
| 421 | } |
| 422 | } |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 423 | |
Angus Ainslie | 3365fd3 | 2022-02-03 10:08:38 -0800 | [diff] [blame] | 424 | count = dev_count_phandle_with_args(phydev, "phys", "#phy-cells", 0); |
| 425 | if (count < 1) { |
| 426 | pr_err("%s : no phys found %d\n", __func__, count); |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 427 | return count; |
Angus Ainslie | 3365fd3 | 2022-02-03 10:08:38 -0800 | [diff] [blame] | 428 | } |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 429 | |
Angus Ainslie | 3365fd3 | 2022-02-03 10:08:38 -0800 | [diff] [blame] | 430 | bulk->phys = devm_kcalloc(phydev, count, sizeof(struct phy), GFP_KERNEL); |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 431 | if (!bulk->phys) |
| 432 | return -ENOMEM; |
| 433 | |
| 434 | for (i = 0; i < count; i++) { |
Angus Ainslie | 3365fd3 | 2022-02-03 10:08:38 -0800 | [diff] [blame] | 435 | ret = generic_phy_get_by_index(phydev, i, &bulk->phys[i]); |
developer | 272bde6 | 2020-05-02 11:35:11 +0200 | [diff] [blame] | 436 | if (ret) { |
| 437 | pr_err("Failed to get PHY%d for %s\n", i, dev->name); |
| 438 | return ret; |
| 439 | } |
| 440 | bulk->count++; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | int generic_phy_init_bulk(struct phy_bulk *bulk) |
| 447 | { |
| 448 | struct phy *phys = bulk->phys; |
| 449 | int i, ret; |
| 450 | |
| 451 | for (i = 0; i < bulk->count; i++) { |
| 452 | ret = generic_phy_init(&phys[i]); |
| 453 | if (ret) { |
| 454 | pr_err("Can't init PHY%d\n", i); |
| 455 | goto phys_init_err; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | |
| 461 | phys_init_err: |
| 462 | for (; i > 0; i--) |
| 463 | generic_phy_exit(&phys[i - 1]); |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | int generic_phy_exit_bulk(struct phy_bulk *bulk) |
| 469 | { |
| 470 | struct phy *phys = bulk->phys; |
| 471 | int i, ret = 0; |
| 472 | |
| 473 | for (i = 0; i < bulk->count; i++) |
| 474 | ret |= generic_phy_exit(&phys[i]); |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | int generic_phy_power_on_bulk(struct phy_bulk *bulk) |
| 480 | { |
| 481 | struct phy *phys = bulk->phys; |
| 482 | int i, ret; |
| 483 | |
| 484 | for (i = 0; i < bulk->count; i++) { |
| 485 | ret = generic_phy_power_on(&phys[i]); |
| 486 | if (ret) { |
| 487 | pr_err("Can't power on PHY%d\n", i); |
| 488 | goto phys_poweron_err; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | |
| 494 | phys_poweron_err: |
| 495 | for (; i > 0; i--) |
| 496 | generic_phy_power_off(&phys[i - 1]); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | int generic_phy_power_off_bulk(struct phy_bulk *bulk) |
| 502 | { |
| 503 | struct phy *phys = bulk->phys; |
| 504 | int i, ret = 0; |
| 505 | |
| 506 | for (i = 0; i < bulk->count; i++) |
| 507 | ret |= generic_phy_power_off(&phys[i]); |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 512 | int generic_setup_phy(struct udevice *dev, struct phy *phy, int index) |
| 513 | { |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 514 | int ret; |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 515 | |
| 516 | ret = generic_phy_get_by_index(dev, index, phy); |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 517 | if (ret) |
| 518 | return ret == -ENOENT ? 0 : ret; |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 519 | |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 520 | ret = generic_phy_init(phy); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | ret = generic_phy_power_on(phy); |
| 525 | if (ret) |
| 526 | generic_phy_exit(phy); |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 527 | |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | int generic_shutdown_phy(struct phy *phy) |
| 532 | { |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 533 | int ret; |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 534 | |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 535 | if (!generic_phy_valid(phy)) |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 536 | return 0; |
| 537 | |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 538 | ret = generic_phy_power_off(phy); |
| 539 | if (ret) |
| 540 | return ret; |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 541 | |
Jonas Karlman | c539e3e | 2023-08-31 23:07:11 +0000 | [diff] [blame] | 542 | return generic_phy_exit(phy); |
Patrice Chotard | ea7c49e | 2022-09-06 08:15:26 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 545 | UCLASS_DRIVER(phy) = { |
| 546 | .id = UCLASS_PHY, |
| 547 | .name = "phy", |
Alper Nebi Yasak | be4e5e1 | 2021-12-30 22:36:51 +0300 | [diff] [blame] | 548 | .pre_probe = phy_uclass_pre_probe, |
| 549 | .pre_remove = phy_uclass_pre_remove, |
| 550 | .per_device_auto = sizeof(struct list_head), |
Jean-Jacques Hiblot | 4844778 | 2017-04-24 11:51:27 +0200 | [diff] [blame] | 551 | }; |