1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2021 Advanced Micro Devices, Inc.
7 //
8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9 //
10 
11 /*
12  * Machine Driver Legacy Support for ACP HW block
13  */
14 
15 #include <sound/core.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc-acpi.h>
18 #include <sound/soc-dapm.h>
19 #include <linux/module.h>
20 
21 #include "acp-mach.h"
22 
23 static struct acp_card_drvdata rt5682_rt1019_data = {
24 	.hs_cpu_id = I2S_SP,
25 	.amp_cpu_id = I2S_SP,
26 	.dmic_cpu_id = DMIC,
27 	.hs_codec_id = RT5682,
28 	.amp_codec_id = RT1019,
29 	.dmic_codec_id = DMIC,
30 	.gpio_spkr_en = EN_SPKR_GPIO_GB,
31 };
32 
33 static struct acp_card_drvdata rt5682s_max_data = {
34 	.hs_cpu_id = I2S_SP,
35 	.amp_cpu_id = I2S_SP,
36 	.dmic_cpu_id = DMIC,
37 	.hs_codec_id = RT5682S,
38 	.amp_codec_id = MAX98360A,
39 	.dmic_codec_id = DMIC,
40 	.gpio_spkr_en = EN_SPKR_GPIO_NONE,
41 };
42 
43 static struct acp_card_drvdata rt5682s_rt1019_data = {
44 	.hs_cpu_id = I2S_SP,
45 	.amp_cpu_id = I2S_SP,
46 	.dmic_cpu_id = DMIC,
47 	.hs_codec_id = RT5682S,
48 	.amp_codec_id = RT1019,
49 	.dmic_codec_id = DMIC,
50 	.gpio_spkr_en = EN_SPKR_GPIO_NONE,
51 };
52 
53 static const struct snd_kcontrol_new acp_controls[] = {
54 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
55 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
56 	SOC_DAPM_PIN_SWITCH("Spk"),
57 	SOC_DAPM_PIN_SWITCH("Left Spk"),
58 	SOC_DAPM_PIN_SWITCH("Right Spk"),
59 
60 };
61 
62 static const struct snd_soc_dapm_widget acp_widgets[] = {
63 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
64 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
65 	SND_SOC_DAPM_SPK("Spk", event_spkr_handler),
66 	SND_SOC_DAPM_SPK("Left Spk", event_spkr_handler),
67 	SND_SOC_DAPM_SPK("Right Spk", event_spkr_handler),
68 };
69 
70 static int acp_asoc_probe(struct platform_device *pdev)
71 {
72 	struct snd_soc_card *card = NULL;
73 	struct device *dev = &pdev->dev;
74 	unsigned int spkr_gpio;
75 	int ret;
76 
77 	if (!pdev->id_entry)
78 		return -EINVAL;
79 
80 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
81 	if (!card)
82 		return -ENOMEM;
83 
84 	card->dev = dev;
85 	card->owner = THIS_MODULE;
86 	card->name = pdev->id_entry->name;
87 	card->dapm_widgets = acp_widgets;
88 	card->num_dapm_widgets = ARRAY_SIZE(acp_widgets);
89 	card->controls = acp_controls;
90 	card->num_controls = ARRAY_SIZE(acp_controls);
91 	card->drvdata = (struct acp_card_drvdata *)pdev->id_entry->driver_data;
92 	spkr_gpio = ((struct acp_card_drvdata *)(card->drvdata))->gpio_spkr_en;
93 
94 	acp_legacy_dai_links_create(card);
95 
96 	if (gpio_is_valid(spkr_gpio)) {
97 		ret = devm_gpio_request(dev, spkr_gpio, "spkren");
98 		if (ret) {
99 			dev_err(dev, "(%s) gpio request failed: %d\n",
100 				__func__, ret);
101 			return ret;
102 		}
103 		gpio_direction_output(spkr_gpio, 0);
104 	}
105 
106 	ret = devm_snd_soc_register_card(&pdev->dev, card);
107 	if (ret) {
108 		dev_err(&pdev->dev,
109 				"devm_snd_soc_register_card(%s) failed: %d\n",
110 				card->name, ret);
111 		return ret;
112 	}
113 
114 	return 0;
115 }
116 
117 static const struct platform_device_id board_ids[] = {
118 	{
119 		.name = "acp3xalc56821019",
120 		.driver_data = (kernel_ulong_t)&rt5682_rt1019_data,
121 	},
122 	{
123 		.name = "acp3xalc5682sm98360",
124 		.driver_data = (kernel_ulong_t)&rt5682s_max_data,
125 	},
126 	{
127 		.name = "acp3xalc5682s1019",
128 		.driver_data = (kernel_ulong_t)&rt5682s_rt1019_data,
129 	},
130 	{ }
131 };
132 static struct platform_driver acp_asoc_audio = {
133 	.driver = {
134 		.name = "acp_mach",
135 	},
136 	.probe = acp_asoc_probe,
137 	.id_table = board_ids,
138 };
139 
140 module_platform_driver(acp_asoc_audio);
141 
142 MODULE_IMPORT_NS(SND_SOC_AMD_MACH);
143 MODULE_DESCRIPTION("ACP chrome audio support");
144 MODULE_ALIAS("platform:acp3xalc56821019");
145 MODULE_ALIAS("platform:acp3xalc5682sm98360");
146 MODULE_ALIAS("platform:acp3xalc5682s1019");
147 MODULE_LICENSE("GPL v2");
148