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