odroid.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2017 Samsung Electronics Co., Ltd.
  4. #include <linux/clk.h>
  5. #include <linux/clk-provider.h>
  6. #include <linux/of.h>
  7. #include <linux/of_device.h>
  8. #include <linux/module.h>
  9. #include <sound/soc.h>
  10. #include <sound/pcm_params.h>
  11. #include "i2s.h"
  12. #include "i2s-regs.h"
  13. struct odroid_priv {
  14. struct snd_soc_card card;
  15. struct clk *clk_i2s_bus;
  16. struct clk *sclk_i2s;
  17. /* Spinlock protecting fields below */
  18. spinlock_t lock;
  19. unsigned int be_sample_rate;
  20. bool be_active;
  21. };
  22. static int odroid_card_fe_startup(struct snd_pcm_substream *substream)
  23. {
  24. struct snd_pcm_runtime *runtime = substream->runtime;
  25. snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  26. return 0;
  27. }
  28. static int odroid_card_fe_hw_params(struct snd_pcm_substream *substream,
  29. struct snd_pcm_hw_params *params)
  30. {
  31. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  32. struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  33. unsigned long flags;
  34. int ret = 0;
  35. spin_lock_irqsave(&priv->lock, flags);
  36. if (priv->be_active && priv->be_sample_rate != params_rate(params))
  37. ret = -EINVAL;
  38. spin_unlock_irqrestore(&priv->lock, flags);
  39. return ret;
  40. }
  41. static const struct snd_soc_ops odroid_card_fe_ops = {
  42. .startup = odroid_card_fe_startup,
  43. .hw_params = odroid_card_fe_hw_params,
  44. };
  45. static int odroid_card_be_hw_params(struct snd_pcm_substream *substream,
  46. struct snd_pcm_hw_params *params)
  47. {
  48. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  49. struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  50. unsigned int pll_freq, rclk_freq, rfs;
  51. unsigned long flags;
  52. int ret;
  53. switch (params_rate(params)) {
  54. case 64000:
  55. pll_freq = 196608001U;
  56. rfs = 384;
  57. break;
  58. case 44100:
  59. case 88200:
  60. pll_freq = 180633609U;
  61. rfs = 512;
  62. break;
  63. case 32000:
  64. case 48000:
  65. case 96000:
  66. pll_freq = 196608001U;
  67. rfs = 512;
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1);
  73. if (ret < 0)
  74. return ret;
  75. /*
  76. * We add 2 to the rclk_freq value in order to avoid too low clock
  77. * frequency values due to the EPLL output frequency not being exact
  78. * multiple of the audio sampling rate.
  79. */
  80. rclk_freq = params_rate(params) * rfs + 2;
  81. ret = clk_set_rate(priv->sclk_i2s, rclk_freq);
  82. if (ret < 0)
  83. return ret;
  84. if (rtd->dai_link->num_codecs > 1) {
  85. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 1);
  86. ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq,
  87. SND_SOC_CLOCK_IN);
  88. if (ret < 0)
  89. return ret;
  90. }
  91. spin_lock_irqsave(&priv->lock, flags);
  92. priv->be_sample_rate = params_rate(params);
  93. spin_unlock_irqrestore(&priv->lock, flags);
  94. return 0;
  95. }
  96. static int odroid_card_be_trigger(struct snd_pcm_substream *substream, int cmd)
  97. {
  98. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  99. struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  100. unsigned long flags;
  101. spin_lock_irqsave(&priv->lock, flags);
  102. switch (cmd) {
  103. case SNDRV_PCM_TRIGGER_START:
  104. case SNDRV_PCM_TRIGGER_RESUME:
  105. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  106. priv->be_active = true;
  107. break;
  108. case SNDRV_PCM_TRIGGER_STOP:
  109. case SNDRV_PCM_TRIGGER_SUSPEND:
  110. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  111. priv->be_active = false;
  112. break;
  113. }
  114. spin_unlock_irqrestore(&priv->lock, flags);
  115. return 0;
  116. }
  117. static const struct snd_soc_ops odroid_card_be_ops = {
  118. .hw_params = odroid_card_be_hw_params,
  119. .trigger = odroid_card_be_trigger,
  120. };
  121. /* DAPM routes for backward compatibility with old DTS */
  122. static const struct snd_soc_dapm_route odroid_dapm_routes[] = {
  123. { "I2S Playback", NULL, "Mixer DAI TX" },
  124. { "HiFi Playback", NULL, "Mixer DAI TX" },
  125. };
  126. SND_SOC_DAILINK_DEFS(primary,
  127. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  128. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  129. DAILINK_COMP_ARRAY(COMP_PLATFORM("3830000.i2s")));
  130. SND_SOC_DAILINK_DEFS(mixer,
  131. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  132. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  133. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  134. SND_SOC_DAILINK_DEFS(secondary,
  135. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  136. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  137. DAILINK_COMP_ARRAY(COMP_PLATFORM("3830000.i2s-sec")));
  138. static struct snd_soc_dai_link odroid_card_dais[] = {
  139. {
  140. /* Primary FE <-> BE link */
  141. .ops = &odroid_card_fe_ops,
  142. .name = "Primary",
  143. .stream_name = "Primary",
  144. .dynamic = 1,
  145. .dpcm_playback = 1,
  146. SND_SOC_DAILINK_REG(primary),
  147. }, {
  148. /* BE <-> CODECs link */
  149. .name = "I2S Mixer",
  150. .ops = &odroid_card_be_ops,
  151. .no_pcm = 1,
  152. .dpcm_playback = 1,
  153. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  154. SND_SOC_DAIFMT_CBS_CFS,
  155. SND_SOC_DAILINK_REG(mixer),
  156. }, {
  157. /* Secondary FE <-> BE link */
  158. .playback_only = 1,
  159. .ops = &odroid_card_fe_ops,
  160. .name = "Secondary",
  161. .stream_name = "Secondary",
  162. .dynamic = 1,
  163. .dpcm_playback = 1,
  164. SND_SOC_DAILINK_REG(secondary),
  165. }
  166. };
  167. static int odroid_audio_probe(struct platform_device *pdev)
  168. {
  169. struct device *dev = &pdev->dev;
  170. struct device_node *cpu_dai = NULL;
  171. struct device_node *cpu, *codec;
  172. struct odroid_priv *priv;
  173. struct snd_soc_card *card;
  174. struct snd_soc_dai_link *link, *codec_link;
  175. int num_pcms, ret, i;
  176. struct of_phandle_args args = {};
  177. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  178. if (!priv)
  179. return -ENOMEM;
  180. card = &priv->card;
  181. card->dev = dev;
  182. card->owner = THIS_MODULE;
  183. card->fully_routed = true;
  184. spin_lock_init(&priv->lock);
  185. snd_soc_card_set_drvdata(card, priv);
  186. ret = snd_soc_of_parse_card_name(card, "model");
  187. if (ret < 0)
  188. return ret;
  189. if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) {
  190. ret = snd_soc_of_parse_audio_simple_widgets(card,
  191. "samsung,audio-widgets");
  192. if (ret < 0)
  193. return ret;
  194. }
  195. if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) {
  196. ret = snd_soc_of_parse_audio_routing(card,
  197. "samsung,audio-routing");
  198. if (ret < 0)
  199. return ret;
  200. }
  201. card->dai_link = odroid_card_dais;
  202. card->num_links = ARRAY_SIZE(odroid_card_dais);
  203. cpu = of_get_child_by_name(dev->of_node, "cpu");
  204. codec = of_get_child_by_name(dev->of_node, "codec");
  205. link = card->dai_link;
  206. codec_link = &card->dai_link[1];
  207. /*
  208. * For backwards compatibility create the secondary CPU DAI link only
  209. * if there are 2 CPU DAI entries in the cpu sound-dai property in DT.
  210. * Also add required DAPM routes not available in old DTS.
  211. */
  212. num_pcms = of_count_phandle_with_args(cpu, "sound-dai",
  213. "#sound-dai-cells");
  214. if (num_pcms == 1) {
  215. card->dapm_routes = odroid_dapm_routes;
  216. card->num_dapm_routes = ARRAY_SIZE(odroid_dapm_routes);
  217. card->num_links--;
  218. }
  219. for (i = 0; i < num_pcms; i++, link += 2) {
  220. ret = of_parse_phandle_with_args(cpu, "sound-dai",
  221. "#sound-dai-cells", i, &args);
  222. if (ret < 0)
  223. break;
  224. if (!args.np) {
  225. dev_err(dev, "sound-dai property parse error: %d\n", ret);
  226. ret = -EINVAL;
  227. break;
  228. }
  229. ret = snd_soc_get_dai_name(&args, &link->cpus->dai_name);
  230. of_node_put(args.np);
  231. if (ret < 0)
  232. break;
  233. }
  234. if (ret == 0) {
  235. cpu_dai = of_parse_phandle(cpu, "sound-dai", 0);
  236. if (!cpu_dai)
  237. ret = -EINVAL;
  238. }
  239. of_node_put(cpu);
  240. if (ret < 0)
  241. goto err_put_node;
  242. ret = snd_soc_of_get_dai_link_codecs(dev, codec, codec_link);
  243. if (ret < 0)
  244. goto err_put_cpu_dai;
  245. /* Set capture capability only for boards with the MAX98090 CODEC */
  246. if (codec_link->num_codecs > 1) {
  247. card->dai_link[0].dpcm_capture = 1;
  248. card->dai_link[1].dpcm_capture = 1;
  249. }
  250. priv->sclk_i2s = of_clk_get_by_name(cpu_dai, "i2s_opclk1");
  251. if (IS_ERR(priv->sclk_i2s)) {
  252. ret = PTR_ERR(priv->sclk_i2s);
  253. goto err_put_cpu_dai;
  254. }
  255. priv->clk_i2s_bus = of_clk_get_by_name(cpu_dai, "iis");
  256. if (IS_ERR(priv->clk_i2s_bus)) {
  257. ret = PTR_ERR(priv->clk_i2s_bus);
  258. goto err_put_sclk;
  259. }
  260. ret = devm_snd_soc_register_card(dev, card);
  261. if (ret < 0) {
  262. dev_err_probe(dev, ret, "snd_soc_register_card() failed\n");
  263. goto err_put_clk_i2s;
  264. }
  265. of_node_put(cpu_dai);
  266. of_node_put(codec);
  267. return 0;
  268. err_put_clk_i2s:
  269. clk_put(priv->clk_i2s_bus);
  270. err_put_sclk:
  271. clk_put(priv->sclk_i2s);
  272. err_put_cpu_dai:
  273. of_node_put(cpu_dai);
  274. snd_soc_of_put_dai_link_codecs(codec_link);
  275. err_put_node:
  276. of_node_put(codec);
  277. return ret;
  278. }
  279. static int odroid_audio_remove(struct platform_device *pdev)
  280. {
  281. struct odroid_priv *priv = platform_get_drvdata(pdev);
  282. snd_soc_of_put_dai_link_codecs(&priv->card.dai_link[1]);
  283. clk_put(priv->sclk_i2s);
  284. clk_put(priv->clk_i2s_bus);
  285. return 0;
  286. }
  287. static const struct of_device_id odroid_audio_of_match[] = {
  288. { .compatible = "hardkernel,odroid-xu3-audio" },
  289. { .compatible = "hardkernel,odroid-xu4-audio" },
  290. { .compatible = "samsung,odroid-xu3-audio" },
  291. { .compatible = "samsung,odroid-xu4-audio" },
  292. { },
  293. };
  294. MODULE_DEVICE_TABLE(of, odroid_audio_of_match);
  295. static struct platform_driver odroid_audio_driver = {
  296. .driver = {
  297. .name = "odroid-audio",
  298. .of_match_table = odroid_audio_of_match,
  299. .pm = &snd_soc_pm_ops,
  300. },
  301. .probe = odroid_audio_probe,
  302. .remove = odroid_audio_remove,
  303. };
  304. module_platform_driver(odroid_audio_driver);
  305. MODULE_AUTHOR("Sylwester Nawrocki <[email protected]>");
  306. MODULE_DESCRIPTION("Odroid XU3/XU4 audio support");
  307. MODULE_LICENSE("GPL v2");