ci_hdrc_npcm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2023 Nuvoton Technology corporation.
  3. #include <linux/module.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/pm_runtime.h>
  6. #include <linux/usb/chipidea.h>
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/reset-controller.h>
  10. #include <linux/of.h>
  11. #include "ci.h"
  12. struct npcm_udc_data {
  13. struct platform_device *ci;
  14. struct clk *core_clk;
  15. struct ci_hdrc_platform_data pdata;
  16. };
  17. static int npcm_udc_notify_event(struct ci_hdrc *ci, unsigned int event)
  18. {
  19. struct device *dev = ci->dev->parent;
  20. switch (event) {
  21. case CI_HDRC_CONTROLLER_RESET_EVENT:
  22. /* clear all mode bits */
  23. hw_write(ci, OP_USBMODE, 0xffffffff, 0x0);
  24. break;
  25. default:
  26. dev_dbg(dev, "unknown ci_hdrc event (%d)\n", event);
  27. break;
  28. }
  29. return 0;
  30. }
  31. static int npcm_udc_probe(struct platform_device *pdev)
  32. {
  33. int ret;
  34. struct npcm_udc_data *ci;
  35. struct platform_device *plat_ci;
  36. struct device *dev = &pdev->dev;
  37. ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
  38. if (!ci)
  39. return -ENOMEM;
  40. platform_set_drvdata(pdev, ci);
  41. ci->core_clk = devm_clk_get_optional(dev, NULL);
  42. if (IS_ERR(ci->core_clk))
  43. return PTR_ERR(ci->core_clk);
  44. ret = clk_prepare_enable(ci->core_clk);
  45. if (ret)
  46. return dev_err_probe(dev, ret, "failed to enable the clock: %d\n", ret);
  47. ci->pdata.name = dev_name(dev);
  48. ci->pdata.capoffset = DEF_CAPOFFSET;
  49. ci->pdata.flags = CI_HDRC_REQUIRES_ALIGNED_DMA |
  50. CI_HDRC_FORCE_VBUS_ACTIVE_ALWAYS;
  51. ci->pdata.phy_mode = USBPHY_INTERFACE_MODE_UTMI;
  52. ci->pdata.notify_event = npcm_udc_notify_event;
  53. plat_ci = ci_hdrc_add_device(dev, pdev->resource, pdev->num_resources,
  54. &ci->pdata);
  55. if (IS_ERR(plat_ci)) {
  56. ret = PTR_ERR(plat_ci);
  57. dev_err(dev, "failed to register HDRC NPCM device: %d\n", ret);
  58. goto clk_err;
  59. }
  60. pm_runtime_no_callbacks(dev);
  61. pm_runtime_enable(dev);
  62. return 0;
  63. clk_err:
  64. clk_disable_unprepare(ci->core_clk);
  65. return ret;
  66. }
  67. static void npcm_udc_remove(struct platform_device *pdev)
  68. {
  69. struct npcm_udc_data *ci = platform_get_drvdata(pdev);
  70. pm_runtime_disable(&pdev->dev);
  71. ci_hdrc_remove_device(ci->ci);
  72. clk_disable_unprepare(ci->core_clk);
  73. }
  74. static const struct of_device_id npcm_udc_dt_match[] = {
  75. { .compatible = "nuvoton,npcm750-udc", },
  76. { .compatible = "nuvoton,npcm845-udc", },
  77. { }
  78. };
  79. MODULE_DEVICE_TABLE(of, npcm_udc_dt_match);
  80. static struct platform_driver npcm_udc_driver = {
  81. .probe = npcm_udc_probe,
  82. .remove_new = npcm_udc_remove,
  83. .driver = {
  84. .name = "npcm_udc",
  85. .of_match_table = npcm_udc_dt_match,
  86. },
  87. };
  88. module_platform_driver(npcm_udc_driver);
  89. MODULE_DESCRIPTION("NPCM USB device controller driver");
  90. MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
  91. MODULE_LICENSE("GPL v2");