clk-twl6040.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * TWL6040 clock module driver for OMAP4 McPDM functional clock
  3. *
  4. * Copyright (C) 2012 Texas Instruments Inc.
  5. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mfd/twl6040.h>
  26. #include <linux/clk-provider.h>
  27. struct twl6040_pdmclk {
  28. struct twl6040 *twl6040;
  29. struct device *dev;
  30. struct clk_hw pdmclk_hw;
  31. int enabled;
  32. };
  33. static int twl6040_pdmclk_is_prepared(struct clk_hw *hw)
  34. {
  35. struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
  36. pdmclk_hw);
  37. return pdmclk->enabled;
  38. }
  39. static int twl6040_pdmclk_reset_one_clock(struct twl6040_pdmclk *pdmclk,
  40. unsigned int reg)
  41. {
  42. const u8 reset_mask = TWL6040_HPLLRST; /* Same for HPPLL and LPPLL */
  43. int ret;
  44. ret = twl6040_set_bits(pdmclk->twl6040, reg, reset_mask);
  45. if (ret < 0)
  46. return ret;
  47. ret = twl6040_clear_bits(pdmclk->twl6040, reg, reset_mask);
  48. if (ret < 0)
  49. return ret;
  50. return 0;
  51. }
  52. /*
  53. * TWL6040A2 Phoenix Audio IC erratum #6: "PDM Clock Generation Issue At
  54. * Cold Temperature". This affects cold boot and deeper idle states it
  55. * seems. The workaround consists of resetting HPPLL and LPPLL.
  56. */
  57. static int twl6040_pdmclk_quirk_reset_clocks(struct twl6040_pdmclk *pdmclk)
  58. {
  59. int ret;
  60. ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_HPPLLCTL);
  61. if (ret)
  62. return ret;
  63. ret = twl6040_pdmclk_reset_one_clock(pdmclk, TWL6040_REG_LPPLLCTL);
  64. if (ret)
  65. return ret;
  66. return 0;
  67. }
  68. static int twl6040_pdmclk_prepare(struct clk_hw *hw)
  69. {
  70. struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
  71. pdmclk_hw);
  72. int ret;
  73. ret = twl6040_power(pdmclk->twl6040, 1);
  74. if (ret)
  75. return ret;
  76. ret = twl6040_pdmclk_quirk_reset_clocks(pdmclk);
  77. if (ret)
  78. goto out_err;
  79. pdmclk->enabled = 1;
  80. return 0;
  81. out_err:
  82. dev_err(pdmclk->dev, "%s: error %i\n", __func__, ret);
  83. twl6040_power(pdmclk->twl6040, 0);
  84. return ret;
  85. }
  86. static void twl6040_pdmclk_unprepare(struct clk_hw *hw)
  87. {
  88. struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
  89. pdmclk_hw);
  90. int ret;
  91. ret = twl6040_power(pdmclk->twl6040, 0);
  92. if (!ret)
  93. pdmclk->enabled = 0;
  94. }
  95. static unsigned long twl6040_pdmclk_recalc_rate(struct clk_hw *hw,
  96. unsigned long parent_rate)
  97. {
  98. struct twl6040_pdmclk *pdmclk = container_of(hw, struct twl6040_pdmclk,
  99. pdmclk_hw);
  100. return twl6040_get_sysclk(pdmclk->twl6040);
  101. }
  102. static const struct clk_ops twl6040_pdmclk_ops = {
  103. .is_prepared = twl6040_pdmclk_is_prepared,
  104. .prepare = twl6040_pdmclk_prepare,
  105. .unprepare = twl6040_pdmclk_unprepare,
  106. .recalc_rate = twl6040_pdmclk_recalc_rate,
  107. };
  108. static struct clk_init_data twl6040_pdmclk_init = {
  109. .name = "pdmclk",
  110. .ops = &twl6040_pdmclk_ops,
  111. .flags = CLK_GET_RATE_NOCACHE,
  112. };
  113. static int twl6040_pdmclk_probe(struct platform_device *pdev)
  114. {
  115. struct twl6040 *twl6040 = dev_get_drvdata(pdev->dev.parent);
  116. struct twl6040_pdmclk *clkdata;
  117. int ret;
  118. clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
  119. if (!clkdata)
  120. return -ENOMEM;
  121. clkdata->dev = &pdev->dev;
  122. clkdata->twl6040 = twl6040;
  123. clkdata->pdmclk_hw.init = &twl6040_pdmclk_init;
  124. ret = devm_clk_hw_register(&pdev->dev, &clkdata->pdmclk_hw);
  125. if (ret)
  126. return ret;
  127. platform_set_drvdata(pdev, clkdata);
  128. return of_clk_add_hw_provider(pdev->dev.parent->of_node,
  129. of_clk_hw_simple_get,
  130. &clkdata->pdmclk_hw);
  131. }
  132. static struct platform_driver twl6040_pdmclk_driver = {
  133. .driver = {
  134. .name = "twl6040-pdmclk",
  135. },
  136. .probe = twl6040_pdmclk_probe,
  137. };
  138. module_platform_driver(twl6040_pdmclk_driver);
  139. MODULE_DESCRIPTION("TWL6040 clock driver for McPDM functional clock");
  140. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  141. MODULE_ALIAS("platform:twl6040-pdmclk");
  142. MODULE_LICENSE("GPL");