aiu-encoder-spdif.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <[email protected]>
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <sound/pcm_params.h>
  8. #include <sound/pcm_iec958.h>
  9. #include <sound/soc.h>
  10. #include <sound/soc-dai.h>
  11. #include "aiu.h"
  12. #define AIU_958_MISC_NON_PCM BIT(0)
  13. #define AIU_958_MISC_MODE_16BITS BIT(1)
  14. #define AIU_958_MISC_16BITS_ALIGN GENMASK(6, 5)
  15. #define AIU_958_MISC_MODE_32BITS BIT(7)
  16. #define AIU_958_MISC_U_FROM_STREAM BIT(12)
  17. #define AIU_958_MISC_FORCE_LR BIT(13)
  18. #define AIU_958_CTRL_HOLD_EN BIT(0)
  19. #define AIU_CLK_CTRL_958_DIV_EN BIT(1)
  20. #define AIU_CLK_CTRL_958_DIV GENMASK(5, 4)
  21. #define AIU_CLK_CTRL_958_DIV_MORE BIT(12)
  22. #define AIU_CS_WORD_LEN 4
  23. #define AIU_958_INTERNAL_DIV 2
  24. static void
  25. aiu_encoder_spdif_divider_enable(struct snd_soc_component *component,
  26. bool enable)
  27. {
  28. snd_soc_component_update_bits(component, AIU_CLK_CTRL,
  29. AIU_CLK_CTRL_958_DIV_EN,
  30. enable ? AIU_CLK_CTRL_958_DIV_EN : 0);
  31. }
  32. static void aiu_encoder_spdif_hold(struct snd_soc_component *component,
  33. bool enable)
  34. {
  35. snd_soc_component_update_bits(component, AIU_958_CTRL,
  36. AIU_958_CTRL_HOLD_EN,
  37. enable ? AIU_958_CTRL_HOLD_EN : 0);
  38. }
  39. static int
  40. aiu_encoder_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  41. struct snd_soc_dai *dai)
  42. {
  43. struct snd_soc_component *component = dai->component;
  44. switch (cmd) {
  45. case SNDRV_PCM_TRIGGER_START:
  46. case SNDRV_PCM_TRIGGER_RESUME:
  47. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  48. aiu_encoder_spdif_hold(component, false);
  49. return 0;
  50. case SNDRV_PCM_TRIGGER_STOP:
  51. case SNDRV_PCM_TRIGGER_SUSPEND:
  52. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  53. aiu_encoder_spdif_hold(component, true);
  54. return 0;
  55. default:
  56. return -EINVAL;
  57. }
  58. }
  59. static int aiu_encoder_spdif_setup_cs_word(struct snd_soc_component *component,
  60. struct snd_pcm_hw_params *params)
  61. {
  62. u8 cs[AIU_CS_WORD_LEN];
  63. unsigned int val;
  64. int ret;
  65. ret = snd_pcm_create_iec958_consumer_hw_params(params, cs,
  66. AIU_CS_WORD_LEN);
  67. if (ret < 0)
  68. return ret;
  69. /* Write the 1st half word */
  70. val = cs[1] | cs[0] << 8;
  71. snd_soc_component_write(component, AIU_958_CHSTAT_L0, val);
  72. snd_soc_component_write(component, AIU_958_CHSTAT_R0, val);
  73. /* Write the 2nd half word */
  74. val = cs[3] | cs[2] << 8;
  75. snd_soc_component_write(component, AIU_958_CHSTAT_L1, val);
  76. snd_soc_component_write(component, AIU_958_CHSTAT_R1, val);
  77. return 0;
  78. }
  79. static int aiu_encoder_spdif_hw_params(struct snd_pcm_substream *substream,
  80. struct snd_pcm_hw_params *params,
  81. struct snd_soc_dai *dai)
  82. {
  83. struct snd_soc_component *component = dai->component;
  84. struct aiu *aiu = snd_soc_component_get_drvdata(component);
  85. unsigned int val = 0, mrate;
  86. int ret;
  87. /* Disable the clock while changing the settings */
  88. aiu_encoder_spdif_divider_enable(component, false);
  89. switch (params_physical_width(params)) {
  90. case 16:
  91. val |= AIU_958_MISC_MODE_16BITS;
  92. val |= FIELD_PREP(AIU_958_MISC_16BITS_ALIGN, 2);
  93. break;
  94. case 32:
  95. val |= AIU_958_MISC_MODE_32BITS;
  96. break;
  97. default:
  98. dev_err(dai->dev, "Unsupported physical width\n");
  99. return -EINVAL;
  100. }
  101. snd_soc_component_update_bits(component, AIU_958_MISC,
  102. AIU_958_MISC_NON_PCM |
  103. AIU_958_MISC_MODE_16BITS |
  104. AIU_958_MISC_16BITS_ALIGN |
  105. AIU_958_MISC_MODE_32BITS |
  106. AIU_958_MISC_FORCE_LR |
  107. AIU_958_MISC_U_FROM_STREAM,
  108. val);
  109. /* Set the stream channel status word */
  110. ret = aiu_encoder_spdif_setup_cs_word(component, params);
  111. if (ret) {
  112. dev_err(dai->dev, "failed to set channel status word\n");
  113. return ret;
  114. }
  115. snd_soc_component_update_bits(component, AIU_CLK_CTRL,
  116. AIU_CLK_CTRL_958_DIV |
  117. AIU_CLK_CTRL_958_DIV_MORE,
  118. FIELD_PREP(AIU_CLK_CTRL_958_DIV,
  119. __ffs(AIU_958_INTERNAL_DIV)));
  120. /* 2 * 32bits per subframe * 2 channels = 128 */
  121. mrate = params_rate(params) * 128 * AIU_958_INTERNAL_DIV;
  122. ret = clk_set_rate(aiu->spdif.clks[MCLK].clk, mrate);
  123. if (ret) {
  124. dev_err(dai->dev, "failed to set mclk rate\n");
  125. return ret;
  126. }
  127. aiu_encoder_spdif_divider_enable(component, true);
  128. return 0;
  129. }
  130. static int aiu_encoder_spdif_hw_free(struct snd_pcm_substream *substream,
  131. struct snd_soc_dai *dai)
  132. {
  133. struct snd_soc_component *component = dai->component;
  134. aiu_encoder_spdif_divider_enable(component, false);
  135. return 0;
  136. }
  137. static int aiu_encoder_spdif_startup(struct snd_pcm_substream *substream,
  138. struct snd_soc_dai *dai)
  139. {
  140. struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
  141. int ret;
  142. /*
  143. * NOTE: Make sure the spdif block is on its own divider.
  144. *
  145. * The spdif can be clocked by the i2s master clock or its own
  146. * clock. We should (in theory) change the source depending on the
  147. * origin of the data.
  148. *
  149. * However, considering the clocking scheme used on these platforms,
  150. * the master clocks will pick the same PLL source when they are
  151. * playing from the same FIFO. The clock should be in sync so, it
  152. * should not be necessary to reparent the spdif master clock.
  153. */
  154. ret = clk_set_parent(aiu->spdif.clks[MCLK].clk,
  155. aiu->spdif_mclk);
  156. if (ret)
  157. return ret;
  158. ret = clk_bulk_prepare_enable(aiu->spdif.clk_num, aiu->spdif.clks);
  159. if (ret)
  160. dev_err(dai->dev, "failed to enable spdif clocks\n");
  161. return ret;
  162. }
  163. static void aiu_encoder_spdif_shutdown(struct snd_pcm_substream *substream,
  164. struct snd_soc_dai *dai)
  165. {
  166. struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
  167. clk_bulk_disable_unprepare(aiu->spdif.clk_num, aiu->spdif.clks);
  168. }
  169. const struct snd_soc_dai_ops aiu_encoder_spdif_dai_ops = {
  170. .trigger = aiu_encoder_spdif_trigger,
  171. .hw_params = aiu_encoder_spdif_hw_params,
  172. .hw_free = aiu_encoder_spdif_hw_free,
  173. .startup = aiu_encoder_spdif_startup,
  174. .shutdown = aiu_encoder_spdif_shutdown,
  175. };