1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
4 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
7 #define LOG_CATEGORY UCLASS_PHY
11 #include <dm/device_compat.h>
12 #include <dm/devres.h>
13 #include <generic-phy.h>
14 #include <linux/list.h>
15 #include <power/regulator.h>
18 * struct phy_counts - Init and power-on counts of a single PHY port
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).
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
33 * @supply: Handle to a phy-supply device
39 struct list_head list;
40 struct udevice *supply;
43 static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
45 return (struct phy_ops *)dev->driver->ops;
48 static struct phy_counts *phy_get_counts(struct phy *phy)
50 struct list_head *uc_priv;
51 struct phy_counts *counts;
53 if (!generic_phy_valid(phy))
56 uc_priv = dev_get_uclass_priv(phy->dev);
57 list_for_each_entry(counts, uc_priv, list)
58 if (counts->id == phy->id)
64 static int phy_alloc_counts(struct phy *phy, struct udevice *supply)
66 struct list_head *uc_priv;
67 struct phy_counts *counts;
69 if (!generic_phy_valid(phy))
71 if (phy_get_counts(phy))
74 uc_priv = dev_get_uclass_priv(phy->dev);
75 counts = kzalloc(sizeof(*counts), GFP_KERNEL);
80 counts->power_on_count = 0;
81 counts->init_count = 0;
82 counts->supply = supply;
83 list_add(&counts->list, uc_priv);
88 static int phy_uclass_pre_probe(struct udevice *dev)
90 struct list_head *uc_priv = dev_get_uclass_priv(dev);
92 INIT_LIST_HEAD(uc_priv);
97 static int phy_uclass_pre_remove(struct udevice *dev)
99 struct list_head *uc_priv = dev_get_uclass_priv(dev);
100 struct phy_counts *counts, *next;
102 list_for_each_entry_safe(counts, next, uc_priv, list)
108 static int generic_phy_xlate_offs_flags(struct phy *phy,
109 struct ofnode_phandle_args *args)
111 debug("%s(phy=%p)\n", __func__, phy);
113 if (args->args_count > 1) {
114 debug("Invalid args_count: %d\n", args->args_count);
118 if (args->args_count)
119 phy->id = args->args[0];
126 int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy)
128 struct ofnode_phandle_args args;
130 struct udevice *phydev, *supply = NULL;
133 debug("%s(node=%s, index=%d, phy=%p)\n",
134 __func__, ofnode_get_name(node), index, phy);
138 ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0,
141 debug("%s: dev_read_phandle_with_args failed: err=%d\n",
146 ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
148 debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
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),
158 /* insert phy idx at first position into args array */
159 for (i = args.args_count; i >= 1 ; i--)
160 args.args[i] = args.args[i - 1];
163 args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
168 ops = phy_dev_ops(phydev);
171 ret = ops->of_xlate(phy, &args);
173 ret = generic_phy_xlate_offs_flags(phy, &args);
175 debug("of_xlate() failed: %d\n", ret);
179 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
180 ret = device_get_supply_regulator(phydev, "phy-supply",
182 if (ret && ret != -ENOENT) {
183 debug("%s: device_get_supply_regulator failed: %d\n",
189 ret = phy_alloc_counts(phy, supply);
191 debug("phy_alloc_counts() failed: %d\n", ret);
201 int generic_phy_get_by_index(struct udevice *dev, int index,
204 return generic_phy_get_by_index_nodev(dev_ofnode(dev), index, phy);
207 int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
212 debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
214 index = dev_read_stringlist_search(dev, "phy-names", phy_name);
216 debug("dev_read_stringlist_search() failed: %d\n", index);
220 return generic_phy_get_by_index(dev, index, phy);
223 int generic_phy_init(struct phy *phy)
225 struct phy_counts *counts;
226 struct phy_ops const *ops;
229 if (!generic_phy_valid(phy))
231 counts = phy_get_counts(phy);
232 if (counts->init_count > 0) {
233 counts->init_count++;
237 ops = phy_dev_ops(phy->dev);
239 ret = ops->init(phy);
241 dev_err(phy->dev, "PHY: Failed to init %s: %d.\n",
242 phy->dev->name, ret);
246 counts->init_count = 1;
251 int generic_phy_reset(struct phy *phy)
253 struct phy_ops const *ops;
256 if (!generic_phy_valid(phy))
258 ops = phy_dev_ops(phy->dev);
261 ret = ops->reset(phy);
263 dev_err(phy->dev, "PHY: Failed to reset %s: %d.\n",
264 phy->dev->name, ret);
269 int generic_phy_exit(struct phy *phy)
271 struct phy_counts *counts;
272 struct phy_ops const *ops;
275 if (!generic_phy_valid(phy))
277 counts = phy_get_counts(phy);
278 if (counts->init_count == 0)
280 if (counts->init_count > 1) {
281 counts->init_count--;
285 ops = phy_dev_ops(phy->dev);
287 ret = ops->exit(phy);
289 dev_err(phy->dev, "PHY: Failed to exit %s: %d.\n",
290 phy->dev->name, ret);
294 counts->init_count = 0;
299 int generic_phy_power_on(struct phy *phy)
301 struct phy_counts *counts;
302 struct phy_ops const *ops;
305 if (!generic_phy_valid(phy))
307 counts = phy_get_counts(phy);
308 if (counts->power_on_count > 0) {
309 counts->power_on_count++;
313 ret = regulator_set_enable_if_allowed(counts->supply, true);
314 if (ret && ret != -ENOSYS) {
315 dev_err(phy->dev, "PHY: Failed to enable regulator %s: %d.\n",
316 counts->supply->name, ret);
320 ops = phy_dev_ops(phy->dev);
322 ret = ops->power_on(phy);
324 dev_err(phy->dev, "PHY: Failed to power on %s: %d.\n",
325 phy->dev->name, ret);
326 regulator_set_enable_if_allowed(counts->supply, false);
330 counts->power_on_count = 1;
335 int generic_phy_power_off(struct phy *phy)
337 struct phy_counts *counts;
338 struct phy_ops const *ops;
341 if (!generic_phy_valid(phy))
343 counts = phy_get_counts(phy);
344 if (counts->power_on_count == 0)
346 if (counts->power_on_count > 1) {
347 counts->power_on_count--;
351 ops = phy_dev_ops(phy->dev);
352 if (ops->power_off) {
353 ret = ops->power_off(phy);
355 dev_err(phy->dev, "PHY: Failed to power off %s: %d.\n",
356 phy->dev->name, ret);
360 counts->power_on_count = 0;
362 ret = regulator_set_enable_if_allowed(counts->supply, false);
363 if (ret && ret != -ENOSYS)
364 dev_err(phy->dev, "PHY: Failed to disable regulator %s: %d.\n",
365 counts->supply->name, ret);
370 int generic_phy_configure(struct phy *phy, void *params)
372 struct phy_ops const *ops;
374 if (!generic_phy_valid(phy))
376 ops = phy_dev_ops(phy->dev);
378 return ops->configure ? ops->configure(phy, params) : 0;
381 int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
383 struct phy_ops const *ops;
385 if (!generic_phy_valid(phy))
387 ops = phy_dev_ops(phy->dev);
389 return ops->set_mode ? ops->set_mode(phy, mode, submode) : 0;
392 int generic_phy_set_speed(struct phy *phy, int speed)
394 struct phy_ops const *ops;
396 if (!generic_phy_valid(phy))
398 ops = phy_dev_ops(phy->dev);
400 return ops->set_speed ? ops->set_speed(phy, speed) : 0;
403 int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk)
406 struct udevice *phydev = dev;
410 /* Return if no phy declared */
411 if (!dev_read_prop(dev, "phys", NULL)) {
412 phydev = dev->parent;
413 if (!dev_read_prop(phydev, "phys", NULL)) {
414 pr_err("%s : no phys property\n", __func__);
419 count = dev_count_phandle_with_args(phydev, "phys", "#phy-cells", 0);
421 pr_err("%s : no phys found %d\n", __func__, count);
425 bulk->phys = devm_kcalloc(phydev, count, sizeof(struct phy), GFP_KERNEL);
429 for (i = 0; i < count; i++) {
430 ret = generic_phy_get_by_index(phydev, i, &bulk->phys[i]);
432 pr_err("Failed to get PHY%d for %s\n", i, dev->name);
441 int generic_phy_init_bulk(struct phy_bulk *bulk)
443 struct phy *phys = bulk->phys;
446 for (i = 0; i < bulk->count; i++) {
447 ret = generic_phy_init(&phys[i]);
449 pr_err("Can't init PHY%d\n", i);
458 generic_phy_exit(&phys[i - 1]);
463 int generic_phy_exit_bulk(struct phy_bulk *bulk)
465 struct phy *phys = bulk->phys;
468 for (i = 0; i < bulk->count; i++)
469 ret |= generic_phy_exit(&phys[i]);
474 int generic_phy_power_on_bulk(struct phy_bulk *bulk)
476 struct phy *phys = bulk->phys;
479 for (i = 0; i < bulk->count; i++) {
480 ret = generic_phy_power_on(&phys[i]);
482 pr_err("Can't power on PHY%d\n", i);
483 goto phys_poweron_err;
491 generic_phy_power_off(&phys[i - 1]);
496 int generic_phy_power_off_bulk(struct phy_bulk *bulk)
498 struct phy *phys = bulk->phys;
501 for (i = 0; i < bulk->count; i++)
502 ret |= generic_phy_power_off(&phys[i]);
507 int generic_setup_phy(struct udevice *dev, struct phy *phy, int index)
514 ret = generic_phy_get_by_index(dev, index, phy);
519 ret = generic_phy_init(phy);
523 ret = generic_phy_power_on(phy);
525 ret = generic_phy_exit(phy);
531 int generic_shutdown_phy(struct phy *phy)
538 if (generic_phy_valid(phy)) {
539 ret = generic_phy_power_off(phy);
543 ret = generic_phy_exit(phy);
549 UCLASS_DRIVER(phy) = {
552 .pre_probe = phy_uclass_pre_probe,
553 .pre_remove = phy_uclass_pre_remove,
554 .per_device_auto = sizeof(struct list_head),