1 /*
2  * Copyright (c) 2016, NVIDIA Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/reset.h>
13 
14 #include <linux/usb/chipidea.h>
15 
16 #include "ci.h"
17 
18 struct tegra_udc {
19 	struct ci_hdrc_platform_data data;
20 	struct platform_device *dev;
21 
22 	struct usb_phy *phy;
23 	struct clk *clk;
24 };
25 
26 struct tegra_udc_soc_info {
27 	unsigned long flags;
28 };
29 
30 static const struct tegra_udc_soc_info tegra20_udc_soc_info = {
31 	.flags = CI_HDRC_REQUIRES_ALIGNED_DMA,
32 };
33 
34 static const struct tegra_udc_soc_info tegra30_udc_soc_info = {
35 	.flags = 0,
36 };
37 
38 static const struct tegra_udc_soc_info tegra114_udc_soc_info = {
39 	.flags = 0,
40 };
41 
42 static const struct tegra_udc_soc_info tegra124_udc_soc_info = {
43 	.flags = 0,
44 };
45 
46 static const struct of_device_id tegra_udc_of_match[] = {
47 	{
48 		.compatible = "nvidia,tegra20-udc",
49 		.data = &tegra20_udc_soc_info,
50 	}, {
51 		.compatible = "nvidia,tegra30-udc",
52 		.data = &tegra30_udc_soc_info,
53 	}, {
54 		.compatible = "nvidia,tegra114-udc",
55 		.data = &tegra114_udc_soc_info,
56 	}, {
57 		.compatible = "nvidia,tegra124-udc",
58 		.data = &tegra124_udc_soc_info,
59 	}, {
60 		/* sentinel */
61 	}
62 };
63 MODULE_DEVICE_TABLE(of, tegra_udc_of_match);
64 
65 static int tegra_udc_probe(struct platform_device *pdev)
66 {
67 	const struct tegra_udc_soc_info *soc;
68 	struct tegra_udc *udc;
69 	int err;
70 
71 	udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
72 	if (!udc)
73 		return -ENOMEM;
74 
75 	soc = of_device_get_match_data(&pdev->dev);
76 	if (!soc) {
77 		dev_err(&pdev->dev, "failed to match OF data\n");
78 		return -EINVAL;
79 	}
80 
81 	udc->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "nvidia,phy", 0);
82 	if (IS_ERR(udc->phy)) {
83 		err = PTR_ERR(udc->phy);
84 		dev_err(&pdev->dev, "failed to get PHY: %d\n", err);
85 		return err;
86 	}
87 
88 	udc->clk = devm_clk_get(&pdev->dev, NULL);
89 	if (IS_ERR(udc->clk)) {
90 		err = PTR_ERR(udc->clk);
91 		dev_err(&pdev->dev, "failed to get clock: %d\n", err);
92 		return err;
93 	}
94 
95 	err = clk_prepare_enable(udc->clk);
96 	if (err < 0) {
97 		dev_err(&pdev->dev, "failed to enable clock: %d\n", err);
98 		return err;
99 	}
100 
101 	/*
102 	 * Tegra's USB PHY driver doesn't implement optional phy_init()
103 	 * hook, so we have to power on UDC controller before ChipIdea
104 	 * driver initialization kicks in.
105 	 */
106 	usb_phy_set_suspend(udc->phy, 0);
107 
108 	/* setup and register ChipIdea HDRC device */
109 	udc->data.name = "tegra-udc";
110 	udc->data.flags = soc->flags;
111 	udc->data.usb_phy = udc->phy;
112 	udc->data.capoffset = DEF_CAPOFFSET;
113 
114 	udc->dev = ci_hdrc_add_device(&pdev->dev, pdev->resource,
115 				      pdev->num_resources, &udc->data);
116 	if (IS_ERR(udc->dev)) {
117 		err = PTR_ERR(udc->dev);
118 		dev_err(&pdev->dev, "failed to add HDRC device: %d\n", err);
119 		goto fail_power_off;
120 	}
121 
122 	platform_set_drvdata(pdev, udc);
123 
124 	return 0;
125 
126 fail_power_off:
127 	usb_phy_set_suspend(udc->phy, 1);
128 	clk_disable_unprepare(udc->clk);
129 	return err;
130 }
131 
132 static int tegra_udc_remove(struct platform_device *pdev)
133 {
134 	struct tegra_udc *udc = platform_get_drvdata(pdev);
135 
136 	usb_phy_set_suspend(udc->phy, 1);
137 	clk_disable_unprepare(udc->clk);
138 
139 	return 0;
140 }
141 
142 static struct platform_driver tegra_udc_driver = {
143 	.driver = {
144 		.name = "tegra-udc",
145 		.of_match_table = tegra_udc_of_match,
146 	},
147 	.probe = tegra_udc_probe,
148 	.remove = tegra_udc_remove,
149 };
150 module_platform_driver(tegra_udc_driver);
151 
152 MODULE_DESCRIPTION("NVIDIA Tegra USB device mode driver");
153 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
154 MODULE_ALIAS("platform:tegra-udc");
155 MODULE_LICENSE("GPL v2");
156