vid-pll-div.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 BayLibre, SAS.
  4. * Author: Neil Armstrong <[email protected]>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/module.h>
  8. #include "clk-regmap.h"
  9. #include "vid-pll-div.h"
  10. static inline struct meson_vid_pll_div_data *
  11. meson_vid_pll_div_data(struct clk_regmap *clk)
  12. {
  13. return (struct meson_vid_pll_div_data *)clk->data;
  14. }
  15. /*
  16. * This vid_pll divided is a fully programmable fractionnal divider to
  17. * achieve complex video clock rates.
  18. *
  19. * Here are provided the commonly used fraction values provided by Amlogic.
  20. */
  21. struct vid_pll_div {
  22. unsigned int shift_val;
  23. unsigned int shift_sel;
  24. unsigned int divider;
  25. unsigned int multiplier;
  26. };
  27. #define VID_PLL_DIV(_val, _sel, _ft, _fb) \
  28. { \
  29. .shift_val = (_val), \
  30. .shift_sel = (_sel), \
  31. .divider = (_ft), \
  32. .multiplier = (_fb), \
  33. }
  34. static const struct vid_pll_div vid_pll_div_table[] = {
  35. VID_PLL_DIV(0x0aaa, 0, 2, 1), /* 2/1 => /2 */
  36. VID_PLL_DIV(0x5294, 2, 5, 2), /* 5/2 => /2.5 */
  37. VID_PLL_DIV(0x0db6, 0, 3, 1), /* 3/1 => /3 */
  38. VID_PLL_DIV(0x36cc, 1, 7, 2), /* 7/2 => /3.5 */
  39. VID_PLL_DIV(0x6666, 2, 15, 4), /* 15/4 => /3.75 */
  40. VID_PLL_DIV(0x0ccc, 0, 4, 1), /* 4/1 => /4 */
  41. VID_PLL_DIV(0x739c, 2, 5, 1), /* 5/1 => /5 */
  42. VID_PLL_DIV(0x0e38, 0, 6, 1), /* 6/1 => /6 */
  43. VID_PLL_DIV(0x0000, 3, 25, 4), /* 25/4 => /6.25 */
  44. VID_PLL_DIV(0x3c78, 1, 7, 1), /* 7/1 => /7 */
  45. VID_PLL_DIV(0x78f0, 2, 15, 2), /* 15/2 => /7.5 */
  46. VID_PLL_DIV(0x0fc0, 0, 12, 1), /* 12/1 => /12 */
  47. VID_PLL_DIV(0x3f80, 1, 14, 1), /* 14/1 => /14 */
  48. VID_PLL_DIV(0x7f80, 2, 15, 1), /* 15/1 => /15 */
  49. };
  50. #define to_meson_vid_pll_div(_hw) \
  51. container_of(_hw, struct meson_vid_pll_div, hw)
  52. static const struct vid_pll_div *_get_table_val(unsigned int shift_val,
  53. unsigned int shift_sel)
  54. {
  55. int i;
  56. for (i = 0 ; i < ARRAY_SIZE(vid_pll_div_table) ; ++i) {
  57. if (vid_pll_div_table[i].shift_val == shift_val &&
  58. vid_pll_div_table[i].shift_sel == shift_sel)
  59. return &vid_pll_div_table[i];
  60. }
  61. return NULL;
  62. }
  63. static unsigned long meson_vid_pll_div_recalc_rate(struct clk_hw *hw,
  64. unsigned long parent_rate)
  65. {
  66. struct clk_regmap *clk = to_clk_regmap(hw);
  67. struct meson_vid_pll_div_data *pll_div = meson_vid_pll_div_data(clk);
  68. const struct vid_pll_div *div;
  69. div = _get_table_val(meson_parm_read(clk->map, &pll_div->val),
  70. meson_parm_read(clk->map, &pll_div->sel));
  71. if (!div || !div->divider) {
  72. pr_debug("%s: Invalid config value for vid_pll_div\n", __func__);
  73. return 0;
  74. }
  75. return DIV_ROUND_UP_ULL(parent_rate * div->multiplier, div->divider);
  76. }
  77. const struct clk_ops meson_vid_pll_div_ro_ops = {
  78. .recalc_rate = meson_vid_pll_div_recalc_rate,
  79. };
  80. EXPORT_SYMBOL_GPL(meson_vid_pll_div_ro_ops);
  81. MODULE_DESCRIPTION("Amlogic video pll divider driver");
  82. MODULE_AUTHOR("Neil Armstrong <[email protected]>");
  83. MODULE_LICENSE("GPL v2");