bells.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Bells audio support
  4. //
  5. // Copyright 2012 Wolfson Microelectronics
  6. #include <sound/soc.h>
  7. #include <sound/soc-dapm.h>
  8. #include <sound/jack.h>
  9. #include <linux/gpio.h>
  10. #include <linux/module.h>
  11. #include "../codecs/wm5102.h"
  12. #include "../codecs/wm9081.h"
  13. /* BCLK2 is fixed at this currently */
  14. #define BCLK2_RATE (64 * 8000)
  15. /*
  16. * Expect a 24.576MHz crystal if one is fitted (the driver will function
  17. * if this is not fitted).
  18. */
  19. #define MCLK_RATE 24576000
  20. #define SYS_AUDIO_RATE 44100
  21. #define SYS_MCLK_RATE (SYS_AUDIO_RATE * 512)
  22. #define DAI_AP_DSP 0
  23. #define DAI_DSP_CODEC 1
  24. #define DAI_CODEC_CP 2
  25. #define DAI_CODEC_SUB 3
  26. struct bells_drvdata {
  27. int sysclk_rate;
  28. int asyncclk_rate;
  29. };
  30. static struct bells_drvdata wm2200_drvdata = {
  31. .sysclk_rate = 22579200,
  32. };
  33. static struct bells_drvdata wm5102_drvdata = {
  34. .sysclk_rate = 45158400,
  35. .asyncclk_rate = 49152000,
  36. };
  37. static struct bells_drvdata wm5110_drvdata = {
  38. .sysclk_rate = 135475200,
  39. .asyncclk_rate = 147456000,
  40. };
  41. static int bells_set_bias_level(struct snd_soc_card *card,
  42. struct snd_soc_dapm_context *dapm,
  43. enum snd_soc_bias_level level)
  44. {
  45. struct snd_soc_pcm_runtime *rtd;
  46. struct snd_soc_dai *codec_dai;
  47. struct snd_soc_component *component;
  48. struct bells_drvdata *bells = card->drvdata;
  49. int ret;
  50. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_DSP_CODEC]);
  51. codec_dai = asoc_rtd_to_codec(rtd, 0);
  52. component = codec_dai->component;
  53. if (dapm->dev != codec_dai->dev)
  54. return 0;
  55. switch (level) {
  56. case SND_SOC_BIAS_PREPARE:
  57. if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
  58. break;
  59. ret = snd_soc_component_set_pll(component, WM5102_FLL1,
  60. ARIZONA_FLL_SRC_MCLK1,
  61. MCLK_RATE,
  62. bells->sysclk_rate);
  63. if (ret < 0)
  64. pr_err("Failed to start FLL: %d\n", ret);
  65. if (bells->asyncclk_rate) {
  66. ret = snd_soc_component_set_pll(component, WM5102_FLL2,
  67. ARIZONA_FLL_SRC_AIF2BCLK,
  68. BCLK2_RATE,
  69. bells->asyncclk_rate);
  70. if (ret < 0)
  71. pr_err("Failed to start FLL: %d\n", ret);
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. return 0;
  78. }
  79. static int bells_set_bias_level_post(struct snd_soc_card *card,
  80. struct snd_soc_dapm_context *dapm,
  81. enum snd_soc_bias_level level)
  82. {
  83. struct snd_soc_pcm_runtime *rtd;
  84. struct snd_soc_dai *codec_dai;
  85. struct snd_soc_component *component;
  86. struct bells_drvdata *bells = card->drvdata;
  87. int ret;
  88. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_DSP_CODEC]);
  89. codec_dai = asoc_rtd_to_codec(rtd, 0);
  90. component = codec_dai->component;
  91. if (dapm->dev != codec_dai->dev)
  92. return 0;
  93. switch (level) {
  94. case SND_SOC_BIAS_STANDBY:
  95. ret = snd_soc_component_set_pll(component, WM5102_FLL1, 0, 0, 0);
  96. if (ret < 0) {
  97. pr_err("Failed to stop FLL: %d\n", ret);
  98. return ret;
  99. }
  100. if (bells->asyncclk_rate) {
  101. ret = snd_soc_component_set_pll(component, WM5102_FLL2,
  102. 0, 0, 0);
  103. if (ret < 0) {
  104. pr_err("Failed to stop FLL: %d\n", ret);
  105. return ret;
  106. }
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. dapm->bias_level = level;
  113. return 0;
  114. }
  115. static int bells_late_probe(struct snd_soc_card *card)
  116. {
  117. struct bells_drvdata *bells = card->drvdata;
  118. struct snd_soc_pcm_runtime *rtd;
  119. struct snd_soc_component *wm0010;
  120. struct snd_soc_component *component;
  121. struct snd_soc_dai *aif1_dai;
  122. struct snd_soc_dai *aif2_dai;
  123. struct snd_soc_dai *aif3_dai;
  124. struct snd_soc_dai *wm9081_dai;
  125. int ret;
  126. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_AP_DSP]);
  127. wm0010 = asoc_rtd_to_codec(rtd, 0)->component;
  128. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_DSP_CODEC]);
  129. component = asoc_rtd_to_codec(rtd, 0)->component;
  130. aif1_dai = asoc_rtd_to_codec(rtd, 0);
  131. ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK,
  132. ARIZONA_CLK_SRC_FLL1,
  133. bells->sysclk_rate,
  134. SND_SOC_CLOCK_IN);
  135. if (ret != 0) {
  136. dev_err(component->dev, "Failed to set SYSCLK: %d\n", ret);
  137. return ret;
  138. }
  139. ret = snd_soc_component_set_sysclk(wm0010, 0, 0, SYS_MCLK_RATE, 0);
  140. if (ret != 0) {
  141. dev_err(wm0010->dev, "Failed to set WM0010 clock: %d\n", ret);
  142. return ret;
  143. }
  144. ret = snd_soc_dai_set_sysclk(aif1_dai, ARIZONA_CLK_SYSCLK, 0, 0);
  145. if (ret != 0)
  146. dev_err(aif1_dai->dev, "Failed to set AIF1 clock: %d\n", ret);
  147. ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_OPCLK, 0,
  148. SYS_MCLK_RATE, SND_SOC_CLOCK_OUT);
  149. if (ret != 0)
  150. dev_err(component->dev, "Failed to set OPCLK: %d\n", ret);
  151. if (card->num_rtd == DAI_CODEC_CP)
  152. return 0;
  153. ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_ASYNCCLK,
  154. ARIZONA_CLK_SRC_FLL2,
  155. bells->asyncclk_rate,
  156. SND_SOC_CLOCK_IN);
  157. if (ret != 0) {
  158. dev_err(component->dev, "Failed to set ASYNCCLK: %d\n", ret);
  159. return ret;
  160. }
  161. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_CODEC_CP]);
  162. aif2_dai = asoc_rtd_to_cpu(rtd, 0);
  163. ret = snd_soc_dai_set_sysclk(aif2_dai, ARIZONA_CLK_ASYNCCLK, 0, 0);
  164. if (ret != 0) {
  165. dev_err(aif2_dai->dev, "Failed to set AIF2 clock: %d\n", ret);
  166. return ret;
  167. }
  168. if (card->num_rtd == DAI_CODEC_SUB)
  169. return 0;
  170. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_CODEC_SUB]);
  171. aif3_dai = asoc_rtd_to_cpu(rtd, 0);
  172. wm9081_dai = asoc_rtd_to_codec(rtd, 0);
  173. ret = snd_soc_dai_set_sysclk(aif3_dai, ARIZONA_CLK_SYSCLK, 0, 0);
  174. if (ret != 0) {
  175. dev_err(aif1_dai->dev, "Failed to set AIF1 clock: %d\n", ret);
  176. return ret;
  177. }
  178. ret = snd_soc_component_set_sysclk(wm9081_dai->component, WM9081_SYSCLK_MCLK,
  179. 0, SYS_MCLK_RATE, 0);
  180. if (ret != 0) {
  181. dev_err(wm9081_dai->dev, "Failed to set MCLK: %d\n", ret);
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. static const struct snd_soc_pcm_stream baseband_params = {
  187. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  188. .rate_min = 8000,
  189. .rate_max = 8000,
  190. .channels_min = 2,
  191. .channels_max = 2,
  192. };
  193. static const struct snd_soc_pcm_stream sub_params = {
  194. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  195. .rate_min = SYS_AUDIO_RATE,
  196. .rate_max = SYS_AUDIO_RATE,
  197. .channels_min = 2,
  198. .channels_max = 2,
  199. };
  200. SND_SOC_DAILINK_DEFS(wm2200_cpu_dsp,
  201. DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")),
  202. DAILINK_COMP_ARRAY(COMP_CODEC("spi0.0", "wm0010-sdi1")),
  203. DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0")));
  204. SND_SOC_DAILINK_DEFS(wm2200_dsp_codec,
  205. DAILINK_COMP_ARRAY(COMP_CPU("wm0010-sdi2")),
  206. DAILINK_COMP_ARRAY(COMP_CODEC("wm2200.1-003a", "wm2200")));
  207. static struct snd_soc_dai_link bells_dai_wm2200[] = {
  208. {
  209. .name = "CPU-DSP",
  210. .stream_name = "CPU-DSP",
  211. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  212. | SND_SOC_DAIFMT_CBM_CFM,
  213. SND_SOC_DAILINK_REG(wm2200_cpu_dsp),
  214. },
  215. {
  216. .name = "DSP-CODEC",
  217. .stream_name = "DSP-CODEC",
  218. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  219. | SND_SOC_DAIFMT_CBM_CFM,
  220. .params = &sub_params,
  221. .ignore_suspend = 1,
  222. SND_SOC_DAILINK_REG(wm2200_dsp_codec),
  223. },
  224. };
  225. SND_SOC_DAILINK_DEFS(wm5102_cpu_dsp,
  226. DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")),
  227. DAILINK_COMP_ARRAY(COMP_CODEC("spi0.0", "wm0010-sdi1")),
  228. DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0")));
  229. SND_SOC_DAILINK_DEFS(wm5102_dsp_codec,
  230. DAILINK_COMP_ARRAY(COMP_CPU("wm0010-sdi2")),
  231. DAILINK_COMP_ARRAY(COMP_CODEC("wm5102-codec", "wm5102-aif1")));
  232. SND_SOC_DAILINK_DEFS(wm5102_baseband,
  233. DAILINK_COMP_ARRAY(COMP_CPU("wm5102-aif2")),
  234. DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027", "wm1250-ev1")));
  235. SND_SOC_DAILINK_DEFS(wm5102_sub,
  236. DAILINK_COMP_ARRAY(COMP_CPU("wm5102-aif3")),
  237. DAILINK_COMP_ARRAY(COMP_CODEC("wm9081.1-006c", "wm9081-hifi")));
  238. static struct snd_soc_dai_link bells_dai_wm5102[] = {
  239. {
  240. .name = "CPU-DSP",
  241. .stream_name = "CPU-DSP",
  242. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  243. | SND_SOC_DAIFMT_CBM_CFM,
  244. SND_SOC_DAILINK_REG(wm5102_cpu_dsp),
  245. },
  246. {
  247. .name = "DSP-CODEC",
  248. .stream_name = "DSP-CODEC",
  249. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  250. | SND_SOC_DAIFMT_CBM_CFM,
  251. .params = &sub_params,
  252. .ignore_suspend = 1,
  253. SND_SOC_DAILINK_REG(wm5102_dsp_codec),
  254. },
  255. {
  256. .name = "Baseband",
  257. .stream_name = "Baseband",
  258. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  259. | SND_SOC_DAIFMT_CBM_CFM,
  260. .ignore_suspend = 1,
  261. .params = &baseband_params,
  262. SND_SOC_DAILINK_REG(wm5102_baseband),
  263. },
  264. {
  265. .name = "Sub",
  266. .stream_name = "Sub",
  267. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  268. | SND_SOC_DAIFMT_CBS_CFS,
  269. .ignore_suspend = 1,
  270. .params = &sub_params,
  271. SND_SOC_DAILINK_REG(wm5102_sub),
  272. },
  273. };
  274. SND_SOC_DAILINK_DEFS(wm5110_cpu_dsp,
  275. DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")),
  276. DAILINK_COMP_ARRAY(COMP_CODEC("spi0.0", "wm0010-sdi1")),
  277. DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0")));
  278. SND_SOC_DAILINK_DEFS(wm5110_dsp_codec,
  279. DAILINK_COMP_ARRAY(COMP_CPU("wm0010-sdi2")),
  280. DAILINK_COMP_ARRAY(COMP_CODEC("wm5110-codec", "wm5110-aif1")));
  281. SND_SOC_DAILINK_DEFS(wm5110_baseband,
  282. DAILINK_COMP_ARRAY(COMP_CPU("wm5110-aif2")),
  283. DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027", "wm1250-ev1")));
  284. SND_SOC_DAILINK_DEFS(wm5110_sub,
  285. DAILINK_COMP_ARRAY(COMP_CPU("wm5110-aif3")),
  286. DAILINK_COMP_ARRAY(COMP_CODEC("wm9081.1-006c", "wm9081-hifi")));
  287. static struct snd_soc_dai_link bells_dai_wm5110[] = {
  288. {
  289. .name = "CPU-DSP",
  290. .stream_name = "CPU-DSP",
  291. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  292. | SND_SOC_DAIFMT_CBM_CFM,
  293. SND_SOC_DAILINK_REG(wm5110_cpu_dsp),
  294. },
  295. {
  296. .name = "DSP-CODEC",
  297. .stream_name = "DSP-CODEC",
  298. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  299. | SND_SOC_DAIFMT_CBM_CFM,
  300. .params = &sub_params,
  301. .ignore_suspend = 1,
  302. SND_SOC_DAILINK_REG(wm5110_dsp_codec),
  303. },
  304. {
  305. .name = "Baseband",
  306. .stream_name = "Baseband",
  307. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  308. | SND_SOC_DAIFMT_CBM_CFM,
  309. .ignore_suspend = 1,
  310. .params = &baseband_params,
  311. SND_SOC_DAILINK_REG(wm5110_baseband),
  312. },
  313. {
  314. .name = "Sub",
  315. .stream_name = "Sub",
  316. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  317. | SND_SOC_DAIFMT_CBS_CFS,
  318. .ignore_suspend = 1,
  319. .params = &sub_params,
  320. SND_SOC_DAILINK_REG(wm5110_sub),
  321. },
  322. };
  323. static struct snd_soc_codec_conf bells_codec_conf[] = {
  324. {
  325. .dlc = COMP_CODEC_CONF("wm9081.1-006c"),
  326. .name_prefix = "Sub",
  327. },
  328. };
  329. static const struct snd_soc_dapm_widget bells_widgets[] = {
  330. SND_SOC_DAPM_MIC("DMIC", NULL),
  331. };
  332. static const struct snd_soc_dapm_route bells_routes[] = {
  333. { "Sub CLK_SYS", NULL, "OPCLK" },
  334. { "CLKIN", NULL, "OPCLK" },
  335. { "DMIC", NULL, "MICBIAS2" },
  336. { "IN2L", NULL, "DMIC" },
  337. { "IN2R", NULL, "DMIC" },
  338. };
  339. static struct snd_soc_card bells_cards[] = {
  340. {
  341. .name = "Bells WM2200",
  342. .owner = THIS_MODULE,
  343. .dai_link = bells_dai_wm2200,
  344. .num_links = ARRAY_SIZE(bells_dai_wm2200),
  345. .codec_conf = bells_codec_conf,
  346. .num_configs = ARRAY_SIZE(bells_codec_conf),
  347. .late_probe = bells_late_probe,
  348. .dapm_widgets = bells_widgets,
  349. .num_dapm_widgets = ARRAY_SIZE(bells_widgets),
  350. .dapm_routes = bells_routes,
  351. .num_dapm_routes = ARRAY_SIZE(bells_routes),
  352. .set_bias_level = bells_set_bias_level,
  353. .set_bias_level_post = bells_set_bias_level_post,
  354. .drvdata = &wm2200_drvdata,
  355. },
  356. {
  357. .name = "Bells WM5102",
  358. .owner = THIS_MODULE,
  359. .dai_link = bells_dai_wm5102,
  360. .num_links = ARRAY_SIZE(bells_dai_wm5102),
  361. .codec_conf = bells_codec_conf,
  362. .num_configs = ARRAY_SIZE(bells_codec_conf),
  363. .late_probe = bells_late_probe,
  364. .dapm_widgets = bells_widgets,
  365. .num_dapm_widgets = ARRAY_SIZE(bells_widgets),
  366. .dapm_routes = bells_routes,
  367. .num_dapm_routes = ARRAY_SIZE(bells_routes),
  368. .set_bias_level = bells_set_bias_level,
  369. .set_bias_level_post = bells_set_bias_level_post,
  370. .drvdata = &wm5102_drvdata,
  371. },
  372. {
  373. .name = "Bells WM5110",
  374. .owner = THIS_MODULE,
  375. .dai_link = bells_dai_wm5110,
  376. .num_links = ARRAY_SIZE(bells_dai_wm5110),
  377. .codec_conf = bells_codec_conf,
  378. .num_configs = ARRAY_SIZE(bells_codec_conf),
  379. .late_probe = bells_late_probe,
  380. .dapm_widgets = bells_widgets,
  381. .num_dapm_widgets = ARRAY_SIZE(bells_widgets),
  382. .dapm_routes = bells_routes,
  383. .num_dapm_routes = ARRAY_SIZE(bells_routes),
  384. .set_bias_level = bells_set_bias_level,
  385. .set_bias_level_post = bells_set_bias_level_post,
  386. .drvdata = &wm5110_drvdata,
  387. },
  388. };
  389. static int bells_probe(struct platform_device *pdev)
  390. {
  391. int ret;
  392. bells_cards[pdev->id].dev = &pdev->dev;
  393. ret = devm_snd_soc_register_card(&pdev->dev, &bells_cards[pdev->id]);
  394. if (ret)
  395. dev_err(&pdev->dev,
  396. "snd_soc_register_card(%s) failed: %d\n",
  397. bells_cards[pdev->id].name, ret);
  398. return ret;
  399. }
  400. static struct platform_driver bells_driver = {
  401. .driver = {
  402. .name = "bells",
  403. .pm = &snd_soc_pm_ops,
  404. },
  405. .probe = bells_probe,
  406. };
  407. module_platform_driver(bells_driver);
  408. MODULE_DESCRIPTION("Bells audio support");
  409. MODULE_AUTHOR("Mark Brown <[email protected]>");
  410. MODULE_LICENSE("GPL");
  411. MODULE_ALIAS("platform:bells");