blob: cd3cf6e691a928ebd0c3d9113f32ed9f2df12910 [file] [log] [blame]
Akshu Agrawal608a300f2018-02-16 13:11:13 +05301/*
2 * Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec
3 *
4 * Copyright 2017 Advanced Micro Devices, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26#include <sound/core.h>
27#include <sound/soc.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc-dapm.h>
31#include <sound/jack.h>
Akshu Agrawalc88d3112018-03-19 11:07:42 +053032#include <linux/clk.h>
Akshu Agrawal608a300f2018-02-16 13:11:13 +053033#include <linux/gpio.h>
34#include <linux/module.h>
Akshu Agrawalc183fec2018-07-25 17:00:59 +080035#include <linux/regulator/machine.h>
36#include <linux/regulator/driver.h>
Akshu Agrawal608a300f2018-02-16 13:11:13 +053037#include <linux/i2c.h>
Agrawal, Akshu923859e2018-04-12 17:57:11 +080038#include <linux/input.h>
Akshu Agrawal608a300f2018-02-16 13:11:13 +053039#include <linux/acpi.h>
40
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +053041#include "acp.h"
Akshu Agrawal608a300f2018-02-16 13:11:13 +053042#include "../codecs/da7219.h"
43#include "../codecs/da7219-aad.h"
44
Akshu Agrawal6e554072018-05-08 10:17:51 +053045#define CZ_PLAT_CLK 25000000
Akshu Agrawal608a300f2018-02-16 13:11:13 +053046#define DUAL_CHANNEL 2
47
48static struct snd_soc_jack cz_jack;
Wei Yongjunede1d352018-03-29 02:14:03 +000049static struct clk *da7219_dai_clk;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +053050extern int bt_uart_enable;
Akshu Agrawal608a300f2018-02-16 13:11:13 +053051
52static int cz_da7219_init(struct snd_soc_pcm_runtime *rtd)
53{
54 int ret;
55 struct snd_soc_card *card = rtd->card;
Akshu Agrawal608a300f2018-02-16 13:11:13 +053056 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Kuninori Morimoto9a60cde2018-02-19 04:16:06 +000057 struct snd_soc_component *component = codec_dai->component;
Akshu Agrawal608a300f2018-02-16 13:11:13 +053058
59 dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
60
61 ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK,
62 CZ_PLAT_CLK, SND_SOC_CLOCK_IN);
63 if (ret < 0) {
64 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
65 return ret;
66 }
67
68 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL,
Akshu Agrawal6e554072018-05-08 10:17:51 +053069 CZ_PLAT_CLK, DA7219_PLL_FREQ_OUT_98304);
Akshu Agrawal608a300f2018-02-16 13:11:13 +053070 if (ret < 0) {
71 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
72 return ret;
73 }
74
Kuninori Morimoto4c50e1e2018-03-28 01:48:07 +000075 da7219_dai_clk = clk_get(component->dev, "da7219-dai-clks");
Akshu Agrawalc88d3112018-03-19 11:07:42 +053076
Akshu Agrawal608a300f2018-02-16 13:11:13 +053077 ret = snd_soc_card_jack_new(card, "Headset Jack",
78 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
79 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
80 SND_JACK_BTN_2 | SND_JACK_BTN_3,
81 &cz_jack, NULL, 0);
82 if (ret) {
83 dev_err(card->dev, "HP jack creation failed %d\n", ret);
84 return ret;
85 }
86
Agrawal, Akshu923859e2018-04-12 17:57:11 +080087 snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
88 snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
89 snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
90 snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
91
Kuninori Morimoto9a60cde2018-02-19 04:16:06 +000092 da7219_aad_jack_det(component, &cz_jack);
Akshu Agrawal608a300f2018-02-16 13:11:13 +053093
94 return 0;
95}
96
Akshu Agrawale9716ff2018-05-08 10:17:50 +053097static int da7219_clk_enable(struct snd_pcm_substream *substream)
Akshu Agrawalc88d3112018-03-19 11:07:42 +053098{
99 int ret = 0;
100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
101
102 ret = clk_prepare_enable(da7219_dai_clk);
103 if (ret < 0) {
104 dev_err(rtd->dev, "can't enable master clock %d\n", ret);
105 return ret;
106 }
107
108 return ret;
109}
110
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530111static void da7219_clk_disable(void)
Akshu Agrawalc88d3112018-03-19 11:07:42 +0530112{
113 clk_disable_unprepare(da7219_dai_clk);
Akshu Agrawalc88d3112018-03-19 11:07:42 +0530114}
115
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530116static const unsigned int channels[] = {
117 DUAL_CHANNEL,
118};
119
120static const unsigned int rates[] = {
121 48000,
122};
123
124static const struct snd_pcm_hw_constraint_list constraints_rates = {
125 .count = ARRAY_SIZE(rates),
126 .list = rates,
127 .mask = 0,
128};
129
130static const struct snd_pcm_hw_constraint_list constraints_channels = {
131 .count = ARRAY_SIZE(channels),
132 .list = channels,
133 .mask = 0,
134};
135
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530136static int cz_da7219_startup(struct snd_pcm_substream *substream)
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530137{
138 struct snd_pcm_runtime *runtime = substream->runtime;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530139 struct snd_soc_pcm_runtime *rtd = substream->private_data;
140 struct snd_soc_card *card = rtd->card;
141 struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530142
143 /*
144 * On this platform for PCM device we support stereo
145 */
146
147 runtime->hw.channels_max = DUAL_CHANNEL;
148 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
149 &constraints_channels);
150 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
151 &constraints_rates);
152
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800153 machine->i2s_instance = I2S_SP_INSTANCE;
Akshu Agrawal2718c892018-06-21 12:58:17 +0800154 machine->capture_channel = CAP_CHANNEL1;
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530155 return da7219_clk_enable(substream);
156}
157
158static void cz_da7219_shutdown(struct snd_pcm_substream *substream)
159{
160 da7219_clk_disable();
161}
162
163static int cz_max_startup(struct snd_pcm_substream *substream)
164{
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530165 struct snd_soc_pcm_runtime *rtd = substream->private_data;
166 struct snd_soc_card *card = rtd->card;
167 struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
168
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800169 machine->i2s_instance = I2S_BT_INSTANCE;
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530170 return da7219_clk_enable(substream);
171}
172
173static void cz_max_shutdown(struct snd_pcm_substream *substream)
174{
175 da7219_clk_disable();
176}
177
Akshu Agrawal2718c892018-06-21 12:58:17 +0800178static int cz_dmic0_startup(struct snd_pcm_substream *substream)
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530179{
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530180 struct snd_soc_pcm_runtime *rtd = substream->private_data;
181 struct snd_soc_card *card = rtd->card;
182 struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
183
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800184 machine->i2s_instance = I2S_BT_INSTANCE;
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530185 return da7219_clk_enable(substream);
186}
187
Akshu Agrawal2718c892018-06-21 12:58:17 +0800188static int cz_dmic1_startup(struct snd_pcm_substream *substream)
189{
190 struct snd_soc_pcm_runtime *rtd = substream->private_data;
191 struct snd_soc_card *card = rtd->card;
192 struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
193
194 machine->i2s_instance = I2S_SP_INSTANCE;
195 machine->capture_channel = CAP_CHANNEL0;
196 return da7219_clk_enable(substream);
197}
198
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530199static void cz_dmic_shutdown(struct snd_pcm_substream *substream)
200{
201 da7219_clk_disable();
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530202}
203
Akshu Agrawal839a12c2018-05-08 10:17:52 +0530204static const struct snd_soc_ops cz_da7219_cap_ops = {
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530205 .startup = cz_da7219_startup,
206 .shutdown = cz_da7219_shutdown,
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530207};
208
Akshu Agrawal839a12c2018-05-08 10:17:52 +0530209static const struct snd_soc_ops cz_max_play_ops = {
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530210 .startup = cz_max_startup,
211 .shutdown = cz_max_shutdown,
Akshu Agrawalc88d3112018-03-19 11:07:42 +0530212};
213
Akshu Agrawal2718c892018-06-21 12:58:17 +0800214static const struct snd_soc_ops cz_dmic0_cap_ops = {
215 .startup = cz_dmic0_startup,
216 .shutdown = cz_dmic_shutdown,
217};
218
219static const struct snd_soc_ops cz_dmic1_cap_ops = {
220 .startup = cz_dmic1_startup,
Akshu Agrawale9716ff2018-05-08 10:17:50 +0530221 .shutdown = cz_dmic_shutdown,
Akshu Agrawalc88d3112018-03-19 11:07:42 +0530222};
223
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530224static struct snd_soc_dai_link cz_dai_7219_98357[] = {
225 {
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800226 .name = "amd-da7219-play",
227 .stream_name = "Playback",
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530228 .platform_name = "acp_audio_dma.0.auto",
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800229 .cpu_dai_name = "designware-i2s.1.auto",
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530230 .codec_dai_name = "da7219-hifi",
231 .codec_name = "i2c-DLGS7219:00",
232 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
233 | SND_SOC_DAIFMT_CBM_CFM,
234 .init = cz_da7219_init,
235 .dpcm_playback = 1,
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800236 .ops = &cz_da7219_cap_ops,
237 },
238 {
239 .name = "amd-da7219-cap",
240 .stream_name = "Capture",
241 .platform_name = "acp_audio_dma.0.auto",
242 .cpu_dai_name = "designware-i2s.2.auto",
243 .codec_dai_name = "da7219-hifi",
244 .codec_name = "i2c-DLGS7219:00",
245 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
246 | SND_SOC_DAIFMT_CBM_CFM,
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530247 .dpcm_capture = 1,
248 .ops = &cz_da7219_cap_ops,
249 },
250 {
251 .name = "amd-max98357-play",
252 .stream_name = "HiFi Playback",
253 .platform_name = "acp_audio_dma.0.auto",
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800254 .cpu_dai_name = "designware-i2s.3.auto",
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530255 .codec_dai_name = "HiFi",
256 .codec_name = "MX98357A:00",
257 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
258 | SND_SOC_DAIFMT_CBM_CFM,
259 .dpcm_playback = 1,
Akshu Agrawalc88d3112018-03-19 11:07:42 +0530260 .ops = &cz_max_play_ops,
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530261 },
262 {
Akshu Agrawal2718c892018-06-21 12:58:17 +0800263 /* C panel DMIC */
264 .name = "dmic0",
265 .stream_name = "DMIC0 Capture",
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530266 .platform_name = "acp_audio_dma.0.auto",
Agrawal, Akshu3bec6fa2018-06-21 12:58:16 +0800267 .cpu_dai_name = "designware-i2s.3.auto",
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530268 .codec_dai_name = "adau7002-hifi",
269 .codec_name = "ADAU7002:00",
270 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
271 | SND_SOC_DAIFMT_CBM_CFM,
272 .dpcm_capture = 1,
Akshu Agrawal2718c892018-06-21 12:58:17 +0800273 .ops = &cz_dmic0_cap_ops,
274 },
275 {
276 /* A/B panel DMIC */
277 .name = "dmic1",
278 .stream_name = "DMIC1 Capture",
279 .platform_name = "acp_audio_dma.0.auto",
280 .cpu_dai_name = "designware-i2s.2.auto",
281 .codec_dai_name = "adau7002-hifi",
282 .codec_name = "ADAU7002:00",
283 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
284 | SND_SOC_DAIFMT_CBM_CFM,
285 .dpcm_capture = 1,
286 .ops = &cz_dmic1_cap_ops,
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530287 },
288};
289
290static const struct snd_soc_dapm_widget cz_widgets[] = {
291 SND_SOC_DAPM_HP("Headphones", NULL),
292 SND_SOC_DAPM_SPK("Speakers", NULL),
293 SND_SOC_DAPM_MIC("Headset Mic", NULL),
294 SND_SOC_DAPM_MIC("Int Mic", NULL),
295};
296
297static const struct snd_soc_dapm_route cz_audio_route[] = {
298 {"Headphones", NULL, "HPL"},
299 {"Headphones", NULL, "HPR"},
300 {"MIC", NULL, "Headset Mic"},
301 {"Speakers", NULL, "Speaker"},
302 {"PDM_DAT", NULL, "Int Mic"},
303};
304
305static const struct snd_kcontrol_new cz_mc_controls[] = {
306 SOC_DAPM_PIN_SWITCH("Headphones"),
307 SOC_DAPM_PIN_SWITCH("Speakers"),
308 SOC_DAPM_PIN_SWITCH("Headset Mic"),
309 SOC_DAPM_PIN_SWITCH("Int Mic"),
310};
311
312static struct snd_soc_card cz_card = {
313 .name = "acpd7219m98357",
314 .owner = THIS_MODULE,
315 .dai_link = cz_dai_7219_98357,
316 .num_links = ARRAY_SIZE(cz_dai_7219_98357),
317 .dapm_widgets = cz_widgets,
318 .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
319 .dapm_routes = cz_audio_route,
320 .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
321 .controls = cz_mc_controls,
322 .num_controls = ARRAY_SIZE(cz_mc_controls),
323};
324
Akshu Agrawalc183fec2018-07-25 17:00:59 +0800325static struct regulator_consumer_supply acp_da7219_supplies[] = {
326 REGULATOR_SUPPLY("VDD", "i2c-DLGS7219:00"),
327 REGULATOR_SUPPLY("VDDMIC", "i2c-DLGS7219:00"),
328 REGULATOR_SUPPLY("VDDIO", "i2c-DLGS7219:00"),
329 REGULATOR_SUPPLY("IOVDD", "ADAU7002:00"),
330};
331
332static struct regulator_init_data acp_da7219_data = {
333 .constraints = {
334 .always_on = 1,
335 },
336 .num_consumer_supplies = ARRAY_SIZE(acp_da7219_supplies),
337 .consumer_supplies = acp_da7219_supplies,
338};
339
340static struct regulator_config acp_da7219_cfg = {
341 .init_data = &acp_da7219_data,
342};
343
344static struct regulator_ops acp_da7219_ops = {
345};
346
347static struct regulator_desc acp_da7219_desc = {
348 .name = "reg-fixed-1.8V",
349 .type = REGULATOR_VOLTAGE,
350 .owner = THIS_MODULE,
351 .ops = &acp_da7219_ops,
352 .fixed_uV = 1800000, /* 1.8V */
353 .n_voltages = 1,
354};
355
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530356static int cz_probe(struct platform_device *pdev)
357{
358 int ret;
359 struct snd_soc_card *card;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530360 struct acp_platform_info *machine;
Akshu Agrawalc183fec2018-07-25 17:00:59 +0800361 struct regulator_dev *rdev;
362
363 acp_da7219_cfg.dev = &pdev->dev;
364 rdev = devm_regulator_register(&pdev->dev, &acp_da7219_desc,
365 &acp_da7219_cfg);
366 if (IS_ERR(rdev)) {
367 dev_err(&pdev->dev, "Failed to register regulator: %d\n",
368 ret);
369 return -EINVAL;
370 }
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530371
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530372 machine = devm_kzalloc(&pdev->dev, sizeof(struct acp_platform_info),
373 GFP_KERNEL);
374 if (!machine)
375 return -ENOMEM;
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530376 card = &cz_card;
377 cz_card.dev = &pdev->dev;
378 platform_set_drvdata(pdev, card);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530379 snd_soc_card_set_drvdata(card, machine);
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530380 ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
381 if (ret) {
382 dev_err(&pdev->dev,
383 "devm_snd_soc_register_card(%s) failed: %d\n",
384 cz_card.name, ret);
385 return ret;
386 }
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530387 bt_uart_enable = !device_property_read_bool(&pdev->dev,
388 "bt-pad-enable");
Akshu Agrawal608a300f2018-02-16 13:11:13 +0530389 return 0;
390}
391
392static const struct acpi_device_id cz_audio_acpi_match[] = {
393 { "AMD7219", 0 },
394 {},
395};
396MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
397
398static struct platform_driver cz_pcm_driver = {
399 .driver = {
400 .name = "cz-da7219-max98357a",
401 .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
402 .pm = &snd_soc_pm_ops,
403 },
404 .probe = cz_probe,
405};
406
407module_platform_driver(cz_pcm_driver);
408
409MODULE_AUTHOR("akshu.agrawal@amd.com");
410MODULE_DESCRIPTION("DA7219 & MAX98357A audio support");
411MODULE_LICENSE("GPL v2");