summaryrefslogtreecommitdiff
path: root/drivers/phy/exynos-phy.c
blob: 46d5603b6d76aab82795c358ea70a0954c5784c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Exynos PHY driver
 *
 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
 * Author: Tomasz Stanislawski <t.stanislaws@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/phy/phy.h>

#define EXYNOS_PHY_ENABLE	(1 << 0)

static int exynos_phy_power_on(struct phy *phy)
{
	void __iomem *reg = phy_get_drvdata(phy);
	u32 val;

	val = readl(reg);
	val |= EXYNOS_PHY_ENABLE;
	writel(val, reg);

	return 0;
}

static int exynos_phy_power_off(struct phy *phy)
{
	void __iomem *reg = phy_get_drvdata(phy);
	u32 val;

	val = readl(reg);
	val &= ~EXYNOS_PHY_ENABLE;
	writel(val, reg);

	return 0;
}

static struct phy_ops exynos_phy_ops = {
	.power_on	= exynos_phy_power_on,
	.power_off	= exynos_phy_power_off,
	.owner		= THIS_MODULE,
};

static const u32 exynos4210_offsets[] = {
	0x0700, /* HDMI_PHY */
	0x070C, /* DAC_PHY */
	0x0718, /* ADC_PHY */
	0x071C, /* PCIE_PHY */
	0x0720, /* SATA_PHY */
	~0, /* end mark */
};

static const u32 exynos4412_offsets[] = {
	0x0700, /* HDMI_PHY */
	0x0718, /* ADC_PHY */
	~0, /* end mark */
};

static const struct of_device_id exynos_phy_of_match[] = {
	{ .compatible = "exynos4210-phy", .data = exynos4210_offsets},
	{ .compatible = "exynos4412-phy", .data = exynos4412_offsets},
	{ },
};
MODULE_DEVICE_TABLE(of, exynos_phy_of_match);

static struct phy *exynos_phy_xlate(struct device *dev,
					struct of_phandle_args *args)
{
	struct phy **phys = dev_get_drvdata(dev);
	int index = args->args[0];
	int i;

	/* verify if index is valid */
	for (i = 0; i <= index; ++i)
		if (!phys[i])
			return ERR_PTR(-ENODEV);

	return phys[index];
}

static int exynos_phy_probe(struct platform_device *pdev)
{
	const struct of_device_id *of_id = of_match_device(
		of_match_ptr(exynos_phy_of_match), &pdev->dev);
	const u32 *offsets = of_id->data;
	int count;
	struct device *dev = &pdev->dev;
	struct phy **phys;
	struct resource *res;
	void __iomem *regs;
	int i;
	struct phy_provider *phy_provider;

	/* count number of phys to create */
	for (count = 0; offsets[count] != ~0; ++count)
		;

	phys = devm_kzalloc(dev, (count + 1) * sizeof(phys[0]), GFP_KERNEL);
	if (!phys)
		return -ENOMEM;

	dev_set_drvdata(dev, phys);
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

	regs = devm_ioremap(dev, res->start, res->end - res->start);
	if (!regs) {
		dev_err(dev, "failed to ioremap registers\n");
		return -EFAULT;
	}

	/* NOTE: last entry in phys[] is NULL */
	for (i = 0; i < count; ++i) {
		phys[i] = devm_phy_create(dev, &exynos_phy_ops, NULL);
		if (IS_ERR(phys[i])) {
			dev_err(dev, "failed to create PHY %d\n", i);
			return PTR_ERR(phys[i]);
		}
		phy_set_drvdata(phys[i], regs + offsets[i]);
	}

	phy_provider = devm_of_phy_provider_register(dev, exynos_phy_xlate);
	if (IS_ERR(phy_provider)) {
		dev_err(dev, "failed to register PHY provider\n");
		return PTR_ERR(phy_provider);
	}

	dev_info(dev, "added %d phys\n", count);

	return 0;
}

static struct platform_driver exynos_phy_driver = {
	.probe	= exynos_phy_probe,
	.driver = {
		.of_match_table	= exynos_phy_of_match,
		.name  = "exynos-phy",
		.owner = THIS_MODULE,
	}
};
module_platform_driver(exynos_phy_driver);

MODULE_DESCRIPTION("Exynos PHY driver");
MODULE_AUTHOR("Tomasz Stanislawski <t.stanislaws@samsung.com>");
MODULE_LICENSE("GPL v2");