xref: /linux/drivers/phy/ti/phy-da8xx-usb.c (revision 18f90d372cf35b387663f1567de701e5393f6eb5)
1 /*
2  * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
3  *
4  * Copyright (C) 2016 David Lechner <david@lechnology.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/clk.h>
17 #include <linux/io.h>
18 #include <linux/of.h>
19 #include <linux/mfd/da8xx-cfgchip.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/module.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_data/phy-da8xx-usb.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 
27 #define PHY_INIT_BITS	(CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
28 
29 struct da8xx_usb_phy {
30 	struct phy_provider	*phy_provider;
31 	struct phy		*usb11_phy;
32 	struct phy		*usb20_phy;
33 	struct clk		*usb11_clk;
34 	struct clk		*usb20_clk;
35 	struct regmap		*regmap;
36 };
37 
38 static int da8xx_usb11_phy_power_on(struct phy *phy)
39 {
40 	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
41 	int ret;
42 
43 	ret = clk_prepare_enable(d_phy->usb11_clk);
44 	if (ret)
45 		return ret;
46 
47 	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
48 			  CFGCHIP2_USB1SUSPENDM);
49 
50 	return 0;
51 }
52 
53 static int da8xx_usb11_phy_power_off(struct phy *phy)
54 {
55 	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
56 
57 	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
58 
59 	clk_disable_unprepare(d_phy->usb11_clk);
60 
61 	return 0;
62 }
63 
64 static const struct phy_ops da8xx_usb11_phy_ops = {
65 	.power_on	= da8xx_usb11_phy_power_on,
66 	.power_off	= da8xx_usb11_phy_power_off,
67 	.owner		= THIS_MODULE,
68 };
69 
70 static int da8xx_usb20_phy_power_on(struct phy *phy)
71 {
72 	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
73 	int ret;
74 
75 	ret = clk_prepare_enable(d_phy->usb20_clk);
76 	if (ret)
77 		return ret;
78 
79 	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
80 
81 	return 0;
82 }
83 
84 static int da8xx_usb20_phy_power_off(struct phy *phy)
85 {
86 	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
87 
88 	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
89 			  CFGCHIP2_OTGPWRDN);
90 
91 	clk_disable_unprepare(d_phy->usb20_clk);
92 
93 	return 0;
94 }
95 
96 static int da8xx_usb20_phy_set_mode(struct phy *phy,
97 				    enum phy_mode mode, int submode)
98 {
99 	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
100 	u32 val;
101 
102 	switch (mode) {
103 	case PHY_MODE_USB_HOST:		/* Force VBUS valid, ID = 0 */
104 		val = CFGCHIP2_OTGMODE_FORCE_HOST;
105 		break;
106 	case PHY_MODE_USB_DEVICE:	/* Force VBUS valid, ID = 1 */
107 		val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
108 		break;
109 	case PHY_MODE_USB_OTG:	/* Don't override the VBUS/ID comparators */
110 		val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
111 		break;
112 	default:
113 		return -EINVAL;
114 	}
115 
116 	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
117 			  val);
118 
119 	return 0;
120 }
121 
122 static const struct phy_ops da8xx_usb20_phy_ops = {
123 	.power_on	= da8xx_usb20_phy_power_on,
124 	.power_off	= da8xx_usb20_phy_power_off,
125 	.set_mode	= da8xx_usb20_phy_set_mode,
126 	.owner		= THIS_MODULE,
127 };
128 
129 static struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
130 					 struct of_phandle_args *args)
131 {
132 	struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
133 
134 	if (!d_phy)
135 		return ERR_PTR(-ENODEV);
136 
137 	switch (args->args[0]) {
138 	case 0:
139 		return d_phy->usb20_phy;
140 	case 1:
141 		return d_phy->usb11_phy;
142 	default:
143 		return ERR_PTR(-EINVAL);
144 	}
145 }
146 
147 static int da8xx_usb_phy_probe(struct platform_device *pdev)
148 {
149 	struct device		*dev = &pdev->dev;
150 	struct da8xx_usb_phy_platform_data *pdata = dev->platform_data;
151 	struct device_node	*node = dev->of_node;
152 	struct da8xx_usb_phy	*d_phy;
153 
154 	d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
155 	if (!d_phy)
156 		return -ENOMEM;
157 
158 	if (pdata)
159 		d_phy->regmap = pdata->cfgchip;
160 	else
161 		d_phy->regmap = syscon_regmap_lookup_by_compatible(
162 							"ti,da830-cfgchip");
163 	if (IS_ERR(d_phy->regmap)) {
164 		dev_err(dev, "Failed to get syscon\n");
165 		return PTR_ERR(d_phy->regmap);
166 	}
167 
168 	d_phy->usb11_clk = devm_clk_get(dev, "usb1_clk48");
169 	if (IS_ERR(d_phy->usb11_clk)) {
170 		dev_err(dev, "Failed to get usb1_clk48\n");
171 		return PTR_ERR(d_phy->usb11_clk);
172 	}
173 
174 	d_phy->usb20_clk = devm_clk_get(dev, "usb0_clk48");
175 	if (IS_ERR(d_phy->usb20_clk)) {
176 		dev_err(dev, "Failed to get usb0_clk48\n");
177 		return PTR_ERR(d_phy->usb20_clk);
178 	}
179 
180 	d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops);
181 	if (IS_ERR(d_phy->usb11_phy)) {
182 		dev_err(dev, "Failed to create usb11 phy\n");
183 		return PTR_ERR(d_phy->usb11_phy);
184 	}
185 
186 	d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops);
187 	if (IS_ERR(d_phy->usb20_phy)) {
188 		dev_err(dev, "Failed to create usb20 phy\n");
189 		return PTR_ERR(d_phy->usb20_phy);
190 	}
191 
192 	platform_set_drvdata(pdev, d_phy);
193 	phy_set_drvdata(d_phy->usb11_phy, d_phy);
194 	phy_set_drvdata(d_phy->usb20_phy, d_phy);
195 
196 	if (node) {
197 		d_phy->phy_provider = devm_of_phy_provider_register(dev,
198 							da8xx_usb_phy_of_xlate);
199 		if (IS_ERR(d_phy->phy_provider)) {
200 			dev_err(dev, "Failed to create phy provider\n");
201 			return PTR_ERR(d_phy->phy_provider);
202 		}
203 	} else {
204 		int ret;
205 
206 		ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
207 					"ohci-da8xx");
208 		if (ret)
209 			dev_warn(dev, "Failed to create usb11 phy lookup\n");
210 		ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
211 					"musb-da8xx");
212 		if (ret)
213 			dev_warn(dev, "Failed to create usb20 phy lookup\n");
214 	}
215 
216 	regmap_write_bits(d_phy->regmap, CFGCHIP(2),
217 			  PHY_INIT_BITS, PHY_INIT_BITS);
218 
219 	return 0;
220 }
221 
222 static int da8xx_usb_phy_remove(struct platform_device *pdev)
223 {
224 	struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
225 
226 	if (!pdev->dev.of_node) {
227 		phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx");
228 		phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci-da8xx");
229 	}
230 
231 	return 0;
232 }
233 
234 static const struct of_device_id da8xx_usb_phy_ids[] = {
235 	{ .compatible = "ti,da830-usb-phy" },
236 	{ }
237 };
238 MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
239 
240 static struct platform_driver da8xx_usb_phy_driver = {
241 	.probe	= da8xx_usb_phy_probe,
242 	.remove	= da8xx_usb_phy_remove,
243 	.driver	= {
244 		.name	= "da8xx-usb-phy",
245 		.of_match_table = da8xx_usb_phy_ids,
246 	},
247 };
248 
249 module_platform_driver(da8xx_usb_phy_driver);
250 
251 MODULE_ALIAS("platform:da8xx-usb-phy");
252 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
253 MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
254 MODULE_LICENSE("GPL v2");
255