]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/soc/amd/acp-da7219-max98357a.c
3879cccbd2c002dedddbb3d70551408165fb5886
[linux.git] / sound / soc / amd / acp-da7219-max98357a.c
1 /*
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>
32 #include <linux/clk.h>
33 #include <linux/gpio.h>
34 #include <linux/module.h>
35 #include <linux/regulator/machine.h>
36 #include <linux/regulator/driver.h>
37 #include <linux/i2c.h>
38 #include <linux/input.h>
39 #include <linux/acpi.h>
40
41 #include "acp.h"
42 #include "../codecs/da7219.h"
43 #include "../codecs/da7219-aad.h"
44
45 #define CZ_PLAT_CLK 48000000
46 #define DUAL_CHANNEL            2
47
48 static struct snd_soc_jack cz_jack;
49 static struct clk *da7219_dai_clk;
50 extern int bt_uart_enable;
51
52 static int cz_da7219_init(struct snd_soc_pcm_runtime *rtd)
53 {
54         int ret;
55         struct snd_soc_card *card = rtd->card;
56         struct snd_soc_dai *codec_dai = rtd->codec_dai;
57         struct snd_soc_component *component = codec_dai->component;
58
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,
69                                   CZ_PLAT_CLK, DA7219_PLL_FREQ_OUT_98304);
70         if (ret < 0) {
71                 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
72                 return ret;
73         }
74
75         da7219_dai_clk = clk_get(component->dev, "da7219-dai-clks");
76
77         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
87         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
92         da7219_aad_jack_det(component, &cz_jack);
93
94         return 0;
95 }
96
97 static int da7219_clk_enable(struct snd_pcm_substream *substream)
98 {
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
111 static void da7219_clk_disable(void)
112 {
113         clk_disable_unprepare(da7219_dai_clk);
114 }
115
116 static const unsigned int channels[] = {
117         DUAL_CHANNEL,
118 };
119
120 static const unsigned int rates[] = {
121         48000,
122 };
123
124 static const struct snd_pcm_hw_constraint_list constraints_rates = {
125         .count = ARRAY_SIZE(rates),
126         .list  = rates,
127         .mask = 0,
128 };
129
130 static const struct snd_pcm_hw_constraint_list constraints_channels = {
131         .count = ARRAY_SIZE(channels),
132         .list = channels,
133         .mask = 0,
134 };
135
136 static int cz_da7219_startup(struct snd_pcm_substream *substream)
137 {
138         struct snd_pcm_runtime *runtime = substream->runtime;
139         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);
142
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
153         machine->i2s_instance = I2S_SP_INSTANCE;
154         machine->capture_channel = CAP_CHANNEL1;
155         return da7219_clk_enable(substream);
156 }
157
158 static void cz_da7219_shutdown(struct snd_pcm_substream *substream)
159 {
160         da7219_clk_disable();
161 }
162
163 static int cz_max_startup(struct snd_pcm_substream *substream)
164 {
165         struct snd_pcm_runtime *runtime = substream->runtime;
166         struct snd_soc_pcm_runtime *rtd = substream->private_data;
167         struct snd_soc_card *card = rtd->card;
168         struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
169
170         /*
171          * On this platform for PCM device we support stereo
172          */
173
174         runtime->hw.channels_max = DUAL_CHANNEL;
175         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
176                                    &constraints_channels);
177         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
178                                    &constraints_rates);
179
180         machine->i2s_instance = I2S_BT_INSTANCE;
181         return da7219_clk_enable(substream);
182 }
183
184 static void cz_max_shutdown(struct snd_pcm_substream *substream)
185 {
186         da7219_clk_disable();
187 }
188
189 static int cz_dmic0_startup(struct snd_pcm_substream *substream)
190 {
191         struct snd_pcm_runtime *runtime = substream->runtime;
192         struct snd_soc_pcm_runtime *rtd = substream->private_data;
193         struct snd_soc_card *card = rtd->card;
194         struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
195
196         /*
197          * On this platform for PCM device we support stereo
198          */
199
200         runtime->hw.channels_max = DUAL_CHANNEL;
201         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
202                                    &constraints_channels);
203         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
204                                    &constraints_rates);
205
206         machine->i2s_instance = I2S_BT_INSTANCE;
207         return da7219_clk_enable(substream);
208 }
209
210 static int cz_dmic1_startup(struct snd_pcm_substream *substream)
211 {
212         struct snd_pcm_runtime *runtime = substream->runtime;
213         struct snd_soc_pcm_runtime *rtd = substream->private_data;
214         struct snd_soc_card *card = rtd->card;
215         struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
216
217         /*
218          * On this platform for PCM device we support stereo
219          */
220
221         runtime->hw.channels_max = DUAL_CHANNEL;
222         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
223                                    &constraints_channels);
224         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
225                                    &constraints_rates);
226
227         machine->i2s_instance = I2S_SP_INSTANCE;
228         machine->capture_channel = CAP_CHANNEL0;
229         return da7219_clk_enable(substream);
230 }
231
232 static void cz_dmic_shutdown(struct snd_pcm_substream *substream)
233 {
234         da7219_clk_disable();
235 }
236
237 static const struct snd_soc_ops cz_da7219_cap_ops = {
238         .startup = cz_da7219_startup,
239         .shutdown = cz_da7219_shutdown,
240 };
241
242 static const struct snd_soc_ops cz_max_play_ops = {
243         .startup = cz_max_startup,
244         .shutdown = cz_max_shutdown,
245 };
246
247 static const struct snd_soc_ops cz_dmic0_cap_ops = {
248         .startup = cz_dmic0_startup,
249         .shutdown = cz_dmic_shutdown,
250 };
251
252 static const struct snd_soc_ops cz_dmic1_cap_ops = {
253         .startup = cz_dmic1_startup,
254         .shutdown = cz_dmic_shutdown,
255 };
256
257 static struct snd_soc_dai_link cz_dai_7219_98357[] = {
258         {
259                 .name = "amd-da7219-play",
260                 .stream_name = "Playback",
261                 .platform_name = "acp_audio_dma.0.auto",
262                 .cpu_dai_name = "designware-i2s.1.auto",
263                 .codec_dai_name = "da7219-hifi",
264                 .codec_name = "i2c-DLGS7219:00",
265                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
266                                 | SND_SOC_DAIFMT_CBM_CFM,
267                 .init = cz_da7219_init,
268                 .dpcm_playback = 1,
269                 .ops = &cz_da7219_cap_ops,
270         },
271         {
272                 .name = "amd-da7219-cap",
273                 .stream_name = "Capture",
274                 .platform_name = "acp_audio_dma.0.auto",
275                 .cpu_dai_name = "designware-i2s.2.auto",
276                 .codec_dai_name = "da7219-hifi",
277                 .codec_name = "i2c-DLGS7219:00",
278                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
279                                 | SND_SOC_DAIFMT_CBM_CFM,
280                 .dpcm_capture = 1,
281                 .ops = &cz_da7219_cap_ops,
282         },
283         {
284                 .name = "amd-max98357-play",
285                 .stream_name = "HiFi Playback",
286                 .platform_name = "acp_audio_dma.0.auto",
287                 .cpu_dai_name = "designware-i2s.3.auto",
288                 .codec_dai_name = "HiFi",
289                 .codec_name = "MX98357A:00",
290                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
291                                 | SND_SOC_DAIFMT_CBM_CFM,
292                 .dpcm_playback = 1,
293                 .ops = &cz_max_play_ops,
294         },
295         {
296                 /* C panel DMIC */
297                 .name = "dmic0",
298                 .stream_name = "DMIC0 Capture",
299                 .platform_name = "acp_audio_dma.0.auto",
300                 .cpu_dai_name = "designware-i2s.3.auto",
301                 .codec_dai_name = "adau7002-hifi",
302                 .codec_name = "ADAU7002:00",
303                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
304                                 | SND_SOC_DAIFMT_CBM_CFM,
305                 .dpcm_capture = 1,
306                 .ops = &cz_dmic0_cap_ops,
307         },
308         {
309                 /* A/B panel DMIC */
310                 .name = "dmic1",
311                 .stream_name = "DMIC1 Capture",
312                 .platform_name = "acp_audio_dma.0.auto",
313                 .cpu_dai_name = "designware-i2s.2.auto",
314                 .codec_dai_name = "adau7002-hifi",
315                 .codec_name = "ADAU7002:00",
316                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
317                                 | SND_SOC_DAIFMT_CBM_CFM,
318                 .dpcm_capture = 1,
319                 .ops = &cz_dmic1_cap_ops,
320         },
321 };
322
323 static const struct snd_soc_dapm_widget cz_widgets[] = {
324         SND_SOC_DAPM_HP("Headphones", NULL),
325         SND_SOC_DAPM_SPK("Speakers", NULL),
326         SND_SOC_DAPM_MIC("Headset Mic", NULL),
327         SND_SOC_DAPM_MIC("Int Mic", NULL),
328 };
329
330 static const struct snd_soc_dapm_route cz_audio_route[] = {
331         {"Headphones", NULL, "HPL"},
332         {"Headphones", NULL, "HPR"},
333         {"MIC", NULL, "Headset Mic"},
334         {"Speakers", NULL, "Speaker"},
335         {"PDM_DAT", NULL, "Int Mic"},
336 };
337
338 static const struct snd_kcontrol_new cz_mc_controls[] = {
339         SOC_DAPM_PIN_SWITCH("Headphones"),
340         SOC_DAPM_PIN_SWITCH("Speakers"),
341         SOC_DAPM_PIN_SWITCH("Headset Mic"),
342         SOC_DAPM_PIN_SWITCH("Int Mic"),
343 };
344
345 static struct snd_soc_card cz_card = {
346         .name = "acpd7219m98357",
347         .owner = THIS_MODULE,
348         .dai_link = cz_dai_7219_98357,
349         .num_links = ARRAY_SIZE(cz_dai_7219_98357),
350         .dapm_widgets = cz_widgets,
351         .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
352         .dapm_routes = cz_audio_route,
353         .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
354         .controls = cz_mc_controls,
355         .num_controls = ARRAY_SIZE(cz_mc_controls),
356 };
357
358 static struct regulator_consumer_supply acp_da7219_supplies[] = {
359         REGULATOR_SUPPLY("VDD", "i2c-DLGS7219:00"),
360         REGULATOR_SUPPLY("VDDMIC", "i2c-DLGS7219:00"),
361         REGULATOR_SUPPLY("VDDIO", "i2c-DLGS7219:00"),
362         REGULATOR_SUPPLY("IOVDD", "ADAU7002:00"),
363 };
364
365 static struct regulator_init_data acp_da7219_data = {
366         .constraints = {
367                 .always_on = 1,
368         },
369         .num_consumer_supplies = ARRAY_SIZE(acp_da7219_supplies),
370         .consumer_supplies = acp_da7219_supplies,
371 };
372
373 static struct regulator_config acp_da7219_cfg = {
374         .init_data = &acp_da7219_data,
375 };
376
377 static struct regulator_ops acp_da7219_ops = {
378 };
379
380 static struct regulator_desc acp_da7219_desc = {
381         .name = "reg-fixed-1.8V",
382         .type = REGULATOR_VOLTAGE,
383         .owner = THIS_MODULE,
384         .ops = &acp_da7219_ops,
385         .fixed_uV = 1800000, /* 1.8V */
386         .n_voltages = 1,
387 };
388
389 static int cz_probe(struct platform_device *pdev)
390 {
391         int ret;
392         struct snd_soc_card *card;
393         struct acp_platform_info *machine;
394         struct regulator_dev *rdev;
395
396         acp_da7219_cfg.dev = &pdev->dev;
397         rdev = devm_regulator_register(&pdev->dev, &acp_da7219_desc,
398                                        &acp_da7219_cfg);
399         if (IS_ERR(rdev)) {
400                 dev_err(&pdev->dev, "Failed to register regulator: %d\n",
401                         (int)PTR_ERR(rdev));
402                 return -EINVAL;
403         }
404
405         machine = devm_kzalloc(&pdev->dev, sizeof(struct acp_platform_info),
406                                GFP_KERNEL);
407         if (!machine)
408                 return -ENOMEM;
409         card = &cz_card;
410         cz_card.dev = &pdev->dev;
411         platform_set_drvdata(pdev, card);
412         snd_soc_card_set_drvdata(card, machine);
413         ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
414         if (ret) {
415                 dev_err(&pdev->dev,
416                                 "devm_snd_soc_register_card(%s) failed: %d\n",
417                                 cz_card.name, ret);
418                 return ret;
419         }
420         bt_uart_enable = !device_property_read_bool(&pdev->dev,
421                                                     "bt-pad-enable");
422         return 0;
423 }
424
425 static const struct acpi_device_id cz_audio_acpi_match[] = {
426         { "AMD7219", 0 },
427         {},
428 };
429 MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
430
431 static struct platform_driver cz_pcm_driver = {
432         .driver = {
433                 .name = "cz-da7219-max98357a",
434                 .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
435                 .pm = &snd_soc_pm_ops,
436         },
437         .probe = cz_probe,
438 };
439
440 module_platform_driver(cz_pcm_driver);
441
442 MODULE_AUTHOR("akshu.agrawal@amd.com");
443 MODULE_DESCRIPTION("DA7219 & MAX98357A audio support");
444 MODULE_LICENSE("GPL v2");