Merge branch '2022-03-10-platform-updates' into next
[platform/kernel/u-boot.git] / drivers / phy / nop-phy.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
4  * Written by Jean-Jacques Hiblot  <jjhiblot@ti.com>
5  */
6
7 #include <clk.h>
8 #include <common.h>
9 #include <dm.h>
10 #include <dm/device.h>
11 #include <dm/device_compat.h>
12 #include <generic-phy.h>
13 #include <asm-generic/gpio.h>
14
15 struct nop_phy_priv {
16         struct clk_bulk bulk;
17 #if CONFIG_IS_ENABLED(DM_GPIO)
18         struct gpio_desc reset_gpio;
19 #endif
20 };
21
22 #if CONFIG_IS_ENABLED(DM_GPIO)
23 static int nop_phy_reset(struct phy *phy)
24 {
25         struct nop_phy_priv *priv = dev_get_priv(phy->dev);
26
27         /* Return if there is no gpio since it's optional */
28         if (!dm_gpio_is_valid(&priv->reset_gpio))
29                 return 0;
30
31         return dm_gpio_set_value(&priv->reset_gpio, true);
32 }
33 #endif
34
35 static int nop_phy_init(struct phy *phy)
36 {
37         struct nop_phy_priv *priv = dev_get_priv(phy->dev);
38         int ret = 0;
39
40         if (CONFIG_IS_ENABLED(CLK)) {
41                 ret = clk_enable_bulk(&priv->bulk);
42                 if (ret)
43                         return ret;
44         }
45
46 #if CONFIG_IS_ENABLED(DM_GPIO)
47         /* Take phy out of reset */
48         ret = dm_gpio_set_value(&priv->reset_gpio, false);
49         if (ret) {
50                 if (CONFIG_IS_ENABLED(CLK))
51                         clk_disable_bulk(&priv->bulk);
52                 return ret;
53         }
54 #endif
55         return 0;
56 }
57
58 static int nop_phy_probe(struct udevice *dev)
59 {
60         struct nop_phy_priv *priv = dev_get_priv(dev);
61         int ret = 0;
62
63         if (CONFIG_IS_ENABLED(CLK)) {
64                 ret = clk_get_bulk(dev, &priv->bulk);
65                 if (ret < 0) {
66                         dev_err(dev, "Failed to get clk: %d\n", ret);
67                         return ret;
68                 }
69         }
70 #if CONFIG_IS_ENABLED(DM_GPIO)
71         ret = gpio_request_by_name(dev, "reset-gpios", 0,
72                                    &priv->reset_gpio,
73                                    GPIOD_IS_OUT);
74 #endif
75         if (ret != -ENOENT)
76                 return ret;
77
78         return 0;
79 }
80
81 static const struct udevice_id nop_phy_ids[] = {
82         { .compatible = "nop-phy" },
83         { .compatible = "usb-nop-xceiv" },
84         { }
85 };
86
87 static struct phy_ops nop_phy_ops = {
88         .init = nop_phy_init,
89 #if CONFIG_IS_ENABLED(DM_GPIO)
90         .reset = nop_phy_reset,
91 #endif
92 };
93
94 U_BOOT_DRIVER(nop_phy) = {
95         .name   = "nop_phy",
96         .id     = UCLASS_PHY,
97         .of_match = nop_phy_ids,
98         .ops = &nop_phy_ops,
99         .probe = nop_phy_probe,
100         .priv_auto      = sizeof(struct nop_phy_priv),
101 };