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