mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	usb: dwc3: add generic driver to support flattened
To support flattened dwc3 dt model and drop the glue layer, introduce the `dwc3-generic` driver. This enables direct binding of the DWC3 core driver and offers an alternative to the existing glue driver `dwc3-of-simple`. Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com> Signed-off-by: Ze Huang <huang.ze@linux.dev> Link: https://lore.kernel.org/r/20250913-dwc3_generic-v8-2-b50f81f05f95@linux.dev Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250913-dwc3_generic-v8-2-b50f81f05f95@linux.dev
This commit is contained in:
		
							parent
							
								
									7d547c1c24
								
							
						
					
					
						commit
						e0b6dc00c7
					
				
					 3 changed files with 178 additions and 0 deletions
				
			
		| 
						 | 
					@ -189,4 +189,15 @@ config USB_DWC3_RTK
 | 
				
			||||||
	  or dual-role mode.
 | 
						  or dual-role mode.
 | 
				
			||||||
	  Say 'Y' or 'M' if you have such device.
 | 
						  Say 'Y' or 'M' if you have such device.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					config USB_DWC3_GENERIC_PLAT
 | 
				
			||||||
 | 
						tristate "DWC3 Generic Platform Driver"
 | 
				
			||||||
 | 
						depends on OF && COMMON_CLK
 | 
				
			||||||
 | 
						default USB_DWC3
 | 
				
			||||||
 | 
						help
 | 
				
			||||||
 | 
						  Support USB3 functionality in simple SoC integrations.
 | 
				
			||||||
 | 
						  Currently supports SpacemiT DWC USB3. Platforms using
 | 
				
			||||||
 | 
						  dwc3-of-simple can easily switch to dwc3-generic by flattening
 | 
				
			||||||
 | 
						  the dwc3 child node in the device tree.
 | 
				
			||||||
 | 
						  Say 'Y' or 'M' here if your platform integrates DWC3 in a similar way.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -57,3 +57,4 @@ obj-$(CONFIG_USB_DWC3_IMX8MP)		+= dwc3-imx8mp.o
 | 
				
			||||||
obj-$(CONFIG_USB_DWC3_XILINX)		+= dwc3-xilinx.o
 | 
					obj-$(CONFIG_USB_DWC3_XILINX)		+= dwc3-xilinx.o
 | 
				
			||||||
obj-$(CONFIG_USB_DWC3_OCTEON)		+= dwc3-octeon.o
 | 
					obj-$(CONFIG_USB_DWC3_OCTEON)		+= dwc3-octeon.o
 | 
				
			||||||
obj-$(CONFIG_USB_DWC3_RTK)		+= dwc3-rtk.o
 | 
					obj-$(CONFIG_USB_DWC3_RTK)		+= dwc3-rtk.o
 | 
				
			||||||
 | 
					obj-$(CONFIG_USB_DWC3_GENERIC_PLAT)	+= dwc3-generic-plat.o
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										166
									
								
								drivers/usb/dwc3/dwc3-generic-plat.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										166
									
								
								drivers/usb/dwc3/dwc3-generic-plat.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,166 @@
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: GPL-2.0-only
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * dwc3-generic-plat.c - DesignWare USB3 generic platform driver
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (C) 2025 Ze Huang <huang.ze@linux.dev>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Inspired by dwc3-qcom.c and dwc3-of-simple.c
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <linux/clk.h>
 | 
				
			||||||
 | 
					#include <linux/platform_device.h>
 | 
				
			||||||
 | 
					#include <linux/reset.h>
 | 
				
			||||||
 | 
					#include "glue.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct dwc3_generic {
 | 
				
			||||||
 | 
						struct device		*dev;
 | 
				
			||||||
 | 
						struct dwc3		dwc;
 | 
				
			||||||
 | 
						struct clk_bulk_data	*clks;
 | 
				
			||||||
 | 
						int			num_clocks;
 | 
				
			||||||
 | 
						struct reset_control	*resets;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define to_dwc3_generic(d) container_of((d), struct dwc3_generic, dwc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void dwc3_generic_reset_control_assert(void *data)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						reset_control_assert(data);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int dwc3_generic_probe(struct platform_device *pdev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct dwc3_probe_data probe_data = {};
 | 
				
			||||||
 | 
						struct device *dev = &pdev->dev;
 | 
				
			||||||
 | 
						struct dwc3_generic *dwc3g;
 | 
				
			||||||
 | 
						struct resource *res;
 | 
				
			||||||
 | 
						int ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dwc3g = devm_kzalloc(dev, sizeof(*dwc3g), GFP_KERNEL);
 | 
				
			||||||
 | 
						if (!dwc3g)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dwc3g->dev = dev;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 | 
				
			||||||
 | 
						if (!res) {
 | 
				
			||||||
 | 
							dev_err(&pdev->dev, "missing memory resource\n");
 | 
				
			||||||
 | 
							return -ENODEV;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dwc3g->resets = devm_reset_control_array_get_optional_exclusive(dev);
 | 
				
			||||||
 | 
						if (IS_ERR(dwc3g->resets))
 | 
				
			||||||
 | 
							return dev_err_probe(dev, PTR_ERR(dwc3g->resets), "failed to get resets\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = reset_control_assert(dwc3g->resets);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return dev_err_probe(dev, ret, "failed to assert resets\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Not strict timing, just for safety */
 | 
				
			||||||
 | 
						udelay(2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = reset_control_deassert(dwc3g->resets);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return dev_err_probe(dev, ret, "failed to deassert resets\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = devm_add_action_or_reset(dev, dwc3_generic_reset_control_assert, dwc3g->resets);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = devm_clk_bulk_get_all_enabled(dwc3g->dev, &dwc3g->clks);
 | 
				
			||||||
 | 
						if (ret < 0)
 | 
				
			||||||
 | 
							return dev_err_probe(dev, ret, "failed to get clocks\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dwc3g->num_clocks = ret;
 | 
				
			||||||
 | 
						dwc3g->dwc.dev = dev;
 | 
				
			||||||
 | 
						probe_data.dwc = &dwc3g->dwc;
 | 
				
			||||||
 | 
						probe_data.res = res;
 | 
				
			||||||
 | 
						probe_data.ignore_clocks_and_resets = true;
 | 
				
			||||||
 | 
						ret = dwc3_core_probe(&probe_data);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return dev_err_probe(dev, ret, "failed to register DWC3 Core\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void dwc3_generic_remove(struct platform_device *pdev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct dwc3 *dwc = platform_get_drvdata(pdev);
 | 
				
			||||||
 | 
						struct dwc3_generic *dwc3g = to_dwc3_generic(dwc);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dwc3_core_remove(dwc);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						clk_bulk_disable_unprepare(dwc3g->num_clocks, dwc3g->clks);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int dwc3_generic_suspend(struct device *dev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct dwc3 *dwc = dev_get_drvdata(dev);
 | 
				
			||||||
 | 
						struct dwc3_generic *dwc3g = to_dwc3_generic(dwc);
 | 
				
			||||||
 | 
						int ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = dwc3_pm_suspend(dwc);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						clk_bulk_disable_unprepare(dwc3g->num_clocks, dwc3g->clks);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int dwc3_generic_resume(struct device *dev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct dwc3 *dwc = dev_get_drvdata(dev);
 | 
				
			||||||
 | 
						struct dwc3_generic *dwc3g = to_dwc3_generic(dwc);
 | 
				
			||||||
 | 
						int ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = clk_bulk_prepare_enable(dwc3g->num_clocks, dwc3g->clks);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ret = dwc3_pm_resume(dwc);
 | 
				
			||||||
 | 
						if (ret)
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int dwc3_generic_runtime_suspend(struct device *dev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return dwc3_runtime_suspend(dev_get_drvdata(dev));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int dwc3_generic_runtime_resume(struct device *dev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return dwc3_runtime_resume(dev_get_drvdata(dev));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int dwc3_generic_runtime_idle(struct device *dev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return dwc3_runtime_idle(dev_get_drvdata(dev));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const struct dev_pm_ops dwc3_generic_dev_pm_ops = {
 | 
				
			||||||
 | 
						SYSTEM_SLEEP_PM_OPS(dwc3_generic_suspend, dwc3_generic_resume)
 | 
				
			||||||
 | 
						RUNTIME_PM_OPS(dwc3_generic_runtime_suspend, dwc3_generic_runtime_resume,
 | 
				
			||||||
 | 
							       dwc3_generic_runtime_idle)
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const struct of_device_id dwc3_generic_of_match[] = {
 | 
				
			||||||
 | 
						{ .compatible = "spacemit,k1-dwc3", },
 | 
				
			||||||
 | 
						{ /* sentinel */ }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					MODULE_DEVICE_TABLE(of, dwc3_generic_of_match);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct platform_driver dwc3_generic_driver = {
 | 
				
			||||||
 | 
						.probe		= dwc3_generic_probe,
 | 
				
			||||||
 | 
						.remove		= dwc3_generic_remove,
 | 
				
			||||||
 | 
						.driver		= {
 | 
				
			||||||
 | 
							.name	= "dwc3-generic-plat",
 | 
				
			||||||
 | 
							.of_match_table = dwc3_generic_of_match,
 | 
				
			||||||
 | 
							.pm	= pm_ptr(&dwc3_generic_dev_pm_ops),
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					module_platform_driver(dwc3_generic_driver);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					MODULE_LICENSE("GPL");
 | 
				
			||||||
 | 
					MODULE_DESCRIPTION("DesignWare USB3 generic platform driver");
 | 
				
			||||||
		Loading…
	
		Reference in a new issue