Merge branch develop-3.10 into develop-3.10-next
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-rockchip-usb.c
1 /*
2  * Rockchip usb PHY driver
3  *
4  * Copyright (C) 2014 Roy 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 #define ROCKCHIP_RK3288_UOC(n)  (0x320 + n * 0x14)
32
33 #define SIDDQ_MSK               (1 << (13 + 16))
34 #define SIDDQ_ON                (1 << 13)
35 #define SIDDQ_OFF               (0 << 13)
36
37 enum rk3288_phy_id {
38         RK3288_OTG,
39         RK3288_HOST0,
40         RK3288_HOST1,
41         RK3288_NUM_PHYS,
42 };
43
44 struct rockchip_usb_phy {
45         struct regmap *reg_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->reg_base, phy->reg_offset,
55                             SIDDQ_MSK | (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         if (ret)
70                 return ret;
71
72         return 0;
73 }
74
75 static int rockchip_usb_phy_power_on(struct phy *_phy)
76 {
77         struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
78         int ret = 0;
79
80         ret = clk_prepare_enable(phy->clk);
81         if (ret)
82                 return ret;
83
84         /* Power up usb phy analog blocks by set siddq 0*/
85         ret = rockchip_usb_phy_power(phy, 0);
86         if (ret)
87                 return ret;
88
89         return 0;
90 }
91
92 static struct phy *rockchip_usb_phy_xlate(struct device *dev,
93                                         struct of_phandle_args *args)
94 {
95         struct rockchip_usb_phy *phy_array = dev_get_drvdata(dev);
96
97         if (WARN_ON(args->args[0] == 0 || args->args[0] >= RK3288_NUM_PHYS))
98                 return ERR_PTR(-ENODEV);
99
100         return (phy_array + args->args[0])->phy;
101 }
102
103 static struct phy_ops ops = {
104         .power_on       = rockchip_usb_phy_power_on,
105         .power_off      = rockchip_usb_phy_power_off,
106         .owner          = THIS_MODULE,
107 };
108
109 static int rockchip_usb_phy_probe(struct platform_device *pdev)
110 {
111         struct device *dev = &pdev->dev;
112         struct rockchip_usb_phy *rk_phy;
113         struct rockchip_usb_phy *phy_array;
114         struct phy_provider *phy_provider;
115         struct regmap *grf;
116         char clk_name[16];
117         int i;
118
119         grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
120         if (IS_ERR(grf)) {
121                 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
122                 return PTR_ERR(grf);
123         }
124
125         phy_array = devm_kzalloc(dev, RK3288_NUM_PHYS * sizeof(*rk_phy),
126                                  GFP_KERNEL);
127         if (!phy_array)
128                 return -ENOMEM;
129
130         for (i = 0; i < RK3288_NUM_PHYS; i++) {
131                 rk_phy = &phy_array[i];
132
133                 rk_phy->reg_base = grf;
134
135                 rk_phy->reg_offset = ROCKCHIP_RK3288_UOC(i);
136
137                 snprintf(clk_name, sizeof(clk_name), "usbphy%d", i);
138                 rk_phy->clk = devm_clk_get(dev, clk_name);
139                 if (IS_ERR(rk_phy->clk)) {
140                         dev_warn(dev, "failed to get clock %s\n", clk_name);
141                         rk_phy->clk = NULL;
142                 }
143
144                 rk_phy->phy = devm_phy_create(dev, NULL, &ops, NULL);
145                 if (IS_ERR(rk_phy->phy)) {
146                         dev_err(dev, "failed to create PHY %d\n", i);
147                         return PTR_ERR(rk_phy->phy);
148                 }
149                 phy_set_drvdata(rk_phy->phy, rk_phy);
150         }
151
152         platform_set_drvdata(pdev, phy_array);
153
154         phy_provider = devm_of_phy_provider_register(dev,
155                                                      rockchip_usb_phy_xlate);
156         return PTR_ERR_OR_ZERO(phy_provider);
157 }
158
159 static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
160         { .compatible = "rockchip,rk3288-usb-phy" },
161         {}
162 };
163
164 MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
165
166 static struct platform_driver rockchip_usb_driver = {
167         .probe          = rockchip_usb_phy_probe,
168         .driver         = {
169                 .name   = "rockchip-usb-phy",
170                 .owner  = THIS_MODULE,
171                 .of_match_table = rockchip_usb_phy_dt_ids,
172         },
173 };
174
175 module_platform_driver(rockchip_usb_driver);
176
177 MODULE_AUTHOR("Roy Li <lyz@rock-chips.com>");
178 MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
179 MODULE_LICENSE("GPL v2");