1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Sound card driver for Intel Haswell Lynx Point with Realtek 5640 4 * 5 * Copyright (C) 2013, Intel Corporation 6 */ 7 8 #include <linux/module.h> 9 #include <linux/platform_device.h> 10 #include <sound/core.h> 11 #include <sound/pcm.h> 12 #include <sound/pcm_params.h> 13 #include <sound/soc.h> 14 #include <sound/soc-acpi.h> 15 #include "../../codecs/rt5640.h" 16 17 static const struct snd_soc_dapm_widget card_widgets[] = { 18 SND_SOC_DAPM_HP("Headphones", NULL), 19 SND_SOC_DAPM_MIC("Mic", NULL), 20 }; 21 22 static const struct snd_soc_dapm_route card_routes[] = { 23 {"Headphones", NULL, "HPOR"}, 24 {"Headphones", NULL, "HPOL"}, 25 {"IN2P", NULL, "Mic"}, 26 27 /* CODEC BE connections */ 28 {"SSP0 CODEC IN", NULL, "AIF1 Capture"}, 29 {"AIF1 Playback", NULL, "SSP0 CODEC OUT"}, 30 }; 31 32 static int codec_link_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 33 struct snd_pcm_hw_params *params) 34 { 35 struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 36 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 37 38 /* The ADSP will convert the FE rate to 48k, stereo. */ 39 rate->min = rate->max = 48000; 40 channels->min = channels->max = 2; 41 /* Set SSP0 to 16 bit. */ 42 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE); 43 44 return 0; 45 } 46 47 static int codec_link_hw_params(struct snd_pcm_substream *substream, 48 struct snd_pcm_hw_params *params) 49 { 50 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 51 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 52 int ret; 53 54 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, 12288000, SND_SOC_CLOCK_IN); 55 if (ret < 0) { 56 dev_err(rtd->dev, "set codec sysclk failed: %d\n", ret); 57 return ret; 58 } 59 60 /* Set correct codec filter for DAI format and clock config. */ 61 snd_soc_component_update_bits(codec_dai->component, 0x83, 0xffff, 0x8000); 62 63 return ret; 64 } 65 66 static const struct snd_soc_ops codec_link_ops = { 67 .hw_params = codec_link_hw_params, 68 }; 69 70 SND_SOC_DAILINK_DEF(system, DAILINK_COMP_ARRAY(COMP_CPU("System Pin"))); 71 SND_SOC_DAILINK_DEF(offload0, DAILINK_COMP_ARRAY(COMP_CPU("Offload0 Pin"))); 72 SND_SOC_DAILINK_DEF(offload1, DAILINK_COMP_ARRAY(COMP_CPU("Offload1 Pin"))); 73 SND_SOC_DAILINK_DEF(loopback, DAILINK_COMP_ARRAY(COMP_CPU("Loopback Pin"))); 74 75 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY())); 76 SND_SOC_DAILINK_DEF(codec, DAILINK_COMP_ARRAY(COMP_CODEC("i2c-INT33CA:00", "rt5640-aif1"))); 77 SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio"))); 78 SND_SOC_DAILINK_DEF(ssp0_port, DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port"))); 79 80 static struct snd_soc_dai_link card_dai_links[] = { 81 /* Front End DAI links */ 82 { 83 .name = "System", 84 .stream_name = "System Playback/Capture", 85 .nonatomic = 1, 86 .dynamic = 1, 87 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 88 SND_SOC_DAILINK_REG(system, dummy, platform), 89 }, 90 { 91 .name = "Offload0", 92 .stream_name = "Offload0 Playback", 93 .nonatomic = 1, 94 .dynamic = 1, 95 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 96 .playback_only = 1, 97 SND_SOC_DAILINK_REG(offload0, dummy, platform), 98 }, 99 { 100 .name = "Offload1", 101 .stream_name = "Offload1 Playback", 102 .nonatomic = 1, 103 .dynamic = 1, 104 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 105 .playback_only = 1, 106 SND_SOC_DAILINK_REG(offload1, dummy, platform), 107 }, 108 { 109 .name = "Loopback", 110 .stream_name = "Loopback", 111 .nonatomic = 1, 112 .dynamic = 1, 113 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 114 .capture_only = 1, 115 SND_SOC_DAILINK_REG(loopback, dummy, platform), 116 }, 117 /* Back End DAI links */ 118 { 119 /* SSP0 - Codec */ 120 .name = "Codec", 121 .id = 0, 122 .nonatomic = 1, 123 .no_pcm = 1, 124 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC, 125 .ignore_pmdown_time = 1, 126 .be_hw_params_fixup = codec_link_hw_params_fixup, 127 .ops = &codec_link_ops, 128 SND_SOC_DAILINK_REG(ssp0_port, codec, platform), 129 }, 130 }; 131 132 static struct snd_soc_card hsw_rt5640_card = { 133 .name = "haswell-rt5640", 134 .owner = THIS_MODULE, 135 .dai_link = card_dai_links, 136 .num_links = ARRAY_SIZE(card_dai_links), 137 .dapm_widgets = card_widgets, 138 .num_dapm_widgets = ARRAY_SIZE(card_widgets), 139 .dapm_routes = card_routes, 140 .num_dapm_routes = ARRAY_SIZE(card_routes), 141 .fully_routed = true, 142 }; 143 144 static int hsw_rt5640_probe(struct platform_device *pdev) 145 { 146 struct snd_soc_acpi_mach *mach; 147 struct device *dev = &pdev->dev; 148 int ret; 149 150 hsw_rt5640_card.dev = dev; 151 mach = dev_get_platdata(dev); 152 153 ret = snd_soc_fixup_dai_links_platform_name(&hsw_rt5640_card, mach->mach_params.platform); 154 if (ret) 155 return ret; 156 157 return devm_snd_soc_register_card(dev, &hsw_rt5640_card); 158 } 159 160 static struct platform_driver hsw_rt5640_driver = { 161 .probe = hsw_rt5640_probe, 162 .driver = { 163 .name = "hsw_rt5640", 164 .pm = &snd_soc_pm_ops, 165 }, 166 }; 167 168 module_platform_driver(hsw_rt5640_driver) 169 170 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang"); 171 MODULE_DESCRIPTION("Sound card driver for Intel Haswell Lynx Point with Realtek 5640"); 172 MODULE_LICENSE("GPL"); 173 MODULE_ALIAS("platform:hsw_rt5640"); 174