1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt8173-max98090.c  --  MT8173 MAX98090 ALSA SoC machine driver
4  *
5  * Copyright (c) 2015 MediaTek Inc.
6  * Author: Koro Chen <koro.chen@mediatek.com>
7  */
8 
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/jack.h>
12 #include <linux/gpio.h>
13 #include "../../codecs/max98090.h"
14 
15 static struct snd_soc_jack mt8173_max98090_jack;
16 
17 static struct snd_soc_jack_pin mt8173_max98090_jack_pins[] = {
18 	{
19 		.pin	= "Headphone",
20 		.mask	= SND_JACK_HEADPHONE,
21 	},
22 	{
23 		.pin	= "Headset Mic",
24 		.mask	= SND_JACK_MICROPHONE,
25 	},
26 };
27 
28 static const struct snd_soc_dapm_widget mt8173_max98090_widgets[] = {
29 	SND_SOC_DAPM_SPK("Speaker", NULL),
30 	SND_SOC_DAPM_MIC("Int Mic", NULL),
31 	SND_SOC_DAPM_HP("Headphone", NULL),
32 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
33 };
34 
35 static const struct snd_soc_dapm_route mt8173_max98090_routes[] = {
36 	{"Speaker", NULL, "SPKL"},
37 	{"Speaker", NULL, "SPKR"},
38 	{"DMICL", NULL, "Int Mic"},
39 	{"Headphone", NULL, "HPL"},
40 	{"Headphone", NULL, "HPR"},
41 	{"Headset Mic", NULL, "MICBIAS"},
42 	{"IN34", NULL, "Headset Mic"},
43 };
44 
45 static const struct snd_kcontrol_new mt8173_max98090_controls[] = {
46 	SOC_DAPM_PIN_SWITCH("Speaker"),
47 	SOC_DAPM_PIN_SWITCH("Int Mic"),
48 	SOC_DAPM_PIN_SWITCH("Headphone"),
49 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
50 };
51 
52 static int mt8173_max98090_hw_params(struct snd_pcm_substream *substream,
53 				     struct snd_pcm_hw_params *params)
54 {
55 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
56 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
57 
58 	return snd_soc_dai_set_sysclk(codec_dai, 0, params_rate(params) * 256,
59 				      SND_SOC_CLOCK_IN);
60 }
61 
62 static const struct snd_soc_ops mt8173_max98090_ops = {
63 	.hw_params = mt8173_max98090_hw_params,
64 };
65 
66 static int mt8173_max98090_init(struct snd_soc_pcm_runtime *runtime)
67 {
68 	int ret;
69 	struct snd_soc_card *card = runtime->card;
70 	struct snd_soc_component *component = runtime->codec_dai->component;
71 
72 	/* enable jack detection */
73 	ret = snd_soc_card_jack_new(card, "Headphone", SND_JACK_HEADPHONE,
74 				    &mt8173_max98090_jack,
75 				    mt8173_max98090_jack_pins,
76 				    ARRAY_SIZE(mt8173_max98090_jack_pins));
77 	if (ret) {
78 		dev_err(card->dev, "Can't create a new Jack %d\n", ret);
79 		return ret;
80 	}
81 
82 	return max98090_mic_detect(component, &mt8173_max98090_jack);
83 }
84 
85 /* Digital audio interface glue - connects codec <---> CPU */
86 static struct snd_soc_dai_link mt8173_max98090_dais[] = {
87 	/* Front End DAI links */
88 	{
89 		.name = "MAX98090 Playback",
90 		.stream_name = "MAX98090 Playback",
91 		.cpu_dai_name = "DL1",
92 		.codec_name = "snd-soc-dummy",
93 		.codec_dai_name = "snd-soc-dummy-dai",
94 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
95 		.dynamic = 1,
96 		.dpcm_playback = 1,
97 	},
98 	{
99 		.name = "MAX98090 Capture",
100 		.stream_name = "MAX98090 Capture",
101 		.cpu_dai_name = "VUL",
102 		.codec_name = "snd-soc-dummy",
103 		.codec_dai_name = "snd-soc-dummy-dai",
104 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
105 		.dynamic = 1,
106 		.dpcm_capture = 1,
107 	},
108 	/* Back End DAI links */
109 	{
110 		.name = "Codec",
111 		.cpu_dai_name = "I2S",
112 		.no_pcm = 1,
113 		.codec_dai_name = "HiFi",
114 		.init = mt8173_max98090_init,
115 		.ops = &mt8173_max98090_ops,
116 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
117 			   SND_SOC_DAIFMT_CBS_CFS,
118 		.dpcm_playback = 1,
119 		.dpcm_capture = 1,
120 	},
121 };
122 
123 static struct snd_soc_card mt8173_max98090_card = {
124 	.name = "mt8173-max98090",
125 	.owner = THIS_MODULE,
126 	.dai_link = mt8173_max98090_dais,
127 	.num_links = ARRAY_SIZE(mt8173_max98090_dais),
128 	.controls = mt8173_max98090_controls,
129 	.num_controls = ARRAY_SIZE(mt8173_max98090_controls),
130 	.dapm_widgets = mt8173_max98090_widgets,
131 	.num_dapm_widgets = ARRAY_SIZE(mt8173_max98090_widgets),
132 	.dapm_routes = mt8173_max98090_routes,
133 	.num_dapm_routes = ARRAY_SIZE(mt8173_max98090_routes),
134 };
135 
136 static int mt8173_max98090_dev_probe(struct platform_device *pdev)
137 {
138 	struct snd_soc_card *card = &mt8173_max98090_card;
139 	struct device_node *codec_node, *platform_node;
140 	int ret, i;
141 
142 	platform_node = of_parse_phandle(pdev->dev.of_node,
143 					 "mediatek,platform", 0);
144 	if (!platform_node) {
145 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
146 		return -EINVAL;
147 	}
148 	for (i = 0; i < card->num_links; i++) {
149 		if (mt8173_max98090_dais[i].platform_name)
150 			continue;
151 		mt8173_max98090_dais[i].platform_of_node = platform_node;
152 	}
153 
154 	codec_node = of_parse_phandle(pdev->dev.of_node,
155 				      "mediatek,audio-codec", 0);
156 	if (!codec_node) {
157 		dev_err(&pdev->dev,
158 			"Property 'audio-codec' missing or invalid\n");
159 		return -EINVAL;
160 	}
161 	for (i = 0; i < card->num_links; i++) {
162 		if (mt8173_max98090_dais[i].codec_name)
163 			continue;
164 		mt8173_max98090_dais[i].codec_of_node = codec_node;
165 	}
166 	card->dev = &pdev->dev;
167 
168 	ret = devm_snd_soc_register_card(&pdev->dev, card);
169 	if (ret)
170 		dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
171 			__func__, ret);
172 	return ret;
173 }
174 
175 static const struct of_device_id mt8173_max98090_dt_match[] = {
176 	{ .compatible = "mediatek,mt8173-max98090", },
177 	{ }
178 };
179 MODULE_DEVICE_TABLE(of, mt8173_max98090_dt_match);
180 
181 static struct platform_driver mt8173_max98090_driver = {
182 	.driver = {
183 		   .name = "mt8173-max98090",
184 		   .of_match_table = mt8173_max98090_dt_match,
185 #ifdef CONFIG_PM
186 		   .pm = &snd_soc_pm_ops,
187 #endif
188 	},
189 	.probe = mt8173_max98090_dev_probe,
190 };
191 
192 module_platform_driver(mt8173_max98090_driver);
193 
194 /* Module information */
195 MODULE_DESCRIPTION("MT8173 MAX98090 ALSA SoC machine driver");
196 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
197 MODULE_LICENSE("GPL v2");
198 MODULE_ALIAS("platform:mt8173-max98090");
199 
200