clk-lpc18xx-creg.c 5.5 KB

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