usb: dwc3: add generic OF glue layer
[deliverable/linux.git] / drivers / usb / dwc3 / dwc3-of-simple.c
CommitLineData
16adc674
FB
1/**
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
39static int dwc3_of_simple_probe(struct platform_device *pdev)
40{
41 struct dwc3_of_simple *simple;
42 struct device *dev = &pdev->dev;
43 struct device_node *np = dev->of_node;
44
45 int ret;
46 int i;
47
48 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
49 if (!simple)
50 return -ENOMEM;
51
52 ret = of_clk_get_parent_count(np);
53 if (ret < 0)
54 return ret;
55
56 simple->num_clocks = ret;
57
58 simple->clks = devm_kcalloc(dev, simple->num_clocks,
59 sizeof(struct clk *), GFP_KERNEL);
60 if (!simple->clks)
61 return -ENOMEM;
62
63 simple->dev = dev;
64
65 for (i = 0; i < simple->num_clocks; i++) {
66 struct clk *clk;
67
68 clk = of_clk_get(np, i);
69 if (IS_ERR(clk)) {
70 while (--i >= 0)
71 clk_put(simple->clks[i]);
72 return PTR_ERR(clk);
73 }
74
75 ret = clk_prepare_enable(clk);
76 if (ret < 0) {
77 while (--i >= 0) {
78 clk_disable_unprepare(simple->clks[i]);
79 clk_put(simple->clks[i]);
80 }
81 clk_put(clk);
82
83 return ret;
84 }
85
86 simple->clks[i] = clk;
87 }
88
89 ret = of_platform_populate(np, NULL, NULL, dev);
90 if (ret) {
91 for (i = 0; i < simple->num_clocks; i++) {
92 clk_disable_unprepare(simple->clks[i]);
93 clk_put(simple->clks[i]);
94 }
95
96 return ret;
97 }
98
99 pm_runtime_set_active(dev);
100 pm_runtime_enable(dev);
101 pm_runtime_get_sync(dev);
102
103 return 0;
104}
105
106static int dwc3_of_simple_remove(struct platform_device *pdev)
107{
108 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
109 struct device *dev = &pdev->dev;
110 int i;
111
112 for (i = 0; i < simple->num_clocks; i++) {
113 clk_unprepare(simple->clks[i]);
114 clk_put(simple->clks[i]);
115 }
116
117 of_platform_depopulate(dev);
118
119 pm_runtime_put_sync(dev);
120 pm_runtime_disable(dev);
121
122 return 0;
123}
124
125static int dwc3_of_simple_runtime_suspend(struct device *dev)
126{
127 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
128 int i;
129
130 for (i = 0; i < simple->num_clocks; i++)
131 clk_disable(simple->clks[i]);
132
133 return 0;
134}
135
136static int dwc3_of_simple_runtime_resume(struct device *dev)
137{
138 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
139 int ret;
140 int i;
141
142 for (i = 0; i < simple->num_clocks; i++) {
143 ret = clk_enable(simple->clks[i]);
144 if (ret < 0) {
145 while (--i >= 0)
146 clk_disable(simple->clks[i]);
147 return ret;
148 }
149 }
150
151 return 0;
152}
153
154static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
155 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
156 dwc3_of_simple_runtime_resume, NULL)
157};
158
159static const struct of_device_id of_dwc3_simple_match[] = {
160 { .compatible = "qcom,dwc3" },
161 { .compatible = "xlnx,zynqmp-dwc3" },
162 { /* Sentinel */ }
163};
164MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
165
166static struct platform_driver dwc3_of_simple_driver = {
167 .probe = dwc3_of_simple_probe,
168 .remove = dwc3_of_simple_remove,
169 .driver = {
170 .name = "dwc3-of-simple",
171 .of_match_table = of_dwc3_simple_match,
172 },
173};
174
175module_platform_driver(dwc3_of_simple_driver);
176MODULE_LICENSE("GPL v2");
177MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
178MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
This page took 0.03074 seconds and 5 git commands to generate.