rcar-usb2-clock-sel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas R-Car USB2.0 clock selector
  4. *
  5. * Copyright (C) 2017 Renesas Electronics Corp.
  6. *
  7. * Based on renesas-cpg-mssr.c
  8. *
  9. * Copyright (C) 2015 Glider bvba
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/device.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #define USB20_CLKSET0 0x00
  22. #define CLKSET0_INTCLK_EN BIT(11)
  23. #define CLKSET0_PRIVATE BIT(0)
  24. #define CLKSET0_EXTAL_ONLY (CLKSET0_INTCLK_EN | CLKSET0_PRIVATE)
  25. struct usb2_clock_sel_priv {
  26. void __iomem *base;
  27. struct clk_hw hw;
  28. bool extal;
  29. bool xtal;
  30. };
  31. #define to_priv(_hw) container_of(_hw, struct usb2_clock_sel_priv, hw)
  32. static void usb2_clock_sel_enable_extal_only(struct usb2_clock_sel_priv *priv)
  33. {
  34. u16 val = readw(priv->base + USB20_CLKSET0);
  35. pr_debug("%s: enter %d %d %x\n", __func__,
  36. priv->extal, priv->xtal, val);
  37. if (priv->extal && !priv->xtal && val != CLKSET0_EXTAL_ONLY)
  38. writew(CLKSET0_EXTAL_ONLY, priv->base + USB20_CLKSET0);
  39. }
  40. static void usb2_clock_sel_disable_extal_only(struct usb2_clock_sel_priv *priv)
  41. {
  42. if (priv->extal && !priv->xtal)
  43. writew(CLKSET0_PRIVATE, priv->base + USB20_CLKSET0);
  44. }
  45. static int usb2_clock_sel_enable(struct clk_hw *hw)
  46. {
  47. usb2_clock_sel_enable_extal_only(to_priv(hw));
  48. return 0;
  49. }
  50. static void usb2_clock_sel_disable(struct clk_hw *hw)
  51. {
  52. usb2_clock_sel_disable_extal_only(to_priv(hw));
  53. }
  54. /*
  55. * This module seems a mux, but this driver assumes a gate because
  56. * ehci/ohci platform drivers don't support clk_set_parent() for now.
  57. * If this driver acts as a gate, ehci/ohci-platform drivers don't need
  58. * any modification.
  59. */
  60. static const struct clk_ops usb2_clock_sel_clock_ops = {
  61. .enable = usb2_clock_sel_enable,
  62. .disable = usb2_clock_sel_disable,
  63. };
  64. static const struct of_device_id rcar_usb2_clock_sel_match[] = {
  65. { .compatible = "renesas,rcar-gen3-usb2-clock-sel" },
  66. { }
  67. };
  68. static int rcar_usb2_clock_sel_suspend(struct device *dev)
  69. {
  70. struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
  71. usb2_clock_sel_disable_extal_only(priv);
  72. pm_runtime_put(dev);
  73. return 0;
  74. }
  75. static int rcar_usb2_clock_sel_resume(struct device *dev)
  76. {
  77. struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev);
  78. pm_runtime_get_sync(dev);
  79. usb2_clock_sel_enable_extal_only(priv);
  80. return 0;
  81. }
  82. static int rcar_usb2_clock_sel_remove(struct platform_device *pdev)
  83. {
  84. struct device *dev = &pdev->dev;
  85. struct usb2_clock_sel_priv *priv = platform_get_drvdata(pdev);
  86. of_clk_del_provider(dev->of_node);
  87. clk_hw_unregister(&priv->hw);
  88. pm_runtime_put(dev);
  89. pm_runtime_disable(dev);
  90. return 0;
  91. }
  92. static int rcar_usb2_clock_sel_probe(struct platform_device *pdev)
  93. {
  94. struct device *dev = &pdev->dev;
  95. struct device_node *np = dev->of_node;
  96. struct usb2_clock_sel_priv *priv;
  97. struct resource *res;
  98. struct clk *clk;
  99. struct clk_init_data init;
  100. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  101. if (!priv)
  102. return -ENOMEM;
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. priv->base = devm_ioremap_resource(dev, res);
  105. if (IS_ERR(priv->base))
  106. return PTR_ERR(priv->base);
  107. pm_runtime_enable(dev);
  108. pm_runtime_get_sync(dev);
  109. clk = devm_clk_get(dev, "usb_extal");
  110. if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
  111. priv->extal = !!clk_get_rate(clk);
  112. clk_disable_unprepare(clk);
  113. }
  114. clk = devm_clk_get(dev, "usb_xtal");
  115. if (!IS_ERR(clk) && !clk_prepare_enable(clk)) {
  116. priv->xtal = !!clk_get_rate(clk);
  117. clk_disable_unprepare(clk);
  118. }
  119. if (!priv->extal && !priv->xtal) {
  120. dev_err(dev, "This driver needs usb_extal or usb_xtal\n");
  121. return -ENOENT;
  122. }
  123. platform_set_drvdata(pdev, priv);
  124. dev_set_drvdata(dev, priv);
  125. init.name = "rcar_usb2_clock_sel";
  126. init.ops = &usb2_clock_sel_clock_ops;
  127. init.flags = 0;
  128. init.parent_names = NULL;
  129. init.num_parents = 0;
  130. priv->hw.init = &init;
  131. clk = clk_register(NULL, &priv->hw);
  132. if (IS_ERR(clk))
  133. return PTR_ERR(clk);
  134. return of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
  135. }
  136. static const struct dev_pm_ops rcar_usb2_clock_sel_pm_ops = {
  137. .suspend = rcar_usb2_clock_sel_suspend,
  138. .resume = rcar_usb2_clock_sel_resume,
  139. };
  140. static struct platform_driver rcar_usb2_clock_sel_driver = {
  141. .driver = {
  142. .name = "rcar-usb2-clock-sel",
  143. .of_match_table = rcar_usb2_clock_sel_match,
  144. .pm = &rcar_usb2_clock_sel_pm_ops,
  145. },
  146. .probe = rcar_usb2_clock_sel_probe,
  147. .remove = rcar_usb2_clock_sel_remove,
  148. };
  149. builtin_platform_driver(rcar_usb2_clock_sel_driver);
  150. MODULE_DESCRIPTION("Renesas R-Car USB2 clock selector Driver");
  151. MODULE_LICENSE("GPL v2");