clk-lpc18xx-creg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Clk driver for NXP LPC18xx/43xx Configuration Registers (CREG)
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/delay.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #define LPC18XX_CREG_CREG0 0x004
  18. #define LPC18XX_CREG_CREG0_EN1KHZ BIT(0)
  19. #define LPC18XX_CREG_CREG0_EN32KHZ BIT(1)
  20. #define LPC18XX_CREG_CREG0_RESET32KHZ BIT(2)
  21. #define LPC18XX_CREG_CREG0_PD32KHZ BIT(3)
  22. #define to_clk_creg(_hw) container_of(_hw, struct clk_creg_data, hw)
  23. enum {
  24. CREG_CLK_1KHZ,
  25. CREG_CLK_32KHZ,
  26. CREG_CLK_MAX,
  27. };
  28. struct clk_creg_data {
  29. struct clk_hw hw;
  30. const char *name;
  31. struct regmap *reg;
  32. unsigned int en_mask;
  33. const struct clk_ops *ops;
  34. };
  35. #define CREG_CLK(_name, _emask, _ops) \
  36. { \
  37. .name = _name, \
  38. .en_mask = LPC18XX_CREG_CREG0_##_emask, \
  39. .ops = &_ops, \
  40. }
  41. static int clk_creg_32k_prepare(struct clk_hw *hw)
  42. {
  43. struct clk_creg_data *creg = to_clk_creg(hw);
  44. int ret;
  45. ret = regmap_update_bits(creg->reg, LPC18XX_CREG_CREG0,
  46. LPC18XX_CREG_CREG0_PD32KHZ |
  47. LPC18XX_CREG_CREG0_RESET32KHZ, 0);
  48. /*
  49. * Powering up the 32k oscillator takes a long while
  50. * and sadly there aren't any status bit to poll.
  51. */
  52. msleep(2500);
  53. return ret;
  54. }
  55. static void clk_creg_32k_unprepare(struct clk_hw *hw)
  56. {
  57. struct clk_creg_data *creg = to_clk_creg(hw);
  58. regmap_update_bits(creg->reg, LPC18XX_CREG_CREG0,
  59. LPC18XX_CREG_CREG0_PD32KHZ,
  60. LPC18XX_CREG_CREG0_PD32KHZ);
  61. }
  62. static int clk_creg_32k_is_prepared(struct clk_hw *hw)
  63. {
  64. struct clk_creg_data *creg = to_clk_creg(hw);
  65. u32 reg;
  66. regmap_read(creg->reg, LPC18XX_CREG_CREG0, &reg);
  67. return !(reg & LPC18XX_CREG_CREG0_PD32KHZ) &&
  68. !(reg & LPC18XX_CREG_CREG0_RESET32KHZ);
  69. }
  70. static unsigned long clk_creg_1k_recalc_rate(struct clk_hw *hw,
  71. unsigned long parent_rate)
  72. {
  73. return parent_rate / 32;
  74. }
  75. static int clk_creg_enable(struct clk_hw *hw)
  76. {
  77. struct clk_creg_data *creg = to_clk_creg(hw);
  78. return regmap_update_bits(creg->reg, LPC18XX_CREG_CREG0,
  79. creg->en_mask, creg->en_mask);
  80. }
  81. static void clk_creg_disable(struct clk_hw *hw)
  82. {
  83. struct clk_creg_data *creg = to_clk_creg(hw);
  84. regmap_update_bits(creg->reg, LPC18XX_CREG_CREG0,
  85. creg->en_mask, 0);
  86. }
  87. static int clk_creg_is_enabled(struct clk_hw *hw)
  88. {
  89. struct clk_creg_data *creg = to_clk_creg(hw);
  90. u32 reg;
  91. regmap_read(creg->reg, LPC18XX_CREG_CREG0, &reg);
  92. return !!(reg & creg->en_mask);
  93. }
  94. static const struct clk_ops clk_creg_32k = {
  95. .enable = clk_creg_enable,
  96. .disable = clk_creg_disable,
  97. .is_enabled = clk_creg_is_enabled,
  98. .prepare = clk_creg_32k_prepare,
  99. .unprepare = clk_creg_32k_unprepare,
  100. .is_prepared = clk_creg_32k_is_prepared,
  101. };
  102. static const struct clk_ops clk_creg_1k = {
  103. .enable = clk_creg_enable,
  104. .disable = clk_creg_disable,
  105. .is_enabled = clk_creg_is_enabled,
  106. .recalc_rate = clk_creg_1k_recalc_rate,
  107. };
  108. static struct clk_creg_data clk_creg_clocks[] = {
  109. [CREG_CLK_1KHZ] = CREG_CLK("1khz_clk", EN1KHZ, clk_creg_1k),
  110. [CREG_CLK_32KHZ] = CREG_CLK("32khz_clk", EN32KHZ, clk_creg_32k),
  111. };
  112. static struct clk *clk_register_creg_clk(struct device *dev,
  113. struct clk_creg_data *creg_clk,
  114. const char **parent_name,
  115. struct regmap *syscon)
  116. {
  117. struct clk_init_data init;
  118. init.ops = creg_clk->ops;
  119. init.name = creg_clk->name;
  120. init.parent_names = parent_name;
  121. init.num_parents = 1;
  122. init.flags = 0;
  123. creg_clk->reg = syscon;
  124. creg_clk->hw.init = &init;
  125. if (dev)
  126. return devm_clk_register(dev, &creg_clk->hw);
  127. return clk_register(NULL, &creg_clk->hw);
  128. }
  129. static struct clk *clk_creg_early[CREG_CLK_MAX];
  130. static struct clk_onecell_data clk_creg_early_data = {
  131. .clks = clk_creg_early,
  132. .clk_num = CREG_CLK_MAX,
  133. };
  134. static void __init lpc18xx_creg_clk_init(struct device_node *np)
  135. {
  136. const char *clk_32khz_parent;
  137. struct regmap *syscon;
  138. syscon = syscon_node_to_regmap(np->parent);
  139. if (IS_ERR(syscon)) {
  140. pr_err("%s: syscon lookup failed\n", __func__);
  141. return;
  142. }
  143. clk_32khz_parent = of_clk_get_parent_name(np, 0);
  144. clk_creg_early[CREG_CLK_32KHZ] =
  145. clk_register_creg_clk(NULL, &clk_creg_clocks[CREG_CLK_32KHZ],
  146. &clk_32khz_parent, syscon);
  147. clk_creg_early[CREG_CLK_1KHZ] = ERR_PTR(-EPROBE_DEFER);
  148. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_creg_early_data);
  149. }
  150. CLK_OF_DECLARE_DRIVER(lpc18xx_creg_clk, "nxp,lpc1850-creg-clk",
  151. lpc18xx_creg_clk_init);
  152. static struct clk *clk_creg[CREG_CLK_MAX];
  153. static struct clk_onecell_data clk_creg_data = {
  154. .clks = clk_creg,
  155. .clk_num = CREG_CLK_MAX,
  156. };
  157. static int lpc18xx_creg_clk_probe(struct platform_device *pdev)
  158. {
  159. struct device_node *np = pdev->dev.of_node;
  160. struct regmap *syscon;
  161. syscon = syscon_node_to_regmap(np->parent);
  162. if (IS_ERR(syscon)) {
  163. dev_err(&pdev->dev, "syscon lookup failed\n");
  164. return PTR_ERR(syscon);
  165. }
  166. clk_creg[CREG_CLK_32KHZ] = clk_creg_early[CREG_CLK_32KHZ];
  167. clk_creg[CREG_CLK_1KHZ] =
  168. clk_register_creg_clk(NULL, &clk_creg_clocks[CREG_CLK_1KHZ],
  169. &clk_creg_clocks[CREG_CLK_32KHZ].name,
  170. syscon);
  171. return of_clk_add_provider(np, of_clk_src_onecell_get, &clk_creg_data);
  172. }
  173. static const struct of_device_id lpc18xx_creg_clk_of_match[] = {
  174. { .compatible = "nxp,lpc1850-creg-clk" },
  175. {},
  176. };
  177. static struct platform_driver lpc18xx_creg_clk_driver = {
  178. .probe = lpc18xx_creg_clk_probe,
  179. .driver = {
  180. .name = "lpc18xx-creg-clk",
  181. .of_match_table = lpc18xx_creg_clk_of_match,
  182. },
  183. };
  184. builtin_platform_driver(lpc18xx_creg_clk_driver);