blob: a26d1fde0f5e4b6f3613387da40aaca00f937477 [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>
Felipe Balbi16adc672015-11-18 13:15:20 -060028#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/pm_runtime.h>
31
32struct dwc3_of_simple {
33 struct device *dev;
34 struct clk **clks;
35 int num_clocks;
36};
37
Felipe Balbi26c9cac2016-09-12 21:20:22 +030038static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
Felipe Balbi16adc672015-11-18 13:15:20 -060039{
Felipe Balbi26c9cac2016-09-12 21:20:22 +030040 struct device *dev = simple->dev;
Felipe Balbi16adc672015-11-18 13:15:20 -060041 struct device_node *np = dev->of_node;
Felipe Balbi16adc672015-11-18 13:15:20 -060042 int i;
43
Stephen Boyd3d755dc2016-02-22 11:12:47 -080044 simple->num_clocks = count;
Felipe Balbi16adc672015-11-18 13:15:20 -060045
Felipe Balbi26c9cac2016-09-12 21:20:22 +030046 if (!count)
47 return 0;
48
Felipe Balbi16adc672015-11-18 13:15:20 -060049 simple->clks = devm_kcalloc(dev, simple->num_clocks,
50 sizeof(struct clk *), GFP_KERNEL);
51 if (!simple->clks)
52 return -ENOMEM;
53
Felipe Balbi16adc672015-11-18 13:15:20 -060054 for (i = 0; i < simple->num_clocks; i++) {
55 struct clk *clk;
Felipe Balbi26c9cac2016-09-12 21:20:22 +030056 int ret;
Felipe Balbi16adc672015-11-18 13:15:20 -060057
58 clk = of_clk_get(np, i);
59 if (IS_ERR(clk)) {
60 while (--i >= 0)
61 clk_put(simple->clks[i]);
62 return PTR_ERR(clk);
63 }
64
65 ret = clk_prepare_enable(clk);
66 if (ret < 0) {
67 while (--i >= 0) {
68 clk_disable_unprepare(simple->clks[i]);
69 clk_put(simple->clks[i]);
70 }
71 clk_put(clk);
72
73 return ret;
74 }
75
76 simple->clks[i] = clk;
77 }
78
Felipe Balbi26c9cac2016-09-12 21:20:22 +030079 return 0;
80}
81
82static int dwc3_of_simple_probe(struct platform_device *pdev)
83{
84 struct dwc3_of_simple *simple;
85 struct device *dev = &pdev->dev;
86 struct device_node *np = dev->of_node;
87
88 int ret;
89 int i;
90
91 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
92 if (!simple)
93 return -ENOMEM;
94
95 platform_set_drvdata(pdev, simple);
96 simple->dev = dev;
97
Shawn Guoe7059ef2017-08-13 22:21:11 +080098 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
99 "clocks", "#clock-cells"));
Felipe Balbi26c9cac2016-09-12 21:20:22 +0300100 if (ret)
101 return ret;
102
Felipe Balbi16adc672015-11-18 13:15:20 -0600103 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++) {
Anurag Kumar Vulisha8d53e622016-09-09 18:58:37 +0530127 clk_disable_unprepare(simple->clks[i]);
Felipe Balbi16adc672015-11-18 13:15:20 -0600128 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
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600139#ifdef CONFIG_PM
Felipe Balbi16adc672015-11-18 13:15:20 -0600140static 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}
Felipe Balbi5072cfc2015-12-22 21:56:10 -0600168#endif
Felipe Balbi16adc672015-11-18 13:15:20 -0600169
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" },
William Wuf6521912016-08-16 22:44:36 +0800177 { .compatible = "rockchip,rk3399-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600178 { .compatible = "xlnx,zynqmp-dwc3" },
Felipe Balbib281dc62016-09-12 21:24:58 +0300179 { .compatible = "cavium,octeon-7130-usb-uctl" },
Baolin Wangc3cdce42017-08-30 19:03:52 +0800180 { .compatible = "sprd,sc9860-dwc3" },
Felipe Balbi16adc672015-11-18 13:15:20 -0600181 { /* Sentinel */ }
182};
183MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
184
185static struct platform_driver dwc3_of_simple_driver = {
186 .probe = dwc3_of_simple_probe,
187 .remove = dwc3_of_simple_remove,
188 .driver = {
189 .name = "dwc3-of-simple",
190 .of_match_table = of_dwc3_simple_match,
191 },
192};
193
194module_platform_driver(dwc3_of_simple_driver);
195MODULE_LICENSE("GPL v2");
196MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
197MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");