summaryrefstree
path: root/kernel/drivers/usb/phy/phy-samsung-usb2-dummy.c
blob: 6dd83444d21ae20b9f50dc3213c0982f151c8731 (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
153
154
155
/* linux/drivers/usb/phy/phy-samsung-usb2-dummy.c
 *
 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
 *              http://www.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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/usb/samsung_usb_phy.h>

#include <mach/exynos-pm.h>

#include "phy-samsung-usb.h"

static bool samsung_usb2phy_dummy_is_active(struct usb_phy *phy)
{
	struct samsung_usbphy *sphy = phy_to_sphy(phy);
	unsigned long flags;
	bool ret;

	spin_lock_irqsave(&sphy->lock, flags);

	/* Dummy phy is used by DWC3 for OTG */
	if (phy->otg && (phy->state == OTG_STATE_B_PERIPHERAL ||
			 phy->state == OTG_STATE_A_HOST))
		ret = true;
	else
		ret = false;

	spin_unlock_irqrestore(&sphy->lock, flags);

	return ret;
}

static int
samsung_usb2phy_dummy_lpa_event(struct notifier_block *nb,
				unsigned long event,
				void *data)
{
	struct samsung_usbphy *sphy = container_of(nb,
					struct samsung_usbphy, lpa_nb);
	int err = 0;

	switch (event) {
	case LPA_PREPARE:
	case LPC_PREPARE:
		if (samsung_usb2phy_dummy_is_active(&sphy->phy))
			err = -EBUSY;
		break;
	default:
		return NOTIFY_DONE;
	}

	return notifier_from_errno(err);
}

static int samsung_usb2phy_dummy_probe(struct platform_device *pdev)
{
	struct samsung_usbphy *sphy;
	const struct samsung_usbphy_drvdata *drv_data;
	struct device *dev = &pdev->dev;
	int ret;

	sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL);
	if (!sphy)
		return -ENOMEM;

	drv_data = samsung_usbphy_get_driver_data(pdev);

	sphy->dev = dev;

	sphy->drv_data		= drv_data;
	sphy->phy.dev		= sphy->dev;
	sphy->phy.label		= "samsung-usb2phy-dummy";
	sphy->phy.type		= USB_PHY_TYPE_USB2;
	sphy->phy.is_active	= samsung_usb2phy_dummy_is_active;

	spin_lock_init(&sphy->lock);

	platform_set_drvdata(pdev, sphy);

	ret = usb_add_phy_dev(&sphy->phy);
	if (ret) {
		dev_err(dev, "Failed to add PHY\n");
		return ret;
	}

	sphy->lpa_nb.notifier_call = samsung_usb2phy_dummy_lpa_event;
	sphy->lpa_nb.next = NULL;
	sphy->lpa_nb.priority = 0;

	ret = exynos_pm_register_notifier(&sphy->lpa_nb);
	if (ret)
		dev_err(dev, "Failed to register lpa notifier\n");

	return 0;
}

static int samsung_usb2phy_dummy_remove(struct platform_device *pdev)
{
	struct samsung_usbphy *sphy = platform_get_drvdata(pdev);

	exynos_pm_unregister_notifier(&sphy->lpa_nb);
	usb_remove_phy(&sphy->phy);

	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id samsung_usbphy_dt_match[] = {
	{
		.compatible = "samsung,exynos5-usb2phy-dummy",
	},
	{},
};
MODULE_DEVICE_TABLE(of, samsung_usbphy_dt_match);
#endif

static struct platform_device_id samsung_usbphy_dummy_driver_ids[] = {
	{
		.name		= "exynos5-usb2phy-dmy",
	},
	{},
};

MODULE_DEVICE_TABLE(platform, samsung_usbphy_driver_ids);

static struct platform_driver samsung_usb2phy_dummy_driver = {
	.probe		= samsung_usb2phy_dummy_probe,
	.remove		= samsung_usb2phy_dummy_remove,
	.id_table	= samsung_usbphy_dummy_driver_ids,
	.driver		= {
		.name	= "samsung-usb2phy-dummy",
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(samsung_usbphy_dt_match),
	},
};

module_platform_driver(samsung_usb2phy_dummy_driver);

MODULE_DESCRIPTION("Samsung USB 2.0 phy dummy controller");
MODULE_AUTHOR("Minho Lee <minho55.lee@samsung.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:samsung-usb2phy-dummy");