dwc3-exynos.c 5.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * dwc3-exynos.c - Samsung EXYNOS DWC3 Specific Glue layer
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * Author: Anton Tikhomirov <av.tikhomirov@samsung.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
16
#include <linux/of.h>
17
#include <linux/of_platform.h>
18
#include <linux/regulator/consumer.h>
19

20 21 22 23 24 25 26 27
#define DWC3_EXYNOS_MAX_CLOCKS	4

struct dwc3_exynos_driverdata {
	const char		*clk_names[DWC3_EXYNOS_MAX_CLOCKS];
	int			num_clks;
	int			suspend_clk_idx;
};

28 29 30
struct dwc3_exynos {
	struct device		*dev;

31 32 33 34
	const char		**clk_names;
	struct clk		*clks[DWC3_EXYNOS_MAX_CLOCKS];
	int			num_clks;
	int			suspend_clk_idx;
35

36 37
	struct regulator	*vdd33;
	struct regulator	*vdd10;
38 39
};

40 41 42 43 44 45 46 47 48
static int dwc3_exynos_remove_child(struct device *dev, void *unused)
{
	struct platform_device *pdev = to_platform_device(dev);

	platform_device_unregister(pdev);

	return 0;
}

B
Bill Pemberton 已提交
49
static int dwc3_exynos_probe(struct platform_device *pdev)
50 51
{
	struct dwc3_exynos	*exynos;
52
	struct device		*dev = &pdev->dev;
53
	struct device_node	*node = dev->of_node;
54 55
	const struct dwc3_exynos_driverdata *driver_data;
	int			i, ret;
56

57
	exynos = devm_kzalloc(dev, sizeof(*exynos), GFP_KERNEL);
58
	if (!exynos)
59
		return -ENOMEM;
60

61 62 63 64 65
	driver_data = of_device_get_match_data(dev);
	exynos->dev = dev;
	exynos->num_clks = driver_data->num_clks;
	exynos->clk_names = (const char **)driver_data->clk_names;
	exynos->suspend_clk_idx = driver_data->suspend_clk_idx;
66

67
	platform_set_drvdata(pdev, exynos);
68

69 70 71 72 73 74 75
	for (i = 0; i < exynos->num_clks; i++) {
		exynos->clks[i] = devm_clk_get(dev, exynos->clk_names[i]);
		if (IS_ERR(exynos->clks[i])) {
			dev_err(dev, "failed to get clock: %s\n",
				exynos->clk_names[i]);
			return PTR_ERR(exynos->clks[i]);
		}
76 77
	}

78 79 80 81 82 83
	for (i = 0; i < exynos->num_clks; i++) {
		ret = clk_prepare_enable(exynos->clks[i]);
		if (ret) {
			while (--i > 0)
				clk_disable_unprepare(exynos->clks[i]);
			return ret;
84 85 86
		}
	}

87 88 89
	if (exynos->suspend_clk_idx >= 0)
		clk_prepare_enable(exynos->clks[exynos->suspend_clk_idx]);

90 91 92
	exynos->vdd33 = devm_regulator_get(dev, "vdd33");
	if (IS_ERR(exynos->vdd33)) {
		ret = PTR_ERR(exynos->vdd33);
93
		goto vdd33_err;
94 95 96 97
	}
	ret = regulator_enable(exynos->vdd33);
	if (ret) {
		dev_err(dev, "Failed to enable VDD33 supply\n");
98
		goto vdd33_err;
99 100 101 102 103
	}

	exynos->vdd10 = devm_regulator_get(dev, "vdd10");
	if (IS_ERR(exynos->vdd10)) {
		ret = PTR_ERR(exynos->vdd10);
104
		goto vdd10_err;
105 106 107 108
	}
	ret = regulator_enable(exynos->vdd10);
	if (ret) {
		dev_err(dev, "Failed to enable VDD10 supply\n");
109
		goto vdd10_err;
110 111
	}

112 113 114 115
	if (node) {
		ret = of_platform_populate(node, NULL, NULL, dev);
		if (ret) {
			dev_err(dev, "failed to add dwc3 core\n");
116
			goto populate_err;
117 118 119 120
		}
	} else {
		dev_err(dev, "no device node, failed to add dwc3 core\n");
		ret = -ENODEV;
121
		goto populate_err;
122 123 124 125
	}

	return 0;

126
populate_err:
127
	regulator_disable(exynos->vdd10);
128
vdd10_err:
129
	regulator_disable(exynos->vdd33);
130
vdd33_err:
131 132 133 134 135 136
	for (i = exynos->num_clks - 1; i >= 0; i--)
		clk_disable_unprepare(exynos->clks[i]);

	if (exynos->suspend_clk_idx >= 0)
		clk_disable_unprepare(exynos->clks[exynos->suspend_clk_idx]);

137 138 139
	return ret;
}

B
Bill Pemberton 已提交
140
static int dwc3_exynos_remove(struct platform_device *pdev)
141 142
{
	struct dwc3_exynos	*exynos = platform_get_drvdata(pdev);
143
	int i;
144

145
	device_for_each_child(&pdev->dev, NULL, dwc3_exynos_remove_child);
146

147 148 149 150 151
	for (i = exynos->num_clks - 1; i >= 0; i--)
		clk_disable_unprepare(exynos->clks[i]);

	if (exynos->suspend_clk_idx >= 0)
		clk_disable_unprepare(exynos->clks[exynos->suspend_clk_idx]);
152

153 154 155
	regulator_disable(exynos->vdd33);
	regulator_disable(exynos->vdd10);

156 157 158
	return 0;
}

159 160 161 162 163 164 165 166 167 168 169 170
static const struct dwc3_exynos_driverdata exynos5250_drvdata = {
	.clk_names = { "usbdrd30" },
	.num_clks = 1,
	.suspend_clk_idx = -1,
};

static const struct dwc3_exynos_driverdata exynos7_drvdata = {
	.clk_names = { "usbdrd30", "usbdrd30_susp_clk", "usbdrd30_axius_clk" },
	.num_clks = 3,
	.suspend_clk_idx = 1,
};

171
static const struct of_device_id exynos_dwc3_match[] = {
172 173 174 175 176 177 178 179
	{
		.compatible = "samsung,exynos5250-dwusb3",
		.data = &exynos5250_drvdata,
	}, {
		.compatible = "samsung,exynos7-dwusb3",
		.data = &exynos7_drvdata,
	}, {
	}
180 181 182
};
MODULE_DEVICE_TABLE(of, exynos_dwc3_match);

183
#ifdef CONFIG_PM_SLEEP
184 185 186
static int dwc3_exynos_suspend(struct device *dev)
{
	struct dwc3_exynos *exynos = dev_get_drvdata(dev);
187
	int i;
188

189 190
	for (i = exynos->num_clks - 1; i >= 0; i--)
		clk_disable_unprepare(exynos->clks[i]);
191

192 193 194
	regulator_disable(exynos->vdd33);
	regulator_disable(exynos->vdd10);

195 196 197 198 199 200
	return 0;
}

static int dwc3_exynos_resume(struct device *dev)
{
	struct dwc3_exynos *exynos = dev_get_drvdata(dev);
201
	int i, ret;
202 203 204 205 206 207 208 209 210 211 212

	ret = regulator_enable(exynos->vdd33);
	if (ret) {
		dev_err(dev, "Failed to enable VDD33 supply\n");
		return ret;
	}
	ret = regulator_enable(exynos->vdd10);
	if (ret) {
		dev_err(dev, "Failed to enable VDD10 supply\n");
		return ret;
	}
213

214 215 216 217 218 219 220 221
	for (i = 0; i < exynos->num_clks; i++) {
		ret = clk_prepare_enable(exynos->clks[i]);
		if (ret) {
			while (--i > 0)
				clk_disable_unprepare(exynos->clks[i]);
			return ret;
		}
	}
222 223 224 225 226 227 228 229 230 231 232

	return 0;
}

static const struct dev_pm_ops dwc3_exynos_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(dwc3_exynos_suspend, dwc3_exynos_resume)
};

#define DEV_PM_OPS	(&dwc3_exynos_dev_pm_ops)
#else
#define DEV_PM_OPS	NULL
233
#endif /* CONFIG_PM_SLEEP */
234

235 236
static struct platform_driver dwc3_exynos_driver = {
	.probe		= dwc3_exynos_probe,
B
Bill Pemberton 已提交
237
	.remove		= dwc3_exynos_remove,
238 239
	.driver		= {
		.name	= "exynos-dwc3",
240
		.of_match_table = exynos_dwc3_match,
241
		.pm	= DEV_PM_OPS,
242 243 244 245 246 247
	},
};

module_platform_driver(dwc3_exynos_driver);

MODULE_AUTHOR("Anton Tikhomirov <av.tikhomirov@samsung.com>");
F
Felipe Balbi 已提交
248
MODULE_LICENSE("GPL v2");
249
MODULE_DESCRIPTION("DesignWare USB3 EXYNOS Glue Layer");