blob: e62ecd22b3ed48d427accb2ba68521185ba2acf0 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
Olivier Deprez157378f2022-04-04 15:47:50 +02002/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 * dwc3-of-simple.c - OF glue layer for simple integrations
4 *
Olivier Deprez157378f2022-04-04 15:47:50 +02005 * Copyright (c) 2015 Texas Instruments Incorporated - https://www.ti.com
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 *
7 * Author: Felipe Balbi <balbi@ti.com>
8 *
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>
20#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/pm_runtime.h>
23#include <linux/reset.h>
24
25struct dwc3_of_simple {
26 struct device *dev;
David Brazdil0f672f62019-12-10 10:32:29 +000027 struct clk_bulk_data *clks;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028 int num_clocks;
29 struct reset_control *resets;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000030 bool need_reset;
31};
32
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033static 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040
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
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
Olivier Deprez157378f2022-04-04 15:47:50 +020055 simple->resets = of_reset_control_array_get(np, false, true,
David Brazdil0f672f62019-12-10 10:32:29 +000056 true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057 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
Olivier Deprez157378f2022-04-04 15:47:50 +020063 ret = reset_control_deassert(simple->resets);
64 if (ret)
65 goto err_resetc_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
David Brazdil0f672f62019-12-10 10:32:29 +000067 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073 if (ret)
74 goto err_resetc_assert;
75
76 ret = of_platform_populate(np, NULL, NULL, dev);
David Brazdil0f672f62019-12-10 10:32:29 +000077 if (ret)
78 goto err_clk_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079
80 pm_runtime_set_active(dev);
81 pm_runtime_enable(dev);
82 pm_runtime_get_sync(dev);
83
84 return 0;
85
David Brazdil0f672f62019-12-10 10:32:29 +000086err_clk_put:
87 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
88 clk_bulk_put_all(simple->num_clocks, simple->clks);
89
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090err_resetc_assert:
Olivier Deprez157378f2022-04-04 15:47:50 +020091 reset_control_assert(simple->resets);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092
93err_resetc_put:
94 reset_control_put(simple->resets);
95 return ret;
96}
97
Olivier Deprez157378f2022-04-04 15:47:50 +020098static void __dwc3_of_simple_teardown(struct dwc3_of_simple *simple)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099{
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 of_platform_depopulate(simple->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101
David Brazdil0f672f62019-12-10 10:32:29 +0000102 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
103 clk_bulk_put_all(simple->num_clocks, simple->clks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 simple->num_clocks = 0;
105
Olivier Deprez157378f2022-04-04 15:47:50 +0200106 reset_control_assert(simple->resets);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107
108 reset_control_put(simple->resets);
109
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120
121 return 0;
122}
123
Olivier Deprez157378f2022-04-04 15:47:50 +0200124static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
132{
133 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134
David Brazdil0f672f62019-12-10 10:32:29 +0000135 clk_bulk_disable(simple->num_clocks, simple->clks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136
137 return 0;
138}
139
140static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
141{
142 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143
David Brazdil0f672f62019-12-10 10:32:29 +0000144 return clk_bulk_enable(simple->num_clocks, simple->clks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145}
146
147static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
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
157static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
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}
166
167static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
168 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
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[] = {
174 { .compatible = "rockchip,rk3399-dwc3" },
175 { .compatible = "xlnx,zynqmp-dwc3" },
176 { .compatible = "cavium,octeon-7130-usb-uctl" },
177 { .compatible = "sprd,sc9860-dwc3" },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 { .compatible = "allwinner,sun50i-h6-dwc3" },
Olivier Deprez0e641232021-09-23 10:07:05 +0200179 { .compatible = "hisilicon,hi3670-dwc3" },
Olivier Deprez157378f2022-04-04 15:47:50 +0200180 { .compatible = "intel,keembay-dwc3" },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 { /* Sentinel */ }
182};
183MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
184
185static struct platform_driver dwc3_of_simple_driver = {
186 .probe = dwc3_of_simple_probe,
187 .remove = dwc3_of_simple_remove,
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 .shutdown = dwc3_of_simple_shutdown,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189 .driver = {
190 .name = "dwc3-of-simple",
191 .of_match_table = of_dwc3_simple_match,
192 .pm = &dwc3_of_simple_dev_pm_ops,
193 },
194};
195
196module_platform_driver(dwc3_of_simple_driver);
197MODULE_LICENSE("GPL v2");
198MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
199MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");