phy: phy-rockchip-usb: add support of rk33 usb phy
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-rockchip-usb.c
1 /*
2  * Rockchip usb PHY driver
3  *
4  * Copyright (C) 2014 Yunzhi Li <lyz@rock-chips.com>
5  * Copyright (C) 2014 ROCKCHIP, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/clk.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/of.h>
23 #include <linux/of_address.h>
24 #include <linux/of_platform.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/reset.h>
29 #include <linux/regmap.h>
30 #include <linux/mfd/syscon.h>
31
32 /*
33  * The higher 16-bit of this register is used for write protection
34  * only if BIT(13 + 16) set to 1 the BIT(13) can be written.
35  */
36 #define SIDDQ_WRITE_ENA BIT(29)
37 #define SIDDQ_ON                BIT(13)
38 #define SIDDQ_OFF               (0 << 13)
39
40 struct rockchip_usb_phy {
41         unsigned int    reg_offset;
42         struct regmap   *reg_base;
43         struct clk      *clk;
44         struct phy      *phy;
45         int (*rk_usb_phy_power)(struct rockchip_usb_phy *phy, bool on);
46 };
47
48 static int rk32_usb_phy_power(struct rockchip_usb_phy *phy,
49                               bool on)
50 {
51         /* Power down usb phy analog blocks by set siddq 1 */
52         bool siddq = !on;
53
54         return regmap_write(phy->reg_base, phy->reg_offset,
55                             SIDDQ_WRITE_ENA | (siddq ? SIDDQ_ON : SIDDQ_OFF));
56 }
57
58 static int rk33_usb_phy_power(struct rockchip_usb_phy *phy,
59                               bool on)
60 {
61         if (on)
62                 return regmap_write(phy->reg_base, phy->reg_offset, 0xffff0000);
63         else
64                 return regmap_write(phy->reg_base, phy->reg_offset, 0xffff01d5);
65 }
66
67 static int rockchip_usb_phy_power_off(struct phy *_phy)
68 {
69         struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
70         int ret = 0;
71
72         ret = phy->rk_usb_phy_power(phy, 0);
73         if (ret)
74                 return ret;
75
76         clk_disable_unprepare(phy->clk);
77         if (ret)
78                 return ret;
79
80         return 0;
81 }
82
83 static int rockchip_usb_phy_power_on(struct phy *_phy)
84 {
85         struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
86         int ret = 0;
87
88         ret = clk_prepare_enable(phy->clk);
89         if (ret)
90                 return ret;
91
92         /* Power up usb phy analog blocks by set siddq 0 */
93         ret = phy->rk_usb_phy_power(phy, 1);
94         if (ret)
95                 return ret;
96
97         return 0;
98 }
99
100 static struct phy_ops ops = {
101         .power_on       = rockchip_usb_phy_power_on,
102         .power_off      = rockchip_usb_phy_power_off,
103         .owner          = THIS_MODULE,
104 };
105
106 static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
107         {
108                 .compatible = "rockchip,rk3288-usb-phy",
109                 .data = (void *)rk32_usb_phy_power
110         },
111         {
112                 .compatible = "rockchip,rk3368-usb-phy",
113                 .data = (void *)rk33_usb_phy_power
114         },
115         {}
116 };
117
118 static int rockchip_usb_phy_probe(struct platform_device *pdev)
119 {
120         struct device *dev = &pdev->dev;
121         struct rockchip_usb_phy *rk_phy;
122         struct phy_provider *phy_provider;
123         struct device_node *child;
124         struct regmap *grf;
125         const struct of_device_id *match;
126         unsigned int reg_offset;
127         int (*rk_usb_phy_power)(struct rockchip_usb_phy *phy, bool siddq);
128
129         grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
130         if (IS_ERR(grf)) {
131                 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
132                 return PTR_ERR(grf);
133         }
134
135         match = of_match_device(rockchip_usb_phy_dt_ids, &pdev->dev);
136         if (match && match->data)
137                 rk_usb_phy_power = match->data;
138         else
139                 return PTR_ERR(match);
140
141         for_each_available_child_of_node(dev->of_node, child) {
142                 rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
143                 if (!rk_phy)
144                         return -ENOMEM;
145
146                 if (of_property_read_u32(child, "reg", &reg_offset)) {
147                         dev_err(dev, "missing reg property in node %s\n",
148                                 child->name);
149                         return -EINVAL;
150                 }
151
152                 rk_phy->rk_usb_phy_power = rk_usb_phy_power;
153                 rk_phy->reg_offset = reg_offset;
154                 rk_phy->reg_base = grf;
155
156                 rk_phy->clk = of_clk_get_by_name(child, "phyclk");
157                 if (IS_ERR(rk_phy->clk))
158                         rk_phy->clk = NULL;
159
160                 rk_phy->phy = devm_phy_create(dev, child, &ops);
161                 if (IS_ERR(rk_phy->phy)) {
162                         dev_err(dev, "failed to create PHY\n");
163                         return PTR_ERR(rk_phy->phy);
164                 }
165                 phy_set_drvdata(rk_phy->phy, rk_phy);
166         }
167
168         phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
169         if (IS_ERR(phy_provider))
170                 return (long)phy_provider;
171         else
172                 return 0;
173 }
174
175 MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
176
177 static struct platform_driver rockchip_usb_driver = {
178         .probe          = rockchip_usb_phy_probe,
179         .driver         = {
180                 .name   = "rockchip-usb-phy",
181                 .owner  = THIS_MODULE,
182                 .of_match_table = rockchip_usb_phy_dt_ids,
183         },
184 };
185
186 module_platform_driver(rockchip_usb_driver);
187
188 MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
189 MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
190 MODULE_LICENSE("GPL v2");