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