Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 2 | /** |
| 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 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 of |
| 11 | * the License as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov |
| 19 | * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC |
| 20 | * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com> |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/clk.h> |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 29 | #include <linux/of.h> |
| 30 | #include <linux/of_platform.h> |
| 31 | #include <linux/pm_runtime.h> |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 32 | #include <linux/reset.h> |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 33 | |
| 34 | struct dwc3_of_simple { |
| 35 | struct device *dev; |
| 36 | struct clk **clks; |
| 37 | int num_clocks; |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 38 | struct reset_control *resets; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 39 | }; |
| 40 | |
Felipe Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 41 | static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count) |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 42 | { |
Felipe Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 43 | struct device *dev = simple->dev; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 44 | struct device_node *np = dev->of_node; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 45 | int i; |
| 46 | |
Stephen Boyd | 3d755dc | 2016-02-22 11:12:47 -0800 | [diff] [blame] | 47 | simple->num_clocks = count; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 48 | |
Felipe Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 49 | if (!count) |
| 50 | return 0; |
| 51 | |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 52 | simple->clks = devm_kcalloc(dev, simple->num_clocks, |
| 53 | sizeof(struct clk *), GFP_KERNEL); |
| 54 | if (!simple->clks) |
| 55 | return -ENOMEM; |
| 56 | |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 57 | for (i = 0; i < simple->num_clocks; i++) { |
| 58 | struct clk *clk; |
Felipe Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 59 | int ret; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 60 | |
| 61 | clk = of_clk_get(np, i); |
| 62 | if (IS_ERR(clk)) { |
| 63 | while (--i >= 0) |
| 64 | clk_put(simple->clks[i]); |
| 65 | 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 Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static 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 | |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 101 | simple->resets = of_reset_control_array_get_optional_exclusive(np); |
| 102 | if (IS_ERR(simple->resets)) { |
| 103 | ret = PTR_ERR(simple->resets); |
| 104 | dev_err(dev, "failed to get device resets, err=%d\n", ret); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | ret = reset_control_deassert(simple->resets); |
| 109 | if (ret) |
| 110 | goto err_resetc_put; |
| 111 | |
Shawn Guo | e7059ef | 2017-08-13 22:21:11 +0800 | [diff] [blame] | 112 | ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np, |
| 113 | "clocks", "#clock-cells")); |
Felipe Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 114 | if (ret) |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 115 | goto err_resetc_assert; |
Felipe Balbi | 26c9cac | 2016-09-12 21:20:22 +0300 | [diff] [blame] | 116 | |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 117 | ret = of_platform_populate(np, NULL, NULL, dev); |
| 118 | if (ret) { |
| 119 | for (i = 0; i < simple->num_clocks; i++) { |
| 120 | clk_disable_unprepare(simple->clks[i]); |
| 121 | clk_put(simple->clks[i]); |
| 122 | } |
| 123 | |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 124 | goto err_resetc_assert; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | pm_runtime_set_active(dev); |
| 128 | pm_runtime_enable(dev); |
| 129 | pm_runtime_get_sync(dev); |
| 130 | |
| 131 | return 0; |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 132 | |
| 133 | err_resetc_assert: |
| 134 | reset_control_assert(simple->resets); |
| 135 | |
| 136 | err_resetc_put: |
| 137 | reset_control_put(simple->resets); |
| 138 | return ret; |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static int dwc3_of_simple_remove(struct platform_device *pdev) |
| 142 | { |
| 143 | struct dwc3_of_simple *simple = platform_get_drvdata(pdev); |
| 144 | struct device *dev = &pdev->dev; |
| 145 | int i; |
| 146 | |
Vivek Gautam | d6d9c2a | 2017-07-19 17:59:06 +0200 | [diff] [blame] | 147 | of_platform_depopulate(dev); |
| 148 | |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 149 | for (i = 0; i < simple->num_clocks; i++) { |
Anurag Kumar Vulisha | 8d53e62 | 2016-09-09 18:58:37 +0530 | [diff] [blame] | 150 | clk_disable_unprepare(simple->clks[i]); |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 151 | clk_put(simple->clks[i]); |
| 152 | } |
| 153 | |
Vivek Gautam | 06c47e6 | 2017-10-19 13:47:43 +0200 | [diff] [blame] | 154 | reset_control_assert(simple->resets); |
| 155 | reset_control_put(simple->resets); |
| 156 | |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 157 | pm_runtime_put_sync(dev); |
| 158 | pm_runtime_disable(dev); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Felipe Balbi | 5072cfc | 2015-12-22 21:56:10 -0600 | [diff] [blame] | 163 | #ifdef CONFIG_PM |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 164 | static int dwc3_of_simple_runtime_suspend(struct device *dev) |
| 165 | { |
| 166 | struct dwc3_of_simple *simple = dev_get_drvdata(dev); |
| 167 | int i; |
| 168 | |
| 169 | for (i = 0; i < simple->num_clocks; i++) |
| 170 | clk_disable(simple->clks[i]); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int dwc3_of_simple_runtime_resume(struct device *dev) |
| 176 | { |
| 177 | struct dwc3_of_simple *simple = dev_get_drvdata(dev); |
| 178 | int ret; |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < simple->num_clocks; i++) { |
| 182 | ret = clk_enable(simple->clks[i]); |
| 183 | if (ret < 0) { |
| 184 | while (--i >= 0) |
| 185 | clk_disable(simple->clks[i]); |
| 186 | return ret; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
Felipe Balbi | 5072cfc | 2015-12-22 21:56:10 -0600 | [diff] [blame] | 192 | #endif |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 193 | |
| 194 | static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = { |
| 195 | SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend, |
| 196 | dwc3_of_simple_runtime_resume, NULL) |
| 197 | }; |
| 198 | |
| 199 | static const struct of_device_id of_dwc3_simple_match[] = { |
| 200 | { .compatible = "qcom,dwc3" }, |
William Wu | f652191 | 2016-08-16 22:44:36 +0800 | [diff] [blame] | 201 | { .compatible = "rockchip,rk3399-dwc3" }, |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 202 | { .compatible = "xlnx,zynqmp-dwc3" }, |
Felipe Balbi | b281dc6 | 2016-09-12 21:24:58 +0300 | [diff] [blame] | 203 | { .compatible = "cavium,octeon-7130-usb-uctl" }, |
Baolin Wang | c3cdce4 | 2017-08-30 19:03:52 +0800 | [diff] [blame] | 204 | { .compatible = "sprd,sc9860-dwc3" }, |
Felipe Balbi | 16adc67 | 2015-11-18 13:15:20 -0600 | [diff] [blame] | 205 | { /* Sentinel */ } |
| 206 | }; |
| 207 | MODULE_DEVICE_TABLE(of, of_dwc3_simple_match); |
| 208 | |
| 209 | static struct platform_driver dwc3_of_simple_driver = { |
| 210 | .probe = dwc3_of_simple_probe, |
| 211 | .remove = dwc3_of_simple_remove, |
| 212 | .driver = { |
| 213 | .name = "dwc3-of-simple", |
| 214 | .of_match_table = of_dwc3_simple_match, |
| 215 | }, |
| 216 | }; |
| 217 | |
| 218 | module_platform_driver(dwc3_of_simple_driver); |
| 219 | MODULE_LICENSE("GPL v2"); |
| 220 | MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer"); |
| 221 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |