Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / usb / dwc3 / dwc3-of-simple.c
blobfe414e7a9c78cda7405db573f321da91d0d4eb5b
1 /**
2 * dwc3-of-simple.c - OF glue layer for simple integrations
4 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Felipe Balbi <balbi@ti.com>
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.
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.
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>
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>
33 struct dwc3_of_simple {
34 struct device *dev;
35 struct clk **clks;
36 int num_clocks;
39 static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
41 struct device *dev = simple->dev;
42 struct device_node *np = dev->of_node;
43 int i;
45 simple->num_clocks = count;
47 if (!count)
48 return 0;
50 simple->clks = devm_kcalloc(dev, simple->num_clocks,
51 sizeof(struct clk *), GFP_KERNEL);
52 if (!simple->clks)
53 return -ENOMEM;
55 for (i = 0; i < simple->num_clocks; i++) {
56 struct clk *clk;
57 int ret;
59 clk = of_clk_get(np, i);
60 if (IS_ERR(clk)) {
61 while (--i >= 0)
62 clk_put(simple->clks[i]);
63 return PTR_ERR(clk);
66 ret = clk_prepare_enable(clk);
67 if (ret < 0) {
68 while (--i >= 0) {
69 clk_disable_unprepare(simple->clks[i]);
70 clk_put(simple->clks[i]);
72 clk_put(clk);
74 return ret;
77 simple->clks[i] = clk;
80 return 0;
83 static int dwc3_of_simple_probe(struct platform_device *pdev)
85 struct dwc3_of_simple *simple;
86 struct device *dev = &pdev->dev;
87 struct device_node *np = dev->of_node;
89 int ret;
90 int i;
92 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
93 if (!simple)
94 return -ENOMEM;
96 platform_set_drvdata(pdev, simple);
97 simple->dev = dev;
99 ret = dwc3_of_simple_clk_init(simple, of_clk_get_parent_count(np));
100 if (ret)
101 return ret;
103 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]);
110 return ret;
113 pm_runtime_set_active(dev);
114 pm_runtime_enable(dev);
115 pm_runtime_get_sync(dev);
117 return 0;
120 static int dwc3_of_simple_remove(struct platform_device *pdev)
122 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
123 struct device *dev = &pdev->dev;
124 int i;
126 for (i = 0; i < simple->num_clocks; i++) {
127 clk_disable_unprepare(simple->clks[i]);
128 clk_put(simple->clks[i]);
131 of_platform_depopulate(dev);
133 pm_runtime_put_sync(dev);
134 pm_runtime_disable(dev);
136 return 0;
139 #ifdef CONFIG_PM
140 static int dwc3_of_simple_runtime_suspend(struct device *dev)
142 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
143 int i;
145 for (i = 0; i < simple->num_clocks; i++)
146 clk_disable(simple->clks[i]);
148 return 0;
151 static int dwc3_of_simple_runtime_resume(struct device *dev)
153 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
154 int ret;
155 int i;
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;
166 return 0;
168 #endif
170 static 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)
175 static const struct of_device_id of_dwc3_simple_match[] = {
176 { .compatible = "qcom,dwc3" },
177 { .compatible = "rockchip,rk3399-dwc3" },
178 { .compatible = "xlnx,zynqmp-dwc3" },
179 { .compatible = "cavium,octeon-7130-usb-uctl" },
180 { /* Sentinel */ }
182 MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
184 static struct platform_driver dwc3_of_simple_driver = {
185 .probe = dwc3_of_simple_probe,
186 .remove = dwc3_of_simple_remove,
187 .driver = {
188 .name = "dwc3-of-simple",
189 .of_match_table = of_dwc3_simple_match,
193 module_platform_driver(dwc3_of_simple_driver);
194 MODULE_LICENSE("GPL v2");
195 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
196 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");