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