Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * phy-uniphier-usb2.c - PHY driver for UniPhier USB2 controller
0004  * Copyright 2015-2018 Socionext Inc.
0005  * Author:
0006  *      Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
0007  */
0008 
0009 #include <linux/mfd/syscon.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/of_platform.h>
0013 #include <linux/phy/phy.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regmap.h>
0016 #include <linux/regulator/consumer.h>
0017 
0018 #define SG_USBPHY1CTRL      0x500
0019 #define SG_USBPHY1CTRL2     0x504
0020 #define SG_USBPHY2CTRL      0x508
0021 #define SG_USBPHY2CTRL2     0x50c   /* LD11 */
0022 #define SG_USBPHY12PLL      0x50c   /* Pro4 */
0023 #define SG_USBPHY3CTRL      0x510
0024 #define SG_USBPHY3CTRL2     0x514
0025 #define SG_USBPHY4CTRL      0x518   /* Pro4 */
0026 #define SG_USBPHY4CTRL2     0x51c   /* Pro4 */
0027 #define SG_USBPHY34PLL      0x51c   /* Pro4 */
0028 
0029 struct uniphier_u2phy_param {
0030     u32 offset;
0031     u32 value;
0032 };
0033 
0034 struct uniphier_u2phy_soc_data {
0035     struct uniphier_u2phy_param config0;
0036     struct uniphier_u2phy_param config1;
0037 };
0038 
0039 struct uniphier_u2phy_priv {
0040     struct regmap *regmap;
0041     struct phy *phy;
0042     struct regulator *vbus;
0043     const struct uniphier_u2phy_soc_data *data;
0044     struct uniphier_u2phy_priv *next;
0045 };
0046 
0047 static int uniphier_u2phy_power_on(struct phy *phy)
0048 {
0049     struct uniphier_u2phy_priv *priv = phy_get_drvdata(phy);
0050     int ret = 0;
0051 
0052     if (priv->vbus)
0053         ret = regulator_enable(priv->vbus);
0054 
0055     return ret;
0056 }
0057 
0058 static int uniphier_u2phy_power_off(struct phy *phy)
0059 {
0060     struct uniphier_u2phy_priv *priv = phy_get_drvdata(phy);
0061 
0062     if (priv->vbus)
0063         regulator_disable(priv->vbus);
0064 
0065     return 0;
0066 }
0067 
0068 static int uniphier_u2phy_init(struct phy *phy)
0069 {
0070     struct uniphier_u2phy_priv *priv = phy_get_drvdata(phy);
0071 
0072     if (!priv->data)
0073         return 0;
0074 
0075     regmap_write(priv->regmap, priv->data->config0.offset,
0076              priv->data->config0.value);
0077     regmap_write(priv->regmap, priv->data->config1.offset,
0078              priv->data->config1.value);
0079 
0080     return 0;
0081 }
0082 
0083 static struct phy *uniphier_u2phy_xlate(struct device *dev,
0084                     struct of_phandle_args *args)
0085 {
0086     struct uniphier_u2phy_priv *priv = dev_get_drvdata(dev);
0087 
0088     while (priv && args->np != priv->phy->dev.of_node)
0089         priv = priv->next;
0090 
0091     if (!priv) {
0092         dev_err(dev, "Failed to find appropriate phy\n");
0093         return ERR_PTR(-EINVAL);
0094     }
0095 
0096     return priv->phy;
0097 }
0098 
0099 static const struct phy_ops uniphier_u2phy_ops = {
0100     .init      = uniphier_u2phy_init,
0101     .power_on  = uniphier_u2phy_power_on,
0102     .power_off = uniphier_u2phy_power_off,
0103     .owner = THIS_MODULE,
0104 };
0105 
0106 static int uniphier_u2phy_probe(struct platform_device *pdev)
0107 {
0108     struct device *dev = &pdev->dev;
0109     struct device_node *parent, *child;
0110     struct uniphier_u2phy_priv *priv = NULL, *next = NULL;
0111     struct phy_provider *phy_provider;
0112     struct regmap *regmap;
0113     const struct uniphier_u2phy_soc_data *data;
0114     int ret, data_idx, ndatas;
0115 
0116     data = of_device_get_match_data(dev);
0117     if (WARN_ON(!data))
0118         return -EINVAL;
0119 
0120     /* get number of data */
0121     for (ndatas = 0; data[ndatas].config0.offset; ndatas++)
0122         ;
0123 
0124     parent = of_get_parent(dev->of_node);
0125     regmap = syscon_node_to_regmap(parent);
0126     of_node_put(parent);
0127     if (IS_ERR(regmap)) {
0128         dev_err(dev, "Failed to get regmap\n");
0129         return PTR_ERR(regmap);
0130     }
0131 
0132     for_each_child_of_node(dev->of_node, child) {
0133         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0134         if (!priv) {
0135             ret = -ENOMEM;
0136             goto out_put_child;
0137         }
0138         priv->regmap = regmap;
0139 
0140         priv->vbus = devm_regulator_get_optional(dev, "vbus");
0141         if (IS_ERR(priv->vbus)) {
0142             if (PTR_ERR(priv->vbus) == -EPROBE_DEFER) {
0143                 ret = PTR_ERR(priv->vbus);
0144                 goto out_put_child;
0145             }
0146             priv->vbus = NULL;
0147         }
0148 
0149         priv->phy = devm_phy_create(dev, child, &uniphier_u2phy_ops);
0150         if (IS_ERR(priv->phy)) {
0151             dev_err(dev, "Failed to create phy\n");
0152             ret = PTR_ERR(priv->phy);
0153             goto out_put_child;
0154         }
0155 
0156         ret = of_property_read_u32(child, "reg", &data_idx);
0157         if (ret) {
0158             dev_err(dev, "Failed to get reg property\n");
0159             goto out_put_child;
0160         }
0161 
0162         if (data_idx < ndatas)
0163             priv->data = &data[data_idx];
0164         else
0165             dev_warn(dev, "No phy configuration: %s\n",
0166                  child->full_name);
0167 
0168         phy_set_drvdata(priv->phy, priv);
0169         priv->next = next;
0170         next = priv;
0171     }
0172 
0173     dev_set_drvdata(dev, priv);
0174     phy_provider = devm_of_phy_provider_register(dev,
0175                              uniphier_u2phy_xlate);
0176     return PTR_ERR_OR_ZERO(phy_provider);
0177 
0178 out_put_child:
0179     of_node_put(child);
0180 
0181     return ret;
0182 }
0183 
0184 static const struct uniphier_u2phy_soc_data uniphier_pro4_data[] = {
0185     {
0186         .config0 = { SG_USBPHY1CTRL, 0x05142400 },
0187         .config1 = { SG_USBPHY12PLL, 0x00010010 },
0188     },
0189     {
0190         .config0 = { SG_USBPHY2CTRL, 0x05142400 },
0191         .config1 = { SG_USBPHY12PLL, 0x00010010 },
0192     },
0193     {
0194         .config0 = { SG_USBPHY3CTRL, 0x05142400 },
0195         .config1 = { SG_USBPHY34PLL, 0x00010010 },
0196     },
0197     {
0198         .config0 = { SG_USBPHY4CTRL, 0x05142400 },
0199         .config1 = { SG_USBPHY34PLL, 0x00010010 },
0200     },
0201     { /* sentinel */ }
0202 };
0203 
0204 static const struct uniphier_u2phy_soc_data uniphier_ld11_data[] = {
0205     {
0206         .config0 = { SG_USBPHY1CTRL,  0x82280000 },
0207         .config1 = { SG_USBPHY1CTRL2, 0x00000106 },
0208     },
0209     {
0210         .config0 = { SG_USBPHY2CTRL,  0x82280000 },
0211         .config1 = { SG_USBPHY2CTRL2, 0x00000106 },
0212     },
0213     {
0214         .config0 = { SG_USBPHY3CTRL,  0x82280000 },
0215         .config1 = { SG_USBPHY3CTRL2, 0x00000106 },
0216     },
0217     { /* sentinel */ }
0218 };
0219 
0220 static const struct of_device_id uniphier_u2phy_match[] = {
0221     {
0222         .compatible = "socionext,uniphier-pro4-usb2-phy",
0223         .data = &uniphier_pro4_data,
0224     },
0225     {
0226         .compatible = "socionext,uniphier-ld11-usb2-phy",
0227         .data = &uniphier_ld11_data,
0228     },
0229     { /* sentinel */ }
0230 };
0231 MODULE_DEVICE_TABLE(of, uniphier_u2phy_match);
0232 
0233 static struct platform_driver uniphier_u2phy_driver = {
0234     .probe = uniphier_u2phy_probe,
0235     .driver = {
0236         .name = "uniphier-usb2-phy",
0237         .of_match_table = uniphier_u2phy_match,
0238     },
0239 };
0240 module_platform_driver(uniphier_u2phy_driver);
0241 
0242 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
0243 MODULE_DESCRIPTION("UniPhier PHY driver for USB2 controller");
0244 MODULE_LICENSE("GPL v2");