aiu-encoder-i2s.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/soc.h>
  9. #include <sound/soc-dai.h>
  10. #include "aiu.h"
  11. #define AIU_I2S_SOURCE_DESC_MODE_8CH BIT(0)
  12. #define AIU_I2S_SOURCE_DESC_MODE_24BIT BIT(5)
  13. #define AIU_I2S_SOURCE_DESC_MODE_32BIT BIT(9)
  14. #define AIU_I2S_SOURCE_DESC_MODE_SPLIT BIT(11)
  15. #define AIU_RST_SOFT_I2S_FAST BIT(0)
  16. #define AIU_I2S_DAC_CFG_MSB_FIRST BIT(2)
  17. #define AIU_CLK_CTRL_I2S_DIV_EN BIT(0)
  18. #define AIU_CLK_CTRL_I2S_DIV GENMASK(3, 2)
  19. #define AIU_CLK_CTRL_AOCLK_INVERT BIT(6)
  20. #define AIU_CLK_CTRL_LRCLK_INVERT BIT(7)
  21. #define AIU_CLK_CTRL_LRCLK_SKEW GENMASK(9, 8)
  22. #define AIU_CLK_CTRL_MORE_HDMI_AMCLK BIT(6)
  23. #define AIU_CLK_CTRL_MORE_I2S_DIV GENMASK(5, 0)
  24. #define AIU_CODEC_DAC_LRCLK_CTRL_DIV GENMASK(11, 0)
  25. static void aiu_encoder_i2s_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_I2S_DIV_EN,
  30. enable ? AIU_CLK_CTRL_I2S_DIV_EN : 0);
  31. }
  32. static int aiu_encoder_i2s_setup_desc(struct snd_soc_component *component,
  33. struct snd_pcm_hw_params *params)
  34. {
  35. /* Always operate in split (classic interleaved) mode */
  36. unsigned int desc = AIU_I2S_SOURCE_DESC_MODE_SPLIT;
  37. /* Reset required to update the pipeline */
  38. snd_soc_component_write(component, AIU_RST_SOFT, AIU_RST_SOFT_I2S_FAST);
  39. snd_soc_component_read(component, AIU_I2S_SYNC);
  40. switch (params_physical_width(params)) {
  41. case 16: /* Nothing to do */
  42. break;
  43. case 32:
  44. desc |= (AIU_I2S_SOURCE_DESC_MODE_24BIT |
  45. AIU_I2S_SOURCE_DESC_MODE_32BIT);
  46. break;
  47. default:
  48. return -EINVAL;
  49. }
  50. switch (params_channels(params)) {
  51. case 2: /* Nothing to do */
  52. break;
  53. case 8:
  54. desc |= AIU_I2S_SOURCE_DESC_MODE_8CH;
  55. break;
  56. default:
  57. return -EINVAL;
  58. }
  59. snd_soc_component_update_bits(component, AIU_I2S_SOURCE_DESC,
  60. AIU_I2S_SOURCE_DESC_MODE_8CH |
  61. AIU_I2S_SOURCE_DESC_MODE_24BIT |
  62. AIU_I2S_SOURCE_DESC_MODE_32BIT |
  63. AIU_I2S_SOURCE_DESC_MODE_SPLIT,
  64. desc);
  65. return 0;
  66. }
  67. static int aiu_encoder_i2s_set_legacy_div(struct snd_soc_component *component,
  68. struct snd_pcm_hw_params *params,
  69. unsigned int bs)
  70. {
  71. switch (bs) {
  72. case 1:
  73. case 2:
  74. case 4:
  75. case 8:
  76. /* These are the only valid legacy dividers */
  77. break;
  78. default:
  79. dev_err(component->dev, "Unsupported i2s divider: %u\n", bs);
  80. return -EINVAL;
  81. }
  82. snd_soc_component_update_bits(component, AIU_CLK_CTRL,
  83. AIU_CLK_CTRL_I2S_DIV,
  84. FIELD_PREP(AIU_CLK_CTRL_I2S_DIV,
  85. __ffs(bs)));
  86. snd_soc_component_update_bits(component, AIU_CLK_CTRL_MORE,
  87. AIU_CLK_CTRL_MORE_I2S_DIV,
  88. FIELD_PREP(AIU_CLK_CTRL_MORE_I2S_DIV,
  89. 0));
  90. return 0;
  91. }
  92. static int aiu_encoder_i2s_set_more_div(struct snd_soc_component *component,
  93. struct snd_pcm_hw_params *params,
  94. unsigned int bs)
  95. {
  96. /*
  97. * NOTE: this HW is odd.
  98. * In most configuration, the i2s divider is 'mclk / blck'.
  99. * However, in 16 bits - 8ch mode, this factor needs to be
  100. * increased by 50% to get the correct output rate.
  101. * No idea why !
  102. */
  103. if (params_width(params) == 16 && params_channels(params) == 8) {
  104. if (bs % 2) {
  105. dev_err(component->dev,
  106. "Cannot increase i2s divider by 50%%\n");
  107. return -EINVAL;
  108. }
  109. bs += bs / 2;
  110. }
  111. /* Use CLK_MORE for mclk to bclk divider */
  112. snd_soc_component_update_bits(component, AIU_CLK_CTRL,
  113. AIU_CLK_CTRL_I2S_DIV,
  114. FIELD_PREP(AIU_CLK_CTRL_I2S_DIV, 0));
  115. snd_soc_component_update_bits(component, AIU_CLK_CTRL_MORE,
  116. AIU_CLK_CTRL_MORE_I2S_DIV,
  117. FIELD_PREP(AIU_CLK_CTRL_MORE_I2S_DIV,
  118. bs - 1));
  119. return 0;
  120. }
  121. static int aiu_encoder_i2s_set_clocks(struct snd_soc_component *component,
  122. struct snd_pcm_hw_params *params)
  123. {
  124. struct aiu *aiu = snd_soc_component_get_drvdata(component);
  125. unsigned int srate = params_rate(params);
  126. unsigned int fs, bs;
  127. int ret;
  128. /* Get the oversampling factor */
  129. fs = DIV_ROUND_CLOSEST(clk_get_rate(aiu->i2s.clks[MCLK].clk), srate);
  130. if (fs % 64)
  131. return -EINVAL;
  132. /* Send data MSB first */
  133. snd_soc_component_update_bits(component, AIU_I2S_DAC_CFG,
  134. AIU_I2S_DAC_CFG_MSB_FIRST,
  135. AIU_I2S_DAC_CFG_MSB_FIRST);
  136. /* Set bclk to lrlck ratio */
  137. snd_soc_component_update_bits(component, AIU_CODEC_DAC_LRCLK_CTRL,
  138. AIU_CODEC_DAC_LRCLK_CTRL_DIV,
  139. FIELD_PREP(AIU_CODEC_DAC_LRCLK_CTRL_DIV,
  140. 64 - 1));
  141. bs = fs / 64;
  142. if (aiu->platform->has_clk_ctrl_more_i2s_div)
  143. ret = aiu_encoder_i2s_set_more_div(component, params, bs);
  144. else
  145. ret = aiu_encoder_i2s_set_legacy_div(component, params, bs);
  146. if (ret)
  147. return ret;
  148. /* Make sure amclk is used for HDMI i2s as well */
  149. snd_soc_component_update_bits(component, AIU_CLK_CTRL_MORE,
  150. AIU_CLK_CTRL_MORE_HDMI_AMCLK,
  151. AIU_CLK_CTRL_MORE_HDMI_AMCLK);
  152. return 0;
  153. }
  154. static int aiu_encoder_i2s_hw_params(struct snd_pcm_substream *substream,
  155. struct snd_pcm_hw_params *params,
  156. struct snd_soc_dai *dai)
  157. {
  158. struct snd_soc_component *component = dai->component;
  159. int ret;
  160. /* Disable the clock while changing the settings */
  161. aiu_encoder_i2s_divider_enable(component, false);
  162. ret = aiu_encoder_i2s_setup_desc(component, params);
  163. if (ret) {
  164. dev_err(dai->dev, "setting i2s desc failed\n");
  165. return ret;
  166. }
  167. ret = aiu_encoder_i2s_set_clocks(component, params);
  168. if (ret) {
  169. dev_err(dai->dev, "setting i2s clocks failed\n");
  170. return ret;
  171. }
  172. aiu_encoder_i2s_divider_enable(component, true);
  173. return 0;
  174. }
  175. static int aiu_encoder_i2s_hw_free(struct snd_pcm_substream *substream,
  176. struct snd_soc_dai *dai)
  177. {
  178. struct snd_soc_component *component = dai->component;
  179. aiu_encoder_i2s_divider_enable(component, false);
  180. return 0;
  181. }
  182. static int aiu_encoder_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  183. {
  184. struct snd_soc_component *component = dai->component;
  185. unsigned int inv = fmt & SND_SOC_DAIFMT_INV_MASK;
  186. unsigned int val = 0;
  187. unsigned int skew;
  188. /* Only CPU Master / Codec Slave supported ATM */
  189. if ((fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_BP_FP)
  190. return -EINVAL;
  191. if (inv == SND_SOC_DAIFMT_NB_IF ||
  192. inv == SND_SOC_DAIFMT_IB_IF)
  193. val |= AIU_CLK_CTRL_LRCLK_INVERT;
  194. if (inv == SND_SOC_DAIFMT_IB_NF ||
  195. inv == SND_SOC_DAIFMT_IB_IF)
  196. val |= AIU_CLK_CTRL_AOCLK_INVERT;
  197. /* Signal skew */
  198. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  199. case SND_SOC_DAIFMT_I2S:
  200. /* Invert sample clock for i2s */
  201. val ^= AIU_CLK_CTRL_LRCLK_INVERT;
  202. skew = 1;
  203. break;
  204. case SND_SOC_DAIFMT_LEFT_J:
  205. skew = 0;
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. val |= FIELD_PREP(AIU_CLK_CTRL_LRCLK_SKEW, skew);
  211. snd_soc_component_update_bits(component, AIU_CLK_CTRL,
  212. AIU_CLK_CTRL_LRCLK_INVERT |
  213. AIU_CLK_CTRL_AOCLK_INVERT |
  214. AIU_CLK_CTRL_LRCLK_SKEW,
  215. val);
  216. return 0;
  217. }
  218. static int aiu_encoder_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  219. unsigned int freq, int dir)
  220. {
  221. struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
  222. int ret;
  223. if (WARN_ON(clk_id != 0))
  224. return -EINVAL;
  225. if (dir == SND_SOC_CLOCK_IN)
  226. return 0;
  227. ret = clk_set_rate(aiu->i2s.clks[MCLK].clk, freq);
  228. if (ret)
  229. dev_err(dai->dev, "Failed to set sysclk to %uHz", freq);
  230. return ret;
  231. }
  232. static const unsigned int hw_channels[] = {2, 8};
  233. static const struct snd_pcm_hw_constraint_list hw_channel_constraints = {
  234. .list = hw_channels,
  235. .count = ARRAY_SIZE(hw_channels),
  236. .mask = 0,
  237. };
  238. static int aiu_encoder_i2s_startup(struct snd_pcm_substream *substream,
  239. struct snd_soc_dai *dai)
  240. {
  241. struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
  242. int ret;
  243. /* Make sure the encoder gets either 2 or 8 channels */
  244. ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
  245. SNDRV_PCM_HW_PARAM_CHANNELS,
  246. &hw_channel_constraints);
  247. if (ret) {
  248. dev_err(dai->dev, "adding channels constraints failed\n");
  249. return ret;
  250. }
  251. ret = clk_bulk_prepare_enable(aiu->i2s.clk_num, aiu->i2s.clks);
  252. if (ret)
  253. dev_err(dai->dev, "failed to enable i2s clocks\n");
  254. return ret;
  255. }
  256. static void aiu_encoder_i2s_shutdown(struct snd_pcm_substream *substream,
  257. struct snd_soc_dai *dai)
  258. {
  259. struct aiu *aiu = snd_soc_component_get_drvdata(dai->component);
  260. clk_bulk_disable_unprepare(aiu->i2s.clk_num, aiu->i2s.clks);
  261. }
  262. const struct snd_soc_dai_ops aiu_encoder_i2s_dai_ops = {
  263. .hw_params = aiu_encoder_i2s_hw_params,
  264. .hw_free = aiu_encoder_i2s_hw_free,
  265. .set_fmt = aiu_encoder_i2s_set_fmt,
  266. .set_sysclk = aiu_encoder_i2s_set_sysclk,
  267. .startup = aiu_encoder_i2s_startup,
  268. .shutdown = aiu_encoder_i2s_shutdown,
  269. };