fsl-asoc-card.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 <[email protected]>
  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 <sound/jack.h>
  18. #include <sound/simple_card_utils.h>
  19. #include "fsl_esai.h"
  20. #include "fsl_sai.h"
  21. #include "imx-audmux.h"
  22. #include "../codecs/sgtl5000.h"
  23. #include "../codecs/wm8962.h"
  24. #include "../codecs/wm8960.h"
  25. #include "../codecs/wm8994.h"
  26. #include "../codecs/tlv320aic31xx.h"
  27. #define CS427x_SYSCLK_MCLK 0
  28. #define RX 0
  29. #define TX 1
  30. /* Default DAI format without Master and Slave flag */
  31. #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
  32. /**
  33. * struct codec_priv - CODEC private data
  34. * @mclk_freq: Clock rate of MCLK
  35. * @free_freq: Clock rate of MCLK for hw_free()
  36. * @mclk_id: MCLK (or main clock) id for set_sysclk()
  37. * @fll_id: FLL (or secordary clock) id for set_sysclk()
  38. * @pll_id: PLL id for set_pll()
  39. */
  40. struct codec_priv {
  41. unsigned long mclk_freq;
  42. unsigned long free_freq;
  43. u32 mclk_id;
  44. u32 fll_id;
  45. u32 pll_id;
  46. };
  47. /**
  48. * struct cpu_priv - CPU private data
  49. * @sysclk_freq: SYSCLK rates for set_sysclk()
  50. * @sysclk_dir: SYSCLK directions for set_sysclk()
  51. * @sysclk_id: SYSCLK ids for set_sysclk()
  52. * @slot_width: Slot width of each frame
  53. *
  54. * Note: [1] for tx and [0] for rx
  55. */
  56. struct cpu_priv {
  57. unsigned long sysclk_freq[2];
  58. u32 sysclk_dir[2];
  59. u32 sysclk_id[2];
  60. u32 slot_width;
  61. };
  62. /**
  63. * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
  64. * @dai_link: DAI link structure including normal one and DPCM link
  65. * @hp_jack: Headphone Jack structure
  66. * @mic_jack: Microphone Jack structure
  67. * @pdev: platform device pointer
  68. * @codec_priv: CODEC private data
  69. * @cpu_priv: CPU private data
  70. * @card: ASoC card structure
  71. * @streams: Mask of current active streams
  72. * @sample_rate: Current sample rate
  73. * @sample_format: Current sample format
  74. * @asrc_rate: ASRC sample rate used by Back-Ends
  75. * @asrc_format: ASRC sample format used by Back-Ends
  76. * @dai_fmt: DAI format between CPU and CODEC
  77. * @name: Card name
  78. */
  79. struct fsl_asoc_card_priv {
  80. struct snd_soc_dai_link dai_link[3];
  81. struct asoc_simple_jack hp_jack;
  82. struct asoc_simple_jack mic_jack;
  83. struct platform_device *pdev;
  84. struct codec_priv codec_priv;
  85. struct cpu_priv cpu_priv;
  86. struct snd_soc_card card;
  87. u8 streams;
  88. u32 sample_rate;
  89. snd_pcm_format_t sample_format;
  90. u32 asrc_rate;
  91. snd_pcm_format_t asrc_format;
  92. u32 dai_fmt;
  93. char name[32];
  94. };
  95. /*
  96. * This dapm route map exists for DPCM link only.
  97. * The other routes shall go through Device Tree.
  98. *
  99. * Note: keep all ASRC routes in the second half
  100. * to drop them easily for non-ASRC cases.
  101. */
  102. static const struct snd_soc_dapm_route audio_map[] = {
  103. /* 1st half -- Normal DAPM routes */
  104. {"Playback", NULL, "CPU-Playback"},
  105. {"CPU-Capture", NULL, "Capture"},
  106. /* 2nd half -- ASRC DAPM routes */
  107. {"CPU-Playback", NULL, "ASRC-Playback"},
  108. {"ASRC-Capture", NULL, "CPU-Capture"},
  109. };
  110. static const struct snd_soc_dapm_route audio_map_ac97[] = {
  111. /* 1st half -- Normal DAPM routes */
  112. {"AC97 Playback", NULL, "CPU AC97 Playback"},
  113. {"CPU AC97 Capture", NULL, "AC97 Capture"},
  114. /* 2nd half -- ASRC DAPM routes */
  115. {"CPU AC97 Playback", NULL, "ASRC-Playback"},
  116. {"ASRC-Capture", NULL, "CPU AC97 Capture"},
  117. };
  118. static const struct snd_soc_dapm_route audio_map_tx[] = {
  119. /* 1st half -- Normal DAPM routes */
  120. {"Playback", NULL, "CPU-Playback"},
  121. /* 2nd half -- ASRC DAPM routes */
  122. {"CPU-Playback", NULL, "ASRC-Playback"},
  123. };
  124. static const struct snd_soc_dapm_route audio_map_rx[] = {
  125. /* 1st half -- Normal DAPM routes */
  126. {"CPU-Capture", NULL, "Capture"},
  127. /* 2nd half -- ASRC DAPM routes */
  128. {"ASRC-Capture", NULL, "CPU-Capture"},
  129. };
  130. /* Add all possible widgets into here without being redundant */
  131. static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
  132. SND_SOC_DAPM_LINE("Line Out Jack", NULL),
  133. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  134. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  135. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  136. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  137. SND_SOC_DAPM_MIC("AMIC", NULL),
  138. SND_SOC_DAPM_MIC("DMIC", NULL),
  139. };
  140. static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
  141. {
  142. return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
  143. }
  144. static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
  145. struct snd_pcm_hw_params *params)
  146. {
  147. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  148. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  149. bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  150. struct codec_priv *codec_priv = &priv->codec_priv;
  151. struct cpu_priv *cpu_priv = &priv->cpu_priv;
  152. struct device *dev = rtd->card->dev;
  153. unsigned int pll_out;
  154. int ret;
  155. priv->sample_rate = params_rate(params);
  156. priv->sample_format = params_format(params);
  157. priv->streams |= BIT(substream->stream);
  158. if (fsl_asoc_card_is_ac97(priv))
  159. return 0;
  160. /* Specific configurations of DAIs starts from here */
  161. ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
  162. cpu_priv->sysclk_freq[tx],
  163. cpu_priv->sysclk_dir[tx]);
  164. if (ret && ret != -ENOTSUPP) {
  165. dev_err(dev, "failed to set sysclk for cpu dai\n");
  166. goto fail;
  167. }
  168. if (cpu_priv->slot_width) {
  169. ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
  170. cpu_priv->slot_width);
  171. if (ret && ret != -ENOTSUPP) {
  172. dev_err(dev, "failed to set TDM slot for cpu dai\n");
  173. goto fail;
  174. }
  175. }
  176. /* Specific configuration for PLL */
  177. if (codec_priv->pll_id && codec_priv->fll_id) {
  178. if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
  179. pll_out = priv->sample_rate * 384;
  180. else
  181. pll_out = priv->sample_rate * 256;
  182. ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
  183. codec_priv->pll_id,
  184. codec_priv->mclk_id,
  185. codec_priv->mclk_freq, pll_out);
  186. if (ret) {
  187. dev_err(dev, "failed to start FLL: %d\n", ret);
  188. goto fail;
  189. }
  190. ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
  191. codec_priv->fll_id,
  192. pll_out, SND_SOC_CLOCK_IN);
  193. if (ret && ret != -ENOTSUPP) {
  194. dev_err(dev, "failed to set SYSCLK: %d\n", ret);
  195. goto fail;
  196. }
  197. }
  198. return 0;
  199. fail:
  200. priv->streams &= ~BIT(substream->stream);
  201. return ret;
  202. }
  203. static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
  204. {
  205. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  206. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  207. struct codec_priv *codec_priv = &priv->codec_priv;
  208. struct device *dev = rtd->card->dev;
  209. int ret;
  210. priv->streams &= ~BIT(substream->stream);
  211. if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
  212. /* Force freq to be free_freq to avoid error message in codec */
  213. ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
  214. codec_priv->mclk_id,
  215. codec_priv->free_freq,
  216. SND_SOC_CLOCK_IN);
  217. if (ret) {
  218. dev_err(dev, "failed to switch away from FLL: %d\n", ret);
  219. return ret;
  220. }
  221. ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
  222. codec_priv->pll_id, 0, 0, 0);
  223. if (ret && ret != -ENOTSUPP) {
  224. dev_err(dev, "failed to stop FLL: %d\n", ret);
  225. return ret;
  226. }
  227. }
  228. return 0;
  229. }
  230. static const struct snd_soc_ops fsl_asoc_card_ops = {
  231. .hw_params = fsl_asoc_card_hw_params,
  232. .hw_free = fsl_asoc_card_hw_free,
  233. };
  234. static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  235. struct snd_pcm_hw_params *params)
  236. {
  237. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  238. struct snd_interval *rate;
  239. struct snd_mask *mask;
  240. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  241. rate->max = rate->min = priv->asrc_rate;
  242. mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  243. snd_mask_none(mask);
  244. snd_mask_set_format(mask, priv->asrc_format);
  245. return 0;
  246. }
  247. SND_SOC_DAILINK_DEFS(hifi,
  248. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  249. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  250. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  251. SND_SOC_DAILINK_DEFS(hifi_fe,
  252. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  253. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  254. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  255. SND_SOC_DAILINK_DEFS(hifi_be,
  256. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  257. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  258. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  259. static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
  260. /* Default ASoC DAI Link*/
  261. {
  262. .name = "HiFi",
  263. .stream_name = "HiFi",
  264. .ops = &fsl_asoc_card_ops,
  265. SND_SOC_DAILINK_REG(hifi),
  266. },
  267. /* DPCM Link between Front-End and Back-End (Optional) */
  268. {
  269. .name = "HiFi-ASRC-FE",
  270. .stream_name = "HiFi-ASRC-FE",
  271. .dpcm_playback = 1,
  272. .dpcm_capture = 1,
  273. .dynamic = 1,
  274. SND_SOC_DAILINK_REG(hifi_fe),
  275. },
  276. {
  277. .name = "HiFi-ASRC-BE",
  278. .stream_name = "HiFi-ASRC-BE",
  279. .be_hw_params_fixup = be_hw_params_fixup,
  280. .ops = &fsl_asoc_card_ops,
  281. .dpcm_playback = 1,
  282. .dpcm_capture = 1,
  283. .no_pcm = 1,
  284. SND_SOC_DAILINK_REG(hifi_be),
  285. },
  286. };
  287. static int fsl_asoc_card_audmux_init(struct device_node *np,
  288. struct fsl_asoc_card_priv *priv)
  289. {
  290. struct device *dev = &priv->pdev->dev;
  291. u32 int_ptcr = 0, ext_ptcr = 0;
  292. int int_port, ext_port;
  293. int ret;
  294. ret = of_property_read_u32(np, "mux-int-port", &int_port);
  295. if (ret) {
  296. dev_err(dev, "mux-int-port missing or invalid\n");
  297. return ret;
  298. }
  299. ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
  300. if (ret) {
  301. dev_err(dev, "mux-ext-port missing or invalid\n");
  302. return ret;
  303. }
  304. /*
  305. * The port numbering in the hardware manual starts at 1, while
  306. * the AUDMUX API expects it starts at 0.
  307. */
  308. int_port--;
  309. ext_port--;
  310. /*
  311. * Use asynchronous mode (6 wires) for all cases except AC97.
  312. * If only 4 wires are needed, just set SSI into
  313. * synchronous mode and enable 4 PADs in IOMUX.
  314. */
  315. switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  316. case SND_SOC_DAIFMT_CBP_CFP:
  317. int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
  318. IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
  319. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  320. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  321. IMX_AUDMUX_V2_PTCR_RFSDIR |
  322. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  323. IMX_AUDMUX_V2_PTCR_TFSDIR |
  324. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  325. break;
  326. case SND_SOC_DAIFMT_CBP_CFC:
  327. int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
  328. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  329. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  330. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  331. ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
  332. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  333. IMX_AUDMUX_V2_PTCR_RFSDIR |
  334. IMX_AUDMUX_V2_PTCR_TFSDIR;
  335. break;
  336. case SND_SOC_DAIFMT_CBC_CFP:
  337. int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
  338. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  339. IMX_AUDMUX_V2_PTCR_RFSDIR |
  340. IMX_AUDMUX_V2_PTCR_TFSDIR;
  341. ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
  342. IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
  343. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  344. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  345. break;
  346. case SND_SOC_DAIFMT_CBC_CFC:
  347. ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
  348. IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
  349. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  350. IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
  351. IMX_AUDMUX_V2_PTCR_RFSDIR |
  352. IMX_AUDMUX_V2_PTCR_RCLKDIR |
  353. IMX_AUDMUX_V2_PTCR_TFSDIR |
  354. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  355. break;
  356. default:
  357. if (!fsl_asoc_card_is_ac97(priv))
  358. return -EINVAL;
  359. }
  360. if (fsl_asoc_card_is_ac97(priv)) {
  361. int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  362. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
  363. IMX_AUDMUX_V2_PTCR_TCLKDIR;
  364. ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  365. IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
  366. IMX_AUDMUX_V2_PTCR_TFSDIR;
  367. }
  368. /* Asynchronous mode can not be set along with RCLKDIR */
  369. if (!fsl_asoc_card_is_ac97(priv)) {
  370. unsigned int pdcr =
  371. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
  372. ret = imx_audmux_v2_configure_port(int_port, 0,
  373. pdcr);
  374. if (ret) {
  375. dev_err(dev, "audmux internal port setup failed\n");
  376. return ret;
  377. }
  378. }
  379. ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
  380. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
  381. if (ret) {
  382. dev_err(dev, "audmux internal port setup failed\n");
  383. return ret;
  384. }
  385. if (!fsl_asoc_card_is_ac97(priv)) {
  386. unsigned int pdcr =
  387. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
  388. ret = imx_audmux_v2_configure_port(ext_port, 0,
  389. pdcr);
  390. if (ret) {
  391. dev_err(dev, "audmux external port setup failed\n");
  392. return ret;
  393. }
  394. }
  395. ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
  396. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
  397. if (ret) {
  398. dev_err(dev, "audmux external port setup failed\n");
  399. return ret;
  400. }
  401. return 0;
  402. }
  403. static int hp_jack_event(struct notifier_block *nb, unsigned long event,
  404. void *data)
  405. {
  406. struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
  407. struct snd_soc_dapm_context *dapm = &jack->card->dapm;
  408. if (event & SND_JACK_HEADPHONE)
  409. /* Disable speaker if headphone is plugged in */
  410. return snd_soc_dapm_disable_pin(dapm, "Ext Spk");
  411. else
  412. return snd_soc_dapm_enable_pin(dapm, "Ext Spk");
  413. }
  414. static struct notifier_block hp_jack_nb = {
  415. .notifier_call = hp_jack_event,
  416. };
  417. static int mic_jack_event(struct notifier_block *nb, unsigned long event,
  418. void *data)
  419. {
  420. struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
  421. struct snd_soc_dapm_context *dapm = &jack->card->dapm;
  422. if (event & SND_JACK_MICROPHONE)
  423. /* Disable dmic if microphone is plugged in */
  424. return snd_soc_dapm_disable_pin(dapm, "DMIC");
  425. else
  426. return snd_soc_dapm_enable_pin(dapm, "DMIC");
  427. }
  428. static struct notifier_block mic_jack_nb = {
  429. .notifier_call = mic_jack_event,
  430. };
  431. static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
  432. {
  433. struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
  434. struct snd_soc_pcm_runtime *rtd = list_first_entry(
  435. &card->rtd_list, struct snd_soc_pcm_runtime, list);
  436. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  437. struct codec_priv *codec_priv = &priv->codec_priv;
  438. struct device *dev = card->dev;
  439. int ret;
  440. if (fsl_asoc_card_is_ac97(priv)) {
  441. #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
  442. struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
  443. struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
  444. /*
  445. * Use slots 3/4 for S/PDIF so SSI won't try to enable
  446. * other slots and send some samples there
  447. * due to SLOTREQ bits for S/PDIF received from codec
  448. */
  449. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  450. AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
  451. #endif
  452. return 0;
  453. }
  454. ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
  455. codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
  456. if (ret && ret != -ENOTSUPP) {
  457. dev_err(dev, "failed to set sysclk in %s\n", __func__);
  458. return ret;
  459. }
  460. return 0;
  461. }
  462. static int fsl_asoc_card_probe(struct platform_device *pdev)
  463. {
  464. struct device_node *cpu_np, *codec_np, *asrc_np;
  465. struct device_node *np = pdev->dev.of_node;
  466. struct platform_device *asrc_pdev = NULL;
  467. struct device_node *bitclkprovider = NULL;
  468. struct device_node *frameprovider = NULL;
  469. struct platform_device *cpu_pdev;
  470. struct fsl_asoc_card_priv *priv;
  471. struct device *codec_dev = NULL;
  472. const char *codec_dai_name;
  473. const char *codec_dev_name;
  474. u32 asrc_fmt = 0;
  475. u32 width;
  476. int ret;
  477. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  478. if (!priv)
  479. return -ENOMEM;
  480. cpu_np = of_parse_phandle(np, "audio-cpu", 0);
  481. /* Give a chance to old DT binding */
  482. if (!cpu_np)
  483. cpu_np = of_parse_phandle(np, "ssi-controller", 0);
  484. if (!cpu_np) {
  485. dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
  486. ret = -EINVAL;
  487. goto fail;
  488. }
  489. cpu_pdev = of_find_device_by_node(cpu_np);
  490. if (!cpu_pdev) {
  491. dev_err(&pdev->dev, "failed to find CPU DAI device\n");
  492. ret = -EINVAL;
  493. goto fail;
  494. }
  495. codec_np = of_parse_phandle(np, "audio-codec", 0);
  496. if (codec_np) {
  497. struct platform_device *codec_pdev;
  498. struct i2c_client *codec_i2c;
  499. codec_i2c = of_find_i2c_device_by_node(codec_np);
  500. if (codec_i2c) {
  501. codec_dev = &codec_i2c->dev;
  502. codec_dev_name = codec_i2c->name;
  503. }
  504. if (!codec_dev) {
  505. codec_pdev = of_find_device_by_node(codec_np);
  506. if (codec_pdev) {
  507. codec_dev = &codec_pdev->dev;
  508. codec_dev_name = codec_pdev->name;
  509. }
  510. }
  511. }
  512. asrc_np = of_parse_phandle(np, "audio-asrc", 0);
  513. if (asrc_np)
  514. asrc_pdev = of_find_device_by_node(asrc_np);
  515. /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
  516. if (codec_dev) {
  517. struct clk *codec_clk = clk_get(codec_dev, NULL);
  518. if (!IS_ERR(codec_clk)) {
  519. priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
  520. clk_put(codec_clk);
  521. }
  522. }
  523. /* Default sample rate and format, will be updated in hw_params() */
  524. priv->sample_rate = 44100;
  525. priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
  526. /* Assign a default DAI format, and allow each card to overwrite it */
  527. priv->dai_fmt = DAI_FMT_BASE;
  528. memcpy(priv->dai_link, fsl_asoc_card_dai,
  529. sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
  530. priv->card.dapm_routes = audio_map;
  531. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
  532. /* Diversify the card configurations */
  533. if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
  534. codec_dai_name = "cs42888";
  535. priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
  536. priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
  537. priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
  538. priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
  539. priv->cpu_priv.slot_width = 32;
  540. priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
  541. } else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
  542. codec_dai_name = "cs4271-hifi";
  543. priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
  544. priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  545. } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
  546. codec_dai_name = "sgtl5000";
  547. priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
  548. priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  549. } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
  550. codec_dai_name = "tlv320aic32x4-hifi";
  551. priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  552. } else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
  553. codec_dai_name = "tlv320dac31xx-hifi";
  554. priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  555. priv->dai_link[1].dpcm_capture = 0;
  556. priv->dai_link[2].dpcm_capture = 0;
  557. priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
  558. priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
  559. priv->card.dapm_routes = audio_map_tx;
  560. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
  561. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
  562. codec_dai_name = "wm8962";
  563. priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
  564. priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
  565. priv->codec_priv.pll_id = WM8962_FLL;
  566. priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  567. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
  568. codec_dai_name = "wm8960-hifi";
  569. priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
  570. priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
  571. priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  572. } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
  573. codec_dai_name = "ac97-hifi";
  574. priv->dai_fmt = SND_SOC_DAIFMT_AC97;
  575. priv->card.dapm_routes = audio_map_ac97;
  576. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
  577. } else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
  578. codec_dai_name = "fsl-mqs-dai";
  579. priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
  580. SND_SOC_DAIFMT_CBC_CFC |
  581. SND_SOC_DAIFMT_NB_NF;
  582. priv->dai_link[1].dpcm_capture = 0;
  583. priv->dai_link[2].dpcm_capture = 0;
  584. priv->card.dapm_routes = audio_map_tx;
  585. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
  586. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
  587. codec_dai_name = "wm8524-hifi";
  588. priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
  589. priv->dai_link[1].dpcm_capture = 0;
  590. priv->dai_link[2].dpcm_capture = 0;
  591. priv->cpu_priv.slot_width = 32;
  592. priv->card.dapm_routes = audio_map_tx;
  593. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
  594. } else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
  595. codec_dai_name = "si476x-codec";
  596. priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
  597. priv->card.dapm_routes = audio_map_rx;
  598. priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
  599. } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
  600. codec_dai_name = "wm8994-aif1";
  601. priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  602. priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1;
  603. priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1;
  604. priv->codec_priv.pll_id = WM8994_FLL1;
  605. priv->codec_priv.free_freq = priv->codec_priv.mclk_freq;
  606. priv->card.dapm_routes = NULL;
  607. priv->card.num_dapm_routes = 0;
  608. } else {
  609. dev_err(&pdev->dev, "unknown Device Tree compatible\n");
  610. ret = -EINVAL;
  611. goto asrc_fail;
  612. }
  613. /*
  614. * Allow setting mclk-id from the device-tree node. Otherwise, the
  615. * default value for each card configuration is used.
  616. */
  617. of_property_read_u32(np, "mclk-id", &priv->codec_priv.mclk_id);
  618. /* Format info from DT is optional. */
  619. snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
  620. if (bitclkprovider || frameprovider) {
  621. unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
  622. if (codec_np == bitclkprovider)
  623. daifmt |= (codec_np == frameprovider) ?
  624. SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
  625. else
  626. daifmt |= (codec_np == frameprovider) ?
  627. SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
  628. /* Override dai_fmt with value from DT */
  629. priv->dai_fmt = daifmt;
  630. }
  631. /* Change direction according to format */
  632. if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
  633. priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
  634. priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
  635. }
  636. of_node_put(bitclkprovider);
  637. of_node_put(frameprovider);
  638. if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
  639. dev_dbg(&pdev->dev, "failed to find codec device\n");
  640. ret = -EPROBE_DEFER;
  641. goto asrc_fail;
  642. }
  643. /* Common settings for corresponding Freescale CPU DAI driver */
  644. if (of_node_name_eq(cpu_np, "ssi")) {
  645. /* Only SSI needs to configure AUDMUX */
  646. ret = fsl_asoc_card_audmux_init(np, priv);
  647. if (ret) {
  648. dev_err(&pdev->dev, "failed to init audmux\n");
  649. goto asrc_fail;
  650. }
  651. } else if (of_node_name_eq(cpu_np, "esai")) {
  652. struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
  653. if (!IS_ERR(esai_clk)) {
  654. priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
  655. priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
  656. clk_put(esai_clk);
  657. } else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
  658. ret = -EPROBE_DEFER;
  659. goto asrc_fail;
  660. }
  661. priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
  662. priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
  663. } else if (of_node_name_eq(cpu_np, "sai")) {
  664. priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
  665. priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
  666. }
  667. /* Initialize sound card */
  668. priv->pdev = pdev;
  669. priv->card.dev = &pdev->dev;
  670. priv->card.owner = THIS_MODULE;
  671. ret = snd_soc_of_parse_card_name(&priv->card, "model");
  672. if (ret) {
  673. snprintf(priv->name, sizeof(priv->name), "%s-audio",
  674. fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
  675. priv->card.name = priv->name;
  676. }
  677. priv->card.dai_link = priv->dai_link;
  678. priv->card.late_probe = fsl_asoc_card_late_probe;
  679. priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
  680. priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
  681. /* Drop the second half of DAPM routes -- ASRC */
  682. if (!asrc_pdev)
  683. priv->card.num_dapm_routes /= 2;
  684. if (of_property_read_bool(np, "audio-routing")) {
  685. ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
  686. if (ret) {
  687. dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
  688. goto asrc_fail;
  689. }
  690. }
  691. /* Normal DAI Link */
  692. priv->dai_link[0].cpus->of_node = cpu_np;
  693. priv->dai_link[0].codecs->dai_name = codec_dai_name;
  694. if (!fsl_asoc_card_is_ac97(priv))
  695. priv->dai_link[0].codecs->of_node = codec_np;
  696. else {
  697. u32 idx;
  698. ret = of_property_read_u32(cpu_np, "cell-index", &idx);
  699. if (ret) {
  700. dev_err(&pdev->dev,
  701. "cannot get CPU index property\n");
  702. goto asrc_fail;
  703. }
  704. priv->dai_link[0].codecs->name =
  705. devm_kasprintf(&pdev->dev, GFP_KERNEL,
  706. "ac97-codec.%u",
  707. (unsigned int)idx);
  708. if (!priv->dai_link[0].codecs->name) {
  709. ret = -ENOMEM;
  710. goto asrc_fail;
  711. }
  712. }
  713. priv->dai_link[0].platforms->of_node = cpu_np;
  714. priv->dai_link[0].dai_fmt = priv->dai_fmt;
  715. priv->card.num_links = 1;
  716. if (asrc_pdev) {
  717. /* DPCM DAI Links only if ASRC exsits */
  718. priv->dai_link[1].cpus->of_node = asrc_np;
  719. priv->dai_link[1].platforms->of_node = asrc_np;
  720. priv->dai_link[2].codecs->dai_name = codec_dai_name;
  721. priv->dai_link[2].codecs->of_node = codec_np;
  722. priv->dai_link[2].codecs->name =
  723. priv->dai_link[0].codecs->name;
  724. priv->dai_link[2].cpus->of_node = cpu_np;
  725. priv->dai_link[2].dai_fmt = priv->dai_fmt;
  726. priv->card.num_links = 3;
  727. ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
  728. &priv->asrc_rate);
  729. if (ret) {
  730. dev_err(&pdev->dev, "failed to get output rate\n");
  731. ret = -EINVAL;
  732. goto asrc_fail;
  733. }
  734. ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);
  735. priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
  736. if (ret) {
  737. /* Fallback to old binding; translate to asrc_format */
  738. ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
  739. &width);
  740. if (ret) {
  741. dev_err(&pdev->dev,
  742. "failed to decide output format\n");
  743. goto asrc_fail;
  744. }
  745. if (width == 24)
  746. priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
  747. else
  748. priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
  749. }
  750. }
  751. /* Finish card registering */
  752. platform_set_drvdata(pdev, priv);
  753. snd_soc_card_set_drvdata(&priv->card, priv);
  754. ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
  755. if (ret) {
  756. dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
  757. goto asrc_fail;
  758. }
  759. /*
  760. * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
  761. * asoc_simple_init_jack uses these properties for creating
  762. * Headphone Jack and Microphone Jack.
  763. *
  764. * The notifier is initialized in snd_soc_card_jack_new(), then
  765. * snd_soc_jack_notifier_register can be called.
  766. */
  767. if (of_property_read_bool(np, "hp-det-gpio")) {
  768. ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
  769. 1, NULL, "Headphone Jack");
  770. if (ret)
  771. goto asrc_fail;
  772. snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
  773. }
  774. if (of_property_read_bool(np, "mic-det-gpio")) {
  775. ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
  776. 0, NULL, "Mic Jack");
  777. if (ret)
  778. goto asrc_fail;
  779. snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
  780. }
  781. asrc_fail:
  782. of_node_put(asrc_np);
  783. of_node_put(codec_np);
  784. put_device(&cpu_pdev->dev);
  785. fail:
  786. of_node_put(cpu_np);
  787. return ret;
  788. }
  789. static const struct of_device_id fsl_asoc_card_dt_ids[] = {
  790. { .compatible = "fsl,imx-audio-ac97", },
  791. { .compatible = "fsl,imx-audio-cs42888", },
  792. { .compatible = "fsl,imx-audio-cs427x", },
  793. { .compatible = "fsl,imx-audio-tlv320aic32x4", },
  794. { .compatible = "fsl,imx-audio-tlv320aic31xx", },
  795. { .compatible = "fsl,imx-audio-sgtl5000", },
  796. { .compatible = "fsl,imx-audio-wm8962", },
  797. { .compatible = "fsl,imx-audio-wm8960", },
  798. { .compatible = "fsl,imx-audio-mqs", },
  799. { .compatible = "fsl,imx-audio-wm8524", },
  800. { .compatible = "fsl,imx-audio-si476x", },
  801. { .compatible = "fsl,imx-audio-wm8958", },
  802. {}
  803. };
  804. MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
  805. static struct platform_driver fsl_asoc_card_driver = {
  806. .probe = fsl_asoc_card_probe,
  807. .driver = {
  808. .name = "fsl-asoc-card",
  809. .pm = &snd_soc_pm_ops,
  810. .of_match_table = fsl_asoc_card_dt_ids,
  811. },
  812. };
  813. module_platform_driver(fsl_asoc_card_driver);
  814. MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
  815. MODULE_AUTHOR("Nicolin Chen <[email protected]>");
  816. MODULE_ALIAS("platform:fsl-asoc-card");
  817. MODULE_LICENSE("GPL");