0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/acpi.h>
0018 #include <sound/core.h>
0019 #include <sound/jack.h>
0020 #include <sound/pcm.h>
0021 #include <sound/pcm_params.h>
0022 #include <sound/soc.h>
0023
0024 #include "../../codecs/hdac_hdmi.h"
0025 #include "../../codecs/rt5660.h"
0026
0027 #define KBL_RT5660_CODEC_DAI "rt5660-aif1"
0028 #define DUAL_CHANNEL 2
0029
0030 static struct snd_soc_card *kabylake_audio_card;
0031 static struct snd_soc_jack skylake_hdmi[3];
0032 static struct snd_soc_jack lineout_jack;
0033 static struct snd_soc_jack mic_jack;
0034
0035 struct kbl_hdmi_pcm {
0036 struct list_head head;
0037 struct snd_soc_dai *codec_dai;
0038 int device;
0039 };
0040
0041 struct kbl_codec_private {
0042 struct gpio_desc *gpio_lo_mute;
0043 struct list_head hdmi_pcm_list;
0044 };
0045
0046 enum {
0047 KBL_DPCM_AUDIO_PB = 0,
0048 KBL_DPCM_AUDIO_CP,
0049 KBL_DPCM_AUDIO_HDMI1_PB,
0050 KBL_DPCM_AUDIO_HDMI2_PB,
0051 KBL_DPCM_AUDIO_HDMI3_PB,
0052 };
0053
0054 #define GPIO_LINEOUT_MUTE_INDEX 0
0055 #define GPIO_LINEOUT_DET_INDEX 3
0056 #define GPIO_LINEIN_DET_INDEX 4
0057
0058 static const struct acpi_gpio_params lineout_mute_gpio = { GPIO_LINEOUT_MUTE_INDEX, 0, true };
0059 static const struct acpi_gpio_params lineout_det_gpio = { GPIO_LINEOUT_DET_INDEX, 0, false };
0060 static const struct acpi_gpio_params mic_det_gpio = { GPIO_LINEIN_DET_INDEX, 0, false };
0061
0062
0063 static const struct acpi_gpio_mapping acpi_rt5660_gpios[] = {
0064 { "lineout-mute-gpios", &lineout_mute_gpio, 1 },
0065 { "lineout-det-gpios", &lineout_det_gpio, 1 },
0066 { "mic-det-gpios", &mic_det_gpio, 1 },
0067 { NULL },
0068 };
0069
0070 static struct snd_soc_jack_pin lineout_jack_pin = {
0071 .pin = "Line Out",
0072 .mask = SND_JACK_LINEOUT,
0073 };
0074
0075 static struct snd_soc_jack_pin mic_jack_pin = {
0076 .pin = "Line In",
0077 .mask = SND_JACK_MICROPHONE,
0078 };
0079
0080 static struct snd_soc_jack_gpio lineout_jack_gpio = {
0081 .name = "lineout-det",
0082 .report = SND_JACK_LINEOUT,
0083 .debounce_time = 200,
0084 };
0085
0086 static struct snd_soc_jack_gpio mic_jack_gpio = {
0087 .name = "mic-det",
0088 .report = SND_JACK_MICROPHONE,
0089 .debounce_time = 200,
0090 };
0091
0092 static int kabylake_5660_event_lineout(struct snd_soc_dapm_widget *w,
0093 struct snd_kcontrol *k, int event)
0094 {
0095 struct snd_soc_dapm_context *dapm = w->dapm;
0096 struct kbl_codec_private *priv = snd_soc_card_get_drvdata(dapm->card);
0097
0098 gpiod_set_value_cansleep(priv->gpio_lo_mute,
0099 !(SND_SOC_DAPM_EVENT_ON(event)));
0100
0101 return 0;
0102 }
0103
0104 static const struct snd_kcontrol_new kabylake_rt5660_controls[] = {
0105 SOC_DAPM_PIN_SWITCH("Line In"),
0106 SOC_DAPM_PIN_SWITCH("Line Out"),
0107 };
0108
0109 static const struct snd_soc_dapm_widget kabylake_rt5660_widgets[] = {
0110 SND_SOC_DAPM_MIC("Line In", NULL),
0111 SND_SOC_DAPM_LINE("Line Out", kabylake_5660_event_lineout),
0112 };
0113
0114 static const struct snd_soc_dapm_route kabylake_rt5660_map[] = {
0115
0116 {"IN1P", NULL, "Line In"},
0117 {"IN2P", NULL, "Line In"},
0118 {"Line Out", NULL, "LOUTR"},
0119 {"Line Out", NULL, "LOUTL"},
0120
0121
0122 { "AIF1 Playback", NULL, "ssp0 Tx"},
0123 { "ssp0 Tx", NULL, "codec0_out"},
0124
0125 { "codec0_in", NULL, "ssp0 Rx" },
0126 { "ssp0 Rx", NULL, "AIF1 Capture" },
0127
0128 { "hifi1", NULL, "iDisp1 Tx"},
0129 { "iDisp1 Tx", NULL, "iDisp1_out"},
0130 { "hifi2", NULL, "iDisp2 Tx"},
0131 { "iDisp2 Tx", NULL, "iDisp2_out"},
0132 { "hifi3", NULL, "iDisp3 Tx"},
0133 { "iDisp3 Tx", NULL, "iDisp3_out"},
0134 };
0135
0136 static int kabylake_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
0137 struct snd_pcm_hw_params *params)
0138 {
0139 struct snd_interval *rate = hw_param_interval(params,
0140 SNDRV_PCM_HW_PARAM_RATE);
0141 struct snd_interval *chan = hw_param_interval(params,
0142 SNDRV_PCM_HW_PARAM_CHANNELS);
0143 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0144
0145
0146 rate->min = rate->max = 48000;
0147 chan->min = chan->max = DUAL_CHANNEL;
0148
0149
0150 snd_mask_none(fmt);
0151 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
0152
0153 return 0;
0154 }
0155
0156 static int kabylake_rt5660_codec_init(struct snd_soc_pcm_runtime *rtd)
0157 {
0158 int ret;
0159 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
0160 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
0161 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
0162
0163 ret = devm_acpi_dev_add_driver_gpios(component->dev, acpi_rt5660_gpios);
0164 if (ret)
0165 dev_warn(component->dev, "Failed to add driver gpios\n");
0166
0167
0168 ctx->gpio_lo_mute = gpiod_get(component->dev, "lineout-mute",
0169 GPIOD_OUT_HIGH);
0170 if (IS_ERR(ctx->gpio_lo_mute)) {
0171 dev_err(component->dev, "Can't find GPIO_MUTE# gpio\n");
0172 return PTR_ERR(ctx->gpio_lo_mute);
0173 }
0174
0175
0176 ret = snd_soc_card_jack_new_pins(rtd->card, "Lineout Jack",
0177 SND_JACK_LINEOUT, &lineout_jack,
0178 &lineout_jack_pin, 1);
0179 if (ret)
0180 dev_warn(component->dev, "Can't create Lineout jack\n");
0181 else {
0182 lineout_jack_gpio.gpiod_dev = component->dev;
0183 ret = snd_soc_jack_add_gpios(&lineout_jack, 1,
0184 &lineout_jack_gpio);
0185 if (ret)
0186 dev_warn(component->dev, "Can't add Lineout jack gpio\n");
0187 }
0188
0189
0190 ret = snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
0191 SND_JACK_MICROPHONE, &mic_jack,
0192 &mic_jack_pin, 1);
0193 if (ret)
0194 dev_warn(component->dev, "Can't create mic jack\n");
0195 else {
0196 mic_jack_gpio.gpiod_dev = component->dev;
0197 ret = snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio);
0198 if (ret)
0199 dev_warn(component->dev, "Can't add mic jack gpio\n");
0200 }
0201
0202
0203 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
0204 snd_soc_dapm_force_enable_pin(dapm, "BST1");
0205 snd_soc_dapm_force_enable_pin(dapm, "BST2");
0206
0207 return 0;
0208 }
0209
0210 static void kabylake_rt5660_codec_exit(struct snd_soc_pcm_runtime *rtd)
0211 {
0212 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
0213
0214
0215
0216
0217
0218 if (!IS_ERR_OR_NULL(ctx->gpio_lo_mute))
0219 gpiod_put(ctx->gpio_lo_mute);
0220 }
0221
0222 static int kabylake_hdmi_init(struct snd_soc_pcm_runtime *rtd, int device)
0223 {
0224 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
0225 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
0226 struct kbl_hdmi_pcm *pcm;
0227
0228 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
0229 if (!pcm)
0230 return -ENOMEM;
0231
0232 pcm->device = device;
0233 pcm->codec_dai = dai;
0234
0235 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
0236
0237 return 0;
0238 }
0239
0240 static int kabylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
0241 {
0242 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI1_PB);
0243 }
0244
0245 static int kabylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
0246 {
0247 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI2_PB);
0248 }
0249
0250 static int kabylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd)
0251 {
0252 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI3_PB);
0253 }
0254
0255 static int kabylake_rt5660_hw_params(struct snd_pcm_substream *substream,
0256 struct snd_pcm_hw_params *params)
0257 {
0258 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0259 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0260 int ret;
0261
0262 ret = snd_soc_dai_set_sysclk(codec_dai,
0263 RT5660_SCLK_S_PLL1, params_rate(params) * 512,
0264 SND_SOC_CLOCK_IN);
0265 if (ret < 0) {
0266 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
0267 return ret;
0268 }
0269
0270 ret = snd_soc_dai_set_pll(codec_dai, 0,
0271 RT5660_PLL1_S_BCLK,
0272 params_rate(params) * 50,
0273 params_rate(params) * 512);
0274 if (ret < 0)
0275 dev_err(codec_dai->dev, "can't set codec pll: %d\n", ret);
0276
0277 return ret;
0278 }
0279
0280 static struct snd_soc_ops kabylake_rt5660_ops = {
0281 .hw_params = kabylake_rt5660_hw_params,
0282 };
0283
0284 static const unsigned int rates[] = {
0285 48000,
0286 };
0287
0288 static const struct snd_pcm_hw_constraint_list constraints_rates = {
0289 .count = ARRAY_SIZE(rates),
0290 .list = rates,
0291 .mask = 0,
0292 };
0293
0294 static const unsigned int channels[] = {
0295 DUAL_CHANNEL,
0296 };
0297
0298 static const struct snd_pcm_hw_constraint_list constraints_channels = {
0299 .count = ARRAY_SIZE(channels),
0300 .list = channels,
0301 .mask = 0,
0302 };
0303
0304 static int kbl_fe_startup(struct snd_pcm_substream *substream)
0305 {
0306 struct snd_pcm_runtime *runtime = substream->runtime;
0307
0308
0309
0310
0311
0312
0313
0314
0315 runtime->hw.channels_max = DUAL_CHANNEL;
0316 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
0317 &constraints_channels);
0318
0319 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
0320 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
0321
0322 snd_pcm_hw_constraint_list(runtime, 0,
0323 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
0324
0325 return 0;
0326 }
0327
0328 static const struct snd_soc_ops kabylake_rt5660_fe_ops = {
0329 .startup = kbl_fe_startup,
0330 };
0331
0332 SND_SOC_DAILINK_DEF(dummy,
0333 DAILINK_COMP_ARRAY(COMP_DUMMY()));
0334
0335 SND_SOC_DAILINK_DEF(system,
0336 DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
0337
0338 SND_SOC_DAILINK_DEF(hdmi1,
0339 DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin")));
0340
0341 SND_SOC_DAILINK_DEF(hdmi2,
0342 DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin")));
0343
0344 SND_SOC_DAILINK_DEF(hdmi3,
0345 DAILINK_COMP_ARRAY(COMP_CPU("HDMI3 Pin")));
0346
0347 SND_SOC_DAILINK_DEF(ssp0_pin,
0348 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
0349 SND_SOC_DAILINK_DEF(ssp0_codec,
0350 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC3277:00", KBL_RT5660_CODEC_DAI)));
0351
0352 SND_SOC_DAILINK_DEF(idisp1_pin,
0353 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
0354 SND_SOC_DAILINK_DEF(idisp1_codec,
0355 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
0356
0357 SND_SOC_DAILINK_DEF(idisp2_pin,
0358 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
0359 SND_SOC_DAILINK_DEF(idisp2_codec,
0360 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
0361
0362 SND_SOC_DAILINK_DEF(idisp3_pin,
0363 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
0364 SND_SOC_DAILINK_DEF(idisp3_codec,
0365 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
0366
0367 SND_SOC_DAILINK_DEF(platform,
0368 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
0369
0370
0371 static struct snd_soc_dai_link kabylake_rt5660_dais[] = {
0372
0373 [KBL_DPCM_AUDIO_PB] = {
0374 .name = "Kbl Audio Port",
0375 .stream_name = "Audio",
0376 .dynamic = 1,
0377 .nonatomic = 1,
0378 .trigger = {
0379 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0380 .dpcm_playback = 1,
0381 .ops = &kabylake_rt5660_fe_ops,
0382 SND_SOC_DAILINK_REG(system, dummy, platform),
0383 },
0384 [KBL_DPCM_AUDIO_CP] = {
0385 .name = "Kbl Audio Capture Port",
0386 .stream_name = "Audio Record",
0387 .dynamic = 1,
0388 .nonatomic = 1,
0389 .trigger = {
0390 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0391 .dpcm_capture = 1,
0392 .ops = &kabylake_rt5660_fe_ops,
0393 SND_SOC_DAILINK_REG(system, dummy, platform),
0394 },
0395 [KBL_DPCM_AUDIO_HDMI1_PB] = {
0396 .name = "Kbl HDMI Port1",
0397 .stream_name = "Hdmi1",
0398 .dpcm_playback = 1,
0399 .init = NULL,
0400 .trigger = {
0401 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0402 .nonatomic = 1,
0403 .dynamic = 1,
0404 SND_SOC_DAILINK_REG(hdmi1, dummy, platform),
0405 },
0406 [KBL_DPCM_AUDIO_HDMI2_PB] = {
0407 .name = "Kbl HDMI Port2",
0408 .stream_name = "Hdmi2",
0409 .dpcm_playback = 1,
0410 .init = NULL,
0411 .trigger = {
0412 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0413 .nonatomic = 1,
0414 .dynamic = 1,
0415 SND_SOC_DAILINK_REG(hdmi2, dummy, platform),
0416 },
0417 [KBL_DPCM_AUDIO_HDMI3_PB] = {
0418 .name = "Kbl HDMI Port3",
0419 .stream_name = "Hdmi3",
0420 .trigger = {
0421 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0422 .dpcm_playback = 1,
0423 .init = NULL,
0424 .nonatomic = 1,
0425 .dynamic = 1,
0426 SND_SOC_DAILINK_REG(hdmi3, dummy, platform),
0427 },
0428
0429
0430 {
0431
0432 .name = "SSP0-Codec",
0433 .id = 0,
0434 .no_pcm = 1,
0435 .init = kabylake_rt5660_codec_init,
0436 .exit = kabylake_rt5660_codec_exit,
0437 .dai_fmt = SND_SOC_DAIFMT_I2S |
0438 SND_SOC_DAIFMT_NB_NF |
0439 SND_SOC_DAIFMT_CBC_CFC,
0440 .ignore_pmdown_time = 1,
0441 .be_hw_params_fixup = kabylake_ssp0_fixup,
0442 .ops = &kabylake_rt5660_ops,
0443 .dpcm_playback = 1,
0444 .dpcm_capture = 1,
0445 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
0446 },
0447 {
0448 .name = "iDisp1",
0449 .id = 1,
0450 .dpcm_playback = 1,
0451 .init = kabylake_hdmi1_init,
0452 .no_pcm = 1,
0453 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
0454 },
0455 {
0456 .name = "iDisp2",
0457 .id = 2,
0458 .init = kabylake_hdmi2_init,
0459 .dpcm_playback = 1,
0460 .no_pcm = 1,
0461 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
0462 },
0463 {
0464 .name = "iDisp3",
0465 .id = 3,
0466 .init = kabylake_hdmi3_init,
0467 .dpcm_playback = 1,
0468 .no_pcm = 1,
0469 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
0470 },
0471 };
0472
0473
0474 #define NAME_SIZE 32
0475 static int kabylake_card_late_probe(struct snd_soc_card *card)
0476 {
0477 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(card);
0478 struct kbl_hdmi_pcm *pcm;
0479 struct snd_soc_component *component = NULL;
0480 int err, i = 0;
0481 char jack_name[NAME_SIZE];
0482
0483 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
0484 component = pcm->codec_dai->component;
0485 snprintf(jack_name, sizeof(jack_name),
0486 "HDMI/DP, pcm=%d Jack", pcm->device);
0487 err = snd_soc_card_jack_new(card, jack_name,
0488 SND_JACK_AVOUT, &skylake_hdmi[i]);
0489
0490 if (err)
0491 return err;
0492
0493 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
0494 &skylake_hdmi[i]);
0495 if (err < 0)
0496 return err;
0497
0498 i++;
0499
0500 }
0501
0502 if (!component)
0503 return -EINVAL;
0504
0505 return hdac_hdmi_jack_port_init(component, &card->dapm);
0506 }
0507
0508
0509 static struct snd_soc_card kabylake_audio_card_rt5660 = {
0510 .name = "kblrt5660",
0511 .owner = THIS_MODULE,
0512 .dai_link = kabylake_rt5660_dais,
0513 .num_links = ARRAY_SIZE(kabylake_rt5660_dais),
0514 .controls = kabylake_rt5660_controls,
0515 .num_controls = ARRAY_SIZE(kabylake_rt5660_controls),
0516 .dapm_widgets = kabylake_rt5660_widgets,
0517 .num_dapm_widgets = ARRAY_SIZE(kabylake_rt5660_widgets),
0518 .dapm_routes = kabylake_rt5660_map,
0519 .num_dapm_routes = ARRAY_SIZE(kabylake_rt5660_map),
0520 .fully_routed = true,
0521 .late_probe = kabylake_card_late_probe,
0522 };
0523
0524 static int kabylake_audio_probe(struct platform_device *pdev)
0525 {
0526 struct kbl_codec_private *ctx;
0527
0528 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
0529 if (!ctx)
0530 return -ENOMEM;
0531
0532 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
0533
0534 kabylake_audio_card =
0535 (struct snd_soc_card *)pdev->id_entry->driver_data;
0536
0537 kabylake_audio_card->dev = &pdev->dev;
0538 snd_soc_card_set_drvdata(kabylake_audio_card, ctx);
0539 return devm_snd_soc_register_card(&pdev->dev, kabylake_audio_card);
0540 }
0541
0542 static const struct platform_device_id kbl_board_ids[] = {
0543 {
0544 .name = "kbl_rt5660",
0545 .driver_data =
0546 (kernel_ulong_t)&kabylake_audio_card_rt5660,
0547 },
0548 { }
0549 };
0550 MODULE_DEVICE_TABLE(platform, kbl_board_ids);
0551
0552 static struct platform_driver kabylake_audio = {
0553 .probe = kabylake_audio_probe,
0554 .driver = {
0555 .name = "kbl_rt5660",
0556 .pm = &snd_soc_pm_ops,
0557 },
0558 .id_table = kbl_board_ids,
0559 };
0560
0561 module_platform_driver(kabylake_audio)
0562
0563
0564 MODULE_DESCRIPTION("Audio Machine driver-RT5660 in I2S mode");
0565 MODULE_AUTHOR("Hui Wang <hui.wang@canonical.com>");
0566 MODULE_LICENSE("GPL v2");