1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2014 Marvell Technology Group Ltd. 4 * 5 * Antoine Tenart <antoine.tenart@free-electrons.com> 6 * 7 * This file is licensed under the terms of the GNU General Public 8 * License version 2. This program is licensed "as is" without any 9 * warranty of any kind, whether express or implied. 10 */ 11 12 #include <linux/clk.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/module.h> 15 #include <linux/of.h> 16 #include <linux/of_platform.h> 17 #include <linux/phy/phy.h> 18 #include <linux/platform_device.h> 19 #include <linux/usb/chipidea.h> 20 #include <linux/usb/hcd.h> 21 #include <linux/usb/ulpi.h> 22 23 #include "ci.h" 24 25 struct ci_hdrc_usb2_priv { 26 struct platform_device *ci_pdev; 27 struct clk *clk; 28 }; 29 30 static const struct ci_hdrc_platform_data ci_default_pdata = { 31 .capoffset = DEF_CAPOFFSET, 32 .flags = CI_HDRC_DISABLE_STREAMING, 33 }; 34 35 static struct ci_hdrc_platform_data ci_zynq_pdata = { 36 .capoffset = DEF_CAPOFFSET, 37 }; 38 39 static const struct of_device_id ci_hdrc_usb2_of_match[] = { 40 { .compatible = "chipidea,usb2"}, 41 { .compatible = "xlnx,zynq-usb-2.20a", .data = &ci_zynq_pdata}, 42 { } 43 }; 44 MODULE_DEVICE_TABLE(of, ci_hdrc_usb2_of_match); 45 46 static int ci_hdrc_usb2_probe(struct platform_device *pdev) 47 { 48 struct device *dev = &pdev->dev; 49 struct ci_hdrc_usb2_priv *priv; 50 struct ci_hdrc_platform_data *ci_pdata = dev_get_platdata(dev); 51 int ret; 52 const struct of_device_id *match; 53 54 if (!ci_pdata) { 55 ci_pdata = devm_kmalloc(dev, sizeof(*ci_pdata), GFP_KERNEL); 56 if (!ci_pdata) 57 return -ENOMEM; 58 *ci_pdata = ci_default_pdata; /* struct copy */ 59 } 60 61 match = of_match_device(ci_hdrc_usb2_of_match, &pdev->dev); 62 if (match && match->data) { 63 /* struct copy */ 64 *ci_pdata = *(struct ci_hdrc_platform_data *)match->data; 65 } 66 67 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 68 if (!priv) 69 return -ENOMEM; 70 71 priv->clk = devm_clk_get(dev, NULL); 72 if (!IS_ERR(priv->clk)) { 73 ret = clk_prepare_enable(priv->clk); 74 if (ret) { 75 dev_err(dev, "failed to enable the clock: %d\n", ret); 76 return ret; 77 } 78 } 79 80 ci_pdata->name = dev_name(dev); 81 82 priv->ci_pdev = ci_hdrc_add_device(dev, pdev->resource, 83 pdev->num_resources, ci_pdata); 84 if (IS_ERR(priv->ci_pdev)) { 85 ret = PTR_ERR(priv->ci_pdev); 86 if (ret != -EPROBE_DEFER) 87 dev_err(dev, 88 "failed to register ci_hdrc platform device: %d\n", 89 ret); 90 goto clk_err; 91 } 92 93 platform_set_drvdata(pdev, priv); 94 95 pm_runtime_no_callbacks(dev); 96 pm_runtime_enable(dev); 97 98 return 0; 99 100 clk_err: 101 if (!IS_ERR(priv->clk)) 102 clk_disable_unprepare(priv->clk); 103 return ret; 104 } 105 106 static int ci_hdrc_usb2_remove(struct platform_device *pdev) 107 { 108 struct ci_hdrc_usb2_priv *priv = platform_get_drvdata(pdev); 109 110 pm_runtime_disable(&pdev->dev); 111 ci_hdrc_remove_device(priv->ci_pdev); 112 clk_disable_unprepare(priv->clk); 113 114 return 0; 115 } 116 117 static struct platform_driver ci_hdrc_usb2_driver = { 118 .probe = ci_hdrc_usb2_probe, 119 .remove = ci_hdrc_usb2_remove, 120 .driver = { 121 .name = "chipidea-usb2", 122 .of_match_table = of_match_ptr(ci_hdrc_usb2_of_match), 123 }, 124 }; 125 module_platform_driver(ci_hdrc_usb2_driver); 126 127 MODULE_DESCRIPTION("ChipIdea HDRC USB2 binding for ci13xxx"); 128 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>"); 129 MODULE_LICENSE("GPL"); 130