clocking.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Audio Clocking
  2. ==============
  3. This text describes the audio clocking terms in ASoC and digital audio in
  4. general. Note: Audio clocking can be complex !
  5. Master Clock
  6. ------------
  7. Every audio subsystem is driven by a master clock (sometimes refered to as MCLK
  8. or SYSCLK). This audio master clock can be derived from a number of sources
  9. (e.g. crystal, PLL, CPU clock) and is responsible for producing the correct
  10. audio playback and capture sample rates.
  11. Some master clocks (e.g. PLL's and CPU based clocks) are configuarble in that
  12. their speed can be altered by software (depending on the system use and to save
  13. power). Other master clocks are fixed at at set frequency (i.e. crystals).
  14. DAI Clocks
  15. ----------
  16. The Digital Audio Interface is usually driven by a Bit Clock (often referred to
  17. as BCLK). This clock is used to drive the digital audio data across the link
  18. between the codec and CPU.
  19. The DAI also has a frame clock to signal the start of each audio frame. This
  20. clock is sometimes referred to as LRC (left right clock) or FRAME. This clock
  21. runs at exactly the sample rate.
  22. Bit Clock is usually always a ratio of MCLK or a multiple of LRC. i.e.
  23. BCLK = MCLK / x
  24. or
  25. BCLK = LRC * x
  26. This relationship depends on the codec or SoC CPU in particular. ASoC can quite
  27. easily match a codec that generates BCLK by division (FSBD) with a CPU that
  28. generates BCLK by multiplication (FSB).
  29. ASoC Clocking
  30. -------------
  31. The ASoC core determines the clocking for each particular configuration at
  32. runtime. This is to allow for dynamic audio clocking wereby the audio clock is
  33. variable and depends on the system state or device usage scenario. i.e. a voice
  34. call requires slower clocks (and hence less power) than MP3 playback.
  35. ASoC will call the config_sysclock() function for the target machine during the
  36. audio parameters configuration. The function is responsible for then clocking
  37. the machine audio subsytem and returning the audio clock speed to the core.
  38. This function should also call the codec and cpu DAI clock_config() functions
  39. to configure their respective internal clocking if required.
  40. ASoC Clocking Control Flow
  41. --------------------------
  42. The ASoC core will call the machine drivers config_sysclock() when most of the
  43. DAI capabilities are known. The machine driver is then responsible for calling
  44. the codec and/or CPU DAI drivers with the selected capabilities and the current
  45. MCLK. Note that the machine driver is also resonsible for setting the MCLK (and
  46. enabling it).
  47. (1) Match Codec and CPU DAI capabilities. At this point we have
  48. matched the majority of the DAI fields and now need to make sure this
  49. mode is currently clockable.
  50. (2) machine->config_sysclk() is now called with the matched DAI FS, sample
  51. rate and BCLK master. This function then gets/sets the current audio
  52. clock (depening on usage) and calls the codec and CPUI DAI drivers with
  53. the FS, rate, BCLK master and MCLK.
  54. (3) Codec/CPU DAI config_sysclock(). This function checks that the FS, rate,
  55. BCLK master and MCLK are acceptable for the codec or CPU DAI. It also
  56. sets the DAI internal state to work with said clocks.
  57. The config_sysclk() functions for CPU, codec and machine should return the MCLK
  58. on success and 0 on failure.
  59. Examples (b = BCLK, l = LRC)
  60. ============================
  61. Example 1
  62. ---------
  63. Simple codec that only runs at 48k @ 256FS in master mode.
  64. CPU only runs as slave DAI, however it generates a variable MCLK.
  65. -------- ---------
  66. | | <----mclk--- | |
  67. | Codec |b -----------> | CPU |
  68. | |l -----------> | |
  69. | | | |
  70. -------- ---------
  71. The codec driver has the following config_sysclock()
  72. static unsigned int config_sysclk(struct snd_soc_codec_dai *dai,
  73. struct snd_soc_clock_info *info, unsigned int clk)
  74. {
  75. /* make sure clock is 256 * rate */
  76. if(info->rate << 8 == clk) {
  77. dai->mclk = clk;
  78. return clk;
  79. }
  80. return 0;
  81. }
  82. The CPU I2S DAI driver has the following config_sysclk()
  83. static unsigned int config_sysclk(struct snd_soc_codec_dai *dai,
  84. struct snd_soc_clock_info *info, unsigned int clk)
  85. {
  86. /* can we support this clk */
  87. if(set_audio_clk(clk) < 0)
  88. return -EINVAL;
  89. dai->mclk = clk;
  90. return dai->clk;
  91. }
  92. The machine driver config_sysclk() in this example is as follows:-
  93. unsigned int machine_config_sysclk(struct snd_soc_pcm_runtime *rtd,
  94. struct snd_soc_clock_info *info)
  95. {
  96. int clk = info->rate * info->fs;
  97. /* check that CPU can deliver clock */
  98. if(rtd->cpu_dai->config_sysclk(rtd->cpu_dai, info, clk) < 0)
  99. return -EINVAL;
  100. /* can codec work with this clock */
  101. return rtd->codec_dai->config_sysclk(rtd->codec_dai, info, clk);
  102. }
  103. Example 2
  104. ---------
  105. Codec that can master at 8k and 48k at various FS (and hence supports a fixed
  106. set of input MCLK's) and can also be slave at various FS .
  107. The CPU can master at 8k and 48k @256 FS and can be slave at any FS.
  108. MCLK is a 12.288MHz crystal on this machine.
  109. -------- ---------
  110. | | <---xtal---> | |
  111. | Codec |b <----------> | CPU |
  112. | |l <----------> | |
  113. | | | |
  114. -------- ---------
  115. The codec driver has the following config_sysclock()
  116. /* supported input clocks */
  117. const static int hifi_clks[] = {11289600, 12000000, 12288000,
  118. 16934400, 18432000};
  119. static unsigned int config_hsysclk(struct snd_soc_codec_dai *dai,
  120. struct snd_soc_clock_info *info, unsigned int clk)
  121. {
  122. int i;
  123. /* is clk supported */
  124. for(i = 0; i < ARRAY_SIZE(hifi_clks); i++) {
  125. if(clk == hifi_clks[i]) {
  126. dai->mclk = clk;
  127. return clk;
  128. }
  129. }
  130. /* this clk is not supported */
  131. return 0;
  132. }
  133. The CPU I2S DAI driver has the following config_sysclk()
  134. static unsigned int config_sysclk(struct snd_soc_codec_dai *dai,
  135. struct snd_soc_clock_info *info, unsigned int clk)
  136. {
  137. /* are we master or slave */
  138. if (info->bclk_master &
  139. (SND_SOC_DAIFMT_CBM_CFM | SND_SOC_DAIFMT_CBM_CFS)) {
  140. /* we can only master @ 256FS */
  141. if(info->rate << 8 == clk) {
  142. dai->mclk = clk;
  143. return dai->mclk;
  144. }
  145. } else {
  146. /* slave we can run at any FS */
  147. dai->mclk = clk;
  148. return dai->mclk;
  149. }
  150. /* not supported */
  151. return dai->clk;
  152. }
  153. The machine driver config_sysclk() in this example is as follows:-
  154. unsigned int machine_config_sysclk(struct snd_soc_pcm_runtime *rtd,
  155. struct snd_soc_clock_info *info)
  156. {
  157. int clk = 12288000; /* 12.288MHz */
  158. /* who's driving the link */
  159. if (info->bclk_master &
  160. (SND_SOC_DAIFMT_CBM_CFM | SND_SOC_DAIFMT_CBM_CFS)) {
  161. /* codec master */
  162. /* check that CPU can work with clock */
  163. if(rtd->cpu_dai->config_sysclk(rtd->cpu_dai, info, clk) < 0)
  164. return -EINVAL;
  165. /* can codec work with this clock */
  166. return rtd->codec_dai->config_sysclk(rtd->codec_dai, info, clk);
  167. } else {
  168. /* cpu master */
  169. /* check that codec can work with clock */
  170. if(rtd->codec_dai->config_sysclk(rtd->codec_dai, info, clk) < 0)
  171. return -EINVAL;
  172. /* can CPU work with this clock */
  173. return rtd->cpu_dai->config_sysclk(rtd->cpu_dai, info, clk);
  174. }
  175. }
  176. Example 3
  177. ---------
  178. Codec that masters at 8k ... 48k @256 FS. Codec can also be slave and
  179. doesn't care about FS. The codec has an internal PLL and dividers to generate
  180. the necessary internal clocks (for 256FS).
  181. CPU can only be slave and doesn't care about FS.
  182. MCLK is a non controllable 13MHz clock from the CPU.
  183. -------- ---------
  184. | | <----mclk--- | |
  185. | Codec |b <----------> | CPU |
  186. | |l <----------> | |
  187. | | | |
  188. -------- ---------
  189. The codec driver has the following config_sysclock()
  190. /* valid PCM clock dividers * 2 */
  191. static int pcm_divs[] = {2, 6, 11, 4, 8, 12, 16};
  192. static unsigned int config_vsysclk(struct snd_soc_codec_dai *dai,
  193. struct snd_soc_clock_info *info, unsigned int clk)
  194. {
  195. int i, j, best_clk = info->fs * info->rate;
  196. /* can we run at this clk without the PLL ? */
  197. for (i = 0; i < ARRAY_SIZE(pcm_divs); i++) {
  198. if ((best_clk >> 1) * pcm_divs[i] == clk) {
  199. dai->pll_in = 0;
  200. dai->clk_div = pcm_divs[i];
  201. dai->mclk = best_clk;
  202. return dai->mclk;
  203. }
  204. }
  205. /* now check for PLL support */
  206. for (i = 0; i < ARRAY_SIZE(pll_div); i++) {
  207. if (pll_div[i].pll_in == clk) {
  208. for (j = 0; j < ARRAY_SIZE(pcm_divs); j++) {
  209. if (pll_div[i].pll_out == pcm_divs[j] * (best_clk >> 1)) {
  210. dai->pll_in = clk;
  211. dai->pll_out = pll_div[i].pll_out;
  212. dai->clk_div = pcm_divs[j];
  213. dai->mclk = best_clk;
  214. return dai->mclk;
  215. }
  216. }
  217. }
  218. }
  219. /* this clk is not supported */
  220. return 0;
  221. }
  222. The CPU I2S DAI driver has the does not need a config_sysclk() as it can slave
  223. at any FS.
  224. unsigned int config_sysclk(struct snd_soc_pcm_runtime *rtd,
  225. struct snd_soc_clock_info *info)
  226. {
  227. /* codec has pll that generates mclk from 13MHz xtal */
  228. return rtd->codec_dai->config_sysclk(rtd->codec_dai, info, 13000000);
  229. }