clk-h32mx.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * clk-h32mx.c
  3. *
  4. * Copyright (C) 2014 Atmel
  5. *
  6. * Alexandre Belloni <alexandre.belloni@free-electrons.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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/clk-provider.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/clk/at91_pmc.h>
  17. #include <linux/of.h>
  18. #include <linux/regmap.h>
  19. #include <linux/mfd/syscon.h>
  20. #include "pmc.h"
  21. #define H32MX_MAX_FREQ 90000000
  22. struct clk_sama5d4_h32mx {
  23. struct clk_hw hw;
  24. struct regmap *regmap;
  25. };
  26. #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
  27. static unsigned long clk_sama5d4_h32mx_recalc_rate(struct clk_hw *hw,
  28. unsigned long parent_rate)
  29. {
  30. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  31. unsigned int mckr;
  32. regmap_read(h32mxclk->regmap, AT91_PMC_MCKR, &mckr);
  33. if (mckr & AT91_PMC_H32MXDIV)
  34. return parent_rate / 2;
  35. if (parent_rate > H32MX_MAX_FREQ)
  36. pr_warn("H32MX clock is too fast\n");
  37. return parent_rate;
  38. }
  39. static long clk_sama5d4_h32mx_round_rate(struct clk_hw *hw, unsigned long rate,
  40. unsigned long *parent_rate)
  41. {
  42. unsigned long div;
  43. if (rate > *parent_rate)
  44. return *parent_rate;
  45. div = *parent_rate / 2;
  46. if (rate < div)
  47. return div;
  48. if (rate - div < *parent_rate - rate)
  49. return div;
  50. return *parent_rate;
  51. }
  52. static int clk_sama5d4_h32mx_set_rate(struct clk_hw *hw, unsigned long rate,
  53. unsigned long parent_rate)
  54. {
  55. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  56. u32 mckr = 0;
  57. if (parent_rate != rate && (parent_rate / 2) != rate)
  58. return -EINVAL;
  59. if ((parent_rate / 2) == rate)
  60. mckr = AT91_PMC_H32MXDIV;
  61. regmap_update_bits(h32mxclk->regmap, AT91_PMC_MCKR,
  62. AT91_PMC_H32MXDIV, mckr);
  63. return 0;
  64. }
  65. static const struct clk_ops h32mx_ops = {
  66. .recalc_rate = clk_sama5d4_h32mx_recalc_rate,
  67. .round_rate = clk_sama5d4_h32mx_round_rate,
  68. .set_rate = clk_sama5d4_h32mx_set_rate,
  69. };
  70. static void __init of_sama5d4_clk_h32mx_setup(struct device_node *np)
  71. {
  72. struct clk_sama5d4_h32mx *h32mxclk;
  73. struct clk_init_data init;
  74. const char *parent_name;
  75. struct regmap *regmap;
  76. int ret;
  77. regmap = syscon_node_to_regmap(of_get_parent(np));
  78. if (IS_ERR(regmap))
  79. return;
  80. h32mxclk = kzalloc(sizeof(*h32mxclk), GFP_KERNEL);
  81. if (!h32mxclk)
  82. return;
  83. parent_name = of_clk_get_parent_name(np, 0);
  84. init.name = np->name;
  85. init.ops = &h32mx_ops;
  86. init.parent_names = parent_name ? &parent_name : NULL;
  87. init.num_parents = parent_name ? 1 : 0;
  88. init.flags = CLK_SET_RATE_GATE;
  89. h32mxclk->hw.init = &init;
  90. h32mxclk->regmap = regmap;
  91. ret = clk_hw_register(NULL, &h32mxclk->hw);
  92. if (ret) {
  93. kfree(h32mxclk);
  94. return;
  95. }
  96. of_clk_add_hw_provider(np, of_clk_hw_simple_get, &h32mxclk->hw);
  97. }
  98. CLK_OF_DECLARE(of_sama5d4_clk_h32mx_setup, "atmel,sama5d4-clk-h32mx",
  99. of_sama5d4_clk_h32mx_setup);