clkdivider-hi6220.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Hisilicon hi6220 SoC divider clock driver
  3. *
  4. * Copyright (c) 2015 Hisilicon Limited.
  5. *
  6. * Author: Bintian Wang <bintian.wang@huawei.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/err.h>
  18. #include <linux/spinlock.h>
  19. #include "clk.h"
  20. #define div_mask(width) ((1 << (width)) - 1)
  21. /**
  22. * struct hi6220_clk_divider - divider clock for hi6220
  23. *
  24. * @hw: handle between common and hardware-specific interfaces
  25. * @reg: register containing divider
  26. * @shift: shift to the divider bit field
  27. * @width: width of the divider bit field
  28. * @mask: mask for setting divider rate
  29. * @table: the div table that the divider supports
  30. * @lock: register lock
  31. */
  32. struct hi6220_clk_divider {
  33. struct clk_hw hw;
  34. void __iomem *reg;
  35. u8 shift;
  36. u8 width;
  37. u32 mask;
  38. const struct clk_div_table *table;
  39. spinlock_t *lock;
  40. };
  41. #define to_hi6220_clk_divider(_hw) \
  42. container_of(_hw, struct hi6220_clk_divider, hw)
  43. static unsigned long hi6220_clkdiv_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. unsigned int val;
  47. struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
  48. val = readl_relaxed(dclk->reg) >> dclk->shift;
  49. val &= div_mask(dclk->width);
  50. return divider_recalc_rate(hw, parent_rate, val, dclk->table,
  51. CLK_DIVIDER_ROUND_CLOSEST);
  52. }
  53. static long hi6220_clkdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  54. unsigned long *prate)
  55. {
  56. struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
  57. return divider_round_rate(hw, rate, prate, dclk->table,
  58. dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
  59. }
  60. static int hi6220_clkdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  61. unsigned long parent_rate)
  62. {
  63. int value;
  64. unsigned long flags = 0;
  65. u32 data;
  66. struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
  67. value = divider_get_val(rate, parent_rate, dclk->table,
  68. dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
  69. if (dclk->lock)
  70. spin_lock_irqsave(dclk->lock, flags);
  71. data = readl_relaxed(dclk->reg);
  72. data &= ~(div_mask(dclk->width) << dclk->shift);
  73. data |= value << dclk->shift;
  74. data |= dclk->mask;
  75. writel_relaxed(data, dclk->reg);
  76. if (dclk->lock)
  77. spin_unlock_irqrestore(dclk->lock, flags);
  78. return 0;
  79. }
  80. static const struct clk_ops hi6220_clkdiv_ops = {
  81. .recalc_rate = hi6220_clkdiv_recalc_rate,
  82. .round_rate = hi6220_clkdiv_round_rate,
  83. .set_rate = hi6220_clkdiv_set_rate,
  84. };
  85. struct clk *hi6220_register_clkdiv(struct device *dev, const char *name,
  86. const char *parent_name, unsigned long flags, void __iomem *reg,
  87. u8 shift, u8 width, u32 mask_bit, spinlock_t *lock)
  88. {
  89. struct hi6220_clk_divider *div;
  90. struct clk *clk;
  91. struct clk_init_data init;
  92. struct clk_div_table *table;
  93. u32 max_div, min_div;
  94. int i;
  95. /* allocate the divider */
  96. div = kzalloc(sizeof(*div), GFP_KERNEL);
  97. if (!div)
  98. return ERR_PTR(-ENOMEM);
  99. /* Init the divider table */
  100. max_div = div_mask(width) + 1;
  101. min_div = 1;
  102. table = kcalloc(max_div + 1, sizeof(*table), GFP_KERNEL);
  103. if (!table) {
  104. kfree(div);
  105. return ERR_PTR(-ENOMEM);
  106. }
  107. for (i = 0; i < max_div; i++) {
  108. table[i].div = min_div + i;
  109. table[i].val = table[i].div - 1;
  110. }
  111. init.name = name;
  112. init.ops = &hi6220_clkdiv_ops;
  113. init.flags = flags;
  114. init.parent_names = parent_name ? &parent_name : NULL;
  115. init.num_parents = parent_name ? 1 : 0;
  116. /* struct hi6220_clk_divider assignments */
  117. div->reg = reg;
  118. div->shift = shift;
  119. div->width = width;
  120. div->mask = mask_bit ? BIT(mask_bit) : 0;
  121. div->lock = lock;
  122. div->hw.init = &init;
  123. div->table = table;
  124. /* register the clock */
  125. clk = clk_register(dev, &div->hw);
  126. if (IS_ERR(clk)) {
  127. kfree(table);
  128. kfree(div);
  129. }
  130. return clk;
  131. }