Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 3 | * Copyright (C) 2012 Marek Vasut <marex@denx.de> |
| 4 | * on behalf of DENX Software Engineering GmbH |
| 5 | * |
| 6 | * The code contained herein is licensed under the GNU General Public |
| 7 | * License. You may obtain a copy of the GNU General Public License |
| 8 | * Version 2 or later at the following locations: |
| 9 | * |
| 10 | * http://www.opensource.org/licenses/gpl-license.html |
| 11 | * http://www.gnu.org/copyleft/gpl.html |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <linux/of_gpio.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/pm_runtime.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/usb/chipidea.h> |
| 21 | #include <linux/clk.h> |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 22 | |
| 23 | #include "ci.h" |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 24 | #include "ci_hdrc_imx.h" |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 25 | |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 26 | struct ci_hdrc_imx_platform_flag { |
| 27 | unsigned int flags; |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 28 | bool runtime_pm; |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static const struct ci_hdrc_imx_platform_flag imx27_usb_data = { |
Peter Chen | 0ef877a | 2014-10-30 11:10:04 +0800 | [diff] [blame] | 32 | CI_HDRC_DISABLE_STREAMING, |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static const struct ci_hdrc_imx_platform_flag imx28_usb_data = { |
Li Jun | 6adb9b7b | 2015-02-11 12:45:01 +0800 | [diff] [blame] | 36 | .flags = CI_HDRC_IMX28_WRITE_FIX | |
Peter Chen | 0ef877a | 2014-10-30 11:10:04 +0800 | [diff] [blame] | 37 | CI_HDRC_TURN_VBUS_EARLY_ON | |
| 38 | CI_HDRC_DISABLE_STREAMING, |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 39 | }; |
| 40 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 41 | static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = { |
Li Jun | 6adb9b7b | 2015-02-11 12:45:01 +0800 | [diff] [blame] | 42 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM | |
Peter Chen | 0ef877a | 2014-10-30 11:10:04 +0800 | [diff] [blame] | 43 | CI_HDRC_TURN_VBUS_EARLY_ON | |
| 44 | CI_HDRC_DISABLE_STREAMING, |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = { |
Li Jun | 6adb9b7b | 2015-02-11 12:45:01 +0800 | [diff] [blame] | 48 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM | |
Peter Chen | 0ef877a | 2014-10-30 11:10:04 +0800 | [diff] [blame] | 49 | CI_HDRC_TURN_VBUS_EARLY_ON | |
| 50 | CI_HDRC_DISABLE_HOST_STREAMING, |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = { |
Li Jun | 6adb9b7b | 2015-02-11 12:45:01 +0800 | [diff] [blame] | 54 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM | |
Peter Chen | 0ef877a | 2014-10-30 11:10:04 +0800 | [diff] [blame] | 55 | CI_HDRC_TURN_VBUS_EARLY_ON | |
| 56 | CI_HDRC_DISABLE_HOST_STREAMING, |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 57 | }; |
| 58 | |
Peter Chen | 52fe568 | 2015-09-16 15:52:32 +0800 | [diff] [blame] | 59 | static const struct ci_hdrc_imx_platform_flag imx6ul_usb_data = { |
| 60 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM | |
| 61 | CI_HDRC_TURN_VBUS_EARLY_ON, |
| 62 | }; |
| 63 | |
Peter Chen | 5cb377c | 2015-09-09 16:33:02 +0800 | [diff] [blame] | 64 | static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = { |
| 65 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM, |
| 66 | }; |
| 67 | |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 68 | static const struct of_device_id ci_hdrc_imx_dt_ids[] = { |
| 69 | { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data}, |
| 70 | { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data}, |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 71 | { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data}, |
| 72 | { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data}, |
Li Jun | 8315b77 | 2015-09-16 14:46:32 +0800 | [diff] [blame] | 73 | { .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data}, |
Peter Chen | 52fe568 | 2015-09-16 15:52:32 +0800 | [diff] [blame] | 74 | { .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data}, |
Peter Chen | 5cb377c | 2015-09-09 16:33:02 +0800 | [diff] [blame] | 75 | { .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data}, |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 76 | { /* sentinel */ } |
| 77 | }; |
| 78 | MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids); |
| 79 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 80 | struct ci_hdrc_imx_data { |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 81 | struct usb_phy *phy; |
| 82 | struct platform_device *ci_pdev; |
| 83 | struct clk *clk; |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 84 | struct imx_usbmisc_data *usbmisc_data; |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 85 | bool supports_runtime_pm; |
| 86 | bool in_lpm; |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 87 | /* SoC before i.mx6 (except imx23/imx28) needs three clks */ |
| 88 | bool need_three_clks; |
| 89 | struct clk *clk_ipg; |
| 90 | struct clk *clk_ahb; |
| 91 | struct clk *clk_per; |
| 92 | /* --------------------------------- */ |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 93 | }; |
| 94 | |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 95 | /* Common functions shared by usbmisc drivers */ |
| 96 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 97 | static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev) |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 98 | { |
Stefan Agner | f40017e | 2014-09-22 08:14:15 +0800 | [diff] [blame] | 99 | struct platform_device *misc_pdev; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 100 | struct device_node *np = dev->of_node; |
| 101 | struct of_phandle_args args; |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 102 | struct imx_usbmisc_data *data; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 103 | int ret; |
| 104 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 105 | /* |
| 106 | * In case the fsl,usbmisc property is not present this device doesn't |
| 107 | * need usbmisc. Return NULL (which is no error here) |
| 108 | */ |
| 109 | if (!of_get_property(np, "fsl,usbmisc", NULL)) |
| 110 | return NULL; |
| 111 | |
| 112 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 113 | if (!data) |
| 114 | return ERR_PTR(-ENOMEM); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 115 | |
| 116 | ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells", |
| 117 | 0, &args); |
| 118 | if (ret) { |
| 119 | dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n", |
| 120 | ret); |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 121 | return ERR_PTR(ret); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 122 | } |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 123 | |
| 124 | data->index = args.args[0]; |
Stefan Agner | f40017e | 2014-09-22 08:14:15 +0800 | [diff] [blame] | 125 | |
| 126 | misc_pdev = of_find_device_by_node(args.np); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 127 | of_node_put(args.np); |
| 128 | |
Tomeu Vizoso | ef12da9 | 2015-08-06 15:09:54 +0200 | [diff] [blame] | 129 | if (!misc_pdev || !platform_get_drvdata(misc_pdev)) |
Stefan Agner | f40017e | 2014-09-22 08:14:15 +0800 | [diff] [blame] | 130 | return ERR_PTR(-EPROBE_DEFER); |
| 131 | |
| 132 | data->dev = &misc_pdev->dev; |
| 133 | |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 134 | if (of_find_property(np, "disable-over-current", NULL)) |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 135 | data->disable_oc = 1; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 136 | |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 137 | if (of_find_property(np, "external-vbus-divider", NULL)) |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 138 | data->evdo = 1; |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 139 | |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 140 | return data; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 141 | } |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 142 | |
| 143 | /* End of common functions shared by usbmisc drivers*/ |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 144 | static int imx_get_clks(struct device *dev) |
| 145 | { |
| 146 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 147 | int ret = 0; |
| 148 | |
| 149 | data->clk_ipg = devm_clk_get(dev, "ipg"); |
| 150 | if (IS_ERR(data->clk_ipg)) { |
| 151 | /* If the platform only needs one clocks */ |
| 152 | data->clk = devm_clk_get(dev, NULL); |
| 153 | if (IS_ERR(data->clk)) { |
| 154 | ret = PTR_ERR(data->clk); |
| 155 | dev_err(dev, |
| 156 | "Failed to get clks, err=%ld,%ld\n", |
| 157 | PTR_ERR(data->clk), PTR_ERR(data->clk_ipg)); |
| 158 | return ret; |
| 159 | } |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | data->clk_ahb = devm_clk_get(dev, "ahb"); |
| 164 | if (IS_ERR(data->clk_ahb)) { |
| 165 | ret = PTR_ERR(data->clk_ahb); |
| 166 | dev_err(dev, |
| 167 | "Failed to get ahb clock, err=%d\n", ret); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | data->clk_per = devm_clk_get(dev, "per"); |
| 172 | if (IS_ERR(data->clk_per)) { |
| 173 | ret = PTR_ERR(data->clk_per); |
| 174 | dev_err(dev, |
| 175 | "Failed to get per clock, err=%d\n", ret); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | data->need_three_clks = true; |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int imx_prepare_enable_clks(struct device *dev) |
| 184 | { |
| 185 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 186 | int ret = 0; |
| 187 | |
| 188 | if (data->need_three_clks) { |
| 189 | ret = clk_prepare_enable(data->clk_ipg); |
| 190 | if (ret) { |
| 191 | dev_err(dev, |
| 192 | "Failed to prepare/enable ipg clk, err=%d\n", |
| 193 | ret); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | ret = clk_prepare_enable(data->clk_ahb); |
| 198 | if (ret) { |
| 199 | dev_err(dev, |
| 200 | "Failed to prepare/enable ahb clk, err=%d\n", |
| 201 | ret); |
| 202 | clk_disable_unprepare(data->clk_ipg); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | ret = clk_prepare_enable(data->clk_per); |
| 207 | if (ret) { |
| 208 | dev_err(dev, |
| 209 | "Failed to prepare/enable per clk, err=%d\n", |
| 210 | ret); |
| 211 | clk_disable_unprepare(data->clk_ahb); |
| 212 | clk_disable_unprepare(data->clk_ipg); |
| 213 | return ret; |
| 214 | } |
| 215 | } else { |
| 216 | ret = clk_prepare_enable(data->clk); |
| 217 | if (ret) { |
| 218 | dev_err(dev, |
| 219 | "Failed to prepare/enable clk, err=%d\n", |
| 220 | ret); |
| 221 | return ret; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static void imx_disable_unprepare_clks(struct device *dev) |
| 229 | { |
| 230 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 231 | |
| 232 | if (data->need_three_clks) { |
| 233 | clk_disable_unprepare(data->clk_per); |
| 234 | clk_disable_unprepare(data->clk_ahb); |
| 235 | clk_disable_unprepare(data->clk_ipg); |
| 236 | } else { |
| 237 | clk_disable_unprepare(data->clk); |
| 238 | } |
| 239 | } |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 240 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 241 | static int ci_hdrc_imx_probe(struct platform_device *pdev) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 242 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 243 | struct ci_hdrc_imx_data *data; |
| 244 | struct ci_hdrc_platform_data pdata = { |
Alexander Shiyan | c844d6c | 2014-03-11 13:47:38 +0800 | [diff] [blame] | 245 | .name = dev_name(&pdev->dev), |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 246 | .capoffset = DEF_CAPOFFSET, |
Peter Chen | 0ef877a | 2014-10-30 11:10:04 +0800 | [diff] [blame] | 247 | .flags = CI_HDRC_SET_NON_ZERO_TTHA, |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 248 | }; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 249 | int ret; |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 250 | const struct of_device_id *of_id = |
| 251 | of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev); |
| 252 | const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 253 | |
| 254 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
Fabio Estevam | 7352982 | 2014-11-26 13:44:24 +0800 | [diff] [blame] | 255 | if (!data) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 256 | return -ENOMEM; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 257 | |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 258 | platform_set_drvdata(pdev, data); |
Sascha Hauer | 05986ba | 2013-08-14 12:44:16 +0300 | [diff] [blame] | 259 | data->usbmisc_data = usbmisc_get_init_data(&pdev->dev); |
| 260 | if (IS_ERR(data->usbmisc_data)) |
| 261 | return PTR_ERR(data->usbmisc_data); |
| 262 | |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 263 | ret = imx_get_clks(&pdev->dev); |
| 264 | if (ret) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 265 | return ret; |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 266 | |
| 267 | ret = imx_prepare_enable_clks(&pdev->dev); |
| 268 | if (ret) |
| 269 | return ret; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 270 | |
Fabio Estevam | 046916d | 2013-06-25 12:58:05 +0300 | [diff] [blame] | 271 | data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0); |
Peter Chen | af59a8b | 2013-09-24 12:47:54 +0800 | [diff] [blame] | 272 | if (IS_ERR(data->phy)) { |
| 273 | ret = PTR_ERR(data->phy); |
Markus Pargmann | 16853d7 | 2014-07-22 10:09:43 +0800 | [diff] [blame] | 274 | /* Return -EINVAL if no usbphy is available */ |
| 275 | if (ret == -ENODEV) |
| 276 | ret = -EINVAL; |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 277 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 278 | } |
| 279 | |
Antoine Tenart | ef44cb4 | 2014-10-30 18:41:16 +0100 | [diff] [blame] | 280 | pdata.usb_phy = data->phy; |
Peter Chen | 5604008 | 2015-02-11 12:44:42 +0800 | [diff] [blame] | 281 | pdata.flags |= imx_platform_flag->flags; |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 282 | if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM) |
| 283 | data->supports_runtime_pm = true; |
Peter Chen | 1071055 | 2014-01-10 13:51:28 +0800 | [diff] [blame] | 284 | |
Russell King | e1fd734 | 2013-06-27 12:36:37 +0100 | [diff] [blame] | 285 | ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
Russell King | 22d9d8e | 2013-06-10 16:28:49 +0100 | [diff] [blame] | 286 | if (ret) |
| 287 | goto err_clk; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 288 | |
Peter Chen | a4cf1b1 | 2015-02-11 12:44:44 +0800 | [diff] [blame] | 289 | ret = imx_usbmisc_init(data->usbmisc_data); |
| 290 | if (ret) { |
| 291 | dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", ret); |
| 292 | goto err_clk; |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 293 | } |
| 294 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 295 | data->ci_pdev = ci_hdrc_add_device(&pdev->dev, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 296 | pdev->resource, pdev->num_resources, |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 297 | &pdata); |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 298 | if (IS_ERR(data->ci_pdev)) { |
| 299 | ret = PTR_ERR(data->ci_pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 300 | dev_err(&pdev->dev, |
| 301 | "Can't register ci_hdrc platform device, err=%d\n", |
| 302 | ret); |
Peter Chen | af59a8b | 2013-09-24 12:47:54 +0800 | [diff] [blame] | 303 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 304 | } |
| 305 | |
Peter Chen | a4cf1b1 | 2015-02-11 12:44:44 +0800 | [diff] [blame] | 306 | ret = imx_usbmisc_init_post(data->usbmisc_data); |
| 307 | if (ret) { |
| 308 | dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", ret); |
| 309 | goto disable_device; |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 312 | if (data->supports_runtime_pm) { |
| 313 | pm_runtime_set_active(&pdev->dev); |
| 314 | pm_runtime_enable(&pdev->dev); |
| 315 | } |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 316 | |
Peter Chen | 6d65311 | 2015-02-11 12:44:49 +0800 | [diff] [blame] | 317 | device_set_wakeup_capable(&pdev->dev, true); |
| 318 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 319 | return 0; |
| 320 | |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 321 | disable_device: |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 322 | ci_hdrc_remove_device(data->ci_pdev); |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 323 | err_clk: |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 324 | imx_disable_unprepare_clks(&pdev->dev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 328 | static int ci_hdrc_imx_remove(struct platform_device *pdev) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 329 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 330 | struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 331 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 332 | if (data->supports_runtime_pm) { |
| 333 | pm_runtime_get_sync(&pdev->dev); |
| 334 | pm_runtime_disable(&pdev->dev); |
| 335 | pm_runtime_put_noidle(&pdev->dev); |
| 336 | } |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 337 | ci_hdrc_remove_device(data->ci_pdev); |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 338 | imx_disable_unprepare_clks(&pdev->dev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 339 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 343 | #ifdef CONFIG_PM |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 344 | static int imx_controller_suspend(struct device *dev) |
| 345 | { |
| 346 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 347 | |
| 348 | dev_dbg(dev, "at %s\n", __func__); |
| 349 | |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 350 | imx_disable_unprepare_clks(dev); |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 351 | data->in_lpm = true; |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int imx_controller_resume(struct device *dev) |
| 357 | { |
| 358 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 359 | int ret = 0; |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 360 | |
| 361 | dev_dbg(dev, "at %s\n", __func__); |
| 362 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 363 | if (!data->in_lpm) { |
| 364 | WARN_ON(1); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 368 | ret = imx_prepare_enable_clks(dev); |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 369 | if (ret) |
| 370 | return ret; |
| 371 | |
| 372 | data->in_lpm = false; |
| 373 | |
| 374 | ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false); |
| 375 | if (ret) { |
| 376 | dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret); |
| 377 | goto clk_disable; |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | |
| 382 | clk_disable: |
Peter Chen | ae3e57a | 2015-09-16 09:40:51 +0800 | [diff] [blame^] | 383 | imx_disable_unprepare_clks(dev); |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 384 | return ret; |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 385 | } |
| 386 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 387 | #ifdef CONFIG_PM_SLEEP |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 388 | static int ci_hdrc_imx_suspend(struct device *dev) |
| 389 | { |
Peter Chen | 6d65311 | 2015-02-11 12:44:49 +0800 | [diff] [blame] | 390 | int ret; |
| 391 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 392 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 393 | |
| 394 | if (data->in_lpm) |
| 395 | /* The core's suspend doesn't run */ |
| 396 | return 0; |
| 397 | |
Peter Chen | 6d65311 | 2015-02-11 12:44:49 +0800 | [diff] [blame] | 398 | if (device_may_wakeup(dev)) { |
| 399 | ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true); |
| 400 | if (ret) { |
| 401 | dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", |
| 402 | ret); |
| 403 | return ret; |
| 404 | } |
| 405 | } |
| 406 | |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 407 | return imx_controller_suspend(dev); |
| 408 | } |
| 409 | |
| 410 | static int ci_hdrc_imx_resume(struct device *dev) |
| 411 | { |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 412 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 413 | int ret; |
| 414 | |
| 415 | ret = imx_controller_resume(dev); |
| 416 | if (!ret && data->supports_runtime_pm) { |
| 417 | pm_runtime_disable(dev); |
| 418 | pm_runtime_set_active(dev); |
| 419 | pm_runtime_enable(dev); |
| 420 | } |
| 421 | |
| 422 | return ret; |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 423 | } |
| 424 | #endif /* CONFIG_PM_SLEEP */ |
| 425 | |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 426 | static int ci_hdrc_imx_runtime_suspend(struct device *dev) |
| 427 | { |
| 428 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
| 429 | int ret; |
| 430 | |
| 431 | if (data->in_lpm) { |
| 432 | WARN_ON(1); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true); |
| 437 | if (ret) { |
| 438 | dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret); |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | return imx_controller_suspend(dev); |
| 443 | } |
| 444 | |
| 445 | static int ci_hdrc_imx_runtime_resume(struct device *dev) |
| 446 | { |
| 447 | return imx_controller_resume(dev); |
| 448 | } |
| 449 | |
| 450 | #endif /* CONFIG_PM */ |
| 451 | |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 452 | static const struct dev_pm_ops ci_hdrc_imx_pm_ops = { |
| 453 | SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume) |
Peter Chen | e14db48 | 2015-02-11 12:44:47 +0800 | [diff] [blame] | 454 | SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend, |
| 455 | ci_hdrc_imx_runtime_resume, NULL) |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 456 | }; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 457 | static struct platform_driver ci_hdrc_imx_driver = { |
| 458 | .probe = ci_hdrc_imx_probe, |
| 459 | .remove = ci_hdrc_imx_remove, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 460 | .driver = { |
| 461 | .name = "imx_usb", |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 462 | .of_match_table = ci_hdrc_imx_dt_ids, |
Peter Chen | 2558c1f | 2014-11-26 13:44:30 +0800 | [diff] [blame] | 463 | .pm = &ci_hdrc_imx_pm_ops, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 464 | }, |
| 465 | }; |
| 466 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 467 | module_platform_driver(ci_hdrc_imx_driver); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 468 | |
| 469 | MODULE_ALIAS("platform:imx-usb"); |
| 470 | MODULE_LICENSE("GPL v2"); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 471 | MODULE_DESCRIPTION("CI HDRC i.MX USB binding"); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 472 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
| 473 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); |