blob: e64754be47b46f8e306cc48b7068b5c8043dae32 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbi16adc672015-11-18 13:15:20 -06002/**
3 * dwc3-of-simple.c - OF glue layer for simple integrations
4 *
5 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Author: Felipe Balbi <balbi@ti.com>
8 *
Felipe Balbi16adc672015-11-18 13:15:20 -06009 * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
10 * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
11 * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
19#include <linux/clk.h>
Felipe Balbi16adc672015-11-18 13:15:20 -060020#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/pm_runtime.h>
Vivek Gautam06c47e62017-10-19 13:47:43 +020023#include <linux/reset.h>
Felipe Balbi16adc672015-11-18 13:15:20 -060024
25struct dwc3_of_simple {
26 struct device *dev;
Robin Murphyc0c61472019-03-27 19:24:06 +000027 struct clk_bulk_data *clks;
Felipe Balbi16adc672015-11-18 13:15:20 -060028 int num_clocks;
Vivek Gautam06c47e62017-10-19 13:47:43 +020029 struct reset_control *resets;
Martin Blumenstinglff0a6322018-02-11 22:15:16 +010030 bool pulse_resets;
Enric Balletbo i Serra76251db2018-07-16 12:25:47 +020031 bool need_reset;
Felipe Balbi16adc672015-11-18 13:15:20 -060032};
33
Felipe Balbi26c9cac2016-09-12 21:20:22 +030034static int dwc3_of_simple_probe(struct platform_device *pdev)
35{
36 struct dwc3_of_simple *simple;
37 struct device *dev = &pdev->dev;
38 struct device_node *np = dev->of_node;
39
40 int ret;
Martin Blumenstinglff0a6322018-02-11 22:15:16 +010041 bool shared_resets = false;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030042
43 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
44 if (!simple)
45 return -ENOMEM;
46
47 platform_set_drvdata(pdev, simple);
48 simple->dev = dev;
49
Enric Balletbo i Serra76251db2018-07-16 12:25:47 +020050 /*
51 * Some controllers need to toggle the usb3-otg reset before trying to
52 * initialize the PHY, otherwise the PHY times out.
53 */
54 if (of_device_is_compatible(np, "rockchip,rk3399-dwc3"))
55 simple->need_reset = true;
56
Martin Blumenstingle8284db2018-02-11 22:15:17 +010057 if (of_device_is_compatible(np, "amlogic,meson-axg-dwc3") ||
58 of_device_is_compatible(np, "amlogic,meson-gxl-dwc3")) {
59 shared_resets = true;
60 simple->pulse_resets = true;
61 }
62
Thierry Redingf31d5c22019-02-21 16:25:54 +010063 simple->resets = of_reset_control_array_get(np, shared_resets, true,
64 true);
Vivek Gautam06c47e62017-10-19 13:47:43 +020065 if (IS_ERR(simple->resets)) {
66 ret = PTR_ERR(simple->resets);
67 dev_err(dev, "failed to get device resets, err=%d\n", ret);
68 return ret;
69 }
70
Martin Blumenstinglff0a6322018-02-11 22:15:16 +010071 if (simple->pulse_resets) {
72 ret = reset_control_reset(simple->resets);
73 if (ret)
74 goto err_resetc_put;
75 } else {
76 ret = reset_control_deassert(simple->resets);
77 if (ret)
78 goto err_resetc_put;
79 }
Vivek Gautam06c47e62017-10-19 13:47:43 +020080
Robin Murphyc0c61472019-03-27 19:24:06 +000081 ret = clk_bulk_get_all(simple->dev, &simple->clks);
82 if (ret < 0)
83 goto err_resetc_assert;
84
85 simple->num_clocks = ret;
86 ret = clk_bulk_prepare_enable(simple->num_clocks, simple->clks);
Felipe Balbi26c9cac2016-09-12 21:20:22 +030087 if (ret)
Vivek Gautam06c47e62017-10-19 13:47:43 +020088 goto err_resetc_assert;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030089
Felipe Balbi16adc672015-11-18 13:15:20 -060090 ret = of_platform_populate(np, NULL, NULL, dev);
Robin Murphyc0c61472019-03-27 19:24:06 +000091 if (ret)
92 goto err_clk_put;
Felipe Balbi16adc672015-11-18 13:15:20 -060093
94 pm_runtime_set_active(dev);
95 pm_runtime_enable(dev);
96 pm_runtime_get_sync(dev);
97
98 return 0;
Vivek Gautam06c47e62017-10-19 13:47:43 +020099
Robin Murphyc0c61472019-03-27 19:24:06 +0000100err_clk_put:
101 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
102 clk_bulk_put_all(simple->num_clocks, simple->clks);
103
Vivek Gautam06c47e62017-10-19 13:47:43 +0200104err_resetc_assert:
Martin Blumenstinglff0a6322018-02-11 22:15:16 +0100105 if (!simple->pulse_resets)
106 reset_control_assert(simple->resets);
Vivek Gautam06c47e62017-10-19 13:47:43 +0200107
108err_resetc_put:
109 reset_control_put(simple->resets);
110 return ret;
Felipe Balbi16adc672015-11-18 13:15:20 -0600111}
112
Felipe Balbi726b4fb2019-10-29 12:56:11 +0200113static void __dwc3_of_simple_teardown(struct dwc3_of_simple *simple)
Felipe Balbi16adc672015-11-18 13:15:20 -0600114{
Felipe Balbi726b4fb2019-10-29 12:56:11 +0200115 of_platform_depopulate(simple->dev);
Vivek Gautamd6d9c2a2017-07-19 17:59:06 +0200116
Robin Murphyc0c61472019-03-27 19:24:06 +0000117 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
118 clk_bulk_put_all(simple->num_clocks, simple->clks);
Enric Balletbo i Serrabff52352017-12-18 16:14:36 +0100119 simple->num_clocks = 0;
Felipe Balbi16adc672015-11-18 13:15:20 -0600120
Martin Blumenstinglff0a6322018-02-11 22:15:16 +0100121 if (!simple->pulse_resets)
122 reset_control_assert(simple->resets);
123
Vivek Gautam06c47e62017-10-19 13:47:43 +0200124 reset_control_put(simple->resets);
125
Felipe Balbi726b4fb2019-10-29 12:56:11 +0200126 pm_runtime_disable(simple->dev);
127 pm_runtime_put_noidle(simple->dev);
128 pm_runtime_set_suspended(simple->dev);
129}
130
131static int dwc3_of_simple_remove(struct platform_device *pdev)
132{
133 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
134
135 __dwc3_of_simple_teardown(simple);
Felipe Balbi16adc672015-11-18 13:15:20 -0600136
137 return 0;
138}
139
Felipe Balbi726b4fb2019-10-29 12:56:11 +0200140static void dwc3_of_simple_shutdown(struct platform_device *pdev)
141{
142 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
143
144 __dwc3_of_simple_teardown(simple);
145}
146
Arnd Bergmann66174b62018-08-13 23:56:55 +0200147static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
Felipe Balbi16adc672015-11-18 13:15:20 -0600148{
149 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
Felipe Balbi16adc672015-11-18 13:15:20 -0600150
Robin Murphyc0c61472019-03-27 19:24:06 +0000151 clk_bulk_disable(simple->num_clocks, simple->clks);
Felipe Balbi16adc672015-11-18 13:15:20 -0600152
153 return 0;
154}
155
Arnd Bergmann66174b62018-08-13 23:56:55 +0200156static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
Felipe Balbi16adc672015-11-18 13:15:20 -0600157{
158 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
Felipe Balbi16adc672015-11-18 13:15:20 -0600159
Robin Murphyc0c61472019-03-27 19:24:06 +0000160 return clk_bulk_enable(simple->num_clocks, simple->clks);
Felipe Balbi16adc672015-11-18 13:15:20 -0600161}
Enric Balletbo i Serra76251db2018-07-16 12:25:47 +0200162
Arnd Bergmann66174b62018-08-13 23:56:55 +0200163static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
Enric Balletbo i Serra76251db2018-07-16 12:25:47 +0200164{
165 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
166
167 if (simple->need_reset)
168 reset_control_assert(simple->resets);
169
170 return 0;
171}
172
Arnd Bergmann66174b62018-08-13 23:56:55 +0200173static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
Enric Balletbo i Serra76251db2018-07-16 12:25:47 +0200174{
175 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
176
177 if (simple->need_reset)
178 reset_control_deassert(simple->resets);
179
180 return 0;
181}
Felipe Balbi16adc672015-11-18 13:15:20 -0600182
183static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
Enric Balletbo i Serra76251db2018-07-16 12:25:47 +0200184 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
Felipe Balbi16adc672015-11-18 13:15:20 -0600185 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
186 dwc3_of_simple_runtime_resume, NULL)
187};
188
189static const struct of_device_id of_dwc3_simple_match[] = {
William Wuf6521912016-08-16 22:44:36 +0800190 { .compatible = "rockchip,rk3399-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600191 { .compatible = "xlnx,zynqmp-dwc3" },
Felipe Balbib281dc62016-09-12 21:24:58 +0300192 { .compatible = "cavium,octeon-7130-usb-uctl" },
Baolin Wangc3cdce42017-08-30 19:03:52 +0800193 { .compatible = "sprd,sc9860-dwc3" },
Martin Blumenstingle8284db2018-02-11 22:15:17 +0100194 { .compatible = "amlogic,meson-axg-dwc3" },
195 { .compatible = "amlogic,meson-gxl-dwc3" },
Icenowy Zhenge3620982018-05-07 23:18:15 +0800196 { .compatible = "allwinner,sun50i-h6-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600197 { /* Sentinel */ }
198};
199MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
200
201static struct platform_driver dwc3_of_simple_driver = {
202 .probe = dwc3_of_simple_probe,
203 .remove = dwc3_of_simple_remove,
Felipe Balbi726b4fb2019-10-29 12:56:11 +0200204 .shutdown = dwc3_of_simple_shutdown,
Felipe Balbi16adc672015-11-18 13:15:20 -0600205 .driver = {
206 .name = "dwc3-of-simple",
207 .of_match_table = of_dwc3_simple_match,
Masahiro Yamadaa0d8c4c2017-12-07 13:40:24 +0900208 .pm = &dwc3_of_simple_dev_pm_ops,
Felipe Balbi16adc672015-11-18 13:15:20 -0600209 },
210};
211
212module_platform_driver(dwc3_of_simple_driver);
213MODULE_LICENSE("GPL v2");
214MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
215MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");