blob: a3e2200f5b5fbd3fe9f50e87a308f5f47f970950 [file] [log] [blame]
Felipe Balbi16adc672015-11-18 13:15:20 -06001/**
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
Felipe Balbi26c9cac2016-09-12 21:20:22 +030039static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
Felipe Balbi16adc672015-11-18 13:15:20 -060040{
Felipe Balbi26c9cac2016-09-12 21:20:22 +030041 struct device *dev = simple->dev;
Felipe Balbi16adc672015-11-18 13:15:20 -060042 struct device_node *np = dev->of_node;
Felipe Balbi16adc672015-11-18 13:15:20 -060043 int i;
44
Stephen Boyd3d755dc2016-02-22 11:12:47 -080045 simple->num_clocks = count;
Felipe Balbi16adc672015-11-18 13:15:20 -060046
Felipe Balbi26c9cac2016-09-12 21:20:22 +030047 if (!count)
48 return 0;
49
Felipe Balbi16adc672015-11-18 13:15:20 -060050 simple->clks = devm_kcalloc(dev, simple->num_clocks,
51 sizeof(struct clk *), GFP_KERNEL);
52 if (!simple->clks)
53 return -ENOMEM;
54
Felipe Balbi16adc672015-11-18 13:15:20 -060055 for (i = 0; i < simple->num_clocks; i++) {
56 struct clk *clk;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030057 int ret;
Felipe Balbi16adc672015-11-18 13:15:20 -060058
59 clk = of_clk_get(np, i);
60 if (IS_ERR(clk)) {
Andreas Platschek93c9e1c2017-12-07 11:32:20 +010061 while (--i >= 0) {
62 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -060063 clk_put(simple->clks[i]);
Andreas Platschek93c9e1c2017-12-07 11:32:20 +010064 }
Felipe Balbi16adc672015-11-18 13:15:20 -060065 return PTR_ERR(clk);
66 }
67
68 ret = clk_prepare_enable(clk);
69 if (ret < 0) {
70 while (--i >= 0) {
71 clk_disable_unprepare(simple->clks[i]);
72 clk_put(simple->clks[i]);
73 }
74 clk_put(clk);
75
76 return ret;
77 }
78
79 simple->clks[i] = clk;
80 }
81
Felipe Balbi26c9cac2016-09-12 21:20:22 +030082 return 0;
83}
84
85static int dwc3_of_simple_probe(struct platform_device *pdev)
86{
87 struct dwc3_of_simple *simple;
88 struct device *dev = &pdev->dev;
89 struct device_node *np = dev->of_node;
90
91 int ret;
92 int i;
93
94 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
95 if (!simple)
96 return -ENOMEM;
97
98 platform_set_drvdata(pdev, simple);
99 simple->dev = dev;
100
101 ret = dwc3_of_simple_clk_init(simple, of_clk_get_parent_count(np));
102 if (ret)
103 return ret;
104
Felipe Balbi16adc672015-11-18 13:15:20 -0600105 ret = of_platform_populate(np, NULL, NULL, dev);
106 if (ret) {
107 for (i = 0; i < simple->num_clocks; i++) {
108 clk_disable_unprepare(simple->clks[i]);
109 clk_put(simple->clks[i]);
110 }
111
112 return ret;
113 }
114
115 pm_runtime_set_active(dev);
116 pm_runtime_enable(dev);
117 pm_runtime_get_sync(dev);
118
119 return 0;
120}
121
122static int dwc3_of_simple_remove(struct platform_device *pdev)
123{
124 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
125 struct device *dev = &pdev->dev;
126 int i;
127
128 for (i = 0; i < simple->num_clocks; i++) {
Anurag Kumar Vulisha8d53e622016-09-09 18:58:37 +0530129 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -0600130 clk_put(simple->clks[i]);
131 }
132
133 of_platform_depopulate(dev);
134
135 pm_runtime_put_sync(dev);
136 pm_runtime_disable(dev);
137
138 return 0;
139}
140
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600141#ifdef CONFIG_PM
Felipe Balbi16adc672015-11-18 13:15:20 -0600142static int dwc3_of_simple_runtime_suspend(struct device *dev)
143{
144 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
145 int i;
146
147 for (i = 0; i < simple->num_clocks; i++)
148 clk_disable(simple->clks[i]);
149
150 return 0;
151}
152
153static int dwc3_of_simple_runtime_resume(struct device *dev)
154{
155 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
156 int ret;
157 int i;
158
159 for (i = 0; i < simple->num_clocks; i++) {
160 ret = clk_enable(simple->clks[i]);
161 if (ret < 0) {
162 while (--i >= 0)
163 clk_disable(simple->clks[i]);
164 return ret;
165 }
166 }
167
168 return 0;
169}
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600170#endif
Felipe Balbi16adc672015-11-18 13:15:20 -0600171
172static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
173 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
174 dwc3_of_simple_runtime_resume, NULL)
175};
176
177static const struct of_device_id of_dwc3_simple_match[] = {
178 { .compatible = "qcom,dwc3" },
William Wuf6521912016-08-16 22:44:36 +0800179 { .compatible = "rockchip,rk3399-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600180 { .compatible = "xlnx,zynqmp-dwc3" },
Felipe Balbib281dc62016-09-12 21:24:58 +0300181 { .compatible = "cavium,octeon-7130-usb-uctl" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600182 { /* Sentinel */ }
183};
184MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
185
186static struct platform_driver dwc3_of_simple_driver = {
187 .probe = dwc3_of_simple_probe,
188 .remove = dwc3_of_simple_remove,
189 .driver = {
190 .name = "dwc3-of-simple",
191 .of_match_table = of_dwc3_simple_match,
192 },
193};
194
195module_platform_driver(dwc3_of_simple_driver);
196MODULE_LICENSE("GPL v2");
197MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
198MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");