blob: 7ae0eefc7cc7daf0382c7aeaa56c14659f025ffb [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;
27 struct clk **clks;
28 int num_clocks;
Vivek Gautam06c47e62017-10-19 13:47:43 +020029 struct reset_control *resets;
Felipe Balbi16adc672015-11-18 13:15:20 -060030};
31
Felipe Balbi26c9cac2016-09-12 21:20:22 +030032static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
Felipe Balbi16adc672015-11-18 13:15:20 -060033{
Felipe Balbi26c9cac2016-09-12 21:20:22 +030034 struct device *dev = simple->dev;
Felipe Balbi16adc672015-11-18 13:15:20 -060035 struct device_node *np = dev->of_node;
Felipe Balbi16adc672015-11-18 13:15:20 -060036 int i;
37
Stephen Boyd3d755dc2016-02-22 11:12:47 -080038 simple->num_clocks = count;
Felipe Balbi16adc672015-11-18 13:15:20 -060039
Felipe Balbi26c9cac2016-09-12 21:20:22 +030040 if (!count)
41 return 0;
42
Felipe Balbi16adc672015-11-18 13:15:20 -060043 simple->clks = devm_kcalloc(dev, simple->num_clocks,
44 sizeof(struct clk *), GFP_KERNEL);
45 if (!simple->clks)
46 return -ENOMEM;
47
Felipe Balbi16adc672015-11-18 13:15:20 -060048 for (i = 0; i < simple->num_clocks; i++) {
49 struct clk *clk;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030050 int ret;
Felipe Balbi16adc672015-11-18 13:15:20 -060051
52 clk = of_clk_get(np, i);
53 if (IS_ERR(clk)) {
Andreas Platschekded600e2017-12-07 11:32:20 +010054 while (--i >= 0) {
55 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -060056 clk_put(simple->clks[i]);
Andreas Platschekded600e2017-12-07 11:32:20 +010057 }
Felipe Balbi16adc672015-11-18 13:15:20 -060058 return PTR_ERR(clk);
59 }
60
61 ret = clk_prepare_enable(clk);
62 if (ret < 0) {
63 while (--i >= 0) {
64 clk_disable_unprepare(simple->clks[i]);
65 clk_put(simple->clks[i]);
66 }
67 clk_put(clk);
68
69 return ret;
70 }
71
72 simple->clks[i] = clk;
73 }
74
Felipe Balbi26c9cac2016-09-12 21:20:22 +030075 return 0;
76}
77
78static int dwc3_of_simple_probe(struct platform_device *pdev)
79{
80 struct dwc3_of_simple *simple;
81 struct device *dev = &pdev->dev;
82 struct device_node *np = dev->of_node;
83
84 int ret;
85 int i;
86
87 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
88 if (!simple)
89 return -ENOMEM;
90
91 platform_set_drvdata(pdev, simple);
92 simple->dev = dev;
93
Vivek Gautam06c47e62017-10-19 13:47:43 +020094 simple->resets = of_reset_control_array_get_optional_exclusive(np);
95 if (IS_ERR(simple->resets)) {
96 ret = PTR_ERR(simple->resets);
97 dev_err(dev, "failed to get device resets, err=%d\n", ret);
98 return ret;
99 }
100
101 ret = reset_control_deassert(simple->resets);
102 if (ret)
103 goto err_resetc_put;
104
Shawn Guoe7059ef2017-08-13 22:21:11 +0800105 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
106 "clocks", "#clock-cells"));
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300107 if (ret)
Vivek Gautam06c47e62017-10-19 13:47:43 +0200108 goto err_resetc_assert;
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300109
Felipe Balbi16adc672015-11-18 13:15:20 -0600110 ret = of_platform_populate(np, NULL, NULL, dev);
111 if (ret) {
112 for (i = 0; i < simple->num_clocks; i++) {
113 clk_disable_unprepare(simple->clks[i]);
114 clk_put(simple->clks[i]);
115 }
116
Vivek Gautam06c47e62017-10-19 13:47:43 +0200117 goto err_resetc_assert;
Felipe Balbi16adc672015-11-18 13:15:20 -0600118 }
119
120 pm_runtime_set_active(dev);
121 pm_runtime_enable(dev);
122 pm_runtime_get_sync(dev);
123
124 return 0;
Vivek Gautam06c47e62017-10-19 13:47:43 +0200125
126err_resetc_assert:
127 reset_control_assert(simple->resets);
128
129err_resetc_put:
130 reset_control_put(simple->resets);
131 return ret;
Felipe Balbi16adc672015-11-18 13:15:20 -0600132}
133
134static int dwc3_of_simple_remove(struct platform_device *pdev)
135{
136 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
137 struct device *dev = &pdev->dev;
138 int i;
139
Vivek Gautamd6d9c2a2017-07-19 17:59:06 +0200140 of_platform_depopulate(dev);
141
Felipe Balbi16adc672015-11-18 13:15:20 -0600142 for (i = 0; i < simple->num_clocks; i++) {
Anurag Kumar Vulisha8d53e622016-09-09 18:58:37 +0530143 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -0600144 clk_put(simple->clks[i]);
145 }
146
Vivek Gautam06c47e62017-10-19 13:47:43 +0200147 reset_control_assert(simple->resets);
148 reset_control_put(simple->resets);
149
Felipe Balbi16adc672015-11-18 13:15:20 -0600150 pm_runtime_put_sync(dev);
151 pm_runtime_disable(dev);
152
153 return 0;
154}
155
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600156#ifdef CONFIG_PM
Felipe Balbi16adc672015-11-18 13:15:20 -0600157static int dwc3_of_simple_runtime_suspend(struct device *dev)
158{
159 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
160 int i;
161
162 for (i = 0; i < simple->num_clocks; i++)
163 clk_disable(simple->clks[i]);
164
165 return 0;
166}
167
168static int dwc3_of_simple_runtime_resume(struct device *dev)
169{
170 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
171 int ret;
172 int i;
173
174 for (i = 0; i < simple->num_clocks; i++) {
175 ret = clk_enable(simple->clks[i]);
176 if (ret < 0) {
177 while (--i >= 0)
178 clk_disable(simple->clks[i]);
179 return ret;
180 }
181 }
182
183 return 0;
184}
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600185#endif
Felipe Balbi16adc672015-11-18 13:15:20 -0600186
187static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
188 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
189 dwc3_of_simple_runtime_resume, NULL)
190};
191
192static const struct of_device_id of_dwc3_simple_match[] = {
193 { .compatible = "qcom,dwc3" },
William Wuf6521912016-08-16 22:44:36 +0800194 { .compatible = "rockchip,rk3399-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600195 { .compatible = "xlnx,zynqmp-dwc3" },
Felipe Balbib281dc62016-09-12 21:24:58 +0300196 { .compatible = "cavium,octeon-7130-usb-uctl" },
Baolin Wangc3cdce42017-08-30 19:03:52 +0800197 { .compatible = "sprd,sc9860-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600198 { /* Sentinel */ }
199};
200MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
201
202static struct platform_driver dwc3_of_simple_driver = {
203 .probe = dwc3_of_simple_probe,
204 .remove = dwc3_of_simple_remove,
205 .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>");