Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Machine driver for AMD ACP Audio engine using Realtek RT5645 codec
0003  *
0004  * Copyright 2017 Advanced Micro Devices, Inc.
0005  *
0006  * This file is modified from rt288 machine driver
0007  *
0008  * Permission is hereby granted, free of charge, to any person obtaining a
0009  * copy of this software and associated documentation files (the "Software"),
0010  * to deal in the Software without restriction, including without limitation
0011  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0012  * and/or sell copies of the Software, and to permit persons to whom the
0013  * Software is furnished to do so, subject to the following conditions:
0014  *
0015  * The above copyright notice and this permission notice shall be included in
0016  * all copies or substantial portions of the Software.
0017  *
0018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0021  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0022  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0023  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0024  * OTHER DEALINGS IN THE SOFTWARE.
0025  *
0026  *
0027  */
0028 
0029 #include <sound/core.h>
0030 #include <sound/soc.h>
0031 #include <sound/pcm.h>
0032 #include <sound/pcm_params.h>
0033 #include <sound/soc-dapm.h>
0034 #include <sound/jack.h>
0035 #include <linux/gpio.h>
0036 #include <linux/module.h>
0037 #include <linux/i2c.h>
0038 #include <linux/acpi.h>
0039 
0040 #include "../codecs/rt5645.h"
0041 
0042 #define CZ_PLAT_CLK 24000000
0043 
0044 static struct snd_soc_jack cz_jack;
0045 
0046 static int cz_aif1_hw_params(struct snd_pcm_substream *substream,
0047                  struct snd_pcm_hw_params *params)
0048 {
0049     int ret = 0;
0050     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0051     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0052 
0053     ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
0054                   CZ_PLAT_CLK, params_rate(params) * 512);
0055     if (ret < 0) {
0056         dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
0057         return ret;
0058     }
0059 
0060     ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
0061                 params_rate(params) * 512, SND_SOC_CLOCK_OUT);
0062     if (ret < 0) {
0063         dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
0064         return ret;
0065     }
0066 
0067     return ret;
0068 }
0069 
0070 static int cz_init(struct snd_soc_pcm_runtime *rtd)
0071 {
0072     int ret;
0073     struct snd_soc_card *card;
0074     struct snd_soc_component *codec;
0075 
0076     codec = asoc_rtd_to_codec(rtd, 0)->component;
0077     card = rtd->card;
0078 
0079     ret = snd_soc_card_jack_new(card, "Headset Jack",
0080                 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
0081                 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0082                 SND_JACK_BTN_2 | SND_JACK_BTN_3,
0083                 &cz_jack);
0084     if (ret) {
0085         dev_err(card->dev, "HP jack creation failed %d\n", ret);
0086         return ret;
0087     }
0088 
0089     rt5645_set_jack_detect(codec, &cz_jack, &cz_jack, &cz_jack);
0090 
0091     return 0;
0092 }
0093 
0094 static const struct snd_soc_ops cz_aif1_ops = {
0095     .hw_params = cz_aif1_hw_params,
0096 };
0097 
0098 SND_SOC_DAILINK_DEF(designware1,
0099     DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.1.auto")));
0100 SND_SOC_DAILINK_DEF(designware2,
0101     DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.2.auto")));
0102 
0103 SND_SOC_DAILINK_DEF(codec,
0104     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1")));
0105 
0106 SND_SOC_DAILINK_DEF(platform,
0107     DAILINK_COMP_ARRAY(COMP_PLATFORM("acp_audio_dma.0.auto")));
0108 
0109 static struct snd_soc_dai_link cz_dai_rt5650[] = {
0110     {
0111         .name = "amd-rt5645-play",
0112         .stream_name = "RT5645_AIF1",
0113         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
0114                 | SND_SOC_DAIFMT_CBP_CFP,
0115         .init = cz_init,
0116         .ops = &cz_aif1_ops,
0117         SND_SOC_DAILINK_REG(designware1, codec, platform),
0118     },
0119     {
0120         .name = "amd-rt5645-cap",
0121         .stream_name = "RT5645_AIF1",
0122         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
0123                 | SND_SOC_DAIFMT_CBP_CFP,
0124         .ops = &cz_aif1_ops,
0125         SND_SOC_DAILINK_REG(designware2, codec, platform),
0126     },
0127 };
0128 
0129 static const struct snd_soc_dapm_widget cz_widgets[] = {
0130     SND_SOC_DAPM_HP("Headphones", NULL),
0131     SND_SOC_DAPM_SPK("Speakers", NULL),
0132     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0133     SND_SOC_DAPM_MIC("Int Mic", NULL),
0134 };
0135 
0136 static const struct snd_soc_dapm_route cz_audio_route[] = {
0137     {"Headphones", NULL, "HPOL"},
0138     {"Headphones", NULL, "HPOR"},
0139     {"RECMIXL", NULL, "Headset Mic"},
0140     {"RECMIXR", NULL, "Headset Mic"},
0141     {"Speakers", NULL, "SPOL"},
0142     {"Speakers", NULL, "SPOR"},
0143     {"DMIC L2", NULL, "Int Mic"},
0144     {"DMIC R2", NULL, "Int Mic"},
0145 };
0146 
0147 static const struct snd_kcontrol_new cz_mc_controls[] = {
0148     SOC_DAPM_PIN_SWITCH("Headphones"),
0149     SOC_DAPM_PIN_SWITCH("Speakers"),
0150     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0151     SOC_DAPM_PIN_SWITCH("Int Mic"),
0152 };
0153 
0154 static struct snd_soc_card cz_card = {
0155     .name = "acprt5650",
0156     .owner = THIS_MODULE,
0157     .dai_link = cz_dai_rt5650,
0158     .num_links = ARRAY_SIZE(cz_dai_rt5650),
0159     .dapm_widgets = cz_widgets,
0160     .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
0161     .dapm_routes = cz_audio_route,
0162     .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
0163     .controls = cz_mc_controls,
0164     .num_controls = ARRAY_SIZE(cz_mc_controls),
0165 };
0166 
0167 static int cz_probe(struct platform_device *pdev)
0168 {
0169     int ret;
0170     struct snd_soc_card *card;
0171 
0172     card = &cz_card;
0173     cz_card.dev = &pdev->dev;
0174     platform_set_drvdata(pdev, card);
0175     ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
0176     if (ret) {
0177         dev_err(&pdev->dev,
0178                 "devm_snd_soc_register_card(%s) failed: %d\n",
0179                 cz_card.name, ret);
0180         return ret;
0181     }
0182     return 0;
0183 }
0184 
0185 #ifdef CONFIG_ACPI
0186 static const struct acpi_device_id cz_audio_acpi_match[] = {
0187     { "AMDI1002", 0 },
0188     {},
0189 };
0190 MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
0191 #endif
0192 
0193 static struct platform_driver cz_pcm_driver = {
0194     .driver = {
0195         .name = "cz-rt5645",
0196         .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
0197         .pm = &snd_soc_pm_ops,
0198     },
0199     .probe = cz_probe,
0200 };
0201 
0202 module_platform_driver(cz_pcm_driver);
0203 
0204 MODULE_AUTHOR("akshu.agrawal@amd.com");
0205 MODULE_DESCRIPTION("cz-rt5645 audio support");
0206 MODULE_LICENSE("GPL v2");