blob: f1d100671ee6a174ec3637c2d5f7bc7baec01d36 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Richard Zhao15302802012-07-07 22:56:48 +08002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
5 * on behalf of DENX Software Engineering GmbH
Richard Zhao15302802012-07-07 22:56:48 +08006 */
7
8#include <linux/module.h>
9#include <linux/of_platform.h>
Richard Zhao15302802012-07-07 22:56:48 +080010#include <linux/platform_device.h>
11#include <linux/pm_runtime.h>
Richard Zhao15302802012-07-07 22:56:48 +080012#include <linux/usb/chipidea.h>
Fabien Lahoudered13631b2016-09-26 13:14:19 +020013#include <linux/usb/of.h>
Richard Zhao15302802012-07-07 22:56:48 +080014#include <linux/clk.h>
Peter Chen7c8e8902018-10-16 09:17:02 +080015#include <linux/pinctrl/consumer.h>
Peter Chend1609c32019-04-28 10:35:31 +080016#include <linux/pm_qos.h>
Richard Zhao15302802012-07-07 22:56:48 +080017
18#include "ci.h"
Alexander Shishkin8e229782013-06-24 14:46:36 +030019#include "ci_hdrc_imx.h"
Richard Zhao15302802012-07-07 22:56:48 +080020
Peter Chen10710552014-01-10 13:51:28 +080021struct ci_hdrc_imx_platform_flag {
22 unsigned int flags;
23};
24
Stefan Wahren81345722015-12-29 16:41:08 +000025static const struct ci_hdrc_imx_platform_flag imx23_usb_data = {
26 .flags = CI_HDRC_TURN_VBUS_EARLY_ON |
27 CI_HDRC_DISABLE_STREAMING,
28};
29
Peter Chen10710552014-01-10 13:51:28 +080030static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
Sebastian Reichel04e16372018-03-29 17:52:48 +080031 .flags = CI_HDRC_DISABLE_STREAMING,
Peter Chen10710552014-01-10 13:51:28 +080032};
33
34static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080035 .flags = CI_HDRC_IMX28_WRITE_FIX |
Peter Chen0ef877a2014-10-30 11:10:04 +080036 CI_HDRC_TURN_VBUS_EARLY_ON |
37 CI_HDRC_DISABLE_STREAMING,
Peter Chen10710552014-01-10 13:51:28 +080038};
39
Peter Chene14db482015-02-11 12:44:47 +080040static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080041 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Peter Chen0ef877a2014-10-30 11:10:04 +080042 CI_HDRC_TURN_VBUS_EARLY_ON |
43 CI_HDRC_DISABLE_STREAMING,
Peter Chene14db482015-02-11 12:44:47 +080044};
45
46static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080047 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Peter Chen0ef877a2014-10-30 11:10:04 +080048 CI_HDRC_TURN_VBUS_EARLY_ON |
49 CI_HDRC_DISABLE_HOST_STREAMING,
Peter Chene14db482015-02-11 12:44:47 +080050};
51
52static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080053 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Peter Chen0ef877a2014-10-30 11:10:04 +080054 CI_HDRC_TURN_VBUS_EARLY_ON |
55 CI_HDRC_DISABLE_HOST_STREAMING,
Peter Chene14db482015-02-11 12:44:47 +080056};
57
Peter Chen52fe5682015-09-16 15:52:32 +080058static const struct ci_hdrc_imx_platform_flag imx6ul_usb_data = {
59 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Fabio Estevamc7721e12020-12-07 10:09:09 +080060 CI_HDRC_TURN_VBUS_EARLY_ON |
61 CI_HDRC_DISABLE_DEVICE_STREAMING,
Peter Chen52fe5682015-09-16 15:52:32 +080062};
63
Peter Chen5cb377c2015-09-09 16:33:02 +080064static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = {
65 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM,
66};
67
Peter Chend1609c32019-04-28 10:35:31 +080068static const struct ci_hdrc_imx_platform_flag imx7ulp_usb_data = {
69 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
70 CI_HDRC_PMQOS,
71};
72
Peter Chen10710552014-01-10 13:51:28 +080073static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
Stefan Wahren81345722015-12-29 16:41:08 +000074 { .compatible = "fsl,imx23-usb", .data = &imx23_usb_data},
Peter Chen10710552014-01-10 13:51:28 +080075 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
76 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
Peter Chene14db482015-02-11 12:44:47 +080077 { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
78 { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
Li Jun8315b772015-09-16 14:46:32 +080079 { .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
Peter Chen52fe5682015-09-16 15:52:32 +080080 { .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data},
Peter Chen5cb377c2015-09-09 16:33:02 +080081 { .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data},
Peter Chend1609c32019-04-28 10:35:31 +080082 { .compatible = "fsl,imx7ulp-usb", .data = &imx7ulp_usb_data},
Peter Chen10710552014-01-10 13:51:28 +080083 { /* sentinel */ }
84};
85MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
86
Alexander Shishkin8e229782013-06-24 14:46:36 +030087struct ci_hdrc_imx_data {
Richard Zhao15302802012-07-07 22:56:48 +080088 struct usb_phy *phy;
89 struct platform_device *ci_pdev;
90 struct clk *clk;
Sascha Hauer05986ba2013-08-14 12:44:16 +030091 struct imx_usbmisc_data *usbmisc_data;
Peter Chene14db482015-02-11 12:44:47 +080092 bool supports_runtime_pm;
Sebastian Reichelbe9cae22018-03-29 17:52:49 +080093 bool override_phy_control;
Peter Chene14db482015-02-11 12:44:47 +080094 bool in_lpm;
Peter Chen7c8e8902018-10-16 09:17:02 +080095 struct pinctrl *pinctrl;
96 struct pinctrl_state *pinctrl_hsic_active;
97 struct regulator *hsic_pad_regulator;
Peter Chenae3e57a2015-09-16 09:40:51 +080098 /* SoC before i.mx6 (except imx23/imx28) needs three clks */
99 bool need_three_clks;
100 struct clk *clk_ipg;
101 struct clk *clk_ahb;
102 struct clk *clk_per;
103 /* --------------------------------- */
Peter Chend1609c32019-04-28 10:35:31 +0800104 struct pm_qos_request pm_qos_req;
105 const struct ci_hdrc_imx_platform_flag *plat_data;
Richard Zhao15302802012-07-07 22:56:48 +0800106};
107
Richard Zhaod142d6b2012-09-12 14:58:05 +0300108/* Common functions shared by usbmisc drivers */
109
Sascha Hauer05986ba2013-08-14 12:44:16 +0300110static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300111{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800112 struct platform_device *misc_pdev;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300113 struct device_node *np = dev->of_node;
114 struct of_phandle_args args;
Sascha Hauer05986ba2013-08-14 12:44:16 +0300115 struct imx_usbmisc_data *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300116 int ret;
117
Sascha Hauer05986ba2013-08-14 12:44:16 +0300118 /*
119 * In case the fsl,usbmisc property is not present this device doesn't
120 * need usbmisc. Return NULL (which is no error here)
121 */
122 if (!of_get_property(np, "fsl,usbmisc", NULL))
123 return NULL;
124
125 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
126 if (!data)
127 return ERR_PTR(-ENOMEM);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300128
129 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
130 0, &args);
131 if (ret) {
132 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
133 ret);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300134 return ERR_PTR(ret);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300135 }
Sascha Hauer05986ba2013-08-14 12:44:16 +0300136
137 data->index = args.args[0];
Stefan Agnerf40017e2014-09-22 08:14:15 +0800138
139 misc_pdev = of_find_device_by_node(args.np);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300140 of_node_put(args.np);
141
Yu Kuai83a43ff2020-11-17 09:14:30 +0800142 if (!misc_pdev)
Stefan Agnerf40017e2014-09-22 08:14:15 +0800143 return ERR_PTR(-EPROBE_DEFER);
144
Yu Kuai83a43ff2020-11-17 09:14:30 +0800145 if (!platform_get_drvdata(misc_pdev)) {
146 put_device(&misc_pdev->dev);
147 return ERR_PTR(-EPROBE_DEFER);
148 }
Stefan Agnerf40017e2014-09-22 08:14:15 +0800149 data->dev = &misc_pdev->dev;
150
Uwe Kleine-Königa82bf692018-12-04 09:31:29 +0100151 /*
152 * Check the various over current related properties. If over current
153 * detection is disabled we're not interested in the polarity.
154 */
155 if (of_find_property(np, "disable-over-current", NULL)) {
Sascha Hauer05986ba2013-08-14 12:44:16 +0300156 data->disable_oc = 1;
Uwe Kleine-Königa82bf692018-12-04 09:31:29 +0100157 } else if (of_find_property(np, "over-current-active-high", NULL)) {
158 data->oc_pol_active_low = 0;
159 data->oc_pol_configured = 1;
160 } else if (of_find_property(np, "over-current-active-low", NULL)) {
161 data->oc_pol_active_low = 1;
162 data->oc_pol_configured = 1;
Uwe Kleine-König1bf47432018-12-04 09:31:30 +0100163 } else {
164 dev_warn(dev, "No over current polarity defined\n");
Uwe Kleine-Königa82bf692018-12-04 09:31:29 +0100165 }
Li Jun9dba5162016-07-20 16:02:42 +0800166
Philipp Puschmann5f0632c2019-02-27 06:51:34 +0000167 data->pwr_pol = of_property_read_bool(np, "power-active-high");
168 data->evdo = of_property_read_bool(np, "external-vbus-divider");
Michael Grzeschika0685332013-03-30 12:54:01 +0200169
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200170 if (of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI)
171 data->ulpi = 1;
172
Peter Chen58a3cef2019-09-30 13:56:26 +0800173 of_property_read_u32(np, "samsung,picophy-pre-emp-curr-control",
174 &data->emp_curr_control);
175 of_property_read_u32(np, "samsung,picophy-dc-vol-level-adjust",
176 &data->dc_vol_level_adjust);
177
Sascha Hauer05986ba2013-08-14 12:44:16 +0300178 return data;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300179}
Richard Zhaod142d6b2012-09-12 14:58:05 +0300180
181/* End of common functions shared by usbmisc drivers*/
Peter Chenae3e57a2015-09-16 09:40:51 +0800182static int imx_get_clks(struct device *dev)
183{
184 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
185 int ret = 0;
186
187 data->clk_ipg = devm_clk_get(dev, "ipg");
188 if (IS_ERR(data->clk_ipg)) {
189 /* If the platform only needs one clocks */
190 data->clk = devm_clk_get(dev, NULL);
191 if (IS_ERR(data->clk)) {
192 ret = PTR_ERR(data->clk);
193 dev_err(dev,
194 "Failed to get clks, err=%ld,%ld\n",
195 PTR_ERR(data->clk), PTR_ERR(data->clk_ipg));
196 return ret;
197 }
198 return ret;
199 }
200
201 data->clk_ahb = devm_clk_get(dev, "ahb");
202 if (IS_ERR(data->clk_ahb)) {
203 ret = PTR_ERR(data->clk_ahb);
204 dev_err(dev,
205 "Failed to get ahb clock, err=%d\n", ret);
206 return ret;
207 }
208
209 data->clk_per = devm_clk_get(dev, "per");
210 if (IS_ERR(data->clk_per)) {
211 ret = PTR_ERR(data->clk_per);
212 dev_err(dev,
213 "Failed to get per clock, err=%d\n", ret);
214 return ret;
215 }
216
217 data->need_three_clks = true;
218 return ret;
219}
220
221static int imx_prepare_enable_clks(struct device *dev)
222{
223 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
224 int ret = 0;
225
226 if (data->need_three_clks) {
227 ret = clk_prepare_enable(data->clk_ipg);
228 if (ret) {
229 dev_err(dev,
230 "Failed to prepare/enable ipg clk, err=%d\n",
231 ret);
232 return ret;
233 }
234
235 ret = clk_prepare_enable(data->clk_ahb);
236 if (ret) {
237 dev_err(dev,
238 "Failed to prepare/enable ahb clk, err=%d\n",
239 ret);
240 clk_disable_unprepare(data->clk_ipg);
241 return ret;
242 }
243
244 ret = clk_prepare_enable(data->clk_per);
245 if (ret) {
246 dev_err(dev,
247 "Failed to prepare/enable per clk, err=%d\n",
248 ret);
249 clk_disable_unprepare(data->clk_ahb);
250 clk_disable_unprepare(data->clk_ipg);
251 return ret;
252 }
253 } else {
254 ret = clk_prepare_enable(data->clk);
255 if (ret) {
256 dev_err(dev,
257 "Failed to prepare/enable clk, err=%d\n",
258 ret);
259 return ret;
260 }
261 }
262
263 return ret;
264}
265
266static void imx_disable_unprepare_clks(struct device *dev)
267{
268 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
269
270 if (data->need_three_clks) {
271 clk_disable_unprepare(data->clk_per);
272 clk_disable_unprepare(data->clk_ahb);
273 clk_disable_unprepare(data->clk_ipg);
274 } else {
275 clk_disable_unprepare(data->clk);
276 }
277}
Richard Zhaod142d6b2012-09-12 14:58:05 +0300278
Peter Chen7c8e8902018-10-16 09:17:02 +0800279static int ci_hdrc_imx_notify_event(struct ci_hdrc *ci, unsigned int event)
280{
281 struct device *dev = ci->dev->parent;
282 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
283 int ret = 0;
Jun Li746f3162020-01-23 14:35:58 +0800284 struct imx_usbmisc_data *mdata = data->usbmisc_data;
Peter Chen7c8e8902018-10-16 09:17:02 +0800285
286 switch (event) {
287 case CI_HDRC_IMX_HSIC_ACTIVE_EVENT:
Peter Chen4d614122019-10-10 09:20:26 +0800288 if (data->pinctrl) {
289 ret = pinctrl_select_state(data->pinctrl,
290 data->pinctrl_hsic_active);
291 if (ret)
292 dev_err(dev,
293 "hsic_active select failed, err=%d\n",
294 ret);
295 }
Peter Chen7c8e8902018-10-16 09:17:02 +0800296 break;
297 case CI_HDRC_IMX_HSIC_SUSPEND_EVENT:
Jun Li746f3162020-01-23 14:35:58 +0800298 ret = imx_usbmisc_hsic_set_connect(mdata);
Peter Chen7c8e8902018-10-16 09:17:02 +0800299 if (ret)
300 dev_err(dev,
301 "hsic_set_connect failed, err=%d\n", ret);
302 break;
Jun Li746f3162020-01-23 14:35:58 +0800303 case CI_HDRC_CONTROLLER_VBUS_EVENT:
304 if (ci->vbus_active)
305 ret = imx_usbmisc_charger_detection(mdata, true);
306 else
307 ret = imx_usbmisc_charger_detection(mdata, false);
308 if (ci->usb_phy)
309 schedule_work(&ci->usb_phy->chg_work);
310 break;
Peter Chen7c8e8902018-10-16 09:17:02 +0800311 default:
312 break;
313 }
314
315 return ret;
316}
317
Alexander Shishkin8e229782013-06-24 14:46:36 +0300318static int ci_hdrc_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800319{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300320 struct ci_hdrc_imx_data *data;
321 struct ci_hdrc_platform_data pdata = {
Alexander Shiyanc844d6c2014-03-11 13:47:38 +0800322 .name = dev_name(&pdev->dev),
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300323 .capoffset = DEF_CAPOFFSET,
Peter Chen7c8e8902018-10-16 09:17:02 +0800324 .notify_event = ci_hdrc_imx_notify_event,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300325 };
Richard Zhao15302802012-07-07 22:56:48 +0800326 int ret;
LABBE Corentin6f51bc32015-11-12 08:43:34 +0100327 const struct ci_hdrc_imx_platform_flag *imx_platform_flag;
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800328 struct device_node *np = pdev->dev.of_node;
Peter Chen7c8e8902018-10-16 09:17:02 +0800329 struct device *dev = &pdev->dev;
LABBE Corentin6f51bc32015-11-12 08:43:34 +0100330
Fabio Estevam59b7c6a2020-11-24 13:39:12 -0300331 imx_platform_flag = of_device_get_match_data(&pdev->dev);
Richard Zhao15302802012-07-07 22:56:48 +0800332
333 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Fabio Estevam735298282014-11-26 13:44:24 +0800334 if (!data)
Richard Zhao15302802012-07-07 22:56:48 +0800335 return -ENOMEM;
Richard Zhao15302802012-07-07 22:56:48 +0800336
Peter Chend1609c32019-04-28 10:35:31 +0800337 data->plat_data = imx_platform_flag;
338 pdata.flags |= imx_platform_flag->flags;
Peter Chenae3e57a2015-09-16 09:40:51 +0800339 platform_set_drvdata(pdev, data);
Peter Chen7c8e8902018-10-16 09:17:02 +0800340 data->usbmisc_data = usbmisc_get_init_data(dev);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300341 if (IS_ERR(data->usbmisc_data))
342 return PTR_ERR(data->usbmisc_data);
343
Peter Chen8ff396f2019-01-17 09:24:20 +0000344 if ((of_usb_get_phy_mode(dev->of_node) == USBPHY_INTERFACE_MODE_HSIC)
345 && data->usbmisc_data) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800346 pdata.flags |= CI_HDRC_IMX_IS_HSIC;
347 data->usbmisc_data->hsic = 1;
348 data->pinctrl = devm_pinctrl_get(dev);
Peter Chen3f4aad62019-10-10 08:59:14 +0800349 if (PTR_ERR(data->pinctrl) == -ENODEV)
350 data->pinctrl = NULL;
351 else if (IS_ERR(data->pinctrl)) {
352 if (PTR_ERR(data->pinctrl) != -EPROBE_DEFER)
353 dev_err(dev, "pinctrl get failed, err=%ld\n",
Peter Chen7c8e8902018-10-16 09:17:02 +0800354 PTR_ERR(data->pinctrl));
355 return PTR_ERR(data->pinctrl);
356 }
Peter Chenae3e57a2015-09-16 09:40:51 +0800357
Peter Chen4d614122019-10-10 09:20:26 +0800358 data->hsic_pad_regulator =
359 devm_regulator_get_optional(dev, "hsic");
360 if (PTR_ERR(data->hsic_pad_regulator) == -ENODEV) {
361 /* no pad regualator is needed */
362 data->hsic_pad_regulator = NULL;
363 } else if (IS_ERR(data->hsic_pad_regulator)) {
364 if (PTR_ERR(data->hsic_pad_regulator) != -EPROBE_DEFER)
365 dev_err(dev,
366 "Get HSIC pad regulator error: %ld\n",
367 PTR_ERR(data->hsic_pad_regulator));
368 return PTR_ERR(data->hsic_pad_regulator);
369 }
370
371 if (data->hsic_pad_regulator) {
372 ret = regulator_enable(data->hsic_pad_regulator);
373 if (ret) {
374 dev_err(dev,
375 "Failed to enable HSIC pad regulator\n");
376 return ret;
377 }
378 }
379 }
380
381 /* HSIC pinctrl handling */
382 if (data->pinctrl) {
383 struct pinctrl_state *pinctrl_hsic_idle;
384
Peter Chen7c8e8902018-10-16 09:17:02 +0800385 pinctrl_hsic_idle = pinctrl_lookup_state(data->pinctrl, "idle");
386 if (IS_ERR(pinctrl_hsic_idle)) {
387 dev_err(dev,
388 "pinctrl_hsic_idle lookup failed, err=%ld\n",
389 PTR_ERR(pinctrl_hsic_idle));
390 return PTR_ERR(pinctrl_hsic_idle);
391 }
Richard Zhao15302802012-07-07 22:56:48 +0800392
Peter Chen7c8e8902018-10-16 09:17:02 +0800393 ret = pinctrl_select_state(data->pinctrl, pinctrl_hsic_idle);
394 if (ret) {
395 dev_err(dev, "hsic_idle select failed, err=%d\n", ret);
396 return ret;
397 }
398
399 data->pinctrl_hsic_active = pinctrl_lookup_state(data->pinctrl,
400 "active");
401 if (IS_ERR(data->pinctrl_hsic_active)) {
402 dev_err(dev,
403 "pinctrl_hsic_active lookup failed, err=%ld\n",
404 PTR_ERR(data->pinctrl_hsic_active));
405 return PTR_ERR(data->pinctrl_hsic_active);
406 }
Peter Chen7c8e8902018-10-16 09:17:02 +0800407 }
Peter Chend1609c32019-04-28 10:35:31 +0800408
409 if (pdata.flags & CI_HDRC_PMQOS)
Rafael J. Wysocki77b35242020-02-12 00:28:44 +0100410 cpu_latency_qos_add_request(&data->pm_qos_req, 0);
Peter Chend1609c32019-04-28 10:35:31 +0800411
Peter Chen7c8e8902018-10-16 09:17:02 +0800412 ret = imx_get_clks(dev);
413 if (ret)
414 goto disable_hsic_regulator;
415
416 ret = imx_prepare_enable_clks(dev);
417 if (ret)
418 goto disable_hsic_regulator;
419
420 data->phy = devm_usb_get_phy_by_phandle(dev, "fsl,usbphy", 0);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800421 if (IS_ERR(data->phy)) {
422 ret = PTR_ERR(data->phy);
Fabio Estevam8253a342021-09-21 08:37:54 -0300423 if (ret == -ENODEV) {
424 data->phy = devm_usb_get_phy_by_phandle(dev, "phys", 0);
425 if (IS_ERR(data->phy)) {
426 ret = PTR_ERR(data->phy);
427 if (ret == -ENODEV)
428 data->phy = NULL;
429 else
430 goto err_clk;
431 }
432 }
Richard Zhao15302802012-07-07 22:56:48 +0800433 }
434
Antoine Tenartef44cb42014-10-30 18:41:16 +0100435 pdata.usb_phy = data->phy;
Jun Li746f3162020-01-23 14:35:58 +0800436 if (data->usbmisc_data)
437 data->usbmisc_data->usb_phy = data->phy;
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800438
Andrey Smirnov03e62752018-05-30 10:34:14 -0700439 if ((of_device_is_compatible(np, "fsl,imx53-usb") ||
440 of_device_is_compatible(np, "fsl,imx51-usb")) && pdata.usb_phy &&
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800441 of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI) {
442 pdata.flags |= CI_HDRC_OVERRIDE_PHY_CONTROL;
443 data->override_phy_control = true;
444 usb_phy_init(pdata.usb_phy);
445 }
446
Peter Chene14db482015-02-11 12:44:47 +0800447 if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
448 data->supports_runtime_pm = true;
Peter Chen10710552014-01-10 13:51:28 +0800449
Peter Chena4cf1b12015-02-11 12:44:44 +0800450 ret = imx_usbmisc_init(data->usbmisc_data);
451 if (ret) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800452 dev_err(dev, "usbmisc init failed, ret=%d\n", ret);
Peter Chena4cf1b12015-02-11 12:44:44 +0800453 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300454 }
455
Peter Chen7c8e8902018-10-16 09:17:02 +0800456 data->ci_pdev = ci_hdrc_add_device(dev,
Richard Zhao15302802012-07-07 22:56:48 +0800457 pdev->resource, pdev->num_resources,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300458 &pdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300459 if (IS_ERR(data->ci_pdev)) {
460 ret = PTR_ERR(data->ci_pdev);
Stefan Agnerd3d84252016-01-27 16:55:45 -0800461 if (ret != -EPROBE_DEFER)
Peter Chen7c8e8902018-10-16 09:17:02 +0800462 dev_err(dev, "ci_hdrc_add_device failed, err=%d\n",
463 ret);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800464 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800465 }
466
Li Jundf17aa92019-10-09 10:52:28 +0800467 if (data->usbmisc_data) {
468 if (!IS_ERR(pdata.id_extcon.edev) ||
469 of_property_read_bool(np, "usb-role-switch"))
470 data->usbmisc_data->ext_id = 1;
Li Jun93c2c732019-09-09 14:41:41 +0800471
Li Jundf17aa92019-10-09 10:52:28 +0800472 if (!IS_ERR(pdata.vbus_extcon.edev) ||
473 of_property_read_bool(np, "usb-role-switch"))
474 data->usbmisc_data->ext_vbus = 1;
Peter Chend6f93d22020-07-28 14:12:11 +0800475
476 /* usbmisc needs to know dr mode to choose wakeup setting */
477 data->usbmisc_data->available_role =
478 ci_hdrc_query_available_role(data->ci_pdev);
Li Jundf17aa92019-10-09 10:52:28 +0800479 }
Li Jun93c2c732019-09-09 14:41:41 +0800480
Peter Chena4cf1b12015-02-11 12:44:44 +0800481 ret = imx_usbmisc_init_post(data->usbmisc_data);
482 if (ret) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800483 dev_err(dev, "usbmisc post failed, ret=%d\n", ret);
Peter Chena4cf1b12015-02-11 12:44:44 +0800484 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200485 }
486
Peter Chene14db482015-02-11 12:44:47 +0800487 if (data->supports_runtime_pm) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800488 pm_runtime_set_active(dev);
489 pm_runtime_enable(dev);
Peter Chene14db482015-02-11 12:44:47 +0800490 }
Richard Zhao15302802012-07-07 22:56:48 +0800491
Peter Chen7c8e8902018-10-16 09:17:02 +0800492 device_set_wakeup_capable(dev, true);
Peter Chen6d653112015-02-11 12:44:49 +0800493
Richard Zhao15302802012-07-07 22:56:48 +0800494 return 0;
495
Fabio Estevam770719d2013-06-13 17:59:48 +0300496disable_device:
Alexander Shishkin8e229782013-06-24 14:46:36 +0300497 ci_hdrc_remove_device(data->ci_pdev);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300498err_clk:
Peter Chen7c8e8902018-10-16 09:17:02 +0800499 imx_disable_unprepare_clks(dev);
500disable_hsic_regulator:
501 if (data->hsic_pad_regulator)
André Draszik141822a2019-08-10 16:07:58 +0100502 /* don't overwrite original ret (cf. EPROBE_DEFER) */
503 regulator_disable(data->hsic_pad_regulator);
Peter Chend1609c32019-04-28 10:35:31 +0800504 if (pdata.flags & CI_HDRC_PMQOS)
Rafael J. Wysocki77b35242020-02-12 00:28:44 +0100505 cpu_latency_qos_remove_request(&data->pm_qos_req);
André Draszik141822a2019-08-10 16:07:58 +0100506 data->ci_pdev = NULL;
Richard Zhao15302802012-07-07 22:56:48 +0800507 return ret;
508}
509
Alexander Shishkin8e229782013-06-24 14:46:36 +0300510static int ci_hdrc_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800511{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300512 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800513
Peter Chene14db482015-02-11 12:44:47 +0800514 if (data->supports_runtime_pm) {
515 pm_runtime_get_sync(&pdev->dev);
516 pm_runtime_disable(&pdev->dev);
517 pm_runtime_put_noidle(&pdev->dev);
518 }
André Draszik141822a2019-08-10 16:07:58 +0100519 if (data->ci_pdev)
520 ci_hdrc_remove_device(data->ci_pdev);
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800521 if (data->override_phy_control)
522 usb_phy_shutdown(data->phy);
André Draszik141822a2019-08-10 16:07:58 +0100523 if (data->ci_pdev) {
524 imx_disable_unprepare_clks(&pdev->dev);
525 if (data->plat_data->flags & CI_HDRC_PMQOS)
Rafael J. Wysocki77b35242020-02-12 00:28:44 +0100526 cpu_latency_qos_remove_request(&data->pm_qos_req);
André Draszik141822a2019-08-10 16:07:58 +0100527 if (data->hsic_pad_regulator)
528 regulator_disable(data->hsic_pad_regulator);
529 }
Richard Zhao15302802012-07-07 22:56:48 +0800530
Richard Zhao15302802012-07-07 22:56:48 +0800531 return 0;
532}
533
Andreas Fenkartb09b5222015-11-12 14:14:40 +0100534static void ci_hdrc_imx_shutdown(struct platform_device *pdev)
535{
536 ci_hdrc_imx_remove(pdev);
537}
538
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200539static int __maybe_unused imx_controller_suspend(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800540{
541 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
Peter Chen7c8e8902018-10-16 09:17:02 +0800542 int ret = 0;
Peter Chen2558c1f2014-11-26 13:44:30 +0800543
544 dev_dbg(dev, "at %s\n", __func__);
545
Peter Chen7c8e8902018-10-16 09:17:02 +0800546 ret = imx_usbmisc_hsic_set_clk(data->usbmisc_data, false);
547 if (ret) {
548 dev_err(dev, "usbmisc hsic_set_clk failed, ret=%d\n", ret);
549 return ret;
550 }
551
Peter Chenae3e57a2015-09-16 09:40:51 +0800552 imx_disable_unprepare_clks(dev);
Peter Chend1609c32019-04-28 10:35:31 +0800553 if (data->plat_data->flags & CI_HDRC_PMQOS)
Rafael J. Wysocki77b35242020-02-12 00:28:44 +0100554 cpu_latency_qos_remove_request(&data->pm_qos_req);
Peter Chend1609c32019-04-28 10:35:31 +0800555
Peter Chene14db482015-02-11 12:44:47 +0800556 data->in_lpm = true;
Peter Chen2558c1f2014-11-26 13:44:30 +0800557
558 return 0;
559}
560
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200561static int __maybe_unused imx_controller_resume(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800562{
563 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
Peter Chene14db482015-02-11 12:44:47 +0800564 int ret = 0;
Peter Chen2558c1f2014-11-26 13:44:30 +0800565
566 dev_dbg(dev, "at %s\n", __func__);
567
Peter Chene14db482015-02-11 12:44:47 +0800568 if (!data->in_lpm) {
569 WARN_ON(1);
570 return 0;
571 }
572
Peter Chend1609c32019-04-28 10:35:31 +0800573 if (data->plat_data->flags & CI_HDRC_PMQOS)
Rafael J. Wysocki77b35242020-02-12 00:28:44 +0100574 cpu_latency_qos_add_request(&data->pm_qos_req, 0);
Peter Chend1609c32019-04-28 10:35:31 +0800575
Peter Chenae3e57a2015-09-16 09:40:51 +0800576 ret = imx_prepare_enable_clks(dev);
Peter Chene14db482015-02-11 12:44:47 +0800577 if (ret)
578 return ret;
579
580 data->in_lpm = false;
581
582 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
583 if (ret) {
584 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
585 goto clk_disable;
586 }
587
Peter Chen7c8e8902018-10-16 09:17:02 +0800588 ret = imx_usbmisc_hsic_set_clk(data->usbmisc_data, true);
589 if (ret) {
590 dev_err(dev, "usbmisc hsic_set_clk failed, ret=%d\n", ret);
591 goto hsic_set_clk_fail;
592 }
593
Peter Chene14db482015-02-11 12:44:47 +0800594 return 0;
595
Peter Chen7c8e8902018-10-16 09:17:02 +0800596hsic_set_clk_fail:
597 imx_usbmisc_set_wakeup(data->usbmisc_data, true);
Peter Chene14db482015-02-11 12:44:47 +0800598clk_disable:
Peter Chenae3e57a2015-09-16 09:40:51 +0800599 imx_disable_unprepare_clks(dev);
Peter Chene14db482015-02-11 12:44:47 +0800600 return ret;
Peter Chen2558c1f2014-11-26 13:44:30 +0800601}
602
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200603static int __maybe_unused ci_hdrc_imx_suspend(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800604{
Peter Chen6d653112015-02-11 12:44:49 +0800605 int ret;
606
Peter Chene14db482015-02-11 12:44:47 +0800607 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
608
609 if (data->in_lpm)
610 /* The core's suspend doesn't run */
611 return 0;
612
Peter Chen6d653112015-02-11 12:44:49 +0800613 if (device_may_wakeup(dev)) {
614 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
615 if (ret) {
616 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
617 ret);
618 return ret;
619 }
620 }
621
Peter Chen71ac6802018-11-01 14:47:20 +0800622 ret = imx_controller_suspend(dev);
623 if (ret)
624 return ret;
625
626 pinctrl_pm_select_sleep_state(dev);
627 return ret;
Peter Chen2558c1f2014-11-26 13:44:30 +0800628}
629
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200630static int __maybe_unused ci_hdrc_imx_resume(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800631{
Peter Chene14db482015-02-11 12:44:47 +0800632 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
633 int ret;
634
Peter Chen71ac6802018-11-01 14:47:20 +0800635 pinctrl_pm_select_default_state(dev);
Peter Chene14db482015-02-11 12:44:47 +0800636 ret = imx_controller_resume(dev);
637 if (!ret && data->supports_runtime_pm) {
638 pm_runtime_disable(dev);
639 pm_runtime_set_active(dev);
640 pm_runtime_enable(dev);
641 }
642
643 return ret;
Peter Chen2558c1f2014-11-26 13:44:30 +0800644}
Peter Chen2558c1f2014-11-26 13:44:30 +0800645
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200646static int __maybe_unused ci_hdrc_imx_runtime_suspend(struct device *dev)
Peter Chene14db482015-02-11 12:44:47 +0800647{
648 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
649 int ret;
650
651 if (data->in_lpm) {
652 WARN_ON(1);
653 return 0;
654 }
655
656 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
657 if (ret) {
658 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
659 return ret;
660 }
661
662 return imx_controller_suspend(dev);
663}
664
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200665static int __maybe_unused ci_hdrc_imx_runtime_resume(struct device *dev)
Peter Chene14db482015-02-11 12:44:47 +0800666{
667 return imx_controller_resume(dev);
668}
669
Peter Chen2558c1f2014-11-26 13:44:30 +0800670static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
671 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
Peter Chene14db482015-02-11 12:44:47 +0800672 SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
673 ci_hdrc_imx_runtime_resume, NULL)
Peter Chen2558c1f2014-11-26 13:44:30 +0800674};
Alexander Shishkin8e229782013-06-24 14:46:36 +0300675static struct platform_driver ci_hdrc_imx_driver = {
676 .probe = ci_hdrc_imx_probe,
677 .remove = ci_hdrc_imx_remove,
Andreas Fenkartb09b5222015-11-12 14:14:40 +0100678 .shutdown = ci_hdrc_imx_shutdown,
Richard Zhao15302802012-07-07 22:56:48 +0800679 .driver = {
680 .name = "imx_usb",
Alexander Shishkin8e229782013-06-24 14:46:36 +0300681 .of_match_table = ci_hdrc_imx_dt_ids,
Peter Chen2558c1f2014-11-26 13:44:30 +0800682 .pm = &ci_hdrc_imx_pm_ops,
Richard Zhao15302802012-07-07 22:56:48 +0800683 },
684};
685
Alexander Shishkin8e229782013-06-24 14:46:36 +0300686module_platform_driver(ci_hdrc_imx_driver);
Richard Zhao15302802012-07-07 22:56:48 +0800687
688MODULE_ALIAS("platform:imx-usb");
Marcus Folkesson1f060722018-09-02 19:36:50 +0200689MODULE_LICENSE("GPL");
Alexander Shishkin8e229782013-06-24 14:46:36 +0300690MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
Richard Zhao15302802012-07-07 22:56:48 +0800691MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
692MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");