2008年04月03日

Ubuntu入れたTypeGでへッドホン挿すと内蔵スピーカが自動で切れるようになるALSAパッチ

TypeGのオンボードサウンドはICH7M+ALC262なんですが、
内部結線がちょい独特なもんで、

/etc/modprobe.d/alsa-base
(省略)
options snd-hda-intel index=0 model=basic
の設定でしか使えない。しかもこれだとヘッドホン挿しても、
内蔵スピーカが自動で切れず、両方から音がする。

激しくイライラしたんで、alsa-driver-1.0.16を自前で弄ってmodelを追加しました。

alsa-driver-1.0.16を公式から落として、
/usr/src/alsa/alsa-driver-1.0.16/alsa-kernel/pci/hda/patch_realtek.c
に追記に書いたパッチを当ててmakeすると、
以下の設定のみでヘッドホン=内蔵スピーカの自動切換ができる筈です。
(省略)
options snd-hda-intel index=0
直接model指定するならこう。
(省略)
options snd-hda-intel index=0 model=sony-vgn-g1
Intelチップセット+ALC262でモノラルスピーカなノートPCなら、
この設定でうまいこと動くやも。

patch_realtek.diff
*** patch_realtek_orig.c	2008-02-05 18:23:24.000000000 +0900
--- patch_realtek.c	2008-04-03 10:47:12.000000000 +0900
***************
*** 95,100 ****
--- 95,101 ----
  	ALC262_HP_RP5700,
  	ALC262_BENQ_ED8,
  	ALC262_SONY_ASSAMD,
+ 	ALC262_SONY_VGN_G1,
  	ALC262_BENQ_T31,
  	ALC262_ULTRA,
  	ALC262_AUTO,
***************
*** 8399,8404 ****
--- 8400,8426 ----
  	{ } /* end */
  };
  
+ static struct snd_kcontrol_new alc262_sony_vgn_g1_mixer[] = {
+ 	HDA_CODEC_VOLUME_MONO("Front Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
+ 	HDA_CODEC_MUTE_MONO("Front Playback Switch", 0x16, 2, 0x0, HDA_OUTPUT),
+ /*	HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
+ 	HDA_CODEC_MUTE("Front Playback Switch", 0x14, 0x0, HDA_OUTPUT),
+ 	HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
+ 	HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
+ 	HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
+ 	HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),*/
+ 	HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
+ 	HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
+ 	HDA_CODEC_VOLUME("Mic Boost", 0x18, 0, HDA_INPUT),
+ /*	HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x01, HDA_INPUT),
+ 	HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x01, HDA_INPUT),
+ 	HDA_CODEC_VOLUME("Front Mic Boost", 0x19, 0, HDA_INPUT),*/
+ 	HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0D, 0x0, HDA_OUTPUT),
+ 	HDA_CODEC_MUTE("Headphone Playback Switch", 0x15, 0x0, HDA_OUTPUT),
+ 	{ } /* end */
+ };
+ 
+ 
  static struct snd_kcontrol_new alc262_benq_t31_mixer[] = {
  	HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
  	HDA_CODEC_MUTE("Front Playback Switch", 0x14, 0x0, HDA_OUTPUT),
***************
*** 8438,8444 ****
  	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
  	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
  	{0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
- 
  	/*
  	 * Set up output mixers (0x0c - 0x0e)
  	 */
--- 8460,8465 ----
***************
*** 8517,8522 ****
--- 8538,8557 ----
  	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
  };
  
+ static struct hda_verb alc262_sony_vgn_g1_unsol_verbs[] = {
+ 	{0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0},
+ 	{0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
+ 	{0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
+ 	{}
+ };
+ 
+ static struct hda_input_mux alc262_sony_vgn_g1_capture_source = {
+ 	.num_items = 1,
+ 	.items = {
+ 		{ "Mic", 0x0 },
+ 	},
+ };
+ 
  /* mute/unmute internal speaker according to the hp jack and mute state */
  static void alc262_hippo_automute(struct hda_codec *codec)
  {
***************
*** 8580,8585 ****
--- 8615,8652 ----
  	alc262_hippo1_automute(codec);
  }
  
+ /* mute/unmute sony VGN-G1  0x16 internal speaker  0x15 headphone */
+ static void alc262_sony_vgn_g1_automute(struct hda_codec *codec)
+ {
+ 	struct alc_spec *spec = codec->spec;
+ 	unsigned int mute;
+ 	unsigned int present;
+ 
+ 	/* need to execute and sync at first */
+ 	snd_hda_codec_read(codec, 0x15, 0, AC_VERB_SET_PIN_SENSE, 0);
+ 	present = snd_hda_codec_read(codec, 0x15, 0,
+ 				     AC_VERB_GET_PIN_SENSE, 0);
+ 	spec->jack_present = (present & 0x80000000) != 0;
+ 	if (spec->jack_present) {
+ 		/* mute internal speaker */
+ 		snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
+ 					 HDA_AMP_MUTE, HDA_AMP_MUTE);
+ 	} else {
+ 		/* unmute internal speaker if necessary */
+ 		mute = snd_hda_codec_amp_read(codec, 0x15, 0, HDA_OUTPUT, 0);
+ 		snd_hda_codec_amp_stereo(codec, 0x16, HDA_OUTPUT, 0,
+ 					 HDA_AMP_MUTE, mute);
+ 	}
+ }
+ 
+ static void alc262_sony_vgn_g1_unsol_event(struct hda_codec *codec,
+ 				       unsigned int res)
+ {
+ 	if ((res >> 26) != ALC880_HP_EVENT)
+ 		return;
+ 	alc262_sony_vgn_g1_automute(codec);
+ }
+ 
  /*
   * fujitsu model
   *  0x14 = headphone/spdif-out, 0x15 = internal speaker
***************
*** 9202,9207 ****
--- 9269,9275 ----
  	[ALC262_BENQ_ED8]	= "benq",
  	[ALC262_BENQ_T31]	= "benq-t31",
  	[ALC262_SONY_ASSAMD]	= "sony-assamd",
+ 	[ALC262_SONY_VGN_G1]	= "sony-vgn-g1",
  	[ALC262_ULTRA]		= "ultra",
  	[ALC262_AUTO]		= "auto",
  };
***************
*** 9232,9237 ****
--- 9300,9306 ----
  	SND_PCI_QUIRK(0x103c, 0x2817, "HP RP5700", ALC262_HP_RP5700),
  	SND_PCI_QUIRK(0x104d, 0x1f00, "Sony ASSAMD", ALC262_SONY_ASSAMD),
  	SND_PCI_QUIRK(0x104d, 0x8203, "Sony UX-90", ALC262_HIPPO),
+ 	SND_PCI_QUIRK(0x104d, 0x8208, "Sony VGN-G1", ALC262_SONY_VGN_G1),
  	SND_PCI_QUIRK(0x104d, 0x820f, "Sony ASSAMD", ALC262_SONY_ASSAMD),
  	SND_PCI_QUIRK(0x104d, 0x900e, "Sony ASSAMD", ALC262_SONY_ASSAMD),
  	SND_PCI_QUIRK(0x104d, 0x9015, "Sony 0x9015", ALC262_SONY_ASSAMD),
***************
*** 9373,9378 ****
--- 9442,9459 ----
  		.unsol_event = alc262_hippo_unsol_event,
  		.init_hook = alc262_hippo_automute,
  	},
+ 	[ALC262_SONY_VGN_G1] = {
+ 		.mixers = { alc262_sony_vgn_g1_mixer },
+ 		.init_verbs = { alc262_init_verbs, alc262_sony_vgn_g1_unsol_verbs },
+ 		.num_dacs = ARRAY_SIZE(alc262_dac_nids),
+ 		.dac_nids = alc262_dac_nids,
+ 		.hp_nid = 0x03,
+ 		.num_channel_mode = ARRAY_SIZE(alc262_modes),
+ 		.channel_mode = alc262_modes,
+ 		.input_mux = &alc262_sony_vgn_g1_capture_source,
+ 		.unsol_event = alc262_sony_vgn_g1_unsol_event,
+ 		.init_hook = alc262_sony_vgn_g1_automute,
+ 	},
  	[ALC262_BENQ_T31] = {
  		.mixers = { alc262_benq_t31_mixer },
  		.init_verbs = { alc262_init_verbs, alc262_benq_t31_EAPD_verbs, alc262_hippo_unsol_verbs },