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