2 * phy-brcm-usb.c - Broadcom USB Phy Driver
4 * Copyright (C) 2015-2017 Broadcom
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
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.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
20 #include <linux/module.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/interrupt.h>
25 #include <linux/soc/brcmstb/brcmstb.h>
26 #include <dt-bindings/phy/phy.h>
28 #include "phy-brcm-usb-init.h"
30 static DEFINE_MUTEX(sysfs_lock
);
32 enum brcm_usb_phy_id
{
38 struct value_to_name_map
{
43 static 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" }
50 static struct value_to_name_map brcm_dual_mode_to_name
[] = {
62 struct brcm_usb_phy_data
{
63 struct brcm_usb_init_params ini
;
66 struct clk
*usb_20_clk
;
67 struct clk
*usb_30_clk
;
68 struct mutex mutex
; /* serialize phy init */
70 struct brcm_usb_phy phys
[BRCM_USB_PHY_ID_MAX
];
73 static int brcm_usb_phy_init(struct phy
*gphy
)
75 struct brcm_usb_phy
*phy
= phy_get_drvdata(gphy
);
76 struct brcm_usb_phy_data
*priv
=
77 container_of(phy
, struct brcm_usb_phy_data
, phys
[phy
->id
]);
80 * Use a lock to make sure a second caller waits until
81 * the base phy is inited before using it.
83 mutex_lock(&priv
->mutex
);
84 if (priv
->init_count
++ == 0) {
85 clk_enable(priv
->usb_20_clk
);
86 clk_enable(priv
->usb_30_clk
);
87 brcm_usb_init_common(&priv
->ini
);
89 mutex_unlock(&priv
->mutex
);
90 if (phy
->id
== BRCM_USB_PHY_2_0
)
91 brcm_usb_init_eohci(&priv
->ini
);
92 else if (phy
->id
== BRCM_USB_PHY_3_0
)
93 brcm_usb_init_xhci(&priv
->ini
);
95 dev_dbg(&gphy
->dev
, "INIT, id: %d, total: %d\n", phy
->id
,
101 static int brcm_usb_phy_exit(struct phy
*gphy
)
103 struct brcm_usb_phy
*phy
= phy_get_drvdata(gphy
);
104 struct brcm_usb_phy_data
*priv
=
105 container_of(phy
, struct brcm_usb_phy_data
, phys
[phy
->id
]);
107 dev_dbg(&gphy
->dev
, "EXIT\n");
108 if (phy
->id
== BRCM_USB_PHY_2_0
)
109 brcm_usb_uninit_eohci(&priv
->ini
);
110 if (phy
->id
== BRCM_USB_PHY_3_0
)
111 brcm_usb_uninit_xhci(&priv
->ini
);
113 /* If both xhci and eohci are gone, reset everything else */
114 mutex_lock(&priv
->mutex
);
115 if (--priv
->init_count
== 0) {
116 brcm_usb_uninit_common(&priv
->ini
);
117 clk_disable(priv
->usb_20_clk
);
118 clk_disable(priv
->usb_30_clk
);
120 mutex_unlock(&priv
->mutex
);
125 static struct phy_ops brcm_usb_phy_ops
= {
126 .init
= brcm_usb_phy_init
,
127 .exit
= brcm_usb_phy_exit
,
128 .owner
= THIS_MODULE
,
131 static struct phy
*brcm_usb_phy_xlate(struct device
*dev
,
132 struct of_phandle_args
*args
)
134 struct brcm_usb_phy_data
*data
= dev_get_drvdata(dev
);
137 * values 0 and 1 are for backward compatibility with
138 * device tree nodes from older bootloaders.
140 switch (args
->args
[0]) {
143 if (data
->phys
[BRCM_USB_PHY_2_0
].phy
)
144 return data
->phys
[BRCM_USB_PHY_2_0
].phy
;
145 dev_warn(dev
, "Error, 2.0 Phy not found\n");
149 if (data
->phys
[BRCM_USB_PHY_3_0
].phy
)
150 return data
->phys
[BRCM_USB_PHY_3_0
].phy
;
151 dev_warn(dev
, "Error, 3.0 Phy not found\n");
154 return ERR_PTR(-ENODEV
);
157 static int name_to_value(struct value_to_name_map
*table
, int count
,
158 const char *name
, int *value
)
163 for (x
= 0; x
< count
; x
++) {
164 if (sysfs_streq(name
, table
[x
].name
)) {
172 static const char *value_to_name(struct value_to_name_map
*table
, int count
,
177 return table
[value
].name
;
180 static ssize_t
dr_mode_show(struct device
*dev
,
181 struct device_attribute
*attr
,
184 struct brcm_usb_phy_data
*priv
= dev_get_drvdata(dev
);
186 return sprintf(buf
, "%s\n",
187 value_to_name(&brcm_dr_mode_to_name
[0],
188 ARRAY_SIZE(brcm_dr_mode_to_name
),
191 static DEVICE_ATTR_RO(dr_mode
);
193 static ssize_t
dual_select_store(struct device
*dev
,
194 struct device_attribute
*attr
,
195 const char *buf
, size_t len
)
197 struct brcm_usb_phy_data
*priv
= dev_get_drvdata(dev
);
201 mutex_lock(&sysfs_lock
);
202 res
= name_to_value(&brcm_dual_mode_to_name
[0],
203 ARRAY_SIZE(brcm_dual_mode_to_name
), buf
, &value
);
205 brcm_usb_init_set_dual_select(&priv
->ini
, value
);
208 mutex_unlock(&sysfs_lock
);
212 static ssize_t
dual_select_show(struct device
*dev
,
213 struct device_attribute
*attr
,
216 struct brcm_usb_phy_data
*priv
= dev_get_drvdata(dev
);
219 mutex_lock(&sysfs_lock
);
220 value
= brcm_usb_init_get_dual_select(&priv
->ini
);
221 mutex_unlock(&sysfs_lock
);
222 return sprintf(buf
, "%s\n",
223 value_to_name(&brcm_dual_mode_to_name
[0],
224 ARRAY_SIZE(brcm_dual_mode_to_name
),
227 static DEVICE_ATTR_RW(dual_select
);
229 static struct attribute
*brcm_usb_phy_attrs
[] = {
230 &dev_attr_dr_mode
.attr
,
231 &dev_attr_dual_select
.attr
,
235 static const struct attribute_group brcm_usb_phy_group
= {
236 .attrs
= brcm_usb_phy_attrs
,
239 static int brcm_usb_phy_dvr_init(struct device
*dev
,
240 struct brcm_usb_phy_data
*priv
,
241 struct device_node
*dn
)
246 priv
->usb_20_clk
= of_clk_get_by_name(dn
, "sw_usb");
247 if (IS_ERR(priv
->usb_20_clk
)) {
248 dev_info(dev
, "Clock not found in Device Tree\n");
249 priv
->usb_20_clk
= NULL
;
251 err
= clk_prepare_enable(priv
->usb_20_clk
);
255 if (priv
->has_eohci
) {
256 gphy
= devm_phy_create(dev
, NULL
, &brcm_usb_phy_ops
);
258 dev_err(dev
, "failed to create EHCI/OHCI PHY\n");
259 return PTR_ERR(gphy
);
261 priv
->phys
[BRCM_USB_PHY_2_0
].phy
= gphy
;
262 priv
->phys
[BRCM_USB_PHY_2_0
].id
= BRCM_USB_PHY_2_0
;
263 phy_set_drvdata(gphy
, &priv
->phys
[BRCM_USB_PHY_2_0
]);
266 if (priv
->has_xhci
) {
267 gphy
= devm_phy_create(dev
, NULL
, &brcm_usb_phy_ops
);
269 dev_err(dev
, "failed to create XHCI PHY\n");
270 return PTR_ERR(gphy
);
272 priv
->phys
[BRCM_USB_PHY_3_0
].phy
= gphy
;
273 priv
->phys
[BRCM_USB_PHY_3_0
].id
= BRCM_USB_PHY_3_0
;
274 phy_set_drvdata(gphy
, &priv
->phys
[BRCM_USB_PHY_3_0
]);
276 priv
->usb_30_clk
= of_clk_get_by_name(dn
, "sw_usb3");
277 if (IS_ERR(priv
->usb_30_clk
)) {
279 "USB3.0 clock not found in Device Tree\n");
280 priv
->usb_30_clk
= NULL
;
282 err
= clk_prepare_enable(priv
->usb_30_clk
);
289 static int brcm_usb_phy_probe(struct platform_device
*pdev
)
291 struct resource
*res
;
292 struct device
*dev
= &pdev
->dev
;
293 struct brcm_usb_phy_data
*priv
;
294 struct phy_provider
*phy_provider
;
295 struct device_node
*dn
= pdev
->dev
.of_node
;
299 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
302 platform_set_drvdata(pdev
, priv
);
304 priv
->ini
.family_id
= brcmstb_get_family_id();
305 priv
->ini
.product_id
= brcmstb_get_product_id();
306 brcm_usb_set_family_map(&priv
->ini
);
307 dev_dbg(dev
, "Best mapping table is for %s\n",
308 priv
->ini
.family_name
);
309 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
311 dev_err(dev
, "can't get USB_CTRL base address\n");
314 priv
->ini
.ctrl_regs
= devm_ioremap_resource(dev
, res
);
315 if (IS_ERR(priv
->ini
.ctrl_regs
)) {
316 dev_err(dev
, "can't map CTRL register space\n");
320 /* The XHCI EC registers are optional */
321 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
323 priv
->ini
.xhci_ec_regs
=
324 devm_ioremap_resource(dev
, res
);
325 if (IS_ERR(priv
->ini
.xhci_ec_regs
)) {
326 dev_err(dev
, "can't map XHCI EC register space\n");
331 of_property_read_u32(dn
, "brcm,ipp", &priv
->ini
.ipp
);
332 of_property_read_u32(dn
, "brcm,ioc", &priv
->ini
.ioc
);
334 priv
->ini
.mode
= USB_CTLR_MODE_HOST
;
335 err
= of_property_read_string(dn
, "dr_mode", &mode
);
337 name_to_value(&brcm_dr_mode_to_name
[0],
338 ARRAY_SIZE(brcm_dr_mode_to_name
),
339 mode
, &priv
->ini
.mode
);
341 if (of_property_read_bool(dn
, "brcm,has-xhci"))
342 priv
->has_xhci
= true;
343 if (of_property_read_bool(dn
, "brcm,has-eohci"))
344 priv
->has_eohci
= true;
346 err
= brcm_usb_phy_dvr_init(dev
, priv
, dn
);
350 mutex_init(&priv
->mutex
);
352 /* make sure invert settings are correct */
353 brcm_usb_init_ipp(&priv
->ini
);
356 * Create sysfs entries for mode.
357 * Remove "dual_select" attribute if not in dual mode
359 if (priv
->ini
.mode
!= USB_CTLR_MODE_DRD
)
360 brcm_usb_phy_attrs
[1] = NULL
;
361 err
= sysfs_create_group(&dev
->kobj
, &brcm_usb_phy_group
);
363 dev_warn(dev
, "Error creating sysfs attributes\n");
365 /* start with everything off */
367 brcm_usb_uninit_xhci(&priv
->ini
);
369 brcm_usb_uninit_eohci(&priv
->ini
);
370 brcm_usb_uninit_common(&priv
->ini
);
371 clk_disable(priv
->usb_20_clk
);
372 clk_disable(priv
->usb_30_clk
);
374 phy_provider
= devm_of_phy_provider_register(dev
, brcm_usb_phy_xlate
);
375 if (IS_ERR(phy_provider
))
376 return PTR_ERR(phy_provider
);
381 static int brcm_usb_phy_remove(struct platform_device
*pdev
)
383 sysfs_remove_group(&pdev
->dev
.kobj
, &brcm_usb_phy_group
);
388 #ifdef CONFIG_PM_SLEEP
389 static int brcm_usb_phy_suspend(struct device
*dev
)
391 struct brcm_usb_phy_data
*priv
= dev_get_drvdata(dev
);
393 if (priv
->init_count
) {
394 clk_disable(priv
->usb_20_clk
);
395 clk_disable(priv
->usb_30_clk
);
400 static int brcm_usb_phy_resume(struct device
*dev
)
402 struct brcm_usb_phy_data
*priv
= dev_get_drvdata(dev
);
404 clk_enable(priv
->usb_20_clk
);
405 clk_enable(priv
->usb_30_clk
);
406 brcm_usb_init_ipp(&priv
->ini
);
409 * Initialize anything that was previously initialized.
410 * Uninitialize anything that wasn't previously initialized.
412 if (priv
->init_count
) {
413 brcm_usb_init_common(&priv
->ini
);
414 if (priv
->phys
[BRCM_USB_PHY_2_0
].inited
) {
415 brcm_usb_init_eohci(&priv
->ini
);
416 } else if (priv
->has_eohci
) {
417 brcm_usb_uninit_eohci(&priv
->ini
);
418 clk_disable(priv
->usb_20_clk
);
420 if (priv
->phys
[BRCM_USB_PHY_3_0
].inited
) {
421 brcm_usb_init_xhci(&priv
->ini
);
422 } else if (priv
->has_xhci
) {
423 brcm_usb_uninit_xhci(&priv
->ini
);
424 clk_disable(priv
->usb_30_clk
);
428 brcm_usb_uninit_xhci(&priv
->ini
);
430 brcm_usb_uninit_eohci(&priv
->ini
);
431 brcm_usb_uninit_common(&priv
->ini
);
432 clk_disable(priv
->usb_20_clk
);
433 clk_disable(priv
->usb_30_clk
);
438 #endif /* CONFIG_PM_SLEEP */
440 static const struct dev_pm_ops brcm_usb_phy_pm_ops
= {
441 SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend
, brcm_usb_phy_resume
)
444 static const struct of_device_id brcm_usb_dt_ids
[] = {
445 { .compatible
= "brcm,brcmstb-usb-phy" },
449 MODULE_DEVICE_TABLE(of
, brcm_usb_dt_ids
);
451 static struct platform_driver brcm_usb_driver
= {
452 .probe
= brcm_usb_phy_probe
,
453 .remove
= brcm_usb_phy_remove
,
455 .name
= "brcmstb-usb-phy",
456 .owner
= THIS_MODULE
,
457 .pm
= &brcm_usb_phy_pm_ops
,
458 .of_match_table
= brcm_usb_dt_ids
,
462 module_platform_driver(brcm_usb_driver
);
464 MODULE_ALIAS("platform:brcmstb-usb-phy");
465 MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
466 MODULE_DESCRIPTION("BRCM USB PHY driver");
467 MODULE_LICENSE("GPL v2");