aboutsummaryrefslogtreecommitdiff
path: root/drivers/phy/mt7620-usb-phy.c
blob: 5045c3f3f9c8e343176c79756ad7c3ddea7e10ae (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
 *
 * Author: Weijie Gao <weijie.gao@mediatek.com>
 */

#include <clk.h>
#include <dm.h>
#include <generic-phy.h>
#include <misc.h>
#include <reset.h>
#include <dm/device_compat.h>
#include <linux/delay.h>
#include <mach/mt7620-sysc.h>

struct mt7620_usb_phy {
	struct udevice *sysc;
	struct clk_bulk clocks;
	struct reset_ctl_bulk resets;
};

static int mt7620_usb_phy_power_on(struct phy *_phy)
{
	struct mt7620_usb_phy *phy = dev_get_priv(_phy->dev);
	u32 mode = MT7620_SYSC_USB_HOST_MODE;
	int ret;

	reset_deassert_bulk(&phy->resets);

	clk_enable_bulk(&phy->clocks);

	mdelay(10);

	ret = misc_ioctl(phy->sysc, MT7620_SYSC_IOCTL_SET_USB_MODE, &mode);
	if (ret) {
		dev_err(_phy->dev,
			"mt7620_usbphy: failed to set USB host mode\n");
		return ret;
	}

	mdelay(10);

	return 0;
}

static int mt7620_usb_phy_power_off(struct phy *_phy)
{
	struct mt7620_usb_phy *phy = dev_get_priv(_phy->dev);

	clk_disable_bulk(&phy->clocks);

	reset_assert_bulk(&phy->resets);

	return 0;
}

static int mt7620_usb_phy_probe(struct udevice *dev)
{
	struct mt7620_usb_phy *phy = dev_get_priv(dev);
	struct ofnode_phandle_args sysc_args;
	int ret;

	ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "mediatek,sysc", NULL,
					     0, 0, &sysc_args);
	if (ret) {
		dev_err(dev, "mt7620_usbphy: sysc property not found\n");
		return ret;
	}

	ret = uclass_get_device_by_ofnode(UCLASS_MISC, sysc_args.node,
					  &phy->sysc);
	if (ret) {
		dev_err(dev, "mt7620_usbphy: failed to sysc device\n");
		return ret;
	}

	ret = clk_get_bulk(dev, &phy->clocks);
	if (ret) {
		dev_err(dev, "mt7620_usbphy: failed to get clocks\n");
		return ret;
	}

	ret = reset_get_bulk(dev, &phy->resets);
	if (ret) {
		dev_err(dev, "mt7620_usbphy: failed to get reset control\n");
		return ret;
	}

	return 0;
}

static struct phy_ops mt7620_usb_phy_ops = {
	.power_on = mt7620_usb_phy_power_on,
	.power_off = mt7620_usb_phy_power_off,
};

static const struct udevice_id mt7620_usb_phy_ids[] = {
	{ .compatible = "mediatek,mt7620-usbphy" },
	{ }
};

U_BOOT_DRIVER(mt7620_usb_phy) = {
	.name		= "mt7620_usb_phy",
	.id		= UCLASS_PHY,
	.of_match	= mt7620_usb_phy_ids,
	.ops		= &mt7620_usb_phy_ops,
	.probe		= mt7620_usb_phy_probe,
	.priv_auto	= sizeof(struct mt7620_usb_phy),
};