clk-hi3660-stub.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Hisilicon clock driver
  3. *
  4. * Copyright (c) 2013-2017 Hisilicon Limited.
  5. * Copyright (c) 2017 Linaro Limited.
  6. *
  7. * Author: Kai Zhao <zhaokai1@hisilicon.com>
  8. * Tao Wang <kevin.wangtao@hisilicon.com>
  9. * Leo Yan <leo.yan@linaro.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. */
  22. #include <linux/clk-provider.h>
  23. #include <linux/device.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/mailbox_client.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/platform_device.h>
  30. #include <dt-bindings/clock/hi3660-clock.h>
  31. #define HI3660_STUB_CLOCK_DATA (0x70)
  32. #define MHZ (1000 * 1000)
  33. #define DEFINE_CLK_STUB(_id, _cmd, _name) \
  34. { \
  35. .id = (_id), \
  36. .cmd = (_cmd), \
  37. .hw.init = &(struct clk_init_data) { \
  38. .name = #_name, \
  39. .ops = &hi3660_stub_clk_ops, \
  40. .num_parents = 0, \
  41. .flags = CLK_GET_RATE_NOCACHE, \
  42. }, \
  43. },
  44. #define to_stub_clk(_hw) container_of(_hw, struct hi3660_stub_clk, hw)
  45. struct hi3660_stub_clk_chan {
  46. struct mbox_client cl;
  47. struct mbox_chan *mbox;
  48. };
  49. struct hi3660_stub_clk {
  50. unsigned int id;
  51. struct clk_hw hw;
  52. unsigned int cmd;
  53. unsigned int msg[8];
  54. unsigned int rate;
  55. };
  56. static void __iomem *freq_reg;
  57. static struct hi3660_stub_clk_chan stub_clk_chan;
  58. static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw,
  59. unsigned long parent_rate)
  60. {
  61. struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
  62. /*
  63. * LPM3 writes back the CPU frequency in shared SRAM so read
  64. * back the frequency.
  65. */
  66. stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ;
  67. return stub_clk->rate;
  68. }
  69. static long hi3660_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  70. unsigned long *prate)
  71. {
  72. /*
  73. * LPM3 handles rate rounding so just return whatever
  74. * rate is requested.
  75. */
  76. return rate;
  77. }
  78. static int hi3660_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  79. unsigned long parent_rate)
  80. {
  81. struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
  82. stub_clk->msg[0] = stub_clk->cmd;
  83. stub_clk->msg[1] = rate / MHZ;
  84. dev_dbg(stub_clk_chan.cl.dev, "set rate msg[0]=0x%x msg[1]=0x%x\n",
  85. stub_clk->msg[0], stub_clk->msg[1]);
  86. mbox_send_message(stub_clk_chan.mbox, stub_clk->msg);
  87. mbox_client_txdone(stub_clk_chan.mbox, 0);
  88. stub_clk->rate = rate;
  89. return 0;
  90. }
  91. static const struct clk_ops hi3660_stub_clk_ops = {
  92. .recalc_rate = hi3660_stub_clk_recalc_rate,
  93. .round_rate = hi3660_stub_clk_round_rate,
  94. .set_rate = hi3660_stub_clk_set_rate,
  95. };
  96. static struct hi3660_stub_clk hi3660_stub_clks[HI3660_CLK_STUB_NUM] = {
  97. DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER0, 0x0001030A, "cpu-cluster.0")
  98. DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER1, 0x0002030A, "cpu-cluster.1")
  99. DEFINE_CLK_STUB(HI3660_CLK_STUB_GPU, 0x0003030A, "clk-g3d")
  100. DEFINE_CLK_STUB(HI3660_CLK_STUB_DDR, 0x00040309, "clk-ddrc")
  101. };
  102. static struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec,
  103. void *data)
  104. {
  105. unsigned int idx = clkspec->args[0];
  106. if (idx >= HI3660_CLK_STUB_NUM) {
  107. pr_err("%s: invalid index %u\n", __func__, idx);
  108. return ERR_PTR(-EINVAL);
  109. }
  110. return &hi3660_stub_clks[idx].hw;
  111. }
  112. static int hi3660_stub_clk_probe(struct platform_device *pdev)
  113. {
  114. struct device *dev = &pdev->dev;
  115. struct resource *res;
  116. unsigned int i;
  117. int ret;
  118. /* Use mailbox client without blocking */
  119. stub_clk_chan.cl.dev = dev;
  120. stub_clk_chan.cl.tx_done = NULL;
  121. stub_clk_chan.cl.tx_block = false;
  122. stub_clk_chan.cl.knows_txdone = false;
  123. /* Allocate mailbox channel */
  124. stub_clk_chan.mbox = mbox_request_channel(&stub_clk_chan.cl, 0);
  125. if (IS_ERR(stub_clk_chan.mbox))
  126. return PTR_ERR(stub_clk_chan.mbox);
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. if (!res)
  129. return -EINVAL;
  130. freq_reg = devm_ioremap(dev, res->start, resource_size(res));
  131. if (!freq_reg)
  132. return -ENOMEM;
  133. freq_reg += HI3660_STUB_CLOCK_DATA;
  134. for (i = 0; i < HI3660_CLK_STUB_NUM; i++) {
  135. ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw);
  136. if (ret)
  137. return ret;
  138. }
  139. return devm_of_clk_add_hw_provider(&pdev->dev, hi3660_stub_clk_hw_get,
  140. hi3660_stub_clks);
  141. }
  142. static const struct of_device_id hi3660_stub_clk_of_match[] = {
  143. { .compatible = "hisilicon,hi3660-stub-clk", },
  144. {}
  145. };
  146. static struct platform_driver hi3660_stub_clk_driver = {
  147. .probe = hi3660_stub_clk_probe,
  148. .driver = {
  149. .name = "hi3660-stub-clk",
  150. .of_match_table = hi3660_stub_clk_of_match,
  151. },
  152. };
  153. static int __init hi3660_stub_clk_init(void)
  154. {
  155. return platform_driver_register(&hi3660_stub_clk_driver);
  156. }
  157. subsys_initcall(hi3660_stub_clk_init);