fsl-asoc-card.c 20 KB

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