Merge tag 'for-5.16/dm-changes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / usb / dwc3 / dwc3-of-simple.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
26ef796f 2/*
16adc674
FB
3 * dwc3-of-simple.c - OF glue layer for simple integrations
4 *
10623b87 5 * Copyright (c) 2015 Texas Instruments Incorporated - https://www.ti.com
16adc674
FB
6 *
7 * Author: Felipe Balbi <balbi@ti.com>
8 *
16adc674
FB
9 * 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>
16adc674
FB
20#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/pm_runtime.h>
06c47e62 23#include <linux/reset.h>
16adc674
FB
24
25struct dwc3_of_simple {
26 struct device *dev;
c0c61471 27 struct clk_bulk_data *clks;
16adc674 28 int num_clocks;
06c47e62 29 struct reset_control *resets;
76251db8 30 bool need_reset;
16adc674
FB
31};
32
26c9cac4
FB
33static int dwc3_of_simple_probe(struct platform_device *pdev)
34{
35 struct dwc3_of_simple *simple;
36 struct device *dev = &pdev->dev;
37 struct device_node *np = dev->of_node;
38
39 int ret;
26c9cac4
FB
40
41 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
42 if (!simple)
43 return -ENOMEM;
44
45 platform_set_drvdata(pdev, simple);
46 simple->dev = dev;
47
76251db8
EBS
48 /*
49 * Some controllers need to toggle the usb3-otg reset before trying to
50 * initialize the PHY, otherwise the PHY times out.
51 */
52 if (of_device_is_compatible(np, "rockchip,rk3399-dwc3"))
53 simple->need_reset = true;
54
f4cc91dd 55 simple->resets = of_reset_control_array_get(np, false, true,
f31d5c24 56 true);
06c47e62
VG
57 if (IS_ERR(simple->resets)) {
58 ret = PTR_ERR(simple->resets);
59 dev_err(dev, "failed to get device resets, err=%d\n", ret);
60 return ret;
61 }
62
f4cc91dd
MB
63 ret = reset_control_deassert(simple->resets);
64 if (ret)
65 goto err_resetc_put;
06c47e62 66
c0c61471
RM
67 ret = clk_bulk_get_all(simple->dev, &simple->clks);
68 if (ret < 0)
69 goto err_resetc_assert;
70
71 simple->num_clocks = ret;
72 ret = clk_bulk_prepare_enable(simple->num_clocks, simple->clks);
26c9cac4 73 if (ret)
06c47e62 74 goto err_resetc_assert;
26c9cac4 75
16adc674 76 ret = of_platform_populate(np, NULL, NULL, dev);
c0c61471
RM
77 if (ret)
78 goto err_clk_put;
16adc674
FB
79
80 pm_runtime_set_active(dev);
81 pm_runtime_enable(dev);
82 pm_runtime_get_sync(dev);
83
84 return 0;
06c47e62 85
c0c61471
RM
86err_clk_put:
87 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
88 clk_bulk_put_all(simple->num_clocks, simple->clks);
89
06c47e62 90err_resetc_assert:
f4cc91dd 91 reset_control_assert(simple->resets);
06c47e62
VG
92
93err_resetc_put:
94 reset_control_put(simple->resets);
95 return ret;
16adc674
FB
96}
97
726b4fba 98static void __dwc3_of_simple_teardown(struct dwc3_of_simple *simple)
16adc674 99{
726b4fba 100 of_platform_depopulate(simple->dev);
d6d9c2a3 101
c0c61471
RM
102 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
103 clk_bulk_put_all(simple->num_clocks, simple->clks);
bff52352 104 simple->num_clocks = 0;
16adc674 105
f4cc91dd 106 reset_control_assert(simple->resets);
ff0a632f 107
06c47e62
VG
108 reset_control_put(simple->resets);
109
726b4fba
FB
110 pm_runtime_disable(simple->dev);
111 pm_runtime_put_noidle(simple->dev);
112 pm_runtime_set_suspended(simple->dev);
113}
114
115static int dwc3_of_simple_remove(struct platform_device *pdev)
116{
117 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
118
119 __dwc3_of_simple_teardown(simple);
16adc674
FB
120
121 return 0;
122}
123
726b4fba
FB
124static void dwc3_of_simple_shutdown(struct platform_device *pdev)
125{
126 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
127
128 __dwc3_of_simple_teardown(simple);
129}
130
66174b69 131static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
16adc674
FB
132{
133 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
16adc674 134
c0c61471 135 clk_bulk_disable(simple->num_clocks, simple->clks);
16adc674
FB
136
137 return 0;
138}
139
66174b69 140static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
16adc674
FB
141{
142 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
16adc674 143
c0c61471 144 return clk_bulk_enable(simple->num_clocks, simple->clks);
16adc674 145}
76251db8 146
66174b69 147static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
76251db8
EBS
148{
149 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
150
151 if (simple->need_reset)
152 reset_control_assert(simple->resets);
153
154 return 0;
155}
156
66174b69 157static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
76251db8
EBS
158{
159 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
160
161 if (simple->need_reset)
162 reset_control_deassert(simple->resets);
163
164 return 0;
165}
16adc674
FB
166
167static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
76251db8 168 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
16adc674
FB
169 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
170 dwc3_of_simple_runtime_resume, NULL)
171};
172
173static const struct of_device_id of_dwc3_simple_match[] = {
f652191b 174 { .compatible = "rockchip,rk3399-dwc3" },
b281dc63 175 { .compatible = "cavium,octeon-7130-usb-uctl" },
c3cdce45 176 { .compatible = "sprd,sc9860-dwc3" },
e362098f 177 { .compatible = "allwinner,sun50i-h6-dwc3" },
b68d9251 178 { .compatible = "hisilicon,hi3670-dwc3" },
e2c53515 179 { .compatible = "intel,keembay-dwc3" },
16adc674
FB
180 { /* Sentinel */ }
181};
182MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
183
184static struct platform_driver dwc3_of_simple_driver = {
185 .probe = dwc3_of_simple_probe,
186 .remove = dwc3_of_simple_remove,
726b4fba 187 .shutdown = dwc3_of_simple_shutdown,
16adc674
FB
188 .driver = {
189 .name = "dwc3-of-simple",
190 .of_match_table = of_dwc3_simple_match,
a0d8c4cf 191 .pm = &dwc3_of_simple_dev_pm_ops,
16adc674
FB
192 },
193};
194
195module_platform_driver(dwc3_of_simple_driver);
196MODULE_LICENSE("GPL v2");
197MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
198MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");