clk-plldiv.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Boris BREZILLON <[email protected]>
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/clkdev.h>
  7. #include <linux/clk/at91_pmc.h>
  8. #include <linux/of.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/regmap.h>
  11. #include "pmc.h"
  12. #define to_clk_plldiv(hw) container_of(hw, struct clk_plldiv, hw)
  13. struct clk_plldiv {
  14. struct clk_hw hw;
  15. struct regmap *regmap;
  16. };
  17. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  18. unsigned long parent_rate)
  19. {
  20. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  21. unsigned int mckr;
  22. regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr);
  23. if (mckr & AT91_PMC_PLLADIV2)
  24. return parent_rate / 2;
  25. return parent_rate;
  26. }
  27. static long clk_plldiv_round_rate(struct clk_hw *hw, unsigned long rate,
  28. unsigned long *parent_rate)
  29. {
  30. unsigned long div;
  31. if (rate > *parent_rate)
  32. return *parent_rate;
  33. div = *parent_rate / 2;
  34. if (rate < div)
  35. return div;
  36. if (rate - div < *parent_rate - rate)
  37. return div;
  38. return *parent_rate;
  39. }
  40. static int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate,
  41. unsigned long parent_rate)
  42. {
  43. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  44. if ((parent_rate != rate) && (parent_rate / 2 != rate))
  45. return -EINVAL;
  46. regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2,
  47. parent_rate != rate ? AT91_PMC_PLLADIV2 : 0);
  48. return 0;
  49. }
  50. static const struct clk_ops plldiv_ops = {
  51. .recalc_rate = clk_plldiv_recalc_rate,
  52. .round_rate = clk_plldiv_round_rate,
  53. .set_rate = clk_plldiv_set_rate,
  54. };
  55. struct clk_hw * __init
  56. at91_clk_register_plldiv(struct regmap *regmap, const char *name,
  57. const char *parent_name)
  58. {
  59. struct clk_plldiv *plldiv;
  60. struct clk_hw *hw;
  61. struct clk_init_data init;
  62. int ret;
  63. plldiv = kzalloc(sizeof(*plldiv), GFP_KERNEL);
  64. if (!plldiv)
  65. return ERR_PTR(-ENOMEM);
  66. init.name = name;
  67. init.ops = &plldiv_ops;
  68. init.parent_names = parent_name ? &parent_name : NULL;
  69. init.num_parents = parent_name ? 1 : 0;
  70. init.flags = CLK_SET_RATE_GATE;
  71. plldiv->hw.init = &init;
  72. plldiv->regmap = regmap;
  73. hw = &plldiv->hw;
  74. ret = clk_hw_register(NULL, &plldiv->hw);
  75. if (ret) {
  76. kfree(plldiv);
  77. hw = ERR_PTR(ret);
  78. }
  79. return hw;
  80. }