clk-oxnas.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2010 Broadcom
  3. * Copyright (C) 2012 Stephen Warren
  4. * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk-provider.h>
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/stringify.h>
  24. #include <linux/regmap.h>
  25. #include <linux/mfd/syscon.h>
  26. /* Standard regmap gate clocks */
  27. struct clk_oxnas {
  28. struct clk_hw hw;
  29. signed char bit;
  30. struct regmap *regmap;
  31. };
  32. /* Regmap offsets */
  33. #define CLK_STAT_REGOFFSET 0x24
  34. #define CLK_SET_REGOFFSET 0x2c
  35. #define CLK_CLR_REGOFFSET 0x30
  36. static inline struct clk_oxnas *to_clk_oxnas(struct clk_hw *hw)
  37. {
  38. return container_of(hw, struct clk_oxnas, hw);
  39. }
  40. static int oxnas_clk_is_enabled(struct clk_hw *hw)
  41. {
  42. struct clk_oxnas *std = to_clk_oxnas(hw);
  43. int ret;
  44. unsigned int val;
  45. ret = regmap_read(std->regmap, CLK_STAT_REGOFFSET, &val);
  46. if (ret < 0)
  47. return ret;
  48. return val & BIT(std->bit);
  49. }
  50. static int oxnas_clk_enable(struct clk_hw *hw)
  51. {
  52. struct clk_oxnas *std = to_clk_oxnas(hw);
  53. regmap_write(std->regmap, CLK_SET_REGOFFSET, BIT(std->bit));
  54. return 0;
  55. }
  56. static void oxnas_clk_disable(struct clk_hw *hw)
  57. {
  58. struct clk_oxnas *std = to_clk_oxnas(hw);
  59. regmap_write(std->regmap, CLK_CLR_REGOFFSET, BIT(std->bit));
  60. }
  61. static const struct clk_ops oxnas_clk_ops = {
  62. .enable = oxnas_clk_enable,
  63. .disable = oxnas_clk_disable,
  64. .is_enabled = oxnas_clk_is_enabled,
  65. };
  66. static const char *const oxnas_clk_parents[] = {
  67. "oscillator",
  68. };
  69. static const char *const eth_parents[] = {
  70. "gmacclk",
  71. };
  72. #define DECLARE_STD_CLKP(__clk, __parent) \
  73. static const struct clk_init_data clk_##__clk##_init = { \
  74. .name = __stringify(__clk), \
  75. .ops = &oxnas_clk_ops, \
  76. .parent_names = __parent, \
  77. .num_parents = ARRAY_SIZE(__parent), \
  78. }
  79. #define DECLARE_STD_CLK(__clk) DECLARE_STD_CLKP(__clk, oxnas_clk_parents)
  80. /* Hardware Bit - Clock association */
  81. struct clk_oxnas_init_data {
  82. unsigned long bit;
  83. const struct clk_init_data *clk_init;
  84. };
  85. /* Clk init data declaration */
  86. DECLARE_STD_CLK(leon);
  87. DECLARE_STD_CLK(dma_sgdma);
  88. DECLARE_STD_CLK(cipher);
  89. DECLARE_STD_CLK(sata);
  90. DECLARE_STD_CLK(audio);
  91. DECLARE_STD_CLK(usbmph);
  92. DECLARE_STD_CLKP(etha, eth_parents);
  93. DECLARE_STD_CLK(pciea);
  94. DECLARE_STD_CLK(nand);
  95. /* Table index is clock indice */
  96. static const struct clk_oxnas_init_data clk_oxnas_init[] = {
  97. [0] = {0, &clk_leon_init},
  98. [1] = {1, &clk_dma_sgdma_init},
  99. [2] = {2, &clk_cipher_init},
  100. /* Skip & Do not touch to DDR clock */
  101. [3] = {4, &clk_sata_init},
  102. [4] = {5, &clk_audio_init},
  103. [5] = {6, &clk_usbmph_init},
  104. [6] = {7, &clk_etha_init},
  105. [7] = {8, &clk_pciea_init},
  106. [8] = {9, &clk_nand_init},
  107. };
  108. struct clk_oxnas_data {
  109. struct clk_oxnas clk_oxnas[ARRAY_SIZE(clk_oxnas_init)];
  110. struct clk_onecell_data onecell_data[ARRAY_SIZE(clk_oxnas_init)];
  111. struct clk *clks[ARRAY_SIZE(clk_oxnas_init)];
  112. };
  113. static int oxnas_stdclk_probe(struct platform_device *pdev)
  114. {
  115. struct device_node *np = pdev->dev.of_node;
  116. struct clk_oxnas_data *clk_oxnas;
  117. struct regmap *regmap;
  118. int i;
  119. clk_oxnas = devm_kzalloc(&pdev->dev, sizeof(*clk_oxnas), GFP_KERNEL);
  120. if (!clk_oxnas)
  121. return -ENOMEM;
  122. regmap = syscon_node_to_regmap(of_get_parent(np));
  123. if (IS_ERR(regmap)) {
  124. dev_err(&pdev->dev, "failed to have parent regmap\n");
  125. return PTR_ERR(regmap);
  126. }
  127. for (i = 0; i < ARRAY_SIZE(clk_oxnas_init); i++) {
  128. struct clk_oxnas *_clk;
  129. _clk = &clk_oxnas->clk_oxnas[i];
  130. _clk->bit = clk_oxnas_init[i].bit;
  131. _clk->hw.init = clk_oxnas_init[i].clk_init;
  132. _clk->regmap = regmap;
  133. clk_oxnas->clks[i] =
  134. devm_clk_register(&pdev->dev, &_clk->hw);
  135. if (WARN_ON(IS_ERR(clk_oxnas->clks[i])))
  136. return PTR_ERR(clk_oxnas->clks[i]);
  137. }
  138. clk_oxnas->onecell_data->clks = clk_oxnas->clks;
  139. clk_oxnas->onecell_data->clk_num = ARRAY_SIZE(clk_oxnas_init);
  140. return of_clk_add_provider(np, of_clk_src_onecell_get,
  141. clk_oxnas->onecell_data);
  142. }
  143. static const struct of_device_id oxnas_stdclk_dt_ids[] = {
  144. { .compatible = "oxsemi,ox810se-stdclk" },
  145. { }
  146. };
  147. static struct platform_driver oxnas_stdclk_driver = {
  148. .probe = oxnas_stdclk_probe,
  149. .driver = {
  150. .name = "oxnas-stdclk",
  151. .suppress_bind_attrs = true,
  152. .of_match_table = oxnas_stdclk_dt_ids,
  153. },
  154. };
  155. builtin_platform_driver(oxnas_stdclk_driver);