clk-smd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 SMD_SOURCE_MAX 2
  18. #define SMD_DIV_SHIFT 8
  19. #define SMD_MAX_DIV 0xf
  20. struct at91sam9x5_clk_smd {
  21. struct clk_hw hw;
  22. struct regmap *regmap;
  23. };
  24. #define to_at91sam9x5_clk_smd(hw) \
  25. container_of(hw, struct at91sam9x5_clk_smd, hw)
  26. static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw *hw,
  27. unsigned long parent_rate)
  28. {
  29. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  30. unsigned int smdr;
  31. u8 smddiv;
  32. regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  33. smddiv = (smdr & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  34. return parent_rate / (smddiv + 1);
  35. }
  36. static long at91sam9x5_clk_smd_round_rate(struct clk_hw *hw, unsigned long rate,
  37. unsigned long *parent_rate)
  38. {
  39. unsigned long div;
  40. unsigned long bestrate;
  41. unsigned long tmp;
  42. if (rate >= *parent_rate)
  43. return *parent_rate;
  44. div = *parent_rate / rate;
  45. if (div > SMD_MAX_DIV)
  46. return *parent_rate / (SMD_MAX_DIV + 1);
  47. bestrate = *parent_rate / div;
  48. tmp = *parent_rate / (div + 1);
  49. if (bestrate - rate > rate - tmp)
  50. bestrate = tmp;
  51. return bestrate;
  52. }
  53. static int at91sam9x5_clk_smd_set_parent(struct clk_hw *hw, u8 index)
  54. {
  55. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  56. if (index > 1)
  57. return -EINVAL;
  58. regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMDS,
  59. index ? AT91_PMC_SMDS : 0);
  60. return 0;
  61. }
  62. static u8 at91sam9x5_clk_smd_get_parent(struct clk_hw *hw)
  63. {
  64. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  65. unsigned int smdr;
  66. regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  67. return smdr & AT91_PMC_SMDS;
  68. }
  69. static int at91sam9x5_clk_smd_set_rate(struct clk_hw *hw, unsigned long rate,
  70. unsigned long parent_rate)
  71. {
  72. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  73. unsigned long div = parent_rate / rate;
  74. if (parent_rate % rate || div < 1 || div > (SMD_MAX_DIV + 1))
  75. return -EINVAL;
  76. regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMD_DIV,
  77. (div - 1) << SMD_DIV_SHIFT);
  78. return 0;
  79. }
  80. static const struct clk_ops at91sam9x5_smd_ops = {
  81. .recalc_rate = at91sam9x5_clk_smd_recalc_rate,
  82. .round_rate = at91sam9x5_clk_smd_round_rate,
  83. .get_parent = at91sam9x5_clk_smd_get_parent,
  84. .set_parent = at91sam9x5_clk_smd_set_parent,
  85. .set_rate = at91sam9x5_clk_smd_set_rate,
  86. };
  87. static struct clk_hw * __init
  88. at91sam9x5_clk_register_smd(struct regmap *regmap, const char *name,
  89. const char **parent_names, u8 num_parents)
  90. {
  91. struct at91sam9x5_clk_smd *smd;
  92. struct clk_hw *hw;
  93. struct clk_init_data init;
  94. int ret;
  95. smd = kzalloc(sizeof(*smd), GFP_KERNEL);
  96. if (!smd)
  97. return ERR_PTR(-ENOMEM);
  98. init.name = name;
  99. init.ops = &at91sam9x5_smd_ops;
  100. init.parent_names = parent_names;
  101. init.num_parents = num_parents;
  102. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  103. smd->hw.init = &init;
  104. smd->regmap = regmap;
  105. hw = &smd->hw;
  106. ret = clk_hw_register(NULL, &smd->hw);
  107. if (ret) {
  108. kfree(smd);
  109. hw = ERR_PTR(ret);
  110. }
  111. return hw;
  112. }
  113. static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  114. {
  115. struct clk_hw *hw;
  116. unsigned int num_parents;
  117. const char *parent_names[SMD_SOURCE_MAX];
  118. const char *name = np->name;
  119. struct regmap *regmap;
  120. num_parents = of_clk_get_parent_count(np);
  121. if (num_parents == 0 || num_parents > SMD_SOURCE_MAX)
  122. return;
  123. of_clk_parent_fill(np, parent_names, num_parents);
  124. of_property_read_string(np, "clock-output-names", &name);
  125. regmap = syscon_node_to_regmap(of_get_parent(np));
  126. if (IS_ERR(regmap))
  127. return;
  128. hw = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  129. num_parents);
  130. if (IS_ERR(hw))
  131. return;
  132. of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  133. }
  134. CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  135. of_at91sam9x5_clk_smd_setup);