2 * Rockchip usb PHY driver
4 * Copyright (C) 2014 Yunzhi Li <lyz@rock-chips.com>
5 * Copyright (C) 2014 ROCKCHIP, Inc.
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.
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.
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
24 #include <linux/of_address.h>
25 #include <linux/of_platform.h>
26 #include <linux/phy/phy.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/regmap.h>
31 #include <linux/mfd/syscon.h>
34 * The higher 16-bit of this register is used for write protection
35 * only if BIT(13 + 16) set to 1 the BIT(13) can be written.
37 #define SIDDQ_WRITE_ENA BIT(29)
38 #define SIDDQ_ON BIT(13)
39 #define SIDDQ_OFF (0 << 13)
41 #define USB2_PHY_WRITE_ENA (0xffff << 16)
42 #define USB2_PHY_SUSPEND (0x5 << 0 | 0xd << 4 | 0x1 << 8)
43 #define USB2_PHY_RESUME (0)
45 struct rockchip_usb_phys {
50 struct rockchip_usb_phy_pdata {
51 struct rockchip_usb_phys *phys;
52 unsigned int phy_pw_on;
53 unsigned int phy_pw_off;
57 struct rockchip_usb_phy_base {
59 struct regmap *reg_base;
60 const struct rockchip_usb_phy_pdata *pdata;
63 struct rockchip_usb_phy {
64 struct rockchip_usb_phy_base *base;
65 struct device_node *np;
66 unsigned int reg_offset;
69 struct clk_hw clk480m_hw;
73 static int rockchip_usb_phy_power(struct rockchip_usb_phy *phy,
78 val = !off ? phy->base->pdata->phy_pw_on : phy->base->pdata->phy_pw_off;
79 return regmap_write(phy->base->reg_base, phy->reg_offset, val);
82 static unsigned long rockchip_usb_phy480m_recalc_rate(struct clk_hw *hw,
83 unsigned long parent_rate)
88 static void rockchip_usb_phy480m_disable(struct clk_hw *hw)
90 struct rockchip_usb_phy *phy = container_of(hw,
91 struct rockchip_usb_phy,
94 /* Power down usb phy analog blocks by set siddq 1 */
95 if (phy->base->pdata->siddq_ctl)
96 rockchip_usb_phy_power(phy, 1);
99 static int rockchip_usb_phy480m_enable(struct clk_hw *hw)
102 struct rockchip_usb_phy *phy = container_of(hw,
103 struct rockchip_usb_phy,
106 /* Power up usb phy analog blocks by set siddq 0 */
107 if (phy->base->pdata->siddq_ctl)
108 ret = rockchip_usb_phy_power(phy, 0);
113 static int rockchip_usb_phy480m_is_enabled(struct clk_hw *hw)
115 struct rockchip_usb_phy *phy = container_of(hw,
116 struct rockchip_usb_phy,
121 if (phy->base->pdata->siddq_ctl) {
122 ret = regmap_read(phy->base->reg_base, phy->reg_offset, &val);
126 ret = (val & SIDDQ_ON) ? 0 : 1;
132 static const struct clk_ops rockchip_usb_phy480m_ops = {
133 .enable = rockchip_usb_phy480m_enable,
134 .disable = rockchip_usb_phy480m_disable,
135 .is_enabled = rockchip_usb_phy480m_is_enabled,
136 .recalc_rate = rockchip_usb_phy480m_recalc_rate,
139 static int rockchip_usb_phy_power_off(struct phy *_phy)
142 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
144 if (!phy->base->pdata->siddq_ctl) {
145 ret = rockchip_usb_phy_power(phy, 1);
150 clk_disable_unprepare(phy->clk480m);
154 static int rockchip_usb_phy_power_on(struct phy *_phy)
157 struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
159 ret = clk_prepare_enable(phy->clk480m);
163 if (!phy->base->pdata->siddq_ctl)
164 ret = rockchip_usb_phy_power(phy, 0);
169 static const struct phy_ops ops = {
170 .power_on = rockchip_usb_phy_power_on,
171 .power_off = rockchip_usb_phy_power_off,
172 .owner = THIS_MODULE,
175 static void rockchip_usb_phy_action(void *data)
177 struct rockchip_usb_phy *rk_phy = data;
179 of_clk_del_provider(rk_phy->np);
180 clk_unregister(rk_phy->clk480m);
183 clk_put(rk_phy->clk);
186 static int rockchip_usb_phy_init(struct rockchip_usb_phy_base *base,
187 struct device_node *child)
189 struct rockchip_usb_phy *rk_phy;
190 unsigned int reg_offset;
191 const char *clk_name;
192 struct clk_init_data init;
195 rk_phy = devm_kzalloc(base->dev, sizeof(*rk_phy), GFP_KERNEL);
202 if (of_property_read_u32(child, "reg", ®_offset)) {
203 dev_err(base->dev, "missing reg property in node %s\n",
208 rk_phy->reg_offset = reg_offset;
210 rk_phy->clk = of_clk_get_by_name(child, "phyclk");
211 if (IS_ERR(rk_phy->clk))
216 while (base->pdata->phys[i].reg) {
217 if (base->pdata->phys[i].reg == reg_offset) {
218 init.name = base->pdata->phys[i].pll_name;
225 dev_err(base->dev, "phy data not found\n");
230 clk_name = __clk_get_name(rk_phy->clk);
232 init.parent_names = &clk_name;
233 init.num_parents = 1;
235 init.flags = CLK_IS_ROOT;
236 init.parent_names = NULL;
237 init.num_parents = 0;
240 init.ops = &rockchip_usb_phy480m_ops;
241 rk_phy->clk480m_hw.init = &init;
243 rk_phy->clk480m = clk_register(base->dev, &rk_phy->clk480m_hw);
244 if (IS_ERR(rk_phy->clk480m)) {
245 err = PTR_ERR(rk_phy->clk480m);
249 err = of_clk_add_provider(child, of_clk_src_simple_get,
254 err = devm_add_action(base->dev, rockchip_usb_phy_action, rk_phy);
256 goto err_devm_action;
258 rk_phy->phy = devm_phy_create(base->dev, child, &ops);
259 if (IS_ERR(rk_phy->phy)) {
260 dev_err(base->dev, "failed to create PHY\n");
261 return PTR_ERR(rk_phy->phy);
263 phy_set_drvdata(rk_phy->phy, rk_phy);
265 /* only power up usb phy when it use, so disable it when init*/
266 return rockchip_usb_phy_power(rk_phy, 1);
269 of_clk_del_provider(child);
271 clk_unregister(rk_phy->clk480m);
274 clk_put(rk_phy->clk);
278 static const struct rockchip_usb_phy_pdata rk3066a_pdata = {
279 .phys = (struct rockchip_usb_phys[]){
280 { .reg = 0x17c, .pll_name = "sclk_otgphy0_480m" },
281 { .reg = 0x188, .pll_name = "sclk_otgphy1_480m" },
284 .phy_pw_on = SIDDQ_WRITE_ENA | SIDDQ_OFF,
285 .phy_pw_off = SIDDQ_WRITE_ENA | SIDDQ_ON,
289 static const struct rockchip_usb_phy_pdata rk3188_pdata = {
290 .phys = (struct rockchip_usb_phys[]){
291 { .reg = 0x10c, .pll_name = "sclk_otgphy0_480m" },
292 { .reg = 0x11c, .pll_name = "sclk_otgphy1_480m" },
295 .phy_pw_on = SIDDQ_WRITE_ENA | SIDDQ_OFF,
296 .phy_pw_off = SIDDQ_WRITE_ENA | SIDDQ_ON,
300 static const struct rockchip_usb_phy_pdata rk3288_pdata = {
301 .phys = (struct rockchip_usb_phys[]){
302 { .reg = 0x320, .pll_name = "sclk_otgphy0_480m" },
303 { .reg = 0x334, .pll_name = "sclk_otgphy1_480m" },
304 { .reg = 0x348, .pll_name = "sclk_otgphy2_480m" },
307 .phy_pw_on = SIDDQ_WRITE_ENA | SIDDQ_OFF,
308 .phy_pw_off = SIDDQ_WRITE_ENA | SIDDQ_ON,
312 static const struct rockchip_usb_phy_pdata rk336x_pdata = {
313 .phys = (struct rockchip_usb_phys[]){
314 { .reg = 0x700, .pll_name = "sclk_otgphy0_480m" },
315 { .reg = 0x728, .pll_name = "sclk_otgphy1_480m" },
318 .phy_pw_on = USB2_PHY_WRITE_ENA | USB2_PHY_RESUME,
319 .phy_pw_off = USB2_PHY_WRITE_ENA | USB2_PHY_SUSPEND,
323 static int rockchip_usb_phy_probe(struct platform_device *pdev)
325 struct device *dev = &pdev->dev;
326 struct rockchip_usb_phy_base *phy_base;
327 struct phy_provider *phy_provider;
328 const struct of_device_id *match;
329 struct device_node *child;
332 phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
336 match = of_match_device(dev->driver->of_match_table, dev);
337 if (!match || !match->data) {
338 dev_err(dev, "missing phy data\n");
342 phy_base->pdata = match->data;
345 phy_base->reg_base = syscon_regmap_lookup_by_phandle(dev->of_node,
347 if (IS_ERR(phy_base->reg_base)) {
348 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
349 return PTR_ERR(phy_base->reg_base);
352 for_each_available_child_of_node(dev->of_node, child) {
353 err = rockchip_usb_phy_init(phy_base, child);
360 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
361 return PTR_ERR_OR_ZERO(phy_provider);
364 static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
365 { .compatible = "rockchip,rk3066a-usb-phy", .data = &rk3066a_pdata },
366 { .compatible = "rockchip,rk3188-usb-phy", .data = &rk3188_pdata },
367 { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
368 { .compatible = "rockchip,rk336x-usb-phy", .data = &rk336x_pdata },
372 MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
374 static struct platform_driver rockchip_usb_driver = {
375 .probe = rockchip_usb_phy_probe,
377 .name = "rockchip-usb-phy",
378 .of_match_table = rockchip_usb_phy_dt_ids,
382 module_platform_driver(rockchip_usb_driver);
384 MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
385 MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
386 MODULE_LICENSE("GPL v2");