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