UPSTREAM: phy: rockchip-usb: move per-phy init into a separate function
[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/phy/phy.h>
25 #include <linux/platform_device.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/reset.h>
28 #include <linux/regmap.h>
29 #include <linux/mfd/syscon.h>
30
31 /*
32  * The higher 16-bit of this register is used for write protection
33  * only if BIT(13 + 16) set to 1 the BIT(13) can be written.
34  */
35 #define SIDDQ_WRITE_ENA BIT(29)
36 #define SIDDQ_ON                BIT(13)
37 #define SIDDQ_OFF               (0 << 13)
38
39 struct rockchip_usb_phy_base {
40         struct device *dev;
41         struct regmap *reg_base;
42 };
43
44 struct rockchip_usb_phy {
45         struct rockchip_usb_phy_base *base;
46         unsigned int    reg_offset;
47         struct clk      *clk;
48         struct phy      *phy;
49 };
50
51 static int rockchip_usb_phy_power(struct rockchip_usb_phy *phy,
52                                            bool siddq)
53 {
54         return regmap_write(phy->base->reg_base, phy->reg_offset,
55                             SIDDQ_WRITE_ENA | (siddq ? SIDDQ_ON : SIDDQ_OFF));
56 }
57
58 static int rockchip_usb_phy_power_off(struct phy *_phy)
59 {
60         struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
61         int ret = 0;
62
63         /* Power down usb phy analog blocks by set siddq 1 */
64         ret = rockchip_usb_phy_power(phy, 1);
65         if (ret)
66                 return ret;
67
68         clk_disable_unprepare(phy->clk);
69
70         return 0;
71 }
72
73 static int rockchip_usb_phy_power_on(struct phy *_phy)
74 {
75         struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
76         int ret = 0;
77
78         ret = clk_prepare_enable(phy->clk);
79         if (ret)
80                 return ret;
81
82         /* Power up usb phy analog blocks by set siddq 0 */
83         ret = rockchip_usb_phy_power(phy, 0);
84         if (ret) {
85                 clk_disable_unprepare(phy->clk);
86                 return ret;
87         }
88
89         return 0;
90 }
91
92 static const struct phy_ops ops = {
93         .power_on       = rockchip_usb_phy_power_on,
94         .power_off      = rockchip_usb_phy_power_off,
95         .owner          = THIS_MODULE,
96 };
97
98 static void rockchip_usb_phy_action(void *data)
99 {
100         struct rockchip_usb_phy *rk_phy = data;
101
102         if (rk_phy->clk)
103                 clk_put(rk_phy->clk);
104 }
105
106 static int rockchip_usb_phy_init(struct rockchip_usb_phy_base *base,
107                                  struct device_node *child)
108 {
109         struct rockchip_usb_phy *rk_phy;
110         unsigned int reg_offset;
111         int err;
112
113         rk_phy = devm_kzalloc(base->dev, sizeof(*rk_phy), GFP_KERNEL);
114         if (!rk_phy)
115                 return -ENOMEM;
116
117         rk_phy->base = base;
118
119         if (of_property_read_u32(child, "reg", &reg_offset)) {
120                 dev_err(base->dev, "missing reg property in node %s\n",
121                         child->name);
122                 return -EINVAL;
123         }
124
125         rk_phy->reg_offset = reg_offset;
126
127         err = devm_add_action(base->dev, rockchip_usb_phy_action, rk_phy);
128         if (err)
129                 return err;
130
131         rk_phy->clk = of_clk_get_by_name(child, "phyclk");
132         if (IS_ERR(rk_phy->clk))
133                 rk_phy->clk = NULL;
134
135         rk_phy->phy = devm_phy_create(base->dev, child, &ops);
136         if (IS_ERR(rk_phy->phy)) {
137                 dev_err(base->dev, "failed to create PHY\n");
138                 return PTR_ERR(rk_phy->phy);
139         }
140         phy_set_drvdata(rk_phy->phy, rk_phy);
141
142         /* only power up usb phy when it use, so disable it when init*/
143         return rockchip_usb_phy_power(rk_phy, 1);
144 }
145
146 static int rockchip_usb_phy_probe(struct platform_device *pdev)
147 {
148         struct device *dev = &pdev->dev;
149         struct rockchip_usb_phy_base *phy_base;
150         struct phy_provider *phy_provider;
151         struct device_node *child;
152         int err;
153
154         phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
155         if (!phy_base)
156                 return -ENOMEM;
157
158         phy_base->dev = dev;
159         phy_base->reg_base = syscon_regmap_lookup_by_phandle(dev->of_node,
160                                                              "rockchip,grf");
161         if (IS_ERR(phy_base->reg_base)) {
162                 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
163                 return PTR_ERR(phy_base->reg_base);
164         }
165
166         for_each_available_child_of_node(dev->of_node, child) {
167                 err = rockchip_usb_phy_init(phy_base, child);
168                 if (err) {
169                         of_node_put(child);
170                         return err;
171                 }
172         }
173
174         phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
175         return PTR_ERR_OR_ZERO(phy_provider);
176 }
177
178 static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
179         { .compatible = "rockchip,rk3288-usb-phy" },
180         {}
181 };
182
183 MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
184
185 static struct platform_driver rockchip_usb_driver = {
186         .probe          = rockchip_usb_phy_probe,
187         .driver         = {
188                 .name   = "rockchip-usb-phy",
189                 .of_match_table = rockchip_usb_phy_dt_ids,
190         },
191 };
192
193 module_platform_driver(rockchip_usb_driver);
194
195 MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
196 MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
197 MODULE_LICENSE("GPL v2");