18b99e24dSCezary Rojewski // SPDX-License-Identifier: GPL-2.0-only
28b99e24dSCezary Rojewski /*
32c53debbSCezary Rojewski * Sound card driver for Intel Haswell Lynx Point with Realtek 5640
48b99e24dSCezary Rojewski *
58b99e24dSCezary Rojewski * Copyright (C) 2013, Intel Corporation. All rights reserved.
68b99e24dSCezary Rojewski */
78b99e24dSCezary Rojewski
88b99e24dSCezary Rojewski #include <linux/module.h>
98b99e24dSCezary Rojewski #include <linux/platform_device.h>
108b99e24dSCezary Rojewski #include <sound/core.h>
118b99e24dSCezary Rojewski #include <sound/pcm.h>
125b66dde4SCezary Rojewski #include <sound/pcm_params.h>
138b99e24dSCezary Rojewski #include <sound/soc.h>
148b99e24dSCezary Rojewski #include <sound/soc-acpi.h>
158b99e24dSCezary Rojewski #include "../../codecs/rt5640.h"
168b99e24dSCezary Rojewski
17675002b6SCezary Rojewski static const struct snd_soc_dapm_widget card_widgets[] = {
188b99e24dSCezary Rojewski SND_SOC_DAPM_HP("Headphones", NULL),
198b99e24dSCezary Rojewski SND_SOC_DAPM_MIC("Mic", NULL),
208b99e24dSCezary Rojewski };
218b99e24dSCezary Rojewski
22675002b6SCezary Rojewski static const struct snd_soc_dapm_route card_routes[] = {
238b99e24dSCezary Rojewski {"Headphones", NULL, "HPOR"},
248b99e24dSCezary Rojewski {"Headphones", NULL, "HPOL"},
258b99e24dSCezary Rojewski {"IN2P", NULL, "Mic"},
268b99e24dSCezary Rojewski
278b99e24dSCezary Rojewski /* CODEC BE connections */
288b99e24dSCezary Rojewski {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
298b99e24dSCezary Rojewski {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
308b99e24dSCezary Rojewski };
318b99e24dSCezary Rojewski
codec_link_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)32675002b6SCezary Rojewski static int codec_link_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
338b99e24dSCezary Rojewski struct snd_pcm_hw_params *params)
348b99e24dSCezary Rojewski {
355b66dde4SCezary Rojewski struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
365b66dde4SCezary Rojewski struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
378b99e24dSCezary Rojewski
382c53debbSCezary Rojewski /* The ADSP will convert the FE rate to 48k, stereo. */
398b99e24dSCezary Rojewski rate->min = rate->max = 48000;
408b99e24dSCezary Rojewski channels->min = channels->max = 2;
412c53debbSCezary Rojewski /* Set SSP0 to 16 bit. */
428b99e24dSCezary Rojewski params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
435b66dde4SCezary Rojewski
448b99e24dSCezary Rojewski return 0;
458b99e24dSCezary Rojewski }
468b99e24dSCezary Rojewski
codec_link_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)47675002b6SCezary Rojewski static int codec_link_hw_params(struct snd_pcm_substream *substream,
488b99e24dSCezary Rojewski struct snd_pcm_hw_params *params)
498b99e24dSCezary Rojewski {
508b99e24dSCezary Rojewski struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
518b99e24dSCezary Rojewski struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
528b99e24dSCezary Rojewski int ret;
538b99e24dSCezary Rojewski
545b66dde4SCezary Rojewski ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_MCLK, 12288000, SND_SOC_CLOCK_IN);
558b99e24dSCezary Rojewski if (ret < 0) {
566c659082SCezary Rojewski dev_err(rtd->dev, "set codec sysclk failed: %d\n", ret);
578b99e24dSCezary Rojewski return ret;
588b99e24dSCezary Rojewski }
598b99e24dSCezary Rojewski
602c53debbSCezary Rojewski /* Set correct codec filter for DAI format and clock config. */
618b99e24dSCezary Rojewski snd_soc_component_update_bits(codec_dai->component, 0x83, 0xffff, 0x8000);
628b99e24dSCezary Rojewski
638b99e24dSCezary Rojewski return ret;
648b99e24dSCezary Rojewski }
658b99e24dSCezary Rojewski
66675002b6SCezary Rojewski static const struct snd_soc_ops codec_link_ops = {
67675002b6SCezary Rojewski .hw_params = codec_link_hw_params,
688b99e24dSCezary Rojewski };
698b99e24dSCezary Rojewski
705b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(system, DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
715b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(offload0, DAILINK_COMP_ARRAY(COMP_CPU("Offload0 Pin")));
725b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(offload1, DAILINK_COMP_ARRAY(COMP_CPU("Offload1 Pin")));
735b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(loopback, DAILINK_COMP_ARRAY(COMP_CPU("Loopback Pin")));
748b99e24dSCezary Rojewski
755b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
765b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(codec, DAILINK_COMP_ARRAY(COMP_CODEC("i2c-INT33CA:00", "rt5640-aif1")));
775b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
785b66dde4SCezary Rojewski SND_SOC_DAILINK_DEF(ssp0_port, DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
798b99e24dSCezary Rojewski
80675002b6SCezary Rojewski static struct snd_soc_dai_link card_dai_links[] = {
818b99e24dSCezary Rojewski /* Front End DAI links */
828b99e24dSCezary Rojewski {
838b99e24dSCezary Rojewski .name = "System",
848b99e24dSCezary Rojewski .stream_name = "System Playback/Capture",
858b99e24dSCezary Rojewski .nonatomic = 1,
868b99e24dSCezary Rojewski .dynamic = 1,
878b99e24dSCezary Rojewski .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
888b99e24dSCezary Rojewski .dpcm_playback = 1,
898b99e24dSCezary Rojewski .dpcm_capture = 1,
908b99e24dSCezary Rojewski SND_SOC_DAILINK_REG(system, dummy, platform),
918b99e24dSCezary Rojewski },
928b99e24dSCezary Rojewski {
938b99e24dSCezary Rojewski .name = "Offload0",
948b99e24dSCezary Rojewski .stream_name = "Offload0 Playback",
958b99e24dSCezary Rojewski .nonatomic = 1,
968b99e24dSCezary Rojewski .dynamic = 1,
978b99e24dSCezary Rojewski .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
988b99e24dSCezary Rojewski .dpcm_playback = 1,
998b99e24dSCezary Rojewski SND_SOC_DAILINK_REG(offload0, dummy, platform),
1008b99e24dSCezary Rojewski },
1018b99e24dSCezary Rojewski {
1028b99e24dSCezary Rojewski .name = "Offload1",
1038b99e24dSCezary Rojewski .stream_name = "Offload1 Playback",
1048b99e24dSCezary Rojewski .nonatomic = 1,
1058b99e24dSCezary Rojewski .dynamic = 1,
1068b99e24dSCezary Rojewski .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
1078b99e24dSCezary Rojewski .dpcm_playback = 1,
1088b99e24dSCezary Rojewski SND_SOC_DAILINK_REG(offload1, dummy, platform),
1098b99e24dSCezary Rojewski },
1108b99e24dSCezary Rojewski {
1118b99e24dSCezary Rojewski .name = "Loopback",
1128b99e24dSCezary Rojewski .stream_name = "Loopback",
1138b99e24dSCezary Rojewski .nonatomic = 1,
1148b99e24dSCezary Rojewski .dynamic = 1,
1158b99e24dSCezary Rojewski .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
1168b99e24dSCezary Rojewski .dpcm_capture = 1,
1178b99e24dSCezary Rojewski SND_SOC_DAILINK_REG(loopback, dummy, platform),
1188b99e24dSCezary Rojewski },
1198b99e24dSCezary Rojewski /* Back End DAI links */
1208b99e24dSCezary Rojewski {
1218b99e24dSCezary Rojewski /* SSP0 - Codec */
1228b99e24dSCezary Rojewski .name = "Codec",
1238b99e24dSCezary Rojewski .id = 0,
124*58ef0d3dSCezary Rojewski .nonatomic = 1,
1258b99e24dSCezary Rojewski .no_pcm = 1,
1265b66dde4SCezary Rojewski .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC,
1278b99e24dSCezary Rojewski .ignore_pmdown_time = 1,
128675002b6SCezary Rojewski .be_hw_params_fixup = codec_link_hw_params_fixup,
129675002b6SCezary Rojewski .ops = &codec_link_ops,
1308b99e24dSCezary Rojewski .dpcm_playback = 1,
1318b99e24dSCezary Rojewski .dpcm_capture = 1,
1328b99e24dSCezary Rojewski SND_SOC_DAILINK_REG(ssp0_port, codec, platform),
1338b99e24dSCezary Rojewski },
1348b99e24dSCezary Rojewski };
1358b99e24dSCezary Rojewski
136675002b6SCezary Rojewski static struct snd_soc_card hsw_rt5640_card = {
1378b99e24dSCezary Rojewski .name = "haswell-rt5640",
1388b99e24dSCezary Rojewski .owner = THIS_MODULE,
139675002b6SCezary Rojewski .dai_link = card_dai_links,
140675002b6SCezary Rojewski .num_links = ARRAY_SIZE(card_dai_links),
141675002b6SCezary Rojewski .dapm_widgets = card_widgets,
142675002b6SCezary Rojewski .num_dapm_widgets = ARRAY_SIZE(card_widgets),
143675002b6SCezary Rojewski .dapm_routes = card_routes,
144675002b6SCezary Rojewski .num_dapm_routes = ARRAY_SIZE(card_routes),
1458b99e24dSCezary Rojewski .fully_routed = true,
1468b99e24dSCezary Rojewski };
1478b99e24dSCezary Rojewski
hsw_rt5640_probe(struct platform_device * pdev)148675002b6SCezary Rojewski static int hsw_rt5640_probe(struct platform_device *pdev)
1498b99e24dSCezary Rojewski {
1508b99e24dSCezary Rojewski struct snd_soc_acpi_mach *mach;
1510439f262SCezary Rojewski struct device *dev = &pdev->dev;
1528b99e24dSCezary Rojewski int ret;
1538b99e24dSCezary Rojewski
1540439f262SCezary Rojewski hsw_rt5640_card.dev = dev;
1550439f262SCezary Rojewski mach = dev_get_platdata(dev);
1565b66dde4SCezary Rojewski
1575b66dde4SCezary Rojewski ret = snd_soc_fixup_dai_links_platform_name(&hsw_rt5640_card, mach->mach_params.platform);
1588b99e24dSCezary Rojewski if (ret)
1598b99e24dSCezary Rojewski return ret;
1608b99e24dSCezary Rojewski
1610439f262SCezary Rojewski return devm_snd_soc_register_card(dev, &hsw_rt5640_card);
1628b99e24dSCezary Rojewski }
1638b99e24dSCezary Rojewski
164675002b6SCezary Rojewski static struct platform_driver hsw_rt5640_driver = {
165675002b6SCezary Rojewski .probe = hsw_rt5640_probe,
1668b99e24dSCezary Rojewski .driver = {
167a69615e8SCezary Rojewski .name = "hsw_rt5640",
1688b99e24dSCezary Rojewski .pm = &snd_soc_pm_ops,
1698b99e24dSCezary Rojewski },
1708b99e24dSCezary Rojewski };
1718b99e24dSCezary Rojewski
172675002b6SCezary Rojewski module_platform_driver(hsw_rt5640_driver)
1738b99e24dSCezary Rojewski
1748b99e24dSCezary Rojewski MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1752c53debbSCezary Rojewski MODULE_DESCRIPTION("Sound card driver for Intel Haswell Lynx Point with Realtek 5640");
1762c53debbSCezary Rojewski MODULE_LICENSE("GPL");
177a69615e8SCezary Rojewski MODULE_ALIAS("platform:hsw_rt5640");
178