18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Machine driver for AMD ACP Audio engine using Realtek RT5645 codec
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2017 Advanced Micro Devices, Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is modified from rt288 machine driver
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
98c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
108c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
118c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
128c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
138c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
168c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
198c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
218c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
228c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
238c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
248c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <sound/core.h>
308c2ecf20Sopenharmony_ci#include <sound/soc.h>
318c2ecf20Sopenharmony_ci#include <sound/pcm.h>
328c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
338c2ecf20Sopenharmony_ci#include <sound/soc-dapm.h>
348c2ecf20Sopenharmony_ci#include <sound/jack.h>
358c2ecf20Sopenharmony_ci#include <linux/gpio.h>
368c2ecf20Sopenharmony_ci#include <linux/module.h>
378c2ecf20Sopenharmony_ci#include <linux/i2c.h>
388c2ecf20Sopenharmony_ci#include <linux/acpi.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include "../codecs/rt5645.h"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define CZ_PLAT_CLK 24000000
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic struct snd_soc_jack cz_jack;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int cz_aif1_hw_params(struct snd_pcm_substream *substream,
478c2ecf20Sopenharmony_ci			     struct snd_pcm_hw_params *params)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	int ret = 0;
508c2ecf20Sopenharmony_ci	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
518c2ecf20Sopenharmony_ci	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
548c2ecf20Sopenharmony_ci				  CZ_PLAT_CLK, params_rate(params) * 512);
558c2ecf20Sopenharmony_ci	if (ret < 0) {
568c2ecf20Sopenharmony_ci		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
578c2ecf20Sopenharmony_ci		return ret;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
618c2ecf20Sopenharmony_ci				params_rate(params) * 512, SND_SOC_CLOCK_OUT);
628c2ecf20Sopenharmony_ci	if (ret < 0) {
638c2ecf20Sopenharmony_ci		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
648c2ecf20Sopenharmony_ci		return ret;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return ret;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic int cz_init(struct snd_soc_pcm_runtime *rtd)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	int ret;
738c2ecf20Sopenharmony_ci	struct snd_soc_card *card;
748c2ecf20Sopenharmony_ci	struct snd_soc_component *codec;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	codec = asoc_rtd_to_codec(rtd, 0)->component;
778c2ecf20Sopenharmony_ci	card = rtd->card;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	ret = snd_soc_card_jack_new(card, "Headset Jack",
808c2ecf20Sopenharmony_ci				SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
818c2ecf20Sopenharmony_ci				SND_JACK_BTN_0 | SND_JACK_BTN_1 |
828c2ecf20Sopenharmony_ci				SND_JACK_BTN_2 | SND_JACK_BTN_3,
838c2ecf20Sopenharmony_ci				&cz_jack, NULL, 0);
848c2ecf20Sopenharmony_ci	if (ret) {
858c2ecf20Sopenharmony_ci		dev_err(card->dev, "HP jack creation failed %d\n", ret);
868c2ecf20Sopenharmony_ci		return ret;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	rt5645_set_jack_detect(codec, &cz_jack, &cz_jack, &cz_jack);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	return 0;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic struct snd_soc_ops cz_aif1_ops = {
958c2ecf20Sopenharmony_ci	.hw_params = cz_aif1_hw_params,
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEF(designware1,
998c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.1.auto")));
1008c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEF(designware2,
1018c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.2.auto")));
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEF(codec,
1048c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1")));
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciSND_SOC_DAILINK_DEF(platform,
1078c2ecf20Sopenharmony_ci	DAILINK_COMP_ARRAY(COMP_PLATFORM("acp_audio_dma.0.auto")));
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic struct snd_soc_dai_link cz_dai_rt5650[] = {
1108c2ecf20Sopenharmony_ci	{
1118c2ecf20Sopenharmony_ci		.name = "amd-rt5645-play",
1128c2ecf20Sopenharmony_ci		.stream_name = "RT5645_AIF1",
1138c2ecf20Sopenharmony_ci		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1148c2ecf20Sopenharmony_ci				| SND_SOC_DAIFMT_CBM_CFM,
1158c2ecf20Sopenharmony_ci		.init = cz_init,
1168c2ecf20Sopenharmony_ci		.ops = &cz_aif1_ops,
1178c2ecf20Sopenharmony_ci		SND_SOC_DAILINK_REG(designware1, codec, platform),
1188c2ecf20Sopenharmony_ci	},
1198c2ecf20Sopenharmony_ci	{
1208c2ecf20Sopenharmony_ci		.name = "amd-rt5645-cap",
1218c2ecf20Sopenharmony_ci		.stream_name = "RT5645_AIF1",
1228c2ecf20Sopenharmony_ci		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1238c2ecf20Sopenharmony_ci				| SND_SOC_DAIFMT_CBM_CFM,
1248c2ecf20Sopenharmony_ci		.ops = &cz_aif1_ops,
1258c2ecf20Sopenharmony_ci		SND_SOC_DAILINK_REG(designware2, codec, platform),
1268c2ecf20Sopenharmony_ci	},
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget cz_widgets[] = {
1308c2ecf20Sopenharmony_ci	SND_SOC_DAPM_HP("Headphones", NULL),
1318c2ecf20Sopenharmony_ci	SND_SOC_DAPM_SPK("Speakers", NULL),
1328c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Headset Mic", NULL),
1338c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MIC("Int Mic", NULL),
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route cz_audio_route[] = {
1378c2ecf20Sopenharmony_ci	{"Headphones", NULL, "HPOL"},
1388c2ecf20Sopenharmony_ci	{"Headphones", NULL, "HPOR"},
1398c2ecf20Sopenharmony_ci	{"RECMIXL", NULL, "Headset Mic"},
1408c2ecf20Sopenharmony_ci	{"RECMIXR", NULL, "Headset Mic"},
1418c2ecf20Sopenharmony_ci	{"Speakers", NULL, "SPOL"},
1428c2ecf20Sopenharmony_ci	{"Speakers", NULL, "SPOR"},
1438c2ecf20Sopenharmony_ci	{"DMIC L2", NULL, "Int Mic"},
1448c2ecf20Sopenharmony_ci	{"DMIC R2", NULL, "Int Mic"},
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new cz_mc_controls[] = {
1488c2ecf20Sopenharmony_ci	SOC_DAPM_PIN_SWITCH("Headphones"),
1498c2ecf20Sopenharmony_ci	SOC_DAPM_PIN_SWITCH("Speakers"),
1508c2ecf20Sopenharmony_ci	SOC_DAPM_PIN_SWITCH("Headset Mic"),
1518c2ecf20Sopenharmony_ci	SOC_DAPM_PIN_SWITCH("Int Mic"),
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic struct snd_soc_card cz_card = {
1558c2ecf20Sopenharmony_ci	.name = "acprt5650",
1568c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
1578c2ecf20Sopenharmony_ci	.dai_link = cz_dai_rt5650,
1588c2ecf20Sopenharmony_ci	.num_links = ARRAY_SIZE(cz_dai_rt5650),
1598c2ecf20Sopenharmony_ci	.dapm_widgets = cz_widgets,
1608c2ecf20Sopenharmony_ci	.num_dapm_widgets = ARRAY_SIZE(cz_widgets),
1618c2ecf20Sopenharmony_ci	.dapm_routes = cz_audio_route,
1628c2ecf20Sopenharmony_ci	.num_dapm_routes = ARRAY_SIZE(cz_audio_route),
1638c2ecf20Sopenharmony_ci	.controls = cz_mc_controls,
1648c2ecf20Sopenharmony_ci	.num_controls = ARRAY_SIZE(cz_mc_controls),
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int cz_probe(struct platform_device *pdev)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	int ret;
1708c2ecf20Sopenharmony_ci	struct snd_soc_card *card;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	card = &cz_card;
1738c2ecf20Sopenharmony_ci	cz_card.dev = &pdev->dev;
1748c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, card);
1758c2ecf20Sopenharmony_ci	ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
1768c2ecf20Sopenharmony_ci	if (ret) {
1778c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
1788c2ecf20Sopenharmony_ci				"devm_snd_soc_register_card(%s) failed: %d\n",
1798c2ecf20Sopenharmony_ci				cz_card.name, ret);
1808c2ecf20Sopenharmony_ci		return ret;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci	return 0;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
1868c2ecf20Sopenharmony_cistatic const struct acpi_device_id cz_audio_acpi_match[] = {
1878c2ecf20Sopenharmony_ci	{ "AMDI1002", 0 },
1888c2ecf20Sopenharmony_ci	{},
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
1918c2ecf20Sopenharmony_ci#endif
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic struct platform_driver cz_pcm_driver = {
1948c2ecf20Sopenharmony_ci	.driver = {
1958c2ecf20Sopenharmony_ci		.name = "cz-rt5645",
1968c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
1978c2ecf20Sopenharmony_ci		.pm = &snd_soc_pm_ops,
1988c2ecf20Sopenharmony_ci	},
1998c2ecf20Sopenharmony_ci	.probe = cz_probe,
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cimodule_platform_driver(cz_pcm_driver);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciMODULE_AUTHOR("akshu.agrawal@amd.com");
2058c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("cz-rt5645 audio support");
2068c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
207