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/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/usb/chipidea.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/ulpi.h>
20 
21 #include "ci.h"
22 
23 struct ci_hdrc_usb2_priv {
24 	struct platform_device	*ci_pdev;
25 	struct clk		*clk;
26 };
27 
28 static const struct ci_hdrc_platform_data ci_default_pdata = {
29 	.capoffset	= DEF_CAPOFFSET,
30 	.flags		= CI_HDRC_DISABLE_STREAMING,
31 };
32 
33 static int ci_hdrc_usb2_probe(struct platform_device *pdev)
34 {
35 	struct device *dev = &pdev->dev;
36 	struct ci_hdrc_usb2_priv *priv;
37 	struct ci_hdrc_platform_data *ci_pdata = dev_get_platdata(dev);
38 	int ret;
39 
40 	if (!ci_pdata) {
41 		ci_pdata = devm_kmalloc(dev, sizeof(*ci_pdata), GFP_KERNEL);
42 		*ci_pdata = ci_default_pdata;	/* struct copy */
43 	}
44 
45 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
46 	if (!priv)
47 		return -ENOMEM;
48 
49 	priv->clk = devm_clk_get(dev, NULL);
50 	if (!IS_ERR(priv->clk)) {
51 		ret = clk_prepare_enable(priv->clk);
52 		if (ret) {
53 			dev_err(dev, "failed to enable the clock: %d\n", ret);
54 			return ret;
55 		}
56 	}
57 
58 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
59 	if (ret)
60 		goto clk_err;
61 
62 	ci_pdata->name = dev_name(dev);
63 
64 	priv->ci_pdev = ci_hdrc_add_device(dev, pdev->resource,
65 					   pdev->num_resources, ci_pdata);
66 	if (IS_ERR(priv->ci_pdev)) {
67 		ret = PTR_ERR(priv->ci_pdev);
68 		if (ret != -EPROBE_DEFER)
69 			dev_err(dev,
70 				"failed to register ci_hdrc platform device: %d\n",
71 				ret);
72 		goto clk_err;
73 	}
74 
75 	platform_set_drvdata(pdev, priv);
76 
77 	pm_runtime_no_callbacks(dev);
78 	pm_runtime_enable(dev);
79 
80 	return 0;
81 
82 clk_err:
83 	if (!IS_ERR(priv->clk))
84 		clk_disable_unprepare(priv->clk);
85 	return ret;
86 }
87 
88 static int ci_hdrc_usb2_remove(struct platform_device *pdev)
89 {
90 	struct ci_hdrc_usb2_priv *priv = platform_get_drvdata(pdev);
91 
92 	pm_runtime_disable(&pdev->dev);
93 	ci_hdrc_remove_device(priv->ci_pdev);
94 	clk_disable_unprepare(priv->clk);
95 
96 	return 0;
97 }
98 
99 static const struct of_device_id ci_hdrc_usb2_of_match[] = {
100 	{ .compatible = "chipidea,usb2" },
101 	{ }
102 };
103 MODULE_DEVICE_TABLE(of, ci_hdrc_usb2_of_match);
104 
105 static struct platform_driver ci_hdrc_usb2_driver = {
106 	.probe	= ci_hdrc_usb2_probe,
107 	.remove	= ci_hdrc_usb2_remove,
108 	.driver	= {
109 		.name		= "chipidea-usb2",
110 		.of_match_table	= of_match_ptr(ci_hdrc_usb2_of_match),
111 	},
112 };
113 module_platform_driver(ci_hdrc_usb2_driver);
114 
115 MODULE_DESCRIPTION("ChipIdea HDRC USB2 binding for ci13xxx");
116 MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
117 MODULE_LICENSE("GPL");
118