rcar-usb2-clock-sel.c 4.7 KB

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