fsl-asoc-card.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Freescale Generic ASoC Sound Card driver with ASRC
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc.
  5. *
  6. * Author: Nicolin Chen <nicoleotsuka@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/of_platform.h>
  16. #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
  17. #include <sound/ac97_codec.h>
  18. #endif
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include "fsl_esai.h"
  22. #include "fsl_sai.h"
  23. #include "imx-audmux.h"
  24. #include "../codecs/sgtl5000.h"
  25. #include "../codecs/wm8962.h"
  26. #include "../codecs/wm8960.h"
  27. #define RX 0
  28. #define TX 1
  29. /* Default DAI format without Master and Slave flag */
  30. #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
  31. /**
  32. * CODEC private data
  33. *
  34. * @mclk_freq: Clock rate of MCLK
  35. * @mclk_id: MCLK (or main clock) id for set_sysclk()
  36. * @fll_id: FLL (or secordary clock) id for set_sysclk()
  37. * @pll_id: PLL id for set_pll()
  38. */
  39. struct codec_priv {
  40. unsigned long mclk_freq;
  41. u32 mclk_id;
  42. u32 fll_id;
  43. u32 pll_id;
  44. };
  45. /**
  46. * CPU private data
  47. *
  48. * @sysclk_freq[2]: SYSCLK rates for set_sysclk()
  49. * @sysclk_dir[2]: SYSCLK directions for set_sysclk()
  50. * @sysclk_id[2]: SYSCLK ids for set_sysclk()
  51. * @slot_width: Slot width of each frame
  52. *
  53. * Note: [1] for tx and [0] for rx
  54. */
  55. struct cpu_priv {
  56. unsigned long sysclk_freq[2];
  57. u32 sysclk_dir[2];
  58. u32 sysclk_id[2];
  59. u32 slot_width;
  60. };
  61. /**
  62. * Freescale Generic ASOC card private data
  63. *
  64. * @dai_link[3]: DAI link structure including normal one and DPCM link
  65. * @pdev: platform device pointer
  66. * @codec_priv: CODEC private data
  67. * @cpu_priv: CPU private data
  68. * @card: ASoC card structure
  69. * @sample_rate: Current sample rate
  70. * @sample_format: Current sample format
  71. * @asrc_rate: ASRC sample rate used by Back-Ends
  72. * @asrc_format: ASRC sample format used by Back-Ends
  73. * @dai_fmt: DAI format between CPU and CODEC
  74. * @name: Card name
  75. */
  76. struct fsl_asoc_card_priv {
  77. struct snd_soc_dai_link dai_link[3];
  78. struct platform_device *pdev;
  79. struct codec_priv codec_priv;
  80. struct cpu_priv cpu_priv;
  81. struct snd_soc_card card;
  82. u32 sample_rate;
  83. u32 sample_format;
  84. u32 asrc_rate;
  85. u32 asrc_format;
  86. u32 dai_fmt;
  87. char name[32];
  88. };
  89. /**
  90. * This dapm route map exsits for DPCM link only.
  91. * The other routes shall go through Device Tree.
  92. */
  93. static const struct snd_soc_dapm_route audio_map[] = {
  94. {"CPU-Playback", NULL, "ASRC-Playback"},
  95. {"Playback", NULL, "CPU-Playback"},
  96. {"ASRC-Capture", NULL, "CPU-Capture"},
  97. {"CPU-Capture", NULL, "Capture"},
  98. };
  99. /* Add all possible widgets into here without being redundant */
  100. static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
  101. SND_SOC_DAPM_LINE("Line Out Jack", NULL),
  102. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  103. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  104. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  105. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  106. SND_SOC_DAPM_MIC("AMIC", NULL),
  107. SND_SOC_DAPM_MIC("DMIC", NULL),
  108. };
  109. static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
  110. {
  111. return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
  112. }
  113. static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
  114. struct snd_pcm_hw_params *params)
  115. {
  116. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  117. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  118. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  119. struct cpu_priv *cpu_priv = &priv->cpu_priv;
  120. struct device *dev = rtd->card->dev;
  121. int ret;
  122. priv->sample_rate = params_rate(params);
  123. priv->sample_format = params_format(params);
  124. /*
  125. * If codec-dai is DAI Master and all configurations are already in the
  126. * set_bias_level(), bypass the remaining settings in hw_params().
  127. * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
  128. */
  129. if ((priv->card.set_bias_level &&
  130. priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) ||
  131. fsl_asoc_card_is_ac97(priv))
  132. return 0;
  133. /* Specific configurations of DAIs starts from here */
  134. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
  135. cpu_priv->sysclk_freq[tx],
  136. cpu_priv->sysclk_dir[tx]);
  137. if (ret) {
  138. dev_err(dev, "failed to set sysclk for cpu dai\n");
  139. return ret;
  140. }
  141. if (cpu_priv->slot_width) {
  142. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2,
  143. cpu_priv->slot_width);
  144. if (ret) {
  145. dev_err(dev, "failed to set TDM slot for cpu dai\n");
  146. return ret;
  147. }
  148. }
  149. return 0;
  150. }
  151. static struct snd_soc_ops fsl_asoc_card_ops = {
  152. .hw_params = fsl_asoc_card_hw_params,
  153. };
  154. static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  155. struct snd_pcm_hw_params *params)
  156. {
  157. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  158. struct snd_interval *rate;
  159. struct snd_mask *mask;
  160. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  161. rate->max = rate->min = priv->asrc_rate;
  162. mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  163. snd_mask_none(mask);
  164. snd_mask_set(mask, priv->asrc_format);
  165. return 0;
  166. }
  167. static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
  168. /* Default ASoC DAI Link*/
  169. {
  170. .name = "HiFi",
  171. .stream_name = "HiFi",
  172. .ops = &fsl_asoc_card_ops,
  173. },
  174. /* DPCM Link between Front-End and Back-End (Optional) */
  175. {
  176. .name = "HiFi-ASRC-FE",
  177. .stream_name = "HiFi-ASRC-FE",
  178. .codec_name = "snd-soc-dummy",
  179. .codec_dai_name = "snd-soc-dummy-dai",
  180. .dpcm_playback = 1,
  181. .dpcm_capture = 1,
  182. .dynamic = 1,
  183. },
  184. {
  185. .name = "HiFi-ASRC-BE",
  186. .stream_name = "HiFi-ASRC-BE",
  187. .platform_name = "snd-soc-dummy",
  188. .be_hw_params_fixup = be_hw_params_fixup,
  189. .ops = &fsl_asoc_card_ops,
  190. .dpcm_playback = 1,
  191. .dpcm_capture = 1,
  192. .no_pcm = 1,
  193. },
  194. };
  195. static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
  196. struct snd_soc_dapm_context *dapm,
  197. enum snd_soc_bias_level level)
  198. {
  199. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
  200. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  201. struct codec_priv *codec_priv = &priv->codec_priv;
  202. struct device *dev = card->dev;
  203. unsigned int pll_out;
  204. int ret;
  205. if (dapm->dev != codec_dai->dev)
  206. return 0;
  207. switch (level) {
  208. case SND_SOC_BIAS_PREPARE:
  209. if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
  210. break;
  211. if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
  212. pll_out = priv->sample_rate * 384;
  213. else
  214. pll_out = priv->sample_rate * 256;
  215. ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
  216. codec_priv->mclk_id,
  217. codec_priv->mclk_freq, pll_out);
  218. if (ret) {
  219. dev_err(dev, "failed to start FLL: %d\n", ret);
  220. return ret;
  221. }
  222. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
  223. pll_out, SND_SOC_CLOCK_IN);
  224. if (ret) {
  225. dev_err(dev, "failed to set SYSCLK: %d\n", ret);
  226. return ret;
  227. }
  228. break;
  229. case SND_SOC_BIAS_STANDBY:
  230. if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
  231. break;
  232. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
  233. codec_priv->mclk_freq,
  234. SND_SOC_CLOCK_IN);
  235. if (ret) {
  236. dev_err(dev, "failed to switch away from FLL: %d\n", ret);
  237. return ret;
  238. }
  239. ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
  240. if (ret) {
  241. dev_err(dev, "failed to stop FLL: %d\n", ret);
  242. return ret;
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. return 0;
  249. }
  250. static int fsl_asoc_card_audmux_init(struct device_node *np,
  251. struct fsl_asoc_card_priv *priv)
  252. {
  253. struct device *dev = &priv->pdev->dev;
  254. u32 int_ptcr = 0, ext_ptcr = 0;
  255. int int_port, ext_port;
  256. int ret;
  257. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  258. if (ret) {
  259. dev_err(dev, "mux-int-port missing or invalid\n");
  260. return ret;
  261. }
  262. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  263. if (ret) {
  264. dev_err(dev, "mux-ext-port missing or invalid\n");
  265. return ret;
  266. }
  267. /*
  268. * The port numbering in the hardware manual starts at 1, while
  269. * the AUDMUX API expects it starts at 0.
  270. */
  271. int_port--;
  272. ext_port--;
  273. /*
  274. * Use asynchronous mode (6 wires) for all cases except AC97.
  275. * If only 4 wires are needed, just set SSI into
  276. * synchronous mode and enable 4 PADs in IOMUX.
  277. */
  278. switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  279. case SND_SOC_DAIFMT_CBM_CFM:
  280. int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
  281. IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
  282. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  283. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  284. IMX_AUDMUX_V2_PTCR_RFSDIR |
  285. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  286. IMX_AUDMUX_V2_PTCR_TFSDIR |
  287. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  288. break;
  289. case SND_SOC_DAIFMT_CBM_CFS:
  290. int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
  291. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  292. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  293. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  294. ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
  295. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  296. IMX_AUDMUX_V2_PTCR_RFSDIR |
  297. IMX_AUDMUX_V2_PTCR_TFSDIR;
  298. break;
  299. case SND_SOC_DAIFMT_CBS_CFM:
  300. int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
  301. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  302. IMX_AUDMUX_V2_PTCR_RFSDIR |
  303. IMX_AUDMUX_V2_PTCR_TFSDIR;
  304. ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
  305. IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
  306. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  307. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  308. break;
  309. case SND_SOC_DAIFMT_CBS_CFS:
  310. ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
  311. IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
  312. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  313. IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
  314. IMX_AUDMUX_V2_PTCR_RFSDIR |
  315. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  316. IMX_AUDMUX_V2_PTCR_TFSDIR |
  317. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  318. break;
  319. default:
  320. if (!fsl_asoc_card_is_ac97(priv))
  321. return -EINVAL;
  322. }
  323. if (fsl_asoc_card_is_ac97(priv)) {
  324. int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  325. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  326. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  327. ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  328. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  329. IMX_AUDMUX_V2_PTCR_TFSDIR;
  330. }
  331. /* Asynchronous mode can not be set along with RCLKDIR */
  332. if (!fsl_asoc_card_is_ac97(priv)) {
  333. unsigned int pdcr =
  334. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
  335. ret = imx_audmux_v2_configure_port(int_port, 0,
  336. pdcr);
  337. if (ret) {
  338. dev_err(dev, "audmux internal port setup failed\n");
  339. return ret;
  340. }
  341. }
  342. ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
  343. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  344. if (ret) {
  345. dev_err(dev, "audmux internal port setup failed\n");
  346. return ret;
  347. }
  348. if (!fsl_asoc_card_is_ac97(priv)) {
  349. unsigned int pdcr =
  350. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
  351. ret = imx_audmux_v2_configure_port(ext_port, 0,
  352. pdcr);
  353. if (ret) {
  354. dev_err(dev, "audmux external port setup failed\n");
  355. return ret;
  356. }
  357. }
  358. ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
  359. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  360. if (ret) {
  361. dev_err(dev, "audmux external port setup failed\n");
  362. return ret;
  363. }
  364. return 0;
  365. }
  366. static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
  367. {
  368. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
  369. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  370. struct codec_priv *codec_priv = &priv->codec_priv;
  371. struct device *dev = card->dev;
  372. int ret;
  373. if (fsl_asoc_card_is_ac97(priv)) {
  374. #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
  375. struct snd_soc_codec *codec = card->rtd[0].codec;
  376. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  377. /*
  378. * Use slots 3/4 for S/PDIF so SSI won't try to enable
  379. * other slots and send some samples there
  380. * due to SLOTREQ bits for S/PDIF received from codec
  381. */
  382. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  383. AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
  384. #endif
  385. return 0;
  386. }
  387. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
  388. codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
  389. if (ret) {
  390. dev_err(dev, "failed to set sysclk in %s\n", __func__);
  391. return ret;
  392. }
  393. return 0;
  394. }
  395. static int fsl_asoc_card_probe(struct platform_device *pdev)
  396. {
  397. struct device_node *cpu_np, *codec_np, *asrc_np;
  398. struct device_node *np = pdev->dev.of_node;
  399. struct platform_device *asrc_pdev = NULL;
  400. struct platform_device *cpu_pdev;
  401. struct fsl_asoc_card_priv *priv;
  402. struct i2c_client *codec_dev;
  403. const char *codec_dai_name;
  404. u32 width;
  405. int ret;
  406. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  407. if (!priv)
  408. return -ENOMEM;
  409. cpu_np = of_parse_phandle(np, "audio-cpu", 0);
  410. /* Give a chance to old DT binding */
  411. if (!cpu_np)
  412. cpu_np = of_parse_phandle(np, "ssi-controller", 0);
  413. if (!cpu_np) {
  414. dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
  415. ret = -EINVAL;
  416. goto fail;
  417. }
  418. cpu_pdev = of_find_device_by_node(cpu_np);
  419. if (!cpu_pdev) {
  420. dev_err(&pdev->dev, "failed to find CPU DAI device\n");
  421. ret = -EINVAL;
  422. goto fail;
  423. }
  424. codec_np = of_parse_phandle(np, "audio-codec", 0);
  425. if (codec_np)
  426. codec_dev = of_find_i2c_device_by_node(codec_np);
  427. else
  428. codec_dev = NULL;
  429. asrc_np = of_parse_phandle(np, "audio-asrc", 0);
  430. if (asrc_np)
  431. asrc_pdev = of_find_device_by_node(asrc_np);
  432. /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
  433. if (codec_dev) {
  434. struct clk *codec_clk = clk_get(&codec_dev->dev, NULL);
  435. if (!IS_ERR(codec_clk)) {
  436. priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
  437. clk_put(codec_clk);
  438. }
  439. }
  440. /* Default sample rate and format, will be updated in hw_params() */
  441. priv->sample_rate = 44100;
  442. priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
  443. /* Assign a default DAI format, and allow each card to overwrite it */
  444. priv->dai_fmt = DAI_FMT_BASE;
  445. /* Diversify the card configurations */
  446. if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
  447. codec_dai_name = "cs42888";
  448. priv->card.set_bias_level = NULL;
  449. priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
  450. priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
  451. priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
  452. priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
  453. priv->cpu_priv.slot_width = 32;
  454. priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  455. } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
  456. codec_dai_name = "sgtl5000";
  457. priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
  458. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  459. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
  460. codec_dai_name = "wm8962";
  461. priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
  462. priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
  463. priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
  464. priv->codec_priv.pll_id = WM8962_FLL;
  465. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  466. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
  467. codec_dai_name = "wm8960-hifi";
  468. priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
  469. priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
  470. priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
  471. priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  472. } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
  473. codec_dai_name = "ac97-hifi";
  474. priv->card.set_bias_level = NULL;
  475. priv->dai_fmt = SND_SOC_DAIFMT_AC97;
  476. } else {
  477. dev_err(&pdev->dev, "unknown Device Tree compatible\n");
  478. ret = -EINVAL;
  479. goto asrc_fail;
  480. }
  481. if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
  482. dev_err(&pdev->dev, "failed to find codec device\n");
  483. ret = -EINVAL;
  484. goto asrc_fail;
  485. }
  486. /* Common settings for corresponding Freescale CPU DAI driver */
  487. if (strstr(cpu_np->name, "ssi")) {
  488. /* Only SSI needs to configure AUDMUX */
  489. ret = fsl_asoc_card_audmux_init(np, priv);
  490. if (ret) {
  491. dev_err(&pdev->dev, "failed to init audmux\n");
  492. goto asrc_fail;
  493. }
  494. } else if (strstr(cpu_np->name, "esai")) {
  495. priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
  496. priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
  497. } else if (strstr(cpu_np->name, "sai")) {
  498. priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
  499. priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
  500. }
  501. snprintf(priv->name, sizeof(priv->name), "%s-audio",
  502. fsl_asoc_card_is_ac97(priv) ? "ac97" :
  503. codec_dev->name);
  504. /* Initialize sound card */
  505. priv->pdev = pdev;
  506. priv->card.dev = &pdev->dev;
  507. priv->card.name = priv->name;
  508. priv->card.dai_link = priv->dai_link;
  509. priv->card.dapm_routes = audio_map;
  510. priv->card.late_probe = fsl_asoc_card_late_probe;
  511. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
  512. priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
  513. priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
  514. memcpy(priv->dai_link, fsl_asoc_card_dai,
  515. sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
  516. ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
  517. if (ret) {
  518. dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
  519. goto asrc_fail;
  520. }
  521. /* Normal DAI Link */
  522. priv->dai_link[0].cpu_of_node = cpu_np;
  523. priv->dai_link[0].codec_dai_name = codec_dai_name;
  524. if (!fsl_asoc_card_is_ac97(priv))
  525. priv->dai_link[0].codec_of_node = codec_np;
  526. else {
  527. u32 idx;
  528. ret = of_property_read_u32(cpu_np, "cell-index", &idx);
  529. if (ret) {
  530. dev_err(&pdev->dev,
  531. "cannot get CPU index property\n");
  532. goto asrc_fail;
  533. }
  534. priv->dai_link[0].codec_name =
  535. devm_kasprintf(&pdev->dev, GFP_KERNEL,
  536. "ac97-codec.%u",
  537. (unsigned int)idx);
  538. }
  539. priv->dai_link[0].platform_of_node = cpu_np;
  540. priv->dai_link[0].dai_fmt = priv->dai_fmt;
  541. priv->card.num_links = 1;
  542. if (asrc_pdev) {
  543. /* DPCM DAI Links only if ASRC exsits */
  544. priv->dai_link[1].cpu_of_node = asrc_np;
  545. priv->dai_link[1].platform_of_node = asrc_np;
  546. priv->dai_link[2].codec_dai_name = codec_dai_name;
  547. priv->dai_link[2].codec_of_node = codec_np;
  548. priv->dai_link[2].codec_name =
  549. priv->dai_link[0].codec_name;
  550. priv->dai_link[2].cpu_of_node = cpu_np;
  551. priv->dai_link[2].dai_fmt = priv->dai_fmt;
  552. priv->card.num_links = 3;
  553. ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
  554. &priv->asrc_rate);
  555. if (ret) {
  556. dev_err(&pdev->dev, "failed to get output rate\n");
  557. ret = -EINVAL;
  558. goto asrc_fail;
  559. }
  560. ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
  561. if (ret) {
  562. dev_err(&pdev->dev, "failed to get output rate\n");
  563. ret = -EINVAL;
  564. goto asrc_fail;
  565. }
  566. if (width == 24)
  567. priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
  568. else
  569. priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
  570. }
  571. /* Finish card registering */
  572. platform_set_drvdata(pdev, priv);
  573. snd_soc_card_set_drvdata(&priv->card, priv);
  574. ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
  575. if (ret)
  576. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  577. asrc_fail:
  578. of_node_put(asrc_np);
  579. of_node_put(codec_np);
  580. put_device(&cpu_pdev->dev);
  581. fail:
  582. of_node_put(cpu_np);
  583. return ret;
  584. }
  585. static const struct of_device_id fsl_asoc_card_dt_ids[] = {
  586. { .compatible = "fsl,imx-audio-ac97", },
  587. { .compatible = "fsl,imx-audio-cs42888", },
  588. { .compatible = "fsl,imx-audio-sgtl5000", },
  589. { .compatible = "fsl,imx-audio-wm8962", },
  590. { .compatible = "fsl,imx-audio-wm8960", },
  591. {}
  592. };
  593. MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
  594. static struct platform_driver fsl_asoc_card_driver = {
  595. .probe = fsl_asoc_card_probe,
  596. .driver = {
  597. .name = "fsl-asoc-card",
  598. .pm = &snd_soc_pm_ops,
  599. .of_match_table = fsl_asoc_card_dt_ids,
  600. },
  601. };
  602. module_platform_driver(fsl_asoc_card_driver);
  603. MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
  604. MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
  605. MODULE_ALIAS("platform:fsl-asoc-card");
  606. MODULE_LICENSE("GPL");