q6dsp-lpass-clocks.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2020, Linaro Limited
  3. #include <linux/err.h>
  4. #include <linux/init.h>
  5. #include <linux/clk-provider.h>
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/slab.h>
  12. #include <dt-bindings/sound/qcom,q6dsp-lpass-ports.h>
  13. #include "q6dsp-lpass-clocks.h"
  14. #define Q6DSP_MAX_CLK_ID 104
  15. #define Q6DSP_LPASS_CLK_ROOT_DEFAULT 0
  16. struct q6dsp_clk {
  17. struct device *dev;
  18. int q6dsp_clk_id;
  19. int attributes;
  20. int rate;
  21. uint32_t handle;
  22. struct clk_hw hw;
  23. };
  24. #define to_q6dsp_clk(_hw) container_of(_hw, struct q6dsp_clk, hw)
  25. struct q6dsp_cc {
  26. struct device *dev;
  27. struct q6dsp_clk *clks[Q6DSP_MAX_CLK_ID];
  28. const struct q6dsp_clk_desc *desc;
  29. };
  30. static int clk_q6dsp_prepare(struct clk_hw *hw)
  31. {
  32. struct q6dsp_clk *clk = to_q6dsp_clk(hw);
  33. struct q6dsp_cc *cc = dev_get_drvdata(clk->dev);
  34. return cc->desc->lpass_set_clk(clk->dev, clk->q6dsp_clk_id, clk->attributes,
  35. Q6DSP_LPASS_CLK_ROOT_DEFAULT, clk->rate);
  36. }
  37. static void clk_q6dsp_unprepare(struct clk_hw *hw)
  38. {
  39. struct q6dsp_clk *clk = to_q6dsp_clk(hw);
  40. struct q6dsp_cc *cc = dev_get_drvdata(clk->dev);
  41. cc->desc->lpass_set_clk(clk->dev, clk->q6dsp_clk_id, clk->attributes,
  42. Q6DSP_LPASS_CLK_ROOT_DEFAULT, 0);
  43. }
  44. static int clk_q6dsp_set_rate(struct clk_hw *hw, unsigned long rate,
  45. unsigned long parent_rate)
  46. {
  47. struct q6dsp_clk *clk = to_q6dsp_clk(hw);
  48. clk->rate = rate;
  49. return 0;
  50. }
  51. static unsigned long clk_q6dsp_recalc_rate(struct clk_hw *hw,
  52. unsigned long parent_rate)
  53. {
  54. struct q6dsp_clk *clk = to_q6dsp_clk(hw);
  55. return clk->rate;
  56. }
  57. static long clk_q6dsp_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *parent_rate)
  59. {
  60. return rate;
  61. }
  62. static const struct clk_ops clk_q6dsp_ops = {
  63. .prepare = clk_q6dsp_prepare,
  64. .unprepare = clk_q6dsp_unprepare,
  65. .set_rate = clk_q6dsp_set_rate,
  66. .round_rate = clk_q6dsp_round_rate,
  67. .recalc_rate = clk_q6dsp_recalc_rate,
  68. };
  69. static int clk_vote_q6dsp_block(struct clk_hw *hw)
  70. {
  71. struct q6dsp_clk *clk = to_q6dsp_clk(hw);
  72. struct q6dsp_cc *cc = dev_get_drvdata(clk->dev);
  73. return cc->desc->lpass_vote_clk(clk->dev, clk->q6dsp_clk_id,
  74. clk_hw_get_name(&clk->hw), &clk->handle);
  75. }
  76. static void clk_unvote_q6dsp_block(struct clk_hw *hw)
  77. {
  78. struct q6dsp_clk *clk = to_q6dsp_clk(hw);
  79. struct q6dsp_cc *cc = dev_get_drvdata(clk->dev);
  80. cc->desc->lpass_unvote_clk(clk->dev, clk->q6dsp_clk_id, clk->handle);
  81. }
  82. static const struct clk_ops clk_vote_q6dsp_ops = {
  83. .prepare = clk_vote_q6dsp_block,
  84. .unprepare = clk_unvote_q6dsp_block,
  85. };
  86. static struct clk_hw *q6dsp_of_clk_hw_get(struct of_phandle_args *clkspec,
  87. void *data)
  88. {
  89. struct q6dsp_cc *cc = data;
  90. unsigned int idx = clkspec->args[0];
  91. unsigned int attr = clkspec->args[1];
  92. if (idx >= Q6DSP_MAX_CLK_ID || attr > LPASS_CLK_ATTRIBUTE_COUPLE_DIVISOR) {
  93. dev_err(cc->dev, "Invalid clk specifier (%d, %d)\n", idx, attr);
  94. return ERR_PTR(-EINVAL);
  95. }
  96. if (cc->clks[idx]) {
  97. cc->clks[idx]->attributes = attr;
  98. return &cc->clks[idx]->hw;
  99. }
  100. return ERR_PTR(-ENOENT);
  101. }
  102. int q6dsp_clock_dev_probe(struct platform_device *pdev)
  103. {
  104. struct q6dsp_cc *cc;
  105. struct device *dev = &pdev->dev;
  106. const struct q6dsp_clk_init *q6dsp_clks;
  107. const struct q6dsp_clk_desc *desc;
  108. int i, ret;
  109. cc = devm_kzalloc(dev, sizeof(*cc), GFP_KERNEL);
  110. if (!cc)
  111. return -ENOMEM;
  112. desc = of_device_get_match_data(&pdev->dev);
  113. if (!desc)
  114. return -EINVAL;
  115. cc->desc = desc;
  116. cc->dev = dev;
  117. q6dsp_clks = desc->clks;
  118. for (i = 0; i < desc->num_clks; i++) {
  119. unsigned int id = q6dsp_clks[i].clk_id;
  120. struct clk_init_data init = {
  121. .name = q6dsp_clks[i].name,
  122. };
  123. struct q6dsp_clk *clk;
  124. clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL);
  125. if (!clk)
  126. return -ENOMEM;
  127. clk->dev = dev;
  128. clk->q6dsp_clk_id = q6dsp_clks[i].q6dsp_clk_id;
  129. clk->rate = q6dsp_clks[i].rate;
  130. clk->hw.init = &init;
  131. if (clk->rate)
  132. init.ops = &clk_q6dsp_ops;
  133. else
  134. init.ops = &clk_vote_q6dsp_ops;
  135. cc->clks[id] = clk;
  136. ret = devm_clk_hw_register(dev, &clk->hw);
  137. if (ret)
  138. return ret;
  139. }
  140. ret = devm_of_clk_add_hw_provider(dev, q6dsp_of_clk_hw_get, cc);
  141. if (ret)
  142. return ret;
  143. dev_set_drvdata(dev, cc);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(q6dsp_clock_dev_probe);