Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * dwc3-of-simple.c - OF glue layer for simple integrations
4 *
5 * Copyright (c) 2015 Texas Instruments Incorporated - https://www.ti.com
6 *
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;
27 struct clk_bulk_data *clks;
28 int num_clocks;
29 struct reset_control *resets;
30 bool need_reset;
31};
32
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;
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
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
55 simple->resets = of_reset_control_array_get_optional_exclusive(np);
56 if (IS_ERR(simple->resets)) {
57 ret = PTR_ERR(simple->resets);
58 dev_err(dev, "failed to get device resets, err=%d\n", ret);
59 return ret;
60 }
61
62 ret = reset_control_deassert(simple->resets);
63 if (ret)
64 goto err_resetc_put;
65
66 ret = clk_bulk_get_all(simple->dev, &simple->clks);
67 if (ret < 0)
68 goto err_resetc_assert;
69
70 simple->num_clocks = ret;
71 ret = clk_bulk_prepare_enable(simple->num_clocks, simple->clks);
72 if (ret)
73 goto err_resetc_assert;
74
75 ret = of_platform_populate(np, NULL, NULL, dev);
76 if (ret)
77 goto err_clk_put;
78
79 pm_runtime_set_active(dev);
80 pm_runtime_enable(dev);
81 pm_runtime_get_sync(dev);
82
83 return 0;
84
85err_clk_put:
86 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
87 clk_bulk_put_all(simple->num_clocks, simple->clks);
88
89err_resetc_assert:
90 reset_control_assert(simple->resets);
91
92err_resetc_put:
93 reset_control_put(simple->resets);
94 return ret;
95}
96
97static void __dwc3_of_simple_teardown(struct dwc3_of_simple *simple)
98{
99 of_platform_depopulate(simple->dev);
100
101 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
102 clk_bulk_put_all(simple->num_clocks, simple->clks);
103 simple->num_clocks = 0;
104
105 reset_control_assert(simple->resets);
106
107 reset_control_put(simple->resets);
108
109 pm_runtime_disable(simple->dev);
110 pm_runtime_put_noidle(simple->dev);
111 pm_runtime_set_suspended(simple->dev);
112}
113
114static void dwc3_of_simple_remove(struct platform_device *pdev)
115{
116 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
117
118 __dwc3_of_simple_teardown(simple);
119}
120
121static void dwc3_of_simple_shutdown(struct platform_device *pdev)
122{
123 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
124
125 __dwc3_of_simple_teardown(simple);
126}
127
128static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
129{
130 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
131
132 clk_bulk_disable(simple->num_clocks, simple->clks);
133
134 return 0;
135}
136
137static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
138{
139 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
140
141 return clk_bulk_enable(simple->num_clocks, simple->clks);
142}
143
144static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
145{
146 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
147
148 if (simple->need_reset)
149 reset_control_assert(simple->resets);
150
151 return 0;
152}
153
154static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
155{
156 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
157
158 if (simple->need_reset)
159 reset_control_deassert(simple->resets);
160
161 return 0;
162}
163
164static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
165 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
166 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
167 dwc3_of_simple_runtime_resume, NULL)
168};
169
170static const struct of_device_id of_dwc3_simple_match[] = {
171 { .compatible = "rockchip,rk3399-dwc3" },
172 { .compatible = "sprd,sc9860-dwc3" },
173 { .compatible = "allwinner,sun50i-h6-dwc3" },
174 { .compatible = "hisilicon,hi3670-dwc3" },
175 { .compatible = "hisilicon,hi3798mv200-dwc3" },
176 { .compatible = "intel,keembay-dwc3" },
177 { /* Sentinel */ }
178};
179MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
180
181static struct platform_driver dwc3_of_simple_driver = {
182 .probe = dwc3_of_simple_probe,
183 .remove_new = dwc3_of_simple_remove,
184 .shutdown = dwc3_of_simple_shutdown,
185 .driver = {
186 .name = "dwc3-of-simple",
187 .of_match_table = of_dwc3_simple_match,
188 .pm = &dwc3_of_simple_dev_pm_ops,
189 },
190};
191
192module_platform_driver(dwc3_of_simple_driver);
193MODULE_LICENSE("GPL v2");
194MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
195MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1/**
2 * dwc3-of-simple.c - OF glue layer for simple integrations
3 *
4 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Author: Felipe Balbi <balbi@ti.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 of
10 * the License as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
18 * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
19 * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/platform_device.h>
26#include <linux/dma-mapping.h>
27#include <linux/clk.h>
28#include <linux/clk-provider.h>
29#include <linux/of.h>
30#include <linux/of_platform.h>
31#include <linux/pm_runtime.h>
32
33struct dwc3_of_simple {
34 struct device *dev;
35 struct clk **clks;
36 int num_clocks;
37};
38
39static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
40{
41 struct device *dev = simple->dev;
42 struct device_node *np = dev->of_node;
43 int i;
44
45 simple->num_clocks = count;
46
47 if (!count)
48 return 0;
49
50 simple->clks = devm_kcalloc(dev, simple->num_clocks,
51 sizeof(struct clk *), GFP_KERNEL);
52 if (!simple->clks)
53 return -ENOMEM;
54
55 for (i = 0; i < simple->num_clocks; i++) {
56 struct clk *clk;
57 int ret;
58
59 clk = of_clk_get(np, i);
60 if (IS_ERR(clk)) {
61 while (--i >= 0)
62 clk_put(simple->clks[i]);
63 return PTR_ERR(clk);
64 }
65
66 ret = clk_prepare_enable(clk);
67 if (ret < 0) {
68 while (--i >= 0) {
69 clk_disable_unprepare(simple->clks[i]);
70 clk_put(simple->clks[i]);
71 }
72 clk_put(clk);
73
74 return ret;
75 }
76
77 simple->clks[i] = clk;
78 }
79
80 return 0;
81}
82
83static int dwc3_of_simple_probe(struct platform_device *pdev)
84{
85 struct dwc3_of_simple *simple;
86 struct device *dev = &pdev->dev;
87 struct device_node *np = dev->of_node;
88
89 int ret;
90 int i;
91
92 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
93 if (!simple)
94 return -ENOMEM;
95
96 platform_set_drvdata(pdev, simple);
97 simple->dev = dev;
98
99 ret = dwc3_of_simple_clk_init(simple, of_clk_get_parent_count(np));
100 if (ret)
101 return ret;
102
103 ret = of_platform_populate(np, NULL, NULL, dev);
104 if (ret) {
105 for (i = 0; i < simple->num_clocks; i++) {
106 clk_disable_unprepare(simple->clks[i]);
107 clk_put(simple->clks[i]);
108 }
109
110 return ret;
111 }
112
113 pm_runtime_set_active(dev);
114 pm_runtime_enable(dev);
115 pm_runtime_get_sync(dev);
116
117 return 0;
118}
119
120static int dwc3_of_simple_remove(struct platform_device *pdev)
121{
122 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
123 struct device *dev = &pdev->dev;
124 int i;
125
126 for (i = 0; i < simple->num_clocks; i++) {
127 clk_disable_unprepare(simple->clks[i]);
128 clk_put(simple->clks[i]);
129 }
130
131 of_platform_depopulate(dev);
132
133 pm_runtime_put_sync(dev);
134 pm_runtime_disable(dev);
135
136 return 0;
137}
138
139#ifdef CONFIG_PM
140static int dwc3_of_simple_runtime_suspend(struct device *dev)
141{
142 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
143 int i;
144
145 for (i = 0; i < simple->num_clocks; i++)
146 clk_disable(simple->clks[i]);
147
148 return 0;
149}
150
151static int dwc3_of_simple_runtime_resume(struct device *dev)
152{
153 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
154 int ret;
155 int i;
156
157 for (i = 0; i < simple->num_clocks; i++) {
158 ret = clk_enable(simple->clks[i]);
159 if (ret < 0) {
160 while (--i >= 0)
161 clk_disable(simple->clks[i]);
162 return ret;
163 }
164 }
165
166 return 0;
167}
168#endif
169
170static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
171 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
172 dwc3_of_simple_runtime_resume, NULL)
173};
174
175static const struct of_device_id of_dwc3_simple_match[] = {
176 { .compatible = "qcom,dwc3" },
177 { .compatible = "rockchip,rk3399-dwc3" },
178 { .compatible = "xlnx,zynqmp-dwc3" },
179 { .compatible = "cavium,octeon-7130-usb-uctl" },
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,
187 .driver = {
188 .name = "dwc3-of-simple",
189 .of_match_table = of_dwc3_simple_match,
190 },
191};
192
193module_platform_driver(dwc3_of_simple_driver);
194MODULE_LICENSE("GPL v2");
195MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
196MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");