sun4i_hdmi_ddc_clk.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2016 Free Electrons
  3. * Copyright (C) 2016 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.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 as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/regmap.h>
  14. #include "sun4i_hdmi.h"
  15. struct sun4i_ddc {
  16. struct clk_hw hw;
  17. struct sun4i_hdmi *hdmi;
  18. struct regmap_field *reg;
  19. u8 pre_div;
  20. u8 m_offset;
  21. };
  22. static inline struct sun4i_ddc *hw_to_ddc(struct clk_hw *hw)
  23. {
  24. return container_of(hw, struct sun4i_ddc, hw);
  25. }
  26. static unsigned long sun4i_ddc_calc_divider(unsigned long rate,
  27. unsigned long parent_rate,
  28. const u8 pre_div,
  29. const u8 m_offset,
  30. u8 *m, u8 *n)
  31. {
  32. unsigned long best_rate = 0;
  33. u8 best_m = 0, best_n = 0, _m, _n;
  34. for (_m = 0; _m < 8; _m++) {
  35. for (_n = 0; _n < 8; _n++) {
  36. unsigned long tmp_rate;
  37. tmp_rate = (((parent_rate / pre_div) / 10) >> _n) /
  38. (_m + m_offset);
  39. if (tmp_rate > rate)
  40. continue;
  41. if (abs(rate - tmp_rate) < abs(rate - best_rate)) {
  42. best_rate = tmp_rate;
  43. best_m = _m;
  44. best_n = _n;
  45. }
  46. }
  47. }
  48. if (m && n) {
  49. *m = best_m;
  50. *n = best_n;
  51. }
  52. return best_rate;
  53. }
  54. static long sun4i_ddc_round_rate(struct clk_hw *hw, unsigned long rate,
  55. unsigned long *prate)
  56. {
  57. struct sun4i_ddc *ddc = hw_to_ddc(hw);
  58. return sun4i_ddc_calc_divider(rate, *prate, ddc->pre_div,
  59. ddc->m_offset, NULL, NULL);
  60. }
  61. static unsigned long sun4i_ddc_recalc_rate(struct clk_hw *hw,
  62. unsigned long parent_rate)
  63. {
  64. struct sun4i_ddc *ddc = hw_to_ddc(hw);
  65. unsigned int reg;
  66. u8 m, n;
  67. regmap_field_read(ddc->reg, &reg);
  68. m = (reg >> 3) & 0xf;
  69. n = reg & 0x7;
  70. return (((parent_rate / ddc->pre_div) / 10) >> n) /
  71. (m + ddc->m_offset);
  72. }
  73. static int sun4i_ddc_set_rate(struct clk_hw *hw, unsigned long rate,
  74. unsigned long parent_rate)
  75. {
  76. struct sun4i_ddc *ddc = hw_to_ddc(hw);
  77. u8 div_m, div_n;
  78. sun4i_ddc_calc_divider(rate, parent_rate, ddc->pre_div,
  79. ddc->m_offset, &div_m, &div_n);
  80. regmap_field_write(ddc->reg,
  81. SUN4I_HDMI_DDC_CLK_M(div_m) |
  82. SUN4I_HDMI_DDC_CLK_N(div_n));
  83. return 0;
  84. }
  85. static const struct clk_ops sun4i_ddc_ops = {
  86. .recalc_rate = sun4i_ddc_recalc_rate,
  87. .round_rate = sun4i_ddc_round_rate,
  88. .set_rate = sun4i_ddc_set_rate,
  89. };
  90. int sun4i_ddc_create(struct sun4i_hdmi *hdmi, struct clk *parent)
  91. {
  92. struct clk_init_data init;
  93. struct sun4i_ddc *ddc;
  94. const char *parent_name;
  95. parent_name = __clk_get_name(parent);
  96. if (!parent_name)
  97. return -ENODEV;
  98. ddc = devm_kzalloc(hdmi->dev, sizeof(*ddc), GFP_KERNEL);
  99. if (!ddc)
  100. return -ENOMEM;
  101. ddc->reg = devm_regmap_field_alloc(hdmi->dev, hdmi->regmap,
  102. hdmi->variant->ddc_clk_reg);
  103. if (IS_ERR(ddc->reg))
  104. return PTR_ERR(ddc->reg);
  105. init.name = "hdmi-ddc";
  106. init.ops = &sun4i_ddc_ops;
  107. init.parent_names = &parent_name;
  108. init.num_parents = 1;
  109. ddc->hdmi = hdmi;
  110. ddc->hw.init = &init;
  111. ddc->pre_div = hdmi->variant->ddc_clk_pre_divider;
  112. ddc->m_offset = hdmi->variant->ddc_clk_m_offset;
  113. hdmi->ddc_clk = devm_clk_register(hdmi->dev, &ddc->hw);
  114. if (IS_ERR(hdmi->ddc_clk))
  115. return PTR_ERR(hdmi->ddc_clk);
  116. return 0;
  117. }