clkdivider-hi6220.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #define div_mask(width) ((1 << (width)) - 1)
  20. /**
  21. * struct hi6220_clk_divider - divider clock for hi6220
  22. *
  23. * @hw: handle between common and hardware-specific interfaces
  24. * @reg: register containing divider
  25. * @shift: shift to the divider bit field
  26. * @width: width of the divider bit field
  27. * @mask: mask for setting divider rate
  28. * @table: the div table that the divider supports
  29. * @lock: register lock
  30. */
  31. struct hi6220_clk_divider {
  32. struct clk_hw hw;
  33. void __iomem *reg;
  34. u8 shift;
  35. u8 width;
  36. u32 mask;
  37. const struct clk_div_table *table;
  38. spinlock_t *lock;
  39. };
  40. #define to_hi6220_clk_divider(_hw) \
  41. container_of(_hw, struct hi6220_clk_divider, hw)
  42. static unsigned long hi6220_clkdiv_recalc_rate(struct clk_hw *hw,
  43. unsigned long parent_rate)
  44. {
  45. unsigned int val;
  46. struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
  47. val = readl_relaxed(dclk->reg) >> dclk->shift;
  48. val &= div_mask(dclk->width);
  49. return divider_recalc_rate(hw, parent_rate, val, dclk->table,
  50. CLK_DIVIDER_ROUND_CLOSEST);
  51. }
  52. static long hi6220_clkdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  53. unsigned long *prate)
  54. {
  55. struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
  56. return divider_round_rate(hw, rate, prate, dclk->table,
  57. dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
  58. }
  59. static int hi6220_clkdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  60. unsigned long parent_rate)
  61. {
  62. int value;
  63. unsigned long flags = 0;
  64. u32 data;
  65. struct hi6220_clk_divider *dclk = to_hi6220_clk_divider(hw);
  66. value = divider_get_val(rate, parent_rate, dclk->table,
  67. dclk->width, CLK_DIVIDER_ROUND_CLOSEST);
  68. if (dclk->lock)
  69. spin_lock_irqsave(dclk->lock, flags);
  70. data = readl_relaxed(dclk->reg);
  71. data &= ~(div_mask(dclk->width) << dclk->shift);
  72. data |= value << dclk->shift;
  73. data |= dclk->mask;
  74. writel_relaxed(data, dclk->reg);
  75. if (dclk->lock)
  76. spin_unlock_irqrestore(dclk->lock, flags);
  77. return 0;
  78. }
  79. static const struct clk_ops hi6220_clkdiv_ops = {
  80. .recalc_rate = hi6220_clkdiv_recalc_rate,
  81. .round_rate = hi6220_clkdiv_round_rate,
  82. .set_rate = hi6220_clkdiv_set_rate,
  83. };
  84. struct clk *hi6220_register_clkdiv(struct device *dev, const char *name,
  85. const char *parent_name, unsigned long flags, void __iomem *reg,
  86. u8 shift, u8 width, u32 mask_bit, spinlock_t *lock)
  87. {
  88. struct hi6220_clk_divider *div;
  89. struct clk *clk;
  90. struct clk_init_data init;
  91. struct clk_div_table *table;
  92. u32 max_div, min_div;
  93. int i;
  94. /* allocate the divider */
  95. div = kzalloc(sizeof(*div), GFP_KERNEL);
  96. if (!div)
  97. return ERR_PTR(-ENOMEM);
  98. /* Init the divider table */
  99. max_div = div_mask(width) + 1;
  100. min_div = 1;
  101. table = kcalloc(max_div + 1, sizeof(*table), GFP_KERNEL);
  102. if (!table) {
  103. kfree(div);
  104. return ERR_PTR(-ENOMEM);
  105. }
  106. for (i = 0; i < max_div; i++) {
  107. table[i].div = min_div + i;
  108. table[i].val = table[i].div - 1;
  109. }
  110. init.name = name;
  111. init.ops = &hi6220_clkdiv_ops;
  112. init.flags = flags;
  113. init.parent_names = parent_name ? &parent_name : NULL;
  114. init.num_parents = parent_name ? 1 : 0;
  115. /* struct hi6220_clk_divider assignments */
  116. div->reg = reg;
  117. div->shift = shift;
  118. div->width = width;
  119. div->mask = mask_bit ? BIT(mask_bit) : 0;
  120. div->lock = lock;
  121. div->hw.init = &init;
  122. div->table = table;
  123. /* register the clock */
  124. clk = clk_register(dev, &div->hw);
  125. if (IS_ERR(clk)) {
  126. kfree(table);
  127. kfree(div);
  128. }
  129. return clk;
  130. }