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