blob: b6d35413c00d988cbd3acc19b3c2d9076f0a1cd5 [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;
Martin Blumenstinglff0a6322018-02-11 22:15:16 +010030 bool pulse_resets;
Felipe Balbi16adc672015-11-18 13:15:20 -060031};
32
Felipe Balbi26c9cac2016-09-12 21:20:22 +030033static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
Felipe Balbi16adc672015-11-18 13:15:20 -060034{
Felipe Balbi26c9cac2016-09-12 21:20:22 +030035 struct device *dev = simple->dev;
Felipe Balbi16adc672015-11-18 13:15:20 -060036 struct device_node *np = dev->of_node;
Felipe Balbi16adc672015-11-18 13:15:20 -060037 int i;
38
Stephen Boyd3d755dc2016-02-22 11:12:47 -080039 simple->num_clocks = count;
Felipe Balbi16adc672015-11-18 13:15:20 -060040
Felipe Balbi26c9cac2016-09-12 21:20:22 +030041 if (!count)
42 return 0;
43
Felipe Balbi16adc672015-11-18 13:15:20 -060044 simple->clks = devm_kcalloc(dev, simple->num_clocks,
45 sizeof(struct clk *), GFP_KERNEL);
46 if (!simple->clks)
47 return -ENOMEM;
48
Felipe Balbi16adc672015-11-18 13:15:20 -060049 for (i = 0; i < simple->num_clocks; i++) {
50 struct clk *clk;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030051 int ret;
Felipe Balbi16adc672015-11-18 13:15:20 -060052
53 clk = of_clk_get(np, i);
54 if (IS_ERR(clk)) {
Andreas Platschekded600e2017-12-07 11:32:20 +010055 while (--i >= 0) {
56 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -060057 clk_put(simple->clks[i]);
Andreas Platschekded600e2017-12-07 11:32:20 +010058 }
Felipe Balbi16adc672015-11-18 13:15:20 -060059 return PTR_ERR(clk);
60 }
61
62 ret = clk_prepare_enable(clk);
63 if (ret < 0) {
64 while (--i >= 0) {
65 clk_disable_unprepare(simple->clks[i]);
66 clk_put(simple->clks[i]);
67 }
68 clk_put(clk);
69
70 return ret;
71 }
72
73 simple->clks[i] = clk;
74 }
75
Felipe Balbi26c9cac2016-09-12 21:20:22 +030076 return 0;
77}
78
79static int dwc3_of_simple_probe(struct platform_device *pdev)
80{
81 struct dwc3_of_simple *simple;
82 struct device *dev = &pdev->dev;
83 struct device_node *np = dev->of_node;
84
85 int ret;
86 int i;
Martin Blumenstinglff0a6322018-02-11 22:15:16 +010087 bool shared_resets = false;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030088
89 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
90 if (!simple)
91 return -ENOMEM;
92
93 platform_set_drvdata(pdev, simple);
94 simple->dev = dev;
95
Martin Blumenstinglff0a6322018-02-11 22:15:16 +010096 simple->resets = of_reset_control_array_get(np, shared_resets, true);
Vivek Gautam06c47e62017-10-19 13:47:43 +020097 if (IS_ERR(simple->resets)) {
98 ret = PTR_ERR(simple->resets);
99 dev_err(dev, "failed to get device resets, err=%d\n", ret);
100 return ret;
101 }
102
Martin Blumenstinglff0a6322018-02-11 22:15:16 +0100103 if (simple->pulse_resets) {
104 ret = reset_control_reset(simple->resets);
105 if (ret)
106 goto err_resetc_put;
107 } else {
108 ret = reset_control_deassert(simple->resets);
109 if (ret)
110 goto err_resetc_put;
111 }
Vivek Gautam06c47e62017-10-19 13:47:43 +0200112
Shawn Guoe7059ef2017-08-13 22:21:11 +0800113 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
114 "clocks", "#clock-cells"));
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300115 if (ret)
Vivek Gautam06c47e62017-10-19 13:47:43 +0200116 goto err_resetc_assert;
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300117
Felipe Balbi16adc672015-11-18 13:15:20 -0600118 ret = of_platform_populate(np, NULL, NULL, dev);
119 if (ret) {
120 for (i = 0; i < simple->num_clocks; i++) {
121 clk_disable_unprepare(simple->clks[i]);
122 clk_put(simple->clks[i]);
123 }
124
Vivek Gautam06c47e62017-10-19 13:47:43 +0200125 goto err_resetc_assert;
Felipe Balbi16adc672015-11-18 13:15:20 -0600126 }
127
128 pm_runtime_set_active(dev);
129 pm_runtime_enable(dev);
130 pm_runtime_get_sync(dev);
131
132 return 0;
Vivek Gautam06c47e62017-10-19 13:47:43 +0200133
134err_resetc_assert:
Martin Blumenstinglff0a6322018-02-11 22:15:16 +0100135 if (!simple->pulse_resets)
136 reset_control_assert(simple->resets);
Vivek Gautam06c47e62017-10-19 13:47:43 +0200137
138err_resetc_put:
139 reset_control_put(simple->resets);
140 return ret;
Felipe Balbi16adc672015-11-18 13:15:20 -0600141}
142
143static int dwc3_of_simple_remove(struct platform_device *pdev)
144{
145 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
146 struct device *dev = &pdev->dev;
147 int i;
148
Vivek Gautamd6d9c2a2017-07-19 17:59:06 +0200149 of_platform_depopulate(dev);
150
Felipe Balbi16adc672015-11-18 13:15:20 -0600151 for (i = 0; i < simple->num_clocks; i++) {
Anurag Kumar Vulisha8d53e622016-09-09 18:58:37 +0530152 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -0600153 clk_put(simple->clks[i]);
154 }
Enric Balletbo i Serrabff52352017-12-18 16:14:36 +0100155 simple->num_clocks = 0;
Felipe Balbi16adc672015-11-18 13:15:20 -0600156
Martin Blumenstinglff0a6322018-02-11 22:15:16 +0100157 if (!simple->pulse_resets)
158 reset_control_assert(simple->resets);
159
Vivek Gautam06c47e62017-10-19 13:47:43 +0200160 reset_control_put(simple->resets);
161
Felipe Balbi16adc672015-11-18 13:15:20 -0600162 pm_runtime_put_sync(dev);
163 pm_runtime_disable(dev);
164
165 return 0;
166}
167
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600168#ifdef CONFIG_PM
Felipe Balbi16adc672015-11-18 13:15:20 -0600169static int dwc3_of_simple_runtime_suspend(struct device *dev)
170{
171 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
172 int i;
173
174 for (i = 0; i < simple->num_clocks; i++)
175 clk_disable(simple->clks[i]);
176
177 return 0;
178}
179
180static int dwc3_of_simple_runtime_resume(struct device *dev)
181{
182 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
183 int ret;
184 int i;
185
186 for (i = 0; i < simple->num_clocks; i++) {
187 ret = clk_enable(simple->clks[i]);
188 if (ret < 0) {
189 while (--i >= 0)
190 clk_disable(simple->clks[i]);
191 return ret;
192 }
193 }
194
195 return 0;
196}
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600197#endif
Felipe Balbi16adc672015-11-18 13:15:20 -0600198
199static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
200 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
201 dwc3_of_simple_runtime_resume, NULL)
202};
203
204static const struct of_device_id of_dwc3_simple_match[] = {
205 { .compatible = "qcom,dwc3" },
William Wuf6521912016-08-16 22:44:36 +0800206 { .compatible = "rockchip,rk3399-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600207 { .compatible = "xlnx,zynqmp-dwc3" },
Felipe Balbib281dc62016-09-12 21:24:58 +0300208 { .compatible = "cavium,octeon-7130-usb-uctl" },
Baolin Wangc3cdce42017-08-30 19:03:52 +0800209 { .compatible = "sprd,sc9860-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600210 { /* Sentinel */ }
211};
212MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
213
214static struct platform_driver dwc3_of_simple_driver = {
215 .probe = dwc3_of_simple_probe,
216 .remove = dwc3_of_simple_remove,
217 .driver = {
218 .name = "dwc3-of-simple",
219 .of_match_table = of_dwc3_simple_match,
Masahiro Yamadaa0d8c4c2017-12-07 13:40:24 +0900220 .pm = &dwc3_of_simple_dev_pm_ops,
Felipe Balbi16adc672015-11-18 13:15:20 -0600221 },
222};
223
224module_platform_driver(dwc3_of_simple_driver);
225MODULE_LICENSE("GPL v2");
226MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
227MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");