0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/clk.h>
0009 #include <linux/err.h>
0010 #include <linux/mfd/syscon.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/phy/phy.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regmap.h>
0016
0017
0018 #define LPC18XX_CREG_CREG0 0x004
0019 #define LPC18XX_CREG_CREG0_USB0PHY BIT(5)
0020
0021 struct lpc18xx_usb_otg_phy {
0022 struct phy *phy;
0023 struct clk *clk;
0024 struct regmap *reg;
0025 };
0026
0027 static int lpc18xx_usb_otg_phy_init(struct phy *phy)
0028 {
0029 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
0030 int ret;
0031
0032
0033 ret = clk_set_rate(lpc->clk, 480000000);
0034 if (ret)
0035 return ret;
0036
0037 return clk_prepare(lpc->clk);
0038 }
0039
0040 static int lpc18xx_usb_otg_phy_exit(struct phy *phy)
0041 {
0042 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
0043
0044 clk_unprepare(lpc->clk);
0045
0046 return 0;
0047 }
0048
0049 static int lpc18xx_usb_otg_phy_power_on(struct phy *phy)
0050 {
0051 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
0052 int ret;
0053
0054 ret = clk_enable(lpc->clk);
0055 if (ret)
0056 return ret;
0057
0058
0059 ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
0060 LPC18XX_CREG_CREG0_USB0PHY, 0);
0061 if (ret) {
0062 clk_disable(lpc->clk);
0063 return ret;
0064 }
0065
0066 return 0;
0067 }
0068
0069 static int lpc18xx_usb_otg_phy_power_off(struct phy *phy)
0070 {
0071 struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
0072 int ret;
0073
0074 ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
0075 LPC18XX_CREG_CREG0_USB0PHY,
0076 LPC18XX_CREG_CREG0_USB0PHY);
0077 if (ret)
0078 return ret;
0079
0080 clk_disable(lpc->clk);
0081
0082 return 0;
0083 }
0084
0085 static const struct phy_ops lpc18xx_usb_otg_phy_ops = {
0086 .init = lpc18xx_usb_otg_phy_init,
0087 .exit = lpc18xx_usb_otg_phy_exit,
0088 .power_on = lpc18xx_usb_otg_phy_power_on,
0089 .power_off = lpc18xx_usb_otg_phy_power_off,
0090 .owner = THIS_MODULE,
0091 };
0092
0093 static int lpc18xx_usb_otg_phy_probe(struct platform_device *pdev)
0094 {
0095 struct phy_provider *phy_provider;
0096 struct lpc18xx_usb_otg_phy *lpc;
0097
0098 lpc = devm_kzalloc(&pdev->dev, sizeof(*lpc), GFP_KERNEL);
0099 if (!lpc)
0100 return -ENOMEM;
0101
0102 lpc->reg = syscon_node_to_regmap(pdev->dev.of_node->parent);
0103 if (IS_ERR(lpc->reg)) {
0104 dev_err(&pdev->dev, "failed to get syscon\n");
0105 return PTR_ERR(lpc->reg);
0106 }
0107
0108 lpc->clk = devm_clk_get(&pdev->dev, NULL);
0109 if (IS_ERR(lpc->clk)) {
0110 dev_err(&pdev->dev, "failed to get clock\n");
0111 return PTR_ERR(lpc->clk);
0112 }
0113
0114 lpc->phy = devm_phy_create(&pdev->dev, NULL, &lpc18xx_usb_otg_phy_ops);
0115 if (IS_ERR(lpc->phy)) {
0116 dev_err(&pdev->dev, "failed to create PHY\n");
0117 return PTR_ERR(lpc->phy);
0118 }
0119
0120 phy_set_drvdata(lpc->phy, lpc);
0121
0122 phy_provider = devm_of_phy_provider_register(&pdev->dev,
0123 of_phy_simple_xlate);
0124
0125 return PTR_ERR_OR_ZERO(phy_provider);
0126 }
0127
0128 static const struct of_device_id lpc18xx_usb_otg_phy_match[] = {
0129 { .compatible = "nxp,lpc1850-usb-otg-phy" },
0130 { }
0131 };
0132 MODULE_DEVICE_TABLE(of, lpc18xx_usb_otg_phy_match);
0133
0134 static struct platform_driver lpc18xx_usb_otg_phy_driver = {
0135 .probe = lpc18xx_usb_otg_phy_probe,
0136 .driver = {
0137 .name = "lpc18xx-usb-otg-phy",
0138 .of_match_table = lpc18xx_usb_otg_phy_match,
0139 },
0140 };
0141 module_platform_driver(lpc18xx_usb_otg_phy_driver);
0142
0143 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
0144 MODULE_DESCRIPTION("NXP LPC18xx/43xx USB OTG PHY driver");
0145 MODULE_LICENSE("GPL v2");