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 	struct snd_soc_dai_link *dai_link;
141 	int ret, i;
142 
143 	platform_node = of_parse_phandle(pdev->dev.of_node,
144 					 "mediatek,platform", 0);
145 	if (!platform_node) {
146 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
147 		return -EINVAL;
148 	}
149 	for_each_card_prelinks(card, i, dai_link) {
150 		if (dai_link->platform_name)
151 			continue;
152 		dai_link->platform_of_node = platform_node;
153 	}
154 
155 	codec_node = of_parse_phandle(pdev->dev.of_node,
156 				      "mediatek,audio-codec", 0);
157 	if (!codec_node) {
158 		dev_err(&pdev->dev,
159 			"Property 'audio-codec' missing or invalid\n");
160 		return -EINVAL;
161 	}
162 	for_each_card_prelinks(card, i, dai_link) {
163 		if (dai_link->codec_name)
164 			continue;
165 		dai_link->codec_of_node = codec_node;
166 	}
167 	card->dev = &pdev->dev;
168 
169 	ret = devm_snd_soc_register_card(&pdev->dev, card);
170 	if (ret)
171 		dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
172 			__func__, ret);
173 	return ret;
174 }
175 
176 static const struct of_device_id mt8173_max98090_dt_match[] = {
177 	{ .compatible = "mediatek,mt8173-max98090", },
178 	{ }
179 };
180 MODULE_DEVICE_TABLE(of, mt8173_max98090_dt_match);
181 
182 static struct platform_driver mt8173_max98090_driver = {
183 	.driver = {
184 		   .name = "mt8173-max98090",
185 		   .of_match_table = mt8173_max98090_dt_match,
186 #ifdef CONFIG_PM
187 		   .pm = &snd_soc_pm_ops,
188 #endif
189 	},
190 	.probe = mt8173_max98090_dev_probe,
191 };
192 
193 module_platform_driver(mt8173_max98090_driver);
194 
195 /* Module information */
196 MODULE_DESCRIPTION("MT8173 MAX98090 ALSA SoC machine driver");
197 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
198 MODULE_LICENSE("GPL v2");
199 MODULE_ALIAS("platform:mt8173-max98090");
200 
201