clk-system.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 SYSTEM_MAX_ID 31
  18. #define SYSTEM_MAX_NAME_SZ 32
  19. #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
  20. struct clk_system {
  21. struct clk_hw hw;
  22. struct regmap *regmap;
  23. u8 id;
  24. };
  25. static inline int is_pck(int id)
  26. {
  27. return (id >= 8) && (id <= 15);
  28. }
  29. static inline bool clk_system_ready(struct regmap *regmap, int id)
  30. {
  31. unsigned int status;
  32. regmap_read(regmap, AT91_PMC_SR, &status);
  33. return status & (1 << id) ? 1 : 0;
  34. }
  35. static int clk_system_prepare(struct clk_hw *hw)
  36. {
  37. struct clk_system *sys = to_clk_system(hw);
  38. regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
  39. if (!is_pck(sys->id))
  40. return 0;
  41. while (!clk_system_ready(sys->regmap, sys->id))
  42. cpu_relax();
  43. return 0;
  44. }
  45. static void clk_system_unprepare(struct clk_hw *hw)
  46. {
  47. struct clk_system *sys = to_clk_system(hw);
  48. regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
  49. }
  50. static int clk_system_is_prepared(struct clk_hw *hw)
  51. {
  52. struct clk_system *sys = to_clk_system(hw);
  53. unsigned int status;
  54. regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
  55. if (!(status & (1 << sys->id)))
  56. return 0;
  57. if (!is_pck(sys->id))
  58. return 1;
  59. regmap_read(sys->regmap, AT91_PMC_SR, &status);
  60. return status & (1 << sys->id) ? 1 : 0;
  61. }
  62. static const struct clk_ops system_ops = {
  63. .prepare = clk_system_prepare,
  64. .unprepare = clk_system_unprepare,
  65. .is_prepared = clk_system_is_prepared,
  66. };
  67. static struct clk_hw * __init
  68. at91_clk_register_system(struct regmap *regmap, const char *name,
  69. const char *parent_name, u8 id)
  70. {
  71. struct clk_system *sys;
  72. struct clk_hw *hw;
  73. struct clk_init_data init;
  74. int ret;
  75. if (!parent_name || id > SYSTEM_MAX_ID)
  76. return ERR_PTR(-EINVAL);
  77. sys = kzalloc(sizeof(*sys), GFP_KERNEL);
  78. if (!sys)
  79. return ERR_PTR(-ENOMEM);
  80. init.name = name;
  81. init.ops = &system_ops;
  82. init.parent_names = &parent_name;
  83. init.num_parents = 1;
  84. init.flags = CLK_SET_RATE_PARENT;
  85. sys->id = id;
  86. sys->hw.init = &init;
  87. sys->regmap = regmap;
  88. hw = &sys->hw;
  89. ret = clk_hw_register(NULL, &sys->hw);
  90. if (ret) {
  91. kfree(sys);
  92. hw = ERR_PTR(ret);
  93. }
  94. return hw;
  95. }
  96. static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  97. {
  98. int num;
  99. u32 id;
  100. struct clk_hw *hw;
  101. const char *name;
  102. struct device_node *sysclknp;
  103. const char *parent_name;
  104. struct regmap *regmap;
  105. num = of_get_child_count(np);
  106. if (num > (SYSTEM_MAX_ID + 1))
  107. return;
  108. regmap = syscon_node_to_regmap(of_get_parent(np));
  109. if (IS_ERR(regmap))
  110. return;
  111. for_each_child_of_node(np, sysclknp) {
  112. if (of_property_read_u32(sysclknp, "reg", &id))
  113. continue;
  114. if (of_property_read_string(np, "clock-output-names", &name))
  115. name = sysclknp->name;
  116. parent_name = of_clk_get_parent_name(sysclknp, 0);
  117. hw = at91_clk_register_system(regmap, name, parent_name, id);
  118. if (IS_ERR(hw))
  119. continue;
  120. of_clk_add_hw_provider(sysclknp, of_clk_hw_simple_get, hw);
  121. }
  122. }
  123. CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  124. of_at91rm9200_clk_sys_setup);