2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
7 * kind, whether express or implied; without even the implied warranty
8 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
16 #include <linux/slab.h>
19 #include <linux/usb/phy_companion.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/delay.h>
24 #include <linux/phy/phy.h>
25 #include <linux/of_platform.h>
27 #include <linux/mfd/syscon.h>
30 * TRM has two sets of USB_CTRL registers.. The correct register bits
31 * are in TRM section 24.9.8.2 USB_CTRL Register. The TRM documents the
32 * phy as being SR70LX Synopsys USB 2.0 OTG nanoPHY. It also seems at
33 * least dm816x rev c ignores writes to USB_CTRL register, but the TI
34 * kernel is writing to those so it's possible that later revisions
35 * have worknig USB_CTRL register.
37 * Also note that At least USB_CTRL register seems to be dm816x specific
38 * according to the TRM. It's possible that USBPHY_CTRL is more generic,
39 * but that would have to be checked against the SR70LX documentation
40 * which does not seem to be publicly available.
42 * Finally, the phy on dm814x and am335x is different from dm816x.
44 #define DM816X_USB_CTRL_PHYCLKSRC BIT(8) /* 1 = PLL ref clock */
45 #define DM816X_USB_CTRL_PHYSLEEP1 BIT(1) /* Enable the first phy */
46 #define DM816X_USB_CTRL_PHYSLEEP0 BIT(0) /* Enable the second phy */
48 #define DM816X_USBPHY_CTRL_TXRISETUNE 1
49 #define DM816X_USBPHY_CTRL_TXVREFTUNE 0xc
50 #define DM816X_USBPHY_CTRL_TXPREEMTUNE 0x2
52 struct dm816x_usb_phy
{
53 struct regmap
*syscon
;
55 unsigned int instance
;
58 unsigned int usb_ctrl
; /* Shared between phy0 and phy1 */
59 unsigned int usbphy_ctrl
;
62 static int dm816x_usb_phy_set_host(struct usb_otg
*otg
, struct usb_bus
*host
)
66 otg
->state
= OTG_STATE_UNDEFINED
;
71 static int dm816x_usb_phy_set_peripheral(struct usb_otg
*otg
,
72 struct usb_gadget
*gadget
)
76 otg
->state
= OTG_STATE_UNDEFINED
;
81 static int dm816x_usb_phy_init(struct phy
*x
)
83 struct dm816x_usb_phy
*phy
= phy_get_drvdata(x
);
86 if (clk_get_rate(phy
->refclk
) != 24000000)
87 dev_warn(phy
->dev
, "nonstandard phy refclk\n");
89 /* Set PLL ref clock and put phys to sleep */
90 regmap_update_bits(phy
->syscon
, phy
->usb_ctrl
,
91 DM816X_USB_CTRL_PHYCLKSRC
|
92 DM816X_USB_CTRL_PHYSLEEP1
|
93 DM816X_USB_CTRL_PHYSLEEP0
,
95 regmap_read(phy
->syscon
, phy
->usb_ctrl
, &val
);
98 "Working dm816x USB_CTRL! (0x%08x)\n",
102 * TI kernel sets these values for "symmetrical eye diagram and
103 * better signal quality" so let's assume somebody checked the
104 * values with a scope and set them here too.
106 regmap_read(phy
->syscon
, phy
->usbphy_ctrl
, &val
);
107 val
|= DM816X_USBPHY_CTRL_TXRISETUNE
|
108 DM816X_USBPHY_CTRL_TXVREFTUNE
|
109 DM816X_USBPHY_CTRL_TXPREEMTUNE
;
110 regmap_write(phy
->syscon
, phy
->usbphy_ctrl
, val
);
115 static const struct phy_ops ops
= {
116 .init
= dm816x_usb_phy_init
,
117 .owner
= THIS_MODULE
,
120 static int __maybe_unused
dm816x_usb_phy_runtime_suspend(struct device
*dev
)
122 struct dm816x_usb_phy
*phy
= dev_get_drvdata(dev
);
123 unsigned int mask
, val
;
126 mask
= BIT(phy
->instance
);
127 val
= ~BIT(phy
->instance
);
128 error
= regmap_update_bits(phy
->syscon
, phy
->usb_ctrl
,
131 dev_err(phy
->dev
, "phy%i failed to power off\n",
133 clk_disable(phy
->refclk
);
138 static int __maybe_unused
dm816x_usb_phy_runtime_resume(struct device
*dev
)
140 struct dm816x_usb_phy
*phy
= dev_get_drvdata(dev
);
141 unsigned int mask
, val
;
144 error
= clk_enable(phy
->refclk
);
149 * Note that at least dm816x rev c does not seem to do
150 * anything with the USB_CTRL register. But let's follow
151 * what the TI tree is doing in case later revisions use
154 mask
= BIT(phy
->instance
);
155 val
= BIT(phy
->instance
);
156 error
= regmap_update_bits(phy
->syscon
, phy
->usb_ctrl
,
159 dev_err(phy
->dev
, "phy%i failed to power on\n",
161 clk_disable(phy
->refclk
);
168 static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops
,
169 dm816x_usb_phy_runtime_suspend
,
170 dm816x_usb_phy_runtime_resume
,
174 static const struct of_device_id dm816x_usb_phy_id_table
[] = {
176 .compatible
= "ti,dm8168-usb-phy",
180 MODULE_DEVICE_TABLE(of
, dm816x_usb_phy_id_table
);
183 static int dm816x_usb_phy_probe(struct platform_device
*pdev
)
185 struct dm816x_usb_phy
*phy
;
186 struct resource
*res
;
187 struct phy
*generic_phy
;
188 struct phy_provider
*phy_provider
;
190 const struct of_device_id
*of_id
;
193 of_id
= of_match_device(of_match_ptr(dm816x_usb_phy_id_table
),
198 phy
= devm_kzalloc(&pdev
->dev
, sizeof(*phy
), GFP_KERNEL
);
202 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
206 phy
->syscon
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
208 if (IS_ERR(phy
->syscon
))
209 return PTR_ERR(phy
->syscon
);
212 * According to sprs614e.pdf, the first usb_ctrl is shared and
213 * the second instance for usb_ctrl is reserved.. Also the
214 * register bits are different from earlier TRMs.
216 phy
->usb_ctrl
= 0x20;
217 phy
->usbphy_ctrl
= (res
->start
& 0xff) + 4;
218 if (phy
->usbphy_ctrl
== 0x2c)
221 otg
= devm_kzalloc(&pdev
->dev
, sizeof(*otg
), GFP_KERNEL
);
225 phy
->dev
= &pdev
->dev
;
226 phy
->phy
.dev
= phy
->dev
;
227 phy
->phy
.label
= "dm8168_usb_phy";
229 phy
->phy
.type
= USB_PHY_TYPE_USB2
;
230 otg
->set_host
= dm816x_usb_phy_set_host
;
231 otg
->set_peripheral
= dm816x_usb_phy_set_peripheral
;
232 otg
->usb_phy
= &phy
->phy
;
234 platform_set_drvdata(pdev
, phy
);
236 phy
->refclk
= devm_clk_get(phy
->dev
, "refclk");
237 if (IS_ERR(phy
->refclk
))
238 return PTR_ERR(phy
->refclk
);
239 error
= clk_prepare(phy
->refclk
);
243 pm_runtime_enable(phy
->dev
);
244 generic_phy
= devm_phy_create(phy
->dev
, NULL
, &ops
);
245 if (IS_ERR(generic_phy
))
246 return PTR_ERR(generic_phy
);
248 phy_set_drvdata(generic_phy
, phy
);
250 phy_provider
= devm_of_phy_provider_register(phy
->dev
,
251 of_phy_simple_xlate
);
252 if (IS_ERR(phy_provider
))
253 return PTR_ERR(phy_provider
);
255 usb_add_phy_dev(&phy
->phy
);
260 static int dm816x_usb_phy_remove(struct platform_device
*pdev
)
262 struct dm816x_usb_phy
*phy
= platform_get_drvdata(pdev
);
264 usb_remove_phy(&phy
->phy
);
265 pm_runtime_disable(phy
->dev
);
266 clk_unprepare(phy
->refclk
);
271 static struct platform_driver dm816x_usb_phy_driver
= {
272 .probe
= dm816x_usb_phy_probe
,
273 .remove
= dm816x_usb_phy_remove
,
275 .name
= "dm816x-usb-phy",
276 .pm
= &dm816x_usb_phy_pm_ops
,
277 .of_match_table
= of_match_ptr(dm816x_usb_phy_id_table
),
281 module_platform_driver(dm816x_usb_phy_driver
);
283 MODULE_ALIAS("platform:dm816x_usb");
284 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
285 MODULE_DESCRIPTION("dm816x usb phy driver");
286 MODULE_LICENSE("GPL v2");