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> |
| 22 | #include <linux/regulator/consumer.h> |
| 23 | |
| 24 | #include "ci.h" |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 25 | #include "ci_hdrc_imx.h" |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 26 | |
| 27 | #define pdev_to_phy(pdev) \ |
| 28 | ((struct usb_phy *)platform_get_drvdata(pdev)) |
| 29 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 30 | struct ci_hdrc_imx_data { |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 31 | struct usb_phy *phy; |
| 32 | struct platform_device *ci_pdev; |
| 33 | struct clk *clk; |
| 34 | struct regulator *reg_vbus; |
| 35 | }; |
| 36 | |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 37 | static const struct usbmisc_ops *usbmisc_ops; |
| 38 | |
| 39 | /* Common functions shared by usbmisc drivers */ |
| 40 | |
| 41 | int usbmisc_set_ops(const struct usbmisc_ops *ops) |
| 42 | { |
| 43 | if (usbmisc_ops) |
| 44 | return -EBUSY; |
| 45 | |
| 46 | usbmisc_ops = ops; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(usbmisc_set_ops); |
| 51 | |
| 52 | void usbmisc_unset_ops(const struct usbmisc_ops *ops) |
| 53 | { |
| 54 | usbmisc_ops = NULL; |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(usbmisc_unset_ops); |
| 57 | |
| 58 | int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev) |
| 59 | { |
| 60 | struct device_node *np = dev->of_node; |
| 61 | struct of_phandle_args args; |
| 62 | int ret; |
| 63 | |
| 64 | usbdev->dev = dev; |
| 65 | |
| 66 | ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells", |
| 67 | 0, &args); |
| 68 | if (ret) { |
| 69 | dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n", |
| 70 | ret); |
| 71 | memset(usbdev, 0, sizeof(*usbdev)); |
| 72 | return ret; |
| 73 | } |
| 74 | usbdev->index = args.args[0]; |
| 75 | of_node_put(args.np); |
| 76 | |
| 77 | if (of_find_property(np, "disable-over-current", NULL)) |
| 78 | usbdev->disable_oc = 1; |
| 79 | |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 80 | if (of_find_property(np, "external-vbus-divider", NULL)) |
| 81 | usbdev->evdo = 1; |
| 82 | |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(usbmisc_get_init_data); |
| 86 | |
| 87 | /* End of common functions shared by usbmisc drivers*/ |
| 88 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 89 | static int ci_hdrc_imx_probe(struct platform_device *pdev) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 90 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 91 | struct ci_hdrc_imx_data *data; |
| 92 | struct ci_hdrc_platform_data pdata = { |
| 93 | .name = "ci_hdrc_imx", |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 94 | .capoffset = DEF_CAPOFFSET, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 95 | .flags = CI_HDRC_REQUIRE_TRANSCEIVER | |
| 96 | CI_HDRC_PULLUP_ON_VBUS | |
| 97 | CI_HDRC_DISABLE_STREAMING, |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 98 | }; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 99 | struct resource *res; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 100 | int ret; |
| 101 | |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 102 | if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL) |
| 103 | && !usbmisc_ops) |
| 104 | return -EPROBE_DEFER; |
| 105 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 106 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 107 | if (!data) { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 108 | dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n"); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 109 | return -ENOMEM; |
| 110 | } |
| 111 | |
| 112 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 113 | if (!res) { |
| 114 | dev_err(&pdev->dev, "Can't get device resources!\n"); |
| 115 | return -ENOENT; |
| 116 | } |
| 117 | |
| 118 | data->clk = devm_clk_get(&pdev->dev, NULL); |
| 119 | if (IS_ERR(data->clk)) { |
| 120 | dev_err(&pdev->dev, |
| 121 | "Failed to get clock, err=%ld\n", PTR_ERR(data->clk)); |
| 122 | return PTR_ERR(data->clk); |
| 123 | } |
| 124 | |
| 125 | ret = clk_prepare_enable(data->clk); |
| 126 | if (ret) { |
| 127 | dev_err(&pdev->dev, |
| 128 | "Failed to prepare or enable clock, err=%d\n", ret); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Fabio Estevam | 046916d | 2013-06-25 12:58:05 +0300 | [diff] [blame^] | 132 | data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0); |
| 133 | if (!IS_ERR(data->phy)) { |
| 134 | ret = usb_phy_init(data->phy); |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 135 | if (ret) { |
| 136 | dev_err(&pdev->dev, "unable to init phy: %d\n", ret); |
| 137 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 138 | } |
Fabio Estevam | 046916d | 2013-06-25 12:58:05 +0300 | [diff] [blame^] | 139 | } else if (PTR_ERR(data->phy) == -EPROBE_DEFER) { |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 140 | ret = -EPROBE_DEFER; |
| 141 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* we only support host now, so enable vbus here */ |
Fabio Estevam | e56ae54 | 2013-06-13 17:59:49 +0300 | [diff] [blame] | 145 | data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus"); |
| 146 | if (!IS_ERR(data->reg_vbus)) { |
| 147 | ret = regulator_enable(data->reg_vbus); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 148 | if (ret) { |
| 149 | dev_err(&pdev->dev, |
| 150 | "Failed to enable vbus regulator, err=%d\n", |
| 151 | ret); |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 152 | goto err_clk; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 153 | } |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 154 | } else { |
Fabio Estevam | e56ae54 | 2013-06-13 17:59:49 +0300 | [diff] [blame] | 155 | data->reg_vbus = NULL; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 158 | pdata.phy = data->phy; |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 159 | |
Stephen Warren | 3b9561e | 2013-05-07 16:53:52 -0600 | [diff] [blame] | 160 | if (!pdev->dev.dma_mask) |
| 161 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 162 | if (!pdev->dev.coherent_dma_mask) |
| 163 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
Richard Zhao | d142d6b | 2012-09-12 14:58:05 +0300 | [diff] [blame] | 164 | |
| 165 | if (usbmisc_ops && usbmisc_ops->init) { |
| 166 | ret = usbmisc_ops->init(&pdev->dev); |
| 167 | if (ret) { |
| 168 | dev_err(&pdev->dev, |
| 169 | "usbmisc init failed, ret=%d\n", ret); |
| 170 | goto err; |
| 171 | } |
| 172 | } |
| 173 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 174 | data->ci_pdev = ci_hdrc_add_device(&pdev->dev, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 175 | pdev->resource, pdev->num_resources, |
Michael Grzeschik | f6a3b3a | 2013-06-13 17:59:58 +0300 | [diff] [blame] | 176 | &pdata); |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 177 | if (IS_ERR(data->ci_pdev)) { |
| 178 | ret = PTR_ERR(data->ci_pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 179 | dev_err(&pdev->dev, |
| 180 | "Can't register ci_hdrc platform device, err=%d\n", |
| 181 | ret); |
| 182 | goto err; |
| 183 | } |
| 184 | |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 185 | if (usbmisc_ops && usbmisc_ops->post) { |
| 186 | ret = usbmisc_ops->post(&pdev->dev); |
| 187 | if (ret) { |
| 188 | dev_err(&pdev->dev, |
| 189 | "usbmisc post failed, ret=%d\n", ret); |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 190 | goto disable_device; |
Michael Grzeschik | a068533 | 2013-03-30 12:54:01 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 194 | platform_set_drvdata(pdev, data); |
| 195 | |
| 196 | pm_runtime_no_callbacks(&pdev->dev); |
| 197 | pm_runtime_enable(&pdev->dev); |
| 198 | |
| 199 | return 0; |
| 200 | |
Fabio Estevam | 770719d | 2013-06-13 17:59:48 +0300 | [diff] [blame] | 201 | disable_device: |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 202 | ci_hdrc_remove_device(data->ci_pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 203 | err: |
Fabio Estevam | e56ae54 | 2013-06-13 17:59:49 +0300 | [diff] [blame] | 204 | if (data->reg_vbus) |
| 205 | regulator_disable(data->reg_vbus); |
Sascha Hauer | ea1418b | 2013-06-13 18:00:00 +0300 | [diff] [blame] | 206 | err_clk: |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 207 | clk_disable_unprepare(data->clk); |
| 208 | return ret; |
| 209 | } |
| 210 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 211 | static int ci_hdrc_imx_remove(struct platform_device *pdev) |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 212 | { |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 213 | struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 214 | |
| 215 | pm_runtime_disable(&pdev->dev); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 216 | ci_hdrc_remove_device(data->ci_pdev); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 217 | |
| 218 | if (data->reg_vbus) |
| 219 | regulator_disable(data->reg_vbus); |
| 220 | |
| 221 | if (data->phy) { |
| 222 | usb_phy_shutdown(data->phy); |
| 223 | module_put(data->phy->dev->driver->owner); |
| 224 | } |
| 225 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 226 | clk_disable_unprepare(data->clk); |
| 227 | |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 231 | static const struct of_device_id ci_hdrc_imx_dt_ids[] = { |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 232 | { .compatible = "fsl,imx27-usb", }, |
| 233 | { /* sentinel */ } |
| 234 | }; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 235 | MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 236 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 237 | static struct platform_driver ci_hdrc_imx_driver = { |
| 238 | .probe = ci_hdrc_imx_probe, |
| 239 | .remove = ci_hdrc_imx_remove, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 240 | .driver = { |
| 241 | .name = "imx_usb", |
| 242 | .owner = THIS_MODULE, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 243 | .of_match_table = ci_hdrc_imx_dt_ids, |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 244 | }, |
| 245 | }; |
| 246 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 247 | module_platform_driver(ci_hdrc_imx_driver); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 248 | |
| 249 | MODULE_ALIAS("platform:imx-usb"); |
| 250 | MODULE_LICENSE("GPL v2"); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 251 | MODULE_DESCRIPTION("CI HDRC i.MX USB binding"); |
Richard Zhao | 1530280 | 2012-07-07 22:56:48 +0800 | [diff] [blame] | 252 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
| 253 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); |