93a8c652949e43cb0a93b94680e7dc961f186c93
[openwrt/staging/lynxis.git] / target / linux / mediatek / patches-4.14 / 0106-usb-mtu3-add-optional-mcu-and-dma-bus-clocks.patch
1 From 677805f6d83524717b46b3cde74aa455dbf6299f Mon Sep 17 00:00:00 2001
2 From: Chunfeng Yun <chunfeng.yun@mediatek.com>
3 Date: Fri, 13 Oct 2017 17:10:40 +0800
4 Subject: [PATCH 106/224] usb: mtu3: add optional mcu and dma bus clocks
5
6 There are mcu_bus and dma_bus clocks needed to be turned on/off by
7 driver on some SoCs, so add them as optional ones
8
9 Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
10 Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
11 ---
12 drivers/usb/mtu3/mtu3.h | 5 ++
13 drivers/usb/mtu3/mtu3_plat.c | 121 +++++++++++++++++++++++++++++--------------
14 2 files changed, 86 insertions(+), 40 deletions(-)
15
16 diff --git a/drivers/usb/mtu3/mtu3.h b/drivers/usb/mtu3/mtu3.h
17 index 6d3278e46431..2795294ec92a 100644
18 --- a/drivers/usb/mtu3/mtu3.h
19 +++ b/drivers/usb/mtu3/mtu3.h
20 @@ -206,6 +206,9 @@ struct otg_switch_mtk {
21 * @ippc_base: register base address of IP Power and Clock interface (IPPC)
22 * @vusb33: usb3.3V shared by device/host IP
23 * @sys_clk: system clock of mtu3, shared by device/host IP
24 + * @ref_clk: reference clock
25 + * @mcu_clk: mcu_bus_ck clock for AHB bus etc
26 + * @dma_clk: dma_bus_ck clock for AXI bus etc
27 * @dr_mode: works in which mode:
28 * host only, device only or dual-role mode
29 * @u2_ports: number of usb2.0 host ports
30 @@ -226,6 +229,8 @@ struct ssusb_mtk {
31 struct regulator *vusb33;
32 struct clk *sys_clk;
33 struct clk *ref_clk;
34 + struct clk *mcu_clk;
35 + struct clk *dma_clk;
36 /* otg */
37 struct otg_switch_mtk otg_switch;
38 enum usb_dr_mode dr_mode;
39 diff --git a/drivers/usb/mtu3/mtu3_plat.c b/drivers/usb/mtu3/mtu3_plat.c
40 index 9edad30c8ae5..fb8992011bde 100644
41 --- a/drivers/usb/mtu3/mtu3_plat.c
42 +++ b/drivers/usb/mtu3/mtu3_plat.c
43 @@ -110,15 +110,9 @@ static void ssusb_phy_power_off(struct ssusb_mtk *ssusb)
44 phy_power_off(ssusb->phys[i]);
45 }
46
47 -static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
48 +static int ssusb_clks_enable(struct ssusb_mtk *ssusb)
49 {
50 - int ret = 0;
51 -
52 - ret = regulator_enable(ssusb->vusb33);
53 - if (ret) {
54 - dev_err(ssusb->dev, "failed to enable vusb33\n");
55 - goto vusb33_err;
56 - }
57 + int ret;
58
59 ret = clk_prepare_enable(ssusb->sys_clk);
60 if (ret) {
61 @@ -132,6 +126,52 @@ static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
62 goto ref_clk_err;
63 }
64
65 + ret = clk_prepare_enable(ssusb->mcu_clk);
66 + if (ret) {
67 + dev_err(ssusb->dev, "failed to enable mcu_clk\n");
68 + goto mcu_clk_err;
69 + }
70 +
71 + ret = clk_prepare_enable(ssusb->dma_clk);
72 + if (ret) {
73 + dev_err(ssusb->dev, "failed to enable dma_clk\n");
74 + goto dma_clk_err;
75 + }
76 +
77 + return 0;
78 +
79 +dma_clk_err:
80 + clk_disable_unprepare(ssusb->mcu_clk);
81 +mcu_clk_err:
82 + clk_disable_unprepare(ssusb->ref_clk);
83 +ref_clk_err:
84 + clk_disable_unprepare(ssusb->sys_clk);
85 +sys_clk_err:
86 + return ret;
87 +}
88 +
89 +static void ssusb_clks_disable(struct ssusb_mtk *ssusb)
90 +{
91 + clk_disable_unprepare(ssusb->dma_clk);
92 + clk_disable_unprepare(ssusb->mcu_clk);
93 + clk_disable_unprepare(ssusb->ref_clk);
94 + clk_disable_unprepare(ssusb->sys_clk);
95 +}
96 +
97 +static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
98 +{
99 + int ret = 0;
100 +
101 + ret = regulator_enable(ssusb->vusb33);
102 + if (ret) {
103 + dev_err(ssusb->dev, "failed to enable vusb33\n");
104 + goto vusb33_err;
105 + }
106 +
107 + ret = ssusb_clks_enable(ssusb);
108 + if (ret)
109 + goto clks_err;
110 +
111 ret = ssusb_phy_init(ssusb);
112 if (ret) {
113 dev_err(ssusb->dev, "failed to init phy\n");
114 @@ -149,20 +189,16 @@ static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
115 phy_err:
116 ssusb_phy_exit(ssusb);
117 phy_init_err:
118 - clk_disable_unprepare(ssusb->ref_clk);
119 -ref_clk_err:
120 - clk_disable_unprepare(ssusb->sys_clk);
121 -sys_clk_err:
122 + ssusb_clks_disable(ssusb);
123 +clks_err:
124 regulator_disable(ssusb->vusb33);
125 vusb33_err:
126 -
127 return ret;
128 }
129
130 static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
131 {
132 - clk_disable_unprepare(ssusb->sys_clk);
133 - clk_disable_unprepare(ssusb->ref_clk);
134 + ssusb_clks_disable(ssusb);
135 regulator_disable(ssusb->vusb33);
136 ssusb_phy_power_off(ssusb);
137 ssusb_phy_exit(ssusb);
138 @@ -203,6 +239,19 @@ static int get_iddig_pinctrl(struct ssusb_mtk *ssusb)
139 return 0;
140 }
141
142 +/* ignore the error if the clock does not exist */
143 +static struct clk *get_optional_clk(struct device *dev, const char *id)
144 +{
145 + struct clk *opt_clk;
146 +
147 + opt_clk = devm_clk_get(dev, id);
148 + /* ignore error number except EPROBE_DEFER */
149 + if (IS_ERR(opt_clk) && (PTR_ERR(opt_clk) != -EPROBE_DEFER))
150 + opt_clk = NULL;
151 +
152 + return opt_clk;
153 +}
154 +
155 static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
156 {
157 struct device_node *node = pdev->dev.of_node;
158 @@ -225,18 +274,17 @@ static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
159 return PTR_ERR(ssusb->sys_clk);
160 }
161
162 - /*
163 - * reference clock is usually a "fixed-clock", make it optional
164 - * for backward compatibility and ignore the error if it does
165 - * not exist.
166 - */
167 - ssusb->ref_clk = devm_clk_get(dev, "ref_ck");
168 - if (IS_ERR(ssusb->ref_clk)) {
169 - if (PTR_ERR(ssusb->ref_clk) == -EPROBE_DEFER)
170 - return -EPROBE_DEFER;
171 + ssusb->ref_clk = get_optional_clk(dev, "ref_ck");
172 + if (IS_ERR(ssusb->ref_clk))
173 + return PTR_ERR(ssusb->ref_clk);
174
175 - ssusb->ref_clk = NULL;
176 - }
177 + ssusb->mcu_clk = get_optional_clk(dev, "mcu_ck");
178 + if (IS_ERR(ssusb->mcu_clk))
179 + return PTR_ERR(ssusb->mcu_clk);
180 +
181 + ssusb->dma_clk = get_optional_clk(dev, "dma_ck");
182 + if (IS_ERR(ssusb->dma_clk))
183 + return PTR_ERR(ssusb->dma_clk);
184
185 ssusb->num_phys = of_count_phandle_with_args(node,
186 "phys", "#phy-cells");
187 @@ -451,8 +499,7 @@ static int __maybe_unused mtu3_suspend(struct device *dev)
188
189 ssusb_host_disable(ssusb, true);
190 ssusb_phy_power_off(ssusb);
191 - clk_disable_unprepare(ssusb->sys_clk);
192 - clk_disable_unprepare(ssusb->ref_clk);
193 + ssusb_clks_disable(ssusb);
194 ssusb_wakeup_enable(ssusb);
195
196 return 0;
197 @@ -470,27 +517,21 @@ static int __maybe_unused mtu3_resume(struct device *dev)
198 return 0;
199
200 ssusb_wakeup_disable(ssusb);
201 - ret = clk_prepare_enable(ssusb->sys_clk);
202 - if (ret)
203 - goto err_sys_clk;
204 -
205 - ret = clk_prepare_enable(ssusb->ref_clk);
206 + ret = ssusb_clks_enable(ssusb);
207 if (ret)
208 - goto err_ref_clk;
209 + goto clks_err;
210
211 ret = ssusb_phy_power_on(ssusb);
212 if (ret)
213 - goto err_power_on;
214 + goto phy_err;
215
216 ssusb_host_enable(ssusb);
217
218 return 0;
219
220 -err_power_on:
221 - clk_disable_unprepare(ssusb->ref_clk);
222 -err_ref_clk:
223 - clk_disable_unprepare(ssusb->sys_clk);
224 -err_sys_clk:
225 +phy_err:
226 + ssusb_clks_disable(ssusb);
227 +clks_err:
228 return ret;
229 }
230
231 --
232 2.11.0
233