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 23 #include "ci.h" 24 #include "ci_hdrc_imx.h" 25 26 #define CI_HDRC_IMX_IMX28_WRITE_FIX BIT(0) 27 28 struct ci_hdrc_imx_platform_flag { 29 unsigned int flags; 30 }; 31 32 static const struct ci_hdrc_imx_platform_flag imx27_usb_data = { 33 }; 34 35 static const struct ci_hdrc_imx_platform_flag imx28_usb_data = { 36 .flags = CI_HDRC_IMX_IMX28_WRITE_FIX, 37 }; 38 39 static const struct of_device_id ci_hdrc_imx_dt_ids[] = { 40 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data}, 41 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data}, 42 { /* sentinel */ } 43 }; 44 MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids); 45 46 struct ci_hdrc_imx_data { 47 struct usb_phy *phy; 48 struct platform_device *ci_pdev; 49 struct clk *clk; 50 struct imx_usbmisc_data *usbmisc_data; 51 }; 52 53 /* Common functions shared by usbmisc drivers */ 54 55 static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev) 56 { 57 struct platform_device *misc_pdev; 58 struct device_node *np = dev->of_node; 59 struct of_phandle_args args; 60 struct imx_usbmisc_data *data; 61 int ret; 62 63 /* 64 * In case the fsl,usbmisc property is not present this device doesn't 65 * need usbmisc. Return NULL (which is no error here) 66 */ 67 if (!of_get_property(np, "fsl,usbmisc", NULL)) 68 return NULL; 69 70 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 71 if (!data) 72 return ERR_PTR(-ENOMEM); 73 74 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells", 75 0, &args); 76 if (ret) { 77 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n", 78 ret); 79 return ERR_PTR(ret); 80 } 81 82 data->index = args.args[0]; 83 84 misc_pdev = of_find_device_by_node(args.np); 85 of_node_put(args.np); 86 87 if (!misc_pdev) 88 return ERR_PTR(-EPROBE_DEFER); 89 90 data->dev = &misc_pdev->dev; 91 92 if (of_find_property(np, "disable-over-current", NULL)) 93 data->disable_oc = 1; 94 95 if (of_find_property(np, "external-vbus-divider", NULL)) 96 data->evdo = 1; 97 98 return data; 99 } 100 101 /* End of common functions shared by usbmisc drivers*/ 102 103 static int ci_hdrc_imx_probe(struct platform_device *pdev) 104 { 105 struct ci_hdrc_imx_data *data; 106 struct ci_hdrc_platform_data pdata = { 107 .name = dev_name(&pdev->dev), 108 .capoffset = DEF_CAPOFFSET, 109 .flags = CI_HDRC_DISABLE_STREAMING, 110 }; 111 int ret; 112 const struct of_device_id *of_id = 113 of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev); 114 const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data; 115 116 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 117 if (!data) 118 return -ENOMEM; 119 120 data->usbmisc_data = usbmisc_get_init_data(&pdev->dev); 121 if (IS_ERR(data->usbmisc_data)) 122 return PTR_ERR(data->usbmisc_data); 123 124 data->clk = devm_clk_get(&pdev->dev, NULL); 125 if (IS_ERR(data->clk)) { 126 dev_err(&pdev->dev, 127 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk)); 128 return PTR_ERR(data->clk); 129 } 130 131 ret = clk_prepare_enable(data->clk); 132 if (ret) { 133 dev_err(&pdev->dev, 134 "Failed to prepare or enable clock, err=%d\n", ret); 135 return ret; 136 } 137 138 data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0); 139 if (IS_ERR(data->phy)) { 140 ret = PTR_ERR(data->phy); 141 /* Return -EINVAL if no usbphy is available */ 142 if (ret == -ENODEV) 143 ret = -EINVAL; 144 goto err_clk; 145 } 146 147 pdata.usb_phy = data->phy; 148 149 if (imx_platform_flag->flags & CI_HDRC_IMX_IMX28_WRITE_FIX) 150 pdata.flags |= CI_HDRC_IMX28_WRITE_FIX; 151 152 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 153 if (ret) 154 goto err_clk; 155 156 if (data->usbmisc_data) { 157 ret = imx_usbmisc_init(data->usbmisc_data); 158 if (ret) { 159 dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", 160 ret); 161 goto err_clk; 162 } 163 } 164 165 data->ci_pdev = ci_hdrc_add_device(&pdev->dev, 166 pdev->resource, pdev->num_resources, 167 &pdata); 168 if (IS_ERR(data->ci_pdev)) { 169 ret = PTR_ERR(data->ci_pdev); 170 dev_err(&pdev->dev, 171 "Can't register ci_hdrc platform device, err=%d\n", 172 ret); 173 goto err_clk; 174 } 175 176 if (data->usbmisc_data) { 177 ret = imx_usbmisc_init_post(data->usbmisc_data); 178 if (ret) { 179 dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", 180 ret); 181 goto disable_device; 182 } 183 } 184 185 platform_set_drvdata(pdev, data); 186 187 pm_runtime_no_callbacks(&pdev->dev); 188 pm_runtime_enable(&pdev->dev); 189 190 return 0; 191 192 disable_device: 193 ci_hdrc_remove_device(data->ci_pdev); 194 err_clk: 195 clk_disable_unprepare(data->clk); 196 return ret; 197 } 198 199 static int ci_hdrc_imx_remove(struct platform_device *pdev) 200 { 201 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev); 202 203 pm_runtime_disable(&pdev->dev); 204 ci_hdrc_remove_device(data->ci_pdev); 205 clk_disable_unprepare(data->clk); 206 207 return 0; 208 } 209 210 #ifdef CONFIG_PM_SLEEP 211 static int imx_controller_suspend(struct device *dev) 212 { 213 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); 214 215 dev_dbg(dev, "at %s\n", __func__); 216 217 clk_disable_unprepare(data->clk); 218 219 return 0; 220 } 221 222 static int imx_controller_resume(struct device *dev) 223 { 224 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); 225 226 dev_dbg(dev, "at %s\n", __func__); 227 228 return clk_prepare_enable(data->clk); 229 } 230 231 static int ci_hdrc_imx_suspend(struct device *dev) 232 { 233 return imx_controller_suspend(dev); 234 } 235 236 static int ci_hdrc_imx_resume(struct device *dev) 237 { 238 return imx_controller_resume(dev); 239 } 240 #endif /* CONFIG_PM_SLEEP */ 241 242 static const struct dev_pm_ops ci_hdrc_imx_pm_ops = { 243 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume) 244 }; 245 static struct platform_driver ci_hdrc_imx_driver = { 246 .probe = ci_hdrc_imx_probe, 247 .remove = ci_hdrc_imx_remove, 248 .driver = { 249 .name = "imx_usb", 250 .of_match_table = ci_hdrc_imx_dt_ids, 251 .pm = &ci_hdrc_imx_pm_ops, 252 }, 253 }; 254 255 module_platform_driver(ci_hdrc_imx_driver); 256 257 MODULE_ALIAS("platform:imx-usb"); 258 MODULE_LICENSE("GPL v2"); 259 MODULE_DESCRIPTION("CI HDRC i.MX USB binding"); 260 MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); 261 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); 262