blob: c4a4d7bd27660225442e732e6be994c333b9b91b [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)) {
54 while (--i >= 0)
55 clk_put(simple->clks[i]);
56 return PTR_ERR(clk);
57 }
58
59 ret = clk_prepare_enable(clk);
60 if (ret < 0) {
61 while (--i >= 0) {
62 clk_disable_unprepare(simple->clks[i]);
63 clk_put(simple->clks[i]);
64 }
65 clk_put(clk);
66
67 return ret;
68 }
69
70 simple->clks[i] = clk;
71 }
72
Felipe Balbi26c9cac2016-09-12 21:20:22 +030073 return 0;
74}
75
76static int dwc3_of_simple_probe(struct platform_device *pdev)
77{
78 struct dwc3_of_simple *simple;
79 struct device *dev = &pdev->dev;
80 struct device_node *np = dev->of_node;
81
82 int ret;
83 int i;
84
85 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
86 if (!simple)
87 return -ENOMEM;
88
89 platform_set_drvdata(pdev, simple);
90 simple->dev = dev;
91
Vivek Gautam06c47e62017-10-19 13:47:43 +020092 simple->resets = of_reset_control_array_get_optional_exclusive(np);
93 if (IS_ERR(simple->resets)) {
94 ret = PTR_ERR(simple->resets);
95 dev_err(dev, "failed to get device resets, err=%d\n", ret);
96 return ret;
97 }
98
99 ret = reset_control_deassert(simple->resets);
100 if (ret)
101 goto err_resetc_put;
102
Shawn Guoe7059ef2017-08-13 22:21:11 +0800103 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
104 "clocks", "#clock-cells"));
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300105 if (ret)
Vivek Gautam06c47e62017-10-19 13:47:43 +0200106 goto err_resetc_assert;
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300107
Felipe Balbi16adc672015-11-18 13:15:20 -0600108 ret = of_platform_populate(np, NULL, NULL, dev);
109 if (ret) {
110 for (i = 0; i < simple->num_clocks; i++) {
111 clk_disable_unprepare(simple->clks[i]);
112 clk_put(simple->clks[i]);
113 }
114
Vivek Gautam06c47e62017-10-19 13:47:43 +0200115 goto err_resetc_assert;
Felipe Balbi16adc672015-11-18 13:15:20 -0600116 }
117
118 pm_runtime_set_active(dev);
119 pm_runtime_enable(dev);
120 pm_runtime_get_sync(dev);
121
122 return 0;
Vivek Gautam06c47e62017-10-19 13:47:43 +0200123
124err_resetc_assert:
125 reset_control_assert(simple->resets);
126
127err_resetc_put:
128 reset_control_put(simple->resets);
129 return ret;
Felipe Balbi16adc672015-11-18 13:15:20 -0600130}
131
132static int dwc3_of_simple_remove(struct platform_device *pdev)
133{
134 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
135 struct device *dev = &pdev->dev;
136 int i;
137
Vivek Gautamd6d9c2a2017-07-19 17:59:06 +0200138 of_platform_depopulate(dev);
139
Felipe Balbi16adc672015-11-18 13:15:20 -0600140 for (i = 0; i < simple->num_clocks; i++) {
Anurag Kumar Vulisha8d53e622016-09-09 18:58:37 +0530141 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -0600142 clk_put(simple->clks[i]);
143 }
144
Vivek Gautam06c47e62017-10-19 13:47:43 +0200145 reset_control_assert(simple->resets);
146 reset_control_put(simple->resets);
147
Felipe Balbi16adc672015-11-18 13:15:20 -0600148 pm_runtime_put_sync(dev);
149 pm_runtime_disable(dev);
150
151 return 0;
152}
153
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600154#ifdef CONFIG_PM
Felipe Balbi16adc672015-11-18 13:15:20 -0600155static int dwc3_of_simple_runtime_suspend(struct device *dev)
156{
157 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
158 int i;
159
160 for (i = 0; i < simple->num_clocks; i++)
161 clk_disable(simple->clks[i]);
162
163 return 0;
164}
165
166static int dwc3_of_simple_runtime_resume(struct device *dev)
167{
168 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
169 int ret;
170 int i;
171
172 for (i = 0; i < simple->num_clocks; i++) {
173 ret = clk_enable(simple->clks[i]);
174 if (ret < 0) {
175 while (--i >= 0)
176 clk_disable(simple->clks[i]);
177 return ret;
178 }
179 }
180
181 return 0;
182}
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600183#endif
Felipe Balbi16adc672015-11-18 13:15:20 -0600184
185static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
186 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
187 dwc3_of_simple_runtime_resume, NULL)
188};
189
190static const struct of_device_id of_dwc3_simple_match[] = {
191 { .compatible = "qcom,dwc3" },
William Wuf6521912016-08-16 22:44:36 +0800192 { .compatible = "rockchip,rk3399-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600193 { .compatible = "xlnx,zynqmp-dwc3" },
Felipe Balbib281dc62016-09-12 21:24:58 +0300194 { .compatible = "cavium,octeon-7130-usb-uctl" },
Baolin Wangc3cdce42017-08-30 19:03:52 +0800195 { .compatible = "sprd,sc9860-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600196 { /* Sentinel */ }
197};
198MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
199
200static struct platform_driver dwc3_of_simple_driver = {
201 .probe = dwc3_of_simple_probe,
202 .remove = dwc3_of_simple_remove,
203 .driver = {
204 .name = "dwc3-of-simple",
205 .of_match_table = of_dwc3_simple_match,
206 },
207};
208
209module_platform_driver(dwc3_of_simple_driver);
210MODULE_LICENSE("GPL v2");
211MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
212MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");