xref: /openbmc/linux/sound/soc/amd/acp-rt5645.c (revision 3ddc8b84)
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 static struct snd_soc_jack_pin cz_jack_pins[] = {
46 	{
47 		.pin = "Headphones",
48 		.mask = SND_JACK_HEADPHONE,
49 	},
50 	{
51 		.pin = "Headset Mic",
52 		.mask = SND_JACK_MICROPHONE,
53 	},
54 };
55 
56 static int cz_aif1_hw_params(struct snd_pcm_substream *substream,
57 			     struct snd_pcm_hw_params *params)
58 {
59 	int ret = 0;
60 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
61 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
62 
63 	ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
64 				  CZ_PLAT_CLK, params_rate(params) * 512);
65 	if (ret < 0) {
66 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
67 		return ret;
68 	}
69 
70 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
71 				params_rate(params) * 512, SND_SOC_CLOCK_OUT);
72 	if (ret < 0) {
73 		dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
74 		return ret;
75 	}
76 
77 	return ret;
78 }
79 
80 static int cz_init(struct snd_soc_pcm_runtime *rtd)
81 {
82 	int ret;
83 	struct snd_soc_card *card;
84 	struct snd_soc_component *codec;
85 
86 	codec = asoc_rtd_to_codec(rtd, 0)->component;
87 	card = rtd->card;
88 
89 	ret = snd_soc_card_jack_new_pins(card, "Headset Jack",
90 					 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
91 					 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
92 					 SND_JACK_BTN_2 | SND_JACK_BTN_3,
93 					 &cz_jack,
94 					 cz_jack_pins,
95 					 ARRAY_SIZE(cz_jack_pins));
96 	if (ret) {
97 		dev_err(card->dev, "HP jack creation failed %d\n", ret);
98 		return ret;
99 	}
100 
101 	rt5645_set_jack_detect(codec, &cz_jack, &cz_jack, &cz_jack);
102 
103 	return 0;
104 }
105 
106 static const struct snd_soc_ops cz_aif1_ops = {
107 	.hw_params = cz_aif1_hw_params,
108 };
109 
110 SND_SOC_DAILINK_DEF(designware1,
111 	DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.1.auto")));
112 SND_SOC_DAILINK_DEF(designware2,
113 	DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.2.auto")));
114 
115 SND_SOC_DAILINK_DEF(codec,
116 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1")));
117 
118 SND_SOC_DAILINK_DEF(platform,
119 	DAILINK_COMP_ARRAY(COMP_PLATFORM("acp_audio_dma.0.auto")));
120 
121 static struct snd_soc_dai_link cz_dai_rt5650[] = {
122 	{
123 		.name = "amd-rt5645-play",
124 		.stream_name = "RT5645_AIF1",
125 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
126 				| SND_SOC_DAIFMT_CBP_CFP,
127 		.init = cz_init,
128 		.ops = &cz_aif1_ops,
129 		SND_SOC_DAILINK_REG(designware1, codec, platform),
130 	},
131 	{
132 		.name = "amd-rt5645-cap",
133 		.stream_name = "RT5645_AIF1",
134 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
135 				| SND_SOC_DAIFMT_CBP_CFP,
136 		.ops = &cz_aif1_ops,
137 		SND_SOC_DAILINK_REG(designware2, codec, platform),
138 	},
139 };
140 
141 static const struct snd_soc_dapm_widget cz_widgets[] = {
142 	SND_SOC_DAPM_HP("Headphones", NULL),
143 	SND_SOC_DAPM_SPK("Speakers", NULL),
144 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
145 	SND_SOC_DAPM_MIC("Int Mic", NULL),
146 };
147 
148 static const struct snd_soc_dapm_route cz_audio_route[] = {
149 	{"Headphones", NULL, "HPOL"},
150 	{"Headphones", NULL, "HPOR"},
151 	{"RECMIXL", NULL, "Headset Mic"},
152 	{"RECMIXR", NULL, "Headset Mic"},
153 	{"Speakers", NULL, "SPOL"},
154 	{"Speakers", NULL, "SPOR"},
155 	{"DMIC L2", NULL, "Int Mic"},
156 	{"DMIC R2", NULL, "Int Mic"},
157 };
158 
159 static const struct snd_kcontrol_new cz_mc_controls[] = {
160 	SOC_DAPM_PIN_SWITCH("Headphones"),
161 	SOC_DAPM_PIN_SWITCH("Speakers"),
162 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
163 	SOC_DAPM_PIN_SWITCH("Int Mic"),
164 };
165 
166 static struct snd_soc_card cz_card = {
167 	.name = "acprt5650",
168 	.owner = THIS_MODULE,
169 	.dai_link = cz_dai_rt5650,
170 	.num_links = ARRAY_SIZE(cz_dai_rt5650),
171 	.dapm_widgets = cz_widgets,
172 	.num_dapm_widgets = ARRAY_SIZE(cz_widgets),
173 	.dapm_routes = cz_audio_route,
174 	.num_dapm_routes = ARRAY_SIZE(cz_audio_route),
175 	.controls = cz_mc_controls,
176 	.num_controls = ARRAY_SIZE(cz_mc_controls),
177 };
178 
179 static int cz_probe(struct platform_device *pdev)
180 {
181 	int ret;
182 	struct snd_soc_card *card;
183 
184 	card = &cz_card;
185 	cz_card.dev = &pdev->dev;
186 	platform_set_drvdata(pdev, card);
187 	ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
188 	if (ret) {
189 		dev_err(&pdev->dev,
190 				"devm_snd_soc_register_card(%s) failed: %d\n",
191 				cz_card.name, ret);
192 		return ret;
193 	}
194 	return 0;
195 }
196 
197 #ifdef CONFIG_ACPI
198 static const struct acpi_device_id cz_audio_acpi_match[] = {
199 	{ "AMDI1002", 0 },
200 	{},
201 };
202 MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
203 #endif
204 
205 static struct platform_driver cz_pcm_driver = {
206 	.driver = {
207 		.name = "cz-rt5645",
208 		.acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
209 		.pm = &snd_soc_pm_ops,
210 	},
211 	.probe = cz_probe,
212 };
213 
214 module_platform_driver(cz_pcm_driver);
215 
216 MODULE_AUTHOR("akshu.agrawal@amd.com");
217 MODULE_DESCRIPTION("cz-rt5645 audio support");
218 MODULE_LICENSE("GPL v2");
219