| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 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, enum phy_mode mode) | 
|  | 97 | { | 
|  | 98 | struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy); | 
|  | 99 | u32 val; | 
|  | 100 |  | 
|  | 101 | switch (mode) { | 
|  | 102 | case PHY_MODE_USB_HOST:		/* Force VBUS valid, ID = 0 */ | 
|  | 103 | val = CFGCHIP2_OTGMODE_FORCE_HOST; | 
|  | 104 | break; | 
|  | 105 | case PHY_MODE_USB_DEVICE:	/* Force VBUS valid, ID = 1 */ | 
|  | 106 | val = CFGCHIP2_OTGMODE_FORCE_DEVICE; | 
|  | 107 | break; | 
|  | 108 | case PHY_MODE_USB_OTG:	/* Don't override the VBUS/ID comparators */ | 
|  | 109 | val = CFGCHIP2_OTGMODE_NO_OVERRIDE; | 
|  | 110 | break; | 
|  | 111 | default: | 
|  | 112 | return -EINVAL; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK, | 
|  | 116 | val); | 
|  | 117 |  | 
|  | 118 | return 0; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | static const struct phy_ops da8xx_usb20_phy_ops = { | 
|  | 122 | .power_on	= da8xx_usb20_phy_power_on, | 
|  | 123 | .power_off	= da8xx_usb20_phy_power_off, | 
|  | 124 | .set_mode	= da8xx_usb20_phy_set_mode, | 
|  | 125 | .owner		= THIS_MODULE, | 
|  | 126 | }; | 
|  | 127 |  | 
|  | 128 | static struct phy *da8xx_usb_phy_of_xlate(struct device *dev, | 
|  | 129 | struct of_phandle_args *args) | 
|  | 130 | { | 
|  | 131 | struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev); | 
|  | 132 |  | 
|  | 133 | if (!d_phy) | 
|  | 134 | return ERR_PTR(-ENODEV); | 
|  | 135 |  | 
|  | 136 | switch (args->args[0]) { | 
|  | 137 | case 0: | 
|  | 138 | return d_phy->usb20_phy; | 
|  | 139 | case 1: | 
|  | 140 | return d_phy->usb11_phy; | 
|  | 141 | default: | 
|  | 142 | return ERR_PTR(-EINVAL); | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | static int da8xx_usb_phy_probe(struct platform_device *pdev) | 
|  | 147 | { | 
|  | 148 | struct device		*dev = &pdev->dev; | 
|  | 149 | struct da8xx_usb_phy_platform_data *pdata = dev->platform_data; | 
|  | 150 | struct device_node	*node = dev->of_node; | 
|  | 151 | struct da8xx_usb_phy	*d_phy; | 
|  | 152 |  | 
|  | 153 | d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL); | 
|  | 154 | if (!d_phy) | 
|  | 155 | return -ENOMEM; | 
|  | 156 |  | 
|  | 157 | if (pdata) | 
|  | 158 | d_phy->regmap = pdata->cfgchip; | 
|  | 159 | else | 
|  | 160 | d_phy->regmap = syscon_regmap_lookup_by_compatible( | 
|  | 161 | "ti,da830-cfgchip"); | 
|  | 162 | if (IS_ERR(d_phy->regmap)) { | 
|  | 163 | dev_err(dev, "Failed to get syscon\n"); | 
|  | 164 | return PTR_ERR(d_phy->regmap); | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | d_phy->usb11_clk = devm_clk_get(dev, "usb1_clk48"); | 
|  | 168 | if (IS_ERR(d_phy->usb11_clk)) { | 
|  | 169 | dev_err(dev, "Failed to get usb1_clk48\n"); | 
|  | 170 | return PTR_ERR(d_phy->usb11_clk); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | d_phy->usb20_clk = devm_clk_get(dev, "usb0_clk48"); | 
|  | 174 | if (IS_ERR(d_phy->usb20_clk)) { | 
|  | 175 | dev_err(dev, "Failed to get usb0_clk48\n"); | 
|  | 176 | return PTR_ERR(d_phy->usb20_clk); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops); | 
|  | 180 | if (IS_ERR(d_phy->usb11_phy)) { | 
|  | 181 | dev_err(dev, "Failed to create usb11 phy\n"); | 
|  | 182 | return PTR_ERR(d_phy->usb11_phy); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops); | 
|  | 186 | if (IS_ERR(d_phy->usb20_phy)) { | 
|  | 187 | dev_err(dev, "Failed to create usb20 phy\n"); | 
|  | 188 | return PTR_ERR(d_phy->usb20_phy); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | platform_set_drvdata(pdev, d_phy); | 
|  | 192 | phy_set_drvdata(d_phy->usb11_phy, d_phy); | 
|  | 193 | phy_set_drvdata(d_phy->usb20_phy, d_phy); | 
|  | 194 |  | 
|  | 195 | if (node) { | 
|  | 196 | d_phy->phy_provider = devm_of_phy_provider_register(dev, | 
|  | 197 | da8xx_usb_phy_of_xlate); | 
|  | 198 | if (IS_ERR(d_phy->phy_provider)) { | 
|  | 199 | dev_err(dev, "Failed to create phy provider\n"); | 
|  | 200 | return PTR_ERR(d_phy->phy_provider); | 
|  | 201 | } | 
|  | 202 | } else { | 
|  | 203 | int ret; | 
|  | 204 |  | 
|  | 205 | ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy", | 
|  | 206 | "ohci-da8xx"); | 
|  | 207 | if (ret) | 
|  | 208 | dev_warn(dev, "Failed to create usb11 phy lookup\n"); | 
|  | 209 | ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy", | 
|  | 210 | "musb-da8xx"); | 
|  | 211 | if (ret) | 
|  | 212 | dev_warn(dev, "Failed to create usb20 phy lookup\n"); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | regmap_write_bits(d_phy->regmap, CFGCHIP(2), | 
|  | 216 | PHY_INIT_BITS, PHY_INIT_BITS); | 
|  | 217 |  | 
|  | 218 | return 0; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | static int da8xx_usb_phy_remove(struct platform_device *pdev) | 
|  | 222 | { | 
|  | 223 | struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev); | 
|  | 224 |  | 
|  | 225 | if (!pdev->dev.of_node) { | 
|  | 226 | phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx"); | 
|  | 227 | phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci-da8xx"); | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | return 0; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | static const struct of_device_id da8xx_usb_phy_ids[] = { | 
|  | 234 | { .compatible = "ti,da830-usb-phy" }, | 
|  | 235 | { } | 
|  | 236 | }; | 
|  | 237 | MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids); | 
|  | 238 |  | 
|  | 239 | static struct platform_driver da8xx_usb_phy_driver = { | 
|  | 240 | .probe	= da8xx_usb_phy_probe, | 
|  | 241 | .remove	= da8xx_usb_phy_remove, | 
|  | 242 | .driver	= { | 
|  | 243 | .name	= "da8xx-usb-phy", | 
|  | 244 | .of_match_table = da8xx_usb_phy_ids, | 
|  | 245 | }, | 
|  | 246 | }; | 
|  | 247 |  | 
|  | 248 | module_platform_driver(da8xx_usb_phy_driver); | 
|  | 249 |  | 
|  | 250 | MODULE_ALIAS("platform:da8xx-usb-phy"); | 
|  | 251 | MODULE_AUTHOR("David Lechner <david@lechnology.com>"); | 
|  | 252 | MODULE_DESCRIPTION("TI DA8xx USB PHY driver"); | 
|  | 253 | MODULE_LICENSE("GPL v2"); |