blob: c091033d17ad154bf4a17de5bfa7685a9efd5794 [file] [log] [blame]
Sylwester Nawrocki686f47b2019-04-19 12:22:01 +02001// 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>
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +01007
8#include <linux/clk.h>
9#include <linux/gpio.h>
Fabian Frederick5d079fd2016-12-09 19:12:50 +010010#include <linux/gpio/consumer.h>
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010011#include <linux/module.h>
12#include <linux/of.h>
13#include <sound/pcm_params.h>
14#include <sound/soc.h>
15
16#include "i2s.h"
17#include "../codecs/wm5110.h"
18
19/*
20 * The source clock is XCLKOUT with its mux set to the external fixed rate
21 * oscillator (XXTI).
22 */
23#define MCLK_RATE 24000000U
24
25#define TM2_DAI_AIF1 0
26#define TM2_DAI_AIF2 1
27
28struct tm2_machine_priv {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000029 struct snd_soc_component *component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010030 unsigned int sysclk_rate;
31 struct gpio_desc *gpio_mic_bias;
32};
33
34static int tm2_start_sysclk(struct snd_soc_card *card)
35{
36 struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000037 struct snd_soc_component *component = priv->component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010038 int ret;
39
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000040 ret = snd_soc_component_set_pll(component, WM5110_FLL1_REFCLK,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010041 ARIZONA_FLL_SRC_MCLK1,
42 MCLK_RATE,
43 priv->sysclk_rate);
44 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000045 dev_err(component->dev, "Failed to set FLL1 source: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010046 return ret;
47 }
48
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000049 ret = snd_soc_component_set_pll(component, WM5110_FLL1,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010050 ARIZONA_FLL_SRC_MCLK1,
51 MCLK_RATE,
52 priv->sysclk_rate);
53 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000054 dev_err(component->dev, "Failed to start FLL1: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010055 return ret;
56 }
57
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000058 ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010059 ARIZONA_CLK_SRC_FLL1,
60 priv->sysclk_rate,
61 SND_SOC_CLOCK_IN);
62 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000063 dev_err(component->dev, "Failed to set SYSCLK source: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010064 return ret;
65 }
66
67 return 0;
68}
69
70static int tm2_stop_sysclk(struct snd_soc_card *card)
71{
72 struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000073 struct snd_soc_component *component = priv->component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010074 int ret;
75
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000076 ret = snd_soc_component_set_pll(component, WM5110_FLL1, 0, 0, 0);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010077 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000078 dev_err(component->dev, "Failed to stop FLL1: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010079 return ret;
80 }
81
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000082 ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_SYSCLK,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010083 ARIZONA_CLK_SRC_FLL1, 0, 0);
84 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000085 dev_err(component->dev, "Failed to stop SYSCLK: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010086 return ret;
87 }
88
89 return 0;
90}
91
92static int tm2_aif1_hw_params(struct snd_pcm_substream *substream,
93 struct snd_pcm_hw_params *params)
94{
95 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +000096 struct snd_soc_component *component = rtd->codec_dai->component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +010097 struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(rtd->card);
98
99 switch (params_rate(params)) {
100 case 4000:
101 case 8000:
102 case 12000:
103 case 16000:
104 case 24000:
105 case 32000:
106 case 48000:
107 case 96000:
108 case 192000:
109 /* Highest possible SYSCLK frequency: 147.456MHz */
110 priv->sysclk_rate = 147456000U;
111 break;
112 case 11025:
113 case 22050:
114 case 44100:
115 case 88200:
116 case 176400:
117 /* Highest possible SYSCLK frequency: 135.4752 MHz */
118 priv->sysclk_rate = 135475200U;
119 break;
120 default:
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000121 dev_err(component->dev, "Not supported sample rate: %d\n",
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100122 params_rate(params));
123 return -EINVAL;
124 }
125
126 return tm2_start_sysclk(rtd->card);
127}
128
129static struct snd_soc_ops tm2_aif1_ops = {
130 .hw_params = tm2_aif1_hw_params,
131};
132
133static int tm2_aif2_hw_params(struct snd_pcm_substream *substream,
134 struct snd_pcm_hw_params *params)
135{
136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000137 struct snd_soc_component *component = rtd->codec_dai->component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100138 unsigned int asyncclk_rate;
139 int ret;
140
141 switch (params_rate(params)) {
142 case 8000:
143 case 12000:
144 case 16000:
145 /* Highest possible ASYNCCLK frequency: 49.152MHz */
146 asyncclk_rate = 49152000U;
147 break;
148 case 11025:
149 /* Highest possible ASYNCCLK frequency: 45.1584 MHz */
150 asyncclk_rate = 45158400U;
151 break;
152 default:
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000153 dev_err(component->dev, "Not supported sample rate: %d\n",
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100154 params_rate(params));
155 return -EINVAL;
156 }
157
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000158 ret = snd_soc_component_set_pll(component, WM5110_FLL2_REFCLK,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100159 ARIZONA_FLL_SRC_MCLK1,
160 MCLK_RATE,
161 asyncclk_rate);
162 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000163 dev_err(component->dev, "Failed to set FLL2 source: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100164 return ret;
165 }
166
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000167 ret = snd_soc_component_set_pll(component, WM5110_FLL2,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100168 ARIZONA_FLL_SRC_MCLK1,
169 MCLK_RATE,
170 asyncclk_rate);
171 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000172 dev_err(component->dev, "Failed to start FLL2: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100173 return ret;
174 }
175
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000176 ret = snd_soc_component_set_sysclk(component, ARIZONA_CLK_ASYNCCLK,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100177 ARIZONA_CLK_SRC_FLL2,
178 asyncclk_rate,
179 SND_SOC_CLOCK_IN);
180 if (ret < 0) {
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000181 dev_err(component->dev, "Failed to set ASYNCCLK source: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100182 return ret;
183 }
184
185 return 0;
186}
187
188static int tm2_aif2_hw_free(struct snd_pcm_substream *substream)
189{
190 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000191 struct snd_soc_component *component = rtd->codec_dai->component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100192 int ret;
193
194 /* disable FLL2 */
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000195 ret = snd_soc_component_set_pll(component, WM5110_FLL2, ARIZONA_FLL_SRC_MCLK1,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100196 0, 0);
197 if (ret < 0)
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000198 dev_err(component->dev, "Failed to stop FLL2: %d\n", ret);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100199
200 return ret;
201}
202
203static struct snd_soc_ops tm2_aif2_ops = {
204 .hw_params = tm2_aif2_hw_params,
205 .hw_free = tm2_aif2_hw_free,
206};
207
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100208static int tm2_hdmi_hw_params(struct snd_pcm_substream *substream,
209 struct snd_pcm_hw_params *params)
210{
211 struct snd_soc_pcm_runtime *rtd = substream->private_data;
212 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
213 unsigned int bfs;
214 int bitwidth, ret;
215
216 bitwidth = snd_pcm_format_width(params_format(params));
217 if (bitwidth < 0) {
218 dev_err(rtd->card->dev, "Invalid bit-width: %d\n", bitwidth);
219 return bitwidth;
220 }
221
222 switch (bitwidth) {
223 case 48:
224 bfs = 64;
225 break;
226 case 16:
227 bfs = 32;
228 break;
229 default:
230 dev_err(rtd->card->dev, "Unsupported bit-width: %d\n", bitwidth);
231 return -EINVAL;
232 }
233
234 switch (params_rate(params)) {
235 case 48000:
236 case 96000:
237 case 192000:
238 break;
239 default:
240 dev_err(rtd->card->dev, "Unsupported sample rate: %d\n",
241 params_rate(params));
242 return -EINVAL;
243 }
244
245 ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK,
246 0, SAMSUNG_I2S_OPCLK_PCLK);
247 if (ret < 0)
248 return ret;
249
250 ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs);
251 if (ret < 0)
252 return ret;
253
254 return 0;
255}
256
257static struct snd_soc_ops tm2_hdmi_ops = {
258 .hw_params = tm2_hdmi_hw_params,
259};
260
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100261static int tm2_mic_bias(struct snd_soc_dapm_widget *w,
262 struct snd_kcontrol *kcontrol, int event)
263{
264 struct snd_soc_card *card = w->dapm->card;
265 struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
266
267 switch (event) {
268 case SND_SOC_DAPM_PRE_PMU:
269 gpiod_set_value_cansleep(priv->gpio_mic_bias, 1);
270 break;
271 case SND_SOC_DAPM_POST_PMD:
272 gpiod_set_value_cansleep(priv->gpio_mic_bias, 0);
273 break;
274 }
275
276 return 0;
277}
278
279static int tm2_set_bias_level(struct snd_soc_card *card,
280 struct snd_soc_dapm_context *dapm,
281 enum snd_soc_bias_level level)
282{
283 struct snd_soc_pcm_runtime *rtd;
284
285 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
286
287 if (dapm->dev != rtd->codec_dai->dev)
288 return 0;
289
290 switch (level) {
291 case SND_SOC_BIAS_STANDBY:
292 if (card->dapm.bias_level == SND_SOC_BIAS_OFF)
293 tm2_start_sysclk(card);
294 break;
295 case SND_SOC_BIAS_OFF:
296 tm2_stop_sysclk(card);
297 break;
298 default:
299 break;
300 }
301
302 return 0;
303}
304
305static struct snd_soc_aux_dev tm2_speaker_amp_dev;
306
307static int tm2_late_probe(struct snd_soc_card *card)
308{
309 struct tm2_machine_priv *priv = snd_soc_card_get_drvdata(card);
310 struct snd_soc_dai_link_component dlc = { 0 };
311 unsigned int ch_map[] = { 0, 1 };
312 struct snd_soc_dai *amp_pdm_dai;
313 struct snd_soc_pcm_runtime *rtd;
314 struct snd_soc_dai *aif1_dai;
315 struct snd_soc_dai *aif2_dai;
316 int ret;
317
318 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[TM2_DAI_AIF1].name);
319 aif1_dai = rtd->codec_dai;
Kuninori Morimoto0fe1daa2018-02-13 02:03:12 +0000320 priv->component = rtd->codec_dai->component;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100321
322 ret = snd_soc_dai_set_sysclk(aif1_dai, ARIZONA_CLK_SYSCLK, 0, 0);
323 if (ret < 0) {
324 dev_err(aif1_dai->dev, "Failed to set SYSCLK: %d\n", ret);
325 return ret;
326 }
327
328 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[TM2_DAI_AIF2].name);
329 aif2_dai = rtd->codec_dai;
330
331 ret = snd_soc_dai_set_sysclk(aif2_dai, ARIZONA_CLK_ASYNCCLK, 0, 0);
332 if (ret < 0) {
333 dev_err(aif2_dai->dev, "Failed to set ASYNCCLK: %d\n", ret);
334 return ret;
335 }
336
337 dlc.of_node = tm2_speaker_amp_dev.codec_of_node;
338 amp_pdm_dai = snd_soc_find_dai(&dlc);
339 if (!amp_pdm_dai)
340 return -ENODEV;
341
342 /* Set the MAX98504 V/I sense PDM Tx DAI channel mapping */
343 ret = snd_soc_dai_set_channel_map(amp_pdm_dai, ARRAY_SIZE(ch_map),
344 ch_map, 0, NULL);
345 if (ret < 0)
346 return ret;
347
348 ret = snd_soc_dai_set_tdm_slot(amp_pdm_dai, 0x3, 0x0, 2, 16);
349 if (ret < 0)
350 return ret;
351
352 return 0;
353}
354
355static const struct snd_kcontrol_new tm2_controls[] = {
356 SOC_DAPM_PIN_SWITCH("HP"),
357 SOC_DAPM_PIN_SWITCH("SPK"),
358 SOC_DAPM_PIN_SWITCH("RCV"),
359 SOC_DAPM_PIN_SWITCH("VPS"),
360 SOC_DAPM_PIN_SWITCH("HDMI"),
361
362 SOC_DAPM_PIN_SWITCH("Main Mic"),
363 SOC_DAPM_PIN_SWITCH("Sub Mic"),
364 SOC_DAPM_PIN_SWITCH("Third Mic"),
365
366 SOC_DAPM_PIN_SWITCH("Headset Mic"),
367};
368
Colin Ian Kingfa991252017-08-13 16:37:15 +0100369static const struct snd_soc_dapm_widget tm2_dapm_widgets[] = {
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100370 SND_SOC_DAPM_HP("HP", NULL),
371 SND_SOC_DAPM_SPK("SPK", NULL),
372 SND_SOC_DAPM_SPK("RCV", NULL),
373 SND_SOC_DAPM_LINE("VPS", NULL),
374 SND_SOC_DAPM_LINE("HDMI", NULL),
375
376 SND_SOC_DAPM_MIC("Main Mic", tm2_mic_bias),
377 SND_SOC_DAPM_MIC("Sub Mic", NULL),
378 SND_SOC_DAPM_MIC("Third Mic", NULL),
379
380 SND_SOC_DAPM_MIC("Headset Mic", NULL),
381};
382
383static const struct snd_soc_component_driver tm2_component = {
384 .name = "tm2-audio",
385};
386
387static struct snd_soc_dai_driver tm2_ext_dai[] = {
388 {
389 .name = "Voice call",
390 .playback = {
391 .channels_min = 1,
392 .channels_max = 4,
393 .rate_min = 8000,
394 .rate_max = 48000,
395 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
396 SNDRV_PCM_RATE_48000),
397 .formats = SNDRV_PCM_FMTBIT_S16_LE,
398 },
399 .capture = {
400 .channels_min = 1,
401 .channels_max = 4,
402 .rate_min = 8000,
403 .rate_max = 48000,
404 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
405 SNDRV_PCM_RATE_48000),
406 .formats = SNDRV_PCM_FMTBIT_S16_LE,
407 },
408 },
409 {
410 .name = "Bluetooth",
411 .playback = {
412 .channels_min = 1,
413 .channels_max = 4,
414 .rate_min = 8000,
415 .rate_max = 16000,
416 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
417 .formats = SNDRV_PCM_FMTBIT_S16_LE,
418 },
419 .capture = {
420 .channels_min = 1,
421 .channels_max = 2,
422 .rate_min = 8000,
423 .rate_max = 16000,
424 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
425 .formats = SNDRV_PCM_FMTBIT_S16_LE,
426 },
427 },
428};
429
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900430SND_SOC_DAILINK_DEFS(aif1,
431 DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
432 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm5110-aif1")),
433 DAILINK_COMP_ARRAY(COMP_EMPTY()));
434
435SND_SOC_DAILINK_DEFS(voice,
436 DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
437 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm5110-aif2")),
438 DAILINK_COMP_ARRAY(COMP_EMPTY()));
439
440SND_SOC_DAILINK_DEFS(bt,
441 DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
442 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm5110-aif3")),
443 DAILINK_COMP_ARRAY(COMP_EMPTY()));
444
445SND_SOC_DAILINK_DEFS(hdmi,
446 DAILINK_COMP_ARRAY(COMP_EMPTY()),
447 DAILINK_COMP_ARRAY(COMP_EMPTY()),
448 DAILINK_COMP_ARRAY(COMP_EMPTY()));
449
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100450static struct snd_soc_dai_link tm2_dai_links[] = {
451 {
452 .name = "WM5110 AIF1",
453 .stream_name = "HiFi Primary",
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100454 .ops = &tm2_aif1_ops,
455 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
456 SND_SOC_DAIFMT_CBM_CFM,
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900457 SND_SOC_DAILINK_REG(aif1),
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100458 }, {
459 .name = "WM5110 Voice",
460 .stream_name = "Voice call",
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100461 .ops = &tm2_aif2_ops,
462 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
463 SND_SOC_DAIFMT_CBM_CFM,
464 .ignore_suspend = 1,
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900465 SND_SOC_DAILINK_REG(voice),
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100466 }, {
467 .name = "WM5110 BT",
468 .stream_name = "Bluetooth",
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100469 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
470 SND_SOC_DAIFMT_CBM_CFM,
471 .ignore_suspend = 1,
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900472 SND_SOC_DAILINK_REG(bt),
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100473 }, {
474 .name = "HDMI",
475 .stream_name = "i2s1",
476 .ops = &tm2_hdmi_ops,
477 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
478 SND_SOC_DAIFMT_CBS_CFS,
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900479 SND_SOC_DAILINK_REG(hdmi),
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100480 }
481};
482
483static struct snd_soc_card tm2_card = {
484 .owner = THIS_MODULE,
485
486 .dai_link = tm2_dai_links,
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100487 .controls = tm2_controls,
488 .num_controls = ARRAY_SIZE(tm2_controls),
489 .dapm_widgets = tm2_dapm_widgets,
490 .num_dapm_widgets = ARRAY_SIZE(tm2_dapm_widgets),
491 .aux_dev = &tm2_speaker_amp_dev,
492 .num_aux_devs = 1,
493
494 .late_probe = tm2_late_probe,
495 .set_bias_level = tm2_set_bias_level,
496};
497
498static int tm2_probe(struct platform_device *pdev)
499{
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100500 struct device_node *cpu_dai_node[2] = {};
501 struct device_node *codec_dai_node[2] = {};
502 const char *cells_name = NULL;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100503 struct device *dev = &pdev->dev;
504 struct snd_soc_card *card = &tm2_card;
505 struct tm2_machine_priv *priv;
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100506 struct of_phandle_args args;
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +0000507 struct snd_soc_dai_link *dai_link;
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100508 int num_codecs, ret, i;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100509
510 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
511 if (!priv)
512 return -ENOMEM;
513
514 snd_soc_card_set_drvdata(card, priv);
515 card->dev = dev;
516
Jaechul Lee975b6a92017-09-06 10:04:15 +0900517 priv->gpio_mic_bias = devm_gpiod_get(dev, "mic-bias", GPIOD_OUT_HIGH);
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100518 if (IS_ERR(priv->gpio_mic_bias)) {
519 dev_err(dev, "Failed to get mic bias gpio\n");
520 return PTR_ERR(priv->gpio_mic_bias);
521 }
522
523 ret = snd_soc_of_parse_card_name(card, "model");
524 if (ret < 0) {
525 dev_err(dev, "Card name is not specified\n");
526 return ret;
527 }
528
529 ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing");
530 if (ret < 0) {
531 dev_err(dev, "Audio routing is not specified or invalid\n");
532 return ret;
533 }
534
535 card->aux_dev[0].codec_of_node = of_parse_phandle(dev->of_node,
536 "audio-amplifier", 0);
537 if (!card->aux_dev[0].codec_of_node) {
538 dev_err(dev, "audio-amplifier property invalid or missing\n");
539 return -EINVAL;
540 }
541
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100542 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;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100551 }
552
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100553 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 (!args.np) {
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 }
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100572 }
573
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100574 /* Initialize WM5110 - I2S and HDMI - I2S1 DAI links */
Kuninori Morimoto7fe072b2018-09-18 01:28:49 +0000575 for_each_card_prelinks(card, i, dai_link) {
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100576 unsigned int dai_index = 0; /* WM5110 */
577
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900578 dai_link->cpus->name = NULL;
579 dai_link->platforms->name = NULL;
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100580
581 if (num_codecs > 1 && i == card->num_links - 1)
582 dai_index = 1; /* HDMI */
583
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900584 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];
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100587 }
588
589 if (num_codecs > 1) {
590 /* HDMI DAI link (I2S1) */
591 i = card->num_links - 1;
592
593 ret = of_parse_phandle_with_fixed_args(dev->of_node,
594 "audio-codec", 0, 1, &args);
595 if (ret) {
596 dev_err(dev, "audio-codec property parse error\n");
597 goto dai_node_put;
598 }
599
Kuninori Morimotof1d26f22019-06-06 13:09:44 +0900600 ret = snd_soc_get_dai_name(&args, &card->dai_link[i].codecs->dai_name);
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100601 if (ret) {
602 dev_err(dev, "Unable to get codec_dai_name\n");
603 goto dai_node_put;
604 }
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100605 }
606
607 ret = devm_snd_soc_register_component(dev, &tm2_component,
608 tm2_ext_dai, ARRAY_SIZE(tm2_ext_dai));
609 if (ret < 0) {
610 dev_err(dev, "Failed to register component: %d\n", ret);
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100611 goto dai_node_put;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100612 }
613
614 ret = devm_snd_soc_register_card(dev, card);
615 if (ret < 0) {
616 dev_err(dev, "Failed to register card: %d\n", ret);
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100617 goto dai_node_put;
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100618 }
619
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100620dai_node_put:
621 for (i = 0; i < num_codecs; i++) {
622 of_node_put(codec_dai_node[i]);
623 of_node_put(cpu_dai_node[i]);
624 }
625
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100626 of_node_put(card->aux_dev[0].codec_of_node);
Sylwester Nawrocki8d1513c2018-02-12 17:15:37 +0100627
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100628 return ret;
629}
630
631static int tm2_pm_prepare(struct device *dev)
632{
633 struct snd_soc_card *card = dev_get_drvdata(dev);
634
635 return tm2_stop_sysclk(card);
636}
637
638static void tm2_pm_complete(struct device *dev)
639{
640 struct snd_soc_card *card = dev_get_drvdata(dev);
641
642 tm2_start_sysclk(card);
643}
644
Colin Ian Kingfa991252017-08-13 16:37:15 +0100645static const struct dev_pm_ops tm2_pm_ops = {
Sylwester Nawrocki1bfbc262016-11-02 17:05:45 +0100646 .prepare = tm2_pm_prepare,
647 .suspend = snd_soc_suspend,
648 .resume = snd_soc_resume,
649 .complete = tm2_pm_complete,
650 .freeze = snd_soc_suspend,
651 .thaw = snd_soc_resume,
652 .poweroff = snd_soc_poweroff,
653 .restore = snd_soc_resume,
654};
655
656static const struct of_device_id tm2_of_match[] = {
657 { .compatible = "samsung,tm2-audio" },
658 { },
659};
660MODULE_DEVICE_TABLE(of, tm2_of_match);
661
662static struct platform_driver tm2_driver = {
663 .driver = {
664 .name = "tm2-audio",
665 .pm = &tm2_pm_ops,
666 .of_match_table = tm2_of_match,
667 },
668 .probe = tm2_probe,
669};
670module_platform_driver(tm2_driver);
671
672MODULE_AUTHOR("Inha Song <ideal.song@samsung.com>");
673MODULE_DESCRIPTION("ALSA SoC Exynos TM2 Audio Support");
674MODULE_LICENSE("GPL v2");