clk-plldiv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/regmap.h>
  16. #include "pmc.h"
  17. #define to_clk_plldiv(hw) container_of(hw, struct clk_plldiv, hw)
  18. struct clk_plldiv {
  19. struct clk_hw hw;
  20. struct regmap *regmap;
  21. };
  22. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  23. unsigned long parent_rate)
  24. {
  25. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  26. unsigned int mckr;
  27. regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr);
  28. if (mckr & AT91_PMC_PLLADIV2)
  29. return parent_rate / 2;
  30. return parent_rate;
  31. }
  32. static long clk_plldiv_round_rate(struct clk_hw *hw, unsigned long rate,
  33. unsigned long *parent_rate)
  34. {
  35. unsigned long div;
  36. if (rate > *parent_rate)
  37. return *parent_rate;
  38. div = *parent_rate / 2;
  39. if (rate < div)
  40. return div;
  41. if (rate - div < *parent_rate - rate)
  42. return div;
  43. return *parent_rate;
  44. }
  45. static int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate,
  46. unsigned long parent_rate)
  47. {
  48. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  49. if ((parent_rate != rate) && (parent_rate / 2 != rate))
  50. return -EINVAL;
  51. regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2,
  52. parent_rate != rate ? AT91_PMC_PLLADIV2 : 0);
  53. return 0;
  54. }
  55. static const struct clk_ops plldiv_ops = {
  56. .recalc_rate = clk_plldiv_recalc_rate,
  57. .round_rate = clk_plldiv_round_rate,
  58. .set_rate = clk_plldiv_set_rate,
  59. };
  60. static struct clk_hw * __init
  61. at91_clk_register_plldiv(struct regmap *regmap, const char *name,
  62. const char *parent_name)
  63. {
  64. struct clk_plldiv *plldiv;
  65. struct clk_hw *hw;
  66. struct clk_init_data init;
  67. int ret;
  68. plldiv = kzalloc(sizeof(*plldiv), GFP_KERNEL);
  69. if (!plldiv)
  70. return ERR_PTR(-ENOMEM);
  71. init.name = name;
  72. init.ops = &plldiv_ops;
  73. init.parent_names = parent_name ? &parent_name : NULL;
  74. init.num_parents = parent_name ? 1 : 0;
  75. init.flags = CLK_SET_RATE_GATE;
  76. plldiv->hw.init = &init;
  77. plldiv->regmap = regmap;
  78. hw = &plldiv->hw;
  79. ret = clk_hw_register(NULL, &plldiv->hw);
  80. if (ret) {
  81. kfree(plldiv);
  82. hw = ERR_PTR(ret);
  83. }
  84. return hw;
  85. }
  86. static void __init
  87. of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  88. {
  89. struct clk_hw *hw;
  90. const char *parent_name;
  91. const char *name = np->name;
  92. struct regmap *regmap;
  93. parent_name = of_clk_get_parent_name(np, 0);
  94. of_property_read_string(np, "clock-output-names", &name);
  95. regmap = syscon_node_to_regmap(of_get_parent(np));
  96. if (IS_ERR(regmap))
  97. return;
  98. hw = at91_clk_register_plldiv(regmap, name, parent_name);
  99. if (IS_ERR(hw))
  100. return;
  101. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  102. }
  103. CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  104. of_at91sam9x5_clk_plldiv_setup);