forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/sound/soc/intel/boards/cht_bsw_rt5672.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
34 * Cherrytrail and Braswell, with RT5672 codec.
....@@ -5,17 +6,10 @@
56 * Copyright (C) 2014 Intel Corp
67 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
78 * Mengdong Lin <mengdong.lin@intel.com>
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License as published by
11
- * the Free Software Foundation; version 2 of the License.
12
- *
13
- * This program is distributed in the hope that it will be useful, but
14
- * WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
- * General Public License for more details.
179 */
1810
11
+#include <linux/gpio/consumer.h>
12
+#include <linux/input.h>
1913 #include <linux/module.h>
2014 #include <linux/platform_device.h>
2115 #include <linux/slab.h>
....@@ -149,8 +143,8 @@
149143 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
150144 struct snd_pcm_hw_params *params)
151145 {
152
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
153
- struct snd_soc_dai *codec_dai = rtd->codec_dai;
146
+ struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
147
+ struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
154148 int ret;
155149
156150 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
....@@ -182,7 +176,7 @@
182176 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
183177 {
184178 int ret;
185
- struct snd_soc_dai *codec_dai = runtime->codec_dai;
179
+ struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0);
186180 struct snd_soc_component *component = codec_dai->component;
187181 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
188182
....@@ -211,6 +205,10 @@
211205 ARRAY_SIZE(cht_bsw_headset_pins));
212206 if (ret)
213207 return ret;
208
+
209
+ snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
210
+ snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
211
+ snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
214212
215213 rt5670_set_jack_detect(component, &ctx->headset);
216214 if (ctx->mclk) {
....@@ -255,21 +253,24 @@
255253 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
256254
257255 /*
258
- * Default mode for SSP configuration is TDM 4 slot
256
+ * The default mode for the cpu-dai is TDM 4 slot. The default mode
257
+ * for the codec-dai is I2S. So we need to either set the cpu-dai to
258
+ * I2S mode to match the codec-dai, or set the codec-dai to TDM 4 slot
259
+ * (or program both to yet another mode).
260
+ * One board, the Lenovo Miix 2 10, uses not 1 but 2 codecs connected
261
+ * to SSP2. The second piggy-backed, output-only codec is inside the
262
+ * keyboard-dock (which has extra speakers). Unlike the main rt5672
263
+ * codec, we cannot configure this codec, it is hard coded to use
264
+ * 2 channel 24 bit I2S. For this to work we must use I2S mode on this
265
+ * board. Since we only support 2 channels anyways, there is no need
266
+ * for TDM on any cht-bsw-rt5672 designs. So we use I2S 2ch everywhere.
259267 */
260
- ret = snd_soc_dai_set_fmt(rtd->codec_dai,
261
- SND_SOC_DAIFMT_DSP_B |
262
- SND_SOC_DAIFMT_IB_NF |
268
+ ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
269
+ SND_SOC_DAIFMT_I2S |
270
+ SND_SOC_DAIFMT_NB_NF |
263271 SND_SOC_DAIFMT_CBS_CFS);
264272 if (ret < 0) {
265
- dev_err(rtd->dev, "can't set format to TDM %d\n", ret);
266
- return ret;
267
- }
268
-
269
- /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
270
- ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0xF, 0xF, 4, 24);
271
- if (ret < 0) {
272
- dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
273
+ dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
273274 return ret;
274275 }
275276
....@@ -290,32 +291,44 @@
290291 .hw_params = cht_aif1_hw_params,
291292 };
292293
294
+SND_SOC_DAILINK_DEF(dummy,
295
+ DAILINK_COMP_ARRAY(COMP_DUMMY()));
296
+
297
+SND_SOC_DAILINK_DEF(media,
298
+ DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
299
+
300
+SND_SOC_DAILINK_DEF(deepbuffer,
301
+ DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
302
+
303
+SND_SOC_DAILINK_DEF(ssp2_port,
304
+ DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
305
+SND_SOC_DAILINK_DEF(ssp2_codec,
306
+ DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5670:00",
307
+ "rt5670-aif1")));
308
+
309
+SND_SOC_DAILINK_DEF(platform,
310
+ DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
311
+
293312 static struct snd_soc_dai_link cht_dailink[] = {
294313 /* Front End DAI links */
295314 [MERR_DPCM_AUDIO] = {
296315 .name = "Audio Port",
297316 .stream_name = "Audio",
298
- .cpu_dai_name = "media-cpu-dai",
299
- .codec_dai_name = "snd-soc-dummy-dai",
300
- .codec_name = "snd-soc-dummy",
301
- .platform_name = "sst-mfld-platform",
302317 .nonatomic = true,
303318 .dynamic = 1,
304319 .dpcm_playback = 1,
305320 .dpcm_capture = 1,
306321 .ops = &cht_aif1_ops,
322
+ SND_SOC_DAILINK_REG(media, dummy, platform),
307323 },
308324 [MERR_DPCM_DEEP_BUFFER] = {
309325 .name = "Deep-Buffer Audio Port",
310326 .stream_name = "Deep-Buffer Audio",
311
- .cpu_dai_name = "deepbuffer-cpu-dai",
312
- .codec_dai_name = "snd-soc-dummy-dai",
313
- .codec_name = "snd-soc-dummy",
314
- .platform_name = "sst-mfld-platform",
315327 .nonatomic = true,
316328 .dynamic = 1,
317329 .dpcm_playback = 1,
318330 .ops = &cht_aif1_ops,
331
+ SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
319332 },
320333
321334 /* Back End DAI links */
....@@ -323,17 +336,14 @@
323336 /* SSP2 - Codec */
324337 .name = "SSP2-Codec",
325338 .id = 0,
326
- .cpu_dai_name = "ssp2-port",
327
- .platform_name = "sst-mfld-platform",
328339 .no_pcm = 1,
329340 .nonatomic = true,
330
- .codec_dai_name = "rt5670-aif1",
331
- .codec_name = "i2c-10EC5670:00",
332341 .init = cht_codec_init,
333342 .be_hw_params_fixup = cht_codec_fixup,
334343 .dpcm_playback = 1,
335344 .dpcm_capture = 1,
336345 .ops = &cht_be_ssp2_ops,
346
+ SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
337347 },
338348 };
339349
....@@ -342,7 +352,7 @@
342352 struct snd_soc_component *component;
343353 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
344354
345
- list_for_each_entry(component, &card->component_dev_list, card_list) {
355
+ for_each_card_components(card, component) {
346356 if (!strncmp(component->name,
347357 ctx->codec_name, sizeof(ctx->codec_name))) {
348358
....@@ -359,7 +369,7 @@
359369 struct snd_soc_component *component;
360370 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
361371
362
- list_for_each_entry(component, &card->component_dev_list, card_list) {
372
+ for_each_card_components(card, component) {
363373 if (!strncmp(component->name,
364374 ctx->codec_name, sizeof(ctx->codec_name))) {
365375
....@@ -372,9 +382,19 @@
372382 return 0;
373383 }
374384
385
+#if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
386
+/* use space before codec name to simplify card ID, and simplify driver name */
387
+#define CARD_NAME "bytcht rt5672" /* card name will be 'sof-bytcht rt5672' */
388
+#define DRIVER_NAME "SOF"
389
+#else
390
+#define CARD_NAME "cht-bsw-rt5672"
391
+#define DRIVER_NAME NULL /* card name will be used for driver name */
392
+#endif
393
+
375394 /* SoC card */
376395 static struct snd_soc_card snd_soc_card_cht = {
377
- .name = "cht-bsw-rt5672",
396
+ .name = CARD_NAME,
397
+ .driver_name = DRIVER_NAME,
378398 .owner = THIS_MODULE,
379399 .dai_link = cht_dailink,
380400 .num_links = ARRAY_SIZE(cht_dailink),
....@@ -395,31 +415,39 @@
395415 int ret_val = 0;
396416 struct cht_mc_private *drv;
397417 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
398
- const char *i2c_name;
418
+ const char *platform_name;
419
+ struct acpi_device *adev;
399420 int i;
400421
401
- drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
422
+ drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
402423 if (!drv)
403424 return -ENOMEM;
404425
405426 strcpy(drv->codec_name, RT5672_I2C_DEFAULT);
406427
407428 /* fixup codec name based on HID */
408
- if (mach) {
409
- i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
410
- if (i2c_name) {
411
- snprintf(drv->codec_name, sizeof(drv->codec_name),
412
- "i2c-%s", i2c_name);
413
- for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) {
414
- if (!strcmp(cht_dailink[i].codec_name,
415
- RT5672_I2C_DEFAULT)) {
416
- cht_dailink[i].codec_name =
417
- drv->codec_name;
418
- break;
419
- }
429
+ adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
430
+ if (adev) {
431
+ snprintf(drv->codec_name, sizeof(drv->codec_name),
432
+ "i2c-%s", acpi_dev_name(adev));
433
+ put_device(&adev->dev);
434
+ for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) {
435
+ if (!strcmp(cht_dailink[i].codecs->name,
436
+ RT5672_I2C_DEFAULT)) {
437
+ cht_dailink[i].codecs->name = drv->codec_name;
438
+ break;
420439 }
421440 }
422441 }
442
+
443
+ /* override plaform name, if required */
444
+ snd_soc_card_cht.dev = &pdev->dev;
445
+ platform_name = mach->mach_params.platform;
446
+
447
+ ret_val = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cht,
448
+ platform_name);
449
+ if (ret_val)
450
+ return ret_val;
423451
424452 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
425453 if (IS_ERR(drv->mclk)) {
....@@ -431,7 +459,6 @@
431459 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
432460
433461 /* register the soc card */
434
- snd_soc_card_cht.dev = &pdev->dev;
435462 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
436463 if (ret_val) {
437464 dev_err(&pdev->dev,
....@@ -445,6 +472,9 @@
445472 static struct platform_driver snd_cht_mc_driver = {
446473 .driver = {
447474 .name = "cht-bsw-rt5672",
475
+#if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
476
+ .pm = &snd_soc_pm_ops,
477
+#endif
448478 },
449479 .probe = snd_cht_mc_probe,
450480 };