Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 2 | /* |
| 3 | * phy-brcm-usb.c - Broadcom USB Phy Driver |
| 4 | * |
| 5 | * Copyright (C) 2015-2017 Broadcom |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/phy/phy.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/soc/brcmstb/brcmstb.h> |
| 18 | #include <dt-bindings/phy/phy.h> |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 19 | #include <linux/mfd/syscon.h> |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 20 | |
| 21 | #include "phy-brcm-usb-init.h" |
| 22 | |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 23 | static DEFINE_MUTEX(sysfs_lock); |
| 24 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 25 | enum brcm_usb_phy_id { |
| 26 | BRCM_USB_PHY_2_0 = 0, |
| 27 | BRCM_USB_PHY_3_0, |
| 28 | BRCM_USB_PHY_ID_MAX |
| 29 | }; |
| 30 | |
| 31 | struct value_to_name_map { |
| 32 | int value; |
| 33 | const char *name; |
| 34 | }; |
| 35 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 36 | struct match_chip_info { |
| 37 | void *init_func; |
| 38 | u8 required_regs[BRCM_REGS_MAX + 1]; |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame^] | 39 | u8 optional_reg; |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 40 | }; |
| 41 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 42 | static struct value_to_name_map brcm_dr_mode_to_name[] = { |
| 43 | { USB_CTLR_MODE_HOST, "host" }, |
| 44 | { USB_CTLR_MODE_DEVICE, "peripheral" }, |
| 45 | { USB_CTLR_MODE_DRD, "drd" }, |
| 46 | { USB_CTLR_MODE_TYPEC_PD, "typec-pd" } |
| 47 | }; |
| 48 | |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 49 | static struct value_to_name_map brcm_dual_mode_to_name[] = { |
| 50 | { 0, "host" }, |
| 51 | { 1, "device" }, |
| 52 | { 2, "auto" }, |
| 53 | }; |
| 54 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 55 | struct brcm_usb_phy { |
| 56 | struct phy *phy; |
| 57 | unsigned int id; |
| 58 | bool inited; |
| 59 | }; |
| 60 | |
| 61 | struct brcm_usb_phy_data { |
| 62 | struct brcm_usb_init_params ini; |
| 63 | bool has_eohci; |
| 64 | bool has_xhci; |
| 65 | struct clk *usb_20_clk; |
| 66 | struct clk *usb_30_clk; |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 67 | struct clk *suspend_clk; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 68 | struct mutex mutex; /* serialize phy init */ |
| 69 | int init_count; |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 70 | int wake_irq; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 71 | struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX]; |
| 72 | }; |
| 73 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 74 | static s8 *node_reg_names[BRCM_REGS_MAX] = { |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame^] | 75 | "crtl", "xhci_ec", "xhci_gbl", "usb_phy", "usb_mdio", "bdc_ec" |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 78 | static irqreturn_t brcm_usb_phy_wake_isr(int irq, void *dev_id) |
| 79 | { |
| 80 | struct phy *gphy = dev_id; |
| 81 | |
| 82 | pm_wakeup_event(&gphy->dev, 0); |
| 83 | |
| 84 | return IRQ_HANDLED; |
| 85 | } |
| 86 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 87 | static int brcm_usb_phy_init(struct phy *gphy) |
| 88 | { |
| 89 | struct brcm_usb_phy *phy = phy_get_drvdata(gphy); |
| 90 | struct brcm_usb_phy_data *priv = |
| 91 | container_of(phy, struct brcm_usb_phy_data, phys[phy->id]); |
| 92 | |
| 93 | /* |
| 94 | * Use a lock to make sure a second caller waits until |
| 95 | * the base phy is inited before using it. |
| 96 | */ |
| 97 | mutex_lock(&priv->mutex); |
| 98 | if (priv->init_count++ == 0) { |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 99 | clk_prepare_enable(priv->usb_20_clk); |
| 100 | clk_prepare_enable(priv->usb_30_clk); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 101 | clk_prepare_enable(priv->suspend_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 102 | brcm_usb_init_common(&priv->ini); |
| 103 | } |
| 104 | mutex_unlock(&priv->mutex); |
| 105 | if (phy->id == BRCM_USB_PHY_2_0) |
| 106 | brcm_usb_init_eohci(&priv->ini); |
| 107 | else if (phy->id == BRCM_USB_PHY_3_0) |
| 108 | brcm_usb_init_xhci(&priv->ini); |
| 109 | phy->inited = true; |
| 110 | dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id, |
| 111 | priv->init_count); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int brcm_usb_phy_exit(struct phy *gphy) |
| 117 | { |
| 118 | struct brcm_usb_phy *phy = phy_get_drvdata(gphy); |
| 119 | struct brcm_usb_phy_data *priv = |
| 120 | container_of(phy, struct brcm_usb_phy_data, phys[phy->id]); |
| 121 | |
| 122 | dev_dbg(&gphy->dev, "EXIT\n"); |
| 123 | if (phy->id == BRCM_USB_PHY_2_0) |
| 124 | brcm_usb_uninit_eohci(&priv->ini); |
| 125 | if (phy->id == BRCM_USB_PHY_3_0) |
| 126 | brcm_usb_uninit_xhci(&priv->ini); |
| 127 | |
| 128 | /* If both xhci and eohci are gone, reset everything else */ |
| 129 | mutex_lock(&priv->mutex); |
| 130 | if (--priv->init_count == 0) { |
| 131 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 132 | clk_disable_unprepare(priv->usb_20_clk); |
| 133 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 134 | clk_disable_unprepare(priv->suspend_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 135 | } |
| 136 | mutex_unlock(&priv->mutex); |
| 137 | phy->inited = false; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static struct phy_ops brcm_usb_phy_ops = { |
| 142 | .init = brcm_usb_phy_init, |
| 143 | .exit = brcm_usb_phy_exit, |
| 144 | .owner = THIS_MODULE, |
| 145 | }; |
| 146 | |
| 147 | static struct phy *brcm_usb_phy_xlate(struct device *dev, |
| 148 | struct of_phandle_args *args) |
| 149 | { |
| 150 | struct brcm_usb_phy_data *data = dev_get_drvdata(dev); |
| 151 | |
| 152 | /* |
| 153 | * values 0 and 1 are for backward compatibility with |
| 154 | * device tree nodes from older bootloaders. |
| 155 | */ |
| 156 | switch (args->args[0]) { |
| 157 | case 0: |
| 158 | case PHY_TYPE_USB2: |
| 159 | if (data->phys[BRCM_USB_PHY_2_0].phy) |
| 160 | return data->phys[BRCM_USB_PHY_2_0].phy; |
| 161 | dev_warn(dev, "Error, 2.0 Phy not found\n"); |
| 162 | break; |
| 163 | case 1: |
| 164 | case PHY_TYPE_USB3: |
| 165 | if (data->phys[BRCM_USB_PHY_3_0].phy) |
| 166 | return data->phys[BRCM_USB_PHY_3_0].phy; |
| 167 | dev_warn(dev, "Error, 3.0 Phy not found\n"); |
| 168 | break; |
| 169 | } |
| 170 | return ERR_PTR(-ENODEV); |
| 171 | } |
| 172 | |
| 173 | static int name_to_value(struct value_to_name_map *table, int count, |
| 174 | const char *name, int *value) |
| 175 | { |
| 176 | int x; |
| 177 | |
| 178 | *value = 0; |
| 179 | for (x = 0; x < count; x++) { |
| 180 | if (sysfs_streq(name, table[x].name)) { |
| 181 | *value = x; |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 188 | static const char *value_to_name(struct value_to_name_map *table, int count, |
| 189 | int value) |
| 190 | { |
| 191 | if (value >= count) |
| 192 | return "unknown"; |
| 193 | return table[value].name; |
| 194 | } |
| 195 | |
| 196 | static ssize_t dr_mode_show(struct device *dev, |
| 197 | struct device_attribute *attr, |
| 198 | char *buf) |
| 199 | { |
| 200 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 201 | |
| 202 | return sprintf(buf, "%s\n", |
| 203 | value_to_name(&brcm_dr_mode_to_name[0], |
| 204 | ARRAY_SIZE(brcm_dr_mode_to_name), |
| 205 | priv->ini.mode)); |
| 206 | } |
| 207 | static DEVICE_ATTR_RO(dr_mode); |
| 208 | |
| 209 | static ssize_t dual_select_store(struct device *dev, |
| 210 | struct device_attribute *attr, |
| 211 | const char *buf, size_t len) |
| 212 | { |
| 213 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 214 | int value; |
| 215 | int res; |
| 216 | |
| 217 | mutex_lock(&sysfs_lock); |
| 218 | res = name_to_value(&brcm_dual_mode_to_name[0], |
| 219 | ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value); |
| 220 | if (!res) { |
Al Cooper | 94583a4 | 2020-01-03 13:18:03 -0500 | [diff] [blame] | 221 | brcm_usb_set_dual_select(&priv->ini, value); |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 222 | res = len; |
| 223 | } |
| 224 | mutex_unlock(&sysfs_lock); |
| 225 | return res; |
| 226 | } |
| 227 | |
| 228 | static ssize_t dual_select_show(struct device *dev, |
| 229 | struct device_attribute *attr, |
| 230 | char *buf) |
| 231 | { |
| 232 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 233 | int value; |
| 234 | |
| 235 | mutex_lock(&sysfs_lock); |
Al Cooper | 94583a4 | 2020-01-03 13:18:03 -0500 | [diff] [blame] | 236 | value = brcm_usb_get_dual_select(&priv->ini); |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 237 | mutex_unlock(&sysfs_lock); |
| 238 | return sprintf(buf, "%s\n", |
| 239 | value_to_name(&brcm_dual_mode_to_name[0], |
| 240 | ARRAY_SIZE(brcm_dual_mode_to_name), |
| 241 | value)); |
| 242 | } |
| 243 | static DEVICE_ATTR_RW(dual_select); |
| 244 | |
| 245 | static struct attribute *brcm_usb_phy_attrs[] = { |
| 246 | &dev_attr_dr_mode.attr, |
| 247 | &dev_attr_dual_select.attr, |
| 248 | NULL |
| 249 | }; |
| 250 | |
| 251 | static const struct attribute_group brcm_usb_phy_group = { |
| 252 | .attrs = brcm_usb_phy_attrs, |
| 253 | }; |
| 254 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 255 | static struct match_chip_info chip_info_7216 = { |
| 256 | .init_func = &brcm_usb_dvr_init_7216, |
| 257 | .required_regs = { |
| 258 | BRCM_REGS_CTRL, |
| 259 | BRCM_REGS_XHCI_EC, |
| 260 | BRCM_REGS_XHCI_GBL, |
| 261 | -1, |
| 262 | }, |
| 263 | }; |
| 264 | |
| 265 | static struct match_chip_info chip_info_7211b0 = { |
| 266 | .init_func = &brcm_usb_dvr_init_7211b0, |
| 267 | .required_regs = { |
| 268 | BRCM_REGS_CTRL, |
| 269 | BRCM_REGS_XHCI_EC, |
| 270 | BRCM_REGS_XHCI_GBL, |
| 271 | BRCM_REGS_USB_PHY, |
| 272 | BRCM_REGS_USB_MDIO, |
| 273 | -1, |
| 274 | }, |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame^] | 275 | .optional_reg = BRCM_REGS_BDC_EC, |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | static struct match_chip_info chip_info_7445 = { |
| 279 | .init_func = &brcm_usb_dvr_init_7445, |
| 280 | .required_regs = { |
| 281 | BRCM_REGS_CTRL, |
| 282 | BRCM_REGS_XHCI_EC, |
| 283 | -1, |
| 284 | }, |
| 285 | }; |
| 286 | |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 287 | static const struct of_device_id brcm_usb_dt_ids[] = { |
| 288 | { |
| 289 | .compatible = "brcm,bcm7216-usb-phy", |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 290 | .data = &chip_info_7216, |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 291 | }, |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 292 | { |
| 293 | .compatible = "brcm,bcm7211-usb-phy", |
| 294 | .data = &chip_info_7211b0, |
| 295 | }, |
| 296 | { |
| 297 | .compatible = "brcm,brcmstb-usb-phy", |
| 298 | .data = &chip_info_7445, |
| 299 | }, |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 300 | { /* sentinel */ } |
| 301 | }; |
| 302 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 303 | static int brcm_usb_get_regs(struct platform_device *pdev, |
| 304 | enum brcmusb_reg_sel regs, |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame^] | 305 | struct brcm_usb_init_params *ini, |
| 306 | bool optional) |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 307 | { |
| 308 | struct resource *res; |
| 309 | |
| 310 | /* Older DT nodes have ctrl and optional xhci_ec by index only */ |
| 311 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 312 | node_reg_names[regs]); |
| 313 | if (res == NULL) { |
| 314 | if (regs == BRCM_REGS_CTRL) { |
| 315 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 316 | } else if (regs == BRCM_REGS_XHCI_EC) { |
| 317 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 318 | /* XHCI_EC registers are optional */ |
| 319 | if (res == NULL) |
| 320 | return 0; |
| 321 | } |
| 322 | if (res == NULL) { |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame^] | 323 | if (optional) { |
| 324 | dev_dbg(&pdev->dev, |
| 325 | "Optional reg %s not found\n", |
| 326 | node_reg_names[regs]); |
| 327 | return 0; |
| 328 | } |
| 329 | dev_err(&pdev->dev, "can't get %s base addr\n", |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 330 | node_reg_names[regs]); |
| 331 | return 1; |
| 332 | } |
| 333 | } |
| 334 | ini->regs[regs] = devm_ioremap_resource(&pdev->dev, res); |
| 335 | if (IS_ERR(ini->regs[regs])) { |
| 336 | dev_err(&pdev->dev, "can't map %s register space\n", |
| 337 | node_reg_names[regs]); |
| 338 | return 1; |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 343 | static int brcm_usb_phy_dvr_init(struct platform_device *pdev, |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 344 | struct brcm_usb_phy_data *priv, |
| 345 | struct device_node *dn) |
| 346 | { |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 347 | struct device *dev = &pdev->dev; |
| 348 | struct phy *gphy = NULL; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 349 | int err; |
| 350 | |
| 351 | priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb"); |
| 352 | if (IS_ERR(priv->usb_20_clk)) { |
Al Cooper | 89927fe | 2020-01-03 13:18:07 -0500 | [diff] [blame] | 353 | if (PTR_ERR(priv->usb_20_clk) == -EPROBE_DEFER) |
| 354 | return -EPROBE_DEFER; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 355 | dev_info(dev, "Clock not found in Device Tree\n"); |
| 356 | priv->usb_20_clk = NULL; |
| 357 | } |
| 358 | err = clk_prepare_enable(priv->usb_20_clk); |
| 359 | if (err) |
| 360 | return err; |
| 361 | |
| 362 | if (priv->has_eohci) { |
| 363 | gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops); |
| 364 | if (IS_ERR(gphy)) { |
| 365 | dev_err(dev, "failed to create EHCI/OHCI PHY\n"); |
| 366 | return PTR_ERR(gphy); |
| 367 | } |
| 368 | priv->phys[BRCM_USB_PHY_2_0].phy = gphy; |
| 369 | priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0; |
| 370 | phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]); |
| 371 | } |
| 372 | |
| 373 | if (priv->has_xhci) { |
| 374 | gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops); |
| 375 | if (IS_ERR(gphy)) { |
| 376 | dev_err(dev, "failed to create XHCI PHY\n"); |
| 377 | return PTR_ERR(gphy); |
| 378 | } |
| 379 | priv->phys[BRCM_USB_PHY_3_0].phy = gphy; |
| 380 | priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0; |
| 381 | phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]); |
| 382 | |
| 383 | priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3"); |
| 384 | if (IS_ERR(priv->usb_30_clk)) { |
Al Cooper | 89927fe | 2020-01-03 13:18:07 -0500 | [diff] [blame] | 385 | if (PTR_ERR(priv->usb_30_clk) == -EPROBE_DEFER) |
| 386 | return -EPROBE_DEFER; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 387 | dev_info(dev, |
| 388 | "USB3.0 clock not found in Device Tree\n"); |
| 389 | priv->usb_30_clk = NULL; |
| 390 | } |
| 391 | err = clk_prepare_enable(priv->usb_30_clk); |
| 392 | if (err) |
| 393 | return err; |
| 394 | } |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 395 | |
| 396 | priv->suspend_clk = clk_get(dev, "usb0_freerun"); |
| 397 | if (IS_ERR(priv->suspend_clk)) { |
Al Cooper | 89927fe | 2020-01-03 13:18:07 -0500 | [diff] [blame] | 398 | if (PTR_ERR(priv->suspend_clk) == -EPROBE_DEFER) |
| 399 | return -EPROBE_DEFER; |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 400 | dev_err(dev, "Suspend Clock not found in Device Tree\n"); |
| 401 | priv->suspend_clk = NULL; |
| 402 | } |
| 403 | |
| 404 | priv->wake_irq = platform_get_irq_byname(pdev, "wake"); |
| 405 | if (priv->wake_irq < 0) |
| 406 | priv->wake_irq = platform_get_irq_byname(pdev, "wakeup"); |
| 407 | if (priv->wake_irq >= 0) { |
| 408 | err = devm_request_irq(dev, priv->wake_irq, |
| 409 | brcm_usb_phy_wake_isr, 0, |
| 410 | dev_name(dev), gphy); |
| 411 | if (err < 0) |
| 412 | return err; |
| 413 | device_set_wakeup_capable(dev, 1); |
| 414 | } else { |
| 415 | dev_info(dev, |
| 416 | "Wake interrupt missing, system wake not supported\n"); |
| 417 | } |
| 418 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int brcm_usb_phy_probe(struct platform_device *pdev) |
| 423 | { |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 424 | struct device *dev = &pdev->dev; |
| 425 | struct brcm_usb_phy_data *priv; |
| 426 | struct phy_provider *phy_provider; |
| 427 | struct device_node *dn = pdev->dev.of_node; |
| 428 | int err; |
| 429 | const char *mode; |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 430 | const struct of_device_id *match; |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 431 | void (*dvr_init)(struct brcm_usb_init_params *params); |
| 432 | const struct match_chip_info *info; |
| 433 | struct regmap *rmap; |
| 434 | int x; |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 435 | |
| 436 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 437 | if (!priv) |
| 438 | return -ENOMEM; |
| 439 | platform_set_drvdata(pdev, priv); |
| 440 | |
| 441 | priv->ini.family_id = brcmstb_get_family_id(); |
| 442 | priv->ini.product_id = brcmstb_get_product_id(); |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 443 | |
| 444 | match = of_match_node(brcm_usb_dt_ids, dev->of_node); |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 445 | info = match->data; |
| 446 | dvr_init = info->init_func; |
| 447 | (*dvr_init)(&priv->ini); |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 448 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 449 | dev_dbg(dev, "Best mapping table is for %s\n", |
| 450 | priv->ini.family_name); |
Al Cooper | 4e5b9c9 | 2020-01-03 13:18:05 -0500 | [diff] [blame] | 451 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 452 | of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp); |
| 453 | of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc); |
| 454 | |
| 455 | priv->ini.mode = USB_CTLR_MODE_HOST; |
| 456 | err = of_property_read_string(dn, "dr_mode", &mode); |
| 457 | if (err == 0) { |
| 458 | name_to_value(&brcm_dr_mode_to_name[0], |
| 459 | ARRAY_SIZE(brcm_dr_mode_to_name), |
| 460 | mode, &priv->ini.mode); |
| 461 | } |
Al Cooper | 5e498ff | 2017-12-27 14:28:48 -0500 | [diff] [blame] | 462 | if (of_property_read_bool(dn, "brcm,has-xhci")) |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 463 | priv->has_xhci = true; |
Al Cooper | 5e498ff | 2017-12-27 14:28:48 -0500 | [diff] [blame] | 464 | if (of_property_read_bool(dn, "brcm,has-eohci")) |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 465 | priv->has_eohci = true; |
| 466 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 467 | for (x = 0; x < BRCM_REGS_MAX; x++) { |
| 468 | if (info->required_regs[x] >= BRCM_REGS_MAX) |
| 469 | break; |
| 470 | |
| 471 | err = brcm_usb_get_regs(pdev, info->required_regs[x], |
Al Cooper | bed63b6 | 2020-01-03 13:18:09 -0500 | [diff] [blame^] | 472 | &priv->ini, false); |
| 473 | if (err) |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | if (info->optional_reg) { |
| 477 | err = brcm_usb_get_regs(pdev, info->optional_reg, |
| 478 | &priv->ini, true); |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 479 | if (err) |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 483 | err = brcm_usb_phy_dvr_init(pdev, priv, dn); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 484 | if (err) |
| 485 | return err; |
| 486 | |
| 487 | mutex_init(&priv->mutex); |
| 488 | |
| 489 | /* make sure invert settings are correct */ |
| 490 | brcm_usb_init_ipp(&priv->ini); |
| 491 | |
Al Cooper | 415060b | 2017-09-22 15:34:02 -0400 | [diff] [blame] | 492 | /* |
| 493 | * Create sysfs entries for mode. |
| 494 | * Remove "dual_select" attribute if not in dual mode |
| 495 | */ |
| 496 | if (priv->ini.mode != USB_CTLR_MODE_DRD) |
| 497 | brcm_usb_phy_attrs[1] = NULL; |
| 498 | err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group); |
| 499 | if (err) |
| 500 | dev_warn(dev, "Error creating sysfs attributes\n"); |
| 501 | |
Al Cooper | 9d5f51d | 2020-01-03 13:18:06 -0500 | [diff] [blame] | 502 | /* Get piarbctl syscon if it exists */ |
| 503 | rmap = syscon_regmap_lookup_by_phandle(dev->of_node, |
| 504 | "syscon-piarbctl"); |
| 505 | if (IS_ERR(rmap)) |
| 506 | rmap = syscon_regmap_lookup_by_phandle(dev->of_node, |
| 507 | "brcm,syscon-piarbctl"); |
| 508 | if (!IS_ERR(rmap)) |
| 509 | priv->ini.syscon_piarbctl = rmap; |
| 510 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 511 | /* start with everything off */ |
| 512 | if (priv->has_xhci) |
| 513 | brcm_usb_uninit_xhci(&priv->ini); |
| 514 | if (priv->has_eohci) |
| 515 | brcm_usb_uninit_eohci(&priv->ini); |
| 516 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 517 | clk_disable_unprepare(priv->usb_20_clk); |
| 518 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 519 | |
| 520 | phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 521 | |
zhong jiang | 5286481 | 2018-08-16 23:58:54 +0800 | [diff] [blame] | 522 | return PTR_ERR_OR_ZERO(phy_provider); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 523 | } |
| 524 | |
Florian Fainelli | d9e1008 | 2019-05-22 11:35:25 -0700 | [diff] [blame] | 525 | static int brcm_usb_phy_remove(struct platform_device *pdev) |
| 526 | { |
| 527 | sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group); |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 532 | #ifdef CONFIG_PM_SLEEP |
| 533 | static int brcm_usb_phy_suspend(struct device *dev) |
| 534 | { |
| 535 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 536 | |
| 537 | if (priv->init_count) { |
Al Cooper | 6597af4 | 2020-01-03 13:18:01 -0500 | [diff] [blame] | 538 | if (priv->phys[BRCM_USB_PHY_3_0].inited) |
| 539 | brcm_usb_uninit_xhci(&priv->ini); |
| 540 | if (priv->phys[BRCM_USB_PHY_2_0].inited) |
| 541 | brcm_usb_uninit_eohci(&priv->ini); |
| 542 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 543 | brcm_usb_wake_enable(&priv->ini, true); |
Al Cooper | 6597af4 | 2020-01-03 13:18:01 -0500 | [diff] [blame] | 544 | if (priv->phys[BRCM_USB_PHY_3_0].inited) |
| 545 | clk_disable_unprepare(priv->usb_30_clk); |
| 546 | if (priv->phys[BRCM_USB_PHY_2_0].inited) |
| 547 | clk_disable_unprepare(priv->usb_20_clk); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 548 | if (priv->wake_irq >= 0) |
| 549 | enable_irq_wake(priv->wake_irq); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 550 | } |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int brcm_usb_phy_resume(struct device *dev) |
| 555 | { |
| 556 | struct brcm_usb_phy_data *priv = dev_get_drvdata(dev); |
| 557 | |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 558 | clk_prepare_enable(priv->usb_20_clk); |
| 559 | clk_prepare_enable(priv->usb_30_clk); |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 560 | brcm_usb_wake_enable(&priv->ini, false); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 561 | brcm_usb_init_ipp(&priv->ini); |
| 562 | |
| 563 | /* |
| 564 | * Initialize anything that was previously initialized. |
| 565 | * Uninitialize anything that wasn't previously initialized. |
| 566 | */ |
| 567 | if (priv->init_count) { |
Al Cooper | f1c0db4 | 2020-01-03 13:18:02 -0500 | [diff] [blame] | 568 | if (priv->wake_irq >= 0) |
| 569 | disable_irq_wake(priv->wake_irq); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 570 | brcm_usb_init_common(&priv->ini); |
| 571 | if (priv->phys[BRCM_USB_PHY_2_0].inited) { |
| 572 | brcm_usb_init_eohci(&priv->ini); |
| 573 | } else if (priv->has_eohci) { |
| 574 | brcm_usb_uninit_eohci(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 575 | clk_disable_unprepare(priv->usb_20_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 576 | } |
| 577 | if (priv->phys[BRCM_USB_PHY_3_0].inited) { |
| 578 | brcm_usb_init_xhci(&priv->ini); |
| 579 | } else if (priv->has_xhci) { |
| 580 | brcm_usb_uninit_xhci(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 581 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 582 | } |
| 583 | } else { |
| 584 | if (priv->has_xhci) |
| 585 | brcm_usb_uninit_xhci(&priv->ini); |
| 586 | if (priv->has_eohci) |
| 587 | brcm_usb_uninit_eohci(&priv->ini); |
| 588 | brcm_usb_uninit_common(&priv->ini); |
Al Cooper | ece5ffd | 2020-01-03 13:18:00 -0500 | [diff] [blame] | 589 | clk_disable_unprepare(priv->usb_20_clk); |
| 590 | clk_disable_unprepare(priv->usb_30_clk); |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | #endif /* CONFIG_PM_SLEEP */ |
| 596 | |
| 597 | static const struct dev_pm_ops brcm_usb_phy_pm_ops = { |
| 598 | SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume) |
| 599 | }; |
| 600 | |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 601 | MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids); |
| 602 | |
| 603 | static struct platform_driver brcm_usb_driver = { |
| 604 | .probe = brcm_usb_phy_probe, |
Florian Fainelli | d9e1008 | 2019-05-22 11:35:25 -0700 | [diff] [blame] | 605 | .remove = brcm_usb_phy_remove, |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 606 | .driver = { |
| 607 | .name = "brcmstb-usb-phy", |
Al Cooper | 49859e5 | 2017-09-22 15:34:01 -0400 | [diff] [blame] | 608 | .pm = &brcm_usb_phy_pm_ops, |
| 609 | .of_match_table = brcm_usb_dt_ids, |
| 610 | }, |
| 611 | }; |
| 612 | |
| 613 | module_platform_driver(brcm_usb_driver); |
| 614 | |
| 615 | MODULE_ALIAS("platform:brcmstb-usb-phy"); |
| 616 | MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>"); |
| 617 | MODULE_DESCRIPTION("BRCM USB PHY driver"); |
| 618 | MODULE_LICENSE("GPL v2"); |