1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (C) 2015 - 2016 Samsung Electronics Co., Ltd.
4 //
5 // Authors: Inha Song <ideal.song@samsung.com>
6 //          Sylwester Nawrocki <s.nawrocki@samsung.com>
7 
8 #include <linux/clk.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 
15 #include "i2s.h"
16 #include "../codecs/wm5110.h"
17 
18 /*
19  * The source clock is XCLKOUT with its mux set to the external fixed rate
20  * oscillator (XXTI).
21  */
22 #define MCLK_RATE	24000000U
23 
24 #define TM2_DAI_AIF1	0
25 #define TM2_DAI_AIF2	1
26 
27 struct tm2_machine_priv {
28 	struct snd_soc_component *component;
29 	unsigned int sysclk_rate;
30 	struct gpio_desc *gpio_mic_bias;
31 };
32 
tm2_start_sysclk(struct snd_soc_card * card)33 static int tm2_start_sysclk(struct snd_soc_card *card)
34 {
35 	struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
36 	struct snd_soc_component *component = priv->component;
37 	int ret;
38 
39 	ret = snd_soc_component_set_pll(component, WM5110_FLL1_REFCLK,
40 				    ARIZONA_FLL_SRC_MCLK1,
41 				    MCLK_RATE,
42 				    priv->sysclk_rate);
43 	if (ret < 0) {
44 		dev_err(component->dev, "Failed to set FLL1 source: %d\n", ret);
45 		return ret;
46 	}
47 
48 	ret = snd_soc_component_set_pll(component, WM5110_FLL1,
49 				    ARIZONA_FLL_SRC_MCLK1,
50 				    MCLK_RATE,
51 				    priv->sysclk_rate);
52 	if (ret < 0) {
53 		dev_err(component->dev, "Failed to start FLL1: %d\n", ret);
54 		return ret;
55 	}
56 
57 	ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK,
58 				       ARIZONA_CLK_SRC_FLL1,
59 				       priv->sysclk_rate,
60 				       SND_SOC_CLOCK_IN);
61 	if (ret < 0) {
62 		dev_err(component->dev, "Failed to set SYSCLK source: %d\n", ret);
63 		return ret;
64 	}
65 
66 	return 0;
67 }
68 
tm2_stop_sysclk(struct snd_soc_card * card)69 static int tm2_stop_sysclk(struct snd_soc_card *card)
70 {
71 	struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
72 	struct snd_soc_component *component = priv->component;
73 	int ret;
74 
75 	ret = snd_soc_component_set_pll(component, WM5110_FLL1, 0, 0, 0);
76 	if (ret < 0) {
77 		dev_err(component->dev, "Failed to stop FLL1: %d\n", ret);
78 		return ret;
79 	}
80 
81 	ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK,
82 				       ARIZONA_CLK_SRC_FLL1, 0, 0);
83 	if (ret < 0) {
84 		dev_err(component->dev, "Failed to stop SYSCLK: %d\n", ret);
85 		return ret;
86 	}
87 
88 	return 0;
89 }
90 
tm2_aif1_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)91 static int tm2_aif1_hw_params(struct snd_pcm_substream *substream,
92 				struct snd_pcm_hw_params *params)
93 {
94 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
95 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
96 	struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(rtd->card);
97 
98 	switch (params_rate(params)) {
99 	case 4000:
100 	case 8000:
101 	case 12000:
102 	case 16000:
103 	case 24000:
104 	case 32000:
105 	case 48000:
106 	case 96000:
107 	case 192000:
108 		/* Highest possible SYSCLK frequency: 147.456MHz */
109 		priv->sysclk_rate = 147456000U;
110 		break;
111 	case 11025:
112 	case 22050:
113 	case 44100:
114 	case 88200:
115 	case 176400:
116 		/* Highest possible SYSCLK frequency: 135.4752 MHz */
117 		priv->sysclk_rate = 135475200U;
118 		break;
119 	default:
120 		dev_err(component->dev, "Not supported sample rate: %d\n",
121 			params_rate(params));
122 		return -EINVAL;
123 	}
124 
125 	return tm2_start_sysclk(rtd->card);
126 }
127 
128 static const struct snd_soc_ops tm2_aif1_ops = {
129 	.hw_params = tm2_aif1_hw_params,
130 };
131 
tm2_aif2_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)132 static int tm2_aif2_hw_params(struct snd_pcm_substream *substream,
133 				struct snd_pcm_hw_params *params)
134 {
135 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
136 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
137 	unsigned int asyncclk_rate;
138 	int ret;
139 
140 	switch (params_rate(params)) {
141 	case 8000:
142 	case 12000:
143 	case 16000:
144 		/* Highest possible ASYNCCLK frequency: 49.152MHz */
145 		asyncclk_rate = 49152000U;
146 		break;
147 	case 11025:
148 		/* Highest possible ASYNCCLK frequency: 45.1584 MHz */
149 		asyncclk_rate = 45158400U;
150 		break;
151 	default:
152 		dev_err(component->dev, "Not supported sample rate: %d\n",
153 			params_rate(params));
154 		return -EINVAL;
155 	}
156 
157 	ret = snd_soc_component_set_pll(component, WM5110_FLL2_REFCLK,
158 				    ARIZONA_FLL_SRC_MCLK1,
159 				    MCLK_RATE,
160 				    asyncclk_rate);
161 	if (ret < 0) {
162 		dev_err(component->dev, "Failed to set FLL2 source: %d\n", ret);
163 		return ret;
164 	}
165 
166 	ret = snd_soc_component_set_pll(component, WM5110_FLL2,
167 				    ARIZONA_FLL_SRC_MCLK1,
168 				    MCLK_RATE,
169 				    asyncclk_rate);
170 	if (ret < 0) {
171 		dev_err(component->dev, "Failed to start FLL2: %d\n", ret);
172 		return ret;
173 	}
174 
175 	ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_ASYNCCLK,
176 				       ARIZONA_CLK_SRC_FLL2,
177 				       asyncclk_rate,
178 				       SND_SOC_CLOCK_IN);
179 	if (ret < 0) {
180 		dev_err(component->dev, "Failed to set ASYNCCLK source: %d\n", ret);
181 		return ret;
182 	}
183 
184 	return 0;
185 }
186 
tm2_aif2_hw_free(struct snd_pcm_substream * substream)187 static int tm2_aif2_hw_free(struct snd_pcm_substream *substream)
188 {
189 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
190 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
191 	int ret;
192 
193 	/* disable FLL2 */
194 	ret = snd_soc_component_set_pll(component, WM5110_FLL2, ARIZONA_FLL_SRC_MCLK1,
195 				    0, 0);
196 	if (ret < 0)
197 		dev_err(component->dev, "Failed to stop FLL2: %d\n", ret);
198 
199 	return ret;
200 }
201 
202 static const struct snd_soc_ops tm2_aif2_ops = {
203 	.hw_params = tm2_aif2_hw_params,
204 	.hw_free = tm2_aif2_hw_free,
205 };
206 
tm2_hdmi_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)207 static int tm2_hdmi_hw_params(struct snd_pcm_substream *substream,
208 			      struct snd_pcm_hw_params *params)
209 {
210 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
211 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
212 	unsigned int bfs;
213 	int bitwidth, ret;
214 
215 	bitwidth = snd_pcm_format_width(params_format(params));
216 	if (bitwidth < 0) {
217 		dev_err(rtd->card->dev, "Invalid bit-width: %d\n", bitwidth);
218 		return bitwidth;
219 	}
220 
221 	switch (bitwidth) {
222 	case 48:
223 		bfs = 64;
224 		break;
225 	case 16:
226 		bfs = 32;
227 		break;
228 	default:
229 		dev_err(rtd->card->dev, "Unsupported bit-width: %d\n", bitwidth);
230 		return -EINVAL;
231 	}
232 
233 	switch (params_rate(params)) {
234 	case 48000:
235 	case 96000:
236 	case 192000:
237 		break;
238 	default:
239 		dev_err(rtd->card->dev, "Unsupported sample rate: %d\n",
240 			params_rate(params));
241 		return -EINVAL;
242 	}
243 
244 	ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK,
245 					0, SAMSUNG_I2S_OPCLK_PCLK);
246 	if (ret < 0)
247 		return ret;
248 
249 	ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs);
250 	if (ret < 0)
251 		return ret;
252 
253 	return 0;
254 }
255 
256 static const struct snd_soc_ops tm2_hdmi_ops = {
257 	.hw_params = tm2_hdmi_hw_params,
258 };
259 
tm2_mic_bias(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)260 static int tm2_mic_bias(struct snd_soc_dapm_widget *w,
261 				struct snd_kcontrol *kcontrol, int event)
262 {
263 	struct snd_soc_card *card = w->dapm->card;
264 	struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
265 
266 	switch (event) {
267 	case SND_SOC_DAPM_PRE_PMU:
268 		gpiod_set_value_cansleep(priv->gpio_mic_bias,  1);
269 		break;
270 	case SND_SOC_DAPM_POST_PMD:
271 		gpiod_set_value_cansleep(priv->gpio_mic_bias,  0);
272 		break;
273 	}
274 
275 	return 0;
276 }
277 
tm2_set_bias_level(struct snd_soc_card * card,struct snd_soc_dapm_context * dapm,enum snd_soc_bias_level level)278 static int tm2_set_bias_level(struct snd_soc_card *card,
279 				struct snd_soc_dapm_context *dapm,
280 				enum snd_soc_bias_level level)
281 {
282 	struct snd_soc_pcm_runtime *rtd;
283 
284 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
285 
286 	if (dapm->dev != snd_soc_rtd_to_codec(rtd, 0)->dev)
287 		return 0;
288 
289 	switch (level) {
290 	case SND_SOC_BIAS_STANDBY:
291 		if (card->dapm.bias_level == SND_SOC_BIAS_OFF)
292 			tm2_start_sysclk(card);
293 		break;
294 	case SND_SOC_BIAS_OFF:
295 		tm2_stop_sysclk(card);
296 		break;
297 	default:
298 		break;
299 	}
300 
301 	return 0;
302 }
303 
304 static struct snd_soc_aux_dev tm2_speaker_amp_dev;
305 
tm2_late_probe(struct snd_soc_card * card)306 static int tm2_late_probe(struct snd_soc_card *card)
307 {
308 	struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
309 	unsigned int ch_map[] = { 0, 1 };
310 	struct snd_soc_dai *amp_pdm_dai;
311 	struct snd_soc_pcm_runtime *rtd;
312 	struct snd_soc_dai *aif1_dai;
313 	struct snd_soc_dai *aif2_dai;
314 	int ret;
315 
316 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[TM2_DAI_AIF1]);
317 	aif1_dai = snd_soc_rtd_to_codec(rtd, 0);
318 	priv->component = snd_soc_rtd_to_codec(rtd, 0)->component;
319 
320 	ret = snd_soc_dai_set_sysclk(aif1_dai, ARIZONA_CLK_SYSCLK, 0, 0);
321 	if (ret < 0) {
322 		dev_err(aif1_dai->dev, "Failed to set SYSCLK: %d\n", ret);
323 		return ret;
324 	}
325 
326 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[TM2_DAI_AIF2]);
327 	aif2_dai = snd_soc_rtd_to_codec(rtd, 0);
328 
329 	ret = snd_soc_dai_set_sysclk(aif2_dai, ARIZONA_CLK_ASYNCCLK, 0, 0);
330 	if (ret < 0) {
331 		dev_err(aif2_dai->dev, "Failed to set ASYNCCLK: %d\n", ret);
332 		return ret;
333 	}
334 
335 	amp_pdm_dai = snd_soc_find_dai(&tm2_speaker_amp_dev.dlc);
336 	if (!amp_pdm_dai)
337 		return -ENODEV;
338 
339 	/* Set the MAX98504 V/I sense PDM Tx DAI channel mapping */
340 	ret = snd_soc_dai_set_channel_map(amp_pdm_dai, ARRAY_SIZE(ch_map),
341 					  ch_map, 0, NULL);
342 	if (ret < 0)
343 		return ret;
344 
345 	ret = snd_soc_dai_set_tdm_slot(amp_pdm_dai, 0x3, 0x0, 2, 16);
346 	if (ret < 0)
347 		return ret;
348 
349 	return 0;
350 }
351 
352 static const struct snd_kcontrol_new tm2_controls[] = {
353 	SOC_DAPM_PIN_SWITCH("HP"),
354 	SOC_DAPM_PIN_SWITCH("SPK"),
355 	SOC_DAPM_PIN_SWITCH("RCV"),
356 	SOC_DAPM_PIN_SWITCH("VPS"),
357 	SOC_DAPM_PIN_SWITCH("HDMI"),
358 
359 	SOC_DAPM_PIN_SWITCH("Main Mic"),
360 	SOC_DAPM_PIN_SWITCH("Sub Mic"),
361 	SOC_DAPM_PIN_SWITCH("Third Mic"),
362 
363 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
364 };
365 
366 static const struct snd_soc_dapm_widget tm2_dapm_widgets[] = {
367 	SND_SOC_DAPM_HP("HP", NULL),
368 	SND_SOC_DAPM_SPK("SPK", NULL),
369 	SND_SOC_DAPM_SPK("RCV", NULL),
370 	SND_SOC_DAPM_LINE("VPS", NULL),
371 	SND_SOC_DAPM_LINE("HDMI", NULL),
372 
373 	SND_SOC_DAPM_MIC("Main Mic", tm2_mic_bias),
374 	SND_SOC_DAPM_MIC("Sub Mic", NULL),
375 	SND_SOC_DAPM_MIC("Third Mic", NULL),
376 
377 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
378 };
379 
380 static const struct snd_soc_component_driver tm2_component = {
381 	.name	= "tm2-audio",
382 };
383 
384 static struct snd_soc_dai_driver tm2_ext_dai[] = {
385 	{
386 		.name = "Voice call",
387 		.playback = {
388 			.channels_min = 1,
389 			.channels_max = 4,
390 			.rate_min = 8000,
391 			.rate_max = 48000,
392 			.rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
393 					SNDRV_PCM_RATE_48000),
394 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
395 		},
396 		.capture = {
397 			.channels_min = 1,
398 			.channels_max = 4,
399 			.rate_min = 8000,
400 			.rate_max = 48000,
401 			.rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
402 					SNDRV_PCM_RATE_48000),
403 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
404 		},
405 	},
406 	{
407 		.name = "Bluetooth",
408 		.playback = {
409 			.channels_min = 1,
410 			.channels_max = 4,
411 			.rate_min = 8000,
412 			.rate_max = 16000,
413 			.rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
414 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
415 		},
416 		.capture = {
417 			.channels_min = 1,
418 			.channels_max = 2,
419 			.rate_min = 8000,
420 			.rate_max = 16000,
421 			.rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
422 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
423 		},
424 	},
425 };
426 
427 SND_SOC_DAILINK_DEFS(aif1,
428 	DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
429 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm5110-aif1")),
430 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
431 
432 SND_SOC_DAILINK_DEFS(voice,
433 	DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
434 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm5110-aif2")),
435 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
436 
437 SND_SOC_DAILINK_DEFS(bt,
438 	DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
439 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm5110-aif3")),
440 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
441 
442 SND_SOC_DAILINK_DEFS(hdmi,
443 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
444 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
445 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
446 
447 static struct snd_soc_dai_link tm2_dai_links[] = {
448 	{
449 		.name		= "WM5110 AIF1",
450 		.stream_name	= "HiFi Primary",
451 		.ops		= &tm2_aif1_ops,
452 		.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
453 				  SND_SOC_DAIFMT_CBP_CFP,
454 		SND_SOC_DAILINK_REG(aif1),
455 	}, {
456 		.name		= "WM5110 Voice",
457 		.stream_name	= "Voice call",
458 		.ops		= &tm2_aif2_ops,
459 		.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
460 				  SND_SOC_DAIFMT_CBP_CFP,
461 		.ignore_suspend = 1,
462 		SND_SOC_DAILINK_REG(voice),
463 	}, {
464 		.name		= "WM5110 BT",
465 		.stream_name	= "Bluetooth",
466 		.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
467 				  SND_SOC_DAIFMT_CBP_CFP,
468 		.ignore_suspend = 1,
469 		SND_SOC_DAILINK_REG(bt),
470 	}, {
471 		.name		= "HDMI",
472 		.stream_name	= "i2s1",
473 		.ops		= &tm2_hdmi_ops,
474 		.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
475 				  SND_SOC_DAIFMT_CBC_CFC,
476 		SND_SOC_DAILINK_REG(hdmi),
477 	}
478 };
479 
480 static struct snd_soc_card tm2_card = {
481 	.owner			= THIS_MODULE,
482 
483 	.dai_link		= tm2_dai_links,
484 	.controls		= tm2_controls,
485 	.num_controls		= ARRAY_SIZE(tm2_controls),
486 	.dapm_widgets		= tm2_dapm_widgets,
487 	.num_dapm_widgets	= ARRAY_SIZE(tm2_dapm_widgets),
488 	.aux_dev		= &tm2_speaker_amp_dev,
489 	.num_aux_devs		= 1,
490 
491 	.late_probe		= tm2_late_probe,
492 	.set_bias_level		= tm2_set_bias_level,
493 };
494 
tm2_probe(struct platform_device * pdev)495 static int tm2_probe(struct platform_device *pdev)
496 {
497 	struct device_node *cpu_dai_node[2] = {};
498 	struct device_node *codec_dai_node[2] = {};
499 	const char *cells_name = NULL;
500 	struct device *dev = &pdev->dev;
501 	struct snd_soc_card *card = &tm2_card;
502 	struct tm2_machine_priv *priv;
503 	struct snd_soc_dai_link *dai_link;
504 	int num_codecs, ret, i;
505 
506 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
507 	if (!priv)
508 		return -ENOMEM;
509 
510 	snd_soc_card_set_drvdata(card, priv);
511 	card->dev = dev;
512 
513 	priv->gpio_mic_bias = devm_gpiod_get(dev, "mic-bias", GPIOD_OUT_HIGH);
514 	if (IS_ERR(priv->gpio_mic_bias)) {
515 		dev_err(dev, "Failed to get mic bias gpio\n");
516 		return PTR_ERR(priv->gpio_mic_bias);
517 	}
518 
519 	ret = snd_soc_of_parse_card_name(card, "model");
520 	if (ret < 0) {
521 		dev_err(dev, "Card name is not specified\n");
522 		return ret;
523 	}
524 
525 	ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
526 	if (ret < 0) {
527 		/* Backwards compatible way */
528 		ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing");
529 		if (ret < 0) {
530 			dev_err(dev, "Audio routing is not specified or invalid\n");
531 			return ret;
532 		}
533 	}
534 
535 	card->aux_dev[0].dlc.of_node = of_parse_phandle(dev->of_node,
536 							"audio-amplifier", 0);
537 	if (!card->aux_dev[0].dlc.of_node) {
538 		dev_err(dev, "audio-amplifier property invalid or missing\n");
539 		return -EINVAL;
540 	}
541 
542 	num_codecs = of_count_phandle_with_args(dev->of_node, "audio-codec",
543 						 NULL);
544 
545 	/* Skip the HDMI link if not specified in DT */
546 	if (num_codecs > 1) {
547 		card->num_links = ARRAY_SIZE(tm2_dai_links);
548 		cells_name = "#sound-dai-cells";
549 	} else {
550 		card->num_links = ARRAY_SIZE(tm2_dai_links) - 1;
551 	}
552 
553 	for (i = 0; i < num_codecs; i++) {
554 		struct of_phandle_args args;
555 
556 		ret = of_parse_phandle_with_args(dev->of_node, "i2s-controller",
557 						 cells_name, i, &args);
558 		if (ret) {
559 			dev_err(dev, "i2s-controller property parse error: %d\n", i);
560 			ret = -EINVAL;
561 			goto dai_node_put;
562 		}
563 		cpu_dai_node[i] = args.np;
564 
565 		codec_dai_node[i] = of_parse_phandle(dev->of_node,
566 						     "audio-codec", i);
567 		if (!codec_dai_node[i]) {
568 			dev_err(dev, "audio-codec property parse error\n");
569 			ret = -EINVAL;
570 			goto dai_node_put;
571 		}
572 	}
573 
574 	/* Initialize WM5110 - I2S and HDMI - I2S1 DAI links */
575 	for_each_card_prelinks(card, i, dai_link) {
576 		unsigned int dai_index = 0; /* WM5110 */
577 
578 		dai_link->cpus->name = NULL;
579 		dai_link->platforms->name = NULL;
580 
581 		if (num_codecs > 1 && i == card->num_links - 1)
582 			dai_index = 1; /* HDMI */
583 
584 		dai_link->codecs->of_node = codec_dai_node[dai_index];
585 		dai_link->cpus->of_node = cpu_dai_node[dai_index];
586 		dai_link->platforms->of_node = cpu_dai_node[dai_index];
587 	}
588 
589 	if (num_codecs > 1) {
590 		struct of_phandle_args args;
591 
592 		/* HDMI DAI link (I2S1) */
593 		i = card->num_links - 1;
594 
595 		ret = of_parse_phandle_with_fixed_args(dev->of_node,
596 						"audio-codec", 0, 1, &args);
597 		if (ret) {
598 			dev_err(dev, "audio-codec property parse error\n");
599 			goto dai_node_put;
600 		}
601 
602 		ret = snd_soc_get_dai_name(&args, &card->dai_link[i].codecs->dai_name);
603 		if (ret) {
604 			dev_err(dev, "Unable to get codec_dai_name\n");
605 			goto dai_node_put;
606 		}
607 	}
608 
609 	ret = devm_snd_soc_register_component(dev, &tm2_component,
610 				tm2_ext_dai, ARRAY_SIZE(tm2_ext_dai));
611 	if (ret < 0) {
612 		dev_err(dev, "Failed to register component: %d\n", ret);
613 		goto dai_node_put;
614 	}
615 
616 	ret = devm_snd_soc_register_card(dev, card);
617 	if (ret < 0) {
618 		dev_err_probe(dev, ret, "Failed to register card\n");
619 		goto dai_node_put;
620 	}
621 
622 dai_node_put:
623 	for (i = 0; i < num_codecs; i++) {
624 		of_node_put(codec_dai_node[i]);
625 		of_node_put(cpu_dai_node[i]);
626 	}
627 
628 	of_node_put(card->aux_dev[0].dlc.of_node);
629 
630 	return ret;
631 }
632 
tm2_pm_prepare(struct device * dev)633 static int tm2_pm_prepare(struct device *dev)
634 {
635 	struct snd_soc_card *card = dev_get_drvdata(dev);
636 
637 	return tm2_stop_sysclk(card);
638 }
639 
tm2_pm_complete(struct device * dev)640 static void tm2_pm_complete(struct device *dev)
641 {
642 	struct snd_soc_card *card = dev_get_drvdata(dev);
643 
644 	tm2_start_sysclk(card);
645 }
646 
647 static const struct dev_pm_ops tm2_pm_ops = {
648 	.prepare	= tm2_pm_prepare,
649 	.suspend	= snd_soc_suspend,
650 	.resume		= snd_soc_resume,
651 	.complete	= tm2_pm_complete,
652 	.freeze		= snd_soc_suspend,
653 	.thaw		= snd_soc_resume,
654 	.poweroff	= snd_soc_poweroff,
655 	.restore	= snd_soc_resume,
656 };
657 
658 static const struct of_device_id tm2_of_match[] = {
659 	{ .compatible = "samsung,tm2-audio" },
660 	{ },
661 };
662 MODULE_DEVICE_TABLE(of, tm2_of_match);
663 
664 static struct platform_driver tm2_driver = {
665 	.driver = {
666 		.name		= "tm2-audio",
667 		.pm		= &tm2_pm_ops,
668 		.of_match_table	= tm2_of_match,
669 	},
670 	.probe	= tm2_probe,
671 };
672 module_platform_driver(tm2_driver);
673 
674 MODULE_AUTHOR("Inha Song <ideal.song@samsung.com>");
675 MODULE_DESCRIPTION("ALSA SoC Exynos TM2 Audio Support");
676 MODULE_LICENSE("GPL v2");
677