clk-tegra-audio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2012, 2013, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/io.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/delay.h>
  21. #include <linux/export.h>
  22. #include <linux/clk/tegra.h>
  23. #include "clk.h"
  24. #include "clk-id.h"
  25. #define AUDIO_SYNC_CLK_I2S0 0x4a0
  26. #define AUDIO_SYNC_CLK_I2S1 0x4a4
  27. #define AUDIO_SYNC_CLK_I2S2 0x4a8
  28. #define AUDIO_SYNC_CLK_I2S3 0x4ac
  29. #define AUDIO_SYNC_CLK_I2S4 0x4b0
  30. #define AUDIO_SYNC_CLK_SPDIF 0x4b4
  31. #define AUDIO_SYNC_CLK_DMIC1 0x560
  32. #define AUDIO_SYNC_CLK_DMIC2 0x564
  33. #define AUDIO_SYNC_CLK_DMIC3 0x6b8
  34. #define AUDIO_SYNC_DOUBLER 0x49c
  35. #define PLLA_OUT 0xb4
  36. struct tegra_sync_source_initdata {
  37. char *name;
  38. unsigned long rate;
  39. unsigned long max_rate;
  40. int clk_id;
  41. };
  42. #define SYNC(_name) \
  43. {\
  44. .name = #_name,\
  45. .clk_id = tegra_clk_ ## _name,\
  46. }
  47. struct tegra_audio_clk_initdata {
  48. char *gate_name;
  49. char *mux_name;
  50. u32 offset;
  51. int gate_clk_id;
  52. int mux_clk_id;
  53. };
  54. #define AUDIO(_name, _offset) \
  55. {\
  56. .gate_name = #_name,\
  57. .mux_name = #_name"_mux",\
  58. .offset = _offset,\
  59. .gate_clk_id = tegra_clk_ ## _name,\
  60. .mux_clk_id = tegra_clk_ ## _name ## _mux,\
  61. }
  62. struct tegra_audio2x_clk_initdata {
  63. char *parent;
  64. char *gate_name;
  65. char *name_2x;
  66. char *div_name;
  67. int clk_id;
  68. int clk_num;
  69. u8 div_offset;
  70. };
  71. #define AUDIO2X(_name, _num, _offset) \
  72. {\
  73. .parent = #_name,\
  74. .gate_name = #_name"_2x",\
  75. .name_2x = #_name"_doubler",\
  76. .div_name = #_name"_div",\
  77. .clk_id = tegra_clk_ ## _name ## _2x,\
  78. .clk_num = _num,\
  79. .div_offset = _offset,\
  80. }
  81. static DEFINE_SPINLOCK(clk_doubler_lock);
  82. static const char * const mux_audio_sync_clk[] = { "spdif_in_sync",
  83. "i2s0_sync", "i2s1_sync", "i2s2_sync", "i2s3_sync", "i2s4_sync",
  84. "pll_a_out0", "vimclk_sync",
  85. };
  86. static const char * const mux_dmic_sync_clk[] = { "unused", "i2s0_sync",
  87. "i2s1_sync", "i2s2_sync", "i2s3_sync", "i2s4_sync", "pll_a_out0",
  88. "vimclk_sync",
  89. };
  90. static struct tegra_sync_source_initdata sync_source_clks[] __initdata = {
  91. SYNC(spdif_in_sync),
  92. SYNC(i2s0_sync),
  93. SYNC(i2s1_sync),
  94. SYNC(i2s2_sync),
  95. SYNC(i2s3_sync),
  96. SYNC(i2s4_sync),
  97. SYNC(vimclk_sync),
  98. };
  99. static struct tegra_audio_clk_initdata audio_clks[] = {
  100. AUDIO(audio0, AUDIO_SYNC_CLK_I2S0),
  101. AUDIO(audio1, AUDIO_SYNC_CLK_I2S1),
  102. AUDIO(audio2, AUDIO_SYNC_CLK_I2S2),
  103. AUDIO(audio3, AUDIO_SYNC_CLK_I2S3),
  104. AUDIO(audio4, AUDIO_SYNC_CLK_I2S4),
  105. AUDIO(spdif, AUDIO_SYNC_CLK_SPDIF),
  106. };
  107. static struct tegra_audio_clk_initdata dmic_clks[] = {
  108. AUDIO(dmic1_sync_clk, AUDIO_SYNC_CLK_DMIC1),
  109. AUDIO(dmic2_sync_clk, AUDIO_SYNC_CLK_DMIC2),
  110. AUDIO(dmic3_sync_clk, AUDIO_SYNC_CLK_DMIC3),
  111. };
  112. static struct tegra_audio2x_clk_initdata audio2x_clks[] = {
  113. AUDIO2X(audio0, 113, 24),
  114. AUDIO2X(audio1, 114, 25),
  115. AUDIO2X(audio2, 115, 26),
  116. AUDIO2X(audio3, 116, 27),
  117. AUDIO2X(audio4, 117, 28),
  118. AUDIO2X(spdif, 118, 29),
  119. };
  120. static void __init tegra_audio_sync_clk_init(void __iomem *clk_base,
  121. struct tegra_clk *tegra_clks,
  122. struct tegra_audio_clk_initdata *sync,
  123. int num_sync_clks,
  124. const char * const *mux_names,
  125. int num_mux_inputs)
  126. {
  127. struct clk *clk;
  128. struct clk **dt_clk;
  129. struct tegra_audio_clk_initdata *data;
  130. int i;
  131. for (i = 0, data = sync; i < num_sync_clks; i++, data++) {
  132. dt_clk = tegra_lookup_dt_id(data->mux_clk_id, tegra_clks);
  133. if (!dt_clk)
  134. continue;
  135. clk = clk_register_mux(NULL, data->mux_name, mux_names,
  136. num_mux_inputs,
  137. CLK_SET_RATE_NO_REPARENT,
  138. clk_base + data->offset, 0, 3, 0,
  139. NULL);
  140. *dt_clk = clk;
  141. dt_clk = tegra_lookup_dt_id(data->gate_clk_id, tegra_clks);
  142. if (!dt_clk)
  143. continue;
  144. clk = clk_register_gate(NULL, data->gate_name, data->mux_name,
  145. 0, clk_base + data->offset, 4,
  146. CLK_GATE_SET_TO_DISABLE, NULL);
  147. *dt_clk = clk;
  148. }
  149. }
  150. void __init tegra_audio_clk_init(void __iomem *clk_base,
  151. void __iomem *pmc_base, struct tegra_clk *tegra_clks,
  152. struct tegra_audio_clk_info *audio_info,
  153. unsigned int num_plls, unsigned long sync_max_rate)
  154. {
  155. struct clk *clk;
  156. struct clk **dt_clk;
  157. int i;
  158. if (!audio_info || num_plls < 1) {
  159. pr_err("No audio data passed to tegra_audio_clk_init\n");
  160. WARN_ON(1);
  161. return;
  162. }
  163. for (i = 0; i < num_plls; i++) {
  164. struct tegra_audio_clk_info *info = &audio_info[i];
  165. dt_clk = tegra_lookup_dt_id(info->clk_id, tegra_clks);
  166. if (dt_clk) {
  167. clk = tegra_clk_register_pll(info->name, info->parent,
  168. clk_base, pmc_base, 0, info->pll_params,
  169. NULL);
  170. *dt_clk = clk;
  171. }
  172. }
  173. /* PLLA_OUT0 */
  174. dt_clk = tegra_lookup_dt_id(tegra_clk_pll_a_out0, tegra_clks);
  175. if (dt_clk) {
  176. clk = tegra_clk_register_divider("pll_a_out0_div", "pll_a",
  177. clk_base + PLLA_OUT, 0, TEGRA_DIVIDER_ROUND_UP,
  178. 8, 8, 1, NULL);
  179. clk = tegra_clk_register_pll_out("pll_a_out0", "pll_a_out0_div",
  180. clk_base + PLLA_OUT, 1, 0, CLK_IGNORE_UNUSED |
  181. CLK_SET_RATE_PARENT, 0, NULL);
  182. *dt_clk = clk;
  183. }
  184. for (i = 0; i < ARRAY_SIZE(sync_source_clks); i++) {
  185. struct tegra_sync_source_initdata *data;
  186. data = &sync_source_clks[i];
  187. dt_clk = tegra_lookup_dt_id(data->clk_id, tegra_clks);
  188. if (!dt_clk)
  189. continue;
  190. clk = tegra_clk_register_sync_source(data->name, sync_max_rate);
  191. *dt_clk = clk;
  192. }
  193. tegra_audio_sync_clk_init(clk_base, tegra_clks, audio_clks,
  194. ARRAY_SIZE(audio_clks), mux_audio_sync_clk,
  195. ARRAY_SIZE(mux_audio_sync_clk));
  196. /* make sure the DMIC sync clocks have a valid parent */
  197. for (i = 0; i < ARRAY_SIZE(dmic_clks); i++)
  198. writel_relaxed(1, clk_base + dmic_clks[i].offset);
  199. tegra_audio_sync_clk_init(clk_base, tegra_clks, dmic_clks,
  200. ARRAY_SIZE(dmic_clks), mux_dmic_sync_clk,
  201. ARRAY_SIZE(mux_dmic_sync_clk));
  202. for (i = 0; i < ARRAY_SIZE(audio2x_clks); i++) {
  203. struct tegra_audio2x_clk_initdata *data;
  204. data = &audio2x_clks[i];
  205. dt_clk = tegra_lookup_dt_id(data->clk_id, tegra_clks);
  206. if (!dt_clk)
  207. continue;
  208. clk = clk_register_fixed_factor(NULL, data->name_2x,
  209. data->parent, CLK_SET_RATE_PARENT, 2, 1);
  210. clk = tegra_clk_register_divider(data->div_name,
  211. data->name_2x, clk_base + AUDIO_SYNC_DOUBLER,
  212. 0, 0, data->div_offset, 1, 0,
  213. &clk_doubler_lock);
  214. clk = tegra_clk_register_periph_gate(data->gate_name,
  215. data->div_name, TEGRA_PERIPH_NO_RESET,
  216. clk_base, CLK_SET_RATE_PARENT, data->clk_num,
  217. periph_clk_enb_refcnt);
  218. *dt_clk = clk;
  219. }
  220. }