xref: /openbmc/linux/sound/soc/amd/acp-rt5645.c (revision 82e6fdd6)
1 /*
2  * Machine driver for AMD ACP Audio engine using Realtek RT5645 codec
3  *
4  * Copyright 2017 Advanced Micro Devices, Inc.
5  *
6  * This file is modified from rt288 machine driver
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  *
27  */
28 
29 #include <sound/core.h>
30 #include <sound/soc.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc-dapm.h>
34 #include <sound/jack.h>
35 #include <linux/gpio.h>
36 #include <linux/module.h>
37 #include <linux/i2c.h>
38 #include <linux/acpi.h>
39 
40 #include "../codecs/rt5645.h"
41 
42 #define CZ_PLAT_CLK 24000000
43 
44 static struct snd_soc_jack cz_jack;
45 
46 static int cz_aif1_hw_params(struct snd_pcm_substream *substream,
47 			     struct snd_pcm_hw_params *params)
48 {
49 	int ret = 0;
50 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
52 
53 	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
54 				  CZ_PLAT_CLK, params_rate(params) * 512);
55 	if (ret < 0) {
56 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
57 		return ret;
58 	}
59 
60 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
61 				params_rate(params) * 512, SND_SOC_CLOCK_OUT);
62 	if (ret < 0) {
63 		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
64 		return ret;
65 	}
66 
67 	return ret;
68 }
69 
70 static int cz_init(struct snd_soc_pcm_runtime *rtd)
71 {
72 	int ret;
73 	struct snd_soc_card *card;
74 	struct snd_soc_codec *codec;
75 
76 	codec = rtd->codec;
77 	card = rtd->card;
78 
79 	ret = snd_soc_card_jack_new(card, "Headset Jack",
80 				SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
81 				SND_JACK_BTN_0 | SND_JACK_BTN_1 |
82 				SND_JACK_BTN_2 | SND_JACK_BTN_3,
83 				&cz_jack, NULL, 0);
84 	if (ret) {
85 		dev_err(card->dev, "HP jack creation failed %d\n", ret);
86 		return ret;
87 	}
88 
89 	rt5645_set_jack_detect(codec, &cz_jack, &cz_jack, &cz_jack);
90 
91 	return 0;
92 }
93 
94 static struct snd_soc_ops cz_aif1_ops = {
95 	.hw_params = cz_aif1_hw_params,
96 };
97 
98 static struct snd_soc_dai_link cz_dai_rt5650[] = {
99 	{
100 		.name = "amd-rt5645-play",
101 		.stream_name = "RT5645_AIF1",
102 		.platform_name = "acp_audio_dma.0.auto",
103 		.cpu_dai_name = "designware-i2s.1.auto",
104 		.codec_dai_name = "rt5645-aif1",
105 		.codec_name = "i2c-10EC5650:00",
106 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
107 				| SND_SOC_DAIFMT_CBM_CFM,
108 		.init = cz_init,
109 		.ops = &cz_aif1_ops,
110 	},
111 	{
112 		.name = "amd-rt5645-cap",
113 		.stream_name = "RT5645_AIF1",
114 		.platform_name = "acp_audio_dma.0.auto",
115 		.cpu_dai_name = "designware-i2s.2.auto",
116 		.codec_dai_name = "rt5645-aif1",
117 		.codec_name = "i2c-10EC5650:00",
118 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
119 				| SND_SOC_DAIFMT_CBM_CFM,
120 		.ops = &cz_aif1_ops,
121 	},
122 };
123 
124 static const struct snd_soc_dapm_widget cz_widgets[] = {
125 	SND_SOC_DAPM_HP("Headphones", NULL),
126 	SND_SOC_DAPM_SPK("Speakers", NULL),
127 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
128 	SND_SOC_DAPM_MIC("Int Mic", NULL),
129 };
130 
131 static const struct snd_soc_dapm_route cz_audio_route[] = {
132 	{"Headphones", NULL, "HPOL"},
133 	{"Headphones", NULL, "HPOR"},
134 	{"RECMIXL", NULL, "Headset Mic"},
135 	{"RECMIXR", NULL, "Headset Mic"},
136 	{"Speakers", NULL, "SPOL"},
137 	{"Speakers", NULL, "SPOR"},
138 	{"DMIC L2", NULL, "Int Mic"},
139 	{"DMIC R2", NULL, "Int Mic"},
140 };
141 
142 static const struct snd_kcontrol_new cz_mc_controls[] = {
143 	SOC_DAPM_PIN_SWITCH("Headphones"),
144 	SOC_DAPM_PIN_SWITCH("Speakers"),
145 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
146 	SOC_DAPM_PIN_SWITCH("Int Mic"),
147 };
148 
149 static struct snd_soc_card cz_card = {
150 	.name = "acprt5650",
151 	.owner = THIS_MODULE,
152 	.dai_link = cz_dai_rt5650,
153 	.num_links = ARRAY_SIZE(cz_dai_rt5650),
154 	.dapm_widgets = cz_widgets,
155 	.num_dapm_widgets = ARRAY_SIZE(cz_widgets),
156 	.dapm_routes = cz_audio_route,
157 	.num_dapm_routes = ARRAY_SIZE(cz_audio_route),
158 	.controls = cz_mc_controls,
159 	.num_controls = ARRAY_SIZE(cz_mc_controls),
160 };
161 
162 static int cz_probe(struct platform_device *pdev)
163 {
164 	int ret;
165 	struct snd_soc_card *card;
166 
167 	card = &cz_card;
168 	cz_card.dev = &pdev->dev;
169 	platform_set_drvdata(pdev, card);
170 	ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
171 	if (ret) {
172 		dev_err(&pdev->dev,
173 				"devm_snd_soc_register_card(%s) failed: %d\n",
174 				cz_card.name, ret);
175 		return ret;
176 	}
177 	return 0;
178 }
179 
180 static const struct acpi_device_id cz_audio_acpi_match[] = {
181 	{ "AMDI1002", 0 },
182 	{},
183 };
184 MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
185 
186 static struct platform_driver cz_pcm_driver = {
187 	.driver = {
188 		.name = "cz-rt5645",
189 		.acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
190 		.pm = &snd_soc_pm_ops,
191 	},
192 	.probe = cz_probe,
193 };
194 
195 module_platform_driver(cz_pcm_driver);
196 
197 MODULE_AUTHOR("akshu.agrawal@amd.com");
198 MODULE_DESCRIPTION("cz-rt5645 audio support");
199 MODULE_LICENSE("GPL v2");
200