xref: /linux/drivers/usb/chipidea/ci_hdrc_npcm.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
170f13579STomer Maimon // SPDX-License-Identifier: GPL-2.0
270f13579STomer Maimon // Copyright (c) 2023 Nuvoton Technology corporation.
370f13579STomer Maimon 
470f13579STomer Maimon #include <linux/module.h>
570f13579STomer Maimon #include <linux/platform_device.h>
670f13579STomer Maimon #include <linux/pm_runtime.h>
770f13579STomer Maimon #include <linux/usb/chipidea.h>
870f13579STomer Maimon #include <linux/clk.h>
970f13579STomer Maimon #include <linux/io.h>
1070f13579STomer Maimon #include <linux/reset-controller.h>
1170f13579STomer Maimon #include <linux/of.h>
1270f13579STomer Maimon 
1370f13579STomer Maimon #include "ci.h"
1470f13579STomer Maimon 
1570f13579STomer Maimon struct npcm_udc_data {
1670f13579STomer Maimon 	struct platform_device	*ci;
1770f13579STomer Maimon 	struct clk		*core_clk;
1870f13579STomer Maimon 	struct ci_hdrc_platform_data pdata;
1970f13579STomer Maimon };
2070f13579STomer Maimon 
npcm_udc_notify_event(struct ci_hdrc * ci,unsigned int event)21*7793472bSAndreas Kühn static int npcm_udc_notify_event(struct ci_hdrc *ci, unsigned int event)
2270f13579STomer Maimon {
2370f13579STomer Maimon 	struct device *dev = ci->dev->parent;
2470f13579STomer Maimon 
2570f13579STomer Maimon 	switch (event) {
2670f13579STomer Maimon 	case CI_HDRC_CONTROLLER_RESET_EVENT:
2770f13579STomer Maimon 		/* clear all mode bits */
2870f13579STomer Maimon 		hw_write(ci, OP_USBMODE, 0xffffffff, 0x0);
2970f13579STomer Maimon 		break;
3070f13579STomer Maimon 	default:
3170f13579STomer Maimon 		dev_dbg(dev, "unknown ci_hdrc event (%d)\n", event);
3270f13579STomer Maimon 		break;
3370f13579STomer Maimon 	}
3470f13579STomer Maimon 
3570f13579STomer Maimon 	return 0;
3670f13579STomer Maimon }
3770f13579STomer Maimon 
npcm_udc_probe(struct platform_device * pdev)3870f13579STomer Maimon static int npcm_udc_probe(struct platform_device *pdev)
3970f13579STomer Maimon {
4070f13579STomer Maimon 	int ret;
4170f13579STomer Maimon 	struct npcm_udc_data *ci;
4270f13579STomer Maimon 	struct platform_device *plat_ci;
4370f13579STomer Maimon 	struct device *dev = &pdev->dev;
4470f13579STomer Maimon 
4570f13579STomer Maimon 	ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
4670f13579STomer Maimon 	if (!ci)
4770f13579STomer Maimon 		return -ENOMEM;
4870f13579STomer Maimon 	platform_set_drvdata(pdev, ci);
4970f13579STomer Maimon 
5070f13579STomer Maimon 	ci->core_clk = devm_clk_get_optional(dev, NULL);
5170f13579STomer Maimon 	if (IS_ERR(ci->core_clk))
5270f13579STomer Maimon 		return PTR_ERR(ci->core_clk);
5370f13579STomer Maimon 
5470f13579STomer Maimon 	ret = clk_prepare_enable(ci->core_clk);
5570f13579STomer Maimon 	if (ret)
5670f13579STomer Maimon 		return dev_err_probe(dev, ret, "failed to enable the clock: %d\n", ret);
5770f13579STomer Maimon 
5870f13579STomer Maimon 	ci->pdata.name = dev_name(dev);
5970f13579STomer Maimon 	ci->pdata.capoffset = DEF_CAPOFFSET;
6070f13579STomer Maimon 	ci->pdata.flags	= CI_HDRC_REQUIRES_ALIGNED_DMA |
6170f13579STomer Maimon 		CI_HDRC_FORCE_VBUS_ACTIVE_ALWAYS;
6270f13579STomer Maimon 	ci->pdata.phy_mode = USBPHY_INTERFACE_MODE_UTMI;
6370f13579STomer Maimon 	ci->pdata.notify_event = npcm_udc_notify_event;
6470f13579STomer Maimon 
6570f13579STomer Maimon 	plat_ci = ci_hdrc_add_device(dev, pdev->resource, pdev->num_resources,
6670f13579STomer Maimon 				     &ci->pdata);
6770f13579STomer Maimon 	if (IS_ERR(plat_ci)) {
6870f13579STomer Maimon 		ret = PTR_ERR(plat_ci);
6970f13579STomer Maimon 		dev_err(dev, "failed to register HDRC NPCM device: %d\n", ret);
7070f13579STomer Maimon 		goto clk_err;
7170f13579STomer Maimon 	}
7270f13579STomer Maimon 
7370f13579STomer Maimon 	pm_runtime_no_callbacks(dev);
7470f13579STomer Maimon 	pm_runtime_enable(dev);
7570f13579STomer Maimon 
7670f13579STomer Maimon 	return 0;
7770f13579STomer Maimon 
7870f13579STomer Maimon clk_err:
7970f13579STomer Maimon 	clk_disable_unprepare(ci->core_clk);
8070f13579STomer Maimon 	return ret;
8170f13579STomer Maimon }
8270f13579STomer Maimon 
npcm_udc_remove(struct platform_device * pdev)831f855c5eSUwe Kleine-König static void npcm_udc_remove(struct platform_device *pdev)
8470f13579STomer Maimon {
8570f13579STomer Maimon 	struct npcm_udc_data *ci = platform_get_drvdata(pdev);
8670f13579STomer Maimon 
8770f13579STomer Maimon 	pm_runtime_disable(&pdev->dev);
8870f13579STomer Maimon 	ci_hdrc_remove_device(ci->ci);
8970f13579STomer Maimon 	clk_disable_unprepare(ci->core_clk);
9070f13579STomer Maimon }
9170f13579STomer Maimon 
9270f13579STomer Maimon static const struct of_device_id npcm_udc_dt_match[] = {
9370f13579STomer Maimon 	{ .compatible = "nuvoton,npcm750-udc", },
9470f13579STomer Maimon 	{ .compatible = "nuvoton,npcm845-udc", },
9570f13579STomer Maimon 	{ }
9670f13579STomer Maimon };
9770f13579STomer Maimon MODULE_DEVICE_TABLE(of, npcm_udc_dt_match);
9870f13579STomer Maimon 
9970f13579STomer Maimon static struct platform_driver npcm_udc_driver = {
10070f13579STomer Maimon 	.probe = npcm_udc_probe,
1011f855c5eSUwe Kleine-König 	.remove_new = npcm_udc_remove,
10270f13579STomer Maimon 	.driver = {
10370f13579STomer Maimon 		.name = "npcm_udc",
10470f13579STomer Maimon 		.of_match_table = npcm_udc_dt_match,
10570f13579STomer Maimon 	},
10670f13579STomer Maimon };
10770f13579STomer Maimon 
10870f13579STomer Maimon module_platform_driver(npcm_udc_driver);
10970f13579STomer Maimon 
11070f13579STomer Maimon MODULE_DESCRIPTION("NPCM USB device controller driver");
11170f13579STomer Maimon MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
11270f13579STomer Maimon MODULE_LICENSE("GPL v2");
113