2 * PHY driver for NXP LPC18xx/43xx internal USB OTG PHY
4 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
21 /* USB OTG PHY register offset and bit in CREG */
22 #define LPC18XX_CREG_CREG0 0x004
23 #define LPC18XX_CREG_CREG0_USB0PHY BIT(5)
25 struct lpc18xx_usb_otg_phy {
31 static int lpc18xx_usb_otg_phy_init(struct phy *phy)
33 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
36 /* The PHY must be clocked at 480 MHz */
37 ret = clk_set_rate(lpc->clk, 480000000);
41 return clk_prepare(lpc->clk);
44 static int lpc18xx_usb_otg_phy_exit(struct phy *phy)
46 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
48 clk_unprepare(lpc->clk);
53 static int lpc18xx_usb_otg_phy_power_on(struct phy *phy)
55 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
58 ret = clk_enable(lpc->clk);
62 /* The bit in CREG is cleared to enable the PHY */
63 return regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
64 LPC18XX_CREG_CREG0_USB0PHY, 0);
67 static int lpc18xx_usb_otg_phy_power_off(struct phy *phy)
69 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
72 ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
73 LPC18XX_CREG_CREG0_USB0PHY,
74 LPC18XX_CREG_CREG0_USB0PHY);
78 clk_disable(lpc->clk);
83 static const struct phy_ops lpc18xx_usb_otg_phy_ops = {
84 .init = lpc18xx_usb_otg_phy_init,
85 .exit = lpc18xx_usb_otg_phy_exit,
86 .power_on = lpc18xx_usb_otg_phy_power_on,
87 .power_off = lpc18xx_usb_otg_phy_power_off,
91 static int lpc18xx_usb_otg_phy_probe(struct platform_device *pdev)
93 struct phy_provider *phy_provider;
94 struct lpc18xx_usb_otg_phy *lpc;
96 lpc = devm_kzalloc(&pdev->dev, sizeof(*lpc), GFP_KERNEL);
100 lpc->reg = syscon_node_to_regmap(pdev->dev.of_node->parent);
101 if (IS_ERR(lpc->reg)) {
102 dev_err(&pdev->dev, "failed to get syscon\n");
103 return PTR_ERR(lpc->reg);
106 lpc->clk = devm_clk_get(&pdev->dev, NULL);
107 if (IS_ERR(lpc->clk)) {
108 dev_err(&pdev->dev, "failed to get clock\n");
109 return PTR_ERR(lpc->clk);
112 lpc->phy = devm_phy_create(&pdev->dev, NULL, &lpc18xx_usb_otg_phy_ops);
113 if (IS_ERR(lpc->phy)) {
114 dev_err(&pdev->dev, "failed to create PHY\n");
115 return PTR_ERR(lpc->phy);
118 phy_set_drvdata(lpc->phy, lpc);
120 phy_provider = devm_of_phy_provider_register(&pdev->dev,
121 of_phy_simple_xlate);
123 return PTR_ERR_OR_ZERO(phy_provider);
126 static const struct of_device_id lpc18xx_usb_otg_phy_match[] = {
127 { .compatible = "nxp,lpc1850-usb-otg-phy" },
130 MODULE_DEVICE_TABLE(of, lpc18xx_usb_otg_phy_match);
132 static struct platform_driver lpc18xx_usb_otg_phy_driver = {
133 .probe = lpc18xx_usb_otg_phy_probe,
135 .name = "lpc18xx-usb-otg-phy",
136 .of_match_table = lpc18xx_usb_otg_phy_match,
139 module_platform_driver(lpc18xx_usb_otg_phy_driver);
141 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
142 MODULE_DESCRIPTION("NXP LPC18xx/43xx USB OTG PHY driver");
143 MODULE_LICENSE("GPL v2");