alc_quirks.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Common codes for Realtek codec quirks
  3. * included by patch_realtek.c
  4. */
  5. /*
  6. * configuration template - to be copied to the spec instance
  7. */
  8. struct alc_config_preset {
  9. const struct snd_kcontrol_new *mixers[5]; /* should be identical size
  10. * with spec
  11. */
  12. const struct snd_kcontrol_new *cap_mixer; /* capture mixer */
  13. const struct hda_verb *init_verbs[5];
  14. unsigned int num_dacs;
  15. const hda_nid_t *dac_nids;
  16. hda_nid_t dig_out_nid; /* optional */
  17. hda_nid_t hp_nid; /* optional */
  18. const hda_nid_t *slave_dig_outs;
  19. unsigned int num_adc_nids;
  20. const hda_nid_t *adc_nids;
  21. const hda_nid_t *capsrc_nids;
  22. hda_nid_t dig_in_nid;
  23. unsigned int num_channel_mode;
  24. const struct hda_channel_mode *channel_mode;
  25. int need_dac_fix;
  26. int const_channel_count;
  27. unsigned int num_mux_defs;
  28. const struct hda_input_mux *input_mux;
  29. void (*unsol_event)(struct hda_codec *, unsigned int);
  30. void (*setup)(struct hda_codec *);
  31. void (*init_hook)(struct hda_codec *);
  32. #ifdef CONFIG_SND_HDA_POWER_SAVE
  33. const struct hda_amp_list *loopbacks;
  34. void (*power_hook)(struct hda_codec *codec);
  35. #endif
  36. };
  37. /*
  38. * channel mode setting
  39. */
  40. static int alc_ch_mode_info(struct snd_kcontrol *kcontrol,
  41. struct snd_ctl_elem_info *uinfo)
  42. {
  43. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  44. struct alc_spec *spec = codec->spec;
  45. return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
  46. spec->num_channel_mode);
  47. }
  48. static int alc_ch_mode_get(struct snd_kcontrol *kcontrol,
  49. struct snd_ctl_elem_value *ucontrol)
  50. {
  51. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  52. struct alc_spec *spec = codec->spec;
  53. return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
  54. spec->num_channel_mode,
  55. spec->ext_channel_count);
  56. }
  57. static int alc_ch_mode_put(struct snd_kcontrol *kcontrol,
  58. struct snd_ctl_elem_value *ucontrol)
  59. {
  60. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  61. struct alc_spec *spec = codec->spec;
  62. int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
  63. spec->num_channel_mode,
  64. &spec->ext_channel_count);
  65. if (err >= 0 && !spec->const_channel_count) {
  66. spec->multiout.max_channels = spec->ext_channel_count;
  67. if (spec->need_dac_fix)
  68. spec->multiout.num_dacs = spec->multiout.max_channels / 2;
  69. }
  70. return err;
  71. }
  72. /*
  73. * Control the mode of pin widget settings via the mixer. "pc" is used
  74. * instead of "%" to avoid consequences of accidentally treating the % as
  75. * being part of a format specifier. Maximum allowed length of a value is
  76. * 63 characters plus NULL terminator.
  77. *
  78. * Note: some retasking pin complexes seem to ignore requests for input
  79. * states other than HiZ (eg: PIN_VREFxx) and revert to HiZ if any of these
  80. * are requested. Therefore order this list so that this behaviour will not
  81. * cause problems when mixer clients move through the enum sequentially.
  82. * NIDs 0x0f and 0x10 have been observed to have this behaviour as of
  83. * March 2006.
  84. */
  85. static const char * const alc_pin_mode_names[] = {
  86. "Mic 50pc bias", "Mic 80pc bias",
  87. "Line in", "Line out", "Headphone out",
  88. };
  89. static const unsigned char alc_pin_mode_values[] = {
  90. PIN_VREF50, PIN_VREF80, PIN_IN, PIN_OUT, PIN_HP,
  91. };
  92. /* The control can present all 5 options, or it can limit the options based
  93. * in the pin being assumed to be exclusively an input or an output pin. In
  94. * addition, "input" pins may or may not process the mic bias option
  95. * depending on actual widget capability (NIDs 0x0f and 0x10 don't seem to
  96. * accept requests for bias as of chip versions up to March 2006) and/or
  97. * wiring in the computer.
  98. */
  99. #define ALC_PIN_DIR_IN 0x00
  100. #define ALC_PIN_DIR_OUT 0x01
  101. #define ALC_PIN_DIR_INOUT 0x02
  102. #define ALC_PIN_DIR_IN_NOMICBIAS 0x03
  103. #define ALC_PIN_DIR_INOUT_NOMICBIAS 0x04
  104. /* Info about the pin modes supported by the different pin direction modes.
  105. * For each direction the minimum and maximum values are given.
  106. */
  107. static const signed char alc_pin_mode_dir_info[5][2] = {
  108. { 0, 2 }, /* ALC_PIN_DIR_IN */
  109. { 3, 4 }, /* ALC_PIN_DIR_OUT */
  110. { 0, 4 }, /* ALC_PIN_DIR_INOUT */
  111. { 2, 2 }, /* ALC_PIN_DIR_IN_NOMICBIAS */
  112. { 2, 4 }, /* ALC_PIN_DIR_INOUT_NOMICBIAS */
  113. };
  114. #define alc_pin_mode_min(_dir) (alc_pin_mode_dir_info[_dir][0])
  115. #define alc_pin_mode_max(_dir) (alc_pin_mode_dir_info[_dir][1])
  116. #define alc_pin_mode_n_items(_dir) \
  117. (alc_pin_mode_max(_dir)-alc_pin_mode_min(_dir)+1)
  118. static int alc_pin_mode_info(struct snd_kcontrol *kcontrol,
  119. struct snd_ctl_elem_info *uinfo)
  120. {
  121. unsigned int item_num = uinfo->value.enumerated.item;
  122. unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
  123. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  124. uinfo->count = 1;
  125. uinfo->value.enumerated.items = alc_pin_mode_n_items(dir);
  126. if (item_num<alc_pin_mode_min(dir) || item_num>alc_pin_mode_max(dir))
  127. item_num = alc_pin_mode_min(dir);
  128. strcpy(uinfo->value.enumerated.name, alc_pin_mode_names[item_num]);
  129. return 0;
  130. }
  131. static int alc_pin_mode_get(struct snd_kcontrol *kcontrol,
  132. struct snd_ctl_elem_value *ucontrol)
  133. {
  134. unsigned int i;
  135. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  136. hda_nid_t nid = kcontrol->private_value & 0xffff;
  137. unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
  138. long *valp = ucontrol->value.integer.value;
  139. unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
  140. AC_VERB_GET_PIN_WIDGET_CONTROL,
  141. 0x00);
  142. /* Find enumerated value for current pinctl setting */
  143. i = alc_pin_mode_min(dir);
  144. while (i <= alc_pin_mode_max(dir) && alc_pin_mode_values[i] != pinctl)
  145. i++;
  146. *valp = i <= alc_pin_mode_max(dir) ? i: alc_pin_mode_min(dir);
  147. return 0;
  148. }
  149. static int alc_pin_mode_put(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. signed int change;
  153. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  154. hda_nid_t nid = kcontrol->private_value & 0xffff;
  155. unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
  156. long val = *ucontrol->value.integer.value;
  157. unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
  158. AC_VERB_GET_PIN_WIDGET_CONTROL,
  159. 0x00);
  160. if (val < alc_pin_mode_min(dir) || val > alc_pin_mode_max(dir))
  161. val = alc_pin_mode_min(dir);
  162. change = pinctl != alc_pin_mode_values[val];
  163. if (change) {
  164. /* Set pin mode to that requested */
  165. snd_hda_codec_write_cache(codec, nid, 0,
  166. AC_VERB_SET_PIN_WIDGET_CONTROL,
  167. alc_pin_mode_values[val]);
  168. /* Also enable the retasking pin's input/output as required
  169. * for the requested pin mode. Enum values of 2 or less are
  170. * input modes.
  171. *
  172. * Dynamically switching the input/output buffers probably
  173. * reduces noise slightly (particularly on input) so we'll
  174. * do it. However, having both input and output buffers
  175. * enabled simultaneously doesn't seem to be problematic if
  176. * this turns out to be necessary in the future.
  177. */
  178. if (val <= 2) {
  179. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  180. HDA_AMP_MUTE, HDA_AMP_MUTE);
  181. snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
  182. HDA_AMP_MUTE, 0);
  183. } else {
  184. snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
  185. HDA_AMP_MUTE, HDA_AMP_MUTE);
  186. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  187. HDA_AMP_MUTE, 0);
  188. }
  189. }
  190. return change;
  191. }
  192. #define ALC_PIN_MODE(xname, nid, dir) \
  193. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
  194. .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
  195. .info = alc_pin_mode_info, \
  196. .get = alc_pin_mode_get, \
  197. .put = alc_pin_mode_put, \
  198. .private_value = nid | (dir<<16) }
  199. /* A switch control for ALC260 GPIO pins. Multiple GPIOs can be ganged
  200. * together using a mask with more than one bit set. This control is
  201. * currently used only by the ALC260 test model. At this stage they are not
  202. * needed for any "production" models.
  203. */
  204. #ifdef CONFIG_SND_DEBUG
  205. #define alc_gpio_data_info snd_ctl_boolean_mono_info
  206. static int alc_gpio_data_get(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *ucontrol)
  208. {
  209. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  210. hda_nid_t nid = kcontrol->private_value & 0xffff;
  211. unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
  212. long *valp = ucontrol->value.integer.value;
  213. unsigned int val = snd_hda_codec_read(codec, nid, 0,
  214. AC_VERB_GET_GPIO_DATA, 0x00);
  215. *valp = (val & mask) != 0;
  216. return 0;
  217. }
  218. static int alc_gpio_data_put(struct snd_kcontrol *kcontrol,
  219. struct snd_ctl_elem_value *ucontrol)
  220. {
  221. signed int change;
  222. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  223. hda_nid_t nid = kcontrol->private_value & 0xffff;
  224. unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
  225. long val = *ucontrol->value.integer.value;
  226. unsigned int gpio_data = snd_hda_codec_read(codec, nid, 0,
  227. AC_VERB_GET_GPIO_DATA,
  228. 0x00);
  229. /* Set/unset the masked GPIO bit(s) as needed */
  230. change = (val == 0 ? 0 : mask) != (gpio_data & mask);
  231. if (val == 0)
  232. gpio_data &= ~mask;
  233. else
  234. gpio_data |= mask;
  235. snd_hda_codec_write_cache(codec, nid, 0,
  236. AC_VERB_SET_GPIO_DATA, gpio_data);
  237. return change;
  238. }
  239. #define ALC_GPIO_DATA_SWITCH(xname, nid, mask) \
  240. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
  241. .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
  242. .info = alc_gpio_data_info, \
  243. .get = alc_gpio_data_get, \
  244. .put = alc_gpio_data_put, \
  245. .private_value = nid | (mask<<16) }
  246. #endif /* CONFIG_SND_DEBUG */
  247. /* A switch control to allow the enabling of the digital IO pins on the
  248. * ALC260. This is incredibly simplistic; the intention of this control is
  249. * to provide something in the test model allowing digital outputs to be
  250. * identified if present. If models are found which can utilise these
  251. * outputs a more complete mixer control can be devised for those models if
  252. * necessary.
  253. */
  254. #ifdef CONFIG_SND_DEBUG
  255. #define alc_spdif_ctrl_info snd_ctl_boolean_mono_info
  256. static int alc_spdif_ctrl_get(struct snd_kcontrol *kcontrol,
  257. struct snd_ctl_elem_value *ucontrol)
  258. {
  259. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  260. hda_nid_t nid = kcontrol->private_value & 0xffff;
  261. unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
  262. long *valp = ucontrol->value.integer.value;
  263. unsigned int val = snd_hda_codec_read(codec, nid, 0,
  264. AC_VERB_GET_DIGI_CONVERT_1, 0x00);
  265. *valp = (val & mask) != 0;
  266. return 0;
  267. }
  268. static int alc_spdif_ctrl_put(struct snd_kcontrol *kcontrol,
  269. struct snd_ctl_elem_value *ucontrol)
  270. {
  271. signed int change;
  272. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  273. hda_nid_t nid = kcontrol->private_value & 0xffff;
  274. unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
  275. long val = *ucontrol->value.integer.value;
  276. unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
  277. AC_VERB_GET_DIGI_CONVERT_1,
  278. 0x00);
  279. /* Set/unset the masked control bit(s) as needed */
  280. change = (val == 0 ? 0 : mask) != (ctrl_data & mask);
  281. if (val==0)
  282. ctrl_data &= ~mask;
  283. else
  284. ctrl_data |= mask;
  285. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
  286. ctrl_data);
  287. return change;
  288. }
  289. #define ALC_SPDIF_CTRL_SWITCH(xname, nid, mask) \
  290. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
  291. .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
  292. .info = alc_spdif_ctrl_info, \
  293. .get = alc_spdif_ctrl_get, \
  294. .put = alc_spdif_ctrl_put, \
  295. .private_value = nid | (mask<<16) }
  296. #endif /* CONFIG_SND_DEBUG */
  297. /* A switch control to allow the enabling EAPD digital outputs on the ALC26x.
  298. * Again, this is only used in the ALC26x test models to help identify when
  299. * the EAPD line must be asserted for features to work.
  300. */
  301. #ifdef CONFIG_SND_DEBUG
  302. #define alc_eapd_ctrl_info snd_ctl_boolean_mono_info
  303. static int alc_eapd_ctrl_get(struct snd_kcontrol *kcontrol,
  304. struct snd_ctl_elem_value *ucontrol)
  305. {
  306. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  307. hda_nid_t nid = kcontrol->private_value & 0xffff;
  308. unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
  309. long *valp = ucontrol->value.integer.value;
  310. unsigned int val = snd_hda_codec_read(codec, nid, 0,
  311. AC_VERB_GET_EAPD_BTLENABLE, 0x00);
  312. *valp = (val & mask) != 0;
  313. return 0;
  314. }
  315. static int alc_eapd_ctrl_put(struct snd_kcontrol *kcontrol,
  316. struct snd_ctl_elem_value *ucontrol)
  317. {
  318. int change;
  319. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  320. hda_nid_t nid = kcontrol->private_value & 0xffff;
  321. unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
  322. long val = *ucontrol->value.integer.value;
  323. unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
  324. AC_VERB_GET_EAPD_BTLENABLE,
  325. 0x00);
  326. /* Set/unset the masked control bit(s) as needed */
  327. change = (!val ? 0 : mask) != (ctrl_data & mask);
  328. if (!val)
  329. ctrl_data &= ~mask;
  330. else
  331. ctrl_data |= mask;
  332. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
  333. ctrl_data);
  334. return change;
  335. }
  336. #define ALC_EAPD_CTRL_SWITCH(xname, nid, mask) \
  337. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0, \
  338. .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
  339. .info = alc_eapd_ctrl_info, \
  340. .get = alc_eapd_ctrl_get, \
  341. .put = alc_eapd_ctrl_put, \
  342. .private_value = nid | (mask<<16) }
  343. #endif /* CONFIG_SND_DEBUG */
  344. static void alc_fixup_autocfg_pin_nums(struct hda_codec *codec)
  345. {
  346. struct alc_spec *spec = codec->spec;
  347. struct auto_pin_cfg *cfg = &spec->autocfg;
  348. if (!cfg->line_outs) {
  349. while (cfg->line_outs < AUTO_CFG_MAX_OUTS &&
  350. cfg->line_out_pins[cfg->line_outs])
  351. cfg->line_outs++;
  352. }
  353. if (!cfg->speaker_outs) {
  354. while (cfg->speaker_outs < AUTO_CFG_MAX_OUTS &&
  355. cfg->speaker_pins[cfg->speaker_outs])
  356. cfg->speaker_outs++;
  357. }
  358. if (!cfg->hp_outs) {
  359. while (cfg->hp_outs < AUTO_CFG_MAX_OUTS &&
  360. cfg->hp_pins[cfg->hp_outs])
  361. cfg->hp_outs++;
  362. }
  363. }
  364. /*
  365. * set up from the preset table
  366. */
  367. static void setup_preset(struct hda_codec *codec,
  368. const struct alc_config_preset *preset)
  369. {
  370. struct alc_spec *spec = codec->spec;
  371. int i;
  372. for (i = 0; i < ARRAY_SIZE(preset->mixers) && preset->mixers[i]; i++)
  373. add_mixer(spec, preset->mixers[i]);
  374. spec->cap_mixer = preset->cap_mixer;
  375. for (i = 0; i < ARRAY_SIZE(preset->init_verbs) && preset->init_verbs[i];
  376. i++)
  377. add_verb(spec, preset->init_verbs[i]);
  378. spec->channel_mode = preset->channel_mode;
  379. spec->num_channel_mode = preset->num_channel_mode;
  380. spec->need_dac_fix = preset->need_dac_fix;
  381. spec->const_channel_count = preset->const_channel_count;
  382. if (preset->const_channel_count)
  383. spec->multiout.max_channels = preset->const_channel_count;
  384. else
  385. spec->multiout.max_channels = spec->channel_mode[0].channels;
  386. spec->ext_channel_count = spec->channel_mode[0].channels;
  387. spec->multiout.num_dacs = preset->num_dacs;
  388. spec->multiout.dac_nids = preset->dac_nids;
  389. spec->multiout.dig_out_nid = preset->dig_out_nid;
  390. spec->multiout.slave_dig_outs = preset->slave_dig_outs;
  391. spec->multiout.hp_nid = preset->hp_nid;
  392. spec->num_mux_defs = preset->num_mux_defs;
  393. if (!spec->num_mux_defs)
  394. spec->num_mux_defs = 1;
  395. spec->input_mux = preset->input_mux;
  396. spec->num_adc_nids = preset->num_adc_nids;
  397. spec->adc_nids = preset->adc_nids;
  398. spec->capsrc_nids = preset->capsrc_nids;
  399. spec->dig_in_nid = preset->dig_in_nid;
  400. spec->unsol_event = preset->unsol_event;
  401. spec->init_hook = preset->init_hook;
  402. #ifdef CONFIG_SND_HDA_POWER_SAVE
  403. spec->power_hook = preset->power_hook;
  404. spec->loopback.amplist = preset->loopbacks;
  405. #endif
  406. if (preset->setup)
  407. preset->setup(codec);
  408. alc_fixup_autocfg_pin_nums(codec);
  409. }
  410. /* auto-toggle front mic */
  411. static void alc88x_simple_mic_automute(struct hda_codec *codec)
  412. {
  413. unsigned int present;
  414. unsigned char bits;
  415. present = snd_hda_jack_detect(codec, 0x18);
  416. bits = present ? HDA_AMP_MUTE : 0;
  417. snd_hda_codec_amp_stereo(codec, 0x0b, HDA_INPUT, 1, HDA_AMP_MUTE, bits);
  418. }